Skip to content

Commit 1843201

Browse files
authored
optimizer: fix #42246, set ssaflags correctly when inserting coverage statements (#42260)
It seems that this change still doesn't resolve #42258 though.
1 parent 7c8cbf6 commit 1843201

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

base/compiler/optimize.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const SLOT_USEDUNDEF = 32 # slot has uses that might raise UndefVarError
142142

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

145+
const IR_FLAG_NULL = 0x00
145146
# This statement is marked as @inbounds by user.
146147
# Ff replaced by inlining, any contained boundschecks may be removed.
147148
const IR_FLAG_INBOUNDS = 0x01 << 0
@@ -349,15 +350,18 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
349350
labelmap = coverage ? fill(0, length(code)) : changemap
350351
prevloc = zero(eltype(ci.codelocs))
351352
stmtinfo = sv.stmt_info
353+
codelocs = ci.codelocs
352354
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
355+
ssaflags = ci.ssaflags
353356
while idx <= length(code)
354-
codeloc = ci.codelocs[idx]
357+
codeloc = codelocs[idx]
355358
if coverage && codeloc != prevloc && codeloc != 0
356359
# insert a side-effect instruction before the current instruction in the same basic block
357360
insert!(code, idx, Expr(:code_coverage_effect))
358-
insert!(ci.codelocs, idx, codeloc)
361+
insert!(codelocs, idx, codeloc)
359362
insert!(ssavaluetypes, idx, Nothing)
360363
insert!(stmtinfo, idx, nothing)
364+
insert!(ssaflags, idx, IR_FLAG_NULL)
361365
changemap[oldidx] += 1
362366
if oldidx < length(labelmap)
363367
labelmap[oldidx + 1] += 1
@@ -369,9 +373,10 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
369373
if !(idx < length(code) && isa(code[idx + 1], ReturnNode) && !isdefined((code[idx + 1]::ReturnNode), :val))
370374
# insert unreachable in the same basic block after the current instruction (splitting it)
371375
insert!(code, idx + 1, ReturnNode())
372-
insert!(ci.codelocs, idx + 1, ci.codelocs[idx])
376+
insert!(codelocs, idx + 1, codelocs[idx])
373377
insert!(ssavaluetypes, idx + 1, Union{})
374378
insert!(stmtinfo, idx + 1, nothing)
379+
insert!(ssaflags, idx + 1, ssaflags[idx])
375380
if oldidx < length(changemap)
376381
changemap[oldidx + 1] += 1
377382
coverage && (labelmap[oldidx + 1] += 1)
@@ -391,7 +396,7 @@ function convert_to_ircode(ci::CodeInfo, code::Vector{Any}, coverage::Bool, sv::
391396
strip_trailing_junk!(ci, code, stmtinfo)
392397
cfg = compute_basic_blocks(code)
393398
types = Any[]
394-
stmts = InstructionStream(code, types, stmtinfo, ci.codelocs, ci.ssaflags)
399+
stmts = InstructionStream(code, types, stmtinfo, codelocs, ssaflags)
395400
ir = IRCode(stmts, cfg, collect(LineInfoNode, ci.linetable::Union{Vector{LineInfoNode},Vector{Any}}), sv.slottypes, meta, sv.sptypes)
396401
return ir
397402
end

base/compiler/ssair/ir.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ end
172172
NewInstruction(@nospecialize(stmt), @nospecialize(type)) =
173173
NewInstruction(stmt, type, nothing)
174174
NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{Nothing, Int32}) =
175-
NewInstruction(stmt, type, nothing, line, 0x00, false)
175+
NewInstruction(stmt, type, nothing, line, IR_FLAG_NULL, false)
176176

177177
effect_free(inst::NewInstruction) =
178178
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag | IR_FLAG_EFFECT_FREE, true)
@@ -193,7 +193,7 @@ function InstructionStream(len::Int)
193193
info = Array{Any}(undef, len)
194194
fill!(info, nothing)
195195
lines = fill(Int32(0), len)
196-
flags = fill(0x00, len)
196+
flags = fill(IR_FLAG_NULL, len)
197197
return InstructionStream(insts, types, info, lines, flags)
198198
end
199199
InstructionStream() = InstructionStream(0)
@@ -221,7 +221,7 @@ function resize!(stmts::InstructionStream, len)
221221
resize!(stmts.flag, len)
222222
for i in (old_length + 1):len
223223
stmts.line[i] = 0
224-
stmts.flag[i] = 0x00
224+
stmts.flag[i] = IR_FLAG_NULL
225225
stmts.info[i] = nothing
226226
end
227227
return stmts

base/compiler/ssair/legacy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function replace_code_newstyle!(ci::CodeInfo, ir::IRCode, nargs::Int)
4848
push!(ci.code, metanode)
4949
push!(ci.codelocs, 1)
5050
push!(ci.ssavaluetypes::Vector{Any}, Any)
51-
push!(ci.ssaflags, 0x00)
51+
push!(ci.ssaflags, IR_FLAG_NULL)
5252
end
5353
# Translate BB Edges to statement edges
5454
# (and undo normalization for now)

base/compiler/ssair/slot2ssa.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function strip_trailing_junk!(ci::CodeInfo, code::Vector{Any}, info::Vector{Any}
195195
push!(ssavaluetypes, Union{})
196196
push!(ci.codelocs, 0)
197197
push!(info, nothing)
198-
push!(ci.ssaflags, 0x00)
198+
push!(ci.ssaflags, IR_FLAG_NULL)
199199
end
200200
nothing
201201
end

base/compiler/typeinfer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance)
866866
tree.code = Any[ ReturnNode(quoted(rettype_const)) ]
867867
nargs = Int(method.nargs)
868868
tree.slotnames = ccall(:jl_uncompress_argnames, Vector{Symbol}, (Any,), method.slot_syms)
869-
tree.slotflags = fill(0x00, nargs)
869+
tree.slotflags = fill(IR_FLAG_NULL, nargs)
870870
tree.ssavaluetypes = 1
871871
tree.codelocs = Int32[1]
872872
tree.linetable = [LineInfoNode(method.module, method.name, method.file, Int(method.line), 0)]

test/compiler/inline.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,25 @@ let
650650
@test ninlined == length(code)
651651
end
652652
end
653+
654+
# https://github.com/JuliaLang/julia/issues/42246
655+
@test mktempdir() do dir
656+
cd(dir) do
657+
code = quote
658+
issue42246() = @noinline IOBuffer("a")
659+
let
660+
ci, rt = only(code_typed(issue42246))
661+
if any(ci.code) do stmt
662+
Meta.isexpr(stmt, :invoke) &&
663+
stmt.args[1].def.name === nameof(IOBuffer)
664+
end
665+
exit(0)
666+
else
667+
exit(1)
668+
end
669+
end
670+
end |> string
671+
cmd = `$(Base.julia_cmd()) --code-coverage=tmp.info -e $code`
672+
success(pipeline(Cmd(cmd); stdout=stdout, stderr=stderr))
673+
end
674+
end

0 commit comments

Comments
 (0)