[llvm-branch-commits] [flang] [mlir] [Flang][MLIR][OpenMP] Explicitly represent omp.target kernel types (PR #186166)

Michael Kruse via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Mar 19 07:47:36 PDT 2026


================
@@ -201,6 +190,393 @@ class HostEvalInfo {
   llvm::SmallVector<const semantics::Symbol *> iv;
   bool loopNestApplied = false, parallelApplied = false;
 };
+
+class OpenMPPatternProcessor {
+public:
+  OpenMPPatternProcessor(semantics::SemanticsContext &semaCtx)
+      : semaCtx(semaCtx) {}
+  virtual ~OpenMPPatternProcessor() = default;
+
+  /// Run the pattern from the given evaluation.
+  void process(lower::pft::Evaluation &eval) {
+    dirsToProcess = initialDirsToProcess();
+    processEval(eval);
+  }
+
+protected:
+  /// Returns the set of directives of interest at the beginning of the pattern.
+  virtual OmpDirectiveSet initialDirsToProcess() const = 0;
+
+  /// Processes a single directive and, based on it, returns the set of other
+  /// directives of interest that would be part of the pattern if nested inside
+  /// of it.
+  virtual OmpDirectiveSet processDirective(lower::pft::Evaluation &eval,
+                                           llvm::omp::Directive dir) = 0;
+
+  /// Obtain the list of clauses of the given OpenMP block or loop construct
+  /// evaluation. If it's not an OpenMP construct, no modifications are made to
+  /// the \c clauses output argument.
+  void extractClauses(lower::pft::Evaluation &eval, List<Clause> &clauses) {
+    const auto *ompEval = eval.getIf<parser::OpenMPConstruct>();
+    if (!ompEval)
+      return;
+
+    const parser::OmpClauseList *beginClauseList = nullptr;
+    const parser::OmpClauseList *endClauseList = nullptr;
+    common::visit(
+        [&](const auto &construct) {
+          using Type = llvm::remove_cvref_t<decltype(construct)>;
+          if constexpr (std::is_same_v<Type, parser::OmpBlockConstruct> ||
+                        std::is_same_v<Type, parser::OpenMPLoopConstruct>) {
+            beginClauseList = &construct.BeginDir().Clauses();
+            if (auto &endSpec = construct.EndDir())
+              endClauseList = &endSpec->Clauses();
+          }
+        },
+        ompEval->u);
+
+    assert(beginClauseList && "expected begin directive");
+    clauses.append(makeClauses(*beginClauseList, semaCtx));
+
+    if (endClauseList)
+      clauses.append(makeClauses(*endClauseList, semaCtx));
+  }
+
+private:
+  /// Decide whether an evaluation must be processed as part of the pattern.
+  ///
+  /// This is the case whenever it's an OpenMP construct and the associated
+  /// directive is part of the current set of directives of interest.
+  bool shouldProcessEval(lower::pft::Evaluation &eval) const {
+    const auto *ompEval = eval.getIf<parser::OpenMPConstruct>();
+    if (!ompEval)
+      return false;
+
+    return dirsToProcess.test(parser::omp::GetOmpDirectiveName(*ompEval).v);
+  }
+
+  /// Processes an evaluation and, potentially, recursively process a single
+  /// nested evaluation.
+  ///
+  /// For a nested evaluation to be recursively processed, it must be an OpenMP
+  /// construct, have no sibling evaluations and match one of the
+  /// next-directives of interest set returned by a call to \c processDirective
+  /// on the parent evaluation.
+  void processEval(lower::pft::Evaluation &eval) {
+    if (!shouldProcessEval(eval))
+      return;
+
+    const auto &ompEval = eval.get<parser::OpenMPConstruct>();
+    OmpDirectiveSet processNested =
+        processDirective(eval, parser::omp::GetOmpDirectiveName(ompEval).v);
+
+    if (processNested.empty())
+      return;
+
+    if (lower::pft::Evaluation *nestedEval = extractOnlyOmpNestedEval(eval)) {
----------------
Meinersbur wrote:

```suggestion
    if (lower::pft::Evaluation *nestedEval{extractOnlyOmpNestedEval(eval)}) {
```
[style] [Use {braced initializers} in all circumstances where they work, including default data member initialization. ](https://flang.llvm.org/docs/C%2B%2Bstyle.html#c-language)

(I concede that formatting is somewhat different in this file than in the rest of Flang; keeping style consistent within this file might be more important)

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


More information about the llvm-branch-commits mailing list