Skip to content

Commit 22ef210

Browse files
authored
Revert "[flang][Lower][OpenMP] Don't read moldarg for static sized array" (#127596)
Reverts #125901 Revert until I have fixed bot failures
1 parent d4a0848 commit 22ef210

File tree

4 files changed

+28
-51
lines changed

4 files changed

+28
-51
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,6 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
508508

509509
lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol(*sym);
510510
assert(hsb && "Host symbol box not found");
511-
hlfir::Entity entity{hsb.getAddr()};
512-
bool cannotHaveNonDefaultLowerBounds = !entity.mayHaveNonDefaultLowerBounds();
513511

514512
mlir::Location symLoc = hsb.getAddr().getLoc();
515513
std::string privatizerName = sym->name().ToString() + ".privatizer";
@@ -530,6 +528,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
530528
// an alloca for a fir.array type there. Get around this by boxing all
531529
// arrays.
532530
if (mlir::isa<fir::SequenceType>(allocType)) {
531+
hlfir::Entity entity{hsb.getAddr()};
533532
entity = genVariableBox(symLoc, firOpBuilder, entity);
534533
privVal = entity.getBase();
535534
allocType = privVal.getType();
@@ -591,7 +590,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
591590
result.getDeallocRegion(),
592591
isFirstPrivate ? DeclOperationKind::FirstPrivate
593592
: DeclOperationKind::Private,
594-
sym, cannotHaveNonDefaultLowerBounds);
593+
sym);
595594
// TODO: currently there are false positives from dead uses of the mold
596595
// arg
597596
if (!result.getInitMoldArg().getUses().empty())

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

+20-41
Original file line numberDiff line numberDiff line change
@@ -122,40 +122,25 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
122122
typeError();
123123
}
124124

125-
fir::ShapeShiftOp
126-
Fortran::lower::omp::getShapeShift(fir::FirOpBuilder &builder,
127-
mlir::Location loc, mlir::Value box,
128-
bool cannotHaveNonDefaultLowerBounds) {
125+
fir::ShapeShiftOp Fortran::lower::omp::getShapeShift(fir::FirOpBuilder &builder,
126+
mlir::Location loc,
127+
mlir::Value box) {
129128
fir::SequenceType sequenceType = mlir::cast<fir::SequenceType>(
130129
hlfir::getFortranElementOrSequenceType(box.getType()));
131130
const unsigned rank = sequenceType.getDimension();
132-
133131
llvm::SmallVector<mlir::Value> lbAndExtents;
134132
lbAndExtents.reserve(rank * 2);
135-
mlir::Type idxTy = builder.getIndexType();
136133

137-
if (cannotHaveNonDefaultLowerBounds && !sequenceType.hasDynamicExtents()) {
138-
// We don't need fir::BoxDimsOp if all of the extents are statically known
139-
// and we can assume default lower bounds. This helps avoids reads from the
140-
// mold arg.
141-
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
142-
for (int64_t extent : sequenceType.getShape()) {
143-
assert(extent != sequenceType.getUnknownExtent());
144-
mlir::Value extentVal = builder.createIntegerConstant(loc, idxTy, extent);
145-
lbAndExtents.push_back(one);
146-
lbAndExtents.push_back(extentVal);
147-
}
148-
} else {
149-
for (unsigned i = 0; i < rank; ++i) {
150-
// TODO: ideally we want to hoist box reads out of the critical section.
151-
// We could do this by having box dimensions in block arguments like
152-
// OpenACC does
153-
mlir::Value dim = builder.createIntegerConstant(loc, idxTy, i);
154-
auto dimInfo =
155-
builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy, box, dim);
156-
lbAndExtents.push_back(dimInfo.getLowerBound());
157-
lbAndExtents.push_back(dimInfo.getExtent());
158-
}
134+
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());
159144
}
160145

161146
auto shapeShiftTy = fir::ShapeShiftType::get(builder.getContext(), rank);
@@ -263,13 +248,12 @@ class PopulateInitAndCleanupRegionsHelper {
263248
mlir::Type argType, mlir::Value scalarInitValue,
264249
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
265250
mlir::Block *initBlock, mlir::Region &cleanupRegion,
266-
DeclOperationKind kind, const Fortran::semantics::Symbol *sym,
267-
bool cannotHaveLowerBounds)
251+
DeclOperationKind kind, const Fortran::semantics::Symbol *sym)
268252
: converter{converter}, builder{converter.getFirOpBuilder()}, loc{loc},
269253
argType{argType}, scalarInitValue{scalarInitValue},
270254
allocatedPrivVarArg{allocatedPrivVarArg}, moldArg{moldArg},
271255
initBlock{initBlock}, cleanupRegion{cleanupRegion}, kind{kind},
272-
sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds} {
256+
sym{sym} {
273257
valType = fir::unwrapRefType(argType);
274258
}
275259

@@ -311,10 +295,6 @@ class PopulateInitAndCleanupRegionsHelper {
311295
/// Any length parameters which have been fetched for the type
312296
mlir::SmallVector<mlir::Value> lenParams;
313297

314-
/// If the source variable being privatized definitely can't have non-default
315-
/// lower bounds then we don't need to generate code to read them.
316-
bool cannotHaveNonDefaultLowerBounds;
317-
318298
void createYield(mlir::Value ret) {
319299
builder.create<mlir::omp::YieldOp>(loc, ret);
320300
}
@@ -452,8 +432,7 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
452432
// Special case for (possibly allocatable) arrays of polymorphic types
453433
// e.g. !fir.class<!fir.heap<!fir.array<?x!fir.type<>>>>
454434
if (source.isPolymorphic()) {
455-
fir::ShapeShiftOp shape =
456-
getShapeShift(builder, loc, source, cannotHaveNonDefaultLowerBounds);
435+
fir::ShapeShiftOp shape = getShapeShift(builder, loc, source);
457436
mlir::Type arrayType = source.getElementOrSequenceType();
458437
mlir::Value allocatedArray = builder.create<fir::AllocMemOp>(
459438
loc, arrayType, /*typeparams=*/mlir::ValueRange{}, shape.getExtents());
@@ -492,8 +471,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
492471
// Put the temporary inside of a box:
493472
// hlfir::genVariableBox doesn't handle non-default lower bounds
494473
mlir::Value box;
495-
fir::ShapeShiftOp shapeShift = getShapeShift(builder, loc, getLoadedMoldArg(),
496-
cannotHaveNonDefaultLowerBounds);
474+
fir::ShapeShiftOp shapeShift =
475+
getShapeShift(builder, loc, getLoadedMoldArg());
497476
mlir::Type boxType = getLoadedMoldArg().getType();
498477
if (mlir::isa<fir::BaseBoxType>(temp.getType()))
499478
// the box created by the declare form createTempFromMold is missing
@@ -628,10 +607,10 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
628607
mlir::Type argType, mlir::Value scalarInitValue, mlir::Block *initBlock,
629608
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
630609
mlir::Region &cleanupRegion, DeclOperationKind kind,
631-
const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds) {
610+
const Fortran::semantics::Symbol *sym) {
632611
PopulateInitAndCleanupRegionsHelper helper(
633612
converter, loc, argType, scalarInitValue, allocatedPrivVarArg, moldArg,
634-
initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds);
613+
initBlock, cleanupRegion, kind, sym);
635614
helper.populateByRefInitAndCleanupRegions();
636615

637616
// Often we load moldArg to check something (e.g. length parameters, shape)

flang/lib/Lower/OpenMP/PrivateReductionUtils.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,11 @@ void populateByRefInitAndCleanupRegions(
5555
mlir::Value scalarInitValue, mlir::Block *initBlock,
5656
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
5757
mlir::Region &cleanupRegion, DeclOperationKind kind,
58-
const Fortran::semantics::Symbol *sym = nullptr,
59-
bool cannotHaveNonDefaultLowerBounds = false);
58+
const Fortran::semantics::Symbol *sym = nullptr);
6059

6160
/// Generate a fir::ShapeShift op describing the provided boxed array.
6261
fir::ShapeShiftOp getShapeShift(fir::FirOpBuilder &builder, mlir::Location loc,
63-
mlir::Value box,
64-
bool cannotHaveNonDefaultLowerBounds = false);
62+
mlir::Value box);
6563

6664
} // namespace omp
6765
} // namespace lower

flang/test/Lower/OpenMP/delayed-privatization-array.f90

+4-3
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ program main
108108
! ONE_DIM_DEFAULT_LB-SAME: @[[PRIVATIZER_SYM:.*]] : [[BOX_TYPE:!fir.box<!fir.array<10xi32>>]] init {
109109

110110
! ONE_DIM_DEFAULT_LB-NEXT: ^bb0(%[[PRIV_ARG:.*]]: [[TYPE:!fir.ref<!fir.box<!fir.array<10xi32>>>]], %[[PRIV_BOX_ALLOC:.*]]: [[TYPE]]):
111+
! ONE_DIM_DEFAULT_LB-NEXT: %[[PRIV_ARG_VAL:.*]] = fir.load %[[PRIV_ARG]]
111112
! ONE_DIM_DEFAULT_LB-NEXT: %[[C10:.*]] = arith.constant 10 : index
112113
! ONE_DIM_DEFAULT_LB-NEXT: %[[SHAPE:.*]] = fir.shape %[[C10]]
113114
! ONE_DIM_DEFAULT_LB-NEXT: %[[ARRAY_ALLOC:.*]] = fir.allocmem !fir.array<10xi32>
114115
! ONE_DIM_DEFAULT_LB-NEXT: %[[TRUE:.*]] = arith.constant true
115116
! ONE_DIM_DEFAULT_LB-NEXT: %[[DECL:.*]]:2 = hlfir.declare %[[ARRAY_ALLOC]](%[[SHAPE]])
116-
! ONE_DIM_DEFAULT_LB-NEXT: %[[ONE:.*]] = arith.constant 1 : index
117-
! ONE_DIM_DEFAULT_LB-NEXT: %[[TEN:.*]] = arith.constant 10 : index
118-
! ONE_DIM_DEFAULT_LB-NEXT: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[ONE]], %[[TEN]]
117+
! ONE_DIM_DEFAULT_LB-NEXT: %[[C0_0:.*]] = arith.constant 0
118+
! ONE_DIM_DEFAULT_LB-NEXT: %[[DIMS2:.*]]:3 = fir.box_dims %[[PRIV_ARG_VAL]], %[[C0_0]]
119+
! ONE_DIM_DEFAULT_LB-NEXT: %[[SHAPE_SHIFT:.*]] = fir.shape_shift %[[DIMS2]]#0, %[[DIMS2]]#1
119120
! ONE_DIM_DEFAULT_LB-NEXT: %[[EMBOX:.*]] = fir.embox %[[DECL]]#0(%[[SHAPE_SHIFT]])
120121
! ONE_DIM_DEFAULT_LB-NEXT: fir.store %[[EMBOX]] to %[[PRIV_BOX_ALLOC]]
121122
! ONE_DIM_DEFAULT_LB-NEXT: omp.yield(%[[PRIV_BOX_ALLOC]] : [[TYPE]])

0 commit comments

Comments
 (0)