Skip to content

Commit 722f82f

Browse files
authored
Minor inference and specialization improvements (#461)
This, together with some companion changes in LoweredCodeUtils and revise, shaves another 200ms off the time for first revision.
1 parent 381ee91 commit 722f82f

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "JuliaInterpreter"
22
uuid = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
3-
version = "0.8.7"
3+
version = "0.8.8"
44

55
[deps]
66
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"

src/builtins.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
168168
return Some{Any}(applicable(getargs(args, frame)...))
169169
elseif f === fieldtype
170170
if nargs == 2
171-
return Some{Any}(fieldtype(@lookup(frame, args[2]), @lookup(frame, args[3])))
171+
return Some{Any}(fieldtype(@lookup(frame, args[2]), @lookup(frame, args[3]))::Type)
172172
elseif nargs == 3
173-
return Some{Any}(fieldtype(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
173+
return Some{Any}(fieldtype(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]))::Type)
174174
else
175-
return Some{Any}(fieldtype(getargs(args, frame)...))
175+
return Some{Any}(fieldtype(getargs(args, frame)...)::Type)
176176
end
177177
elseif f === getfield
178178
if nargs == 2

src/optimize.jl

+16-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function extract_inner_call!(stmt::Expr, idx, once::Bool=false)
2525
return nothing
2626
end
2727

28-
function replace_ssa!(stmt, ssalookup)
28+
function replace_ssa!(@nospecialize(stmt), ssalookup)
2929
isa(stmt, Expr) || return nothing
3030
for (i, a) in enumerate(stmt.args)
3131
if isa(a, SSAValue)
@@ -59,7 +59,7 @@ function renumber_ssa!(stmts::Vector{Any}, ssalookup)
5959
stmt.args[end] = jumplookup(ssalookup, stmt.args[end])
6060
end
6161
elseif is_GotoIfNot(stmt)
62-
cond = stmt.cond
62+
cond = (stmt::Core.GotoIfNot).cond
6363
if isa(cond, SSAValue)
6464
cond = SSAValue(ssalookup[cond.id])
6565
end
@@ -245,15 +245,15 @@ function optimize!(code::CodeInfo, scope)
245245
return code, methodtables
246246
end
247247

248-
function parametric_type_to_expr(t::Type)
248+
function parametric_type_to_expr(@nospecialize(t::Type))
249249
t isa Core.TypeofBottom && return t
250250
t isa UnionAll && (t = t.body)
251251
t = t::DataType
252252
if Base.isvarargtype(t)
253253
return Expr(:(...), t.parameters[1])
254254
end
255255
if t.hasfreetypevars
256-
params = map(t.parameters) do p
256+
params = map(t.parameters) do @nospecialize(p)
257257
isa(p, TypeVar) ? p.name :
258258
isa(p, DataType) && p.hasfreetypevars ? parametric_type_to_expr(p) : p
259259
end
@@ -403,10 +403,18 @@ end
403403

404404
function replace_coretypes_list!(list::AbstractVector; rev::Bool)
405405
function rep(@nospecialize(x), rev::Bool)
406-
if isa(x, rev ? SSAValue : Core.SSAValue)
407-
return rev ? Core.SSAValue(x.id) : SSAValue(x.id)
408-
elseif isa(x, rev ? SlotNumber : Core.SlotNumber)
409-
return rev ? Core.SlotNumber(x.id) : SlotNumber(x.id)
406+
if rev
407+
if isa(x, SSAValue)
408+
return Core.SSAValue(x.id)
409+
elseif isa(x, SlotNumber)
410+
return Core.SlotNumber(x.id)
411+
end
412+
return x
413+
end
414+
if isa(x, Core.SSAValue)
415+
return SSAValue(x.id)
416+
elseif isa(x, Core.SlotNumber)
417+
return SlotNumber(x.id)
410418
end
411419
return x
412420
end

src/precompile.jl

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function _precompile_()
1717
@assert precompile(Tuple{typeof(eval_rhs), Any, Frame, Expr})
1818
@assert precompile(Tuple{typeof(step_expr!), Any, Frame, Any, Bool})
1919
for f in (finish!, finish_and_return!, finish_stack!, next_call!, maybe_next_call!, next_line!)
20+
@assert precompile(Tuple{typeof(f), Any, Frame})
2021
@assert precompile(Tuple{typeof(f), Any, Frame, Bool})
2122
end
2223
@assert precompile(Tuple{typeof(through_methoddef_or_done!), Any, Frame})
@@ -51,4 +52,5 @@ function _precompile_()
5152
@assert precompile(Tuple{typeof(append_any), Any})
5253
@assert precompile(Tuple{typeof(append_any), Any, Vararg{Any, 100}})
5354
end
55+
@assert precompile(Tuple{typeof(whichtt), Any})
5456
end

src/utils.jl

+10-4
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,15 @@ end
123123

124124
function scopename(tn::TypeName)
125125
modpath = Base.fullname(tn.module)
126-
return Expr(:., _scopename(modpath...), QuoteNode(tn.name))
126+
if isa(modpath, Tuple{Symbol})
127+
return Expr(:., modpath[1], QuoteNode(tn.name))
128+
end
129+
ex = Expr(:., modpath[end-1], QuoteNode(modpath[end]))
130+
for i = length(modpath)-2:-1:1
131+
ex = Expr(:., modpath[i], ex)
132+
end
133+
return Expr(:., ex, QuoteNode(tn.name))
127134
end
128-
_scopename(sym) = sym
129-
_scopename(parent, child) = Expr(:., parent, QuoteNode(child))
130-
_scopename(parent, child, rest...) = Expr(:., parent, _scopename(child, rest...))
131135

132136
## Predicates
133137

@@ -146,9 +150,11 @@ end
146150
if isdefined(Core, :ReturnNode)
147151
is_ReturnNode(@nospecialize(node)) = isa(node, Core.ReturnNode)
148152
is_return(@nospecialize(node)) = is_ReturnNode(node)
153+
get_return_node(@nospecialize(node)) = (node::Core.ReturnNode).val
149154
else
150155
is_ReturnNode(@nospecialize(node)) = false
151156
is_return(@nospecialize(node)) = isexpr(node, :return)
157+
get_return_node(@nospecialize(node)) = node.args[1]
152158
end
153159

154160
is_loc_meta(@nospecialize(expr), @nospecialize(kind)) = isexpr(expr, :meta) && length(expr.args) >= 1 && expr.args[1] === kind

test/core.jl

+7
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ using Test
2323
if isdefined(Base.IRShow, :show_ir_stmt) # only works on Julia 1.6 and higher
2424
@test any(str->occursin(":copyast", str) && occursin("println", str), lines)
2525
end
26+
27+
thunk = Meta.lower(Main, :(return 1+2))
28+
stmt = thunk.args[1].code[end] # the return
29+
@test JuliaInterpreter.get_return_node(stmt) isa Core.SSAValue
30+
31+
@test string(JuliaInterpreter.parametric_type_to_expr(Base.Iterators.Stateful{String}))
32+
("Base.Iterators.Stateful{String, VS}", "(Base.Iterators).Stateful{String, VS}")
2633
end

0 commit comments

Comments
 (0)