Skip to content

Commit

Permalink
Add optimize option
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed May 17, 2022
1 parent 1f03f37 commit d5a9dc2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
20 changes: 20 additions & 0 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,26 @@ function ipo_escape_cache(mi_cache::MICache) where MICache
end
null_escape_cache(linfo::Union{InferenceResult,MethodInstance}) = nothing

"""
run_minimal_passes(
ci::CodeInfo,
sv::OptimizationState,
caller::InferenceResult,
) -> ir::IRCode
Transform a `CodeInfo` to an `IRCode`. Like [`run_passes`](@ref) but it does not run the
majority of optimization passes.
"""
function run_minimal_passes(ci::CodeInfo, sv::OptimizationState, caller::InferenceResult)
@timeit "convert" ir = convert_to_ircode(ci, sv)
@timeit "slot2reg" ir = slot2reg(ir, ci, sv)
@timeit "compact" ir = compact!(ir)
if JLOptions().debug_level == 2
@timeit "verify" (verify_ir(ir); verify_linetable(ir.linetable))
end
return ir
end

function run_passes(ci::CodeInfo, sv::OptimizationState, caller::InferenceResult)
@timeit "convert" ir = convert_to_ircode(ci, sv)
@timeit "slot2reg" ir = slot2reg(ir, ci, sv)
Expand Down
8 changes: 6 additions & 2 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -928,13 +928,17 @@ end
Infer a `method` and return an optimized `IRCode` with inferred `returntype` on success.
"""
function typeinf_ircode(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector)
function typeinf_ircode(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool)
frame = typeinf_frame(interp, method, atype, sparams, false)
frame === nothing && return nothing, Any
(; result) = frame
opt_params = OptimizationParams(interp)
opt = OptimizationState(frame, opt_params, interp)
ir = run_passes(opt.src, opt, result)
if run_optimizer
ir = run_passes(opt.src, opt, result)
else
ir = run_minimal_passes(opt.src, opt, result)
end
rt = widenconst(ignorelimited(result.result))
return ir, rt
end
Expand Down
13 changes: 10 additions & 3 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,7 @@ function code_ircode(
@nospecialize(types = default_tt(f));
world = get_world_counter(),
interp = Core.Compiler.NativeInterpreter(world),
optimize::Bool = true,
)
if isa(f, Core.OpaqueClosure)
error("OpaqueClosure not supported")
Expand All @@ -1337,7 +1338,7 @@ function code_ircode(
else
tt = Tuple{ft,types...}
end
return code_ircode_by_type(tt; world, interp)
return code_ircode_by_type(tt; world, interp, optimize)
end

"""
Expand All @@ -1350,6 +1351,7 @@ function code_ircode_by_type(
@nospecialize(tt::Type);
world = get_world_counter(),
interp = Core.Compiler.NativeInterpreter(world),
optimize::Bool = true,
)
ccall(:jl_is_in_pure_context, Bool, ()) &&
error("code reflection cannot be used from generated functions")
Expand All @@ -1359,8 +1361,13 @@ function code_ircode_by_type(
for match in matches
match = match::Core.MethodMatch
meth = func_for_method_checked(match.method, tt, match.sparams)
(code, ty) =
Core.Compiler.typeinf_ircode(interp, meth, match.spec_types, match.sparams)
(code, ty) = Core.Compiler.typeinf_ircode(
interp,
meth,
match.spec_types,
match.sparams,
optimize,
)
if code === nothing
push!(asts, meth => Any)
else
Expand Down
9 changes: 7 additions & 2 deletions test/compiler/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,14 @@ end

@testset "code_ircode" begin
@test first(only(Base.code_ircode(+, (Float64, Float64)))) isa Compiler.IRCode
@test Base.code_ircode() do f
@test first(only(Base.code_ircode(+, (Float64, Float64); optimize = false))) isa
Compiler.IRCode

function demo(f)
f()
f()
f()
end |> only |> first isa Compiler.IRCode
end
@test first(only(Base.code_ircode(demo))) isa Compiler.IRCode
@test first(only(Base.code_ircode(demo; optimize = false))) isa Compiler.IRCode
end

0 comments on commit d5a9dc2

Please sign in to comment.