Skip to content

Commit fe5cf6e

Browse files
committed
Pass state argument to calls originating from ccalls.
1 parent f4e9137 commit fe5cf6e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/irgen.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
637637
end
638638
end
639639

640-
# update other uses of the old function
641-
for (f, new_f) in workmap
640+
# update other uses of the old function, modifying call sites to pass the state argument
641+
function rewrite_uses!(f, new_f)
642642
# update uses
643643
Builder(ctx) do builder
644644
for use in uses(f)
@@ -661,13 +661,28 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
661661
replace_uses!(val, new_val)
662662
@assert isempty(uses(val))
663663
unsafe_delete!(LLVM.parent(val), val)
664+
elseif val isa LLVM.ConstantExpr && opcode(val) == LLVM.API.LLVMBitCast
665+
# XXX: why isn't this caught by the value materializer above?
666+
target = operands(val)[1]
667+
@assert target == f
668+
new_val = LLVM.const_bitcast(new_f, llvmtype(val))
669+
rewrite_uses!(val, new_val)
670+
# we can't simply replace this constant expression, as it may be used
671+
# as a call, taking arguments (so we need to rewrite it to pass the state)
672+
673+
# drop the old constant if it is unused
674+
# XXX: can we do this differently?
675+
if isempty(uses(val))
676+
LLVM.unsafe_destroy!(val)
677+
end
664678
else
665679
error("Cannot rewrite unknown use of function: $val")
666680
end
667681
end
668682
end
669-
670-
# clean-up
683+
end
684+
for (f, new_f) in workmap
685+
rewrite_uses!(f, new_f)
671686
@assert isempty(uses(f))
672687
unsafe_delete!(mod, f)
673688
end

0 commit comments

Comments
 (0)