[flang-commits] [flang] [Flang][OpenMP] Update declare mapper lookup via use-module (PR #163860)

Akash Banerjee via flang-commits flang-commits at lists.llvm.org
Wed Oct 29 07:21:52 PDT 2025


================
@@ -4239,3 +4255,32 @@ void Fortran::lower::genOpenMPRequires(mlir::Operation *mod,
     offloadMod.setRequires(mlirFlags);
   }
 }
+
+// Walk scopes and materialize omp.declare_mapper ops for mapper declarations
+// found in imported modules. If \p scope is null, start from the global scope.
+void Fortran::lower::materializeOpenMPDeclareMappers(
+    Fortran::lower::AbstractConverter &converter,
+    semantics::SemanticsContext &semaCtx, const semantics::Scope *scope) {
+  const semantics::Scope &root = scope ? *scope : semaCtx.globalScope();
+
+  // Recurse into child scopes first (modules, submodules, etc.).
+  for (const semantics::Scope &child : root.children())
+    materializeOpenMPDeclareMappers(converter, semaCtx, &child);
+
+  // Only consider module scopes to avoid duplicating local constructs.
+  if (!root.IsModule())
+    return;
+
+  // Scan symbols in this module scope for MapperDetails.
+  for (auto &it : root) {
+    const semantics::Symbol &sym = *it.second;
+    if (auto *md = sym.detailsIf<semantics::MapperDetails>()) {
+      for (const auto *decl : md->GetDeclList()) {
+        if (const auto *mapperDecl =
+                std::get_if<parser::OpenMPDeclareMapperConstruct>(&decl->u)) {
+          genOpenMPDeclareMapperImpl(converter, semaCtx, *mapperDecl, &sym);
+        }
+      }
+    }
+  }
+}
----------------
TIFitis wrote:

If X uses module A and A USEs module B (B from a .mod), B’s mappers will live under A’s scope in the semantics tree. Without recursion, B’s mappers would be skipped, and map(mapper(...)) using B’s mapper would not resolve.

Otherwise, we already early-return for local scopes and non .mod files.

https://github.com/llvm/llvm-project/pull/163860


More information about the flang-commits mailing list