Skip to content

Commit 2b66d28

Browse files
authored
[flang][fir] handle poly to non poly case in rebox_assumed_rank (#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.
1 parent 8b435c1 commit 2b66d28

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

flang/lib/Optimizer/Transforms/AssumedRankOpConversion.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ class ReboxAssumedRankConv
8484
auto oldBoxType = mlir::cast<fir::BaseBoxType>(
8585
fir::unwrapRefType(rebox.getBox().getType()));
8686
auto newDerivedType = mlir::dyn_cast<fir::RecordType>(newEleType);
87-
if (newDerivedType && (newEleType != oldBoxType.unwrapInnerType()) &&
87+
if (newDerivedType && !fir::isPolymorphicType(newBoxType) &&
88+
(fir::isPolymorphicType(oldBoxType) ||
89+
(newEleType != oldBoxType.unwrapInnerType())) &&
8890
!fir::isPolymorphicType(newBoxType)) {
8991
newDtype = builder.create<fir::TypeDescOp>(
9092
loc, mlir::TypeAttr::get(newDerivedType));

flang/test/Fir/rebox_assumed_rank_codegen.fir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ func.func @test_new_dtype(%arg0: !fir.box<!fir.array<*:!t2>> ) {
3434
return
3535
}
3636

37+
!sometype = !fir.type<sometype{i:i32}>
38+
func.func @test_poly_to_nonepoly(%arg0: !fir.class<!fir.array<*:!sometype>>) {
39+
%1 = fir.rebox_assumed_rank %arg0 lbs ones : (!fir.class<!fir.array<*:!sometype>>) -> !fir.box<!fir.array<*:!sometype>>
40+
fir.call @takes_assumed_rank_t(%1) : (!fir.box<!fir.array<*:!sometype>>) -> ()
41+
return
42+
}
43+
3744
func.func private @somefunc(!fir.box<!fir.array<*:f32>>)
3845
func.func private @somefuncalloc(!fir.box<!fir.heap<!fir.array<*:f32>>>)
3946
func.func private @somefuncpointer(!fir.box<!fir.ptr<!fir.array<*:f32>>>)
4047
func.func private @somefunct1(!fir.box<!fir.array<*:!t1>>)
48+
func.func private @takes_assumed_rank_t(!fir.box<!fir.array<*:!sometype>>)
4149

4250
// CHECK-LABEL: func.func @test_simple(
4351
// CHECK-SAME: %[[VAL_0:.*]]: !fir.box<!fir.array<*:f32>>) {
@@ -108,4 +116,9 @@ func.func private @somefunct1(!fir.box<!fir.array<*:!t1>>)
108116
// CHECK: return
109117
// CHECK: }
110118

119+
// CHECK-LABEL: func.func @test_poly_to_nonepoly(
120+
// CHECK: %[[VAL_4:.*]] = fir.type_desc !fir.type<sometype{i:i32}>
121+
// CHECK: %[[VAL_7:.*]] = fir.convert %[[VAL_4]] : (!fir.tdesc<!fir.type<sometype{i:i32}>>) -> !fir.ref<none>
122+
// CHECK: %[[VAL_8:.*]] = fir.call @_FortranACopyAndUpdateDescriptor(%{{.*}}, %{{.*}}, %[[VAL_7]],
123+
111124
// 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}

0 commit comments

Comments
 (0)