Skip to content

Commit

Permalink
EAUtils: make sure to always run analysis on entry frame (JuliaLang#5…
Browse files Browse the repository at this point in the history
…1402)

Otherwise we may hit error when analyzing the same function multiple
times.
  • Loading branch information
aviatesk committed Sep 20, 2023
1 parent 4923e95 commit 151ef23
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,9 @@ end
# compute an inferred frame
function typeinf_frame(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool)
mi = specialize_method(method, atype, sparams)::MethodInstance
return typeinf_frame(interp, mi, run_optimizer)
end
function typeinf_frame(interp::AbstractInterpreter, mi::MethodInstance, run_optimizer::Bool)
start_time = ccall(:jl_typeinf_timing_begin, UInt64, ())
result = InferenceResult(mi, typeinf_lattice(interp))
frame = InferenceState(result, run_optimizer ? :global : :no, interp)
Expand Down
18 changes: 13 additions & 5 deletions test/compiler/EscapeAnalysis/EAUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ function code_escapes(@nospecialize(f), @nospecialize(types=Base.default_tt(f));
match = Base._which(tt; world, raise=true)
mi = Core.Compiler.specialize_method(match)::MethodInstance
interp = EscapeAnalyzer(world, mi)
Core.Compiler.typeinf_ext(interp, mi)
frame = Core.Compiler.typeinf_frame(interp, mi, #=run_optimizer=#true)
isdefined(interp, :result) || error("optimization didn't happen: maybe everything has been constant folded?")
return EscapeResult(interp.result.ir, interp.result.estate, interp.result.mi, debuginfo === :source, interp)
slotnames = let src = frame.src
src isa CodeInfo ? src.slotnames : nothing
end
return EscapeResult(interp.result.ir, interp.result.estate, interp.result.mi,
slotnames, debuginfo === :source, interp)
end

# in order to run a whole analysis from ground zero (e.g. for benchmarking, etc.)
Expand Down Expand Up @@ -285,13 +289,15 @@ struct EscapeResult
ir::IRCode
state::EscapeState
mi::Union{Nothing,MethodInstance}
slotnames::Union{Nothing,Vector{Symbol}}
source::Bool
interp::Union{Nothing,EscapeAnalyzer}
function EscapeResult(ir::IRCode, state::EscapeState,
mi::Union{Nothing,MethodInstance}=nothing,
slotnames::Union{Nothing,Vector{Symbol}}=nothing,
source::Bool=false,
interp::Union{Nothing,EscapeAnalyzer}=nothing)
return new(ir, state, mi, source, interp)
return new(ir, state, mi, slotnames, source, interp)
end
end
Base.show(io::IO, result::EscapeResult) = print_with_info(io, result)
Expand All @@ -301,7 +307,8 @@ Base.show(io::IO, result::EscapeResult) = print_with_info(io, result)
Base.show(io::IO, cached::EscapeCacheInfo) = show(io, EscapeResult(cached.ir, cached.state))

# adapted from https://github.com/JuliaDebug/LoweredCodeUtils.jl/blob/4612349432447e868cf9285f647108f43bd0a11c/src/codeedges.jl#L881-L897
function print_with_info(io::IO, (; ir, state, mi, source)::EscapeResult)
function print_with_info(io::IO, result::EscapeResult)
(; ir, state, mi, slotnames, source) = result
# print escape information on SSA values
function preprint(io::IO)
ft = ir.argtypes[1]
Expand All @@ -314,7 +321,8 @@ function print_with_info(io::IO, (; ir, state, mi, source)::EscapeResult)
arg = state[Argument(i)]
i == 1 && continue
c, color = get_name_color(arg, true)
printstyled(io, c, ' ', '_', i, "::", ir.argtypes[i]; color)
slot = isnothing(slotnames) ? "_$i" : slotnames[i]
printstyled(io, c, ' ', slot, "::", ir.argtypes[i]; color)
i state.nargs && print(io, ", ")
end
print(io, ')')
Expand Down
2 changes: 2 additions & 0 deletions test/compiler/EscapeAnalysis/EscapeAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ end

@testset "EAUtils" begin
@test_throws "everything has been constant folded" code_escapes() do; sin(42); end
@test code_escapes(sin, (Int,)) isa EAUtils.EscapeResult
@test code_escapes(sin, (Int,)) isa EAUtils.EscapeResult
end

@testset "basics" begin
Expand Down

0 comments on commit 151ef23

Please sign in to comment.