From 8d75756c0792ffabf07b239440bc5c1fa48fb799 Mon Sep 17 00:00:00 2001 From: William Moses Date: Tue, 27 Feb 2024 16:53:36 -0500 Subject: [PATCH] Correct complex inv (#1767) --- enzyme/Enzyme/InstructionDerivatives.td | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/enzyme/Enzyme/InstructionDerivatives.td b/enzyme/Enzyme/InstructionDerivatives.td index 32b30bd8f9b4..6491fa032798 100644 --- a/enzyme/Enzyme/InstructionDerivatives.td +++ b/enzyme/Enzyme/InstructionDerivatives.td @@ -162,12 +162,20 @@ def CFNeg : SubRoutine<(Op (Op $re, $im):$z), (FNeg $re), (FNeg $im) )>; + +def Conj : SubRoutine<(Op (Op $re, $im):$z), + (ArrayRet + $re, + (FNeg $im) + )>; + def CFExp : SubRoutine<(Op (Op $re, $im):$z), (ArrayRet (FMul (FExp $re):$exp, (FCos $im)), (FMul $exp, (FSin $im)) )>; + // Same function as the one being called def SameFunc { } @@ -826,9 +834,10 @@ def : CallPattern<(Op (Op $x, $y):$z), def : CallPattern<(Op (Op $x, $y):$z), ["cmplx_inv"], [ - (CFDiv (CFNeg (DiffeRet)), (CFMul $z, $z)), + // Reverse mode needs to return the conjugate + (Conj (CFDiv (CFNeg (Conj (DiffeRet))), (CFMul $z, $z))), ], - (ForwardFromSummedReverse), + (CFDiv (CFNeg (Shadow $z)), (CFMul $z, $z)), [ReadNone, NoUnwind] >;