From 368674363c817d3153b05becfa4832df2c178a98 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 22 May 2024 15:43:46 +0200 Subject: [PATCH 1/3] bitcast: skip setName on folded inputs --- src/intrinsics.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/intrinsics.cpp b/src/intrinsics.cpp index 633024b5786a3..172f1a4b66025 100644 --- a/src/intrinsics.cpp +++ b/src/intrinsics.cpp @@ -640,7 +640,9 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, ArrayRef argv) setName(ctx.emission_context, vx, "bitcast_coercion"); } else { vx = emit_bitcast(ctx, vx, llvmt); - setName(ctx.emission_context, vx, "bitcast_coercion"); + if (isa(vx) && !vx->hasName()) + // emit_bitcast may undo another bitcast + setName(ctx.emission_context, vx, "bitcast_coercion"); } } From 2f9e739e7c8a58c75d0638dec061aea5e6db27bd Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 22 May 2024 16:21:39 +0200 Subject: [PATCH 2/3] bitcast: ensure the target pointer address space is used. --- src/intrinsics.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/intrinsics.cpp b/src/intrinsics.cpp index 172f1a4b66025..d6d5ff837d79e 100644 --- a/src/intrinsics.cpp +++ b/src/intrinsics.cpp @@ -638,6 +638,16 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, ArrayRef argv) if (isa(vx) && !vx->hasName()) // emit_inttoptr may undo an PtrToInt setName(ctx.emission_context, vx, "bitcast_coercion"); + } else if (vxt->isPointerTy() && llvmt->isPointerTy()) { + // emit_bitcast preserves the origin address space, which we can't have here + #if JL_LLVM_VERSION >= 170000 + vx = ctx.builder.CreateAddrSpaceCast(vx, llvmt); + #else + vx = ctx.builder.CreatePointerBitCastOrAddrSpaceCast(vx, llvmt); + #endif + if (isa(vx) && !vx->hasName()) + // cast may have been folded + setName(ctx.emission_context, vx, "bitcast_coercion"); } else { vx = emit_bitcast(ctx, vx, llvmt); if (isa(vx) && !vx->hasName()) From 47766075cc903a3563173a6c461f7bc03ff233ba Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 22 May 2024 16:32:21 +0200 Subject: [PATCH 3/3] Add test. --- test/intrinsics.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/intrinsics.jl b/test/intrinsics.jl index 6fbe4e5364354..e61354fe4f7f3 100644 --- a/test/intrinsics.jl +++ b/test/intrinsics.jl @@ -345,3 +345,16 @@ Base.show(io::IO, a::IntWrap) = print(io, "IntWrap(", a.x, ")") @test r2 isa IntWrap && r2.x === 103 === r[].x && r2 !== r[] end end)() + +@testset "issue #54548" begin + @inline passthrough(ptr::Core.LLVMPtr{T,A}) where {T,A} = Base.llvmcall((""" + define ptr addrspace(1) @entry(ptr addrspace(1) %0) #0 { + entry: + ret ptr addrspace(1) %0 + } + + attributes #0 = { alwaysinline }""", "entry"), + Core.LLVMPtr{T,A}, Tuple{Core.LLVMPtr{T,A}}, ptr) + f(gws) = passthrough(Core.bitcast(Core.LLVMPtr{UInt32,1}, gws)) + f(C_NULL) +end