Skip to content

Commit

Permalink
Fixes for bitcast bugs with LLVM 17 / opaque pointers (#54548)
Browse files Browse the repository at this point in the history
Skip setName on folded inputs, and ensure
the correct pointer address space is used.
  • Loading branch information
maleadt authored May 22, 2024
1 parent 0437210 commit baca8ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,21 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
if (isa<Instruction>(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<Instruction>(vx) && !vx->hasName())
// cast may have been folded
setName(ctx.emission_context, vx, "bitcast_coercion");
} else {
vx = emit_bitcast(ctx, vx, llvmt);
setName(ctx.emission_context, vx, "bitcast_coercion");
if (isa<Instruction>(vx) && !vx->hasName())
// emit_bitcast may undo another bitcast
setName(ctx.emission_context, vx, "bitcast_coercion");
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/intrinsics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit baca8ba

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package evaluation job you requested has completed - possible new issues were detected.
The full report is available.

Please sign in to comment.