@@ -56,6 +56,19 @@ function renumber_ssa!(stmts::Vector{Any}, ssalookup)
56
56
return stmts
57
57
end
58
58
59
+ function compute_ssa_mapping_delete_statements! (code:: CodeInfo , stmts:: Vector{Int} )
60
+ stmts = unique! (sort! (stmts))
61
+ ssalookup = collect (1 : length (code. codelocs))
62
+ cnt = 1
63
+ for i in 1 : length (stmts)
64
+ start = stmts[i] + 1
65
+ stop = i == length (stmts) ? length (code. codelocs) : stmts[i+ 1 ]
66
+ ssalookup[start: stop] .- = cnt
67
+ cnt += 1
68
+ end
69
+ return ssalookup
70
+ end
71
+
59
72
# Pre-frame-construction lookup
60
73
function lookup_stmt (stmts, arg)
61
74
if isa (arg, SSAValue)
@@ -127,10 +140,10 @@ function optimize!(code::CodeInfo, scope)
127
140
end
128
141
end
129
142
130
- # Replace :llvmcall and :foreigncall with compiled variants. See
143
+ # Replace :llvmcall and :foreigncall with compiled variants. See
131
144
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/13#issuecomment-464880123
132
- # @show code
133
145
foreigncalls_idx = Int[]
146
+ delete_idxs = Int[]
134
147
for (idx, stmt) in enumerate (code. code)
135
148
# Foregincalls can be rhs of assignments
136
149
if isexpr (stmt, :(= ))
@@ -144,8 +157,9 @@ function optimize!(code::CodeInfo, scope)
144
157
ustr = replace (string (uuid), ' -' => ' _' )
145
158
methname = Symbol (" llvmcall_" , ustr)
146
159
nargs = length (stmt. args)- 4
147
- build_compiled_call! (stmt, methname, Base. llvmcall, stmt. args[2 : 4 ], code, idx, nargs, sparams)
160
+ delete_idx = build_compiled_call! (stmt, methname, Base. llvmcall, stmt. args[2 : 4 ], code, idx, nargs, sparams)
148
161
push! (foreigncalls_idx, idx)
162
+ append! (delete_idxs, delete_idx)
149
163
end
150
164
elseif isexpr (stmt, :foreigncall ) && scope isa Method
151
165
f = lookup_stmt (code. code, stmt. args[1 ])
@@ -169,11 +183,21 @@ function optimize!(code::CodeInfo, scope)
169
183
ustr = replace (string (uuid), ' -' => ' _' )
170
184
methname = Symbol (" ccall" , ' _' , f, ' _' , ustr)
171
185
nargs = stmt. args[5 ]
172
- build_compiled_call! (stmt, methname, :ccall , stmt. args[1 : 3 ], code, idx, nargs, sparams)
186
+ delete_idx = build_compiled_call! (stmt, methname, :ccall , stmt. args[1 : 3 ], code, idx, nargs, sparams)
173
187
push! (foreigncalls_idx, idx)
188
+ append! (delete_idxs, delete_idx)
174
189
end
175
190
end
176
191
192
+ if ! isempty (delete_idxs)
193
+ ssalookup = compute_ssa_mapping_delete_statements! (code, delete_idxs)
194
+ foreigncalls_idx = map (x -> ssalookup[x], foreigncalls_idx)
195
+ deleteat! (code. codelocs, delete_idxs)
196
+ deleteat! (code. code, delete_idxs)
197
+ code. ssavaluetypes = length (code. code)
198
+ renumber_ssa! (code. code, ssalookup)
199
+ end
200
+
177
201
# # Un-nest :call expressions (so that there will be only one :call per line)
178
202
# This will allow us to re-use args-buffers rather than having to allocate new ones each time.
179
203
old_code, old_codelocs = code. code, code. codelocs
214
238
# Handle :llvmcall & :foreigncall (issue #28)
215
239
function build_compiled_call! (stmt, methname, fcall, typargs, code, idx, nargs, sparams)
216
240
argnames = Any[Symbol (" arg" , string (i)) for i = 1 : nargs]
241
+ delete_idx = Int[]
217
242
if fcall == :ccall
218
243
cfunc, RetType, ArgType = lookup_stmt (code. code, stmt. args[1 ]), stmt. args[2 ], stmt. args[3 ]
219
244
# The result of this is useful to have next to you when reading this code:
@@ -226,7 +251,10 @@ function build_compiled_call!(stmt, methname, fcall, typargs, code, idx, nargs,
226
251
else
227
252
@assert arg isa SSAValue
228
253
unsafe_convert_expr = code. code[arg. id]
229
- cconvert_expr = code. code[unsafe_convert_expr. args[3 ]. id]
254
+ push! (delete_idx, arg. id) # delete the unsafe_convert
255
+ cconvert_stmt = unsafe_convert_expr. args[3 ]
256
+ push! (delete_idx, cconvert_stmt. id) # delete the cconvert
257
+ cconvert_expr = code. code[cconvert_stmt. id]
230
258
push! (args, cconvert_expr. args[3 ])
231
259
end
232
260
end
@@ -286,6 +314,6 @@ function build_compiled_call!(stmt, methname, fcall, typargs, code, idx, nargs,
286
314
for i in 1 : length (sparams)
287
315
push! (stmt. args, :($ Val ($ (Expr (:static_parameter , i)))))
288
316
end
289
- return
317
+ return delete_idx
290
318
end
291
319
0 commit comments