Skip to content

Commit 9f3ca7c

Browse files
authored
Merge branch 'master' into new-heuristics
2 parents 15b34a5 + 51941ed commit 9f3ca7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+668
-269
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ New language features
44
---------------------
55

66
* JuliaSyntax.jl is now used as the default parser, providing better diagnostics and faster
7-
parsing. Set environment variable `JULIA_USE_NEW_PARSER` to `0` to switch back to the old
7+
parsing. Set environment variable `JULIA_USE_FLISP_PARSER` to `1` to switch back to the old
88
parser if necessary (and if you find this necessary, please file an issue) ([#46372]).
99
* `` (U+297A, `\leftarrowsubset`) and `` (U+2977, `\leftarrowless`)
1010
may now be used as binary operators with arrow precedence. ([#45962])

base/Base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ function __init__()
604604
_require_world_age[] = get_world_counter()
605605
# Prevent spawned Julia process from getting stuck waiting on Tracy to connect.
606606
delete!(ENV, "JULIA_WAIT_FOR_TRACY")
607-
if get_bool_env("JULIA_USE_NEW_PARSER", true) === true
607+
if get_bool_env("JULIA_USE_FLISP_PARSER", false) === false
608608
JuliaSyntax.enable_in_core!()
609609
end
610610
nothing

base/compiler/abstractinterpretation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
20532053
end
20542054

20552055
function abstract_call_opaque_closure(interp::AbstractInterpreter,
2056-
closure::PartialOpaque, arginfo::ArgInfo, si::StmtInfo, sv::InferenceState, check::Bool=true)
2056+
closure::PartialOpaque, arginfo::ArgInfo, si::StmtInfo, sv::AbsIntState, check::Bool=true)
20572057
sig = argtypes_to_type(arginfo.argtypes)
20582058
result = abstract_call_method(interp, closure.source::Method, sig, Core.svec(), false, si, sv)
20592059
(; rt, edge, effects) = result

base/compiler/abstractlattice.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ end
249249
isa(x, Const) && return true
250250
return is_forwardable_argtype(widenlattice(𝕃), x)
251251
end
252-
@nospecializeinfer function is_forwardable_argtype(::JLTypeLattice, @nospecialize x)
253-
return false
254-
end
252+
@nospecializeinfer is_forwardable_argtype(::JLTypeLattice, @nospecialize x) = false
255253

256254
"""
257255
widenreturn(𝕃ᵢ::AbstractLattice, @nospecialize(rt), info::BestguessInfo) -> new_bestguess

base/compiler/inferenceresult.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ function pick_const_args!(𝕃::AbstractLattice, cache_argtypes::Vector{Any}, ov
6161
cache_argtype = cache_argtypes[i]
6262
if !is_argtype_match(𝕃, given_argtype, cache_argtype, false)
6363
# prefer the argtype we were given over the one computed from `linfo`
64+
if (isa(given_argtype, PartialStruct) && isa(cache_argtype, Type) &&
65+
!(𝕃, given_argtype, cache_argtype))
66+
# if the type information of this `PartialStruct` is less strict than
67+
# declared method signature, narrow it down using `tmeet`
68+
given_argtype = tmeet(𝕃, given_argtype, cache_argtype)
69+
end
6470
cache_argtypes[i] = given_argtype
6571
overridden_by_const[i] = true
6672
end
@@ -80,7 +86,7 @@ end
8086

8187
va_process_argtypes(𝕃::AbstractLattice, given_argtypes::Vector{Any}, mi::MethodInstance) =
8288
va_process_argtypes(Returns(nothing), 𝕃, given_argtypes, mi)
83-
function va_process_argtypes(@nospecialize(va_handler!), 𝕃::AbstractLattice, given_argtypes::Vector{Any}, mi::MethodInstance)
89+
function va_process_argtypes(@specialize(va_handler!), 𝕃::AbstractLattice, given_argtypes::Vector{Any}, mi::MethodInstance)
8490
def = mi.def
8591
isva = isa(def, Method) ? def.isva : false
8692
nargs = isa(def, Method) ? Int(def.nargs) : length(mi.specTypes.parameters)
@@ -203,31 +209,29 @@ function elim_free_typevars(@nospecialize t)
203209
end
204210
end
205211

206-
function cache_lookup(lattice::AbstractLattice, linfo::MethodInstance, given_argtypes::Vector{Any}, cache::Vector{InferenceResult})
212+
function cache_lookup(𝕃::AbstractLattice, linfo::MethodInstance, given_argtypes::Vector{Any}, cache::Vector{InferenceResult})
207213
method = linfo.def::Method
208-
nargs::Int = method.nargs
214+
nargs = Int(method.nargs)
209215
method.isva && (nargs -= 1)
210-
length(given_argtypes) >= nargs || return nothing
216+
length(given_argtypes) nargs || return nothing
211217
for cached_result in cache
212218
cached_result.linfo === linfo || continue
213-
cache_match = true
214219
cache_argtypes = cached_result.argtypes
215220
cache_overridden_by_const = cached_result.overridden_by_const
216221
for i in 1:nargs
217-
if !is_argtype_match(lattice, widenmustalias(given_argtypes[i]),
218-
cache_argtypes[i],
219-
cache_overridden_by_const[i])
220-
cache_match = false
221-
break
222+
if !is_argtype_match(𝕃, widenmustalias(given_argtypes[i]),
223+
cache_argtypes[i], cache_overridden_by_const[i])
224+
@goto next_cache
222225
end
223226
end
224-
if method.isva && cache_match
225-
cache_match = is_argtype_match(lattice, tuple_tfunc(lattice, given_argtypes[(nargs + 1):end]),
226-
cache_argtypes[end],
227-
cache_overridden_by_const[end])
227+
if method.isva
228+
if !is_argtype_match(𝕃, tuple_tfunc(𝕃, given_argtypes[(nargs + 1):end]),
229+
cache_argtypes[end], cache_overridden_by_const[end])
230+
@goto next_cache
231+
end
228232
end
229-
cache_match || continue
230233
return cached_result
234+
@label next_cache
231235
end
232236
return nothing
233237
end

base/compiler/inferencestate.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,10 @@ end
768768
iterate(unw::AbsIntStackUnwind) = (unw.sv, (unw.sv, 0))
769769
function iterate(unw::AbsIntStackUnwind, (sv, cyclei)::Tuple{AbsIntState, Int})
770770
# iterate through the cycle before walking to the parent
771-
if cyclei < length(callers_in_cycle(sv))
771+
callers = callers_in_cycle(sv)
772+
if callers !== () && cyclei < length(callers)
772773
cyclei += 1
773-
parent = callers_in_cycle(sv)[cyclei]
774+
parent = callers[cyclei]
774775
else
775776
cyclei = 0
776777
parent = frame_parent(sv)

base/compiler/ssair/inlining.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ inline_node_is_duplicate(topline::LineInfoNode, line::LineInfoNode) =
322322
topline.line === line.line
323323

324324
function ir_inline_linetable!(linetable::Vector{LineInfoNode}, inlinee_ir::IRCode,
325-
inlinee::MethodInstance,
326-
inlined_at::Int32)
325+
inlinee::MethodInstance, inlined_at::Int32)
327326
inlinee_def = inlinee.def::Method
328327
coverage = coverage_enabled(inlinee_def.module)
329328
linetable_offset::Int32 = length(linetable)
@@ -358,18 +357,18 @@ function ir_inline_linetable!(linetable::Vector{LineInfoNode}, inlinee_ir::IRCod
358357
end
359358

360359
function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCode, IncrementalCompact},
361-
linetable::Vector{LineInfoNode}, ir′::IRCode, sparam_vals::SimpleVector,
362-
mi::MethodInstance, inlined_at::Int32, argexprs::Vector{Any})
360+
ir::IRCode, mi::MethodInstance, inlined_at::Int32, argexprs::Vector{Any})
363361
def = mi.def::Method
362+
linetable = inline_target isa IRCode ? inline_target.linetable : inline_target.ir.linetable
364363
topline::Int32 = length(linetable) + Int32(1)
365-
linetable_offset, extra_coverage_line = ir_inline_linetable!(linetable, ir, mi, inlined_at)
364+
linetable_offset, extra_coverage_line = ir_inline_linetable!(linetable, ir, mi, inlined_at)
366365
if extra_coverage_line != 0
367366
insert_node!(NewInstruction(Expr(:code_coverage_effect), Nothing, extra_coverage_line))
368367
end
369-
sp_ssa = nothing
370-
if !validate_sparams(sparam_vals)
368+
spvals_ssa = nothing
369+
if !validate_sparams(mi.sparam_vals)
371370
# N.B. This works on the caller-side argexprs, (i.e. before the va fixup below)
372-
sp_ssa = insert_node!(
371+
spvals_ssa = insert_node!(
373372
effect_free_and_nothrow(NewInstruction(Expr(:call, Core._compute_sparams, def, argexprs...), SimpleVector, topline)))
374373
end
375374
if def.isva
@@ -382,20 +381,17 @@ function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCod
382381
# Replace the first argument by a load of the capture environment
383382
argexprs[1] = insert_node!(
384383
NewInstruction(Expr(:call, GlobalRef(Core, :getfield), argexprs[1], QuoteNode(:captures)),
385-
ir.argtypes[1], topline))
384+
ir.argtypes[1], topline))
386385
end
387-
return (Pair{Union{Nothing, SSAValue}, Vector{Any}}(sp_ssa, argexprs), linetable_offset)
386+
return SSASubstitute(mi, argexprs, spvals_ssa, linetable_offset)
388387
end
389388

390389
function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
391-
linetable::Vector{LineInfoNode}, item::InliningTodo,
392-
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
390+
item::InliningTodo, boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
393391
# Ok, do the inlining here
394-
sparam_vals = item.mi.sparam_vals
395392
inlined_at = compact.result[idx][:line]
396393

397-
((sp_ssa, argexprs), linetable_offset) = ir_prepare_inlining!(InsertHere(compact),
398-
compact, linetable, item.ir, sparam_vals, item.mi, inlined_at, argexprs)
394+
ssa_substitute = ir_prepare_inlining!(InsertHere(compact), compact, item.ir, item.mi, inlined_at, argexprs)
399395

400396
if boundscheck === :default || boundscheck === :propagate
401397
if (compact.result[idx][:flag] & IR_FLAG_INBOUNDS) != 0
@@ -405,8 +401,6 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
405401
# If the iterator already moved on to the next basic block,
406402
# temporarily re-open in again.
407403
local return_value
408-
def = item.mi.def::Method
409-
sig = def.sig
410404
# Special case inlining that maintains the current basic block if there's only one BB in the target
411405
new_new_offset = length(compact.new_new_nodes)
412406
late_fixup_offset = length(compact.late_fixup)
@@ -418,7 +412,9 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
418412
# face of rename_arguments! mutating in place - should figure out
419413
# something better eventually.
420414
inline_compact[idx′] = nothing
421-
stmt′ = ssa_substitute!(InsertBefore(inline_compact, SSAValue(idx′)), inline_compact[SSAValue(idx′)], stmt′, argexprs, sig, sparam_vals, sp_ssa, linetable_offset, boundscheck)
415+
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
416+
stmt′ = ssa_substitute!(insert_node!, inline_compact[SSAValue(idx′)], stmt′,
417+
ssa_substitute, boundscheck)
422418
if isa(stmt′, ReturnNode)
423419
val = stmt′.val
424420
return_value = SSAValue(idx′)
@@ -445,7 +441,9 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
445441
inline_compact = IncrementalCompact(compact, item.ir, compact.result_idx)
446442
for ((_, idx′), stmt′) in inline_compact
447443
inline_compact[idx′] = nothing
448-
stmt′ = ssa_substitute!(InsertBefore(inline_compact, SSAValue(idx′)), inline_compact[SSAValue(idx′)], stmt′, argexprs, sig, sparam_vals, sp_ssa, linetable_offset, boundscheck)
444+
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
445+
stmt′ = ssa_substitute!(insert_node!, inline_compact[SSAValue(idx′)], stmt′,
446+
ssa_substitute, boundscheck)
449447
if isa(stmt′, ReturnNode)
450448
if isdefined(stmt′, :val)
451449
val = stmt′.val
@@ -554,11 +552,10 @@ excluding cases where `case.sig::UnionAll`.
554552
In short, here we can process the dispatch candidates in order, assuming we haven't changed
555553
their order somehow somewhere up to this point.
556554
"""
557-
function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
558-
argexprs::Vector{Any}, linetable::Vector{LineInfoNode},
559-
(; fully_covered, atype, cases, bbs)::UnionSplit,
560-
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}},
561-
params::OptimizationParams)
555+
function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
556+
union_split::UnionSplit, boundscheck::Symbol,
557+
todo_bbs::Vector{Tuple{Int,Int}}, params::OptimizationParams)
558+
(; fully_covered, atype, cases, bbs) = union_split
562559
stmt, typ, line = compact.result[idx][:inst], compact.result[idx][:type], compact.result[idx][:line]
563560
join_bb = bbs[end]
564561
pn = PhiNode()
@@ -606,7 +603,7 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
606603
end
607604
end
608605
if isa(case, InliningTodo)
609-
val = ir_inline_item!(compact, idx, argexprs′, linetable, case, boundscheck, todo_bbs)
606+
val = ir_inline_item!(compact, idx, argexprs′, case, boundscheck, todo_bbs)
610607
elseif isa(case, InvokeCase)
611608
inst = Expr(:invoke, case.invoke, argexprs′...)
612609
flag = flags_for_effects(case.effects)
@@ -698,9 +695,9 @@ function batch_inline!(ir::IRCode, todo::Vector{Pair{Int,Any}}, propagate_inboun
698695
end
699696
end
700697
if isa(item, InliningTodo)
701-
compact.ssa_rename[old_idx] = ir_inline_item!(compact, idx, argexprs, ir.linetable, item, boundscheck, state.todo_bbs)
698+
compact.ssa_rename[old_idx] = ir_inline_item!(compact, idx, argexprs, item, boundscheck, state.todo_bbs)
702699
elseif isa(item, UnionSplit)
703-
compact.ssa_rename[old_idx] = ir_inline_unionsplit!(compact, idx, argexprs, ir.linetable, item, boundscheck, state.todo_bbs, params)
700+
compact.ssa_rename[old_idx] = ir_inline_unionsplit!(compact, idx, argexprs, item, boundscheck, state.todo_bbs, params)
704701
end
705702
compact[idx] = nothing
706703
refinish && finish_current_bb!(compact, 0)
@@ -1795,15 +1792,18 @@ function late_inline_special_case!(
17951792
return nothing
17961793
end
17971794

1798-
function ssa_substitute!(insert_node!::Inserter,
1799-
subst_inst::Instruction, @nospecialize(val), arg_replacements::Vector{Any},
1800-
@nospecialize(spsig), spvals::SimpleVector,
1801-
spvals_ssa::Union{Nothing, SSAValue},
1802-
linetable_offset::Int32, boundscheck::Symbol)
1795+
struct SSASubstitute
1796+
mi::MethodInstance
1797+
arg_replacements::Vector{Any}
1798+
spvals_ssa::Union{Nothing,SSAValue}
1799+
linetable_offset::Int32
1800+
end
1801+
1802+
function ssa_substitute!(insert_node!::Inserter, subst_inst::Instruction, @nospecialize(val),
1803+
ssa_substitute::SSASubstitute, boundscheck::Symbol)
18031804
subst_inst[:flag] &= ~IR_FLAG_INBOUNDS
1804-
subst_inst[:line] += linetable_offset
1805-
return ssa_substitute_op!(insert_node!, subst_inst,
1806-
val, arg_replacements, spsig, spvals, spvals_ssa, boundscheck)
1805+
subst_inst[:line] += ssa_substitute.linetable_offset
1806+
return ssa_substitute_op!(insert_node!, subst_inst, val, ssa_substitute, boundscheck)
18071807
end
18081808

18091809
function insert_spval!(insert_node!::Inserter, spvals_ssa::SSAValue, spidx::Int, do_isdefined::Bool)
@@ -1819,26 +1819,24 @@ function insert_spval!(insert_node!::Inserter, spvals_ssa::SSAValue, spidx::Int,
18191819
return (ret, tcheck_not)
18201820
end
18211821

1822-
function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
1823-
@nospecialize(val), arg_replacements::Vector{Any},
1824-
@nospecialize(spsig), spvals::SimpleVector,
1825-
spvals_ssa::Union{Nothing, SSAValue},
1826-
boundscheck::Symbol)
1822+
function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction, @nospecialize(val),
1823+
ssa_substitute::SSASubstitute, boundscheck::Symbol)
18271824
if isa(val, Argument)
1828-
return arg_replacements[val.n]
1825+
return ssa_substitute.arg_replacements[val.n]
18291826
end
18301827
if isa(val, Expr)
18311828
e = val::Expr
18321829
head = e.head
1830+
sparam_vals = ssa_substitute.mi.sparam_vals
18331831
if head === :static_parameter
18341832
spidx = e.args[1]::Int
1835-
val = spvals[spidx]
1833+
val = sparam_vals[spidx]
18361834
if !isa(val, TypeVar) && val !== Vararg
18371835
return quoted(val)
18381836
else
18391837
flag = subst_inst[:flag]
18401838
maybe_undef = (flag & IR_FLAG_NOTHROW) == 0 && isa(val, TypeVar)
1841-
(ret, tcheck_not) = insert_spval!(insert_node!, spvals_ssa::SSAValue, spidx, maybe_undef)
1839+
(ret, tcheck_not) = insert_spval!(insert_node!, ssa_substitute.spvals_ssa::SSAValue, spidx, maybe_undef)
18421840
if maybe_undef
18431841
insert_node!(
18441842
NewInstruction(Expr(:throw_undef_if_not, val.name, tcheck_not), Nothing))
@@ -1847,27 +1845,29 @@ function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
18471845
end
18481846
elseif head === :isdefined && isa(e.args[1], Expr) && e.args[1].head === :static_parameter
18491847
spidx = (e.args[1]::Expr).args[1]::Int
1850-
val = spvals[spidx]
1848+
val = sparam_vals[spidx]
18511849
if !isa(val, TypeVar)
18521850
return true
18531851
else
1854-
(_, tcheck_not) = insert_spval!(insert_node!, spvals_ssa::SSAValue, spidx, true)
1852+
(_, tcheck_not) = insert_spval!(insert_node!, ssa_substitute.spvals_ssa::SSAValue, spidx, true)
18551853
return tcheck_not
18561854
end
1857-
elseif head === :cfunction && spvals_ssa === nothing
1858-
@assert !isa(spsig, UnionAll) || !isempty(spvals)
1859-
e.args[3] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[3], spsig, spvals)
1855+
elseif head === :cfunction && ssa_substitute.spvals_ssa === nothing
1856+
msig = (ssa_substitute.mi.def::Method).sig
1857+
@assert !isa(msig, UnionAll) || !isempty(sparam_vals)
1858+
e.args[3] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[3], msig, sparam_vals)
18601859
e.args[4] = svec(Any[
1861-
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, spsig, spvals)
1860+
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, msig, sparam_vals)
18621861
for argt in e.args[4]::SimpleVector ]...)
1863-
elseif head === :foreigncall && spvals_ssa === nothing
1864-
@assert !isa(spsig, UnionAll) || !isempty(spvals)
1862+
elseif head === :foreigncall && ssa_substitute.spvals_ssa === nothing
1863+
msig = (ssa_substitute.mi.def::Method).sig
1864+
@assert !isa(msig, UnionAll) || !isempty(sparam_vals)
18651865
for i = 1:length(e.args)
18661866
if i == 2
1867-
e.args[2] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[2], spsig, spvals)
1867+
e.args[2] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[2], msig, sparam_vals)
18681868
elseif i == 3
18691869
e.args[3] = svec(Any[
1870-
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, spsig, spvals)
1870+
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, msig, sparam_vals)
18711871
for argt in e.args[3]::SimpleVector ]...)
18721872
end
18731873
end
@@ -1884,7 +1884,7 @@ function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
18841884
isa(val, Union{SSAValue, NewSSAValue}) && return val # avoid infinite loop
18851885
urs = userefs(val)
18861886
for op in urs
1887-
op[] = ssa_substitute_op!(insert_node!, subst_inst, op[], arg_replacements, spsig, spvals, spvals_ssa, boundscheck)
1887+
op[] = ssa_substitute_op!(insert_node!, subst_inst, op[], ssa_substitute, boundscheck)
18881888
end
18891889
return urs[]
18901890
end

0 commit comments

Comments
 (0)