@@ -637,8 +637,8 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
637
637
end
638
638
end
639
639
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)
642
642
# update uses
643
643
Builder (ctx) do builder
644
644
for use in uses (f)
@@ -661,13 +661,28 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
661
661
replace_uses! (val, new_val)
662
662
@assert isempty (uses (val))
663
663
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
664
678
else
665
679
error (" Cannot rewrite unknown use of function: $val " )
666
680
end
667
681
end
668
682
end
669
-
670
- # clean-up
683
+ end
684
+ for (f, new_f) in workmap
685
+ rewrite_uses! (f, new_f)
671
686
@assert isempty (uses (f))
672
687
unsafe_delete! (mod, f)
673
688
end
0 commit comments