Skip to content

Commit 0a8da3a

Browse files
authored
minimum adjustments to make JuliaInterpreter work with v1.12 (#631)
For now, I've made the minimal changes needed to use JuliaInterpreter (and Revise) with v1.12. CI is still broken, so additional changes are needed. Additionally, while making these changes, I found that the implementation of `JuliaInterpreter.optimize!` is quite illegal. This is an issue that should be resolved eventually, so I've left comments in optimize.jl regarding the known problems.
1 parent 397ea70 commit 0a8da3a

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/interpret.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,14 @@ evaluate_call!(frame::Frame, call_expr::Expr; kwargs...) = evaluate_call!(finish
313313
# The following come up only when evaluating toplevel code
314314
function evaluate_methoddef(frame, node)
315315
f = node.args[1]
316-
if isa(f, Symbol)
317-
mod = moduleof(frame)
318-
if Base.isbindingresolved(mod, f) && isdefined(mod, f) # `isdefined` accesses the binding, making it impossible to create a new one
319-
f = getfield(mod, f)
316+
if f isa Symbol || f isa GlobalRef
317+
mod = f isa Symbol ? moduleof(frame) : f.mod
318+
name = f isa Symbol ? f : f.name
319+
if Base.isbindingresolved(mod, name) && isdefined(mod, name) # `isdefined` accesses the binding, making it impossible to create a new one
320+
f = getfield(mod, name)
320321
else
321-
f = Core.eval(moduleof(frame), Expr(:function, f)) # create a new function
322+
f = Core.eval(mod, Expr(:function, name)) # create a new function
322323
end
323-
elseif isa(f, GlobalRef)
324-
f = getfield(f.mod, f.name)
325324
end
326325
length(node.args) == 1 && return f
327326
sig = @lookup(frame, node.args[2])::SimpleVector

src/optimize.jl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function lookup_stmt(stmts::Vector{Any}, @nospecialize arg)
66
arg = stmts[arg.id]
77
end
88
if isa(arg, QuoteNode)
9-
arg = arg.value
9+
return arg.value
1010
end
1111
return arg
1212
end
@@ -24,16 +24,16 @@ function smallest_ref(stmts, arg, idmin)
2424
end
2525

2626
function lookup_global_ref(a::GlobalRef)
27-
if isdefined(a.mod, a.name) && isconst(a.mod, a.name)
28-
r = getfield(a.mod, a.name)
29-
return QuoteNode(r)
30-
else
31-
return a
27+
if Base.isbindingresolved(a.mod, a.name) && isdefined(a.mod, a.name)
28+
return QuoteNode(getfield(a.mod, a.name))
3229
end
30+
return a
3331
end
3432

3533
function lookup_global_refs!(ex::Expr)
36-
(ex.head === :isdefined || ex.head === :thunk || ex.head === :toplevel) && return nothing
34+
if isexpr(ex, (:isdefined, :thunk, :toplevel, :method, :global, :const))
35+
return nothing
36+
end
3737
for (i, a) in enumerate(ex.args)
3838
ex.head === :(=) && i == 1 && continue # Don't look up globalrefs on the LHS of an assignment (issue #98)
3939
if isa(a, GlobalRef)
@@ -57,7 +57,14 @@ function lookup_getproperties(code::Vector{Any}, @nospecialize a)
5757
return lookup_global_ref(GlobalRef(arg2, arg3))
5858
end
5959

60-
# TODO This isn't optimization really, but necessary to bypass llvmcall and foreigncall
60+
# HACK This isn't optimization really, but necessary to bypass llvmcall and foreigncall
61+
# TODO This "optimization" should be refactored into a "minimum compilation" necessary to
62+
# execute `llvmcall` and `foreigncall` and pure optimizations on the lowered code representation.
63+
# In particular, the optimization that replaces `GlobalRef` with `QuoteNode` is invalid and
64+
# should be removed: This is because it is not possible to know when and where the binding
65+
# will be resolved without executing the code.
66+
# Since the current `build_compiled_[llvmcall|foreigncall]!` relies on this replacement,
67+
# they also need to be reimplemented.
6168

6269
"""
6370
optimize!(code::CodeInfo, mod::Module)
@@ -73,6 +80,7 @@ function optimize!(code::CodeInfo, scope)
7380
evalmod = mod == Core.Compiler ? Core.Compiler : CompiledCalls
7481
sparams = scope isa Method ? sparam_syms(scope) : Symbol[]
7582
replace_coretypes!(code)
83+
7684
# TODO: because of builtins.jl, for CodeInfos like
7785
# %1 = Core.apply_type
7886
# %2 = (%1)(args...)

0 commit comments

Comments
 (0)