Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compiler] Remove ConstAPI struct #48598

Merged
merged 7 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move coverage effects analysis into inference
Code coverage is implemented as an optimization pass that inserts an SSA
IR node during optimization, however effects analysis is perfomed during
inference, and so the desired effects tainting that should occur from
insertion of the code coverage IR node is ignored.

This teaches `InferenceState` to taint itself with the effects flag
 `effects_free = ALWAYS_FALSE` when coverage is applicable to this
inference state's method.
  • Loading branch information
staticfloat committed Feb 9, 2023
commit f35381454a2cca99a1bd2d3c039eeae0a779212b
31 changes: 29 additions & 2 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ mutable struct InferenceState
# Set by default for toplevel frame.
restrict_abstract_call_sites::Bool
cached::Bool # TODO move this to InferenceResult?
insert_coverage::Bool

# The interpreter that created this inference state. Not looked at by
# NativeInterpreter. But other interpreters may use this to detect cycles
interp::AbstractInterpreter

# src is assumed to be a newly-allocated CodeInfo, that can be modified in-place to contain intermediate results
function InferenceState(result::InferenceResult, src::CodeInfo, cache::Symbol,
interp::AbstractInterpreter)
interp::AbstractInterpreter)
linfo = result.linfo
world = get_world_counter(interp)
def = linfo.def
Expand Down Expand Up @@ -179,6 +180,32 @@ mutable struct InferenceState
bestguess = Bottom
ipo_effects = EFFECTS_TOTAL

# check if coverage mode is enabled
insert_coverage = coverage_enabled(mod)
if !insert_coverage && JLOptions().code_coverage == 3 # path-specific coverage mode
linetable = src.linetable
if isa(linetable, Vector{Any})
for line in linetable
line = line::LineInfoNode
if is_file_tracked(line.file)
# if any line falls in a tracked file enable coverage for all
insert_coverage = true
break
end
end
elseif isa(linetable, Vector{LineInfo})
for line in linetable
if is_file_tracked(line.file)
insert_coverage = true
break
end
end
end
end
if insert_coverage
ipo_effects = Effects(ipo_effects; effect_free = ALWAYS_FALSE)
end

params = InferenceParams(interp)
restrict_abstract_call_sites = isa(linfo.def, Module)
@assert cache === :no || cache === :local || cache === :global
Expand All @@ -189,7 +216,7 @@ mutable struct InferenceState
currbb, currpc, ip, handler_at, ssavalue_uses, bb_vartables, ssavaluetypes, stmt_edges, stmt_info,
pclimitations, limitations, cycle_backedges, callers_in_cycle, dont_work_on_me, parent, inferred,
result, valid_worlds, bestguess, ipo_effects,
params, restrict_abstract_call_sites, cached,
params, restrict_abstract_call_sites, cached, insert_coverage,
interp)

# some more setups
Expand Down
23 changes: 6 additions & 17 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ mutable struct OptimizationState{Interp<:AbstractInterpreter}
slottypes::Vector{Any}
inlining::InliningState{Interp}
cfg::Union{Nothing,CFG}
insert_coverage::Bool
end
function OptimizationState(frame::InferenceState, params::OptimizationParams,
interp::AbstractInterpreter, recompute_cfg::Bool=true)
inlining = InliningState(frame, params, interp)
cfg = recompute_cfg ? nothing : frame.cfg
return OptimizationState(frame.linfo, frame.src, nothing, frame.stmt_info, frame.mod,
frame.sptypes, frame.slottypes, inlining, cfg)
frame.sptypes, frame.slottypes, inlining, cfg, frame.insert_coverage)
end
function OptimizationState(linfo::MethodInstance, src::CodeInfo, params::OptimizationParams,
interp::AbstractInterpreter)
Expand Down Expand Up @@ -540,18 +541,6 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
linetable = collect(LineInfoNode, linetable::Vector{Any})::Vector{LineInfoNode}
end

# check if coverage mode is enabled
coverage = coverage_enabled(sv.mod)
if !coverage && JLOptions().code_coverage == 3 # path-specific coverage mode
for line in linetable
if is_file_tracked(line.file)
# if any line falls in a tracked file enable coverage for all
coverage = true
break
end
end
end

# Go through and add an unreachable node after every
# Union{} call. Then reindex labels.
code = copy_exprargs(ci.code)
Expand All @@ -567,7 +556,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
prevloc = zero(eltype(ci.codelocs))
while idx <= length(code)
codeloc = codelocs[idx]
if coverage && codeloc != prevloc && codeloc != 0
if sv.insert_coverage && codeloc != prevloc && codeloc != 0
# insert a side-effect instruction before the current instruction in the same basic block
insert!(code, idx, Expr(:code_coverage_effect))
insert!(codelocs, idx, codeloc)
Expand All @@ -578,7 +567,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
ssachangemap = fill(0, nstmts)
end
if labelchangemap === nothing
labelchangemap = coverage ? fill(0, nstmts) : ssachangemap
labelchangemap = fill(0, nstmts)
end
ssachangemap[oldidx] += 1
if oldidx < length(labelchangemap)
Expand All @@ -599,11 +588,11 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
ssachangemap = fill(0, nstmts)
end
if labelchangemap === nothing
labelchangemap = coverage ? fill(0, nstmts) : ssachangemap
labelchangemap = sv.insert_coverage ? fill(0, nstmts) : ssachangemap
end
if oldidx < length(ssachangemap)
ssachangemap[oldidx + 1] += 1
coverage && (labelchangemap[oldidx + 1] += 1)
sv.insert_coverage && (labelchangemap[oldidx + 1] += 1)
end
idx += 1
end
Expand Down