Skip to content

Commit 7e9b949

Browse files
authored
[flang][OpenMP] Adapt OMPMapInfoFinalization to run on all top level ops (#93545)
This is generally just for consistency with the rest of the pipeline. The assertion for the insertion point is because I am not sure if omp::PrivateClauseOp is supported by FirOpBuilder::getAllocaBlock. I didn't try to fix it because I don't see why we would generate IR like that. See RFC: https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations
1 parent fd8b2d2 commit 7e9b949

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

flang/include/flang/Optimizer/Transforms/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace fir {
5151
#define GEN_PASS_DECL_STACKARRAYS
5252
#define GEN_PASS_DECL_LOOPVERSIONING
5353
#define GEN_PASS_DECL_ADDALIASTAGS
54+
#define GEN_PASS_DECL_OMPMAPINFOFINALIZATIONPASS
5455
#include "flang/Optimizer/Transforms/Passes.h.inc"
5556

5657
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
@@ -70,7 +71,6 @@ std::unique_ptr<mlir::Pass> createAlgebraicSimplificationPass();
7071
std::unique_ptr<mlir::Pass>
7172
createAlgebraicSimplificationPass(const mlir::GreedyRewriteConfig &config);
7273

73-
std::unique_ptr<mlir::Pass> createOMPMapInfoFinalizationPass();
7474
std::unique_ptr<mlir::Pass> createOMPFunctionFilteringPass();
7575
std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
7676
createOMPMarkDeclareTargetPass();

flang/include/flang/Optimizer/Transforms/Passes.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,13 @@ def LoopVersioning : Pass<"loop-versioning", "mlir::func::FuncOp"> {
324324
}
325325

326326
def OMPMapInfoFinalizationPass
327-
: Pass<"omp-map-info-finalization", "mlir::func::FuncOp"> {
327+
: Pass<"omp-map-info-finalization"> {
328328
let summary = "expands OpenMP MapInfo operations containing descriptors";
329329
let description = [{
330330
Expands MapInfo operations containing descriptor types into multiple
331331
MapInfo's for each pointer element in the descriptor that requires
332332
explicit individual mapping by the OpenMP runtime.
333333
}];
334-
let constructor = "::fir::createOMPMapInfoFinalizationPass()";
335334
let dependentDialects = ["mlir::omp::OpenMPDialect"];
336335
}
337336

flang/include/flang/Tools/CLOptions.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,8 @@ inline void createHLFIRToFIRPassPipeline(
345345
/// rather than the host device.
346346
inline void createOpenMPFIRPassPipeline(
347347
mlir::PassManager &pm, bool isTargetDevice) {
348-
pm.addPass(fir::createOMPMapInfoFinalizationPass());
348+
addNestedPassToAllTopLevelOperations(
349+
pm, fir::createOMPMapInfoFinalizationPass);
349350
pm.addPass(fir::createOMPMarkDeclareTargetPass());
350351
if (isTargetDevice)
351352
pm.addPass(fir::createOMPFunctionFilteringPass());

flang/lib/Optimizer/Transforms/OMPMapInfoFinalization.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ class OMPMapInfoFinalizationPass
7676
// alloca.
7777
if (mlir::isa<fir::BaseBoxType>(descriptor.getType())) {
7878
mlir::OpBuilder::InsertPoint insPt = builder.saveInsertionPoint();
79-
builder.setInsertionPointToStart(builder.getAllocaBlock());
79+
mlir::Block *allocaBlock = builder.getAllocaBlock();
80+
assert(allocaBlock && "No alloca block found for this top level op");
81+
builder.setInsertionPointToStart(allocaBlock);
8082
auto alloca = builder.create<fir::AllocaOp>(loc, descriptor.getType());
8183
builder.restoreInsertionPoint(insPt);
8284
builder.create<fir::StoreOp>(loc, descriptor, alloca);
@@ -217,17 +219,23 @@ class OMPMapInfoFinalizationPass
217219
mapClauseOwner.getMapOperandsMutable().assign(newMapOps);
218220
}
219221

220-
// This pass executes on mlir::ModuleOp's finding omp::MapInfoOp's containing
221-
// descriptor based types (allocatables, pointers, assumed shape etc.) and
222-
// expanding them into multiple omp::MapInfoOp's for each pointer member
223-
// contained within the descriptor.
222+
// This pass executes on omp::MapInfoOp's containing descriptor based types
223+
// (allocatables, pointers, assumed shape etc.) and expanding them into
224+
// multiple omp::MapInfoOp's for each pointer member contained within the
225+
// descriptor.
226+
//
227+
// From the perspective of the MLIR pass manager this runs on the top level
228+
// operation (usually function) containing the MapInfoOp because this pass
229+
// will mutate siblings of MapInfoOp.
224230
void runOnOperation() override {
225-
mlir::func::FuncOp func = getOperation();
226-
mlir::ModuleOp module = func->getParentOfType<mlir::ModuleOp>();
231+
mlir::ModuleOp module =
232+
mlir::dyn_cast_or_null<mlir::ModuleOp>(getOperation());
233+
if (!module)
234+
module = getOperation()->getParentOfType<mlir::ModuleOp>();
227235
fir::KindMapping kindMap = fir::getKindMapping(module);
228236
fir::FirOpBuilder builder{module, std::move(kindMap)};
229237

230-
func->walk([&](mlir::omp::MapInfoOp op) {
238+
getOperation()->walk([&](mlir::omp::MapInfoOp op) {
231239
// TODO: Currently only supports a single user for the MapInfoOp, this
232240
// is fine for the moment as the Fortran Frontend will generate a
233241
// new MapInfoOp per Target operation for the moment. However, when/if
@@ -253,9 +261,3 @@ class OMPMapInfoFinalizationPass
253261
};
254262

255263
} // namespace
256-
257-
namespace fir {
258-
std::unique_ptr<mlir::Pass> createOMPMapInfoFinalizationPass() {
259-
return std::make_unique<OMPMapInfoFinalizationPass>();
260-
}
261-
} // namespace fir

0 commit comments

Comments
 (0)