Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errunion #1424

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ version = "0.12.2"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
ComponentArrays = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66"
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Copy link
Member

Choose a reason for hiding this comment

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

Reminder to clean the Project.toml before merging

EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
Enzyme_jll = "7cc45869-7501-5eee-bdea-0790c847d4ef"
GPUCompiler = "61eb1bfa-7361-4325-ad38-22787b887f55"
Expand Down
33 changes: 30 additions & 3 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5063,7 +5063,7 @@ function GPUCompiler.codegen(output::Symbol, job::CompilerJob{<:EnzymeTarget};

if shouldemit
b = IRBuilder()
position!(b, term)
position!(b, term)is guaranteed to return an error a
emit_error(b, term, "Enzyme: The original primal code hits this error condition, thus differentiating it does not make sense")
end
end
Expand Down Expand Up @@ -5772,9 +5772,36 @@ end
rrt = something(Core.Compiler.typeinf_type(interp, mi.def, mi.specTypes, mi.sparam_vals), Any)

if rrt == Union{}
estr = "Function to differentiate `$mi` is guaranteed to return an error and doesn't make sense to autodiff. Giving up"
estr = "Function to differentiate `$mi` is guaranteed to return an error and doesn't make sense to autodiff. Giving up and just running primal"
primargs = Expr[]

RT = Tuple{map(eltype, (A, TT.parameters...))}
llvm_f, _ = LLVM.Interop.create_function(RT, ())
push!(function_attributes(llvm_f), EnumAttribute("alwaysinline", 0))

mod = LLVM.parent(llvm_f)

LLVM.IRBuilder() do builder
entry = BasicBlock(llvm_f, "entry")

lvals = LLVM.Value[]
for AT in (FA, TT.parameters...)
T = eltype(AT)

obj = emit_allocobj!(builder, T)
push!(lvals, obj)
end
ret!(lvals)
end

ir = string(mod)
fn = LLVM.name(llvm_f)

return quote
error($estr)
@warn $estr
vals = llvmcall(($ir, $fn), $RT, ())
vals[1](vals[2:end]...)
error($estr)
end
end

Expand Down
8 changes: 8 additions & 0 deletions src/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ function nodecayed_phis!(mod::LLVM.Module)
return v2, offset, skipload
end

if isa(v, LLVM.ConstantExpr) && opcode(v) == LLVM.API.GetElementPtr && !hasload
v2, offset, skipload = getparent(operands(v)[1], offset, hasload)
offset = nuwadd!(b, offset, API.EnzymeComputeByteOffsetOfGEP(b, v, offty))
v2 = bitcast!(b, v2, LLVM.PointerType(eltype(value_type(v)), addrspace(value_type(v2))))
@assert eltype(value_type(v2)) == eltype(value_type(v))
return v2, offset, skipload
end

undeforpoison = isa(v, LLVM.UndefValue)
@static if LLVM.version() >= v"12"
undeforpoison |= isa(v, LLVM.PoisonValue)
Expand Down