Skip to content

Commit 139e69b

Browse files
authored
[flang] Simple folding for hlfir.shape_of. (#119649)
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.
1 parent 2546ae4 commit 139e69b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

flang/include/flang/Optimizer/HLFIR/HLFIROps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ def hlfir_ShapeOfOp : hlfir_Op<"shape_of", [Pure]> {
12051205
}];
12061206

12071207
let builders = [OpBuilder<(ins "mlir::Value":$expr)>];
1208+
let hasFolder = 1;
12081209
}
12091210

12101211
def hlfir_GetExtentOp : hlfir_Op<"get_extent", [Pure]> {

flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,15 @@ hlfir::ShapeOfOp::canonicalize(ShapeOfOp shapeOf,
17041704
return llvm::LogicalResult::success();
17051705
}
17061706

1707+
mlir::OpFoldResult hlfir::ShapeOfOp::fold(FoldAdaptor adaptor) {
1708+
if (matchPattern(getExpr(), mlir::m_Op<hlfir::ElementalOp>())) {
1709+
auto elementalOp =
1710+
mlir::cast<hlfir::ElementalOp>(getExpr().getDefiningOp());
1711+
return elementalOp.getShape();
1712+
}
1713+
return {};
1714+
}
1715+
17071716
//===----------------------------------------------------------------------===//
17081717
// GetExtent
17091718
//===----------------------------------------------------------------------===//

flang/test/HLFIR/shapeof.fir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ func.func @shapeof2(%arg0: !hlfir.expr<?x2xi32>) -> !fir.shape<2> {
2727
// CHECK-ALL: %[[EXPR:.*]]: !hlfir.expr<?x2xi32>
2828
// CHECK-ALL-NEXT: %[[SHAPE:.*]] = hlfir.shape_of %[[EXPR]] : (!hlfir.expr<?x2xi32>) -> !fir.shape<2>
2929
// CHECK-ALL-NEXT: return %[[SHAPE]]
30+
31+
// Checks hlfir.elemental -> hlfir.shape_of folding
32+
func.func @shapeof_fold1(%extent: index) -> !fir.shape<1> {
33+
%shape1 = fir.shape %extent : (index) -> !fir.shape<1>
34+
%elem = hlfir.elemental %shape1 : (!fir.shape<1>) -> !hlfir.expr<?xindex> {
35+
hlfir.yield_element %extent : index
36+
}
37+
%shape2 = hlfir.shape_of %elem : (!hlfir.expr<?xindex>) -> !fir.shape<1>
38+
return %shape2 : !fir.shape<1>
39+
}
40+
// CHECK-ALL-LABEL: func.func @shapeof_fold1(
41+
// CHECK-ALL-SAME: %[[VAL_0:.*]]: index) -> !fir.shape<1> {
42+
// CHECK-CANON-NEXT: %[[VAL_1:.*]] = fir.shape %[[VAL_0]] : (index) -> !fir.shape<1>
43+
// CHECK-CANON-NEXT: %[[VAL_2:.*]] = hlfir.elemental %[[VAL_1]] : (!fir.shape<1>) -> !hlfir.expr<?xindex> {
44+
// CHECK-CANON-NEXT: hlfir.yield_element %[[VAL_0]] : index
45+
// CHECK-CANON-NEXT: }
46+
// CHECK-CANON-NEXT: return %[[VAL_1]] : !fir.shape<1>
47+
// CHECK-CANON-NEXT: }

0 commit comments

Comments
 (0)