Skip to content

Commit f8a6c4e

Browse files
committed
Peek into constant expressions when discovering functions to add state arguments to.
1 parent e810ec6 commit f8a6c4e

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/irgen.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,11 +562,22 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
562562
# iteratively discover functions that use the intrinsic or any function calling it
563563
worklist_length = length(worklist)
564564
additions = LLVM.Function[]
565+
function check_user(val)
566+
if val isa Instruction
567+
bb = LLVM.parent(val)
568+
new_f = LLVM.parent(bb)
569+
in(new_f, worklist) || push!(additions, new_f)
570+
elseif val isa ConstantExpr
571+
# constant expressions don't have a parent; we need to look up their uses
572+
for use in uses(val)
573+
check_user(user(use))
574+
end
575+
else
576+
error("Don't know how to check uses of $val. Please file an issue.")
577+
end
578+
end
565579
for f in worklist, use in uses(f)
566-
inst = user(use)::Instruction
567-
bb = LLVM.parent(inst)
568-
new_f = LLVM.parent(bb)
569-
in(new_f, worklist) || push!(additions, new_f)
580+
check_user(user(use))
570581
end
571582
for f in additions
572583
push!(worklist, f)
@@ -604,6 +615,7 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
604615
end
605616

606617
value_map[f] = new_f
618+
# XXX: do we want this? we're adding a new arg, after all
607619
clone_into!(new_f, f; value_map,
608620
changes=LLVM.API.LLVMCloneFunctionChangeTypeGlobalChanges)
609621

@@ -613,6 +625,8 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
613625
end
614626

615627
# update other uses of the old function, modifying call sites to pass the state argument
628+
# TODO: why isn't this covered by the value mapper above? because we need to add an arg!
629+
# XXX: do this with a value mapper, and a materialize (?) to rewrite calls.
616630
function rewrite_uses!(f, new_f)
617631
# update uses
618632
Builder(ctx) do builder
@@ -636,6 +650,16 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
636650
replace_uses!(val, new_val)
637651
@assert isempty(uses(val))
638652
unsafe_delete!(LLVM.parent(val), val)
653+
elseif val isa ConstantExpr && opcode(val) == LLVM.API.LLVMPtrToInt
654+
# XXX: are these safe? we're not adding an arg
655+
# XXX: in addition, won't this RAUW assert in debug mode?
656+
replace_uses!(val, const_ptrtoint(new_f, llvmtype(val)))
657+
LLVM.unsafe_destroy!(val)
658+
elseif val isa ConstantExpr && opcode(val) == LLVM.API.LLVMBitCast
659+
# XXX: are these safe? we're not adding an arg
660+
# XXX: in addition, won't this RAUW assert in debug mode?
661+
replace_uses!(val, const_bitcast(new_f, llvmtype(val)))
662+
LLVM.unsafe_destroy!(val)
639663
else
640664
error("Cannot rewrite unknown use of function: $val")
641665
end

src/validation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ function check_ir!(job, errors::Vector{IRError}, inst::LLVM.CallInst)
224224
if !valid_function_pointer(job, ptr)
225225
# look it up in the Julia JIT cache
226226
frames = ccall(:jl_lookup_code_address, Any, (Ptr{Cvoid}, Cint,), ptr, 0)
227-
if length(frames) >= 1
228-
@compiler_assert length(frames) == 1 job frames=frames
227+
# XXX: what if multiple frames are returned? rare, but happens
228+
if length(frames) == 1
229229
fn, file, line, linfo, fromC, inlined = last(frames)
230230
push!(errors, (POINTER_FUNCTION, bt, fn))
231231
else

0 commit comments

Comments
 (0)