Skip to content

Commit 2636722

Browse files
authored
inference: rename refactoring for constant analysis (#44888)
The naming `ConstResult` is a bit ambiguous, given that we have several different concepts of "constant" inference: - constant prop': `InferenceResult` - concrete evaluation: `ConstResult` - WIP: semi-concrete evaluation: `SemiConcreteResult` - (and more in the future...?) (better to be `ConcreteResult` instead) Now I'd like to rename `ConstResult` to `ConcreteResult` and also setup a specific data type `ConstPropResult` representing const-prop'ed result: - constant prop': `ConstPropResult` - concrete evaluation: `ConcreteResult` - WIP: semi-concrete evaluation: `SemiConcreteResult` - (and more in the future...?) And now `ConstResult` is a general type alias that is equivalent to `Union{ConstPropResult,ConcreteResult,...}`.
1 parent 4c72343 commit 2636722

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
8484
conditionals = nothing # keeps refinement information of call argument types when the return type is boolean
8585
seen = 0 # number of signatures actually inferred
8686
any_const_result = false
87-
const_results = Union{InferenceResult,Nothing,ConstResult}[]
87+
const_results = Union{Nothing,ConstResult}[]
8888
multiple_matches = napplicable > 1
8989
fargs = arginfo.fargs
9090
all_effects = EFFECTS_TOTAL
@@ -752,13 +752,13 @@ function concrete_eval_call(interp::AbstractInterpreter,
752752
Core._call_in_world_total(world, f, args...)
753753
catch
754754
# The evaulation threw. By :consistent-cy, we're guaranteed this would have happened at runtime
755-
return ConstCallResults(Union{}, ConstResult(result.edge, result.edge_effects), result.edge_effects)
755+
return ConstCallResults(Union{}, ConcreteResult(result.edge, result.edge_effects), result.edge_effects)
756756
end
757757
if is_inlineable_constant(value) || call_result_unused(sv)
758758
# If the constant is not inlineable, still do the const-prop, since the
759759
# code that led to the creation of the Const may be inlineable in the same
760760
# circumstance and may be optimizable.
761-
return ConstCallResults(Const(value), ConstResult(result.edge, EFFECTS_TOTAL, value), EFFECTS_TOTAL)
761+
return ConstCallResults(Const(value), ConcreteResult(result.edge, EFFECTS_TOTAL, value), EFFECTS_TOTAL)
762762
end
763763
return nothing
764764
end
@@ -778,10 +778,10 @@ end
778778

779779
struct ConstCallResults
780780
rt::Any
781-
const_result::Union{InferenceResult, ConstResult}
781+
const_result::ConstResult
782782
effects::Effects
783783
ConstCallResults(@nospecialize(rt),
784-
const_result::Union{InferenceResult, ConstResult},
784+
const_result::ConstResult,
785785
effects::Effects) =
786786
new(rt, const_result, effects)
787787
end
@@ -831,7 +831,7 @@ function abstract_call_method_with_const_args(interp::AbstractInterpreter, resul
831831
# if constant inference hits a cycle, just bail out
832832
isa(result, InferenceState) && return nothing
833833
add_backedge!(mi, sv)
834-
return ConstCallResults(result, inf_result, inf_result.ipo_effects)
834+
return ConstCallResults(result, ConstPropResult(inf_result), inf_result.ipo_effects)
835835
end
836836

837837
# if there's a possibility we could get a better result (hopefully without doing too much work)

base/compiler/ssair/EscapeAnalysis/interprocedural.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# TODO this file contains many duplications with the inlining analysis code, factor them out
22

33
import Core.Compiler:
4-
MethodInstance, InferenceResult, Signature, ConstResult,
4+
MethodInstance, InferenceResult, Signature, ConstPropResult, ConcreteResult,
55
MethodResultPure, MethodMatchInfo, UnionSplitInfo, ConstCallInfo, InvokeCallInfo,
66
call_sig, argtypes_to_type, is_builtin, is_return_type, istopfunction, validate_sparams,
77
specialize_method, invoke_rewrite
@@ -62,8 +62,8 @@ function analyze_invoke_call(sig::Signature, info::InvokeCallInfo)
6262
return missing
6363
end
6464
result = info.result
65-
if isa(result, InferenceResult)
66-
return CallInfo(Linfo[result], true)
65+
if isa(result, ConstPropResult)
66+
return CallInfo(Linfo[result.result], true)
6767
else
6868
argtypes = invoke_rewrite(sig.argtypes)
6969
mi = analyze_match(match, length(argtypes))
@@ -95,11 +95,11 @@ function analyze_const_call(sig::Signature, cinfo::ConstCallInfo)
9595
mi = analyze_match(match, length(sig.argtypes))
9696
mi === nothing && return missing
9797
push!(linfos, mi)
98-
elseif isa(result, ConstResult)
98+
elseif isa(result, ConcreteResult)
9999
# TODO we may want to feedback information that this call always throws if !isdefined(result, :result)
100100
push!(linfos, result.mi)
101-
else
102-
push!(linfos, result)
101+
elseif isa(result, ConstPropResult)
102+
push!(linfos, result.result)
103103
end
104104
nothrow &= match.fully_covers
105105
end

base/compiler/ssair/inlining.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,12 +1098,12 @@ function inline_invoke!(
10981098
return nothing
10991099
end
11001100
result = info.result
1101-
if isa(result, ConstResult)
1102-
item = const_result_item(result, state)
1101+
if isa(result, ConcreteResult)
1102+
item = concrete_result_item(result, state)
11031103
else
11041104
argtypes = invoke_rewrite(sig.argtypes)
1105-
if isa(result, InferenceResult)
1106-
(; mi) = item = InliningTodo(result, argtypes)
1105+
if isa(result, ConstPropResult)
1106+
(; mi) = item = InliningTodo(result.result, argtypes)
11071107
validate_sparams(mi.sparam_vals) || return nothing
11081108
if argtypes_to_type(argtypes) <: mi.def.sig
11091109
state.mi_cache !== nothing && (item = resolve_todo(item, state, flag))
@@ -1285,11 +1285,11 @@ function handle_const_call!(
12851285
j += 1
12861286
result = results[j]
12871287
any_fully_covered |= match.fully_covers
1288-
if isa(result, ConstResult)
1289-
case = const_result_item(result, state)
1288+
if isa(result, ConcreteResult)
1289+
case = concrete_result_item(result, state)
12901290
push!(cases, InliningCase(result.mi.specTypes, case))
1291-
elseif isa(result, InferenceResult)
1292-
handled_all_cases &= handle_inf_result!(result, argtypes, flag, state, cases, true)
1291+
elseif isa(result, ConstPropResult)
1292+
handled_all_cases &= handle_const_prop_result!(result, argtypes, flag, state, cases, true)
12931293
else
12941294
@assert result === nothing
12951295
handled_all_cases &= handle_match!(match, argtypes, flag, state, cases, true)
@@ -1321,10 +1321,10 @@ function handle_match!(
13211321
return true
13221322
end
13231323

1324-
function handle_inf_result!(
1325-
result::InferenceResult, argtypes::Vector{Any}, flag::UInt8, state::InliningState,
1324+
function handle_const_prop_result!(
1325+
result::ConstPropResult, argtypes::Vector{Any}, flag::UInt8, state::InliningState,
13261326
cases::Vector{InliningCase}, allow_abstract::Bool = false)
1327-
(; mi) = item = InliningTodo(result, argtypes)
1327+
(; mi) = item = InliningTodo(result.result, argtypes)
13281328
spec_types = mi.specTypes
13291329
allow_abstract || isdispatchtuple(spec_types) || return false
13301330
validate_sparams(mi.sparam_vals) || return false
@@ -1334,7 +1334,7 @@ function handle_inf_result!(
13341334
return true
13351335
end
13361336

1337-
function const_result_item(result::ConstResult, state::InliningState)
1337+
function concrete_result_item(result::ConcreteResult, state::InliningState)
13381338
if !isdefined(result, :result) || !is_inlineable_constant(result.result)
13391339
return compileable_specialization(state.et, result.mi, result.effects)
13401340
end
@@ -1361,9 +1361,9 @@ function handle_cases!(ir::IRCode, idx::Int, stmt::Expr, @nospecialize(atype),
13611361
end
13621362

13631363
function handle_const_opaque_closure_call!(
1364-
ir::IRCode, idx::Int, stmt::Expr, result::InferenceResult, flag::UInt8,
1364+
ir::IRCode, idx::Int, stmt::Expr, result::ConstPropResult, flag::UInt8,
13651365
sig::Signature, state::InliningState, todo::Vector{Pair{Int, Any}})
1366-
item = InliningTodo(result, sig.argtypes)
1366+
item = InliningTodo(result.result, sig.argtypes)
13671367
isdispatchtuple(item.mi.specTypes) || return
13681368
validate_sparams(item.mi.sparam_vals) || return
13691369
state.mi_cache !== nothing && (item = resolve_todo(item, state, flag))
@@ -1407,13 +1407,13 @@ function assemble_inline_todo!(ir::IRCode, state::InliningState)
14071407

14081408
if isa(info, OpaqueClosureCallInfo)
14091409
result = info.result
1410-
if isa(result, InferenceResult)
1410+
if isa(result, ConstPropResult)
14111411
handle_const_opaque_closure_call!(
14121412
ir, idx, stmt, result, flag,
14131413
sig, state, todo)
14141414
else
1415-
if isa(result, ConstResult)
1416-
item = const_result_item(result, state)
1415+
if isa(result, ConcreteResult)
1416+
item = concrete_result_item(result, state)
14171417
else
14181418
item = analyze_method!(info.match, sig.argtypes, flag, state)
14191419
end

base/compiler/stmtinfo.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,30 @@ function nmatches(info::UnionSplitInfo)
4747
return n
4848
end
4949

50-
struct ConstResult
50+
struct ConstPropResult
51+
result::InferenceResult
52+
end
53+
54+
struct ConcreteResult
5155
mi::MethodInstance
5256
effects::Effects
5357
result
54-
ConstResult(mi::MethodInstance, effects::Effects) = new(mi, effects)
55-
ConstResult(mi::MethodInstance, effects::Effects, @nospecialize val) = new(mi, effects, val)
58+
ConcreteResult(mi::MethodInstance, effects::Effects) = new(mi, effects)
59+
ConcreteResult(mi::MethodInstance, effects::Effects, @nospecialize val) = new(mi, effects, val)
5660
end
5761

62+
const ConstResult = Union{ConstPropResult,ConcreteResult}
63+
5864
"""
5965
info::ConstCallInfo
6066
6167
The precision of this call was improved using constant information.
62-
In addition to the original call information `info.call`, this info also keeps
63-
the inference results with constant information `info.results::Vector{Union{Nothing,InferenceResult}}`.
68+
In addition to the original call information `info.call`, this info also keeps the results
69+
of constant inference `info.results::Vector{Union{Nothing,ConstResult}}`.
6470
"""
6571
struct ConstCallInfo
6672
call::Union{MethodMatchInfo,UnionSplitInfo}
67-
results::Vector{Union{Nothing,InferenceResult,ConstResult}}
73+
results::Vector{Union{Nothing,ConstResult}}
6874
end
6975

7076
"""
@@ -130,7 +136,7 @@ Optionally keeps `info.result::InferenceResult` that keeps constant information.
130136
"""
131137
struct InvokeCallInfo
132138
match::MethodMatch
133-
result::Union{Nothing,InferenceResult,ConstResult}
139+
result::Union{Nothing,ConstResult}
134140
end
135141

136142
"""
@@ -142,7 +148,7 @@ Optionally keeps `info.result::InferenceResult` that keeps constant information.
142148
"""
143149
struct OpaqueClosureCallInfo
144150
match::MethodMatch
145-
result::Union{Nothing,InferenceResult,ConstResult}
151+
result::Union{Nothing,ConstResult}
146152
end
147153

148154
"""

0 commit comments

Comments
 (0)