Skip to content

Commit 6afde4b

Browse files
committed
set up a specific type to capture the 3-set data of codelocs
1 parent eb05b4f commit 6afde4b

File tree

6 files changed

+68
-27
lines changed

6 files changed

+68
-27
lines changed

base/compiler/ssair/inlining.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCod
321321
debuginfo = inline_target isa IRCode ? inline_target.debuginfo : inline_target.ir.debuginfo
322322

323323
linetable_offset = ir_inline_linetable!(debuginfo, di, mi)
324-
topline = (inlined_at, linetable_offset, Int32(0))
324+
topline = DebugCodeLoc(inlined_at, linetable_offset, Int32(0))
325325
if should_insert_coverage(def.module, di)
326326
insert_node!(NewInstruction(Expr(:code_coverage_effect), Nothing, topline))
327327
end
@@ -340,8 +340,9 @@ function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCod
340340
if def.is_for_opaque_closure
341341
# Replace the first argument by a load of the capture environment
342342
argexprs[1] = insert_node!(
343-
NewInstruction(Expr(:call, GlobalRef(Core, :getfield), argexprs[1], QuoteNode(:captures)),
344-
ir.argtypes[1], topline))
343+
NewInstruction(
344+
Expr(:call, GlobalRef(Core, :getfield), argexprs[1], QuoteNode(:captures)),
345+
ir.argtypes[1], topline))
345346
end
346347
return SSASubstitute(mi, argexprs, spvals_ssa, (inlined_at, linetable_offset))
347348
end
@@ -379,7 +380,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
379380
# something better eventually.
380381
inline_compact[idx′] = nothing
381382
# alter the line number information for InsertBefore to point to the current instruction in the new linetable
382-
inline_compact[SSAValue(idx′)][:line] = (ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], Int32(lineidx))
383+
inline_compact[SSAValue(idx′)][:line] = DebugCodeLoc(ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], lineidx)
383384
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
384385
stmt′ = ssa_substitute_op!(insert_node!, inline_compact[SSAValue(idx′)], stmt′, ssa_substitute)
385386
if isa(stmt′, ReturnNode)
@@ -411,7 +412,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
411412
@assert isempty(inline_compact.perm) && isempty(inline_compact.pending_perm) "linetable not in canonical form (missing compact call)"
412413
for ((lineidx, idx′), stmt′) in inline_compact
413414
inline_compact[idx′] = nothing
414-
inline_compact[SSAValue(idx′)][:line] = (ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], Int32(lineidx))
415+
inline_compact[SSAValue(idx′)][:line] = DebugCodeLoc(ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], lineidx)
415416
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
416417
stmt′ = ssa_substitute_op!(insert_node!, inline_compact[SSAValue(idx′)], stmt′, ssa_substitute)
417418
if isa(stmt′, ReturnNode)
@@ -449,7 +450,7 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
449450
end
450451

451452
function fix_va_argexprs!(insert_node!::Inserter, inline_target::Union{IRCode, IncrementalCompact},
452-
argexprs::Vector{Any}, nargs_def::Int, line_idx::NTuple{3,Int32})
453+
argexprs::Vector{Any}, nargs_def::Int, line_idx::DebugCodeLoc)
453454
newargexprs = argexprs[1:(nargs_def-1)]
454455
tuple_call = Expr(:call, TOP_TUPLE)
455456
tuple_typs = Any[]

base/compiler/ssair/ir.jl

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ mutable struct DebugInfoStream
189189
end
190190
#DebugInfoStream(def::Union{MethodInstance,Nothing}, di::DebugInfo, nstmts::Int) =
191191
# if debuginfo_file1(di.def) === debuginfo_file1(di.def)
192-
# new(def, di.linetable, Core.svec(di.edges...), getdebugidx(di, 0),
192+
# new(def, di.linetable, Core.svec(di.edges...), getdebugidx(di, 0)[1],
193193
# ccall(:jl_uncompress_codelocs, Any, (Any, Int), di.codelocs, nstmts)::Vector{Int32})
194194
# else
195195
function DebugInfoStream(def::Union{MethodInstance,Nothing}, di::DebugInfo, nstmts::Int)
@@ -206,15 +206,37 @@ Core.DebugInfo(di::DebugInfoStream, nstmts::Int) =
206206
Core.DebugInfo(something(di.def), di.linetable, Core.svec(di.edges...),
207207
ccall(:jl_compress_codelocs, Any, (Int32, Any, Int), di.firstline, di.codelocs, nstmts)::String)
208208

209-
getdebugidx(debuginfo::Core.DebugInfo, pc::Int) = ccall(:jl_uncompress1_codeloc, NTuple{3,Int32}, (Any, Int), debuginfo.codelocs, pc)
209+
struct DebugCodeLoc
210+
line::Int32
211+
edge::Int32
212+
edge_line::Int32
213+
end
214+
function getindex(dcl::DebugCodeLoc, idx::Int)
215+
if idx == 1
216+
return dcl.line
217+
elseif idx == 2
218+
return dcl.edge
219+
elseif idx == 3
220+
return dcl.edge_line
221+
else
222+
throw(BoundsError(dcl, idx))
223+
end
224+
end
225+
function iterate(dcl::DebugCodeLoc, i::Int=1)
226+
i > 3 && return nothing
227+
return getfield(dcl, i), i+1
228+
end
229+
230+
getdebugidx(debuginfo::Core.DebugInfo, pc::Int) = DebugCodeLoc(
231+
ccall(:jl_uncompress1_codeloc, NTuple{3,Int32}, (Any, Int), debuginfo.codelocs, pc)...)
210232

211233
function getdebugidx(debuginfo::DebugInfoStream, pc::Int)
212234
if 3 <= 3pc <= length(debuginfo.codelocs)
213-
return (debuginfo.codelocs[3pc - 2], debuginfo.codelocs[3pc - 1], debuginfo.codelocs[3pc - 0])
235+
return DebugCodeLoc(debuginfo.codelocs[3pc - 2], debuginfo.codelocs[3pc - 1], debuginfo.codelocs[3pc - 0])
214236
elseif pc == 0
215-
return (Int32(debuginfo.firstline), Int32(0), Int32(0))
237+
return DebugCodeLoc(debuginfo.firstline, 0, 0)
216238
else
217-
return (Int32(-1), Int32(0), Int32(0))
239+
return DebugCodeLoc(-1, 0, 0)
218240
end
219241
end
220242

@@ -310,15 +332,15 @@ Instruction(is::InstructionStream) = Instruction(is, add_new_idx!(is))
310332
isdefined(node, fld) && return getfield(node, fld)
311333
fldarray = getfield(getfield(node, :data), fld)
312334
fldidx = getfield(node, :idx)
313-
(fld === :line) && return (fldarray[3fldidx-2], fldarray[3fldidx-1], fldarray[3fldidx-0])
335+
(fld === :line) && return DebugCodeLoc(fldarray[3fldidx-2], fldarray[3fldidx-1], fldarray[3fldidx-0])
314336
return fldarray[fldidx]
315337
end
316338
@inline function setindex!(node::Instruction, @nospecialize(val), fld::Symbol)
317339
(fld === :inst) && (fld = :stmt) # deprecated
318340
fldarray = getfield(getfield(node, :data), fld)
319341
fldidx = getfield(node, :idx)
320342
if fld === :line
321-
(fldarray[3fldidx-2], fldarray[3fldidx-1], fldarray[3fldidx-0]) = val::NTuple{3,Int32}
343+
(fldarray[3fldidx-2], fldarray[3fldidx-1], fldarray[3fldidx-0]) = val::DebugCodeLoc
322344
else
323345
fldarray[fldidx] = val
324346
end
@@ -370,31 +392,31 @@ struct NewInstruction
370392
stmt::Any
371393
type::Any
372394
info::CallInfo
373-
line::Union{NTuple{3,Int32},Nothing} # if nothing, copy the line from previous statement in the insertion location
395+
line::Union{DebugCodeLoc,Nothing} # if nothing, copy the line from previous statement in the insertion location
374396
flag::Union{UInt32,Nothing} # if nothing, IR flags will be recomputed on insertion
375397
function NewInstruction(@nospecialize(stmt), @nospecialize(type), @nospecialize(info::CallInfo),
376-
line::Union{NTuple{3,Int32},Int32,Nothing}, flag::Union{UInt32,Nothing})
377-
line isa Int32 && (line = (line, zero(Int32), zero(Int32)))
398+
line::Union{DebugCodeLoc,Int32,Nothing}, flag::Union{UInt32,Nothing})
399+
line isa Int32 && (line = DebugCodeLoc(line, 0, 0))
378400
return new(stmt, type, info, line, flag)
379401
end
380402
end
381-
function NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{NTuple{3,Int32},Int32,Nothing}=nothing)
403+
function NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{DebugCodeLoc,Int32,Nothing}=nothing)
382404
return NewInstruction(stmt, type, NoCallInfo(), line, nothing)
383405
end
384406
@nospecialize
385407
function NewInstruction(newinst::NewInstruction;
386408
stmt::Any=newinst.stmt,
387409
type::Any=newinst.type,
388410
info::CallInfo=newinst.info,
389-
line::Union{NTuple{3,Int32},Int32,Nothing}=newinst.line,
411+
line::Union{DebugCodeLoc,Int32,Nothing}=newinst.line,
390412
flag::Union{UInt32,Nothing}=newinst.flag)
391413
return NewInstruction(stmt, type, info, line, flag)
392414
end
393415
function NewInstruction(inst::Instruction;
394416
stmt::Any=inst[:stmt],
395417
type::Any=inst[:type],
396418
info::CallInfo=inst[:info],
397-
line::Union{NTuple{3,Int32},Int32,Nothing}=inst[:line],
419+
line::Union{DebugCodeLoc,Int32,Nothing}=inst[:line],
398420
flag::Union{UInt32,Nothing}=inst[:flag])
399421
return NewInstruction(stmt, type, info, line, flag)
400422
end
@@ -939,7 +961,7 @@ function add_pending!(compact::IncrementalCompact, pos::Int, attach_after::Bool)
939961
end
940962

941963
function inst_from_newinst!(node::Instruction, newinst::NewInstruction,
942-
newline::NTuple{3,Int32}=newinst.line::NTuple{3,Int32}, newflag::UInt32=newinst.flag::UInt32)
964+
newline::DebugCodeLoc=newinst.line::DebugCodeLoc, newflag::UInt32=newinst.flag::UInt32)
943965
node[:stmt] = newinst.stmt
944966
node[:type] = newinst.type
945967
node[:info] = newinst.info
@@ -1029,7 +1051,7 @@ function maybe_reopen_bb!(compact)
10291051
end
10301052

10311053
function insert_node_here!(compact::IncrementalCompact, newinst::NewInstruction, reverse_affinity::Bool=false)
1032-
newline = newinst.line::NTuple{3,Int32}
1054+
newline = newinst.line::DebugCodeLoc
10331055
refinish = false
10341056
result_idx = compact.result_idx
10351057
result_bbs = compact.cfg_transform.result_bbs
@@ -1671,7 +1693,7 @@ function resize!(compact::IncrementalCompact, nnewnodes::Int)
16711693
return compact
16721694
end
16731695

1674-
const NoLineUpdate = (Int32(0), Int32(0), Int32(0))
1696+
const NoLineUpdate = DebugCodeLoc(0, 0, 0)
16751697

16761698
function finish_current_bb!(compact::IncrementalCompact, active_bb::Int,
16771699
old_result_idx::Int=compact.result_idx, unreachable::Bool=false)

base/compiler/ssair/passes.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,14 +1523,15 @@ function try_inline_finalizer!(ir::IRCode, argexprs::Vector{Any}, idx::Int,
15231523
ssa_rename = Vector{Any}(undef, length(src.stmts))
15241524
for idx′ = 1:length(src.stmts)
15251525
inst = src[SSAValue(idx′)]
1526-
stmt = inst[:stmt]
1527-
isa(stmt, ReturnNode) && continue
1528-
stmt = ssamap(stmt) do ssa::SSAValue
1526+
stmt = inst[:stmt]
1527+
isa(stmt, ReturnNode) && continue
1528+
stmt = ssamap(stmt) do ssa::SSAValue
15291529
ssa_rename[ssa.id]
15301530
end
1531-
stmt′ = ssa_substitute_op!(InsertBefore(ir, SSAValue(idx)), inst, stmt′, ssa_substitute)
1531+
stmt = ssa_substitute_op!(InsertBefore(ir, SSAValue(idx)), inst, stmt, ssa_substitute)
1532+
line = DebugCodeLoc(ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], idx′)
15321533
ssa_rename[idx′] = insert_node!(ir, idx,
1533-
NewInstruction(inst; stmt=stmt′, line=(ssa_substitute.inlined_at[1], ssa_substitute.inlined_at[2], Int32(idx′))),
1534+
NewInstruction(inst; stmt, line),
15341535
attach_after)
15351536
end
15361537

base/show.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,8 @@ module IRShow
28382838
Base.iterate(is::Compiler.InstructionStream, st::Int=1) = (st <= Compiler.length(is)) ? (is[st], st + 1) : nothing
28392839
Base.getindex(is::Compiler.InstructionStream, idx::Int) = Compiler.getindex(is, idx)
28402840
Base.getindex(node::Compiler.Instruction, fld::Symbol) = Compiler.getindex(node, fld)
2841+
Base.getindex(dcl::Compiler.DebugCodeLoc, idx::Int) = Compiler.getindex(dcl, idx)
2842+
Base.iterate(dcl::Compiler.DebugCodeLoc, s::Int=1) = Compiler.iterate(dcl, s)
28412843
Base.getindex(ir::IRCode, ssa::SSAValue) = Compiler.getindex(ir, ssa)
28422844
include("compiler/ssair/show.jl")
28432845

doc/src/devdocs/ast.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,3 +844,8 @@ end
844844
depth also might have changed, though most callers should ignore that.
845845
- (zero, non-zero, *) : no line number, just edges (usually because of macro-expansion into
846846
top-level code)
847+
848+
`Core.Compiler` has a bunch of utility types and functions for handling this data.
849+
The `Core.Compiler.DebugCodeLoc` object represents the 3 values. And there's a handy
850+
utility function, `Core.Compiler.getdebugidx(debuginfo::Union{Core.DebugInfo,Core.Compiler.DebugInfoStream}, idx::Int)`,
851+
that gives you the `DebugCodeLoc` matching a specific statement index (`idx`).

test/show.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,3 +2694,13 @@ let lowered = Meta.lower(Main, Expr(:let, Expr(:block), Expr(:block, Expr(:tople
26942694
# Check that this gets printed as `_1 = 1` not `y = 1`
26952695
@test contains(sprint(show, ci), "_1 = 1")
26962696
end
2697+
2698+
# exercise DILineInfoPrinter
2699+
test_DILineInfoPrinter(x) = @inline sin(x)
2700+
test_DILineInfoPrinter(42) # ensure we have code instance cache for `test_DILineInfoPrinter(::Int)`
2701+
let io = IOContext(IOBuffer(), :color=>true)
2702+
mi = only(methods(test_DILineInfoPrinter, (Int,))).specializations
2703+
src = Core.Compiler._uncompressed_ir(mi.cache, mi.cache.inferred::String)
2704+
Base.IRShow.show_ir(io, src, Base.IRShow.IRShowConfig(Base.IRShow.DILineInfoPrinter(src.debuginfo, :var"unknown scope")));
2705+
Base.IRShow.show_ir(io, src, Base.IRShow.IRShowConfig(Base.IRShow.DILineInfoPrinter(src.debuginfo, :var"unknown scope", true)));
2706+
end

0 commit comments

Comments
 (0)