Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/enzyme_ad/jax/Passes/EnzymeHLOOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21918,6 +21918,10 @@ struct CommonCompareExpressionRewrite
rewriter.replaceOp(op, negatedCondition);
return success();
} else {
// The negation reads op's result, so it must sit after op — the
// rewriter's insertion point is before op here.
PatternRewriter::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(user);
auto negatedCondition = stablehlo::NotOp::create(
rewriter, userCompareOp.getLoc(), op.getResult());
rewriter.replaceOp(user, negatedCondition);
Expand Down
15 changes: 15 additions & 0 deletions test/lit_tests/commoncompare_neg_after.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: enzymexlamlir-opt %s --enzyme-hlo-opt | FileCheck %s

// The negated twin appears after the matched compare: the rewrite replaces
// it with a negation of the earlier compare, which must be inserted after
// the compare it reads, not at the match point before it.
func.func @negpair(%a: tensor<4xi32>, %b: tensor<4xi32>) -> (tensor<4xi1>, tensor<4xi1>) {
%le = stablehlo.compare LE, %a, %b, SIGNED : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1>
%gt = stablehlo.compare GT, %a, %b, SIGNED : (tensor<4xi32>, tensor<4xi32>) -> tensor<4xi1>
return %le, %gt : tensor<4xi1>, tensor<4xi1>
}

// CHECK-LABEL: func.func @negpair(
// CHECK-NEXT: %[[LE:.+]] = stablehlo.compare LE, %arg0, %arg1, SIGNED
// CHECK-NEXT: %[[NOT:.+]] = stablehlo.not %[[LE]]
// CHECK-NEXT: return %[[LE]], %[[NOT]]
Loading