[flang-commits] [flang] 491b6a9 - [flang] Fix pointer association with remap on polymorphic entities
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Mon Dec 5 09:29:30 PST 2022
Author: Valentin Clement
Date: 2022-12-05T18:29:06+01:00
New Revision: 491b6a9ccb05e5e6784ce50718570d204378c75e
URL: https://github.com/llvm/llvm-project/commit/491b6a9ccb05e5e6784ce50718570d204378c75e
DIFF: https://github.com/llvm/llvm-project/commit/491b6a9ccb05e5e6784ce50718570d204378c75e.diff
LOG: [flang] Fix pointer association with remap on polymorphic entities
Runtime is expecting a 1d array. This patch fixes the generation
of the array holding the bounds to be passed to the runtime function call.
Reviewed By: jeanPerier, PeteSteinfeld
Differential Revision: https://reviews.llvm.org/D139324
Added:
Modified:
flang/lib/Lower/Bridge.cpp
flang/test/Lower/polymorphic.f90
Removed:
################################################################################
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index a45b325a5d6bd..8913cb8aff7e8 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -2804,45 +2804,38 @@ class FirConverter : public Fortran::lower::AbstractConverter {
(rhsType && rhsType->IsPolymorphic())) {
if (explicitIterationSpace())
TODO(loc, "polymorphic pointer assignment in FORALL");
-
+
mlir::Value lhs = genExprMutableBox(loc, assign.lhs).getAddr();
mlir::Value rhs =
fir::getBase(genExprBox(loc, assign.rhs, stmtCtx));
- // Create the 2xnewRank array with the bounds to be passed to
+ // Create the newRank x 2 array with the bounds to be passed to
// the runtime as a descriptor.
assert(lbounds.size() && ubounds.size());
- fir::SequenceType::Shape shape(2, lbounds.size());
mlir::Type indexTy = builder->getIndexType();
- mlir::Type boundArrayTy =
- fir::SequenceType::get(shape, builder->getI64Type());
+ mlir::Type boundArrayTy = fir::SequenceType::get(
+ {static_cast<int64_t>(lbounds.size()) * 2},
+ builder->getI64Type());
mlir::Value boundArray =
builder->create<fir::AllocaOp>(loc, boundArrayTy);
mlir::Value array =
builder->create<fir::UndefOp>(loc, boundArrayTy);
- llvm::SmallVector<mlir::Value> exts;
- mlir::Value c2 =
- builder->createIntegerConstant(loc, indexTy, 2);
for (unsigned i = 0; i < lbounds.size(); ++i) {
array = builder->create<fir::InsertValueOp>(
loc, boundArrayTy, array, lbounds[i],
- builder->getArrayAttr(
- {builder->getIntegerAttr(builder->getIndexType(),
- static_cast<int>(i)),
- builder->getIntegerAttr(builder->getIndexType(),
- static_cast<int>(0))}));
+ builder->getArrayAttr({builder->getIntegerAttr(
+ builder->getIndexType(), static_cast<int>(i * 2))}));
array = builder->create<fir::InsertValueOp>(
loc, boundArrayTy, array, ubounds[i],
- builder->getArrayAttr(
- {builder->getIntegerAttr(builder->getIndexType(),
- static_cast<int>(i)),
- builder->getIntegerAttr(builder->getIndexType(),
- static_cast<int>(1))}));
- exts.push_back(c2);
+ builder->getArrayAttr({builder->getIntegerAttr(
+ builder->getIndexType(),
+ static_cast<int>(i * 2 + 1))}));
}
builder->create<fir::StoreOp>(loc, array, boundArray);
mlir::Type boxTy = fir::BoxType::get(boundArrayTy);
- mlir::Value shapeOp = builder->genShape(loc, exts);
+ mlir::Value ext = builder->createIntegerConstant(
+ loc, indexTy, lbounds.size() * 2);
+ mlir::Value shapeOp = builder->genShape(loc, {ext});
mlir::Value boundsDesc = builder->create<fir::EmboxOp>(
loc, boxTy, boundArray, shapeOp);
Fortran::lower::genPointerAssociateRemapping(*builder, loc, lhs,
diff --git a/flang/test/Lower/polymorphic.f90 b/flang/test/Lower/polymorphic.f90
index 37afc5dc6ec82..3ac805556dee1 100644
--- a/flang/test/Lower/polymorphic.f90
+++ b/flang/test/Lower/polymorphic.f90
@@ -327,32 +327,52 @@ subroutine assign_polymorphic_allocatable()
subroutine pointer_assign_remap()
class(p1), pointer :: a(:)
class(p1), pointer :: p(:,:)
+ class(p1), pointer :: q(:)
allocate(a(100))
p(1:10,1:10) => a
+ q(0:99) => a
end subroutine
! CHECK-LABEL: func.func @_QMpolymorphic_testPpointer_assign_remap() {
! CHECK: %[[A:.*]] = fir.alloca !fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>> {bindc_name = "a", uniq_name = "_QMpolymorphic_testFpointer_assign_remapEa"}
! CHECK: %[[P:.*]] = fir.alloca !fir.class<!fir.ptr<!fir.array<?x?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>> {bindc_name = "p", uniq_name = "_QMpolymorphic_testFpointer_assign_remapEp"}
+! CHECK: %[[Q:.*]] = fir.alloca !fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>> {bindc_name = "q", uniq_name = "_QMpolymorphic_testFpointer_assign_remapEq"}
! CHECK: %[[C1_0:.*]] = arith.constant 1 : i64
! CHECK: %[[C10_0:.*]] = arith.constant 10 : i64
! CHECK: %[[C1_1:.*]] = arith.constant 1 : i64
! CHECK: %[[C10_1:.*]] = arith.constant 10 : i64
! CHECK: %[[LOAD_A:.*]] = fir.load %[[A]] : !fir.ref<!fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>>
! CHECK: %[[REBOX_A:.*]] = fir.rebox %[[LOAD_A]](%{{.*}}) : (!fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>, !fir.shift<1>) -> !fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>
-! CHECK: %[[BOUND_ARRAY:.*]] = fir.alloca !fir.array<2x2xi64>
-! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<2x2xi64>
-! CHECK: %[[C2:.*]] = arith.constant 2 : index
-! CHECK: %[[ARRAY0:.*]] = fir.insert_value %[[ARRAY]], %[[C1_0]], [0 : index, 0 : index] : (!fir.array<2x2xi64>, i64) -> !fir.array<2x2xi64>
-! CHECK: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C10_0]], [0 : index, 1 : index] : (!fir.array<2x2xi64>, i64) -> !fir.array<2x2xi64>
-! CHECK: %[[ARRAY2:.*]] = fir.insert_value %[[ARRAY1]], %[[C1_1]], [1 : index, 0 : index] : (!fir.array<2x2xi64>, i64) -> !fir.array<2x2xi64>
-! CHECK: %[[ARRAY3:.*]] = fir.insert_value %[[ARRAY2]], %[[C10_1]], [1 : index, 1 : index] : (!fir.array<2x2xi64>, i64) -> !fir.array<2x2xi64>
-! CHECK: fir.store %[[ARRAY3]] to %[[BOUND_ARRAY]] : !fir.ref<!fir.array<2x2xi64>>
-! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C2]], %[[C2]] : (index, index) -> !fir.shape<2>
-! CHECK: %[[BOXED_BOUND_ARRAY:.*]] = fir.embox %[[BOUND_ARRAY]](%[[BOUND_ARRAY_SHAPE]]) : (!fir.ref<!fir.array<2x2xi64>>, !fir.shape<2>) -> !fir.box<!fir.array<2x2xi64>>
+! CHECK: %[[BOUND_ARRAY:.*]] = fir.alloca !fir.array<4xi64>
+! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<4xi64>
+! CHECK: %[[ARRAY0:.*]] = fir.insert_value %[[ARRAY]], %[[C1_0]], [0 : index] : (!fir.array<4xi64>, i64) -> !fir.array<4xi64>
+! CHECK: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C10_0]], [1 : index] : (!fir.array<4xi64>, i64) -> !fir.array<4xi64>
+! CHECK: %[[ARRAY2:.*]] = fir.insert_value %[[ARRAY1]], %[[C1_1]], [2 : index] : (!fir.array<4xi64>, i64) -> !fir.array<4xi64>
+! CHECK: %[[ARRAY3:.*]] = fir.insert_value %[[ARRAY2]], %[[C10_1]], [3 : index] : (!fir.array<4xi64>, i64) -> !fir.array<4xi64>
+! CHECK: fir.store %[[ARRAY3]] to %[[BOUND_ARRAY]] : !fir.ref<!fir.array<4xi64>>
+! CHECK: %[[C4:.*]] = arith.constant 4 : index
+! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C4]] : (index) -> !fir.shape<1>
+! CHECK: %[[BOXED_BOUND_ARRAY:.*]] = fir.embox %[[BOUND_ARRAY]](%[[BOUND_ARRAY_SHAPE]]) : (!fir.ref<!fir.array<4xi64>>, !fir.shape<1>) -> !fir.box<!fir.array<4xi64>>
! CHECK: %[[ARG0:.*]] = fir.convert %[[P]] : (!fir.ref<!fir.class<!fir.ptr<!fir.array<?x?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>>) -> !fir.ref<!fir.box<none>>
! CHECK: %[[ARG1:.*]] = fir.convert %[[REBOX_A]] : (!fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>) -> !fir.box<none>
-! CHECK: %[[ARG2:.*]] = fir.convert %[[BOXED_BOUND_ARRAY]] : (!fir.box<!fir.array<2x2xi64>>) -> !fir.box<none>
-! CHECK: %{{.*}} = fir.call @_FortranAPointerAssociateRemapping(%[[ARG0]], %[[ARG1]], %[[ARG2]], %{{.*}}, %{{.*}}) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
+! CHECK: %[[ARG2:.*]] = fir.convert %[[BOXED_BOUND_ARRAY]] : (!fir.box<!fir.array<4xi64>>) -> !fir.box<none>
+! CHECK: %{{.*}} = fir.call @_FortranAPointerAssociateRemapping(%[[ARG0]], %[[ARG1]], %[[ARG2]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
+
+! CHECK: %[[C0:.*]] = arith.constant 0 : i64
+! CHECK: %[[C99:.*]] = arith.constant 99 : i64
+! CHECK: %[[LOAD_A:.*]] = fir.load %[[A]] : !fir.ref<!fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>>
+! CHECK: %[[REBOX_A:.*]] = fir.rebox %[[LOAD_A]](%{{.*}}) : (!fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>, !fir.shift<1>) -> !fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>
+! CHECK: %[[BOUND_ARRAY:.*]] = fir.alloca !fir.array<2xi64>
+! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<2xi64>
+! CHECK: %[[ARRAY0:.*]] = fir.insert_value %[[ARRAY]], %[[C0]], [0 : index] : (!fir.array<2xi64>, i64) -> !fir.array<2xi64>
+! CHECK: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C99]], [1 : index] : (!fir.array<2xi64>, i64) -> !fir.array<2xi64>
+! CHECK: fir.store %[[ARRAY1]] to %[[BOUND_ARRAY]] : !fir.ref<!fir.array<2xi64>>
+! CHECK: %[[C2:.*]] = arith.constant 2 : index
+! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C2]] : (index) -> !fir.shape<1>
+! CHECK: %[[BOXED_BOUND_ARRAY:.*]] = fir.embox %[[BOUND_ARRAY]](%[[BOUND_ARRAY_SHAPE]]) : (!fir.ref<!fir.array<2xi64>>, !fir.shape<1>) -> !fir.box<!fir.array<2xi64>>
+! CHECK: %[[ARG0:.*]] = fir.convert %[[Q]] : (!fir.ref<!fir.class<!fir.ptr<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>>>) -> !fir.ref<!fir.box<none>>
+! CHECK: %[[ARG1:.*]] = fir.convert %[[REBOX_A]] : (!fir.class<!fir.array<?x!fir.type<_QMpolymorphic_testTp1{a:i32,b:i32}>>>) -> !fir.box<none>
+! CHECK: %[[ARG2:.*]] = fir.convert %[[BOXED_BOUND_ARRAY]] : (!fir.box<!fir.array<2xi64>>) -> !fir.box<none>
+! CHECK: %{{.*}} = fir.call @_FortranAPointerAssociateRemapping(%[[ARG0]], %[[ARG1]], %[[ARG2]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.ref<i8>, i32) -> none
end module
More information about the flang-commits
mailing list