Skip to content

Commit 610ded4

Browse files
committed
move other related utils
1 parent 429380d commit 610ded4

File tree

8 files changed

+132
-176
lines changed

8 files changed

+132
-176
lines changed

flang/include/flang/Support/OpenMP-utils.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,6 @@ struct EntryBlockArgs {
7878
mlir::Block *genEntryBlock(
7979
mlir::OpBuilder &builder, const EntryBlockArgs &args, mlir::Region &region);
8080

81-
mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
82-
mlir::Location loc, mlir::Value baseAddr, mlir::Value varPtrPtr,
83-
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
84-
llvm::ArrayRef<mlir::Value> members, mlir::ArrayAttr membersIndex,
85-
uint64_t mapType, mlir::omp::VariableCaptureKind mapCaptureType,
86-
mlir::Type retTy, bool partialMap = false,
87-
mlir::FlatSymbolRefAttr mapperId = mlir::FlatSymbolRefAttr());
88-
89-
mlir::Value mapTemporaryValue(fir::FirOpBuilder &firOpBuilder,
90-
mlir::omp::TargetOp targetOp, mlir::Value val, llvm::StringRef name);
91-
92-
void cloneOrMapRegionOutsiders(
93-
fir::FirOpBuilder &firOpBuilder, mlir::omp::TargetOp targetOp);
94-
9581
// Returns true if the variable has a dynamic size and therefore requires
9682
// bounds operations to describe its extents.
9783
inline bool needsBoundsOps(mlir::Value var) {

flang/include/flang/Utils/OpenMP.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1313

14+
namespace fir {
15+
class FirOpBuilder;
16+
} // namespace fir
17+
1418
namespace Fortran::utils::openmp {
1519
// TODO We can probably move the stuff inside `Support/OpenMP-utils.h/.cpp` here
1620
// as well.
@@ -28,6 +32,12 @@ mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
2832
uint64_t mapType, mlir::omp::VariableCaptureKind mapCaptureType,
2933
mlir::Type retTy, bool partialMap = false,
3034
mlir::FlatSymbolRefAttr mapperId = mlir::FlatSymbolRefAttr());
35+
36+
mlir::Value mapTemporaryValue(fir::FirOpBuilder &firOpBuilder,
37+
mlir::omp::TargetOp targetOp, mlir::Value val, llvm::StringRef name);
38+
39+
void cloneOrMapRegionOutsiders(
40+
fir::FirOpBuilder &firOpBuilder, mlir::omp::TargetOp targetOp);
3141
} // namespace Fortran::utils::openmp
3242

3343
#endif // FORTRAN_UTILS_OPENMP_H_

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,7 @@ static void genBodyOfTargetOp(
14341434
// If so, then either clone them as well if they are MemoryEffectFree, or else
14351435
// copy them to a new temporary and add them to the map and block_argument
14361436
// lists and replace their uses with the new temporary.
1437-
Fortran::common::openmp::cloneOrMapRegionOutsiders(firOpBuilder, targetOp);
1437+
cloneOrMapRegionOutsiders(firOpBuilder, targetOp);
14381438

14391439
// Insert dummy instruction to remember the insertion position. The
14401440
// marker will be deleted since there are not uses.

flang/lib/Optimizer/OpenMP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_flang_library(FlangOpenMPTransforms
2727
FIRSupport
2828
FortranSupport
2929
HLFIRDialect
30+
FortranUtils
3031

3132
MLIR_DEPS
3233
${dialect_libs}

flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "flang/Optimizer/OpenMP/Passes.h"
1919
#include "flang/Optimizer/OpenMP/Utils.h"
2020
#include "flang/Support/OpenMP-utils.h"
21+
#include "flang/Utils/OpenMP.h"
2122
#include "mlir/Analysis/SliceAnalysis.h"
2223
#include "mlir/Dialect/Func/IR/FuncOps.h"
2324
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
@@ -631,7 +632,7 @@ class DoConcurrentConversion
631632
llvm::SmallVector<mlir::Value> boundsOps;
632633
genBoundsOps(builder, liveIn, rawAddr, boundsOps);
633634

634-
return Fortran::common::openmp::createMapInfoOp(
635+
return Fortran::utils::openmp::createMapInfoOp(
635636
builder, liveIn.getLoc(), rawAddr,
636637
/*varPtrPtr=*/{}, name.str(), boundsOps,
637638
/*members=*/{},
@@ -720,7 +721,7 @@ class DoConcurrentConversion
720721
// MemoryEffectFree, or else copy them to a new temporary and add them to
721722
// the map and block_argument lists and replace their uses with the new
722723
// temporary.
723-
Fortran::common::openmp::cloneOrMapRegionOutsiders(builder, targetOp);
724+
Fortran::utils::openmp::cloneOrMapRegionOutsiders(builder, targetOp);
724725
rewriter.setInsertionPoint(
725726
rewriter.create<mlir::omp::TerminatorOp>(targetOp.getLoc()));
726727

@@ -753,11 +754,11 @@ class DoConcurrentConversion
753754
llvm::zip_equal(targetShapeCreationInfo.startIndices,
754755
targetShapeCreationInfo.extents)) {
755756
shapeShiftOperands.push_back(
756-
Fortran::common::openmp::mapTemporaryValue(
757+
Fortran::utils::openmp::mapTemporaryValue(
757758
builder, targetOp, startIndex,
758759
liveInName + ".start_idx.dim" + std::to_string(shapeIdx)));
759760
shapeShiftOperands.push_back(
760-
Fortran::common::openmp::mapTemporaryValue(
761+
Fortran::utils::openmp::mapTemporaryValue(
761762
builder, targetOp, extent,
762763
liveInName + ".extent.dim" + std::to_string(shapeIdx)));
763764
++shapeIdx;
@@ -772,7 +773,7 @@ class DoConcurrentConversion
772773
llvm::SmallVector<mlir::Value> shapeOperands;
773774
size_t shapeIdx = 0;
774775
for (auto extent : targetShapeCreationInfo.extents) {
775-
shapeOperands.push_back(Fortran::common::openmp::mapTemporaryValue(
776+
shapeOperands.push_back(Fortran::utils::openmp::mapTemporaryValue(
776777
builder, targetOp, extent,
777778
liveInName + ".extent.dim" + std::to_string(shapeIdx)));
778779
++shapeIdx;

flang/lib/Support/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,10 @@ add_flang_library(FortranSupport
5454
Version.cpp
5555
${version_inc}
5656

57-
DEPENDS
58-
FIRDialect
59-
60-
LINK_LIBS
61-
FIRDialect
62-
6357
LINK_COMPONENTS
6458
Support
6559

6660
MLIR_LIBS
6761
MLIRIR
6862
MLIRSupport
69-
MLIROpenMPDialect
7063
)

flang/lib/Support/OpenMP-utils.cpp

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "flang/Support/OpenMP-utils.h"
10-
#include "flang/Optimizer/Builder/DirectivesCommon.h"
11-
#include "flang/Optimizer/Builder/FIRBuilder.h"
12-
#include "flang/Optimizer/Builder/HLFIRTools.h"
1310

14-
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1511
#include "mlir/IR/OpDefinition.h"
16-
#include "mlir/Transforms/RegionUtils.h"
17-
18-
#include "llvm/Frontend/OpenMP/OMPConstants.h"
1912

2013
namespace Fortran::common::openmp {
2114
mlir::Block *genEntryBlock(mlir::OpBuilder &builder, const EntryBlockArgs &args,
@@ -54,146 +47,4 @@ mlir::Block *genEntryBlock(mlir::OpBuilder &builder, const EntryBlockArgs &args,
5447

5548
return builder.createBlock(&region, {}, types, locs);
5649
}
57-
58-
mlir::omp::MapInfoOp createMapInfoOp(mlir::OpBuilder &builder,
59-
mlir::Location loc, mlir::Value baseAddr, mlir::Value varPtrPtr,
60-
llvm::StringRef name, llvm::ArrayRef<mlir::Value> bounds,
61-
llvm::ArrayRef<mlir::Value> members, mlir::ArrayAttr membersIndex,
62-
uint64_t mapType, mlir::omp::VariableCaptureKind mapCaptureType,
63-
mlir::Type retTy, bool partialMap, mlir::FlatSymbolRefAttr mapperId) {
64-
65-
if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(baseAddr.getType())) {
66-
baseAddr = fir::BoxAddrOp::create(builder, loc, baseAddr);
67-
retTy = baseAddr.getType();
68-
}
69-
70-
mlir::TypeAttr varType = mlir::TypeAttr::get(
71-
llvm::cast<mlir::omp::PointerLikeType>(retTy).getElementType());
72-
73-
// For types with unknown extents such as <2x?xi32> we discard the incomplete
74-
// type info and only retain the base type. The correct dimensions are later
75-
// recovered through the bounds info.
76-
if (auto seqType = llvm::dyn_cast<fir::SequenceType>(varType.getValue()))
77-
if (seqType.hasDynamicExtents())
78-
varType = mlir::TypeAttr::get(seqType.getEleTy());
79-
80-
mlir::omp::MapInfoOp op =
81-
mlir::omp::MapInfoOp::create(builder, loc, retTy, baseAddr, varType,
82-
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
83-
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(mapCaptureType),
84-
varPtrPtr, members, membersIndex, bounds, mapperId,
85-
builder.getStringAttr(name), builder.getBoolAttr(partialMap));
86-
return op;
87-
}
88-
89-
mlir::Value mapTemporaryValue(fir::FirOpBuilder &firOpBuilder,
90-
mlir::omp::TargetOp targetOp, mlir::Value val, llvm::StringRef name) {
91-
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
92-
mlir::Operation *valOp = val.getDefiningOp();
93-
94-
if (valOp)
95-
firOpBuilder.setInsertionPointAfter(valOp);
96-
else
97-
// This means val is a block argument
98-
firOpBuilder.setInsertionPoint(targetOp);
99-
100-
auto copyVal = firOpBuilder.createTemporary(val.getLoc(), val.getType());
101-
firOpBuilder.createStoreWithConvert(copyVal.getLoc(), val, copyVal);
102-
103-
fir::factory::AddrAndBoundsInfo info = fir::factory::getDataOperandBaseAddr(
104-
firOpBuilder, val, /*isOptional=*/false, val.getLoc());
105-
llvm::SmallVector<mlir::Value> bounds =
106-
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
107-
mlir::omp::MapBoundsType>(firOpBuilder, info,
108-
hlfir::translateToExtendedValue(
109-
val.getLoc(), firOpBuilder, hlfir::Entity{val})
110-
.first,
111-
/*dataExvIsAssumedSize=*/false, val.getLoc());
112-
113-
firOpBuilder.setInsertionPoint(targetOp);
114-
115-
llvm::omp::OpenMPOffloadMappingFlags mapFlag =
116-
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT;
117-
mlir::omp::VariableCaptureKind captureKind =
118-
mlir::omp::VariableCaptureKind::ByRef;
119-
120-
mlir::Type eleType = copyVal.getType();
121-
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(copyVal.getType())) {
122-
eleType = refType.getElementType();
123-
}
124-
125-
if (fir::isa_trivial(eleType) || fir::isa_char(eleType)) {
126-
captureKind = mlir::omp::VariableCaptureKind::ByCopy;
127-
} else if (!fir::isa_builtin_cptr_type(eleType)) {
128-
mapFlag |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO;
129-
}
130-
131-
mlir::Value mapOp = Fortran::common::openmp::createMapInfoOp(firOpBuilder,
132-
copyVal.getLoc(), copyVal,
133-
/*varPtrPtr=*/mlir::Value{}, name.str(), bounds,
134-
/*members=*/llvm::SmallVector<mlir::Value>{},
135-
/*membersIndex=*/mlir::ArrayAttr{},
136-
static_cast<std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
137-
mapFlag),
138-
captureKind, copyVal.getType());
139-
140-
auto argIface = llvm::cast<mlir::omp::BlockArgOpenMPOpInterface>(*targetOp);
141-
mlir::Region &region = targetOp.getRegion();
142-
143-
// Get the index of the first non-map argument before modifying mapVars,
144-
// then append an element to mapVars and an associated entry block
145-
// argument at that index.
146-
unsigned insertIndex =
147-
argIface.getMapBlockArgsStart() + argIface.numMapBlockArgs();
148-
targetOp.getMapVarsMutable().append(mapOp);
149-
mlir::Value clonedValArg =
150-
region.insertArgument(insertIndex, copyVal.getType(), copyVal.getLoc());
151-
152-
mlir::Block *entryBlock = &region.getBlocks().front();
153-
firOpBuilder.setInsertionPointToStart(entryBlock);
154-
auto loadOp =
155-
firOpBuilder.create<fir::LoadOp>(clonedValArg.getLoc(), clonedValArg);
156-
return loadOp.getResult();
157-
}
158-
159-
void cloneOrMapRegionOutsiders(fir::FirOpBuilder &firOpBuilder,
160-
mlir::omp::TargetOp targetOp) {
161-
mlir::Region &region = targetOp.getRegion();
162-
mlir::Block *entryBlock = &region.getBlocks().front();
163-
164-
llvm::SetVector<mlir::Value> valuesDefinedAbove;
165-
mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove);
166-
while (!valuesDefinedAbove.empty()) {
167-
for (mlir::Value val : valuesDefinedAbove) {
168-
mlir::Operation *valOp = val.getDefiningOp();
169-
170-
// NOTE: We skip BoxDimsOp's as the lesser of two evils is to map the
171-
// indices separately, as the alternative is to eventually map the Box,
172-
// which comes with a fairly large overhead comparatively. We could be
173-
// more robust about this and check using a BackwardsSlice to see if we
174-
// run the risk of mapping a box.
175-
if (valOp && mlir::isMemoryEffectFree(valOp) &&
176-
!mlir::isa<fir::BoxDimsOp>(valOp)) {
177-
mlir::Operation *clonedOp = valOp->clone();
178-
entryBlock->push_front(clonedOp);
179-
180-
auto replace = [entryBlock](mlir::OpOperand &use) {
181-
return use.getOwner()->getBlock() == entryBlock;
182-
};
183-
184-
valOp->getResults().replaceUsesWithIf(clonedOp->getResults(), replace);
185-
valOp->replaceUsesWithIf(clonedOp, replace);
186-
} else {
187-
mlir::Value mappedTemp = Fortran::common::openmp::mapTemporaryValue(
188-
firOpBuilder, targetOp, val,
189-
/*name=*/{});
190-
val.replaceUsesWithIf(mappedTemp, [entryBlock](mlir::OpOperand &use) {
191-
return use.getOwner()->getBlock() == entryBlock;
192-
});
193-
}
194-
}
195-
valuesDefinedAbove.clear();
196-
mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove);
197-
}
198-
}
19950
} // namespace Fortran::common::openmp

0 commit comments

Comments
 (0)