Skip to content

Commit bdd0d25

Browse files
author
Ian Atol
committed
Inital addressing of review
1 parent 8dc5d87 commit bdd0d25

File tree

9 files changed

+41
-64
lines changed

9 files changed

+41
-64
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
132132
f, this_arginfo, match, sv)
133133
const_result = nothing
134134
if const_call_result !== nothing
135-
const_rt = const_call_result.rt
136-
if const_rt rt
137-
rt = const_rt
135+
if const_call_result.rt rt
136+
rt = const_call_result.rt
138137
(; effects, const_result) = const_call_result
139138
end
140139
end
@@ -388,7 +387,7 @@ function collect_limitations!(@nospecialize(typ), sv::InferenceState)
388387
end
389388

390389
function collect_limitations!(@nospecialize(typ), ::IRCode)
391-
@assert !isa(typ, LimitedAccuracy)
390+
@assert !isa(typ, LimitedAccuracy) "semi-concrete eval on recursive call graph"
392391
return typ
393392
end
394393

@@ -813,8 +812,16 @@ function is_all_const_arg(argtypes::Vector{Any})
813812
return true
814813
end
815814

816-
collect_const_args((; argtypes)::ArgInfo) = collect_const_args(argtypes)
815+
collect_const_args(arginfo::ArgInfo, start::Int=2) = collect_const_args(arginfo.argtypes, start)
817816
function collect_const_args(argtypes::Vector{Any}, start::Int=2)
817+
return Any[ let a = widenconditional(argtypes[i])
818+
isa(a, Const) ? a.val :
819+
isconstType(a) ? (a::DataType).parameters[1] :
820+
(a::DataType).instance
821+
end for i = 2:length(argtypes) ]
822+
end
823+
824+
function collect_semi_const_args(argtypes::Vector{Any}, start::Int=2)
818825
return Any[ let a = widenconditional(argtypes[i])
819826
isa(a, Const) ? a.val :
820827
isconstType(a) ? (a::DataType).parameters[1] :
@@ -823,6 +830,7 @@ function collect_const_args(argtypes::Vector{Any}, start::Int=2)
823830
end for i in start:length(argtypes) ]
824831
end
825832

833+
826834
function invoke_signature(invokesig::Vector{Any})
827835
ft, argtyps = widenconst(invokesig[2]), instanceof_tfunc(widenconst(invokesig[3]))[1]
828836
return rewrap_unionall(Tuple{ft, unwrap_unionall(argtyps).parameters...}, argtyps)
@@ -886,7 +894,7 @@ function abstract_call_method_with_const_args(interp::AbstractInterpreter, resul
886894
end
887895
res = concrete_eval_call(interp, f, result, arginfo, sv)
888896
if isa(res, ConstCallResults)
889-
add_backedge!(result.edge, sv)
897+
add_backedge!(res.const_result.mi, sv)
890898
return res
891899
end
892900
mi = maybe_get_const_prop_profitable(interp, result, f, arginfo, match, sv)

base/compiler/inferencestate.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,7 @@ end
218218

219219
merge_effects!(interp::AbstractInterpreter, caller::InferenceState, callee::InferenceState) =
220220
merge_effects!(interp, caller, Effects(callee))
221-
222-
function merge_effects!(interp::AbstractInterpreter, caller::IRCode, effects::Effects)
223-
nothing
224-
end
221+
merge_effects!(interp::AbstractInterpreter, caller::IRCode, effects::Effects) = nothing
225222

226223
is_effect_overridden(sv::InferenceState, effect::Symbol) = is_effect_overridden(sv.linfo, effect)
227224
function is_effect_overridden(linfo::MethodInstance, effect::Symbol)

base/compiler/ssair/inlining.jl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,7 @@ function inline_invoke!(
11371137
end
11381138
result = info.result
11391139
if isa(result, ConcreteResult)
1140-
item = const_result_item(result, state)
1140+
item = concrete_result_item(result, state)
11411141
else
11421142
invokesig = invoke_signature(sig.argtypes)
11431143
argtypes = invoke_rewrite(sig.argtypes)
@@ -1367,14 +1367,12 @@ function compute_inlining_cases(info::ConstCallInfo,
13671367
result = results[j]
13681368
any_fully_covered |= match.fully_covers
13691369
if isa(result, ConcreteResult)
1370-
case = const_result_item(result, state)
1370+
case = concrete_result_item(result, state)
13711371
push!(cases, InliningCase(result.mi.specTypes, case))
13721372
elseif isa(result, ConstPropResult)
13731373
handled_all_cases &= handle_const_prop_result!(result, argtypes, flag, state, cases, #=allow_abstract=#true)
1374-
elseif isa(result, InferenceResult)
1375-
handled_all_cases &= handle_inf_result!(result, argtypes, flag, state, cases, true)
13761374
elseif isa(result, SemiConcreteResult)
1377-
handled_all_cases &= handle_semi_concrete_result!(result, cases, true)
1375+
handled_all_cases &= handle_semi_concrete_result!(result, cases, #=allow_abstract=#true)
13781376
else
13791377
@assert result === nothing
13801378
handled_all_cases &= handle_match!(match, argtypes, flag, state, cases, #=allow_abstract=#true, #=allow_typevars=#false)
@@ -1447,7 +1445,7 @@ function handle_semi_concrete_result!(result::SemiConcreteResult, cases::Vector{
14471445
return true
14481446
end
14491447

1450-
function const_result_item(result::ConstResult, state::InliningState)
1448+
function concrete_result_item(result::ConcreteResult, state::InliningState)
14511449
if !isdefined(result, :result) || !is_inlineable_constant(result.result)
14521450
case = compileable_specialization(state.et, result.mi, result.effects)
14531451
@assert case !== nothing "concrete evaluation should never happen for uncompileable callsite"
@@ -1573,7 +1571,7 @@ function assemble_inline_todo!(ir::IRCode, state::InliningState)
15731571
sig, state, todo)
15741572
else
15751573
if isa(result, ConcreteResult)
1576-
item = const_result_item(result, state)
1574+
item = concrete_result_item(result, state)
15771575
else
15781576
item = analyze_method!(info.match, sig.argtypes, nothing, flag, state)
15791577
end

base/compiler/ssair/irinterp.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function codeinst_to_ir(interp::AbstractInterpreter, code::CodeInstance)
44
mi = code.def
55

66
if isa(src, Vector{UInt8})
7-
src = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), mi.def, C_NULL, src::Vector{UInt8})::CodeInfo
7+
src = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), mi.def, C_NULL, src)::CodeInfo
88
end
99

1010
isa(src, CodeInfo) || return src
@@ -111,7 +111,7 @@ function concrete_eval_invoke(interp::AbstractInterpreter, ir::IRCode, mi_cache,
111111
argtypes = collect_argtypes(interp, inst.args[2:end], nothing, ir)
112112
effects = decode_effects(code.ipo_purity_bits)
113113
if is_foldable(effects) && is_all_const_arg(argtypes)
114-
args = collect_const_args(argtypes, 1)
114+
args = collect_semi_const_args(argtypes, 1)
115115
world = get_world_counter(interp)
116116
value = try
117117
Core._call_in_world_total(world, args...)
@@ -137,8 +137,8 @@ function reprocess_instruction!(interp::AbstractInterpreter, ir::IRCode, mi::Met
137137
mi_cache, sv::InferenceState,
138138
tpdum::TwoPhaseDefUseMap, idx::Int, bb::Union{Int, Nothing},
139139
@nospecialize(inst), @nospecialize(typ),
140-
phi_revisit)
141-
function update_phi!(from, to)
140+
phi_revisit::BitSet)
141+
function update_phi!(from::Int, to::Int)
142142
if length(ir.cfg.blocks[to].preds) == 0
143143
return
144144
end
@@ -224,14 +224,14 @@ function reprocess_instruction!(interp::AbstractInterpreter, ir::IRCode, mi::Met
224224
return false
225225
end
226226

227-
function _ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache, frame::InferenceState, mi::MethodInstance, ir, argtypes)
227+
function _ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache,
228+
frame::InferenceState, mi::MethodInstance, ir::IRCode, argtypes::Vector{Any})
228229
argtypes = va_process_argtypes(argtypes, mi)
229230
argtypes_refined = Bool[!(ir.argtypes[i] argtypes[i]) for i = 1:length(argtypes)]
230231
empty!(ir.argtypes)
231232
append!(ir.argtypes, argtypes)
232233
ssa_refined = BitSet()
233234

234-
ultimate_rt = Union{}
235235
bbs = ir.cfg.blocks
236236
ip = BitSetBoundedMinPrioritySet(length(bbs))
237237
push!(ip, 1)
@@ -245,7 +245,7 @@ function _ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache
245245
Process the terminator and add the successor to `ip`. Returns whether a
246246
backedge was seen.
247247
"""
248-
function process_terminator!(ip, bb, idx)
248+
function process_terminator!(ip::BitSetBoundedMinPrioritySet, bb::Int, idx::Int)
249249
inst = ir.stmts[idx][:inst]
250250
if isa(inst, ReturnNode)
251251
if isdefined(inst, :val)
@@ -378,14 +378,15 @@ function _ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache
378378
# Could have discovered this block is dead after the initial scan
379379
continue
380380
end
381-
inst = ir.stmts[idx][:inst]
381+
inst = ir.stmts[idx][:inst]::ReturnNode
382382
rt = argextype(inst.val, ir)
383383
ultimate_rt = tmerge(ultimate_rt, rt)
384384
end
385385
return ultimate_rt
386386
end
387387

388-
function ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache, frame::InferenceState, mi::MethodInstance, ir, argtypes)
388+
function ir_abstract_constant_propagation(interp::AbstractInterpreter, mi_cache,
389+
frame::InferenceState, mi::MethodInstance, ir::IRCode, argtypes::Vector{Any})
389390
if __measure_typeinf__[]
390391
inf_frame = Timings.InferenceFrameInfo(mi, frame.world, Any[], Any[], length(ir.argtypes))
391392
Timings.enter_new_timer(inf_frame)

base/compiler/stmtinfo.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct SemiConcreteResult
6767
end
6868

6969
const ConstResult = Union{ConstPropResult,ConcreteResult, SemiConcreteResult}
70+
7071
"""
7172
info::ConstCallInfo
7273

base/compiler/tfuncs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ function builtin_nothrow(@nospecialize(f), argtypes::Vector{Any}, @nospecialize(
20202020
return _builtin_nothrow(f, argtypes, rt)
20212021
end
20222022

2023-
function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtypes::Array{Any,1},
2023+
function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtypes::Vector{Any},
20242024
sv::Union{InferenceState,IRCode,Nothing})
20252025
if f === tuple
20262026
return tuple_tfunc(argtypes)

base/compiler/utilities.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,12 @@ function singleton_type(@nospecialize(ft))
294294
end
295295

296296
function maybe_singleton_const(@nospecialize(t))
297-
if isa(t, DataType) && isdefined(t, :instance)
298-
return Const(t.instance)
299-
elseif isconstType(t)
300-
return Const(t.parameters[1])
297+
if isa(t, DataType)
298+
if isdefined(t, :instance)
299+
return Const(t.instance)
300+
elseif isconstType(t)
301+
return Const(t.parameters[1])
302+
end
301303
end
302304
return t
303305
end

base/essentials.jl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,6 @@ struct Colon <: Function
763763
end
764764
const (:) = Colon()
765765

766-
# TODO: Change lowering to do this automatically
767-
@eval struct Val{x}
768-
(T::Type{Val{x}} where x)() = $(Expr(:new, :T))
769-
end
770-
771766

772767
"""
773768
Val(c)
@@ -789,7 +784,8 @@ julia> f(Val(true))
789784
"Good"
790785
```
791786
"""
792-
Val
787+
struct Val{x}
788+
end
793789

794790
Val(x) = Val{x}()
795791

test/compiler/inference.jl

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,7 +4116,6 @@ end
41164116
f45780() = Val{Issue45780(@Base.Experimental.opaque ()->1).oc()}()
41174117
@test (@inferred f45780()) == Val{1}()
41184118

4119-
41204119
# issue #45600
41214120
@test only(code_typed() do
41224121
while true
@@ -4192,31 +4191,6 @@ end
41924191

41934192
# Test that Const ⊑ PartialStruct respects vararg
41944193
@test Const((1,2)) PartialStruct(Tuple{Vararg{Int}}, [Const(1), Vararg{Int}])
4195-
# backedge insertion for Any-typed, effect-free frame
4196-
const CONST_DICT = let d = Dict()
4197-
for c in 'A':'z'
4198-
push!(d, c => Int(c))
4199-
end
4200-
d
4201-
end
4202-
Base.@assume_effects :foldable getcharid(c) = CONST_DICT[c]
4203-
@noinline callf(f, args...) = f(args...)
4204-
function entry_to_be_invalidated(c)
4205-
return callf(getcharid, c)
4206-
end
4207-
@test Base.infer_effects((Char,)) do x
4208-
entry_to_be_invalidated(x)
4209-
end |> Core.Compiler.is_foldable
4210-
@test fully_eliminated(; retval=97) do
4211-
entry_to_be_invalidated('a')
4212-
end
4213-
getcharid(c) = CONST_DICT[c] # now this is not eligible for concrete evaluation
4214-
@test Base.infer_effects((Char,)) do x
4215-
entry_to_be_invalidated(x)
4216-
end |> !Core.Compiler.is_foldable
4217-
@test !fully_eliminated() do
4218-
entry_to_be_invalidated('a')
4219-
end
42204194

42214195
# Test that semi-concrete interpretation doesn't break on functions with while loops in them.
42224196
@Base.assume_effects :consistent :effect_free :terminates_globally function pure_annotated_loop(x::Int, y::Int)
@@ -4226,4 +4200,4 @@ end
42264200
return y
42274201
end
42284202
call_pure_annotated_loop(x) = Val{pure_annotated_loop(x, 1)}()
4229-
@test Core.Compiler.return_type(call_pure_annotated_loop, Tuple{Int}) == Val{1}
4203+
@test only(Base.return_types(call_pure_annotated_loop, Tuple{Int})) === Val{1}

0 commit comments

Comments
 (0)