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

boxfloat fixup #1726

Merged
merged 1 commit into from
Aug 11, 2024
Merged
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
61 changes: 39 additions & 22 deletions src/rules/llvmrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,12 @@ end
@register_fwd function boxfloat_fwd(B, orig, gutils, normalR, shadowR)
origops = collect(operands(orig))
width = get_width(gutils)
if is_constant_value(gutils, orig)

needsShadowP = Ref{UInt8}(0)
needsPrimalP = Ref{UInt8}(0)
activep = API.EnzymeGradientUtilsGetReturnDiffeType(gutils, orig, needsPrimalP, needsShadowP, get_mode(gutils))

if is_constant_value(gutils, orig) || needsShadowP[] == 0
return true
end

Expand All @@ -616,7 +621,12 @@ end
@register_aug function boxfloat_augfwd(B, orig, gutils, normalR, shadowR, tapeR)
origops = collect(operands(orig))
width = get_width(gutils)
if is_constant_value(gutils, orig)

needsShadowP = Ref{UInt8}(0)
needsPrimalP = Ref{UInt8}(0)
activep = API.EnzymeGradientUtilsGetReturnDiffeType(gutils, orig, needsPrimalP, needsShadowP, get_mode(gutils))

if is_constant_value(gutils, orig) || needsShadowP[] == 0
return true
end

Expand All @@ -642,30 +652,37 @@ end
end

@register_rev function boxfloat_rev(B, orig, gutils, tape)

needsShadowP = Ref{UInt8}(0)
needsPrimalP = Ref{UInt8}(0)
activep = API.EnzymeGradientUtilsGetReturnDiffeType(gutils, orig, needsPrimalP, needsShadowP, get_mode(gutils))

if is_constant_value(gutils, orig) || needsShadowP[] == 0
return nothing
end

origops = collect(operands(orig))
width = get_width(gutils)
if !is_constant_value(gutils, orig)
ip = lookup_value(gutils, invert_pointer(gutils, orig, B), B)
flt = value_type(origops[1])
if width == 1
ipc = bitcast!(B, ip, LLVM.PointerType(flt, addrspace(value_type(orig))))
ip = lookup_value(gutils, invert_pointer(gutils, orig, B), B)
flt = value_type(origops[1])
if width == 1
ipc = bitcast!(B, ip, LLVM.PointerType(flt, addrspace(value_type(orig))))
ld = load!(B, flt, ipc)
store!(B, ConstantFP(flt, 0.0), ipc)
if !is_constant_value(gutils, origops[1])
API.EnzymeGradientUtilsAddToDiffe(gutils, origops[1], ld, B, flt)
end
else
shadowres = UndefValue(LLVM.LLVMType(API.EnzymeGetShadowType(width, flt)))
for idx in 1:width
ipc = extract_value!(B, ip, idx-1)
ipc = bitcast!(B, ipc, LLVM.PointerType(flt, addrspace(value_type(orig))))
ld = load!(B, flt, ipc)
store!(B, ConstantFP(flt, 0.0), ipc)
if !is_constant_value(gutils, origops[1])
API.EnzymeGradientUtilsAddToDiffe(gutils, origops[1], ld, B, flt)
end
else
shadowres = UndefValue(LLVM.LLVMType(API.EnzymeGetShadowType(width, flt)))
for idx in 1:width
ipc = extract_value!(B, ip, idx-1)
ipc = bitcast!(B, ipc, LLVM.PointerType(flt, addrspace(value_type(orig))))
ld = load!(B, flt, ipc)
store!(B, ConstantFP(flt, 0.0), ipc)
shadowres = insert_value!(B, shadowres, ld, idx-1)
end
if !is_constant_value(gutils, origops[1])
API.EnzymeGradientUtilsAddToDiffe(gutils, origops[1], shadowret, B, flt)
end
shadowres = insert_value!(B, shadowres, ld, idx-1)
end
if !is_constant_value(gutils, origops[1])
API.EnzymeGradientUtilsAddToDiffe(gutils, origops[1], shadowret, B, flt)
end
end
return nothing
Expand Down
Loading