Skip to content

Commit 151ef23

Browse files
authored
EAUtils: make sure to always run analysis on entry frame (#51402)
Otherwise we may hit error when analyzing the same function multiple times.
1 parent 4923e95 commit 151ef23

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

base/compiler/typeinfer.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,9 @@ end
969969
# compute an inferred frame
970970
function typeinf_frame(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool)
971971
mi = specialize_method(method, atype, sparams)::MethodInstance
972+
return typeinf_frame(interp, mi, run_optimizer)
973+
end
974+
function typeinf_frame(interp::AbstractInterpreter, mi::MethodInstance, run_optimizer::Bool)
972975
start_time = ccall(:jl_typeinf_timing_begin, UInt64, ())
973976
result = InferenceResult(mi, typeinf_lattice(interp))
974977
frame = InferenceState(result, run_optimizer ? :global : :no, interp)

test/compiler/EscapeAnalysis/EAUtils.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ function code_escapes(@nospecialize(f), @nospecialize(types=Base.default_tt(f));
4444
match = Base._which(tt; world, raise=true)
4545
mi = Core.Compiler.specialize_method(match)::MethodInstance
4646
interp = EscapeAnalyzer(world, mi)
47-
Core.Compiler.typeinf_ext(interp, mi)
47+
frame = Core.Compiler.typeinf_frame(interp, mi, #=run_optimizer=#true)
4848
isdefined(interp, :result) || error("optimization didn't happen: maybe everything has been constant folded?")
49-
return EscapeResult(interp.result.ir, interp.result.estate, interp.result.mi, debuginfo === :source, interp)
49+
slotnames = let src = frame.src
50+
src isa CodeInfo ? src.slotnames : nothing
51+
end
52+
return EscapeResult(interp.result.ir, interp.result.estate, interp.result.mi,
53+
slotnames, debuginfo === :source, interp)
5054
end
5155

5256
# in order to run a whole analysis from ground zero (e.g. for benchmarking, etc.)
@@ -285,13 +289,15 @@ struct EscapeResult
285289
ir::IRCode
286290
state::EscapeState
287291
mi::Union{Nothing,MethodInstance}
292+
slotnames::Union{Nothing,Vector{Symbol}}
288293
source::Bool
289294
interp::Union{Nothing,EscapeAnalyzer}
290295
function EscapeResult(ir::IRCode, state::EscapeState,
291296
mi::Union{Nothing,MethodInstance}=nothing,
297+
slotnames::Union{Nothing,Vector{Symbol}}=nothing,
292298
source::Bool=false,
293299
interp::Union{Nothing,EscapeAnalyzer}=nothing)
294-
return new(ir, state, mi, source, interp)
300+
return new(ir, state, mi, slotnames, source, interp)
295301
end
296302
end
297303
Base.show(io::IO, result::EscapeResult) = print_with_info(io, result)
@@ -301,7 +307,8 @@ Base.show(io::IO, result::EscapeResult) = print_with_info(io, result)
301307
Base.show(io::IO, cached::EscapeCacheInfo) = show(io, EscapeResult(cached.ir, cached.state))
302308

303309
# adapted from https://github.com/JuliaDebug/LoweredCodeUtils.jl/blob/4612349432447e868cf9285f647108f43bd0a11c/src/codeedges.jl#L881-L897
304-
function print_with_info(io::IO, (; ir, state, mi, source)::EscapeResult)
310+
function print_with_info(io::IO, result::EscapeResult)
311+
(; ir, state, mi, slotnames, source) = result
305312
# print escape information on SSA values
306313
function preprint(io::IO)
307314
ft = ir.argtypes[1]
@@ -314,7 +321,8 @@ function print_with_info(io::IO, (; ir, state, mi, source)::EscapeResult)
314321
arg = state[Argument(i)]
315322
i == 1 && continue
316323
c, color = get_name_color(arg, true)
317-
printstyled(io, c, ' ', '_', i, "::", ir.argtypes[i]; color)
324+
slot = isnothing(slotnames) ? "_$i" : slotnames[i]
325+
printstyled(io, c, ' ', slot, "::", ir.argtypes[i]; color)
318326
i state.nargs && print(io, ", ")
319327
end
320328
print(io, ')')

test/compiler/EscapeAnalysis/EscapeAnalysis.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ end
7373

7474
@testset "EAUtils" begin
7575
@test_throws "everything has been constant folded" code_escapes() do; sin(42); end
76+
@test code_escapes(sin, (Int,)) isa EAUtils.EscapeResult
77+
@test code_escapes(sin, (Int,)) isa EAUtils.EscapeResult
7678
end
7779

7880
@testset "basics" begin

0 commit comments

Comments
 (0)