[flang] Allow non-index length parameter on exprs fed into hlfir.get_length. (#124827)

The length might be any integer, so hlfir.get_length lowering
should explicitly cast it to `index`.
This commit is contained in:
Slava Zakharin 2025-01-29 12:01:13 -08:00 committed by GitHub
parent 8a334af417
commit b8708753c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View File

@ -362,6 +362,7 @@ struct GetLengthOpConversion
if (!length)
return rewriter.notifyMatchFailure(
getLength, "could not deduce length from GetLengthOp operand");
length = builder.createConvert(loc, builder.getIndexType(), length);
rewriter.replaceOp(getLength, length);
return mlir::success();
}

View File

@ -30,3 +30,22 @@ fir.global linkonce @_QQclX616263 constant : !fir.char<1,3> {
// 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,?>>)
// CHECK: return %[[VAL_9]] : index
// CHECK: }
// Test get_length taking the length from an expression with i32 length parameter.
func.func @i32_length(%char: !fir.boxchar<1>, %shape : i32, %len : i32) -> index {
%14 = fir.shape %shape : (i32) -> !fir.shape<1>
%15 = hlfir.elemental %14 typeparams %len unordered : (!fir.shape<1>, i32) -> !hlfir.expr<?x!fir.char<1,?>> {
^bb0(%arg0: index):
hlfir.yield_element %char : !fir.boxchar<1>
}
%18 = hlfir.get_length %15 : (!hlfir.expr<?x!fir.char<1,?>>) -> index
hlfir.destroy %15 : !hlfir.expr<?x!fir.char<1,?>>
return %18 : index
}
// CHECK-LABEL: func.func @i32_length(
// CHECK-SAME: %[[VAL_0:.*]]: !fir.boxchar<1>,
// CHECK-SAME: %[[VAL_1:.*]]: i32,
// CHECK-SAME: %[[VAL_2:.*]]: i32) -> index {
// CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_2]] : (i32) -> index
// CHECK: return %[[VAL_14]] : index
// CHECK: }