Skip to content

Commit 326f58d

Browse files
authored
[flang][HLFIR] lower hlfir.declare of assumed-ranks (#93468)
hlfir.declare is in charge of ensuring that the lower bounds of its "hlfir entity" output are the ones of the source program. For non-allocatable/non-pointer assumed-ranks where the input descriptor lower bounds may not be ones, the hlfir.declare needs to be lowered to an hlfir.rebox_assumed_rank to set the lower bounds to ones.
1 parent 5aba0de commit 326f58d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,17 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
348348
// Helper to generate the hlfir fir.box with the local lower bounds and
349349
// type parameters.
350350
auto genHlfirBox = [&]() -> mlir::Value {
351-
if (!mlir::isa<fir::BaseBoxType>(firBase.getType())) {
351+
if (auto baseBoxType =
352+
mlir::dyn_cast<fir::BaseBoxType>(firBase.getType())) {
353+
// Rebox so that lower bounds are correct.
354+
if (baseBoxType.isAssumedRank())
355+
return builder.create<fir::ReboxAssumedRankOp>(
356+
loc, hlfirBaseType, firBase,
357+
fir::LowerBoundModifierAttribute::SetToOnes);
358+
return builder.create<fir::ReboxOp>(loc, hlfirBaseType, firBase,
359+
declareOp.getShape(),
360+
/*slice=*/mlir::Value{});
361+
} else {
352362
llvm::SmallVector<mlir::Value> typeParams;
353363
auto maybeCharType = mlir::dyn_cast<fir::CharacterType>(
354364
fir::unwrapSequenceType(fir::unwrapPassByRefType(hlfirBaseType)));
@@ -358,11 +368,6 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
358368
return builder.create<fir::EmboxOp>(
359369
loc, hlfirBaseType, firBase, declareOp.getShape(),
360370
/*slice=*/mlir::Value{}, typeParams);
361-
} else {
362-
// Rebox so that lower bounds are correct.
363-
return builder.create<fir::ReboxOp>(loc, hlfirBaseType, firBase,
364-
declareOp.getShape(),
365-
/*slice=*/mlir::Value{});
366371
}
367372
};
368373
if (!mlir::cast<fir::FortranVariableOpInterface>(declareOp.getOperation())

flang/test/HLFIR/declare-codegen.fir

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,12 @@ func.func @dummy_scope(%arg0: !fir.ref<f32>) {
210210
// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f32>) {
211211
// CHECK: %[[SCOPE:.*]] = fir.dummy_scope : !fir.dscope
212212
// CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[SCOPE]] {uniq_name = "x"} : (!fir.ref<f32>, !fir.dscope) -> !fir.ref<f32>
213+
214+
func.func @assumed_rank_declare(%arg0: !fir.box<!fir.array<*:f32>>) {
215+
%0:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<*:f32>>) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
216+
return
217+
}
218+
// CHECK-LABEL: func.func @assumed_rank_declare(
219+
// CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>>) {
220+
// CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>
221+
// CHECK: %[[VAL_2:.*]] = fir.rebox_assumed_rank %[[VAL_1]] lbs ones : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>

0 commit comments

Comments
 (0)