Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 37 additions & 13 deletions src/debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#
# this works by looking up the debug information of the instruction, and inspecting the call
# sites of the containing function. if there's only one, repeat the process from that call.
# finally, the debug information is converted to a Julia stack trace.
function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
# if there are several, it ends with a "multiple call sites" frame and, with
# `call_site_frames`, the outermost frame of each call. those are off by default, as code
# that turns every frame into instructions would repeat them in each inlined copy. finally,
# the debug information is converted to a Julia stack trace.
function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[];
call_site_frames::Bool=false)
done = Set{LLVM.Instruction}()
while true
if in(inst, done)
Expand All @@ -16,17 +20,8 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
# look up the debug information from the current instruction
loc = inst.debug_location
while loc !== nothing
scope = loc.scope
if scope !== nothing
name = replace(something(scope.name, ""), r";$"=>"")
isempty(name) && (name = "unknown")
file = scope.file
path = file === nothing ? "unknown" :
joinpath(file.directory::Union{Nothing,String},
file.filename::Union{Nothing,String})
line = loc.line
push!(bt, StackTraces.StackFrame(name, path, line))
end
frame = stack_frame(loc)
frame === nothing || push!(bt, frame)
loc = loc.inlined_at
end

Expand All @@ -48,6 +43,23 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
if length(call_sites) > 1
frame = StackTraces.StackFrame("multiple call sites", "unknown", 0)
push!(bt, frame)

if call_site_frames
frames = StackTraces.StackFrame[]
for call in call_sites
loc = call.user.debug_location
while loc.inlined_at !== nothing
loc = loc.inlined_at
end
frame = stack_frame(loc)
frame === nothing || push!(frames, frame)
end
# calls from inlined code can share the frame of the function they were
# inlined into
unique!(frames)
sort!(frames; by=frame -> (frame.file, frame.line, frame.func))
append!(bt, frames)
end
elseif length(call_sites) == 1
inst = first(call_sites).user::LLVM.CallInst
continue
Expand All @@ -58,3 +70,15 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])

return bt
end

function stack_frame(loc)
scope = loc.scope
scope === nothing && return nothing
name = replace(something(scope.name, ""), r";$"=>"")
isempty(name) && (name = "unknown")
file = scope.file
path = file === nothing ? "unknown" :
joinpath(file.directory::Union{Nothing,String},
file.filename::Union{Nothing,String})
return StackTraces.StackFrame(name, path, loc.line)
end
8 changes: 6 additions & 2 deletions src/exception_sites.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ end
ExceptionSite(name, reason, frames)

A throw site in device code, as reported to the host: the exception's `name`, the `reason`
(possibly empty), and at debug level 2 the `frames` of the backtrace, innermost first.
(possibly empty), and at debug level 2 the `frames` of the backtrace, innermost first. Where
the backtrace reaches a function that is called from several places, it continues with a
frame named "multiple call sites" and then the outermost frame of each of those calls,
without duplicates.

Compiling a job that [`uses_exception_sites`](@ref) returns these as
`meta.exception_sites`, indexed by the id that device code passes to the `exception_site`
Expand Down Expand Up @@ -245,8 +248,9 @@ function finalize_exception_sites!(@nospecialize(job::CompilerJob), mod::LLVM.Mo

# identical sites that LLVM merged anyway share this call and its location
frames = if job.config.debug_level >= 2
bt = backtrace(call; call_site_frames=true)
ExceptionFrame[ExceptionFrame(String(frame.func), String(frame.file),
frame.line) for frame in backtrace(call)]
frame.line) for frame in bt]
else
ExceptionFrame[]
end
Expand Down
3 changes: 2 additions & 1 deletion src/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ function validate_ir(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing ||
push!(errors, (reason, backtrace(inst; call_site_frames=true), string(inst)))
end
return errors
end
Expand Down
3 changes: 2 additions & 1 deletion src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ function lower_throw!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
for use in collect(f.uses)
call = use.user::LLVM.CallInst
if is_unknown_intrinsic_error(call)
push!(errors, (UNKNOWN_INTRINSIC, backtrace(call), nothing))
bt = backtrace(call; call_site_frames=true)
push!(errors, (UNKNOWN_INTRINSIC, bt, nothing))
end

# replace the throw with a PTX-compatible exception
Expand Down
6 changes: 4 additions & 2 deletions src/metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ function validate_ir(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing ||
push!(errors, (reason, backtrace(inst; call_site_frames=true), string(inst)))
end

errors
Expand Down Expand Up @@ -1501,7 +1502,8 @@ function lower_atomics!(@nospecialize(job::CompilerJob{MetalCompilerTarget}),
if action === :select
select_atomic!(job, mod, inst)
elseif action isa String
push!(unsupported, (action, backtrace(inst), string(inst)))
bt = backtrace(inst; call_site_frames=true)
push!(unsupported, (action, bt, string(inst)))
else
error("Atomic operation was not legalized ($action): $inst")
end
Expand Down
3 changes: 2 additions & 1 deletion src/ptx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ function validate_ir(job::CompilerJob{PTXCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing ||
push!(errors, (reason, backtrace(inst; call_site_frames=true), string(inst)))
end
return errors
end
Expand Down
9 changes: 6 additions & 3 deletions src/spirv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ function validate_ir(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing ||
push!(errors, (reason, backtrace(inst; call_site_frames=true), string(inst)))
end

return errors
Expand Down Expand Up @@ -906,7 +907,8 @@ function lower_atomics!(@nospecialize(job::CompilerJob{SPIRVCompilerTarget}),
select_atomic!(mod, inst)
changed = true
elseif action isa String
push!(unsupported, (action, backtrace(inst), string(inst)))
bt = backtrace(inst; call_site_frames=true)
push!(unsupported, (action, bt, string(inst)))
else
error("Atomic operation was not legalized ($action): $inst")
end
Expand Down Expand Up @@ -1182,7 +1184,8 @@ function widen_bfloat16!(@nospecialize(job::CompilerJob{SPIRVCompilerTarget}),
if action isa Symbol
push!(worklist, (inst, action))
elseif action isa String
push!(unsupported, (action, backtrace(inst), string(inst)))
bt = backtrace(inst; call_site_frames=true)
push!(unsupported, (action, bt, string(inst)))
end
end
# `validate_ir` rejects these too, but optimization after it can introduce them, and
Expand Down
16 changes: 9 additions & 7 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function is_runtime_call((kind, bt, meta)::IRError)
end

# The frames of the call in the outermost function that an error originates from, or `nothing`
# if the backtrace does not reach that function.
# if the backtrace passes through a function with several call sites.
function error_origin(bt::StackTraces.StackTrace)
isempty(bt) && return nothing
any(frame -> frame.func === Symbol("multiple call sites"), bt) && return nothing
Expand Down Expand Up @@ -317,12 +317,13 @@ function check_ir!(job, errors::Vector{IRError}, f::LLVM.Function, relocs::Reloc
end
if (isa(inst, LLVM.LoadInst) || isa(inst, LLVM.StoreInst)) && is_binding_access(inst)
binding = accessed_binding(inst, relocs, dl)
bt = backtrace(inst; call_site_frames=true)
if binding === nothing
@safe_debug "Decoding the binding of a global access failed" inst bb=inst.parent
push!(errors, (NONCONST_GLOBAL, backtrace(inst), nothing))
push!(errors, (NONCONST_GLOBAL, bt, nothing))
else
gr = binding.globalref
push!(errors, (global_access_error(job, gr), backtrace(inst), gr))
push!(errors, (global_access_error(job, gr), bt, gr))
end
end
if explain !== nothing
Expand Down Expand Up @@ -356,7 +357,8 @@ function check_constants!(@nospecialize(job::CompilerJob), errors::Vector{IRErro
gv === nothing && continue
inst = constant_use(job, gv)
inst === nothing && continue
push!(errors, (UNSUPPORTED_CONSTANT, backtrace(inst), typeof(value)))
bt = backtrace(inst; call_site_frames=true)
push!(errors, (UNSUPPORTED_CONSTANT, bt, typeof(value)))
end
return errors
end
Expand Down Expand Up @@ -396,7 +398,7 @@ end
const libjulia = Ref{Ptr{Cvoid}}(C_NULL)

function check_ir!(job, errors::Vector{IRError}, inst::LLVM.LoadInst)
bt = backtrace(inst)
bt = backtrace(inst; call_site_frames=true)
src = inst.operands[1]
if src isa ConstantExpr
if src.opcode == LLVM.Opcode.BitCast
Expand Down Expand Up @@ -505,7 +507,7 @@ function is_unknown_intrinsic_error(call::LLVM.CallInst)
end

function check_ir!(job, errors::Vector{IRError}, inst::LLVM.CallInst, relocs::Relocations)
bt = backtrace(inst)
bt = backtrace(inst; call_site_frames=true)
dest = inst.called_operand
if isa(dest, LLVM.Function)
fn = dest.name
Expand Down Expand Up @@ -813,7 +815,7 @@ function check_ir_values(mod::LLVM.Module, predicate, msg="value")
errors = IRError[]
for fun in mod.functions, bb in fun.blocks, inst in bb.instructions
if predicate(inst) || any(predicate, inst.operands)
bt = backtrace(inst)
bt = backtrace(inst; call_site_frames=true)
# snapshot to a string: the error may outlive the module, and showing a
# disposed LLVM value segfaults
push!(errors, (msg, bt, string(inst)))
Expand Down
25 changes: 25 additions & 0 deletions test/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,31 @@ end
@test any(GPUCompiler.is_runtime_call, err.errors)
end

@testset "backtraces through several call sites" begin
mod = @eval module $(gensym())
@noinline helper(x) = Base.inferencebarrier(sin)(x)::Float64
function kernel(p, x)
unsafe_store!(p, helper(x))
unsafe_store!(p, helper(x + 1))
return
end
end
line = first(methods(mod.kernel)).line

err = try
Native.code_execution(mod.kernel, Tuple{Ptr{Float64},Float64})
nothing
catch err
err
end
@test err isa InvalidIRError
_, bt, _ = only(filter(error -> error[1] == GPUCompiler.DYNAMIC_CALL, err.errors))
i = findfirst(frame -> frame.func === Symbol("multiple call sites"), bt)
@test i !== nothing
@test [(frame.func, frame.line) for frame in bt[i+1:end]] ==
[(:kernel, line + 1), (:kernel, line + 2)]
end

@testset "explanations in Julia code" begin
mod = @eval module $(gensym())
f(x) = x + 1
Expand Down
Loading