Skip to content

Commit 7cb83e3

Browse files
committed
Readability improvments
1 parent 04e6c51 commit 7cb83e3

File tree

6 files changed

+33
-29
lines changed

6 files changed

+33
-29
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void DataSharingProcessor::cloneSymbol(const semantics::Symbol *sym) {
168168

169169
if (needInitClone()) {
170170
Fortran::lower::initializeCloneAtRuntime(converter, *sym, symTable);
171-
mightHaveReadMoldArg = true;
171+
mightHaveReadHostSym = true;
172172
}
173173
}
174174

@@ -221,7 +221,7 @@ bool DataSharingProcessor::needBarrier() {
221221
for (const semantics::Symbol *sym : allPrivatizedSymbols) {
222222
if (sym->test(semantics::Symbol::Flag::OmpLastPrivate) &&
223223
(sym->test(semantics::Symbol::Flag::OmpFirstPrivate) ||
224-
mightHaveReadMoldArg))
224+
mightHaveReadHostSym))
225225
return true;
226226
}
227227
return false;
@@ -505,17 +505,15 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
505505
lower::SymbolBox hsb = converter.lookupOneLevelUpSymbol(*sym);
506506
assert(hsb && "Host symbol box not found");
507507

508-
mlir::Value privVal = hsb.getAddr();
509-
mlir::Type allocType;
510-
if (mlir::isa<fir::PointerType>(privVal.getType()))
511-
allocType = privVal.getType();
512-
else
513-
allocType = fir::unwrapRefType(privVal.getType());
514-
515508
mlir::Location symLoc = hsb.getAddr().getLoc();
516509
std::string privatizerName = sym->name().ToString() + ".privatizer";
517510
bool isFirstPrivate = sym->test(semantics::Symbol::Flag::OmpFirstPrivate);
518511

512+
mlir::Value privVal = hsb.getAddr();
513+
mlir::Type allocType = privVal.getType();
514+
if (!mlir::isa<fir::PointerType>(privVal.getType()))
515+
allocType = fir::unwrapRefType(privVal.getType());
516+
519517
if (auto poly = mlir::dyn_cast<fir::ClassType>(allocType)) {
520518
if (!mlir::isa<fir::PointerType>(poly.getEleTy()) && isFirstPrivate)
521519
TODO(symLoc, "create polymorphic host associated copy");
@@ -566,6 +564,12 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
566564
lower::SymMapScope outerScope(symTable);
567565

568566
// Populate the `init` region.
567+
// We need to initialize in the following cases:
568+
// 1. The allocation was for a derived type which requires initialization
569+
// (this can be skipped if it will be initialized anyway by the copy
570+
// region, unless the derived type has allocatable components)
571+
// 2. The allocation was for any kind of box
572+
// 3. The allocation was for a boxed character
569573
const bool needsInitialization =
570574
(Fortran::lower::hasDefaultInitialization(sym->GetUltimate()) &&
571575
(!isFirstPrivate || hlfir::mayHaveAllocatableComponent(allocType))) ||
@@ -586,7 +590,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
586590
// TODO: currently there are false positives from dead uses of the mold
587591
// arg
588592
if (!result.getInitMoldArg().getUses().empty())
589-
mightHaveReadMoldArg = true;
593+
mightHaveReadHostSym = true;
590594
}
591595

592596
// Populate the `copy` region if this is a `firstprivate`.

flang/lib/Lower/OpenMP/DataSharingProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class DataSharingProcessor {
8686
lower::pft::Evaluation &eval;
8787
bool shouldCollectPreDeterminedSymbols;
8888
bool useDelayedPrivatization;
89-
bool mightHaveReadMoldArg = false;
89+
bool mightHaveReadHostSym = false;
9090
lower::SymMap &symTable;
9191
OMPConstructSymbolVisitor visitor;
9292

flang/lib/Lower/OpenMP/PrivateReductionUtils.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ static void initializeIfDerivedTypeBox(fir::FirOpBuilder &builder,
160160
mlir::Location loc, mlir::Value newBox,
161161
mlir::Value moldBox, bool hasInitializer,
162162
bool isFirstPrivate) {
163+
assert(moldBox.getType() == newBox.getType());
163164
fir::BoxType boxTy = mlir::dyn_cast<fir::BoxType>(newBox.getType());
164165
fir::ClassType classTy = mlir::dyn_cast<fir::ClassType>(newBox.getType());
165166
if (!boxTy && !classTy)
@@ -177,7 +178,6 @@ static void initializeIfDerivedTypeBox(fir::FirOpBuilder &builder,
177178

178179
if (!fir::isa_derived(derivedTy))
179180
return;
180-
assert(moldBox.getType() == newBox.getType());
181181

182182
if (hasInitializer)
183183
fir::runtime::genDerivedTypeInitialize(builder, loc, newBox);
@@ -218,17 +218,16 @@ isDerivedTypeNeedingInitialization(const Fortran::semantics::Symbol &sym) {
218218
if (const Fortran::semantics::DeclTypeSpec *declTypeSpec = sym.GetType())
219219
if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
220220
declTypeSpec->AsDerived())
221-
if (derivedTypeSpec->HasDefaultInitialization(
222-
/*ignoreAllocatable=*/false, /*ignorePointer=*/true))
223-
return true;
221+
return derivedTypeSpec->HasDefaultInitialization(
222+
/*ignoreAllocatable=*/false, /*ignorePointer=*/true);
224223
return false;
225224
}
226225

227226
static mlir::Value generateZeroShapeForRank(fir::FirOpBuilder &builder,
228227
mlir::Location loc,
229228
mlir::Value moldArg) {
230-
mlir::Type moldVal = fir::unwrapRefType(moldArg.getType());
231-
mlir::Type eleType = fir::dyn_cast_ptrOrBoxEleTy(moldVal);
229+
mlir::Type moldType = fir::unwrapRefType(moldArg.getType());
230+
mlir::Type eleType = fir::dyn_cast_ptrOrBoxEleTy(moldType);
232231
fir::SequenceType seqTy =
233232
mlir::dyn_cast_if_present<fir::SequenceType>(eleType);
234233
if (!seqTy)
@@ -324,13 +323,8 @@ void Fortran::lower::omp::populateByRefInitAndCleanupRegions(
324323
// Just incase, do initialize the box with a null value
325324
mlir::Value null = builder.createNullConstant(loc, boxTy.getEleTy());
326325
mlir::Value nullBox;
327-
if (shape)
328-
nullBox = builder.create<fir::EmboxOp>(
329-
loc, boxTy, null, shape, /*slice=*/mlir::Value{}, lenParams);
330-
else
331-
nullBox = builder.create<fir::EmboxOp>(
332-
loc, boxTy, null, /*shape=*/mlir::Value{}, /*slice=*/mlir::Value{},
333-
lenParams);
326+
nullBox = builder.create<fir::EmboxOp>(
327+
loc, boxTy, null, shape, /*slice=*/mlir::Value{}, lenParams);
334328
builder.create<fir::StoreOp>(loc, nullBox, boxAlloca);
335329
yield(boxAlloca);
336330
return;

flang/lib/Lower/OpenMP/PrivateReductionUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ inline bool isReduction(DeclOperationKind kind) {
4848

4949
/// Generate init and cleanup regions suitable for reduction or privatizer
5050
/// declarations. `scalarInitValue` may be nullptr if there is no default
51-
/// initialization (for privatization). If this is for a privatizer, set
52-
/// `isPrivate` to `true`.
51+
/// initialization (for privatization). `kind` should be set to indicate
52+
/// what kind of operation definition this initialization belongs to.
5353
void populateByRefInitAndCleanupRegions(
5454
AbstractConverter &converter, mlir::Location loc, mlir::Type argType,
5555
mlir::Value scalarInitValue, mlir::Block *initBlock,

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class MapsForPrivatizedSymbolsPass
6565
// decalred needs a descriptor.
6666
// Some types are boxed immediately before privatization. These have other
6767
// operations in between the privatization and the declaration. It is safe
68-
// to use var directly here because they will be boxed anyay.
68+
// to use var directly here because they will be boxed anyway.
6969
if (auto declOp = llvm::dyn_cast<hlfir::DeclareOp>(definingOp))
7070
varPtr = declOp.getBase();
7171

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,17 @@ def PrivateClauseOp : OpenMP_Op<"private", [IsolatedFromAbove, RecipeInterface]>
6464
```mlir
6565
omp.private {type = private} @x.privatizer : !some.type init {
6666
^bb0(%arg0: !some.pointer<!some.type>, %arg1: !some.pointer<!some.type>):
67-
// initialize %arg1, using %arg0 as a mold for allocations
67+
// initialize %arg1, using %arg0 as a mold for allocations.
68+
// For example if %arg0 is a heap allocated array with a runtime determined
69+
// length and !some.type is a runtime type descriptor, the init region
70+
// will read the array length from %arg0, and heap allocate an array of the
71+
// right length and initialize %arg1 to contain the array allocation and
72+
// length.
6873
omp.yield(%arg1 : !some.pointer<!some.type>)
6974
} dealloc {
7075
^bb0(%arg0: !some.pointer<!some.type>):
71-
... deallocate allocated memory ...
76+
// ... deallocate memory allocated by the init region...
77+
// In the example above, this will free the heap allocated array data.
7278
omp.yield
7379
}
7480
```

0 commit comments

Comments
 (0)