From c1bcf3f59b26e741fce5f1274c01684802830cfd Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Sat, 12 Sep 2026 19:41:18 +0200 Subject: [PATCH 1/2] Do not emit aliasscope markers on Julia 1.11 and later Julia miscompiles code placed inside an `aliasscope` (JuliaLang/julia#63129), so on 1.11 and later `@kernel` no longer emits the `aliasscope` / `popaliasscope` markers for kernels that use `@Const` arguments. `@Const` arguments are still wrapped in `Base.Experimental.Const`, they just no longer participate in alias-scope based optimizations. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01Be2cuG5qj1BfmHFnsKADNZ --- src/macros.jl | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index 2da784577..bf745fac3 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -78,6 +78,12 @@ function __kernel(expr, generate_cpu = true, force_inbounds = false, unsafe_indi end end +# Julia 1.11 and later miscompile code placed inside an `aliasscope`, even when the +# kernel makes no use of `@Const`, so the markers are only emitted on older versions. +# See https://github.com/JuliaGPU/KernelAbstractions.jl/issues/652 +# and https://github.com/JuliaLang/julia/issues/63129 +const EMIT_ALIASSCOPE = VERSION < v"1.11-" + # The easy case, transform the function for GPU execution # - mark constant arguments by applying `constify`. function transform_gpu!(def, constargs, force_inbounds, unsafe_indices) @@ -91,10 +97,8 @@ function transform_gpu!(def, constargs, force_inbounds, unsafe_indices) pushfirst!(def[:args], :__ctx__) new_stmts = Expr[] body = MacroTools.flatten(def[:body]) - # On 1.11 and later having this aliasscope causes issues - # even with kernels that don't use `@Const` on arguments - # See https://github.com/JuliaGPU/KernelAbstractions.jl/issues/652 - has_constargs && push!(new_stmts, Expr(:aliasscope)) + emit_aliasscope = has_constargs && EMIT_ALIASSCOPE + emit_aliasscope && push!(new_stmts, Expr(:aliasscope)) if !unsafe_indices push!(new_stmts, :(__active_lane__ = $__validindex(__ctx__))) end @@ -109,7 +113,7 @@ function transform_gpu!(def, constargs, force_inbounds, unsafe_indices) if force_inbounds push!(new_stmts, Expr(:inbounds, :pop)) end - has_constargs && push!(new_stmts, Expr(:popaliasscope)) + emit_aliasscope && push!(new_stmts, Expr(:popaliasscope)) push!(new_stmts, :(return nothing)) def[:body] = Expr( :let, @@ -137,10 +141,8 @@ function transform_cpu!(def, constargs, force_inbounds) pushfirst!(def[:args], :__ctx__) new_stmts = Expr[] body = MacroTools.flatten(def[:body]) - # On 1.11 and later having this aliasscope causes issues - # even with kernels that don't use `@Const` on arguments - # See https://github.com/JuliaGPU/KernelAbstractions.jl/issues/652 - has_constargs && push!(new_stmts, Expr(:aliasscope)) + emit_aliasscope = has_constargs && EMIT_ALIASSCOPE + emit_aliasscope && push!(new_stmts, Expr(:aliasscope)) if force_inbounds push!(new_stmts, Expr(:inbounds, true)) end @@ -148,7 +150,7 @@ function transform_cpu!(def, constargs, force_inbounds) if force_inbounds push!(new_stmts, Expr(:inbounds, :pop)) end - has_constargs && push!(new_stmts, Expr(:popaliasscope)) + emit_aliasscope && push!(new_stmts, Expr(:popaliasscope)) push!(new_stmts, :(return nothing)) def[:body] = Expr( :let, From 24609fb6747b6f20c6de33bb119010e59de5dd38 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Fri, 25 Sep 2026 20:02:13 +0200 Subject: [PATCH 2/2] Skip alias scope metadata test when markers are not emitted The CPU Const test checked for !alias.scope and !noalias on every Julia version. On 1.11 and later KernelAbstractions no longer emits aliasscope markers, so gate the checks on EMIT_ALIASSCOPE. Assisted-by: Claude Code (Opus 5.5) --- test/test.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/test.jl b/test/test.jl index d0d3e1d07..ee8a93be6 100644 --- a/test/test.jl +++ b/test/test.jl @@ -203,8 +203,12 @@ function unittest_testsuite(Backend, backend_str, backend_mod, BackendArrayT; sk end end if backend_str == "CPU" - @test occursin("!alias.scope", IR) - @test occursin("!noalias", IR) + if KernelAbstractions.EMIT_ALIASSCOPE + @test occursin("!alias.scope", IR) + @test occursin("!noalias", IR) + else + @test_skip false + end elseif backend_str == "CUDA" if Base.libllvm_version >= v"20" @test occursin("addrspace(1)", IR)