Skip to content
Open
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
103 changes: 87 additions & 16 deletions src/debug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
#
# 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.
# if there are several, the backtrace ends with a "multiple call sites" frame (`caller_chains`
# lists those calls). finally, the debug information is converted to a Julia stack trace.
function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
trace!(bt, inst)
return bt
end

# the walk of `backtrace`, returning the function with several call sites it ended at, if any
function trace!(bt, inst::LLVM.Instruction)
done = Set{LLVM.Instruction}()
while true
if in(inst, done)
Expand All @@ -14,21 +21,7 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
push!(done, inst)

# 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
loc = loc.inlined_at
end
push_frames!(bt, inst.debug_location)

# move up the call chain
f = inst.parent.parent
Expand All @@ -48,6 +41,7 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
if length(call_sites) > 1
frame = StackTraces.StackFrame("multiple call sites", "unknown", 0)
push!(bt, frame)
return f
elseif length(call_sites) == 1
inst = first(call_sites).user::LLVM.CallInst
continue
Expand All @@ -56,5 +50,82 @@ function backtrace(inst::LLVM.Instruction, bt = StackTraces.StackFrame[])
break
end

return nothing
end

# the frames of a debug location, innermost (inlined) first
function push_frames!(bt, loc)
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
loc = loc.inlined_at
end
return bt
end

# The paths of direct calls into `f`: for each, the frames of the call, followed by those of a
# call into the function containing it, and so on, innermost first. A path ends at a function
# without calls that have debug information. Calls back into a function already on the path
# are left out, so recursion is not followed. Paths that show the same frames are listed once,
# and at most `limit` of them, in source order.
function caller_chains(f::LLVM.Function, limit::Int)
# the paths into a function are the same however it was reached, unless a call into
# another function on the path was left out: those are not reused. that makes walking
# mutually recursive functions exponential, but they are rare in device code.
memo = Dict{LLVM.Function,Tuple{Vector{StackTraces.StackTrace},Bool}}()
active = Set{LLVM.Function}()
# returns the paths, whether a recursive call was left out, and whether `f` is called at all
function chains(f)
if haskey(memo, f)
result, called = memo[f]
return result, false, called
end
push!(active, f)
result = StackTraces.StackTrace[]
cut = called = false
for use in f.uses
call = use.user
call isa LLVM.CallInst && call.called_operand == f || continue
loc = call.debug_location
loc === nothing && continue
called = true
caller = call.parent.parent
if caller in active
# (a function calling itself is left out wherever it is reached from)
cut |= caller != f
continue
end
frames = push_frames!(StackTraces.StackFrame[], loc)
outer, outer_cut, outer_called = chains(caller)
cut |= outer_cut
if !outer_called
push!(result, frames)
else
# (nothing when every path into the caller is recursive)
append!(result, vcat(frames, chain) for chain in outer)
end
end
delete!(active, f)
normalize_chains!(result, limit)
cut || (memo[f] = (result, called))
return result, cut, called
end
return first(chains(f))
end

function normalize_chains!(chains::Vector{StackTraces.StackTrace}, limit::Int)
key(chain) = [(String(frame.file), frame.line, String(frame.func)) for frame in chain]
unique!(key, chains)
sort!(chains; by=key)
length(chains) > limit && resize!(chains, limit)
return chains
end
7 changes: 5 additions & 2 deletions src/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ end
# atomics, which only fail when loading the code object, some atomics abort instruction
# selection, and the errors it does report don't point at the Julia code.
# Validation runs after `lower_syncscopes!`, so the scopes are the ones AMDGPU knows.
function validate_ir(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module)
function validate_ir(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module,
callers=IRCallers())
errors = IRError[]
dl = mod.datalayout
for f in mod.functions, bb in f.blocks, inst in bb.instructions
Expand All @@ -330,7 +331,9 @@ function validate_ir(job::CompilerJob{GCNCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing && continue
push!(errors, (reason, backtrace(inst), string(inst)))
record_callers!(callers, errors[end], inst)
end
return errors
end
Expand Down
4 changes: 3 additions & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -802,5 +802,7 @@ finish_runtime_intrinsics!(@nospecialize(job::CompilerJob), mod::LLVM.Module) =
finish_ir!(@nospecialize(job::CompilerJob), mod::LLVM.Module, entry::LLVM.Function) =
entry

# whether an LLVM function is valid for this back-end
# the errors that make a module invalid for this back-end. back-ends can also implement the
# three-argument form, to record the calls that reach an error (see `record_callers!`)
validate_ir(@nospecialize(job::CompilerJob), mod::LLVM.Module) = IRError[]
validate_ir(@nospecialize(job::CompilerJob), mod::LLVM.Module, callers) = validate_ir(job, mod)
12 changes: 8 additions & 4 deletions src/metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ function optimize_module!(@nospecialize(job::CompilerJob{MetalCompilerTarget}),
return
end

function validate_ir(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module)
function validate_ir(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module,
callers=IRCallers())
errors = IRError[]

# Metal does not support double precision, except for logging
Expand All @@ -414,10 +415,11 @@ function validate_ir(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module)

return true
end
append!(errors, check_ir_values(mod, is_illegal_double, "use of double value"))
append!(errors,
check_ir_values(mod, is_illegal_double, "use of double value"; callers))

# Metal never supports 128-bit integers
append!(errors, check_ir_values(mod, LLVM.IntType(128)))
append!(errors, check_ir_values(mod, LLVM.IntType(128); callers))

# atomics that `lower_atomics!` cannot lower
for f in mod.functions, bb in f.blocks, inst in bb.instructions
Expand All @@ -433,7 +435,9 @@ function validate_ir(job::CompilerJob{MetalCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing && continue
push!(errors, (reason, backtrace(inst), string(inst)))
record_callers!(callers, errors[end], inst)
end

errors
Expand Down
7 changes: 5 additions & 2 deletions src/ptx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,8 @@ end
# target cannot run: it silently drops a system scope before sm_60, emits PTX that `ptxas`
# or the driver reject, or aborts the process. Validation runs after `lower_syncscopes!`, so
# the scopes are the ones NVPTX knows.
function validate_ir(job::CompilerJob{PTXCompilerTarget}, mod::LLVM.Module)
function validate_ir(job::CompilerJob{PTXCompilerTarget}, mod::LLVM.Module,
callers=IRCallers())
errors = IRError[]
dl = mod.datalayout
for f in mod.functions, bb in f.blocks, inst in bb.instructions
Expand All @@ -567,7 +568,9 @@ function validate_ir(job::CompilerJob{PTXCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing && continue
push!(errors, (reason, backtrace(inst), string(inst)))
record_callers!(callers, errors[end], inst)
end
return errors
end
Expand Down
13 changes: 8 additions & 5 deletions src/spirv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,20 @@ function finish_module!(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module,
return entry
end

function validate_ir(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module)
function validate_ir(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module,
callers=IRCallers())
errors = IRError[]

# support for half and double depends on the target
if !job.config.target.supports_fp16
append!(errors, check_ir_values(mod, LLVM.HalfType()))
append!(errors, check_ir_values(mod, LLVM.HalfType(); callers))
end
if !job.config.target.supports_fp64
append!(errors, check_ir_values(mod, LLVM.DoubleType()))
append!(errors, check_ir_values(mod, LLVM.DoubleType(); callers))
end
bfloat16 = job.config.target.supports_bfloat16
if !bfloat16
append!(errors, check_ir_values(mod, LLVM.BFloatType()))
append!(errors, check_ir_values(mod, LLVM.BFloatType(); callers))
end

# atomics that `lower_atomics!` cannot lower, and `bfloat` operations that
Expand All @@ -211,7 +212,9 @@ function validate_ir(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module)
else
nothing
end
reason === nothing || push!(errors, (reason, backtrace(inst), string(inst)))
reason === nothing && continue
push!(errors, (reason, backtrace(inst), string(inst)))
record_callers!(callers, errors[end], inst)
end

return errors
Expand Down
Loading
Loading