Skip to content

Commit b870875

Browse files
authored
[flang] Allow non-index length parameter on exprs fed into hlfir.get_length. (llvm#124827)
The length might be any integer, so hlfir.get_length lowering should explicitly cast it to `index`.
1 parent 8a334af commit b870875

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ struct GetLengthOpConversion
362362
if (!length)
363363
return rewriter.notifyMatchFailure(
364364
getLength, "could not deduce length from GetLengthOp operand");
365+
length = builder.createConvert(loc, builder.getIndexType(), length);
365366
rewriter.replaceOp(getLength, length);
366367
return mlir::success();
367368
}

flang/test/HLFIR/get_length_codegen.fir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,22 @@ fir.global linkonce @_QQclX616263 constant : !fir.char<1,3> {
3030
// CHECK: %[[VAL_33:.*]]:2 = hlfir.declare %[[VAL_31:.*]] typeparams %[[VAL_9]] {uniq_name = ".tmp"} : (!fir.ref<!fir.char<1,?>>, index) -> (!fir.boxchar<1>, !fir.ref<!fir.char<1,?>>)
3131
// CHECK: return %[[VAL_9]] : index
3232
// CHECK: }
33+
34+
// Test get_length taking the length from an expression with i32 length parameter.
35+
func.func @i32_length(%char: !fir.boxchar<1>, %shape : i32, %len : i32) -> index {
36+
%14 = fir.shape %shape : (i32) -> !fir.shape<1>
37+
%15 = hlfir.elemental %14 typeparams %len unordered : (!fir.shape<1>, i32) -> !hlfir.expr<?x!fir.char<1,?>> {
38+
^bb0(%arg0: index):
39+
hlfir.yield_element %char : !fir.boxchar<1>
40+
}
41+
%18 = hlfir.get_length %15 : (!hlfir.expr<?x!fir.char<1,?>>) -> index
42+
hlfir.destroy %15 : !hlfir.expr<?x!fir.char<1,?>>
43+
return %18 : index
44+
}
45+
// CHECK-LABEL: func.func @i32_length(
46+
// CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>,
47+
// CHECK-SAME: %[[VAL_1:.*]]: i32,
48+
// CHECK-SAME: %[[VAL_2:.*]]: i32) -> index {
49+
// CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_2]] : (i32) -> index
50+
// CHECK: return %[[VAL_14]] : index
51+
// CHECK: }

0 commit comments

Comments
 (0)