File tree Expand file tree Collapse file tree 3 files changed +28
-0
lines changed
include/flang/Optimizer/HLFIR Expand file tree Collapse file tree 3 files changed +28
-0
lines changed Original file line number Diff line number Diff 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
12101211def hlfir_GetExtentOp : hlfir_Op<"get_extent", [Pure]> {
Original file line number Diff line number Diff 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// ===----------------------------------------------------------------------===//
Original file line number Diff line number Diff 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: }
You can’t perform that action at this time.
0 commit comments