@@ -562,11 +562,22 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
562
562
# iteratively discover functions that use the intrinsic or any function calling it
563
563
worklist_length = length (worklist)
564
564
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
565
579
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))
570
581
end
571
582
for f in additions
572
583
push! (worklist, f)
@@ -604,6 +615,7 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
604
615
end
605
616
606
617
value_map[f] = new_f
618
+ # XXX : do we want this? we're adding a new arg, after all
607
619
clone_into! (new_f, f; value_map,
608
620
changes= LLVM. API. LLVMCloneFunctionChangeTypeGlobalChanges)
609
621
@@ -613,6 +625,8 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
613
625
end
614
626
615
627
# 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.
616
630
function rewrite_uses! (f, new_f)
617
631
# update uses
618
632
Builder (ctx) do builder
@@ -636,6 +650,16 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
636
650
replace_uses! (val, new_val)
637
651
@assert isempty (uses (val))
638
652
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)
639
663
else
640
664
error (" Cannot rewrite unknown use of function: $val " )
641
665
end
0 commit comments