Skip to content

Commit

Permalink
[flang] Fix creation of the bound array for pointer remapping
Browse files Browse the repository at this point in the history
The runtime function expects a 2 x newRank array and the code
was passing a newRank x 2 array. This patch updates the
creation of the array to fit the runtime expectation.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D143405
  • Loading branch information
clementval committed Feb 6, 2023
1 parent d1d93da commit 3b73fc3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
70 changes: 37 additions & 33 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,42 @@ class FirConverter : public Fortran::lower::AbstractConverter {
Fortran::lower::associateMutableBox(*this, loc, lhs, assign.rhs, lbounds,
stmtCtx);
}

// Create the 2 x newRank array with the bounds to be passed to the runtime as
// a descriptor.
mlir::Value createBoundArray(llvm::ArrayRef<mlir::Value> lbounds,
llvm::ArrayRef<mlir::Value> ubounds,
mlir::Location loc) {
assert(lbounds.size() && ubounds.size());
mlir::Type indexTy = builder->getIndexType();
mlir::Type boundArrayTy = fir::SequenceType::get(
{2, static_cast<int64_t>(lbounds.size())}, builder->getI64Type());
mlir::Value boundArray = builder->create<fir::AllocaOp>(loc, boundArrayTy);
mlir::Value array = builder->create<fir::UndefOp>(loc, boundArrayTy);
for (unsigned i = 0; i < lbounds.size(); ++i) {
array = builder->create<fir::InsertValueOp>(
loc, boundArrayTy, array, lbounds[i],
builder->getArrayAttr(
{builder->getIntegerAttr(builder->getIndexType(), 0),
builder->getIntegerAttr(builder->getIndexType(),
static_cast<int>(i))}));
array = builder->create<fir::InsertValueOp>(
loc, boundArrayTy, array, ubounds[i],
builder->getArrayAttr(
{builder->getIntegerAttr(builder->getIndexType(), 1),
builder->getIntegerAttr(builder->getIndexType(),
static_cast<int>(i))}));
}
builder->create<fir::StoreOp>(loc, array, boundArray);
mlir::Type boxTy = fir::BoxType::get(boundArrayTy);
mlir::Value ext =
builder->createIntegerConstant(loc, indexTy, lbounds.size());
mlir::Value c2 = builder->createIntegerConstant(loc, indexTy, 2);
llvm::SmallVector<mlir::Value> shapes = {c2, ext};
mlir::Value shapeOp = builder->genShape(loc, shapes);
return builder->create<fir::EmboxOp>(loc, boxTy, boundArray, shapeOp);
}

// Pointer assignment with bounds-remapping. R1036: a bounds-remapping is a
// pair, lower bound and upper bound.
void genPointerAssignment(
Expand Down Expand Up @@ -2653,39 +2689,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {

mlir::Value lhs = genExprMutableBox(loc, assign.lhs).getAddr();
mlir::Value rhs = fir::getBase(genExprBox(loc, assign.rhs, stmtCtx));

// Create the newRank x 2 array with the bounds to be passed to
// the runtime as a descriptor.
assert(lbounds.size() && ubounds.size());
mlir::Type indexTy = builder->getIndexType();
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);
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(), 0)}));
array = builder->create<fir::InsertValueOp>(
loc, boundArrayTy, array, ubounds[i],
builder->getArrayAttr(
{builder->getIntegerAttr(builder->getIndexType(),
static_cast<int>(i)),
builder->getIntegerAttr(builder->getIndexType(), 1)}));
}
builder->create<fir::StoreOp>(loc, array, boundArray);
mlir::Type boxTy = fir::BoxType::get(boundArrayTy);
mlir::Value ext =
builder->createIntegerConstant(loc, indexTy, lbounds.size());
mlir::Value c2 = builder->createIntegerConstant(loc, indexTy, 2);
llvm::SmallVector<mlir::Value> shapes = {ext, c2};
mlir::Value shapeOp = builder->genShape(loc, shapes);
mlir::Value boundsDesc =
builder->create<fir::EmboxOp>(loc, boxTy, boundArray, shapeOp);
mlir::Value boundsDesc = createBoundArray(lbounds, ubounds, loc);
Fortran::lower::genPointerAssociateRemapping(*builder, loc, lhs, rhs,
boundsDesc);
return;
Expand Down
22 changes: 11 additions & 11 deletions flang/test/Lower/polymorphic.f90
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ subroutine pointer_assign_remap()
! CHECK: %[[BOUND_ARRAY:.*]] = fir.alloca !fir.array<2x2xi64>
! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<2x2xi64>
! 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: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C10_0]], [1 : index, 0 : index] : (!fir.array<2x2xi64>, i64) -> !fir.array<2x2xi64>
! CHECK: %[[ARRAY2:.*]] = fir.insert_value %[[ARRAY1]], %[[C1_1]], [0 : index, 1 : 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: %[[C2_0:.*]] = arith.constant 2 : index
! CHECK: %[[C2_1:.*]] = arith.constant 2 : index
! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C2_0]], %[[C2_1]] : (index, index) -> !fir.shape<2>
! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C2_1]], %[[C2_0]] : (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: %[[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>
Expand All @@ -420,18 +420,18 @@ subroutine pointer_assign_remap()
! 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<1x2xi64>
! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<1x2xi64>
! CHECK: %[[ARRAY0:.*]] = fir.insert_value %[[ARRAY]], %[[C0]], [0 : index, 0 : index] : (!fir.array<1x2xi64>, i64) -> !fir.array<1x2xi64>
! CHECK: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C99]], [0 : index, 1 : index] : (!fir.array<1x2xi64>, i64) -> !fir.array<1x2xi64>
! CHECK: fir.store %[[ARRAY1]] to %[[BOUND_ARRAY]] : !fir.ref<!fir.array<1x2xi64>>
! CHECK: %[[BOUND_ARRAY:.*]] = fir.alloca !fir.array<2x1xi64>
! CHECK: %[[ARRAY:.*]] = fir.undefined !fir.array<2x1xi64>
! CHECK: %[[ARRAY0:.*]] = fir.insert_value %[[ARRAY]], %[[C0]], [0 : index, 0 : index] : (!fir.array<2x1xi64>, i64) -> !fir.array<2x1xi64>
! CHECK: %[[ARRAY1:.*]] = fir.insert_value %[[ARRAY0]], %[[C99]], [1 : index, 0 : index] : (!fir.array<2x1xi64>, i64) -> !fir.array<2x1xi64>
! CHECK: fir.store %[[ARRAY1]] to %[[BOUND_ARRAY]] : !fir.ref<!fir.array<2x1xi64>>
! CHECK: %[[C1:.*]] = arith.constant 1 : index
! CHECK: %[[C2:.*]] = arith.constant 2 : index
! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C1]], %[[C2]] : (index, index) -> !fir.shape<2>
! CHECK: %[[BOXED_BOUND_ARRAY:.*]] = fir.embox %[[BOUND_ARRAY]](%[[BOUND_ARRAY_SHAPE]]) : (!fir.ref<!fir.array<1x2xi64>>, !fir.shape<2>) -> !fir.box<!fir.array<1x2xi64>>
! CHECK: %[[BOUND_ARRAY_SHAPE:.*]] = fir.shape %[[C2]], %[[C1]] : (index, index) -> !fir.shape<2>
! CHECK: %[[BOXED_BOUND_ARRAY:.*]] = fir.embox %[[BOUND_ARRAY]](%[[BOUND_ARRAY_SHAPE]]) : (!fir.ref<!fir.array<2x1xi64>>, !fir.shape<2>) -> !fir.box<!fir.array<2x1xi64>>
! 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<1x2xi64>>) -> !fir.box<none>
! CHECK: %[[ARG2:.*]] = fir.convert %[[BOXED_BOUND_ARRAY]] : (!fir.box<!fir.array<2x1xi64>>) -> !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

subroutine test_elemental_assign()
Expand Down

0 comments on commit 3b73fc3

Please sign in to comment.