Skip to content

[flang][fir] handle poly to non poly case in rebox_assumed_rank #95240

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

Merged
merged 1 commit into from
Jun 13, 2024

Conversation

jeanPerier
Copy link
Contributor

Dynamic type and element size of the descriptor dummy must match the dummy static type when the dummy is not polymorphic, otherwise IS_CONTIGUOUS, C_SIZEOF.... won't work properly inside the callee.

When the actual argument is polymorphic the descriptor of the actual may have a different dynamic type/element size. Hence, the dummy argument cannot simply take or copy the descriptor of the actual argument.

@jeanPerier jeanPerier requested a review from clementval June 12, 2024 12:50
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Jun 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 12, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

Dynamic type and element size of the descriptor dummy must match the dummy static type when the dummy is not polymorphic, otherwise IS_CONTIGUOUS, C_SIZEOF.... won't work properly inside the callee.

When the actual argument is polymorphic the descriptor of the actual may have a different dynamic type/element size. Hence, the dummy argument cannot simply take or copy the descriptor of the actual argument.


Full diff: https://github.com/llvm/llvm-project/pull/95240.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp (+3-1)
  • (modified) flang/test/Fir/rebox_assumed_rank_codegen.fir (+13)
diff --git a/flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp b/flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp
index 2c545d66ebd8e..cb6d6a73eace2 100644
--- a/flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp
+++ b/flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp
@@ -84,7 +84,9 @@ class ReboxAssumedRankConv
     auto oldBoxType = mlir::cast<fir::BaseBoxType>(
         fir::unwrapRefType(rebox.getBox().getType()));
     auto newDerivedType = mlir::dyn_cast<fir::RecordType>(newEleType);
-    if (newDerivedType && (newEleType != oldBoxType.unwrapInnerType()) &&
+    if (newDerivedType && !fir::isPolymorphicType(newBoxType) &&
+        (fir::isPolymorphicType(oldBoxType) ||
+         (newEleType != oldBoxType.unwrapInnerType())) &&
         !fir::isPolymorphicType(newBoxType)) {
       newDtype = builder.create<fir::TypeDescOp>(
           loc, mlir::TypeAttr::get(newDerivedType));
diff --git a/flang/test/Fir/rebox_assumed_rank_codegen.fir b/flang/test/Fir/rebox_assumed_rank_codegen.fir
index 6f9cd6edda31b..3c4de0bef509f 100644
--- a/flang/test/Fir/rebox_assumed_rank_codegen.fir
+++ b/flang/test/Fir/rebox_assumed_rank_codegen.fir
@@ -34,10 +34,18 @@ func.func @test_new_dtype(%arg0: !fir.box<!fir.array<*:!t2>> ) {
   return
 }
 
+!sometype = !fir.type<sometype{i:i32}>
+func.func @test_poly_to_nonepoly(%arg0: !fir.class<!fir.array<*:!sometype>>) {
+  %1 = fir.rebox_assumed_rank %arg0 lbs ones : (!fir.class<!fir.array<*:!sometype>>) -> !fir.box<!fir.array<*:!sometype>>
+  fir.call @takes_assumed_rank_t(%1) : (!fir.box<!fir.array<*:!sometype>>) -> ()
+  return
+}
+
 func.func private @somefunc(!fir.box<!fir.array<*:f32>>)
 func.func private @somefuncalloc(!fir.box<!fir.heap<!fir.array<*:f32>>>)
 func.func private @somefuncpointer(!fir.box<!fir.ptr<!fir.array<*:f32>>>)
 func.func private @somefunct1(!fir.box<!fir.array<*:!t1>>)
+func.func private @takes_assumed_rank_t(!fir.box<!fir.array<*:!sometype>>)
 
 // CHECK-LABEL:   func.func @test_simple(
 // CHECK-SAME:                           %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>>) {
@@ -108,4 +116,9 @@ func.func private @somefunct1(!fir.box<!fir.array<*:!t1>>)
 // CHECK:           return
 // CHECK:         }
 
+// CHECK-LABEL:   func.func @test_poly_to_nonepoly(
+// CHECK:           %[[VAL_4:.*]] = fir.type_desc !fir.type<sometype{i:i32}>
+// CHECK:           %[[VAL_7:.*]] = fir.convert %[[VAL_4]] : (!fir.tdesc<!fir.type<sometype{i:i32}>>) -> !fir.ref<none>
+// CHECK:           %[[VAL_8:.*]] = fir.call @_FortranACopyAndUpdateDescriptor(%{{.*}}, %{{.*}}, %[[VAL_7]],
+
 // CHECK:         func.func private @_FortranACopyAndUpdateDescriptor(!fir.ref<!fir.box<none>> {llvm.nocapture}, !fir.box<none> {llvm.nocapture}, !fir.ref<none>, i8, i32) -> none attributes {fir.runtime}

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jeanPerier jeanPerier merged commit 2b66d28 into llvm:main Jun 13, 2024
10 checks passed
@jeanPerier jeanPerier deleted the jpr-fix-boxassume-2 branch June 13, 2024 08:44
EthanLuisMcDonough pushed a commit to EthanLuisMcDonough/llvm-project that referenced this pull request Aug 13, 2024
…#95240)

Dynamic type and element size of the descriptor dummy must match the
dummy static type when the dummy is not polymorphic, otherwise
IS_CONTIGUOUS, C_SIZEOF.... won't work properly inside the callee.

When the actual argument is polymorphic the descriptor of the actual may
have a different dynamic type/element size. Hence, the dummy argument
cannot simply take or copy the descriptor of the actual argument.
EthanLuisMcDonough pushed a commit to EthanLuisMcDonough/llvm-project that referenced this pull request Aug 13, 2024
…#95240)

Dynamic type and element size of the descriptor dummy must match the
dummy static type when the dummy is not polymorphic, otherwise
IS_CONTIGUOUS, C_SIZEOF.... won't work properly inside the callee.

When the actual argument is polymorphic the descriptor of the actual may
have a different dynamic type/element size. Hence, the dummy argument
cannot simply take or copy the descriptor of the actual argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants