-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang][HLFIR] lower hlfir.declare of assumed-ranks #93468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
@llvm/pr-subscribers-flang-fir-hlfir Author: None (jeanPerier) Changeshlfir.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. Full diff: https://github.com/llvm/llvm-project/pull/93468.diff 2 Files Affected:
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
index e56595d1c8e23..44552359930c1 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
@@ -348,7 +348,17 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
// Helper to generate the hlfir fir.box with the local lower bounds and
// type parameters.
auto genHlfirBox = [&]() -> mlir::Value {
- if (!mlir::isa<fir::BaseBoxType>(firBase.getType())) {
+ if (auto baseBoxType =
+ mlir::dyn_cast<fir::BaseBoxType>(firBase.getType())) {
+ // Rebox so that lower bounds are correct.
+ if (baseBoxType.isAssumedRank())
+ return builder.create<fir::ReboxAssumedRankOp>(
+ loc, hlfirBaseType, firBase,
+ fir::LowerBoundModifierAttribute::SetToOnes);
+ return builder.create<fir::ReboxOp>(loc, hlfirBaseType, firBase,
+ declareOp.getShape(),
+ /*slice=*/mlir::Value{});
+ } else {
llvm::SmallVector<mlir::Value> typeParams;
auto maybeCharType = mlir::dyn_cast<fir::CharacterType>(
fir::unwrapSequenceType(fir::unwrapPassByRefType(hlfirBaseType)));
@@ -358,11 +368,6 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
return builder.create<fir::EmboxOp>(
loc, hlfirBaseType, firBase, declareOp.getShape(),
/*slice=*/mlir::Value{}, typeParams);
- } else {
- // Rebox so that lower bounds are correct.
- return builder.create<fir::ReboxOp>(loc, hlfirBaseType, firBase,
- declareOp.getShape(),
- /*slice=*/mlir::Value{});
}
};
if (!mlir::cast<fir::FortranVariableOpInterface>(declareOp.getOperation())
diff --git a/flang/test/HLFIR/declare-codegen.fir b/flang/test/HLFIR/declare-codegen.fir
index 9f51d0fbc7afd..bd0d61a2559db 100644
--- a/flang/test/HLFIR/declare-codegen.fir
+++ b/flang/test/HLFIR/declare-codegen.fir
@@ -210,3 +210,12 @@ func.func @dummy_scope(%arg0: !fir.ref<f32>) {
// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<f32>) {
// CHECK: %[[SCOPE:.*]] = fir.dummy_scope : !fir.dscope
// CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] dummy_scope %[[SCOPE]] {uniq_name = "x"} : (!fir.ref<f32>, !fir.dscope) -> !fir.ref<f32>
+
+func.func @assumed_rank_declare(%arg0: !fir.box<!fir.array<*:f32>>) {
+ %0:2 = hlfir.declare %arg0 {uniq_name = "x"} : (!fir.box<!fir.array<*:f32>>) -> (!fir.box<!fir.array<*:f32>>, !fir.box<!fir.array<*:f32>>)
+ return
+}
+// CHECK-LABEL: func.func @assumed_rank_declare(
+// CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>>) {
+// CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "x"} : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>
+// CHECK: %[[VAL_2:.*]] = fir.rebox_assumed_rank %[[VAL_1]] lbs ones : (!fir.box<!fir.array<*:f32>>) -> !fir.box<!fir.array<*:f32>>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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.
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.