diff --git a/src/debug.jl b/src/debug.jl index 3150f174..dd70da7f 100644 --- a/src/debug.jl +++ b/src/debug.jl @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/src/exception_sites.jl b/src/exception_sites.jl index 74991871..c2563902 100644 --- a/src/exception_sites.jl +++ b/src/exception_sites.jl @@ -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` @@ -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 diff --git a/src/gcn.jl b/src/gcn.jl index 0ce8c34e..2aa67e35 100644 --- a/src/gcn.jl +++ b/src/gcn.jl @@ -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 diff --git a/src/irgen.jl b/src/irgen.jl index 6fafe3cc..8b53be4c 100644 --- a/src/irgen.jl +++ b/src/irgen.jl @@ -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 diff --git a/src/metal.jl b/src/metal.jl index 2363f986..39702445 100644 --- a/src/metal.jl +++ b/src/metal.jl @@ -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 @@ -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 diff --git a/src/ptx.jl b/src/ptx.jl index f13350db..2c6db1cd 100644 --- a/src/ptx.jl +++ b/src/ptx.jl @@ -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 diff --git a/src/spirv.jl b/src/spirv.jl index 8c5b591c..a9bc1058 100644 --- a/src/spirv.jl +++ b/src/spirv.jl @@ -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 @@ -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 @@ -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 diff --git a/src/validation.jl b/src/validation.jl index fa44e630..ef7a64de 100644 --- a/src/validation.jl +++ b/src/validation.jl @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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))) diff --git a/test/native.jl b/test/native.jl index 34c200b0..878e734a 100644 --- a/test/native.jl +++ b/test/native.jl @@ -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