Skip to content

Commit

Permalink
More diverse error types (#1710)
Browse files Browse the repository at this point in the history
* More diverse types

* fix
  • Loading branch information
wsmoses authored Aug 7, 2024
1 parent d19307d commit 356ef34
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
55 changes: 46 additions & 9 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,35 @@ function Base.showerror(io::IO, ece::EnzymeRuntimeActivityError)
print(io, msg, '\n')
end

struct EnzymeNoTypeError <: Base.Exception
msg::Cstring
end

function Base.showerror(io::IO, ece::EnzymeNoTypeError)
print(io, "Enzyme cannot deduce type\n")
msg = Base.unsafe_string(ece.msg)
print(io, msg, '\n')
end

struct EnzymeNoShadowError <: Base.Exception
msg::Cstring
end

function Base.showerror(io::IO, ece::EnzymeNoShadowError)
print(io, "Enzyme could not find shadow for value\n")
msg = Base.unsafe_string(ece.msg)
print(io, msg, '\n')
end

struct EnzymeNoDerivativeError <: Base.Exception
msg::Cstring
end

function Base.showerror(io::IO, ece::EnzymeNoDerivativeError)
msg = Base.unsafe_string(ece.msg)
print(io, msg, '\n')
end

@static if VERSION >= v"1.8.0"
const JuliaEnzymeNameMap = Dict{String, Any}(
"enz_val_true" => Val(true),
Expand All @@ -1033,6 +1062,9 @@ const JuliaEnzymeNameMap = Dict{String, Any}(
"enz_runtime_exc" => EnzymeRuntimeException,
"enz_mut_exc" => EnzymeMutabilityException,
"enz_runtime_activity_exc" => EnzymeRuntimeActivityError,
"enz_no_type_exc" => EnzymeNoTypeError,
"enz_no_shadow_exc" => EnzymeNoShadowError,
"enz_no_derivative_exc" => EnzymeNoDerivativeError,
)
else
const JuliaEnzymeNameMap = Dict{String, Any}()
Expand Down Expand Up @@ -2139,21 +2171,27 @@ function julia_error(cstr::Cstring, val::LLVM.API.LLVMValueRef, errtype::API.Err
if occursin("No create nofree of empty function", msg) || occursin("No forward mode derivative found for", msg) || occursin("No augmented forward pass", msg) || occursin("No reverse pass found", msg)
ir = nothing
end
exc = NoDerivativeException(msg, ir, bt)
if B != C_NULL
B = IRBuilder(B)
msg2 = sprint() do io
Base.showerror(io, exc)
msg2 = sprint() do io
if ir !== nothing
print(io, "Current scope: \n")
print(io, ir)
end
print(io, '\n', msg, '\n')
if bt !== nothing
Base.show_backtrace(io, bt)
println(io)
end
end
emit_error(B, nothing, msg2)
emit_error(B, nothing, msg2, EnzymeNoDerivativeError)
return C_NULL
end
throw(exc)
throw(NoDerivativeException(msg, ir, bt))
elseif errtype == API.ET_NoShadow
gutils = GradientUtils(API.EnzymeGradientUtilsRef(data))

msgN = sprint() do io::IO
print(io, "Enzyme could not find shadow for value\n")
if isa(val, LLVM.Argument)
fn = parent_scope(val)
ir = string(LLVM.name(fn))*string(function_type(fn))
Expand All @@ -2174,7 +2212,7 @@ function julia_error(cstr::Cstring, val::LLVM.API.LLVMValueRef, errtype::API.Err
println(io)
end
end
emit_error(IRBuilder(B), nothing, msgN)
emit_error(IRBuilder(B), nothing, msgN, EnzymeNoShadowError)
return LLVM.null(get_shadow_type(gutils, value_type(val))).ref
elseif errtype == API.ET_IllegalTypeAnalysis
data = API.EnzymeTypeAnalyzerRef(data)
Expand All @@ -2199,7 +2237,6 @@ function julia_error(cstr::Cstring, val::LLVM.API.LLVMValueRef, errtype::API.Err
API.EnzymeStringFree(ip)

msg2 = sprint() do io::IO
print(io, "Enzyme cannot deduce type\n")
if !occursin("Cannot deduce single type of store", msg)
if ir !== nothing
print(io, "Current scope: \n")
Expand All @@ -2220,7 +2257,7 @@ function julia_error(cstr::Cstring, val::LLVM.API.LLVMValueRef, errtype::API.Err
println(io, "within ", mi)
end
end
emit_error(B, nothing, msg2)
emit_error(B, nothing, msg2, EnzymeNoTypeError)
return C_NULL
elseif errtype == API.ET_IllegalFirstPointer
throw(IllegalFirstPointerException(msg, ir, bt))
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,7 @@ end
@testset "Exception" begin

f_no_derv(x) = ccall("extern doesnotexist", llvmcall, Float64, (Float64,), x)
@test_throws Enzyme.Compiler.EnzymeRuntimeException autodiff(Reverse, f_no_derv, Active, Active(0.5))
@test_throws Enzyme.Compiler.EnzymeNoDerivativeError autodiff(Reverse, f_no_derv, Active, Active(0.5))

f_union(cond, x) = cond ? x : 0
g_union(cond, x) = f_union(cond,x)*x
Expand Down

2 comments on commit 356ef34

@wsmoses
Copy link
Member Author

@wsmoses wsmoses commented on 356ef34 Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir="lib/EnzymeCore"

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/112635

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a EnzymeCore-v0.7.8 -m "<description of version>" 356ef3493085cdfa5084c4c6d23859e8f183ed3e
git push origin EnzymeCore-v0.7.8

Please sign in to comment.