Skip to content

Commit fc775c5

Browse files
authored
add missing setting of inferred field when setting inference result (#55081)
This previously could confuse inference, which expects that the field is set to indicate that the rettype has been computed, and cannot tolerate putting objects in the cache for which that is not true. This was causing Nanosoldier to fail. Also cleanup `sv.unreachable` after IR modification so that it remains (mostly) consistent, even though unused (except for the unreachable nodes themselves), as this was confusing me in debugging.
1 parent ec90012 commit fc775c5

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

base/compiler/optimize.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,20 +1178,19 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
11781178
# Any statements from here to the end of the block have been wrapped in Core.Const(...)
11791179
# by type inference (effectively deleting them). Only task left is to replace the block
11801180
# terminator with an explicit `unreachable` marker.
1181-
if block_end > idx
1182-
code[block_end] = ReturnNode()
1183-
codelocs[3block_end-2], codelocs[3block_end-1], codelocs[3block_end-0] = (codelocs[3idx-2], codelocs[3idx-1], codelocs[3idx-0])
1184-
ssavaluetypes[block_end] = Union{}
1185-
stmtinfo[block_end] = NoCallInfo()
1186-
ssaflags[block_end] = IR_FLAG_NOTHROW
11871181

1188-
# Verify that type-inference did its job
1182+
if block_end > idx
11891183
if is_asserts()
1184+
# Verify that type-inference did its job
11901185
for i = (oldidx + 1):last(sv.cfg.blocks[block].stmts)
11911186
@assert i in sv.unreachable
11921187
end
11931188
end
1194-
1189+
code[block_end] = ReturnNode()
1190+
codelocs[3block_end-2], codelocs[3block_end-1], codelocs[3block_end-0] = (codelocs[3idx-2], codelocs[3idx-1], codelocs[3idx-0])
1191+
ssavaluetypes[block_end] = Union{}
1192+
stmtinfo[block_end] = NoCallInfo()
1193+
ssaflags[block_end] = IR_FLAG_NOTHROW
11951194
idx += block_end - idx
11961195
else
11971196
insert!(code, idx + 1, ReturnNode())
@@ -1221,6 +1220,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
12211220
idx += 1
12221221
oldidx += 1
12231222
end
1223+
empty!(sv.unreachable)
12241224

12251225
if ssachangemap !== nothing && labelchangemap !== nothing
12261226
renumber_ir_elements!(code, ssachangemap, labelchangemap)

base/compiler/typeinfer.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,20 +369,17 @@ function cache_result!(interp::AbstractInterpreter, result::InferenceResult)
369369
# we can now widen our applicability in the global cache too
370370
result.valid_worlds = WorldRange(first(result.valid_worlds), typemax(UInt))
371371
end
372+
@assert isdefined(result.ci, :inferred)
372373
# check if the existing linfo metadata is also sufficient to describe the current inference result
373374
# to decide if it is worth caching this right now
374375
mi = result.linfo
375376
cache_results = true
376377
cache = WorldView(code_cache(interp), result.valid_worlds)
377378
if cache_results && haskey(cache, mi)
378379
ci = cache[mi]
379-
# Even if we already have a CI for this, it's possible that the new CI has more
380-
# information (E.g. because the source was limited before, but is no longer - this
381-
# happens during bootstrap). In that case, allow the result to be recached.
382380
# n.b.: accurate edge representation might cause the CodeInstance for this to be constructed later
383-
if isdefined(ci, :inferred)
384-
cache_results = false
385-
end
381+
@assert isdefined(ci, :inferred)
382+
cache_results = false
386383
end
387384

388385
if cache_results
@@ -1205,6 +1202,7 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance, source_mod
12051202
if source_mode == SOURCE_MODE_ABI && frame.cache_mode != CACHE_MODE_GLOBAL
12061203
# XXX: jl_type_infer somewhat ambiguously assumes this must be cached, while jl_ci_cache_lookup sort of ambiguously re-caches it
12071204
# XXX: this should be using the CI from the cache, if possible instead: haskey(cache, mi) && (ci = cache[mi])
1205+
@assert isdefined(ci, :inferred) "interpreter did not fulfill its requirements"
12081206
code_cache(interp)[mi] = ci
12091207
end
12101208
if source_mode == SOURCE_MODE_FORCE_SOURCE && use_const_api(ci)

src/gf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ JL_DLLEXPORT void jl_fill_codeinst(
609609
codeinst->analysis_results = analysis_results;
610610
assert(jl_atomic_load_relaxed(&codeinst->min_world) == 1);
611611
assert(jl_atomic_load_relaxed(&codeinst->max_world) == 0);
612+
jl_atomic_store_release(&codeinst->inferred, jl_nothing);
612613
jl_atomic_store_release(&codeinst->min_world, min_world);
613614
jl_atomic_store_release(&codeinst->max_world, max_world);
614615
}

0 commit comments

Comments
 (0)