[flang-commits] [flang] [flang][openmp] Support importing module declare target globals (PR #213930)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Tue Aug 25 05:25:47 PDT 2026


================
@@ -8093,3 +8096,54 @@ void Fortran::lower::materializeOpenMPDeclareMappers(
 // Walk scopes and materialize omp.declare_reduction ops for user-defined
 // operator reductions imported from modules (deleted: replaced by lazy,
 // clause-driven materialization).
+
+// Visitor used to mark declare target globals from imported modules.
+struct ModuleDeclareTargetVisitor {
+  Fortran::lower::AbstractConverter &converter;
+  semantics::SemanticsContext &semaCtx;
+
+  explicit ModuleDeclareTargetVisitor(
+      Fortran::lower::AbstractConverter &converter,
+      semantics::SemanticsContext &ctx)
+      : converter(converter), semaCtx(ctx) {}
+
+  template <typename T>
+  bool Pre(const T &) {
+    return true;
+  }
+  template <typename T>
+  void Post(const T &) {}
+
+  void Post(const parser::OmpDeclareTargetDirective &directive) {
+    mlir::omp::DeclareTargetOperands clauseOps;
+    llvm::SmallVector<DeclareTargetCaptureInfo> symbolAndClause;
+    mlir::ModuleOp mod = converter.getFirOpBuilder().getModule();
+
+    getDeclareTargetInfo(converter, semaCtx, std::nullopt, directive, clauseOps,
+                         symbolAndClause);
+
+    for (const DeclareTargetCaptureInfo &symClause : symbolAndClause) {
+      mlir::Operation *op =
+          mod.lookupSymbol(converter.mangleName(symClause.symbol));
+
+      // op not found, so nothing to mark. This happens for variables
+      // and functions that are not actually used in the current
+      // translation unit.
+      if (!op)
+        continue;
+
+      markDeclareTarget(op, converter, symClause.clause, clauseOps.deviceType,
+                        symClause.automap);
+    }
+  }
+};
+
+void Fortran::lower::markOpenMPImportedDeclareTargets(
+    Fortran::lower::AbstractConverter &converter,
+    semantics::SemanticsContext &semaCtx) {
+  std::list<parser::Program> &modTrees = semaCtx.GetModFileParseTrees();
----------------
skatrak wrote:

The `materializeOpenMPDeclareMappers()` seems to be achieving something similar without needing to expose the `modFileParseTrees_`. Can the same be done here?

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


More information about the flang-commits mailing list