Skip to content

Commit

Permalink
Fix constant instruction binop in forward mode (rust-lang#680)
Browse files Browse the repository at this point in the history
* Fix constant instruction binop in forward mode

* Fix llvm7
  • Loading branch information
wsmoses authored Jun 6, 2022
1 parent 55e050e commit 2f01442
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions enzyme/Enzyme/AdjointGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1760,8 +1760,6 @@ class AdjointGenerator

void visitBinaryOperator(llvm::BinaryOperator &BO) {
eraseIfUnused(BO);
if (gutils->isConstantInstruction(&BO))
return;

size_t size = 1;
if (BO.getType()->isSized())
Expand All @@ -1778,6 +1776,8 @@ class AdjointGenerator
switch (Mode) {
case DerivativeMode::ReverseModeGradient:
case DerivativeMode::ReverseModeCombined:
if (gutils->isConstantInstruction(&BO))
return;
createBinaryOperatorAdjoint(BO);
break;
case DerivativeMode::ForwardMode:
Expand Down Expand Up @@ -2255,6 +2255,11 @@ class AdjointGenerator
}

void createBinaryOperatorDual(llvm::BinaryOperator &BO) {
if (gutils->isConstantInstruction(&BO)) {
forwardModeInvertedPointerFallback(BO);
return;
}

IRBuilder<> Builder2(&BO);
getForwardBuilder(Builder2);

Expand Down
22 changes: 22 additions & 0 deletions enzyme/test/Enzyme/ForwardMode/inactivebinop.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: %opt < %s %loadEnzyme -enzyme -enzyme-preopt=false -mem2reg -instsimplify -simplifycfg -S | FileCheck %s

declare void @_Z16__enzyme_fwddiff(...)

define void @_Z34testFwdDerivativesRosenbrockEnzymev(i8* %a, i8* %b) {
call void (...) @_Z16__enzyme_fwddiff(double (i64*)* @f, metadata !"enzyme_dup", i8* %a, i8* %b)
ret void
}

define double @f(i64* %i10) {
bb:
%i13 = load i64, i64* %i10, align 8
%i14 = sub i64 2, %i13
%i15 = sdiv exact i64 %i14, 8
%a5 = uitofp i64 %i15 to double
ret double %a5
}

; CHECK: define internal double @fwddiffef(i64* %i10, i64* %"i10'")
; CHECK-NEXT: bb:
; CHECK-NEXT: ret double 0.000000e+00
; CHECK-NEXT: }

0 comments on commit 2f01442

Please sign in to comment.