[flang-commits] [flang] [llvm] [flang][OpenMP] Track reachable metadirective replacements (PR #219014)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 9 19:47:40 PDT 2026


================
@@ -207,38 +208,39 @@ void OmpStructureChecker::Enter(const parser::EndMpSubprogramStmt &x) {
   scopeStack_.pop_back();
 }
 
-void OmpStructureChecker::BeginMetadirectiveVariantScope() {
-  metadirectiveVariantScopeStarts_.push_back(metadirectiveLoopVariants_.size());
+void OmpStructureChecker::BeginPendingLoopDirectiveScope() {
+  pendingLoopDirectiveScopeStarts_.push_back(
+      pendingLoopDirectiveGroups_.size());
 }
 
-void OmpStructureChecker::EndMetadirectiveVariantScope() {
-  CHECK(!metadirectiveVariantScopeStarts_.empty());
-  std::size_t firstVariant{metadirectiveVariantScopeStarts_.back()};
-  metadirectiveVariantScopeStarts_.pop_back();
-  if (firstVariant < metadirectiveLoopVariants_.size()) {
-    // Diagnose variants that were recorded in this scope but not consumed by
-    // one of its executable constructs, preserving variants from an enclosing
-    // scope.
-    CheckMetadirectiveVariantsWithoutLoop(firstVariant);
+void OmpStructureChecker::EndPendingLoopDirectiveScope() {
+  CHECK(!pendingLoopDirectiveScopeStarts_.empty());
+  std::size_t firstDirectiveGroup{pendingLoopDirectiveScopeStarts_.back()};
+  pendingLoopDirectiveScopeStarts_.pop_back();
+  if (firstDirectiveGroup < pendingLoopDirectiveGroups_.size()) {
----------------
MattPD wrote:

Could the saved scope boundary remain valid when earlier pending groups are consumed? In the reproducer, `continue` consumes the enclosing group, and the inner metadirective adds one group with two variants. The group count equals the saved boundary, so cleanup misses the group the inner metadirective added. The DO outside the selected PARALLEL region then satisfies the inner directives.

Save this as `repro.f90` and run `flang -fc1 -fopenmp -fopenmp-version=52 -fsyntax-only repro.f90`. Revision `7f525837ea63` accepts it. [The base revision](https://github.com/llvm/llvm-project/commit/89082772bed1ed16c0311b12147a96c35bb063d8) diagnoses the missing associated loop. With the outer metadirective replaced by a directly written PARALLEL region, both revisions diagnose both inner variants.

```fortran
subroutine escaped_region(flag, n)
  logical :: flag
  integer :: n, i
  !$omp begin metadirective otherwise(parallel)
    continue
    !$omp metadirective when(user={condition(flag)}: simd) otherwise(do)
  !$omp end metadirective
  do i = 1, n
  end do
end subroutine
```

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


More information about the flang-commits mailing list