Skip to content

Commit 4265ae9

Browse files
authored
delete leftover cconvert and unsafe_convert (#237)
1 parent 7ac56b5 commit 4265ae9

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/optimize.jl

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ function renumber_ssa!(stmts::Vector{Any}, ssalookup)
5656
return stmts
5757
end
5858

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+
5972
# Pre-frame-construction lookup
6073
function lookup_stmt(stmts, arg)
6174
if isa(arg, SSAValue)
@@ -127,10 +140,10 @@ function optimize!(code::CodeInfo, scope)
127140
end
128141
end
129142

130-
# Replace :llvmcall and :foreigncall with compiled variants. See
143+
# Replace :llvmcall and :foreigncall with compiled variants. See
131144
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/13#issuecomment-464880123
132-
# @show code
133145
foreigncalls_idx = Int[]
146+
delete_idxs = Int[]
134147
for (idx, stmt) in enumerate(code.code)
135148
# Foregincalls can be rhs of assignments
136149
if isexpr(stmt, :(=))
@@ -144,8 +157,9 @@ function optimize!(code::CodeInfo, scope)
144157
ustr = replace(string(uuid), '-'=>'_')
145158
methname = Symbol("llvmcall_", ustr)
146159
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)
148161
push!(foreigncalls_idx, idx)
162+
append!(delete_idxs, delete_idx)
149163
end
150164
elseif isexpr(stmt, :foreigncall) && scope isa Method
151165
f = lookup_stmt(code.code, stmt.args[1])
@@ -169,11 +183,21 @@ function optimize!(code::CodeInfo, scope)
169183
ustr = replace(string(uuid), '-'=>'_')
170184
methname = Symbol("ccall", '_', f, '_', ustr)
171185
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)
173187
push!(foreigncalls_idx, idx)
188+
append!(delete_idxs, delete_idx)
174189
end
175190
end
176191

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+
177201
## Un-nest :call expressions (so that there will be only one :call per line)
178202
# This will allow us to re-use args-buffers rather than having to allocate new ones each time.
179203
old_code, old_codelocs = code.code, code.codelocs
@@ -214,6 +238,7 @@ end
214238
# Handle :llvmcall & :foreigncall (issue #28)
215239
function build_compiled_call!(stmt, methname, fcall, typargs, code, idx, nargs, sparams)
216240
argnames = Any[Symbol("arg", string(i)) for i = 1:nargs]
241+
delete_idx = Int[]
217242
if fcall == :ccall
218243
cfunc, RetType, ArgType = lookup_stmt(code.code, stmt.args[1]), stmt.args[2], stmt.args[3]
219244
# 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,
226251
else
227252
@assert arg isa SSAValue
228253
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]
230258
push!(args, cconvert_expr.args[3])
231259
end
232260
end
@@ -286,6 +314,6 @@ function build_compiled_call!(stmt, methname, fcall, typargs, code, idx, nargs,
286314
for i in 1:length(sparams)
287315
push!(stmt.args, :($Val($(Expr(:static_parameter, i)))))
288316
end
289-
return
317+
return delete_idx
290318
end
291319

0 commit comments

Comments
 (0)