@@ -268,9 +268,8 @@ function build_compiled_call!(stmt::Expr, fcall, code, idx, nargs::Int, sparams:
268
268
delete_idx = Int[]
269
269
if fcall === :ccall
270
270
cfunc, RetType, ArgType = lookup_stmt (code. code, stmt. args[1 ]), stmt. args[2 ], stmt. args[3 ]:: SimpleVector
271
- # The result of this is useful to have next to you when reading this code:
272
- # f(x, y) = ccall(:jl_value_ptr, Ptr{Cvoid}, (Float32,Any), x, y)
273
- # @code_lowered f(2, 3)
271
+ # delete cconvert and unsafe_convert calls and forward the original values, since
272
+ # the same conversions will be applied within the generated compiled variant of this :foreigncall anyway
274
273
args = []
275
274
for (atype, arg) in zip (ArgType, stmt. args[6 : 6 + nargs- 1 ])
276
275
if atype === Any
@@ -279,19 +278,23 @@ function build_compiled_call!(stmt::Expr, fcall, code, idx, nargs::Int, sparams:
279
278
if arg isa SSAValue
280
279
unsafe_convert_expr = code. code[arg. id]:: Expr
281
280
push! (delete_idx, arg. id) # delete the unsafe_convert
282
- cconvert_stmt = unsafe_convert_expr. args[3 ]:: SSAValue
283
- push! (delete_idx, cconvert_stmt. id) # delete the cconvert
284
- cconvert_expr = code. code[cconvert_stmt. id]:: Expr
285
- push! (args, cconvert_expr. args[3 ])
281
+ cconvert_val = unsafe_convert_expr. args[3 ]
282
+ if isa (cconvert_val, SSAValue)
283
+ push! (delete_idx, cconvert_val. id) # delete the cconvert
284
+ newarg = (code. code[cconvert_val. id]:: Expr ). args[3 ]
285
+ push! (args, newarg)
286
+ else
287
+ @assert isa (cconvert_val, SlotNumber)
288
+ push! (args, cconvert_val)
289
+ end
286
290
elseif arg isa SlotNumber
287
- index = findfirst (code. code) do expr
291
+ idx = findfirst (code. code) do expr
288
292
Meta. isexpr (expr, :(= )) || return false
289
293
lhs = expr. args[1 ]
290
294
return lhs isa SlotNumber && lhs. id === arg. id
291
- end
292
- index = index:: Int
293
- unsafe_convert_expr = code. code[index]:: Expr
294
- push! (delete_idx, index) # delete the unsafe_convert
295
+ end :: Int
296
+ unsafe_convert_expr = code. code[idx]:: Expr
297
+ push! (delete_idx, idx) # delete the unsafe_convert
295
298
push! (args, unsafe_convert_expr. args[2 ])
296
299
else
297
300
error (" unexpected foreigncall argument type encountered: $(typeof (arg)) " )
0 commit comments