[clang] [flang] [llvm] [clang][flang][OpenMP] Fix context selector matching and scoring (PR #224431)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 30 21:54:36 PDT 2026


================
@@ -1473,27 +1490,78 @@ semantics::omp::OmpVariantMatchContext makeVariantMatchContext(
 }
 
 void collectEnclosingConstructTraits(
-    mlir::Operation *op,
+    AbstractConverter &converter, const pft::Evaluation *evaluation,
     llvm::SmallVectorImpl<llvm::omp::TraitProperty> &constructTraits) {
-  // Collect enclosing OpenMP operations so variants chosen by an outer
-  // metadirective are part of this metadirective's context. For example, an
-  // inner metadirective inside `target` and an outer-selected `parallel` must
-  // be able to match construct={target, parallel}. The final reverse yields
-  // outermost-to-innermost order as required by OMPContext.
-  for (; op; op = op->getParentOp()) {
-    if (mlir::isa<mlir::omp::WsloopOp>(op))
-      constructTraits.push_back(llvm::omp::TraitProperty::construct_for_for);
-    if (mlir::isa<mlir::omp::ParallelOp>(op))
-      constructTraits.push_back(
-          llvm::omp::TraitProperty::construct_parallel_parallel);
-    if (mlir::isa<mlir::omp::TeamsOp>(op))
-      constructTraits.push_back(
-          llvm::omp::TraitProperty::construct_teams_teams);
-    if (mlir::isa<mlir::omp::TargetOp>(op))
-      constructTraits.push_back(
-          llvm::omp::TraitProperty::construct_target_target);
+  const auto *loopControl =
+      converter.getStateStack().getStackTop<LoopControlContext>();
+  // Lastprivate can re-evaluate bounds after lowering the loop body, leaving
+  // a body evaluation current. Use the owning directive's ancestors so the
+  // loop itself is not added before filtering its entered constituents below.
+  if (loopControl)
+    evaluation = &loopControl->evaluation;
----------------
MattPD wrote:

Both tests added in 89a879d already pass on 3080eca, because each test's copy-back calls `parallel_bound`. `genEval` restores the current evaluation after each body evaluation. The copy-back therefore reaches this code while the loop directive is the current evaluation, so this line does not affect that path. It does change host evaluation: `HostEvalVisitor` passes a loop directive nested in TARGET to `processCollapse` while TARGET is the current evaluation. The host-evaluated bound now selects the same variant as device compilation when the loop is written as three separate directives: TARGET, PARALLEL, and DO. At the merge base, host and device compilation select different variants for that bound. Could the code comment and a test cover that case instead?

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


More information about the llvm-commits mailing list