Skip to content

Commit 6d813e8

Browse files
committed
Use lazy approach to define inherited declare mappers.
1 parent cdf2bd8 commit 6d813e8

File tree

4 files changed

+36
-45
lines changed

4 files changed

+36
-45
lines changed

flang/include/flang/Lower/OpenMP.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ bool markOpenMPDeferredDeclareTargetFunctions(
9797
AbstractConverter &);
9898
void genOpenMPRequires(mlir::Operation *, const Fortran::semantics::Symbol *);
9999

100-
// Materialize omp.declare_mapper ops for mapper declarations found in
101-
// imported modules. If \p scope is null, materialize for the whole
102-
// semantics global scope; otherwise, operate recursively starting at \p scope.
103-
void materializeOpenMPDeclareMappers(
100+
// Materialize an omp.declare_mapper op for the given mapper symbol by
101+
// re-lowering its saved declaration from semantics.
102+
void materializeOpenMPDeclareMapperForSymbol(
104103
Fortran::lower::AbstractConverter &, Fortran::semantics::SemanticsContext &,
105-
const Fortran::semantics::Scope *scope = nullptr);
104+
const Fortran::semantics::Symbol &);
106105

107106
} // namespace lower
108107
} // namespace Fortran

flang/lib/Lower/Bridge.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
447447
}
448448
});
449449

450-
// Ensure imported OpenMP declare mappers are materialized at module
451-
// scope before lowering any constructs that may reference them.
452-
createBuilderOutsideOfFuncOpAndDo([&]() {
453-
Fortran::lower::materializeOpenMPDeclareMappers(
454-
*this, bridge.getSemanticsContext());
455-
});
456-
457450
// Create definitions of intrinsic module constants.
458451
createBuilderOutsideOfFuncOpAndDo(
459452
[&]() { createIntrinsicModuleDefinitions(pft); });

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "flang/Lower/ConvertExprToHLFIR.h"
1717
#include "flang/Lower/OpenMP/Clauses.h"
18+
#include "flang/Lower/OpenMP.h"
1819
#include "flang/Lower/PFTBuilder.h"
1920
#include "flang/Lower/Support/ReductionProcessor.h"
2021
#include "flang/Parser/tools.h"
@@ -1255,6 +1256,11 @@ void ClauseProcessor::processMapObjects(
12551256
const omp::Object &object = objects.front();
12561257
if (mapperIdNameRef == "default")
12571258
getDefaultMapperID(object, mapperIdName);
1259+
// On-demand materialize named or default mappers if missing.
1260+
if (!converter.getModuleOp().lookupSymbol(mapperIdName))
1261+
if (auto *sym = converter.getCurrentScope().FindSymbol(mapperIdName))
1262+
Fortran::lower::materializeOpenMPDeclareMapperForSymbol(
1263+
converter, semaCtx, sym->GetUltimate());
12581264
assert(converter.getModuleOp().lookupSymbol(mapperIdName) &&
12591265
"mapper not found");
12601266
mapperId =
@@ -1295,6 +1301,12 @@ void ClauseProcessor::processMapObjects(
12951301
if (mapperIdNameRef == "__implicit_mapper") {
12961302
std::string mapperIdName;
12971303
getDefaultMapperID(object, mapperIdName);
1304+
// On-demand materialize default mappers if missing.
1305+
if (!mapperIdName.empty() &&
1306+
!converter.getModuleOp().lookupSymbol(mapperIdName))
1307+
if (auto *sym = converter.getCurrentScope().FindSymbol(mapperIdName))
1308+
Fortran::lower::materializeOpenMPDeclareMapperForSymbol(
1309+
converter, semaCtx, sym->GetUltimate());
12981310
mapperId = converter.getModuleOp().lookupSymbol(mapperIdName)
12991311
? mlir::FlatSymbolRefAttr::get(&converter.getMLIRContext(),
13001312
mapperIdName)
@@ -1410,6 +1422,10 @@ bool ClauseProcessor::processMap(
14101422
const semantics::Symbol &ultimate = mapperSym->GetUltimate();
14111423
mapperIdName = converter.mangleName(mapperIdName, ultimate.owner());
14121424
}
1425+
// On-demand materialize if missing.
1426+
if (!converter.getModuleOp().lookupSymbol(mapperIdName))
1427+
Fortran::lower::materializeOpenMPDeclareMapperForSymbol(
1428+
converter, semaCtx, *mapperSym);
14131429
}
14141430

14151431
processMapObjects(stmtCtx, clauseLocation,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,6 +3605,22 @@ static void genOpenMPDeclareMapperImpl(
36053605
mlir::omp::DeclareMapperInfoOp::create(firOpBuilder, loc, clauseOps.mapVars);
36063606
}
36073607

3608+
// Materialize omp.declare_mapper for a specific mapper symbol by lowering the
3609+
// saved declaration from semantics.
3610+
void Fortran::lower::materializeOpenMPDeclareMapperForSymbol(
3611+
Fortran::lower::AbstractConverter &converter,
3612+
semantics::SemanticsContext &semaCtx, const semantics::Symbol &sym) {
3613+
const semantics::Symbol &ultimate = sym.GetUltimate();
3614+
if (auto *md = ultimate.detailsIf<semantics::MapperDetails>()) {
3615+
for (const auto *decl : md->GetDeclList()) {
3616+
if (const auto *mapperDecl =
3617+
std::get_if<parser::OpenMPDeclareMapperConstruct>(&decl->u)) {
3618+
genOpenMPDeclareMapperImpl(converter, semaCtx, *mapperDecl, &ultimate);
3619+
}
3620+
}
3621+
}
3622+
}
3623+
36083624
static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
36093625
semantics::SemanticsContext &semaCtx,
36103626
lower::pft::Evaluation &eval,
@@ -4255,36 +4271,3 @@ void Fortran::lower::genOpenMPRequires(mlir::Operation *mod,
42554271
offloadMod.setRequires(mlirFlags);
42564272
}
42574273
}
4258-
4259-
// Walk scopes and materialize omp.declare_mapper ops for mapper declarations
4260-
// found in imported modules. If \p scope is null, start from the global scope.
4261-
void Fortran::lower::materializeOpenMPDeclareMappers(
4262-
Fortran::lower::AbstractConverter &converter,
4263-
semantics::SemanticsContext &semaCtx, const semantics::Scope *scope) {
4264-
const semantics::Scope &root = scope ? *scope : semaCtx.globalScope();
4265-
4266-
// Recurse into child scopes first (modules, submodules, etc.).
4267-
for (const semantics::Scope &child : root.children())
4268-
materializeOpenMPDeclareMappers(converter, semaCtx, &child);
4269-
4270-
// Only consider module scopes to avoid duplicating local constructs.
4271-
if (!root.IsModule())
4272-
return;
4273-
4274-
// Only materialize for modules coming from mod files to avoid duplicates.
4275-
if (!root.symbol() || !root.symbol()->test(semantics::Symbol::Flag::ModFile))
4276-
return;
4277-
4278-
// Scan symbols in this module scope for MapperDetails.
4279-
for (auto &it : root) {
4280-
const semantics::Symbol &sym = *it.second;
4281-
if (auto *md = sym.detailsIf<semantics::MapperDetails>()) {
4282-
for (const auto *decl : md->GetDeclList()) {
4283-
if (const auto *mapperDecl =
4284-
std::get_if<parser::OpenMPDeclareMapperConstruct>(&decl->u)) {
4285-
genOpenMPDeclareMapperImpl(converter, semaCtx, *mapperDecl, &sym);
4286-
}
4287-
}
4288-
}
4289-
}
4290-
}

0 commit comments

Comments
 (0)