Skip to content

[mlir][tosa] Avoid overflow in reduction folders #132786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
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
6 changes: 2 additions & 4 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1731,8 +1731,7 @@ def Tosa_ReduceMaxOp : Tosa_InferTensorTypeOp<"reduce_max"> {

/// Return the max of the two integer operands
static inline APInt calcOneElement(APInt leftOperand, APInt rightOperand) {
const llvm::APInt subtractRes = leftOperand - rightOperand;
return (!subtractRes.isNegative()) ? leftOperand : rightOperand;
return (leftOperand.sge(rightOperand)) ? leftOperand : rightOperand;
}
}];
}
Expand Down Expand Up @@ -1772,8 +1771,7 @@ def Tosa_ReduceMinOp : Tosa_InferTensorTypeOp<"reduce_min"> {

/// Return the min of the two integer operands
static inline APInt calcOneElement(APInt leftOperand, APInt rightOperand) {
const llvm::APInt subtractRes = leftOperand - rightOperand;
return (!subtractRes.isNegative()) ? rightOperand : leftOperand;
return (leftOperand.sle(rightOperand)) ? leftOperand : rightOperand;
}
}];
}
Expand Down
25 changes: 25 additions & 0 deletions mlir/test/Dialect/Tosa/constant-op-fold.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,18 @@ func.func @reduce_max_constant() -> tensor<1x1x1xi32> {
return %0 : tensor<1x1x1xi32>
}

// -----

func.func @reduce_max_constant_no_overflow() -> tensor<1xi8> {
// CHECK-LABEL: func.func @reduce_max_constant_no_overflow() -> tensor<1xi8> {
// CHECK: %[[VAL_0:.*]] = "tosa.const"() <{values = dense<120> : tensor<1xi8>}> : () -> tensor<1xi8>
// CHECK: return %[[VAL_0]] : tensor<1xi8>
// CHECK: }
%const = "tosa.const"() <{values = dense<[-127, 120, -126]> : tensor<3xi8>}> : () -> tensor<3xi8>
%0 = tosa.reduce_max %const {axis = 0 : i32} : (tensor<3xi8>) -> tensor<1xi8>
return %0 : tensor<1xi8>
}

// -----

func.func @reduce_min_constant() -> tensor<1x3xi32> {
Expand Down Expand Up @@ -968,6 +980,19 @@ func.func @reduce_min_constant() -> tensor<1x1x1xi32> {
return %0 : tensor<1x1x1xi32>
}

// -----

func.func @reduce_min_constant_no_overflow() -> tensor<1xi8> {
// CHECK-LABEL: func.func @reduce_min_constant_no_overflow() -> tensor<1xi8> {
// CHECK: %[[VAL_0:.*]] = "tosa.const"() <{values = dense<-127> : tensor<1xi8>}> : () -> tensor<1xi8>
// CHECK: return %[[VAL_0]] : tensor<1xi8>
// CHECK: }
%const = "tosa.const"() <{values = dense<[-127, 120, -126]> : tensor<3xi8>}> : () -> tensor<3xi8>
%0 = tosa.reduce_min %const {axis = 0 : i32} : (tensor<3xi8>) -> tensor<1xi8>
return %0 : tensor<1xi8>
}


// -----

func.func @reduce_any_constant() -> tensor<1x3xi1> {
Expand Down