Skip to content

Commit 2a4392c

Browse files
authored
2 parents 4f775bc + e6a673c commit 2a4392c

17 files changed

+186
-69
lines changed

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,49 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
122122
typeError();
123123
}
124124

125-
fir::ShapeShiftOp Fortran::lower::omp::getShapeShift(fir::FirOpBuilder &builder,
126-
mlir::Location loc,
127-
mlir::Value box) {
125+
fir::ShapeShiftOp
126+
Fortran::lower::omp::getShapeShift(fir::FirOpBuilder &builder,
127+
mlir::Location loc, mlir::Value box,
128+
bool useDefaultLowerBounds) {
128129
fir::SequenceType sequenceType = mlir::cast<fir::SequenceType>(
129130
hlfir::getFortranElementOrSequenceType(box.getType()));
130131
const unsigned rank = sequenceType.getDimension();
131132
llvm::SmallVector<mlir::Value> lbAndExtents;
132133
lbAndExtents.reserve(rank * 2);
133134

134135
mlir::Type idxTy = builder.getIndexType();
135-
for (unsigned i = 0; i < rank; ++i) {
136-
// TODO: ideally we want to hoist box reads out of the critical section.
137-
// We could do this by having box dimensions in block arguments like
138-
// OpenACC does
139-
mlir::Value dim = builder.createIntegerConstant(loc, idxTy, i);
140-
auto dimInfo =
141-
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
142-
lbAndExtents.push_back(dimInfo.getLowerBound());
143-
lbAndExtents.push_back(dimInfo.getExtent());
136+
137+
mlir::Value oneVal;
138+
auto one = [&] {
139+
if (!oneVal)
140+
oneVal = builder.createIntegerConstant(loc, idxTy, 1);
141+
return oneVal;
142+
};
143+
144+
if ((useDefaultLowerBounds) && !sequenceType.hasDynamicExtents()) {
145+
// We don't need fir::BoxDimsOp if all of the extents are statically known
146+
// and we can assume default lower bounds. This helps avoids reads from the
147+
// mold arg.
148+
// We may also want to use default lower bounds to iterate through array
149+
// elements without having to adjust each index.
150+
for (int64_t extent : sequenceType.getShape()) {
151+
assert(extent != sequenceType.getUnknownExtent());
152+
lbAndExtents.push_back(one());
153+
mlir::Value extentVal = builder.createIntegerConstant(loc, idxTy, extent);
154+
lbAndExtents.push_back(extentVal);
155+
}
156+
} else {
157+
for (unsigned i = 0; i < rank; ++i) {
158+
// TODO: ideally we want to hoist box reads out of the critical section.
159+
// We could do this by having box dimensions in block arguments like
160+
// OpenACC does
161+
mlir::Value dim = builder.createIntegerConstant(loc, idxTy, i);
162+
auto dimInfo =
163+
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
164+
lbAndExtents.push_back(useDefaultLowerBounds ? one()
165+
: dimInfo.getLowerBound());
166+
lbAndExtents.push_back(dimInfo.getExtent());
167+
}
144168
}
145169

146170
auto shapeShiftTy = fir::ShapeShiftType::get(builder.getContext(), rank);

flang/lib/Lower/OpenMP/PrivateReductionUtils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ void populateByRefInitAndCleanupRegions(
5858
const Fortran::semantics::Symbol *sym = nullptr);
5959

6060
/// Generate a fir::ShapeShift op describing the provided boxed array.
61+
/// `cannotHaveNonDefaultLowerBounds` should be set if `box` is known to have
62+
/// default lower bounds. This can improve code generation.
63+
/// `useDefaultLowerBounds` can be set to force the returned fir::ShapeShiftOp
64+
/// to have default lower bounds, which is useful to iterate through array
65+
/// elements without having to adjust each index.
6166
fir::ShapeShiftOp getShapeShift(fir::FirOpBuilder &builder, mlir::Location loc,
62-
mlir::Value box);
67+
mlir::Value box,
68+
bool useDefaultLowerBounds = false);
6369

6470
} // namespace omp
6571
} // namespace lower

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ static void genBoxCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
337337
return;
338338
}
339339

340-
fir::ShapeShiftOp shapeShift = getShapeShift(builder, loc, lhs);
340+
// Get ShapeShift with default lower bounds. This makes it possible to use
341+
// unmodified LoopNest's indices with ArrayCoorOp.
342+
fir::ShapeShiftOp shapeShift = getShapeShift(builder, loc, lhs,
343+
/*useDefaultLowerBounds=*/true);
341344

342345
// Iterate over array elements, applying the equivalent scalar reduction:
343346

flang/test/Lower/OpenMP/parallel-reduction-allocatable-array.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ program reduce
5555
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
5656
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
5757
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, index) -> (index, index, index)
58-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
58+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
59+
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[C1]], %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
5960
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
6061
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
6162
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.heap<!fir.array<?xi32>>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>

flang/test/Lower/OpenMP/parallel-reduction-array-lb.f90

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ program reduce
3737
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.array<3x2xi32>>>, %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.array<3x2xi32>>>):
3838
! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.box<!fir.array<3x2xi32>>>
3939
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.array<3x2xi32>>>
40-
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
41-
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.array<3x2xi32>>, index) -> (index, index, index)
42-
! CHECK: %[[VAL_6:.*]] = arith.constant 1 : index
43-
! CHECK: %[[VAL_7:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_6]] : (!fir.box<!fir.array<3x2xi32>>, index) -> (index, index, index)
44-
! CHECK: %[[VAL_8:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1, %[[VAL_7]]#0, %[[VAL_7]]#1 : (index, index, index, index) -> !fir.shapeshift<2>
45-
! CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
46-
! CHECK: fir.do_loop %[[VAL_10:.*]] = %[[VAL_9]] to %[[VAL_7]]#1 step %[[VAL_9]] unordered {
47-
! CHECK: fir.do_loop %[[VAL_11:.*]] = %[[VAL_9]] to %[[VAL_5]]#1 step %[[VAL_9]] unordered {
48-
! CHECK: %[[VAL_12:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_8]]) %[[VAL_11]], %[[VAL_10]] : (!fir.box<!fir.array<3x2xi32>>, !fir.shapeshift<2>, index, index) -> !fir.ref<i32>
49-
! CHECK: %[[VAL_13:.*]] = fir.array_coor %[[VAL_3]](%[[VAL_8]]) %[[VAL_11]], %[[VAL_10]] : (!fir.box<!fir.array<3x2xi32>>, !fir.shapeshift<2>, index, index) -> !fir.ref<i32>
40+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
41+
! CHECK: %[[C3:.*]] = arith.constant 3 : index
42+
! CHECK: %[[C2:.*]] = arith.constant 2 : index
43+
! CHECK: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[C1]], %[[C3]], %[[C1]], %[[C2]] : (index, index, index, index) -> !fir.shapeshift<2>
44+
! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
45+
! CHECK: fir.do_loop %[[VAL_10:.*]] = %[[C1_0]] to %[[C2]] step %[[C1_0]] unordered {
46+
! CHECK: fir.do_loop %[[VAL_11:.*]] = %[[C1_0]] to %[[C3]] step %[[C1_0]] unordered {
47+
! CHECK: %[[VAL_12:.*]] = fir.array_coor %[[VAL_2]](%[[SHAPE_SHIFT]]) %[[VAL_11]], %[[VAL_10]] : (!fir.box<!fir.array<3x2xi32>>, !fir.shapeshift<2>, index, index) -> !fir.ref<i32>
48+
! CHECK: %[[VAL_13:.*]] = fir.array_coor %[[VAL_3]](%[[SHAPE_SHIFT]]) %[[VAL_11]], %[[VAL_10]] : (!fir.box<!fir.array<3x2xi32>>, !fir.shapeshift<2>, index, index) -> !fir.ref<i32>
5049
! CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_12]] : !fir.ref<i32>
5150
! CHECK: %[[VAL_15:.*]] = fir.load %[[VAL_13]] : !fir.ref<i32>
5251
! CHECK: %[[VAL_16:.*]] = arith.addi %[[VAL_14]], %[[VAL_15]] : i32

flang/test/Lower/OpenMP/parallel-reduction-array.f90

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ program reduce
3636
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.array<3xi32>>>, %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.array<3xi32>>>):
3737
! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.box<!fir.array<3xi32>>>
3838
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.array<3xi32>>>
39-
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
40-
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.array<3xi32>>, index) -> (index, index, index)
41-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
42-
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
43-
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
44-
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
45-
! CHECK: %[[VAL_10:.*]] = fir.array_coor %[[VAL_3]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
39+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
40+
! CHECK: %[[C3:.*]] = arith.constant 3 : index
41+
! CHECK: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[C1]], %[[C3]] : (index, index) -> !fir.shapeshift<1>
42+
! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
43+
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[C1_0]] to %[[C3]] step %[[C1_0]] unordered {
44+
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[SHAPE_SHIFT]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
45+
! CHECK: %[[VAL_10:.*]] = fir.array_coor %[[VAL_3]](%[[SHAPE_SHIFT]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
4646
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_9]] : !fir.ref<i32>
4747
! CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_10]] : !fir.ref<i32>
4848
! CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_11]], %[[VAL_12]] : i32

flang/test/Lower/OpenMP/parallel-reduction-array2.f90

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ program reduce
3535
! CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.array<3xi32>>>, %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.array<3xi32>>>):
3636
! CHECK: %[[VAL_2:.*]] = fir.load %[[VAL_0]] : !fir.ref<!fir.box<!fir.array<3xi32>>>
3737
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.array<3xi32>>>
38-
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
39-
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.array<3xi32>>, index) -> (index, index, index)
40-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
41-
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
42-
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
43-
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
44-
! CHECK: %[[VAL_10:.*]] = fir.array_coor %[[VAL_3]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
38+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
39+
! CHECK: %[[C3:.*]] = arith.constant 3 : index
40+
! CHECK: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[C1]], %[[C3]] : (index, index) -> !fir.shapeshift<1>
41+
! CHECK: %[[C1_0:.*]] = arith.constant 1 : index
42+
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[C1_0]] to %[[C3]] step %[[C1_0]] unordered {
43+
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[SHAPE_SHIFT]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
44+
! CHECK: %[[VAL_10:.*]] = fir.array_coor %[[VAL_3]](%[[SHAPE_SHIFT]]) %[[VAL_8]] : (!fir.box<!fir.array<3xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
4545
! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_9]] : !fir.ref<i32>
4646
! CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_10]] : !fir.ref<i32>
4747
! CHECK: %[[VAL_13:.*]] = arith.addi %[[VAL_11]], %[[VAL_12]] : i32

flang/test/Lower/OpenMP/parallel-reduction-pointer-array.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ program reduce
5656
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
5757
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
5858
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, index) -> (index, index, index)
59-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
59+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
60+
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[C1]], %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
6061
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
6162
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
6263
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>

flang/test/Lower/OpenMP/parallel-reduction3.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.array<?xi32>>>
2727
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
2828
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
29-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
29+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
30+
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[C1]], %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
3031
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
3132
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
3233
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<?xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>

flang/test/Lower/OpenMP/reduction-array-intrinsic.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ subroutine max_array_reduction(l, r)
3434
! CHECK: %[[VAL_3:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.box<!fir.array<?xi32>>>
3535
! CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
3636
! CHECK: %[[VAL_5:.*]]:3 = fir.box_dims %[[VAL_2]], %[[VAL_4]] : (!fir.box<!fir.array<?xi32>>, index) -> (index, index, index)
37-
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[VAL_5]]#0, %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
37+
! CHECK: %[[C1:.*]] = arith.constant 1 : index
38+
! CHECK: %[[VAL_6:.*]] = fir.shape_shift %[[C1]], %[[VAL_5]]#1 : (index, index) -> !fir.shapeshift<1>
3839
! CHECK: %[[VAL_7:.*]] = arith.constant 1 : index
3940
! CHECK: fir.do_loop %[[VAL_8:.*]] = %[[VAL_7]] to %[[VAL_5]]#1 step %[[VAL_7]] unordered {
4041
! CHECK: %[[VAL_9:.*]] = fir.array_coor %[[VAL_2]](%[[VAL_6]]) %[[VAL_8]] : (!fir.box<!fir.array<?xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>

0 commit comments

Comments
 (0)