Skip to content

[flang] Simple folding for hlfir.shape_of. #119649

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
Dec 12, 2024

Conversation

vzakhari
Copy link
Contributor

This folding makes sure there are no hlfir.shape_of users
of hlfir.elemental - this may enable more InlineElementals matches,
because it is looking for exactly two uses of an hlfir.elemental.

This folding makes sure there are no hlfir.shape_of users
of hlfir.elemental - this may enable more InlineElementals matches,
because it is looking for exactly two uses of an hlfir.elemental.
@vzakhari vzakhari requested review from tblah and jeanPerier December 12, 2024 02:54
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Dec 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: Slava Zakharin (vzakhari)

Changes

This folding makes sure there are no hlfir.shape_of users
of hlfir.elemental - this may enable more InlineElementals matches,
because it is looking for exactly two uses of an hlfir.elemental.


Full diff: https://github.com/llvm/llvm-project/pull/119649.diff

3 Files Affected:

  • (modified) flang/include/flang/Optimizer/HLFIR/HLFIROps.td (+1)
  • (modified) flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp (+9)
  • (modified) flang/test/HLFIR/shapeof.fir (+18)
diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
index f11162dc0d95e1..48764580d526d2 100644
--- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
+++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
@@ -1205,6 +1205,7 @@ def hlfir_ShapeOfOp : hlfir_Op<"shape_of", [Pure]> {
   }];
 
   let builders = [OpBuilder<(ins "mlir::Value":$expr)>];
+  let hasFolder = 1;
 }
 
 def hlfir_GetExtentOp : hlfir_Op<"get_extent", [Pure]> {
diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
index ad53527f43441b..82aac7cafa1d0e 100644
--- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -1704,6 +1704,15 @@ hlfir::ShapeOfOp::canonicalize(ShapeOfOp shapeOf,
   return llvm::LogicalResult::success();
 }
 
+mlir::OpFoldResult hlfir::ShapeOfOp::fold(FoldAdaptor adaptor) {
+  if (matchPattern(getExpr(), mlir::m_Op<hlfir::ElementalOp>())) {
+    auto elementalOp =
+        mlir::cast<hlfir::ElementalOp>(getExpr().getDefiningOp());
+    return elementalOp.getShape();
+  }
+  return {};
+}
+
 //===----------------------------------------------------------------------===//
 // GetExtent
 //===----------------------------------------------------------------------===//
diff --git a/flang/test/HLFIR/shapeof.fir b/flang/test/HLFIR/shapeof.fir
index b91efc276b62e2..43e22dd320c18b 100644
--- a/flang/test/HLFIR/shapeof.fir
+++ b/flang/test/HLFIR/shapeof.fir
@@ -27,3 +27,21 @@ func.func @shapeof2(%arg0: !hlfir.expr<?x2xi32>) -> !fir.shape<2> {
 // CHECK-ALL:          %[[EXPR:.*]]: !hlfir.expr<?x2xi32>
 // CHECK-ALL-NEXT:   %[[SHAPE:.*]] = hlfir.shape_of %[[EXPR]] : (!hlfir.expr<?x2xi32>) -> !fir.shape<2>
 // CHECK-ALL-NEXT:   return %[[SHAPE]]
+
+// Checks hlfir.elemental -> hlfir.shape_of folding
+func.func @shapeof_fold1(%extent: index) -> !fir.shape<1> {
+  %shape1 = fir.shape %extent : (index) -> !fir.shape<1>
+  %elem = hlfir.elemental %shape1 : (!fir.shape<1>) -> !hlfir.expr<?xindex> {
+    hlfir.yield_element %extent : index
+  }
+  %shape2 = hlfir.shape_of %elem : (!hlfir.expr<?xindex>) -> !fir.shape<1>
+  return %shape2 : !fir.shape<1>
+}
+// CHECK-ALL-LABEL:   func.func @shapeof_fold1(
+// CHECK-ALL-SAME:        %[[VAL_0:.*]]: index) -> !fir.shape<1> {
+// CHECK-CANON-NEXT:    %[[VAL_1:.*]] = fir.shape %[[VAL_0]] : (index) -> !fir.shape<1>
+// CHECK-CANON-NEXT:    %[[VAL_2:.*]] = hlfir.elemental %[[VAL_1]] : (!fir.shape<1>) -> !hlfir.expr<?xindex> {
+// CHECK-CANON-NEXT:      hlfir.yield_element %[[VAL_0]] : index
+// CHECK-CANON-NEXT:    }
+// CHECK-CANON-NEXT:    return %[[VAL_1]] : !fir.shape<1>
+// CHECK-CANON-NEXT:  }

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vzakhari vzakhari merged commit 139e69b into llvm:main Dec 12, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants