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
11 changes: 9 additions & 2 deletions src/enzyme_ad/jax/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,15 @@ NonNegativeResultAnalysis::State NonNegativeResultAnalysis::localGuaranteed(

// integer ops
if (isa<stablehlo::AbsOp, stablehlo::SqrtOp, stablehlo::ExpOp,
stablehlo::IotaOp, stablehlo::AndOp, stablehlo::OrOp,
stablehlo::XorOp, stablehlo::NotOp>(op)) {
stablehlo::IotaOp>(op)) {
return State::GUARANTEED;
}

// Bitwise ops are non-negative only for boolean (i1) results: on a signed
// integer not(0) == -1, and or/xor/and can set the sign bit.
if (isa<stablehlo::AndOp, stablehlo::OrOp, stablehlo::XorOp,
stablehlo::NotOp>(op) &&
cast<ShapedType>(val.getType()).getElementType().isInteger(1)) {
return State::GUARANTEED;
}

Expand Down
9 changes: 9 additions & 0 deletions test/lit_tests/abspositive.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ func.func @test8(%arg0: tensor<4xf64>) -> tensor<4xf64> {
// CHECK-NEXT: %0 = stablehlo.logistic %arg0 {enzymexla.non_negative = [#enzymexla<guaranteed GUARANTEED>]} : tensor<4xf64>
// CHECK-NEXT: return %0 : tensor<4xf64>
// CHECK-NEXT: }

// A bitwise op on a signed integer is NOT non-negative: not(0) == -1, so the
// abs must stay.
func.func @test_signed_bitwise(%arg0: tensor<4xi32>) -> tensor<4xi32> {
%0 = stablehlo.not %arg0 : tensor<4xi32>
// CHECK: stablehlo.abs
%1 = stablehlo.abs %0 : tensor<4xi32>
return %1 : tensor<4xi32>
}