Skip to content

[FXML-1991] Reciprocal Constant Folding For a Splat Tensor #32

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
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
2 changes: 2 additions & 0 deletions mlir/include/mlir/Dialect/Tosa/IR/TosaOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,8 @@ def Tosa_ReciprocalOp : Tosa_Op<"reciprocal", [
let results = (outs
Tosa_Tensor:$output
);

let hasFolder = 1;
}

//===----------------------------------------------------------------------===//
Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,26 @@ OpFoldResult MulOp::fold(ArrayRef<Attribute> operands) {
return mulBinaryFolder(lhsAttr, rhsAttr, lhsTy, getShift());
}

OpFoldResult ReciprocalOp::fold(ArrayRef<Attribute> operands) {
auto constantAttr = dyn_cast_or_null<DenseElementsAttr>(operands[0]);
auto lhsTy = dyn_cast<RankedTensorType>(getInput1().getType());

if (!lhsTy || !constantAttr) {
return {};
}

if (!constantAttr.isSplat()) {
return {};
}

auto floatVal = constantAttr.getSplatValue<llvm::APFloat>();

auto recipAttr = FloatAttr::get(lhsTy.getElementType(), 1.0);
APFloat recip = recipAttr.getValue();
recip.divide(floatVal, APFloat::rmNearestTiesToEven);
return DenseElementsAttr::get(lhsTy, recip);
}

OpFoldResult SubOp::fold(ArrayRef<Attribute> operands) {
auto lhsTy = getInput1().getType().dyn_cast<RankedTensorType>();
auto rhsTy = getInput2().getType().dyn_cast<RankedTensorType>();
Expand Down
23 changes: 23 additions & 0 deletions mlir/test/Dialect/Tosa/constant-op-fold.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,29 @@ func.func @fold_mul_splat_f32() -> tensor<10xf32> {

// -----

// CHECK-LABEL: @fold_reciprocal_splat_f32
func.func @fold_reciprocal_splat_f32() -> tensor<f32> {
%half = "tosa.const"() {value = dense<0.5> : tensor<f32>} : () -> tensor<f32>
%recp = "tosa.reciprocal"(%half) : (tensor<f32>) -> tensor<f32>
// CHECK: %[[CST:.*]] = "tosa.const"() {value = dense<2.000000e+00> : tensor<f32>}
// CHECK: return %[[CST]]
return %recp : tensor<f32>
}

// -----

// CHECK-LABEL: @fold_reciprocal_splat_zero_f32
func.func @fold_reciprocal_splat_zero_f32() -> tensor<f32> {
%zero = "tosa.const"() {value = dense<0.0> : tensor<f32>} : () -> tensor<f32>
%recp = "tosa.reciprocal"(%zero) : (tensor<f32>) -> tensor<f32>
// 0x7F800000 represents +inf as we have computed 1/0
// CHECK: %[[CST:.*]] = "tosa.const"() {value = dense<0x7F800000> : tensor<f32>}
// CHECK: return %[[CST]]
return %recp : tensor<f32>
}

// -----

// CHECK-LABEL: @fold_sub_zero_rhs_f32
func.func @fold_sub_zero_rhs_f32(%arg0: tensor<f32>) -> tensor<f32> {
%zero = "tosa.const"() {value = dense<0.0> : tensor<f32>} : () -> tensor<f32>
Expand Down