Skip to content

Commit 2f077df

Browse files
authored
[flang] Support non-index shape in hlfir.get_extent codegen. (#124622)
hlfir.reshape inlining uncovered an existing bug that non-index shapes result in failures during hlfir.get_extent conversion to FIR. I could have "fixed" the shape during hlfir.reshape inlining, but this seems like a better fix.
1 parent 947d8eb commit 2f077df

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ class GetExtentOpConversion
735735
llvm::APInt dim = getExtentOp.getDim();
736736
uint64_t dimVal = dim.getLimitedValue(shapeTy.getRank());
737737
mlir::Value extent = s.getExtents()[dimVal];
738+
fir::FirOpBuilder builder(rewriter, getExtentOp.getOperation());
739+
extent = builder.createConvert(getExtentOp.getLoc(),
740+
builder.getIndexType(), extent);
738741
rewriter.replaceOp(getExtentOp, extent);
739742
return mlir::success();
740743
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Test hlfir.get_extent code generation to FIR
2+
// RUN: fir-opt %s -convert-hlfir-to-fir | FileCheck %s
3+
4+
func.func @test1(%arg0: i1, %arg1: i32, %arg2: i64, %arg3: index) -> (index, index, index, index) {
5+
%0 = fir.shape %arg0, %arg1, %arg2, %arg3 : (i1, i32, i64, index) -> !fir.shape<4>
6+
%1 = hlfir.get_extent %0 {dim = 0 : index} : (!fir.shape<4>) -> index
7+
%2 = hlfir.get_extent %0 {dim = 1 : index} : (!fir.shape<4>) -> index
8+
%3 = hlfir.get_extent %0 {dim = 2 : index} : (!fir.shape<4>) -> index
9+
%4 = hlfir.get_extent %0 {dim = 3 : index} : (!fir.shape<4>) -> index
10+
return %1, %2, %3, %4 : index, index, index, index
11+
}
12+
// CHECK-LABEL: func.func @test1(
13+
// CHECK-SAME: %[[VAL_0:.*]]: i1,
14+
// CHECK-SAME: %[[VAL_1:.*]]: i32,
15+
// CHECK-SAME: %[[VAL_2:.*]]: i64,
16+
// CHECK-SAME: %[[VAL_3:.*]]: index) -> (index, index, index, index) {
17+
// CHECK: %[[VAL_4:.*]] = fir.shape %[[VAL_0]], %[[VAL_1]], %[[VAL_2]], %[[VAL_3]] : (i1, i32, i64, index) -> !fir.shape<4>
18+
// CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_0]] : (i1) -> index
19+
// CHECK: %[[VAL_6:.*]] = fir.convert %[[VAL_1]] : (i32) -> index
20+
// CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_2]] : (i64) -> index
21+
// CHECK: return %[[VAL_5]], %[[VAL_6]], %[[VAL_7]], %[[VAL_3]] : index, index, index, index
22+
// CHECK: }

0 commit comments

Comments
 (0)