Skip to content

Commit 708c56a

Browse files
aviateskKristofferC
authored andcommitted
inlining: allow callsite inlining with cached results (#50048)
In some rare cases with callsite inlining, we try to inline an inferred result from a local cache (`inf_result::InferenceResult`), whose source has been transformed by `transform_result_for_cache`. At present, `inf_result.src` stays to be `OptimizationState` in such cases, causing `inlining_policy` to handle the callsite inlining. This commit adjusts `transform_result_for_cache` so that it stores the transformed source in `inf_result.src`, letting the callsite inliner use it. Down the line, we might revisit this change to align it with 532125d, which isn't enabled yet. (cherry picked from commit f407a4c)
1 parent 167708b commit 708c56a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

base/compiler/typeinfer.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,9 @@ end
367367
function transform_result_for_cache(interp::AbstractInterpreter,
368368
linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult)
369369
inferred_result = result.src
370-
# If we decided not to optimize, drop the OptimizationState now.
371-
# External interpreters can override as necessary to cache additional information
372370
if inferred_result isa OptimizationState{typeof(interp)}
373-
inferred_result = ir_to_codeinf!(inferred_result)
371+
# TODO respect must_be_codeinf setting here?
372+
result.src = inferred_result = ir_to_codeinf!(inferred_result)
374373
end
375374
if inferred_result isa CodeInfo
376375
inferred_result.min_world = first(valid_worlds)

test/compiler/inline.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,27 @@ end
709709
end
710710
end
711711

712+
# callsite inlining with cached frames
713+
issue49823_events = @NamedTuple{evid::Int8, base_time::Float64}[
714+
(evid = 1, base_time = 0.0), (evid = -1, base_time = 0.0)]
715+
issue49823_fl1(t, events) = @inline findlast(x -> x.evid (1, 4) && x.base_time <= t, events)
716+
issue49823_fl3(t, events) = @inline findlast(x -> any(==(x.evid), (1,4)) && x.base_time <= t, events)
717+
issue49823_fl5(t, events) = begin
718+
f = let t=t
719+
x -> x.evid (1, 4) && x.base_time <= t
720+
end
721+
@inline findlast(f, events)
722+
end
723+
let src = @code_typed1 issue49823_fl1(0.0, issue49823_events)
724+
@test count(isinvoke(:findlast), src.code) == 0 # successful inlining
725+
end
726+
let src = @code_typed1 issue49823_fl3(0.0, issue49823_events)
727+
@test count(isinvoke(:findlast), src.code) == 0 # successful inlining
728+
end
729+
let src = @code_typed1 issue49823_fl5(0.0, issue49823_events)
730+
@test count(isinvoke(:findlast), src.code) == 0 # successful inlining
731+
end
732+
712733
# Issue #42264 - crash on certain union splits
713734
let f(x) = (x...,)
714735
# Test splatting with a Union of non-{Tuple, SimpleVector} types that require creating new `iterate` calls

0 commit comments

Comments
 (0)