Skip to content

[FXML-1991] Update Add and Sub Constant Folding #33

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
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
10 changes: 6 additions & 4 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,6 @@ OpFoldResult AddOp::fold(ArrayRef<Attribute> operands) {
auto resultTy = getType().dyn_cast<RankedTensorType>();
if (!lhsTy || !rhsTy || !resultTy)
return {};
if (lhsTy != rhsTy)
return {};

auto resultETy = resultTy.getElementType();
auto lhsAttr = operands[0].dyn_cast_or_null<DenseElementsAttr>();
Expand Down Expand Up @@ -504,6 +502,9 @@ OpFoldResult AddOp::fold(ArrayRef<Attribute> operands) {
if (!lhsAttr || !rhsAttr)
return {};

if (lhsTy != rhsTy)
return {};

return binaryFolder<std::plus<APInt>, std::plus<APFloat>>(lhsAttr, rhsAttr,
lhsTy);
}
Expand Down Expand Up @@ -635,8 +636,6 @@ OpFoldResult SubOp::fold(ArrayRef<Attribute> operands) {
auto resultTy = getType().dyn_cast<RankedTensorType>();
if (!lhsTy || !rhsTy || !resultTy)
return {};
if (lhsTy != rhsTy)
return {};

auto resultETy = resultTy.getElementType();
auto lhsAttr = operands[0].dyn_cast_or_null<DenseElementsAttr>();
Expand All @@ -655,6 +654,9 @@ OpFoldResult SubOp::fold(ArrayRef<Attribute> operands) {
if (!lhsAttr || !rhsAttr)
return {};

if (lhsTy != rhsTy)
return {};

return binaryFolder<std::minus<APInt>, std::minus<APFloat>>(lhsAttr, rhsAttr,
lhsTy);
}
Expand Down
32 changes: 32 additions & 0 deletions mlir/test/Dialect/Tosa/constant-op-fold.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ func.func @fold_add_splat_f32() -> tensor<10xf32> {

// -----

// CHECK-LABEL: @fold_add_zero_splat_different_shape_f32
func.func @fold_add_zero_splat_different_shape_f32(%arg0: tensor<1x10xf32>) -> tensor<1x10xf32> {
%zero = "tosa.const"() {value = dense<0.0> : tensor<1x1xf32>} : () -> tensor<1x1xf32>
%add = "tosa.add"(%arg0, %zero) : (tensor<1x10xf32>, tensor<1x1xf32>) -> tensor<1x10xf32>
// CHECK: return %arg0
return %add : tensor<1x10xf32>
}

// -----

// CHECK-LABEL: @fold_add_zero_broadcast_arg_f32
func.func @fold_add_zero_broadcast_arg_f32(%arg0: tensor<1x10xf32>) -> tensor<4x10xf32> {
%zero = "tosa.const"() {value = dense<0.0> : tensor<1x1xf32>} : () -> tensor<4x10xf32>
%add = "tosa.add"(%arg0, %zero) : (tensor<1x10xf32>, tensor<4x10xf32>) -> tensor<4x10xf32>
// CHECK: %[[ZERO:.+]] = "tosa.const"() {value = dense<0.000000e+00> : tensor<1x1xf32>} : () -> tensor<4x10xf32>
// CHECK: %[[ADD:.+]] = "tosa.add"(%arg0, %[[ZERO]]) : (tensor<1x10xf32>, tensor<4x10xf32>) -> tensor<4x10xf32>
// CHECK: return %[[ADD]] : tensor<4x10xf32>
return %add : tensor<4x10xf32>
}

// -----

// CHECK-LABEL: @fold_div_zero_lhs_i32
func.func @fold_div_zero_lhs_i32(%arg0: tensor<i32>) -> tensor<i32> {
%zero = "tosa.const"() {value = dense<0> : tensor<i32>} : () -> tensor<i32>
Expand Down Expand Up @@ -350,6 +372,16 @@ func.func @fold_sub_splat_f32() -> tensor<10xf32> {

// -----

// CHECK-LABEL: @fold_sub_zero_splat_different_shape_f32
func.func @fold_sub_zero_splat_different_shape_f32(%arg0: tensor<1x10xf32>) -> tensor<1x10xf32> {
%zero = "tosa.const"() {value = dense<0.0> : tensor<1x1xf32>} : () -> tensor<1x1xf32>
%sub = "tosa.sub"(%arg0, %zero) : (tensor<1x10xf32>, tensor<1x1xf32>) -> tensor<1x10xf32>
// CHECK: return %arg0
return %sub : tensor<1x10xf32>
}

// -----

// CHECK-LABEL: @fold_greater_splat_f32
func.func @fold_greater_splat_f32() -> (tensor<10xi1>, tensor<10xi1>) {
%0 = "tosa.const"() {value = dense<4.0> : tensor<10xf32>} : () -> tensor<10xf32>
Expand Down