Skip to content

Commit

Permalink
add intr minVer and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuseZ4 committed Oct 2, 2024
1 parent a1d7334 commit 1da1c58
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
20 changes: 19 additions & 1 deletion enzyme/Enzyme/InstructionDerivatives.td
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ def : CallPattern<(Op $x, $y),
[ReadNone, NoUnwind]
>;

def : CallPattern<(Op $x),
["tanh"],
[(FDiv (DiffeRet), (FMul(Call<(SameTypesFunc<"cosh">), [ReadNone,NoUnwind]> $x):$c, $c))],
(ForwardFromSummedReverse),
[ReadNone, NoUnwind]
>;
def : CallPattern<(Op $x),
["tanhf"],
[(FDiv (DiffeRet), (FMul(Call<(SameTypesFunc<"coshf">), [ReadNone,NoUnwind]> $x):$c, $c))],
Expand Down Expand Up @@ -867,11 +873,23 @@ def : CallPattern<(Op (Op $x, $y):$z),
>;

def : IntrPattern<(Op $x),
[["tanh"]],
[["tanh", "19"]],
[(FDiv (DiffeRet), (FMul(Intrinsic<"cosh"> $x):$c, $c))],
(ForwardFromSummedReverse)
>;

def : IntrPattern<(Op $x),
[["sinh", "19"]],
[(FMul (DiffeRet), (Intrinsic<"cosh"> $x))],
(ForwardFromSummedReverse)
>;

def : IntrPattern<(Op $x),
[["cosh", "19"]],
[(FMul (DiffeRet), (Intrinsic<"sinh"> $x))],
(ForwardFromSummedReverse)
>;

def : IntrPattern<(Op $x),
[["sin"]],
[(FMul (DiffeRet), (Intrinsic<"cos"> $x))] ,
Expand Down
28 changes: 28 additions & 0 deletions enzyme/test/Enzyme/ReverseMode/cosh19.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; RUN: if [ %llvmver -ge 19 ]; then %opt < %s %newLoadEnzyme -enzyme-preopt=false -passes="enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s; fi

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.cosh.f64(double) #14

; Function Attrs: nounwind readnone uwtable
define double @tester(double %x) {
entry:
%0 = call double @llvm.cosh.f64(double %x)
ret double %0
}

define double @test_derivative(double %x) {
entry:
%0 = tail call double (double (double)*, ...) @__enzyme_autodiff(double (double)* nonnull @tester, double %x)
ret double %0
}

; Function Attrs: nounwind
declare double @__enzyme_autodiff(double (double)*, ...)

; CHECK: define internal { double } @diffetester(double %x, double %differeturn)
; CHECK-NEXT: entry:
; CHECK-NEXT: %0 = call fast double @llvm.sinh.f64(double %x)
; CHECK-NEXT: %1 = fmul fast double %differeturn, %0
; CHECK-NEXT: %2 = insertvalue { double } undef, double %1, 0
; CHECK-NEXT: ret { double } %2
; CHECK-NEXT: }
28 changes: 28 additions & 0 deletions enzyme/test/Enzyme/ReverseMode/sinh19.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
; RUN: if [ %llvmver -ge 19 ]; then %opt < %s %newLoadEnzyme -enzyme-preopt=false -passes="enzyme,function(mem2reg,instsimplify,%simplifycfg)" -S | FileCheck %s; fi

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.sinh.f64(double) #14

; Function Attrs: nounwind readnone uwtable
define double @tester(double %x) {
entry:
%0 = call double @llvm.sinh.f64(double %x)
ret double %0
}

define double @test_derivative(double %x) {
entry:
%0 = tail call double (double (double)*, ...) @__enzyme_autodiff(double (double)* nonnull @tester, double %x)
ret double %0
}

; Function Attrs: nounwind
declare double @__enzyme_autodiff(double (double)*, ...)

; CHECK: define internal { double } @diffetester(double %x, double %differeturn)
; CHECK-NEXT: entry:
; CHECK-NEXT: %0 = call fast double @llvm.cosh.f64(double %x)
; CHECK-NEXT: %1 = fmul fast double %differeturn, %0
; CHECK-NEXT: %2 = insertvalue { double } undef, double %1, 0
; CHECK-NEXT: ret { double } %2
; CHECK-NEXT: }

0 comments on commit 1da1c58

Please sign in to comment.