Skip to content

Commit 538da39

Browse files
committed
base hlfir.designate triplets upon Fortran bounds
1 parent 21f82df commit 538da39

File tree

3 files changed

+56
-20
lines changed

3 files changed

+56
-20
lines changed

flang/lib/Lower/OpenACC.cpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,19 +1209,45 @@ static hlfir::Entity genDesignateWithTriplets(
12091209
return hlfir::Entity{designate.getResult()};
12101210
}
12111211

1212+
// Designate uses triplets based on object lower bounds while acc.bounds are
1213+
// zero based. This helper shift the bounds to create the designate triplets.
1214+
static hlfir::DesignateOp::Subscripts
1215+
genTripletsFromAccBounds(fir::FirOpBuilder &builder, mlir::Location loc,
1216+
const llvm::SmallVector<mlir::Value> &accBounds,
1217+
hlfir::Entity entity) {
1218+
assert(entity.getRank() * 3 == static_cast<int>(accBounds.size()) &&
1219+
"must get lb,ub,step for each dimension");
1220+
hlfir::DesignateOp::Subscripts triplets;
1221+
for (unsigned i = 0; i < accBounds.size(); i += 3) {
1222+
mlir::Value lb = hlfir::genLBound(loc, builder, entity, i / 3);
1223+
lb = builder.createConvert(loc, accBounds[i].getType(), lb);
1224+
assert(accBounds[i].getType() == accBounds[i + 1].getType() &&
1225+
"mix of integer types in triplets");
1226+
mlir::Value sliceLB =
1227+
builder.createOrFold<mlir::arith::AddIOp>(loc, accBounds[i], lb);
1228+
mlir::Value sliceUB =
1229+
builder.createOrFold<mlir::arith::AddIOp>(loc, accBounds[i + 1], lb);
1230+
triplets.emplace_back(
1231+
hlfir::DesignateOp::Triplet{sliceLB, sliceUB, accBounds[i + 2]});
1232+
}
1233+
return triplets;
1234+
}
1235+
12121236
static std::pair<hlfir::Entity, hlfir::Entity>
12131237
genArraySectionsInRecipe(fir::FirOpBuilder &builder, mlir::Location loc,
12141238
llvm::SmallVector<mlir::Value> &dataOperationBounds,
12151239
mlir::ValueRange recipeArguments,
12161240
bool allConstantBound, hlfir::Entity lhs,
12171241
hlfir::Entity rhs) {
1242+
lhs = hlfir::derefPointersAndAllocatables(loc, builder, lhs);
1243+
rhs = hlfir::derefPointersAndAllocatables(loc, builder, rhs);
12181244
// Get the list of lb,ub,step values for the sections that can be used inside
12191245
// the recipe region.
12201246
llvm::SmallVector<mlir::Value> bounds;
12211247
if (allConstantBound) {
12221248
// For constant bounds, the bounds are not region arguments. Materialize
12231249
// constants looking at the IR for the bounds on the data operation.
1224-
for (auto bound : llvm::reverse(dataOperationBounds)) {
1250+
for (auto bound : dataOperationBounds) {
12251251
auto dataBound =
12261252
mlir::cast<mlir::acc::DataBoundsOp>(bound.getDefiningOp());
12271253
bounds.append(genConstantBounds(builder, loc, dataBound));
@@ -1237,20 +1263,24 @@ genArraySectionsInRecipe(fir::FirOpBuilder &builder, mlir::Location loc,
12371263
"must get lb,ub,step for each dimension");
12381264
llvm::SmallVector<mlir::Value> extents;
12391265
mlir::Type idxTy = builder.getIndexType();
1240-
hlfir::DesignateOp::Subscripts triplets;
1241-
for (unsigned i = 0; i < bounds.size(); i += 3) {
1266+
for (unsigned i = 0; i < bounds.size(); i += 3)
12421267
extents.push_back(builder.genExtentFromTriplet(
12431268
loc, bounds[i], bounds[i + 1], bounds[i + 2], idxTy));
1244-
triplets.emplace_back(
1245-
hlfir::DesignateOp::Triplet{bounds[i], bounds[i + 1], bounds[i + 2]});
1246-
}
1247-
lhs = hlfir::derefPointersAndAllocatables(loc, builder, lhs);
1248-
rhs = hlfir::derefPointersAndAllocatables(loc, builder, rhs);
12491269
mlir::Value shape = fir::ShapeOp::create(builder, loc, extents);
1270+
hlfir::DesignateOp::Subscripts rhsTriplets =
1271+
genTripletsFromAccBounds(builder, loc, bounds, rhs);
1272+
hlfir::DesignateOp::Subscripts lhsTriplets;
1273+
// Share the bounds when both rhs/lhs are known to be 1-based to avoid noise
1274+
// in the IR for the most common cases.
1275+
if (!lhs.mayHaveNonDefaultLowerBounds() &&
1276+
!rhs.mayHaveNonDefaultLowerBounds())
1277+
lhsTriplets = rhsTriplets;
1278+
else
1279+
lhsTriplets = genTripletsFromAccBounds(builder, loc, bounds, lhs);
12501280
hlfir::Entity leftSection =
1251-
genDesignateWithTriplets(builder, loc, lhs, triplets, shape);
1281+
genDesignateWithTriplets(builder, loc, lhs, lhsTriplets, shape);
12521282
hlfir::Entity rightSection =
1253-
genDesignateWithTriplets(builder, loc, rhs, triplets, shape);
1283+
genDesignateWithTriplets(builder, loc, rhs, rhsTriplets, shape);
12541284
return {leftSection, rightSection};
12551285
}
12561286

flang/test/Lower/OpenACC/acc-private.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@
174174
! CHECK: %[[CMP:.*]] = arith.cmpi sgt, %[[D2]], %[[C0]] : index
175175
! CHECK: %[[SEL:.*]] = arith.select %[[CMP]], %[[D2]], %[[C0]] : index
176176
! CHECK: %[[SH:.*]] = fir.shape %[[SEL]] : (index) -> !fir.shape<1>
177-
! CHECK: %[[SEC_SRC:.*]] = hlfir.designate %[[SRC]] (%[[C50]]:%[[C99]]:%[[C1]]) shape %[[SH]] : (!fir.ref<!fir.array<50xf32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<50xf32>>
178-
! CHECK: %[[SEC_DST:.*]] = hlfir.designate %[[DST]] (%[[C50]]:%[[C99]]:%[[C1]]) shape %[[SH]] : (!fir.ref<!fir.array<50xf32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<50xf32>>
177+
! CHECK: %[[SEC_SRC:.*]] = hlfir.designate %[[SRC]] (%c51{{.*}}:%c100{{.*}}:%c1{{.*}}) shape %[[SH]] : (!fir.ref<!fir.array<50xf32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<50xf32>>
178+
! CHECK: %[[SEC_DST:.*]] = hlfir.designate %[[DST]] (%c51{{.*}}:%c100{{.*}}:%c1{{.*}}) shape %[[SH]] : (!fir.ref<!fir.array<50xf32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<50xf32>>
179179
! CHECK: hlfir.assign %[[SEC_SRC]] to %[[SEC_DST]] temporary_lhs : !fir.ref<!fir.array<50xf32>>, !fir.ref<!fir.array<50xf32>>
180180
! CHECK: acc.terminator
181181
! CHECK: }

flang/test/Lower/OpenACC/acc-reduction.f90

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,14 @@
203203
! CHECK: %[[CMPI_0:.*]] = arith.cmpi sgt, %[[DIVSI_0]], %[[CONSTANT_3]] : index
204204
! CHECK: %[[SELECT_0:.*]] = arith.select %[[CMPI_0]], %[[DIVSI_0]], %[[CONSTANT_3]] : index
205205
! CHECK: %[[SHAPE_0:.*]] = fir.shape %[[SELECT_0]] : (index) -> !fir.shape<1>
206-
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]]) shape %[[SHAPE_0]] : (!fir.box<!fir.array<?xi32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
207-
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]]) shape %[[SHAPE_0]] : (!fir.box<!fir.array<?xi32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
206+
! CHECK: %[[BD_LHS:.*]]:3 = fir.box_dims %[[VAL_0]], %c0{{.*}} : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
207+
! CHECK: %[[LB_LHS:.*]] = arith.addi %[[BD_LHS]]#0, %c1{{.*}} : index
208+
! CHECK: %[[UB_LHS:.*]] = arith.addi %[[BD_LHS]]#0, %c3{{.*}} : index
209+
! CHECK: %[[BD_RHS:.*]]:3 = fir.box_dims %[[VAL_1]], %c0{{.*}} : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
210+
! CHECK: %[[LB_RHS:.*]] = arith.addi %[[BD_RHS]]#0, %c1{{.*}} : index
211+
! CHECK: %[[UB_RHS:.*]] = arith.addi %[[BD_RHS]]#0, %c3{{.*}} : index
212+
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%[[LB_RHS]]:%[[UB_RHS]]:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.box<!fir.array<?xi32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
213+
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%[[LB_LHS]]:%[[UB_LHS]]:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.box<!fir.array<?xi32>>, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
208214
! CHECK: %[[CONSTANT_4:.*]] = arith.constant 1 : index
209215
! CHECK: fir.do_loop %[[VAL_2:.*]] = %[[CONSTANT_4]] to %[[SELECT_0]] step %[[CONSTANT_4]] unordered {
210216
! CHECK: %[[DESIGNATE_2:.*]] = hlfir.designate %[[DESIGNATE_0]] (%[[VAL_2]]) : (!fir.box<!fir.array<?xi32>>, index) -> !fir.ref<i32>
@@ -346,10 +352,10 @@
346352
! CHECK-LABEL: } combiner {
347353
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.array<10x20xi32>>, %[[VAL_1:.*]]: !fir.ref<!fir.array<10x20xi32>>):
348354
! CHECK: %[[CONSTANT_0:.*]] = arith.constant 0 : index
349-
! CHECK: %[[CONSTANT_1:.*]] = arith.constant 19 : index
355+
! CHECK: %[[CONSTANT_1:.*]] = arith.constant 9 : index
350356
! CHECK: %[[CONSTANT_2:.*]] = arith.constant 1 : index
351357
! CHECK: %[[CONSTANT_3:.*]] = arith.constant 0 : index
352-
! CHECK: %[[CONSTANT_4:.*]] = arith.constant 9 : index
358+
! CHECK: %[[CONSTANT_4:.*]] = arith.constant 19 : index
353359
! CHECK: %[[CONSTANT_5:.*]] = arith.constant 1 : index
354360
! CHECK: %[[CONSTANT_6:.*]] = arith.constant 0 : index
355361
! CHECK: %[[SUBI_0:.*]] = arith.subi %[[CONSTANT_1]], %[[CONSTANT_0]] : index
@@ -364,8 +370,8 @@
364370
! CHECK: %[[CMPI_1:.*]] = arith.cmpi sgt, %[[DIVSI_1]], %[[CONSTANT_7]] : index
365371
! CHECK: %[[SELECT_1:.*]] = arith.select %[[CMPI_1]], %[[DIVSI_1]], %[[CONSTANT_7]] : index
366372
! CHECK: %[[SHAPE_0:.*]] = fir.shape %[[SELECT_0]], %[[SELECT_1]] : (index, index) -> !fir.shape<2>
367-
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]], %[[CONSTANT_3]]:%[[CONSTANT_4]]:%[[CONSTANT_5]]) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<10x20xi32>>, index, index, index, index, index, index, !fir.shape<2>) -> !fir.ref<!fir.array<10x20xi32>>
368-
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]], %[[CONSTANT_3]]:%[[CONSTANT_4]]:%[[CONSTANT_5]]) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<10x20xi32>>, index, index, index, index, index, index, !fir.shape<2>) -> !fir.ref<!fir.array<10x20xi32>>
373+
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%c1{{.*}}:%c10{{.*}}:%c1{{.*}}, %c1{{.*}}:%c20{{.*}}:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<10x20xi32>>, index, index, index, index, index, index, !fir.shape<2>) -> !fir.ref<!fir.array<10x20xi32>>
374+
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%c1{{.*}}:%c10{{.*}}:%c1{{.*}}, %c1{{.*}}:%c20{{.*}}:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<10x20xi32>>, index, index, index, index, index, index, !fir.shape<2>) -> !fir.ref<!fir.array<10x20xi32>>
369375
! CHECK: %[[CONSTANT_8:.*]] = arith.constant 1 : index
370376
! CHECK: fir.do_loop %[[VAL_2:.*]] = %[[CONSTANT_8]] to %[[SELECT_1]] step %[[CONSTANT_8]] unordered {
371377
! CHECK: fir.do_loop %[[VAL_3:.*]] = %[[CONSTANT_8]] to %[[SELECT_0]] step %[[CONSTANT_8]] unordered {
@@ -408,8 +414,8 @@
408414
! CHECK: %[[CMPI_0:.*]] = arith.cmpi sgt, %[[DIVSI_0]], %[[CONSTANT_3]] : index
409415
! CHECK: %[[SELECT_0:.*]] = arith.select %[[CMPI_0]], %[[DIVSI_0]], %[[CONSTANT_3]] : index
410416
! CHECK: %[[SHAPE_0:.*]] = fir.shape %[[SELECT_0]] : (index) -> !fir.shape<1>
411-
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]]) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<100xi32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<100xi32>>
412-
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%[[CONSTANT_0]]:%[[CONSTANT_1]]:%[[CONSTANT_2]]) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<100xi32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<100xi32>>
417+
! CHECK: %[[DESIGNATE_0:.*]] = hlfir.designate %[[VAL_1]] (%c11{{.*}}:%c20{{.*}}:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<100xi32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<100xi32>>
418+
! CHECK: %[[DESIGNATE_1:.*]] = hlfir.designate %[[VAL_0]] (%c11{{.*}}:%c20{{.*}}:%c1{{.*}}) shape %[[SHAPE_0]] : (!fir.ref<!fir.array<100xi32>>, index, index, index, !fir.shape<1>) -> !fir.ref<!fir.array<100xi32>>
413419
! CHECK: %[[CONSTANT_4:.*]] = arith.constant 1 : index
414420
! CHECK: fir.do_loop %[[VAL_2:.*]] = %[[CONSTANT_4]] to %[[SELECT_0]] step %[[CONSTANT_4]] unordered {
415421
! CHECK: %[[DESIGNATE_2:.*]] = hlfir.designate %[[DESIGNATE_0]] (%[[VAL_2]]) : (!fir.ref<!fir.array<100xi32>>, index) -> !fir.ref<i32>

0 commit comments

Comments
 (0)