Skip to content

Commit

Permalink
optimizer: fix #42246, set ssaflags correctly when inserting covera…
Browse files Browse the repository at this point in the history
…ge statements (#42260)

It seems that this change still doesn't resolve #42258 though.
  • Loading branch information
aviatesk committed Sep 20, 2021
1 parent 7c8cbf6 commit 1843201
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
13 changes: 9 additions & 4 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const SLOT_USEDUNDEF = 32 # slot has uses that might raise UndefVarError

# NOTE make sure to sync the flag definitions below with julia.h and `jl_code_info_set_ir` in method.c

const IR_FLAG_NULL = 0x00
# This statement is marked as @inbounds by user.
# Ff replaced by inlining, any contained boundschecks may be removed.
const IR_FLAG_INBOUNDS = 0x01 << 0
Expand Down Expand Up @@ -349,15 +350,18 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
labelmap = coverage ? fill(0, length(code)) : changemap
prevloc = zero(eltype(ci.codelocs))
stmtinfo = sv.stmt_info
codelocs = ci.codelocs
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
ssaflags = ci.ssaflags
while idx <= length(code)
codeloc = ci.codelocs[idx]
codeloc = codelocs[idx]
if 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!(ci.codelocs, idx, codeloc)
insert!(codelocs, idx, codeloc)
insert!(ssavaluetypes, idx, Nothing)
insert!(stmtinfo, idx, nothing)
insert!(ssaflags, idx, IR_FLAG_NULL)
changemap[oldidx] += 1
if oldidx < length(labelmap)
labelmap[oldidx + 1] += 1
Expand All @@ -369,9 +373,10 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
if !(idx < length(code) && isa(code[idx + 1], ReturnNode) && !isdefined((code[idx + 1]::ReturnNode), :val))
# insert unreachable in the same basic block after the current instruction (splitting it)
insert!(code, idx + 1, ReturnNode())
insert!(ci.codelocs, idx + 1, ci.codelocs[idx])
insert!(codelocs, idx + 1, codelocs[idx])
insert!(ssavaluetypes, idx + 1, Union{})
insert!(stmtinfo, idx + 1, nothing)
insert!(ssaflags, idx + 1, ssaflags[idx])
if oldidx < length(changemap)
changemap[oldidx + 1] += 1
coverage && (labelmap[oldidx + 1] += 1)
Expand All @@ -391,7 +396,7 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
strip_trailing_junk!(ci, code, stmtinfo)
cfg = compute_basic_blocks(code)
types = Any[]
stmts = InstructionStream(code, types, stmtinfo, ci.codelocs, ci.ssaflags)
stmts = InstructionStream(code, types, stmtinfo, codelocs, ssaflags)
ir = IRCode(stmts, cfg, collect(LineInfoNode, ci.linetable::Union{Vector{LineInfoNode},Vector{Any}}), sv.slottypes, meta, sv.sptypes)
return ir
end
Expand Down
6 changes: 3 additions & 3 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ end
NewInstruction(@nospecialize(stmt), @nospecialize(type)) =
NewInstruction(stmt, type, nothing)
NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{Nothing, Int32}) =
NewInstruction(stmt, type, nothing, line, 0x00, false)
NewInstruction(stmt, type, nothing, line, IR_FLAG_NULL, false)

effect_free(inst::NewInstruction) =
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag | IR_FLAG_EFFECT_FREE, true)
Expand All @@ -193,7 +193,7 @@ function InstructionStream(len::Int)
info = Array{Any}(undef, len)
fill!(info, nothing)
lines = fill(Int32(0), len)
flags = fill(0x00, len)
flags = fill(IR_FLAG_NULL, len)
return InstructionStream(insts, types, info, lines, flags)
end
InstructionStream() = InstructionStream(0)
Expand Down Expand Up @@ -221,7 +221,7 @@ function resize!(stmts::InstructionStream, len)
resize!(stmts.flag, len)
for i in (old_length + 1):len
stmts.line[i] = 0
stmts.flag[i] = 0x00
stmts.flag[i] = IR_FLAG_NULL
stmts.info[i] = nothing
end
return stmts
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/legacy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function replace_code_newstyle!(ci::CodeInfo, ir::IRCode, nargs::Int)
push!(ci.code, metanode)
push!(ci.codelocs, 1)
push!(ci.ssavaluetypes::Vector{Any}, Any)
push!(ci.ssaflags, 0x00)
push!(ci.ssaflags, IR_FLAG_NULL)
end
# Translate BB Edges to statement edges
# (and undo normalization for now)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function strip_trailing_junk!(ci::CodeInfo, code::Vector{Any}, info::Vector{Any}
push!(ssavaluetypes, Union{})
push!(ci.codelocs, 0)
push!(info, nothing)
push!(ci.ssaflags, 0x00)
push!(ci.ssaflags, IR_FLAG_NULL)
end
nothing
end
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance)
tree.code = Any[ ReturnNode(quoted(rettype_const)) ]
nargs = Int(method.nargs)
tree.slotnames = ccall(:jl_uncompress_argnames, Vector{Symbol}, (Any,), method.slot_syms)
tree.slotflags = fill(0x00, nargs)
tree.slotflags = fill(IR_FLAG_NULL, nargs)
tree.ssavaluetypes = 1
tree.codelocs = Int32[1]
tree.linetable = [LineInfoNode(method.module, method.name, method.file, Int(method.line), 0)]
Expand Down
22 changes: 22 additions & 0 deletions test/compiler/inline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,25 @@ let
@test ninlined == length(code)
end
end

# https://github.com/JuliaLang/julia/issues/42246
@test mktempdir() do dir
cd(dir) do
code = quote
issue42246() = @noinline IOBuffer("a")
let
ci, rt = only(code_typed(issue42246))
if any(ci.code) do stmt
Meta.isexpr(stmt, :invoke) &&
stmt.args[1].def.name === nameof(IOBuffer)
end
exit(0)
else
exit(1)
end
end
end |> string
cmd = `$(Base.julia_cmd()) --code-coverage=tmp.info -e $code`
success(pipeline(Cmd(cmd); stdout=stdout, stderr=stderr))
end
end

0 comments on commit 1843201

Please sign in to comment.