Skip to content

Commit c0e62dd

Browse files
authored
Apply llvm#127838: [flang][Lower][OpenMP] Don't read moldarg for static sized array (llvm#2918)
2 parents fadd3f9 + 54b801a commit c0e62dd

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ void DataSharingProcessor::privatizeSymbol(
625625
result.getDeallocRegion(),
626626
emitCopyRegion ? omp::DeclOperationKind::FirstPrivate
627627
: omp::DeclOperationKind::Private,
628-
symToPrivatize);
628+
symToPrivatize, cannotHaveNonDefaultLowerBounds);
629629
// TODO: currently there are false positives from dead uses of the mold
630630
// arg
631631
if (result.initReadsFromMold())

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,15 @@ static void createCleanupRegion(Fortran::lower::AbstractConverter &converter,
123123
typeError();
124124
}
125125

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

138137
mlir::Value oneVal;
@@ -142,7 +141,8 @@ Fortran::lower::omp::getShapeShift(fir::FirOpBuilder &builder,
142141
return oneVal;
143142
};
144143

145-
if ((useDefaultLowerBounds) && !sequenceType.hasDynamicExtents()) {
144+
if ((cannotHaveNonDefaultLowerBounds || useDefaultLowerBounds) &&
145+
!sequenceType.hasDynamicExtents()) {
146146
// We don't need fir::BoxDimsOp if all of the extents are statically known
147147
// and we can assume default lower bounds. This helps avoids reads from the
148148
// mold arg.
@@ -273,12 +273,13 @@ class PopulateInitAndCleanupRegionsHelper {
273273
mlir::Type argType, mlir::Value scalarInitValue,
274274
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
275275
mlir::Block *initBlock, mlir::Region &cleanupRegion,
276-
DeclOperationKind kind, const Fortran::semantics::Symbol *sym)
276+
DeclOperationKind kind, const Fortran::semantics::Symbol *sym,
277+
bool cannotHaveLowerBounds)
277278
: converter{converter}, builder{converter.getFirOpBuilder()}, loc{loc},
278279
argType{argType}, scalarInitValue{scalarInitValue},
279280
allocatedPrivVarArg{allocatedPrivVarArg}, moldArg{moldArg},
280281
initBlock{initBlock}, cleanupRegion{cleanupRegion}, kind{kind},
281-
sym{sym} {
282+
sym{sym}, cannotHaveNonDefaultLowerBounds{cannotHaveLowerBounds} {
282283
valType = fir::unwrapRefType(argType);
283284
}
284285

@@ -320,6 +321,10 @@ class PopulateInitAndCleanupRegionsHelper {
320321
/// Any length parameters which have been fetched for the type
321322
mlir::SmallVector<mlir::Value> lenParams;
322323

324+
/// If the source variable being privatized definitely can't have non-default
325+
/// lower bounds then we don't need to generate code to read them.
326+
bool cannotHaveNonDefaultLowerBounds;
327+
323328
void createYield(mlir::Value ret) {
324329
builder.create<mlir::omp::YieldOp>(loc, ret);
325330
}
@@ -457,7 +462,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
457462
// Special case for (possibly allocatable) arrays of polymorphic types
458463
// e.g. !fir.class<!fir.heap<!fir.array<?x!fir.type<>>>>
459464
if (source.isPolymorphic()) {
460-
fir::ShapeShiftOp shape = getShapeShift(builder, loc, source);
465+
fir::ShapeShiftOp shape =
466+
getShapeShift(builder, loc, source, cannotHaveNonDefaultLowerBounds);
461467
mlir::Type arrayType = source.getElementOrSequenceType();
462468
mlir::Value allocatedArray = builder.create<fir::AllocMemOp>(
463469
loc, arrayType, /*typeparams=*/mlir::ValueRange{}, shape.getExtents());
@@ -505,8 +511,8 @@ void PopulateInitAndCleanupRegionsHelper::initAndCleanupBoxedArray(
505511
// Put the temporary inside of a box:
506512
// hlfir::genVariableBox doesn't handle non-default lower bounds
507513
mlir::Value box;
508-
fir::ShapeShiftOp shapeShift =
509-
getShapeShift(builder, loc, getLoadedMoldArg());
514+
fir::ShapeShiftOp shapeShift = getShapeShift(builder, loc, getLoadedMoldArg(),
515+
cannotHaveNonDefaultLowerBounds);
510516
mlir::Type boxType = getLoadedMoldArg().getType();
511517
if (mlir::isa<fir::BaseBoxType>(temp.getType()))
512518
// the box created by the declare form createTempFromMold is missing
@@ -641,10 +647,10 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
641647
mlir::Type argType, mlir::Value scalarInitValue, mlir::Block *initBlock,
642648
mlir::Value allocatedPrivVarArg, mlir::Value moldArg,
643649
mlir::Region &cleanupRegion, DeclOperationKind kind,
644-
const Fortran::semantics::Symbol *sym) {
650+
const Fortran::semantics::Symbol *sym, bool cannotHaveLowerBounds) {
645651
PopulateInitAndCleanupRegionsHelper helper(
646652
converter, loc, argType, scalarInitValue, allocatedPrivVarArg, moldArg,
647-
initBlock, cleanupRegion, kind, sym);
653+
initBlock, cleanupRegion, kind, sym, cannotHaveLowerBounds);
648654
helper.populateByRefInitAndCleanupRegions();
649655

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

flang/lib/Lower/OpenMP/PrivateReductionUtils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ 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);
58+
const Fortran::semantics::Symbol *sym = nullptr,
59+
bool cannotHaveNonDefaultLowerBounds = false);
5960

6061
/// Generate a fir::ShapeShift op describing the provided boxed array.
6162
/// `cannotHaveNonDefaultLowerBounds` should be set if `box` is known to have
@@ -65,6 +66,7 @@ void populateByRefInitAndCleanupRegions(
6566
/// elements without having to adjust each index.
6667
fir::ShapeShiftOp getShapeShift(fir::FirOpBuilder &builder, mlir::Location loc,
6768
mlir::Value box,
69+
bool cannotHaveNonDefaultLowerBounds = false,
6870
bool useDefaultLowerBounds = false);
6971

7072
} // namespace omp

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,10 @@ static void genBoxCombiner(fir::FirOpBuilder &builder, mlir::Location loc,
371371

372372
// Get ShapeShift with default lower bounds. This makes it possible to use
373373
// unmodified LoopNest's indices with ArrayCoorOp.
374-
fir::ShapeShiftOp shapeShift = getShapeShift(builder, loc, lhs,
375-
/*useDefaultLowerBounds=*/true);
374+
fir::ShapeShiftOp shapeShift =
375+
getShapeShift(builder, loc, lhs,
376+
/*cannotHaveNonDefaultLowerBounds=*/false,
377+
/*useDefaultLowerBounds=*/true);
376378

377379
// Iterate over array elements, applying the equivalent scalar reduction:
378380

0 commit comments

Comments
 (0)