Skip to content

Commit e1f9a7d

Browse files
authored
During kernel state rewrite, handle functions passed as arguments. (#474)
1 parent ef5e40f commit e1f9a7d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/irgen.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,27 @@ function add_kernel_state!(mod::LLVM.Module)
773773
@assert isempty(uses(val))
774774
unsafe_delete!(LLVM.parent(val), val)
775775
elseif val isa LLVM.CallBase
776-
# the function is being passed as an argument, which we'll just permit,
777-
# because we expect to have rewritten the call down the line separately.
776+
# the function is being passed as an argument. to avoid having to
777+
# rewrite the target function, instead case the rewritten function to
778+
# the old stateless type.
779+
# XXX: we won't have to do this with opaque pointers.
780+
position!(builder, val)
781+
target_ft = called_type(val)
782+
new_args = map(zip(parameters(target_ft),
783+
arguments(val))) do (param_typ, arg)
784+
if value_type(arg) != param_typ
785+
const_bitcast(arg, param_typ)
786+
else
787+
arg
788+
end
789+
end
790+
new_val = call!(builder, called_type(val), called_value(val), new_args,
791+
operand_bundles(val))
792+
callconv!(new_val, callconv(val))
793+
794+
replace_uses!(val, new_val)
795+
@assert isempty(uses(val))
796+
unsafe_delete!(LLVM.parent(val), val)
778797
elseif val isa LLVM.StoreInst
779798
# the function is being stored, which again we'll permit like before.
780799
elseif val isa ConstantExpr

0 commit comments

Comments
 (0)