-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathoptimize.jl
More file actions
369 lines (348 loc) · 13.7 KB
/
optimize.jl
File metadata and controls
369 lines (348 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
const compiled_calls = Dict{Any,Any}()
# Pre-frame-construction lookup
function lookup_stmt(stmts::Vector{Any}, @nospecialize arg)
# this converts a statement into something else, without the slightest interest in correctness:
if isa(arg, SSAValue)
arg = stmts[arg.id]
end
if isa(arg, QuoteNode)
return arg.value
elseif isexpr(arg, :call, 3) && is_global_ref(arg.args[1], Base, :getproperty)
# Starting with Julia 1.12, llvmcall looks like this:
# julia> src.code[1:3]
# 3-element Vector{Any}:
# :(TheModule.Core) # GlobalRef
# :(Base.getproperty(%1, :Intrinsics))
# :(Base.getproperty(%2, :llvmcall))
q = arg.args[3]
if isa(q, QuoteNode) && (qval = q.value; qval isa Symbol)
mod = lookup_stmt(stmts, arg.args[2])
if isa(mod, GlobalRef)
if @invokelatest isdefinedglobal(mod.mod, mod.name)
mod = @invokelatest getglobal(mod.mod, mod.name)
end
end
if isa(mod, Module)
if @invokelatest isdefinedglobal(mod, qval)
return @invokelatest getglobal(mod, qval)
end
end
end
end
return arg
end
function smallest_ref(stmts, arg, idmin)
if isa(arg, SSAValue)
idmin = min(idmin, arg.id)
return smallest_ref(stmts, stmts[arg.id], idmin)
elseif isa(arg, Expr)
for a in arg.args
idmin = smallest_ref(stmts, a, idmin)
end
end
return idmin
end
function lookup_global_ref(a::GlobalRef)
isbindingresolved_deprecated && return a
if Base.isbindingresolved(a.mod, a.name) &&
(@invokelatest isdefinedglobal(a.mod, a.name)) &&
(@invokelatest isconst(a.mod, a.name))
return QuoteNode(@invokelatest getfield(a.mod, a.name))
end
return a
end
function lookup_global_refs!(ex::Expr)
if isexpr(ex, (:isdefined, :thunk, :toplevel, :method, :global, :const, :globaldecl))
return nothing
end
for (i, a) in enumerate(ex.args)
ex.head === :(=) && i == 1 && continue # Don't look up globalrefs on the LHS of an assignment (issue #98)
if isa(a, GlobalRef)
ex.args[i] = lookup_global_ref(a)
elseif isa(a, Expr)
lookup_global_refs!(a)
end
end
return nothing
end
function lookup_getproperties(code::Vector{Any}, @nospecialize a)
isexpr(a, :call) || return a
length(a.args) == 3 || return a
arg1 = lookup_stmt(code, a.args[1])
arg1 === Base.getproperty || return a
arg2 = lookup_stmt(code, a.args[2])
arg2 isa Module || return a
arg3 = lookup_stmt(code, a.args[3])
arg3 isa Symbol || return a
return lookup_global_ref(GlobalRef(arg2, arg3))
end
# HACK This isn't optimization really, but necessary to bypass llvmcall and foreigncall
# TODO This "optimization" should be refactored into a "minimum compilation" necessary to
# execute `llvmcall` and `foreigncall` and pure optimizations on the lowered code representation.
# In particular, the optimization that replaces `GlobalRef` with `QuoteNode` is invalid and
# should be removed: This is because it is not possible to know when and where the binding
# will be resolved without executing the code.
# Since the current `build_compiled_[llvmcall|foreigncall]!` relies on this replacement,
# they also need to be reimplemented.
"""
optimize!(code::CodeInfo, mod::Module)
Perform minor optimizations on the lowered AST in `code` to reduce execution time
of the interpreter.
Currently it looks up `GlobalRef`s (for which it needs `mod` to know the scope in
which this will run) and ensures that no statement includes nested `:call` expressions
(splitting them out into multiple SSA-form statements if needed).
"""
function optimize!(code::CodeInfo, scope)
mod = moduleof(scope)
evalmod = mod == Core.Compiler ? Core.Compiler : CompiledCalls
sparams = scope isa Method ? sparam_syms(scope) : Symbol[]
replace_coretypes!(code)
# TODO: because of builtins.jl, for CodeInfos like
# %1 = Core.apply_type
# %2 = (%1)(args...)
# it would be best to *not* resolve the GlobalRef at %1
## Replace GlobalRefs with QuoteNodes
for (i, stmt) in enumerate(code.code)
if isa(stmt, GlobalRef)
code.code[i] = lookup_global_ref(stmt)
elseif isa(stmt, Expr)
if stmt.head === :call && stmt.args[1] === :cglobal # cglobal requires literals
continue
else
lookup_global_refs!(stmt)
code.code[i] = lookup_getproperties(code.code, stmt)
end
end
end
# Replace :llvmcall and :foreigncall with compiled variants. See
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/13#issuecomment-464880123
# Insert the foreigncall wrappers at the updated idxs
methodtables = Vector{Union{Compiled,DispatchableMethod}}(undef, length(code.code))
for (idx, stmt) in enumerate(code.code)
# Foregincalls can be rhs of assignments
if isexpr(stmt, :(=))
stmt = (stmt::Expr).args[2]
end
if isa(stmt, Expr)
if stmt.head === :call
# Check for :llvmcall
arg1 = stmt.args[1]
larg1 = lookup_stmt(code.code, arg1)
if (arg1 === :llvmcall || larg1 === Base.llvmcall || is_global_ref_egal(larg1, :llvmcall, Core.Intrinsics.llvmcall)) && isempty(sparams) && scope isa Method
# Call via `invokelatest` to avoid compiling it until we need it
@invokelatest build_compiled_llvmcall!(stmt, code, idx, evalmod)
methodtables[idx] = Compiled()
end
elseif stmt.head === :foreigncall && scope isa Method
# Call via `invokelatest` to avoid compiling it until we need it
@invokelatest build_compiled_foreigncall!(stmt, code, sparams, evalmod)
methodtables[idx] = Compiled()
end
end
end
return code, methodtables
end
function parametric_type_to_expr(@nospecialize(t::Type))
t isa Core.TypeofBottom && return t
while t isa UnionAll
t = t.body
end
t = t::DataType
if Base.isvarargtype(t)
return Expr(:(...), t.parameters[1])
end
if Base.has_free_typevars(t)
params = map(t.parameters) do @nospecialize(p)
isa(p, TypeVar) ? p.name :
isa(p, DataType) && Base.has_free_typevars(p) ? parametric_type_to_expr(p) : p
end
return Expr(:curly, scopename(t.name), params...)::Expr
end
return t
end
function build_compiled_llvmcall!(stmt::Expr, code::CodeInfo, idx::Int, evalmod::Module)
# Run a mini-interpreter to extract the types
framecode = FrameCode(CompiledCalls, code; optimize=false)
frame = Frame(framecode, prepare_framedata(framecode, []))
idxstart = idx
for i = 2:4
idxstart = smallest_ref(code.code, stmt.args[i], idxstart)
end
frame.pc = idxstart
if idxstart < idx
while true
pc = step_expr!(NonRecursiveInterpreter(), frame)
pc === idx && break
pc === nothing && error("this should never happen")
end
end
llvmir, RetType, ArgType = lookup(frame, stmt.args[2]), lookup(frame, stmt.args[3]), lookup(frame, stmt.args[4])::DataType
args = stmt.args[5:end]
argnames = Any[Symbol(:arg, i) for i = 1:length(args)]
cc_key = (llvmir, RetType, ArgType, evalmod) # compiled call key
f = get(compiled_calls, cc_key, nothing)
if f === nothing
methname = gensym("compiled_llvmcall")
def = :(
function $methname($(argnames...))
return $(Base.llvmcall)($llvmir, $RetType, $ArgType, $(argnames...))
end)
f = Core.eval(evalmod, def)
compiled_calls[cc_key] = f
end
stmt.args[1] = QuoteNode(f)
stmt.head = :call
deleteat!(stmt.args, 2:length(stmt.args))
append!(stmt.args, args)
end
# Handle :llvmcall & :foreigncall (issue #28)
function build_compiled_foreigncall!(stmt::Expr, code::CodeInfo, sparams::Vector{Symbol}, evalmod::Module)
TVal = evalmod == Core.Compiler ? Core.Compiler.Val : Val
RetType, ArgType = stmt.args[2], stmt.args[3]::SimpleVector
dynamic_ccall = false
argcfunc = cfunc = stmt.args[1]
if @isdefined(__has_internal_change) && __has_internal_change(v"1.13.0", :syntacticccall)
if !isexpr(cfunc, :tuple)
dynamic_ccall = true
cfunc = gensym("ptr")
end
else
while isa(cfunc, SSAValue)
cfunc = lookup_stmt(code.code, cfunc)
cfunc isa Symbol && (cfunc = QuoteNode(cfunc))
end
# n.b. Base.memhash is deprecated (continued use would cause serious faults) in the same version as the syntax is deprecated
# so this is only needed as a legacy hack
if isa(cfunc, Expr) || (cfunc isa GlobalRef && cfunc == GlobalRef(Base, :memhash))
cfunc = something(try QuoteNode(Core.eval(evalmod, cfunc)) catch nothing end, cfunc)
end
if !(isa(cfunc, Union{String, Tuple}) || (isa(cfunc, QuoteNode) && isa(cfunc.value, Union{String, Tuple, Symbol})))
dynamic_ccall = true
cfunc = gensym("ptr")
end
end
if isa(RetType, SimpleVector)
@assert length(RetType) == 1
RetType = RetType[1]
end
args = stmt.args[6:end]
# When the ccall is dynamic we pass the pointer as an argument so can reuse the function
cc_key = ((dynamic_ccall ? :ptr : cfunc), RetType, ArgType, evalmod, length(sparams), length(args)) # compiled call key
f = get(compiled_calls, cc_key, nothing)
if f === nothing
ArgType = Expr(:tuple, Any[parametric_type_to_expr(t) for t in ArgType::SimpleVector]...)
RetType = parametric_type_to_expr(RetType)
# #285: test whether we can evaluate an type constraints on parametric expressions
# this essentially comes down to having the names be available in CompiledCalls,
# if they are not then executing the method will fail
try
isa(RetType, Expr) && Core.eval(CompiledCalls, wrap_params(RetType, sparams))
isa(ArgType, Expr) && Core.eval(CompiledCalls, wrap_params(ArgType, sparams))
catch
return nothing
end
argnames = Any[Symbol(:arg, i) for i = 1:length(args)]
wrapargs = copy(argnames)
for sparam in sparams
push!(wrapargs, :(::$TVal{$sparam}))
end
if dynamic_ccall
pushfirst!(wrapargs, cfunc)
end
methname = gensym("compiled_ccall")
def = :(
function $methname($(wrapargs...)) where {$(sparams...)}
return $(Expr(:foreigncall, cfunc, RetType, stmt.args[3:5]..., argnames...))
end)
f = Core.eval(evalmod, def)
compiled_calls[cc_key] = f
end
stmt.args[1] = QuoteNode(f)
stmt.head = :call
deleteat!(stmt.args, 2:length(stmt.args))
if dynamic_ccall
push!(stmt.args, argcfunc)
end
append!(stmt.args, args)
for i in 1:length(sparams)
push!(stmt.args, :($TVal($(Expr(:static_parameter, i)))))
end
return nothing
end
function replace_coretypes!(@nospecialize(src); rev::Bool=false)
if isa(src, CodeInfo)
replace_coretypes_list!(src.code; rev=rev)
elseif isa(src, Expr)
replace_coretypes_list!(src.args; rev=rev)
end
return src
end
function replace_coretypes_list!(list::AbstractVector; rev::Bool=false)
function rep(@nospecialize(x), rev::Bool)
if rev
isa(x, SSAValue) && return Core.SSAValue(x.id)
isa(x, SlotNumber) && return Core.SlotNumber(x.id)
return x
else
isa(x, Core.SSAValue) && return SSAValue(x.id)
isa(x, Core.SlotNumber) && return SlotNumber(x.id)
@static if VERSION < v"1.11.0-DEV.337"
isa(x, Core.Compiler.TypedSlot) && return SlotNumber(x.id)
end
return x
end
end
for (i, stmt) in enumerate(list)
rstmt = rep(stmt, rev)
if rstmt !== stmt
list[i] = rstmt
elseif isa(stmt, GotoIfNot)
cond = stmt.cond
rcond = rep(cond, rev)
if rcond !== cond
list[i] = GotoIfNot(rcond, stmt.dest)
end
elseif isa(stmt, ReturnNode)
val = stmt.val
rval = rep(val, rev)
if rval !== val
list[i] = ReturnNode(rval)
end
elseif @static (isdefinedglobal(Core.IR, :EnterNode) && true) && isa(stmt, Core.IR.EnterNode)
if isdefined(stmt, :scope)
rscope = rep(stmt.scope, rev)
if rscope !== stmt.scope
list[i] = Core.IR.EnterNode(stmt.catch_dest, rscope)
end
end
elseif isa(stmt, Expr)
replace_coretypes!(stmt; rev=rev)
end
end
return nothing
end
function reverse_lookup_globalref!(list)
# This only handles the function in calls
for (i, stmt) in enumerate(list)
if isexpr(stmt, :(=))
stmt = (stmt::Expr).args[2]
end
if isexpr(stmt, :call)
stmt = stmt::Expr
f = stmt.args[1]
if isa(f, QuoteNode)
f = f.value
if isa(f, Function) && !isa(f, Core.IntrinsicFunction)
ft = typeof(f)
tn = ft.name::Core.TypeName
name = String(tn.name)
if startswith(name, '#')
name = name[2:end]
end
stmt.args[1] = GlobalRef(tn.module, Symbol(name))
end
end
end
end
return list
end