Skip to content

Commit 47bf47b

Browse files
vchuravyclaude
andcommitted
Add unwrap_error and recommend it for introspecting compiler errors
The error hint tells users to catch the exception as `err` and call `code_typed(err; interactive=true)`, but in the REPL the `err` global is a `Base.ExceptionStack`, and errors from `include`d files are wrapped in `LoadError`, so that advice often fails. Provide `GPUCompiler.unwrap_error` to strip these wrappers and update the hint to use it, instead of committing type piracy on the `InteractiveUtils` reflection functions. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 78eaff3 commit 47bf47b

3 files changed

Lines changed: 36 additions & 1 deletion

File tree

src/error.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ end
2525
Base.show(io::IO, err::KernelError) = showerror(io, err)
2626

2727

28+
"""
29+
unwrap_error(err)
30+
31+
Unwrap a compiler exception from container exceptions such as the REPL's `err` global
32+
(a `Base.ExceptionStack`) or the `LoadError` thrown by `include`, so that it can be
33+
passed to reflection functions like `code_typed`. Other exceptions are returned as-is.
34+
"""
35+
unwrap_error(err) = err
36+
unwrap_error(err::Base.ExceptionStack) =
37+
isempty(err.stack) ? err : unwrap_error(last(err.stack).exception)
38+
unwrap_error(err::LoadError) = unwrap_error(err.error)
39+
40+
2841
struct InternalCompilerError <: Exception
2942
job::CompilerJob
3043
message::String

src/validation.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ function Base.showerror(io::IO, err::InvalidIRError)
153153
printstyled(io, "Hint"; bold = true, color = :cyan)
154154
printstyled(
155155
io,
156-
": catch this exception as `err` and call `code_typed(err; interactive = true)` to",
156+
": catch this exception as `err` and call",
157+
" `code_typed(GPUCompiler.unwrap_error(err); interactive = true)` to",
157158
" introspect the erroneous code with Cthulhu.jl";
158159
color = :cyan,
159160
)

test/native.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using InteractiveUtils
2+
13
@testset "reflection" begin
24
job, _ = Native.create_job(identity, (Int,))
35

@@ -622,6 +624,25 @@ end
622624
end
623625
end
624626

627+
@testset "unwrap_error" begin
628+
mod = @eval module $(gensym())
629+
foobar(i) = println(i)
630+
end
631+
632+
stack = try
633+
Native.code_execution(mod.foobar, Tuple{Int})
634+
nothing
635+
catch
636+
current_exceptions()
637+
end
638+
@test stack isa Base.ExceptionStack
639+
err = GPUCompiler.unwrap_error(stack)
640+
@test err isa GPUCompiler.InvalidIRError
641+
@test GPUCompiler.unwrap_error(err) === err
642+
@test GPUCompiler.unwrap_error(LoadError("foo.jl", 1, err)) === err
643+
@test only(code_typed(GPUCompiler.unwrap_error(stack))) isa Pair
644+
end
645+
625646
@testset "invalid LLVM IR (ccall)" begin
626647
mod = @eval module $(gensym())
627648
function foobar(p)

0 commit comments

Comments
 (0)