Skip to content

Commit fb22d2f

Browse files
authored
Fix fmuladd fwd inactive (rust-lang#705)
1 parent 3d28f83 commit fb22d2f

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

enzyme/Enzyme/AdjointGenerator.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -3917,8 +3917,13 @@ class AdjointGenerator
39173917
: diffe(orig_ops[2], Builder2);
39183918

39193919
auto rule = [&](Value *dif0, Value *dif1, Value *dif2) {
3920-
Value *dif = Builder2.CreateFAdd(Builder2.CreateFMul(op0, dif1),
3921-
Builder2.CreateFMul(op1, dif0));
3920+
Value *dif =
3921+
Builder2.CreateFAdd(gutils->isConstantValue(orig_ops[1])
3922+
? Constant::getNullValue(opType1)
3923+
: Builder2.CreateFMul(op0, dif1),
3924+
gutils->isConstantValue(orig_ops[2])
3925+
? Constant::getNullValue(opType2)
3926+
: Builder2.CreateFMul(op1, dif0));
39223927
return Builder2.CreateFAdd(dif, dif2);
39233928
};
39243929

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; RUN: %opt < %s %loadEnzyme -enzyme -enzyme-preopt=false -mem2reg -gvn -simplifycfg -instcombine -S | FileCheck %s
2+
3+
; Function Attrs: nounwind readnone uwtable
4+
define double @tester(double* %yp) {
5+
entry:
6+
%y = load double, double* %yp, align 8
7+
%0 = tail call fast double @llvm.fmuladd.f64(double 1.000000e+00, double %y, double 0.000000e+00)
8+
ret double %0
9+
}
10+
11+
define double @test_derivative(double* %yp, double* %dyp) {
12+
entry:
13+
%0 = tail call double (double (double*)*, ...) @__enzyme_fwddiff(double (double*)* nonnull @tester, double* %yp, double* %dyp)
14+
ret double %0
15+
}
16+
17+
declare double @llvm.fmuladd.f64(double %a, double %b, double %c)
18+
19+
; Function Attrs: nounwind
20+
declare double @__enzyme_fwddiff(double (double*)*, ...)
21+
22+
; CHECK: define internal double @fwddiffetester(double* %yp, double* %"yp'")
23+
; CHECK-NEXT: entry:
24+
; CHECK-NEXT: %"y'ipl" = load double, double* %"yp'", align 8
25+
; CHECK-NEXT: ret double %"y'ipl"
26+
; CHECK-NEXT: }

0 commit comments

Comments
 (0)