Skip to content

Commit 21f480f

Browse files
committed
More revert [MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (llvm#153048)
1 parent c241804 commit 21f480f

File tree

6 files changed

+198
-39
lines changed

6 files changed

+198
-39
lines changed

flang/include/flang/Optimizer/OpenMP/Passes.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,15 @@ def GenericLoopConversionPass
129129
];
130130
}
131131

132+
def AutomapToTargetDataPass
133+
: Pass<"omp-automap-to-target-data", "::mlir::ModuleOp"> {
134+
let summary = "Insert OpenMP target data operations for AUTOMAP variables";
135+
let description = [{
136+
Inserts `omp.target_enter_data` and `omp.target_exit_data` operations to
137+
map variables marked with the `AUTOMAP` modifier when their allocation
138+
or deallocation is detected in the FIR.
139+
}];
140+
let dependentDialects = ["mlir::omp::OpenMPDialect"];
141+
}
142+
132143
#endif //FORTRAN_OPTIMIZER_OPENMP_PASSES

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
#ifndef FORTRAN_SUPPORT_OPENMP_UTILS_H_
1010
#define FORTRAN_SUPPORT_OPENMP_UTILS_H_
1111

12+
#include "flang/Optimizer/Builder/DirectivesCommon.h"
13+
#include "flang/Optimizer/Builder/FIRBuilder.h"
14+
#include "flang/Optimizer/Builder/HLFIRTools.h"
15+
#include "flang/Optimizer/Dialect/FIRType.h"
1216
#include "flang/Semantics/symbol.h"
1317

18+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1419
#include "mlir/IR/Builders.h"
1520
#include "mlir/IR/Value.h"
1621

@@ -72,6 +77,49 @@ struct EntryBlockArgs {
7277
/// \param [in] region - Empty region in which to create the entry block.
7378
mlir::Block *genEntryBlock(
7479
mlir::OpBuilder &builder, const EntryBlockArgs &args, mlir::Region &region);
80+
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+
95+
// Returns true if the variable has a dynamic size and therefore requires
96+
// bounds operations to describe its extents.
97+
inline bool needsBoundsOps(mlir::Value var) {
98+
assert(mlir::isa<mlir::omp::PointerLikeType>(var.getType()) &&
99+
"only pointer like types expected");
100+
mlir::Type t = fir::unwrapRefType(var.getType());
101+
if (mlir::Type inner = fir::dyn_cast_ptrOrBoxEleTy(t))
102+
return fir::hasDynamicSize(inner);
103+
return fir::hasDynamicSize(t);
104+
}
105+
106+
// Generate MapBoundsOp operations for the variable if required.
107+
inline void genBoundsOps(fir::FirOpBuilder &builder, mlir::Value var,
108+
llvm::SmallVectorImpl<mlir::Value> &boundsOps) {
109+
mlir::Location loc = var.getLoc();
110+
fir::factory::AddrAndBoundsInfo info =
111+
fir::factory::getDataOperandBaseAddr(builder, var,
112+
/*isOptional=*/false, loc);
113+
fir::ExtendedValue exv =
114+
hlfir::translateToExtendedValue(loc, builder, hlfir::Entity{info.addr},
115+
/*contiguousHint=*/true)
116+
.first;
117+
llvm::SmallVector<mlir::Value> tmp =
118+
fir::factory::genImplicitBoundsOps<mlir::omp::MapBoundsOp,
119+
mlir::omp::MapBoundsType>(
120+
builder, info, exv, /*dataExvIsAssumedSize=*/false, loc);
121+
llvm::append_range(boundsOps, tmp);
122+
}
75123
} // namespace Fortran::common::openmp
76124

77125
#endif // FORTRAN_SUPPORT_OPENMP_UTILS_H_
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//===- AutomapToTargetData.cpp -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Optimizer/Builder/FIRBuilder.h"
10+
#include "flang/Optimizer/Builder/HLFIRTools.h"
11+
#include "flang/Optimizer/Dialect/FIROps.h"
12+
#include "flang/Optimizer/Dialect/FIRType.h"
13+
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
14+
#include "flang/Optimizer/HLFIR/HLFIROps.h"
15+
#include "flang/Support/OpenMP-utils.h"
16+
17+
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
18+
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
19+
#include "mlir/IR/BuiltinAttributes.h"
20+
#include "mlir/IR/Operation.h"
21+
#include "mlir/Pass/Pass.h"
22+
23+
#include "llvm/Frontend/OpenMP/OMPConstants.h"
24+
25+
namespace flangomp {
26+
#define GEN_PASS_DEF_AUTOMAPTOTARGETDATAPASS
27+
#include "flang/Optimizer/OpenMP/Passes.h.inc"
28+
} // namespace flangomp
29+
30+
using namespace mlir;
31+
using namespace Fortran::common::openmp;
32+
33+
namespace {
34+
class AutomapToTargetDataPass
35+
: public flangomp::impl::AutomapToTargetDataPassBase<
36+
AutomapToTargetDataPass> {
37+
void findRelatedAllocmemFreemem(fir::AddrOfOp addressOfOp,
38+
llvm::DenseSet<fir::StoreOp> &allocmems,
39+
llvm::DenseSet<fir::LoadOp> &freemems) {
40+
assert(addressOfOp->hasOneUse() && "op must have single use");
41+
42+
auto declaredRef =
43+
cast<hlfir::DeclareOp>(*addressOfOp->getUsers().begin())->getResult(0);
44+
45+
for (Operation *refUser : declaredRef.getUsers()) {
46+
if (auto storeOp = dyn_cast<fir::StoreOp>(refUser))
47+
if (auto emboxOp = storeOp.getValue().getDefiningOp<fir::EmboxOp>())
48+
if (auto allocmemOp =
49+
emboxOp.getOperand(0).getDefiningOp<fir::AllocMemOp>())
50+
allocmems.insert(storeOp);
51+
52+
if (auto loadOp = dyn_cast<fir::LoadOp>(refUser))
53+
for (Operation *loadUser : loadOp.getResult().getUsers())
54+
if (auto boxAddrOp = dyn_cast<fir::BoxAddrOp>(loadUser))
55+
for (Operation *boxAddrUser : boxAddrOp.getResult().getUsers())
56+
if (auto freememOp = dyn_cast<fir::FreeMemOp>(boxAddrUser))
57+
freemems.insert(loadOp);
58+
}
59+
}
60+
61+
void runOnOperation() override {
62+
ModuleOp module = getOperation()->getParentOfType<ModuleOp>();
63+
if (!module)
64+
module = dyn_cast<ModuleOp>(getOperation());
65+
if (!module)
66+
return;
67+
68+
// Build FIR builder for helper utilities.
69+
fir::KindMapping kindMap = fir::getKindMapping(module);
70+
fir::FirOpBuilder builder{module, std::move(kindMap)};
71+
72+
// Collect global variables with AUTOMAP flag.
73+
llvm::DenseSet<fir::GlobalOp> automapGlobals;
74+
module.walk([&](fir::GlobalOp globalOp) {
75+
if (auto iface =
76+
dyn_cast<omp::DeclareTargetInterface>(globalOp.getOperation()))
77+
if (iface.isDeclareTarget() && iface.getDeclareTargetAutomap() &&
78+
iface.getDeclareTargetDeviceType() !=
79+
omp::DeclareTargetDeviceType::host)
80+
automapGlobals.insert(globalOp);
81+
});
82+
83+
auto addMapInfo = [&](auto globalOp, auto memOp) {
84+
builder.setInsertionPointAfter(memOp);
85+
SmallVector<Value> bounds;
86+
if (needsBoundsOps(memOp.getMemref()))
87+
genBoundsOps(builder, memOp.getMemref(), bounds);
88+
89+
omp::TargetEnterExitUpdateDataOperands clauses;
90+
mlir::omp::MapInfoOp mapInfo = mlir::omp::MapInfoOp::create(
91+
builder, memOp.getLoc(), memOp.getMemref().getType(),
92+
memOp.getMemref(),
93+
TypeAttr::get(fir::unwrapRefType(memOp.getMemref().getType())),
94+
builder.getIntegerAttr(
95+
builder.getIntegerType(64, false),
96+
static_cast<unsigned>(
97+
isa<fir::StoreOp>(memOp)
98+
? llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO
99+
: llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE)),
100+
builder.getAttr<omp::VariableCaptureKindAttr>(
101+
omp::VariableCaptureKind::ByCopy),
102+
/*var_ptr_ptr=*/mlir::Value{},
103+
/*members=*/SmallVector<Value>{},
104+
/*members_index=*/ArrayAttr{}, bounds,
105+
/*mapperId=*/mlir::FlatSymbolRefAttr(), globalOp.getSymNameAttr(),
106+
builder.getBoolAttr(false));
107+
clauses.mapVars.push_back(mapInfo);
108+
isa<fir::StoreOp>(memOp)
109+
? builder.create<omp::TargetEnterDataOp>(memOp.getLoc(), clauses)
110+
: builder.create<omp::TargetExitDataOp>(memOp.getLoc(), clauses);
111+
};
112+
113+
for (fir::GlobalOp globalOp : automapGlobals) {
114+
if (auto uses = globalOp.getSymbolUses(module.getOperation())) {
115+
llvm::DenseSet<fir::StoreOp> allocmemStores;
116+
llvm::DenseSet<fir::LoadOp> freememLoads;
117+
for (auto &x : *uses)
118+
if (auto addrOp = dyn_cast<fir::AddrOfOp>(x.getUser()))
119+
findRelatedAllocmemFreemem(addrOp, allocmemStores, freememLoads);
120+
121+
for (auto storeOp : allocmemStores)
122+
addMapInfo(globalOp, storeOp);
123+
124+
for (auto loadOp : freememLoads)
125+
addMapInfo(globalOp, loadOp);
126+
}
127+
}
128+
}
129+
};
130+
} // namespace

flang/lib/Optimizer/OpenMP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
22

33
add_flang_library(FlangOpenMPTransforms
4+
AutomapToTargetData.cpp
45
DoConcurrentConversion.cpp
56
FunctionFiltering.cpp
67
GenericLoopConversion.cpp

flang/lib/Optimizer/OpenMP/MapsForPrivatizedSymbols.cpp

Lines changed: 2 additions & 33 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/Support/OpenMP-utils.h"
3233

3334
#include "mlir/Dialect/Func/IR/FuncOps.h"
3435
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
@@ -47,6 +48,7 @@ namespace flangomp {
4748
} // namespace flangomp
4849

4950
using namespace mlir;
51+
using namespace Fortran::common::openmp;
5052

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

flang/lib/Optimizer/Passes/Pipelines.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,13 @@ void createOpenMPFIRPassPipeline(mlir::PassManager &pm,
316316
pm.addPass(flangomp::createDoConcurrentConversionPass(
317317
opts.doConcurrentMappingKind == DoConcurrentMappingKind::DCMK_Device));
318318

319-
// The MapsForPrivatizedSymbols pass needs to run before
320-
// MapInfoFinalizationPass because the former creates new
321-
// MapInfoOp instances, typically for descriptors.
322-
// MapInfoFinalizationPass adds MapInfoOp instances for the descriptors
323-
// underlying data which is necessary to access the data on the offload
324-
// target device.
319+
// The MapsForPrivatizedSymbols and AutomapToTargetDataPass pass need to run
320+
// before MapInfoFinalizationPass because they create new MapInfoOp
321+
// instances, typically for descriptors. MapInfoFinalizationPass adds
322+
// MapInfoOp instances for the descriptors underlying data which is necessary
323+
// to access the data on the offload target device.
325324
pm.addPass(flangomp::createMapsForPrivatizedSymbolsPass());
325+
pm.addPass(flangomp::createAutomapToTargetDataPass());
326326
pm.addPass(flangomp::createMapInfoFinalizationPass(opts.deferDescMap));
327327
pm.addPass(flangomp::createMarkDeclareTargetPass());
328328
pm.addPass(flangomp::createGenericLoopConversionPass());

0 commit comments

Comments
 (0)