From 3770c71088bd2908c30728a4d21f542c905443f1 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Tue, 15 Sep 2026 12:03:14 +0200 Subject: [PATCH] Stop emitting `aliasscope` regions in `@kernel` `transform_gpu!` wrapped every kernel body in `Expr(:aliasscope)` / `Expr(:popaliasscope)`. Those tokens only do something when a load inside the region is tied to them, i.e. Base's `Const(::Array)` lowering, which emits `!alias.scope`/`!noalias` against the enclosing scope. That was the native-Julia CPU backend, replaced by POCL in #556. No current backend takes that path: POCL's `@Const` goes through `const_arrayref` on `CLDeviceArray` (`!invariant.load`), and the external GPU backends use their own device-array `Const` wrappers over `ldg`/`!invariant.load`. The region was therefore inert, while still forcing the body into a scope that blocks some code motion and showing up in the lowered IR. Drop the `ConstAdaptor`/`Array` rule along with it. `constify` runs inside the kernel, after arguments have been adapted to device arrays, so it was already unreachable -- and without an enclosing aliasscope region the resulting `Const` wrapper lowered to ordinary loads anyway. Assisted-by: Claude Code (Opus 5) --- src/KernelAbstractions.jl | 2 -- src/macros.jl | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/KernelAbstractions.jl b/src/KernelAbstractions.jl index c022c7cef..d7ca0ac8a 100644 --- a/src/KernelAbstractions.jl +++ b/src/KernelAbstractions.jl @@ -447,8 +447,6 @@ y = adapt(CPU(), x) # an Array again """ Adapt.adapt_storage(::Backend, x) -Adapt.adapt_storage(::ConstAdaptor, a::Array) = Base.Experimental.Const(a) - constify(arg) = adapt(ConstAdaptor(), arg) include("nditeration.jl") diff --git a/src/macros.jl b/src/macros.jl index bb1ca5635..d0b05e707 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -79,7 +79,6 @@ function transform_gpu!(def, constargs, force_inbounds, unsafe_indices) # `Any[]`, since `split` hands back `LineNumberNode`s alongside `Expr`s new_stmts = Any[] body = MacroTools.flatten(def[:body]) - push!(new_stmts, Expr(:aliasscope)) if !unsafe_indices push!(new_stmts, :(__active_lane__ = $__validindex(__ctx__))) end @@ -94,7 +93,6 @@ function transform_gpu!(def, constargs, force_inbounds, unsafe_indices) if force_inbounds push!(new_stmts, Expr(:inbounds, :pop)) end - push!(new_stmts, Expr(:popaliasscope)) push!(new_stmts, :(return nothing)) def[:body] = Expr( :let,