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
13 changes: 13 additions & 0 deletions src/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ end
Base.show(io::IO, err::KernelError) = showerror(io, err)


"""
unwrap_error(err)

Unwrap a compiler exception from container exceptions such as the REPL's `err` global
(a `Base.ExceptionStack`) or the `LoadError` thrown by `include`, so that it can be
passed to reflection functions like `code_typed`. Other exceptions are returned as-is.
"""
unwrap_error(err) = err
unwrap_error(err::Base.ExceptionStack) =
isempty(err.stack) ? err : unwrap_error(last(err.stack).exception)
unwrap_error(err::LoadError) = unwrap_error(err.error)


struct InternalCompilerError <: Exception
job::CompilerJob
message::String
Expand Down
3 changes: 2 additions & 1 deletion src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ function Base.showerror(io::IO, err::InvalidIRError)
printstyled(io, "Hint"; bold = true, color = :cyan)
printstyled(
io,
": catch this exception as `err` and call `code_typed(err; interactive = true)` to",
": catch this exception as `err` and call",
" `code_typed(GPUCompiler.unwrap_error(err); interactive = true)` to",
" introspect the erroneous code with Cthulhu.jl";
color = :cyan,
)
Expand Down
21 changes: 21 additions & 0 deletions test/native.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using InteractiveUtils

@testset "reflection" begin
job, _ = Native.create_job(identity, (Int,))

Expand Down Expand Up @@ -622,6 +624,25 @@ end
end
end

@testset "unwrap_error" begin
mod = @eval module $(gensym())
foobar(i) = println(i)
end

stack = try
Native.code_execution(mod.foobar, Tuple{Int})
nothing
catch
current_exceptions()
end
@test stack isa Base.ExceptionStack
err = GPUCompiler.unwrap_error(stack)
@test err isa GPUCompiler.InvalidIRError
@test GPUCompiler.unwrap_error(err) === err
@test GPUCompiler.unwrap_error(LoadError("foo.jl", 1, err)) === err
@test only(code_typed(GPUCompiler.unwrap_error(stack))) isa Pair
end

@testset "invalid LLVM IR (ccall)" begin
mod = @eval module $(gensym())
function foobar(p)
Expand Down
Loading