Skip to content

Commit 5479a40

Browse files
committed
tidy up compiler implementation
- remove `Effects(sv::InferenceState)` utility: replace all the usages with `sv.ipo_effects`, which is more explictly saying that we are looking at IPO-valid effects - use more `update_valid_age!(::InferenceState, ::InferenceState)` - normalize more `li::MethodInstance` to `mi::MethodInstance` - import `Core.MethodTable` - fix up `setindex!` return values
1 parent 608e26d commit 5479a40

File tree

10 files changed

+51
-51
lines changed

10 files changed

+51
-51
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function should_infer_this_call(interp::AbstractInterpreter, sv::InferenceState)
4444
end
4545

4646
function should_infer_for_effects(sv::InferenceState)
47-
effects = Effects(sv)
47+
effects = sv.ipo_effects
4848
return is_terminates(effects) && is_effect_free(effects)
4949
end
5050

@@ -255,7 +255,7 @@ struct MethodMatches
255255
applicable::Vector{Any}
256256
info::MethodMatchInfo
257257
valid_worlds::WorldRange
258-
mt::Core.MethodTable
258+
mt::MethodTable
259259
fullmatch::Bool
260260
nonoverlayed::Bool
261261
end
@@ -267,7 +267,7 @@ struct UnionSplitMethodMatches
267267
applicable_argtypes::Vector{Vector{Any}}
268268
info::UnionSplitInfo
269269
valid_worlds::WorldRange
270-
mts::Vector{Core.MethodTable}
270+
mts::Vector{MethodTable}
271271
fullmatches::Vector{Bool}
272272
nonoverlayed::Bool
273273
end
@@ -282,15 +282,15 @@ function find_matching_methods(argtypes::Vector{Any}, @nospecialize(atype), meth
282282
applicable = Any[]
283283
applicable_argtypes = Vector{Any}[] # arrays like `argtypes`, including constants, for each match
284284
valid_worlds = WorldRange()
285-
mts = Core.MethodTable[]
285+
mts = MethodTable[]
286286
fullmatches = Bool[]
287287
nonoverlayed = true
288288
for i in 1:length(split_argtypes)
289289
arg_n = split_argtypes[i]::Vector{Any}
290290
sig_n = argtypes_to_type(arg_n)
291291
mt = ccall(:jl_method_table_for, Any, (Any,), sig_n)
292292
mt === nothing && return FailedMethodMatch("Could not identify method table for call")
293-
mt = mt::Core.MethodTable
293+
mt = mt::MethodTable
294294
result = findall(sig_n, method_table; limit = max_methods)
295295
if result === nothing
296296
return FailedMethodMatch("For one of the union split cases, too many methods matched")
@@ -329,7 +329,7 @@ function find_matching_methods(argtypes::Vector{Any}, @nospecialize(atype), meth
329329
if mt === nothing
330330
return FailedMethodMatch("Could not identify method table for call")
331331
end
332-
mt = mt::Core.MethodTable
332+
mt = mt::MethodTable
333333
result = findall(atype, method_table; limit = max_methods)
334334
if result === nothing
335335
# this means too many methods matched
@@ -3081,7 +3081,7 @@ function typeinf_nocycle(interp::AbstractInterpreter, frame::InferenceState)
30813081
typeinf_local(interp, caller)
30823082
no_active_ips_in_callers = false
30833083
end
3084-
caller.valid_worlds = intersect(caller.valid_worlds, frame.valid_worlds)
3084+
update_valid_age!(caller, frame)
30853085
end
30863086
end
30873087
return true

base/compiler/cicache.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ Internally, each `MethodInstance` keep a unique global cache of code instances
77
that have been created for the given method instance, stratified by world age
88
ranges. This struct abstracts over access to this cache.
99
"""
10-
struct InternalCodeCache
11-
end
10+
struct InternalCodeCache end
1211

1312
function setindex!(cache::InternalCodeCache, ci::CodeInstance, mi::MethodInstance)
1413
ccall(:jl_mi_cache_insert, Cvoid, (Any, Any), mi, ci)
14+
return cache
1515
end
1616

1717
const GLOBAL_CI_CACHE = InternalCodeCache()
@@ -49,11 +49,11 @@ WorldView(wvc::WorldView, wr::WorldRange) = WorldView(wvc.cache, wr)
4949
WorldView(wvc::WorldView, args...) = WorldView(wvc.cache, args...)
5050

5151
function haskey(wvc::WorldView{InternalCodeCache}, mi::MethodInstance)
52-
ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds))::Union{Nothing, CodeInstance} !== nothing
52+
return ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds)) !== nothing
5353
end
5454

5555
function get(wvc::WorldView{InternalCodeCache}, mi::MethodInstance, default)
56-
r = ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds))::Union{Nothing, CodeInstance}
56+
r = ccall(:jl_rettype_inferred, Any, (Any, UInt, UInt), mi, first(wvc.worlds), last(wvc.worlds))
5757
if r === nothing
5858
return default
5959
end
@@ -66,5 +66,7 @@ function getindex(wvc::WorldView{InternalCodeCache}, mi::MethodInstance)
6666
return r::CodeInstance
6767
end
6868

69-
setindex!(wvc::WorldView{InternalCodeCache}, ci::CodeInstance, mi::MethodInstance) =
69+
function setindex!(wvc::WorldView{InternalCodeCache}, ci::CodeInstance, mi::MethodInstance)
7070
setindex!(wvc.cache, ci, mi)
71+
return wvc
72+
end

base/compiler/compiler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Core.Intrinsics, Core.IR
66

77
import Core: print, println, show, write, unsafe_write, stdout, stderr,
88
_apply_iterate, svec, apply_type, Builtin, IntrinsicFunction,
9-
MethodInstance, CodeInstance, MethodMatch, PartialOpaque,
9+
MethodInstance, CodeInstance, MethodTable, MethodMatch, PartialOpaque,
1010
TypeofVararg
1111

1212
const getproperty = Core.getfield

base/compiler/inferencestate.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ end
195195
is_inferred(sv::InferenceState) = is_inferred(sv.result)
196196
is_inferred(result::InferenceResult) = result.result !== nothing
197197

198-
Effects(state::InferenceState) = state.ipo_effects
199-
200198
function merge_effects!(::AbstractInterpreter, caller::InferenceState, effects::Effects)
201199
caller.ipo_effects = merge_effects(caller.ipo_effects, effects)
202200
end
@@ -505,10 +503,10 @@ end
505503
_topmod(sv::InferenceState) = _topmod(sv.mod)
506504

507505
# work towards converging the valid age range for sv
508-
function update_valid_age!(sv::InferenceState, worlds::WorldRange)
509-
sv.valid_worlds = intersect(worlds, sv.valid_worlds)
510-
@assert(sv.world in sv.valid_worlds, "invalid age range update")
511-
nothing
506+
function update_valid_age!(sv::InferenceState, valid_worlds::WorldRange)
507+
valid_worlds = sv.valid_worlds = intersect(valid_worlds, sv.valid_worlds)
508+
@assert(sv.world in valid_worlds, "invalid age range update")
509+
return valid_worlds
512510
end
513511

514512
update_valid_age!(edge::InferenceState, sv::InferenceState) = update_valid_age!(sv, edge.valid_worlds)
@@ -546,24 +544,24 @@ function add_cycle_backedge!(caller::InferenceState, frame::InferenceState, curr
546544
end
547545

548546
# temporarily accumulate our edges to later add as backedges in the callee
549-
function add_backedge!(caller::InferenceState, li::MethodInstance)
547+
function add_backedge!(caller::InferenceState, mi::MethodInstance)
550548
edges = get_stmt_edges!(caller)
551549
if edges !== nothing
552-
push!(edges, li)
550+
push!(edges, mi)
553551
end
554552
return nothing
555553
end
556554

557-
function add_invoke_backedge!(caller::InferenceState, @nospecialize(invokesig::Type), li::MethodInstance)
555+
function add_invoke_backedge!(caller::InferenceState, @nospecialize(invokesig::Type), mi::MethodInstance)
558556
edges = get_stmt_edges!(caller)
559557
if edges !== nothing
560-
push!(edges, invokesig, li)
558+
push!(edges, invokesig, mi)
561559
end
562560
return nothing
563561
end
564562

565563
# used to temporarily accumulate our no method errors to later add as backedges in the callee method table
566-
function add_mt_backedge!(caller::InferenceState, mt::Core.MethodTable, @nospecialize(typ))
564+
function add_mt_backedge!(caller::InferenceState, mt::MethodTable, @nospecialize(typ))
567565
edges = get_stmt_edges!(caller)
568566
if edges !== nothing
569567
push!(edges, mt, typ)

base/compiler/methodtable.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ external table, e.g., to override existing method.
3939
"""
4040
struct OverlayMethodTable <: MethodTableView
4141
world::UInt
42-
mt::Core.MethodTable
42+
mt::MethodTable
4343
end
4444

4545
struct MethodMatchKey
@@ -98,7 +98,7 @@ function findall(@nospecialize(sig::Type), table::OverlayMethodTable; limit::Int
9898
!isempty(result))
9999
end
100100

101-
function _findall(@nospecialize(sig::Type), mt::Union{Nothing,Core.MethodTable}, world::UInt, limit::Int)
101+
function _findall(@nospecialize(sig::Type), mt::Union{Nothing,MethodTable}, world::UInt, limit::Int)
102102
_min_val = RefValue{UInt}(typemin(UInt))
103103
_max_val = RefValue{UInt}(typemax(UInt))
104104
_ambig = RefValue{Int32}(0)
@@ -155,7 +155,7 @@ function findsup(@nospecialize(sig::Type), table::OverlayMethodTable)
155155
false)
156156
end
157157

158-
function _findsup(@nospecialize(sig::Type), mt::Union{Nothing,Core.MethodTable}, world::UInt)
158+
function _findsup(@nospecialize(sig::Type), mt::Union{Nothing,MethodTable}, world::UInt)
159159
min_valid = RefValue{UInt}(typemin(UInt))
160160
max_valid = RefValue{UInt}(typemax(UInt))
161161
match = ccall(:jl_gf_invoke_lookup_worlds, Any, (Any, Any, UInt, Ptr{Csize_t}, Ptr{Csize_t}),

base/compiler/ssair/inlining.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,15 +852,15 @@ function resolve_todo(mi::MethodInstance, result::Union{MethodMatch,InferenceRes
852852
#XXX: update_valid_age!(min_valid[1], max_valid[1], sv)
853853
if isa(result, InferenceResult)
854854
src = result.src
855-
if is_foldable_nothrow(result.ipo_effects)
855+
effects = result.ipo_effects
856+
if is_foldable_nothrow(effects)
856857
res = result.result
857858
if isa(res, Const) && is_inlineable_constant(res.val)
858859
# use constant calling convention
859860
add_inlining_backedge!(et, mi)
860861
return ConstantCase(quoted(res.val))
861862
end
862863
end
863-
effects = result.ipo_effects
864864
else
865865
cached_result = get_cached_result(state, mi)
866866
if cached_result isa ConstantCase

base/compiler/ssair/ir.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ end
936936

937937
function setindex!(compact::IncrementalCompact, @nospecialize(v), idx::SSAValue)
938938
@assert idx.id < compact.result_idx
939-
(compact.result[idx.id][:inst] === v) && return
939+
(compact.result[idx.id][:inst] === v) && return compact
940940
# Kill count for current uses
941941
kill_current_uses!(compact, compact.result[idx.id][:inst])
942942
compact.result[idx.id][:inst] = v
@@ -949,7 +949,7 @@ function setindex!(compact::IncrementalCompact, @nospecialize(v), idx::OldSSAVal
949949
id = idx.id
950950
if id < compact.idx
951951
new_idx = compact.ssa_rename[id]
952-
(compact.result[new_idx][:inst] === v) && return
952+
(compact.result[new_idx][:inst] === v) && return compact
953953
kill_current_uses!(compact, compact.result[new_idx][:inst])
954954
compact.result[new_idx][:inst] = v
955955
count_added_node!(compact, v) && push!(compact.late_fixup, new_idx)

base/compiler/tfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,7 @@ function _hasmethod_tfunc(interp::AbstractInterpreter, argtypes::Vector{Any}, sv
26052605
types = rewrap_unionall(Tuple{ft, unwrapped.parameters...}, types)::Type
26062606
end
26072607
mt = ccall(:jl_method_table_for, Any, (Any,), types)
2608-
if !isa(mt, Core.MethodTable)
2608+
if !isa(mt, MethodTable)
26092609
return CallMeta(Bool, EFFECTS_THROWS, NoCallInfo())
26102610
end
26112611
match, valid_worlds, overlayed = findsup(types, method_table(interp))

base/compiler/typeinfer.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ function cycle_fix_limited(@nospecialize(typ), sv::InferenceState)
431431
end
432432

433433
function adjust_effects(sv::InferenceState)
434-
ipo_effects = Effects(sv)
434+
ipo_effects = sv.ipo_effects
435435

436436
# refine :consistent-cy effect using the return type information
437437
# TODO this adjustment tries to compromise imprecise :consistent-cy information,
@@ -577,7 +577,7 @@ function store_backedges(frame::MethodInstance, edges::Vector{Any})
577577
if isa(caller, MethodInstance)
578578
ccall(:jl_method_instance_add_backedge, Cvoid, (Any, Any, Any), caller, sig, frame)
579579
else
580-
typeassert(caller, Core.MethodTable)
580+
typeassert(caller, MethodTable)
581581
ccall(:jl_method_table_add_backedge, Cvoid, (Any, Any, Any), caller, sig, frame)
582582
end
583583
end
@@ -792,28 +792,28 @@ function merge_call_chain!(interp::AbstractInterpreter, parent::InferenceState,
792792
end
793793
end
794794

795-
function is_same_frame(interp::AbstractInterpreter, linfo::MethodInstance, frame::InferenceState)
796-
return linfo === frame.linfo
795+
function is_same_frame(interp::AbstractInterpreter, mi::MethodInstance, frame::InferenceState)
796+
return mi === frame.linfo
797797
end
798798

799799
function poison_callstack(infstate::InferenceState, topmost::InferenceState)
800800
push!(infstate.pclimitations, topmost)
801801
nothing
802802
end
803803

804-
# Walk through `linfo`'s upstream call chain, starting at `parent`. If a parent
805-
# frame matching `linfo` is encountered, then there is a cycle in the call graph
806-
# (i.e. `linfo` is a descendant callee of itself). Upon encountering this cycle,
804+
# Walk through `mi`'s upstream call chain, starting at `parent`. If a parent
805+
# frame matching `mi` is encountered, then there is a cycle in the call graph
806+
# (i.e. `mi` is a descendant callee of itself). Upon encountering this cycle,
807807
# we "resolve" it by merging the call chain, which entails unioning each intermediary
808808
# frame's `callers_in_cycle` field and adding the appropriate backedges. Finally,
809-
# we return `linfo`'s pre-existing frame. If no cycles are found, `nothing` is
809+
# we return `mi`'s pre-existing frame. If no cycles are found, `nothing` is
810810
# returned instead.
811-
function resolve_call_cycle!(interp::AbstractInterpreter, linfo::MethodInstance, parent::InferenceState)
811+
function resolve_call_cycle!(interp::AbstractInterpreter, mi::MethodInstance, parent::InferenceState)
812812
frame = parent
813813
uncached = false
814814
while isa(frame, InferenceState)
815815
uncached |= !frame.cached # ensure we never add an uncached frame to a cycle
816-
if is_same_frame(interp, linfo, frame)
816+
if is_same_frame(interp, mi, frame)
817817
if uncached
818818
# our attempt to speculate into a constant call lead to an undesired self-cycle
819819
# that cannot be converged: poison our call-stack (up to the discovered duplicate frame)
@@ -825,7 +825,7 @@ function resolve_call_cycle!(interp::AbstractInterpreter, linfo::MethodInstance,
825825
return frame
826826
end
827827
for caller in frame.callers_in_cycle
828-
if is_same_frame(interp, linfo, caller)
828+
if is_same_frame(interp, mi, caller)
829829
if uncached
830830
poison_callstack(parent, frame)
831831
return true
@@ -918,7 +918,7 @@ function typeinf_edge(interp::AbstractInterpreter, method::Method, @nospecialize
918918
typeinf(interp, frame)
919919
update_valid_age!(frame, caller)
920920
edge = is_inferred(frame) ? mi : nothing
921-
return EdgeCallResult(frame.bestguess, edge, Effects(frame)) # effects are adjusted already within `finish`
921+
return EdgeCallResult(frame.bestguess, edge, frame.ipo_effects) # effects are adjusted already within `finish`
922922
elseif frame === true
923923
# unresolvable cycle
924924
return EdgeCallResult(Any, nothing, Effects())

base/compiler/utilities.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ Return an iterator over a list of backedges. Iteration returns `(sig, caller)` e
271271
which will be one of the following:
272272
273273
- `BackedgePair(nothing, caller::MethodInstance)`: a call made by ordinary inferable dispatch
274-
- `BackedgePair(invokesig, caller::MethodInstance)`: a call made by `invoke(f, invokesig, args...)`
275-
- `BackedgePair(specsig, mt::MethodTable)`: an abstract call
274+
- `BackedgePair(invokesig::Type, caller::MethodInstance)`: a call made by `invoke(f, invokesig, args...)`
275+
- `BackedgePair(specsig::Type, mt::MethodTable)`: an abstract call
276276
277277
# Examples
278278
@@ -305,17 +305,17 @@ const empty_backedge_iter = BackedgeIterator(Any[])
305305

306306
struct BackedgePair
307307
sig # ::Union{Nothing,Type}
308-
caller::Union{MethodInstance,Core.MethodTable}
309-
BackedgePair(@nospecialize(sig), caller::Union{MethodInstance,Core.MethodTable}) = new(sig, caller)
308+
caller::Union{MethodInstance,MethodTable}
309+
BackedgePair(@nospecialize(sig), caller::Union{MethodInstance,MethodTable}) = new(sig, caller)
310310
end
311311

312312
function iterate(iter::BackedgeIterator, i::Int=1)
313313
backedges = iter.backedges
314314
i > length(backedges) && return nothing
315315
item = backedges[i]
316-
isa(item, MethodInstance) && return BackedgePair(nothing, item), i+1 # regular dispatch
317-
isa(item, Core.MethodTable) && return BackedgePair(backedges[i+1], item), i+2 # abstract dispatch
318-
return BackedgePair(item, backedges[i+1]::MethodInstance), i+2 # `invoke` calls
316+
isa(item, MethodInstance) && return BackedgePair(nothing, item), i+1 # regular dispatch
317+
isa(item, MethodTable) && return BackedgePair(backedges[i+1], item), i+2 # abstract dispatch
318+
return BackedgePair(item, backedges[i+1]::MethodInstance), i+2 # `invoke` calls
319319
end
320320

321321
#########

0 commit comments

Comments
 (0)