Skip to content

Commit 394d003

Browse files
authored
Allow materializer callback to devolve responsibility (#319)
1 parent a29511d commit 394d003

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/utils.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ export clone_into!, clone
66

77
type_mapper_callback(typ, type_mapper) =
88
Base.unsafe_convert(API.LLVMTypeRef, type_mapper[](LLVMType(typ)))
9-
materializer_callback(val, materializer) =
10-
Base.unsafe_convert(API.LLVMValueRef, materializer[](Value(val)))
9+
function materializer_callback(val, materializer)
10+
new_val = materializer[](Value(val))
11+
if new_val === nothing
12+
return Base.unsafe_convert(API.LLVMValueRef, C_NULL)
13+
else
14+
return Base.unsafe_convert(API.LLVMValueRef, new_val)
15+
end
16+
end
1117

1218
function clone_into!(new::Function, old::Function;
1319
value_map::Dict{Value,Value}=Dict{Value,Value}(),

test/utils.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
@dispose builder=Builder(ctx) begin
1515
entry = BasicBlock(f, "entry"; ctx)
1616
position!(builder, entry)
17-
17+
ptr = const_inttoptr(
18+
ConstantInt(0xdeadbeef%UInt; ctx),
19+
LLVM.PointerType(LLVM.Int32Type(ctx)))
1820
tmp = add!(builder, parameters(f)[1], parameters(f)[2], "tmp")
19-
ret!(builder, tmp)
21+
tmp2 = load!(builder, ptr)
22+
tmp3 = add!(builder, tmp, tmp2)
23+
ret!(builder, tmp3)
2024

2125
verify(mod)
2226
end
@@ -70,8 +74,12 @@
7074
end
7175
end
7276
function materializer(val)
77+
if val isa Union{LLVM.ConstantExpr, ConstantInt}
78+
# test that we can return nothing
79+
return nothing
80+
end
7381
# not needed here
74-
error()
82+
error("")
7583
end
7684
clone_into!(new_f, f; value_map, type_mapper, materializer)
7785

0 commit comments

Comments
 (0)