Skip to content

Commit 32ab6ff

Browse files
authored
[NFC][Flang] Move bounds helper functions to Util header. (#154164)
This PR moves the `needsBoundsOps` and `genBoundsOps` helper functions to `flang/include/flang/Optimizer/OpenMP/Utils.h`.
1 parent 959c3b6 commit 32ab6ff

File tree

4 files changed

+51
-73
lines changed

4 files changed

+51
-73
lines changed

flang/include/flang/Optimizer/OpenMP/Utils.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
#ifndef FORTRAN_OPTIMIZER_OPENMP_UTILS_H
1414
#define FORTRAN_OPTIMIZER_OPENMP_UTILS_H
1515

16+
#include "flang/Optimizer/Builder/BoxValue.h"
17+
#include "flang/Optimizer/Builder/DirectivesCommon.h"
18+
#include "flang/Optimizer/Builder/FIRBuilder.h"
19+
#include "flang/Optimizer/Builder/HLFIRTools.h"
20+
#include "flang/Optimizer/Dialect/FIRType.h"
21+
22+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
23+
#include "mlir/IR/Value.h"
24+
25+
#include "llvm/ADT/SmallVector.h"
26+
1627
namespace flangomp {
1728

1829
enum class DoConcurrentMappingKind {
@@ -21,6 +32,35 @@ enum class DoConcurrentMappingKind {
2132
DCMK_Device ///< Lower to run in parallel on the GPU.
2233
};
2334

35+
/// Return true if the variable has a dynamic size and therefore requires
36+
/// bounds operations to describe its extents.
37+
inline bool needsBoundsOps(mlir::Value var) {
38+
assert(mlir::isa<mlir::omp::PointerLikeType>(var.getType()) &&
39+
"needsBoundsOps can deal only with pointer types");
40+
mlir::Type t = fir::unwrapRefType(var.getType());
41+
if (mlir::Type inner = fir::dyn_cast_ptrOrBoxEleTy(t))
42+
return fir::hasDynamicSize(inner);
43+
return fir::hasDynamicSize(t);
44+
}
45+
46+
/// Generate MapBoundsOp operations for the variable and append them to
47+
/// `boundsOps`.
48+
inline llvm::SmallVector<mlir::Value> genBoundsOps(fir::FirOpBuilder &builder,
49+
mlir::Value var,
50+
bool isAssumedSize = false,
51+
bool isOptional = false) {
52+
mlir::Location loc = var.getLoc();
53+
fir::factory::AddrAndBoundsInfo info =
54+
fir::factory::getDataOperandBaseAddr(builder, var, isOptional, loc);
55+
fir::ExtendedValue exv =
56+
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
57+
/*contiguousHint=*/true)
58+
.first;
59+
return fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
60+
mlir::omp::MapBoundsType>(
61+
builder, info, exv, isAssumedSize, loc);
62+
}
63+
2464
} // namespace flangomp
2565

2666
#endif // FORTRAN_OPTIMIZER_OPENMP_UTILS_H

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "flang/Optimizer/Builder/Todo.h"
3131
#include "flang/Optimizer/Dialect/FIRType.h"
3232
#include "flang/Optimizer/HLFIR/HLFIROps.h"
33+
#include "flang/Optimizer/OpenMP/Utils.h"
3334
#include "flang/Parser/characters.h"
3435
#include "flang/Parser/openmp-utils.h"
3536
#include "flang/Parser/parse-tree.h"
@@ -2496,12 +2497,10 @@ genTargetOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
24962497
Fortran::lower::getDataOperandBaseAddr(
24972498
converter, firOpBuilder, sym.GetUltimate(),
24982499
converter.getCurrentLocation());
2499-
llvm::SmallVector<mlir::Value> bounds =
2500-
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
2501-
mlir::omp::MapBoundsType>(
2502-
firOpBuilder, info, dataExv,
2503-
semantics::IsAssumedSizeArray(sym.GetUltimate()),
2504-
converter.getCurrentLocation());
2500+
llvm::SmallVector<mlir::Value> bounds = flangomp::genBoundsOps(
2501+
firOpBuilder, info.rawInput,
2502+
semantics::IsAssumedSizeArray(sym.GetUltimate()),
2503+
semantics::IsOptional(sym.GetUltimate()));
25052504
mlir::Value baseOp = info.rawInput;
25062505
mlir::Type eleType = baseOp.getType();
25072506
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(baseOp.getType()))

flang/lib/Optimizer/OpenMP/AutomapToTargetData.cpp

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "flang/Optimizer/Dialect/FIRType.h"
1414
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
1515
#include "flang/Optimizer/HLFIR/HLFIROps.h"
16+
#include "flang/Optimizer/OpenMP/Utils.h"
1617

1718
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1819
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
@@ -33,36 +34,6 @@ namespace {
3334
class AutomapToTargetDataPass
3435
: public flangomp::impl::AutomapToTargetDataPassBase<
3536
AutomapToTargetDataPass> {
36-
37-
// Returns true if the variable has a dynamic size and therefore requires
38-
// bounds operations to describe its extents.
39-
inline bool needsBoundsOps(mlir::Value var) {
40-
assert(mlir::isa<mlir::omp::PointerLikeType>(var.getType()) &&
41-
"only pointer like types expected");
42-
mlir::Type t = fir::unwrapRefType(var.getType());
43-
if (mlir::Type inner = fir::dyn_cast_ptrOrBoxEleTy(t))
44-
return fir::hasDynamicSize(inner);
45-
return fir::hasDynamicSize(t);
46-
}
47-
48-
// Generate MapBoundsOp operations for the variable if required.
49-
inline void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
50-
llvm::SmallVectorImpl<mlir::Value> &boundsOps) {
51-
mlir::Location loc = var.getLoc();
52-
fir::factory::AddrAndBoundsInfo info =
53-
fir::factory::getDataOperandBaseAddr(builder, var,
54-
/*isOptional=*/false, loc);
55-
fir::ExtendedValue exv =
56-
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
57-
/*contiguousHint=*/true)
58-
.first;
59-
llvm::SmallVector<mlir::Value> tmp =
60-
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
61-
mlir::omp::MapBoundsType>(
62-
builder, info, exv, /*dataExvIsAssumedSize=*/false, loc);
63-
llvm::append_range(boundsOps, tmp);
64-
}
65-
6637
void findRelatedAllocmemFreemem(fir::AddrOfOp addressOfOp,
6738
llvm::DenseSet<fir::StoreOp> &allocmems,
6839
llvm::DenseSet<fir::LoadOp> &freemems) {
@@ -112,8 +83,8 @@ class AutomapToTargetDataPass
11283
auto addMapInfo = [&](auto globalOp, auto memOp) {
11384
builder.setInsertionPointAfter(memOp);
11485
SmallVector<Value> bounds;
115-
if (needsBoundsOps(memOp.getMemref()))
116-
genBoundsOps(builder, memOp.getMemref(), bounds);
86+
if (flangomp::needsBoundsOps(memOp.getMemref()))
87+
bounds = flangomp::genBoundsOps(builder, memOp.getMemref());
11788

11889
omp::TargetEnterExitUpdateDataOperands clauses;
11990
mlir::omp::MapInfoOp mapInfo = mlir::omp::MapInfoOp::create(

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
3030
#include "flang/Optimizer/HLFIR/HLFIROps.h"
3131
#include "flang/Optimizer/OpenMP/Passes.h"
32+
#include "flang/Optimizer/OpenMP/Utils.h"
3233

3334
#include "mlir/Dialect/Func/IR/FuncOps.h"
3435
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
@@ -106,8 +107,8 @@ class MapsForPrivatizedSymbolsPass
106107
// Figure out the bounds because knowing the bounds will help the subsequent
107108
// MapInfoFinalizationPass map the underlying data of the descriptor.
108109
llvm::SmallVector<mlir::Value> boundsOps;
109-
if (needsBoundsOps(varPtr))
110-
genBoundsOps(builder, varPtr, boundsOps);
110+
if (flangomp::needsBoundsOps(varPtr))
111+
boundsOps = flangomp::genBoundsOps(builder, varPtr);
111112

112113
mlir::omp::VariableCaptureKind captureKind =
113114
mlir::omp::VariableCaptureKind::ByRef;
@@ -194,38 +195,5 @@ class MapsForPrivatizedSymbolsPass
194195
}
195196
}
196197
}
197-
// As the name suggests, this function examines var to determine if
198-
// it has dynamic size. If true, this pass'll have to extract these
199-
// bounds from descriptor of var and add the bounds to the resultant
200-
// MapInfoOp.
201-
bool needsBoundsOps(mlir::Value var) {
202-
assert(mlir::isa<omp::PointerLikeType>(var.getType()) &&
203-
"needsBoundsOps can deal only with pointer types");
204-
mlir::Type t = fir::unwrapRefType(var.getType());
205-
// t could be a box, so look inside the box
206-
auto innerType = fir::dyn_cast_ptrOrBoxEleTy(t);
207-
if (innerType)
208-
return fir::hasDynamicSize(innerType);
209-
return fir::hasDynamicSize(t);
210-
}
211-
212-
void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
213-
llvm::SmallVector<mlir::Value> &boundsOps) {
214-
mlir::Location loc = var.getLoc();
215-
fir::factory::AddrAndBoundsInfo info =
216-
fir::factory::getDataOperandBaseAddr(builder, var,
217-
/*isOptional=*/false, loc);
218-
fir::ExtendedValue extendedValue =
219-
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
220-
/*continguousHint=*/true)
221-
.first;
222-
llvm::SmallVector<mlir::Value> boundsOpsVec =
223-
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
224-
mlir::omp::MapBoundsType>(
225-
builder, info, extendedValue,
226-
/*dataExvIsAssumedSize=*/false, loc);
227-
for (auto bounds : boundsOpsVec)
228-
boundsOps.push_back(bounds);
229-
}
230198
};
231199
} // namespace

0 commit comments

Comments
 (0)