[flang-commits] [flang] [flang][OpenMP] Fix standalone SIMD directive post-loop variable value (PR #196731)

via flang-commits flang-commits at lists.llvm.org
Sat May 9 07:37:09 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: Michael Klemm (mjklemm)

<details>
<summary>Changes</summary>

The OpenMP API requires that the last value of a loop variable of a standalone SIMD directive is preserved in the orginal variable.

---
Full diff: https://github.com/llvm/llvm-project/pull/196731.diff


2 Files Affected:

- (modified) flang/lib/Semantics/resolve-directives.cpp (+10-1) 
- (modified) flang/test/Semantics/OpenMP/implicit_linear_symbols.f90 (+1-1) 


``````````diff
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 52cd445027d37..3ac1835dbc87a 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2134,9 +2134,18 @@ void OmpAttributeVisitor::PrivatizeAssociatedLoopIndex(
         continue;
       }
       if (auto *symbol{ResolveOmp(*iv, ivDSA, scope)}) {
-        SetSymbolDSA(*symbol, {Symbol::Flag::OmpPreDetermined, ivDSA});
+        Symbol::Flags ivFlags{Symbol::Flag::OmpPreDetermined, ivDSA};
+        // For a standalone SIMD loop variable set lastprivate so the original
+        // variable is correctly set to the last value of the loop variable as
+        // per the OpenMP spec
+        if (ivDSA == Symbol::Flag::OmpLinear &&
+            llvm::omp::topSimdSet.test(GetContext().directive))
+          ivFlags.set(Symbol::Flag::OmpLastPrivate);
+        SetSymbolDSA(*symbol, ivFlags);
         iv->symbol = symbol; // adjust the symbol within region
         AddToContextObjectWithDSA(*symbol, ivDSA);
+        if (ivFlags.test(Symbol::Flag::OmpLastPrivate))
+          AddToContextObjectWithDSA(*symbol, Symbol::Flag::OmpLastPrivate);
       }
     }
   }
diff --git a/flang/test/Semantics/OpenMP/implicit_linear_symbols.f90 b/flang/test/Semantics/OpenMP/implicit_linear_symbols.f90
index e57a063d8a9d0..7f8110dd931de 100644
--- a/flang/test/Semantics/OpenMP/implicit_linear_symbols.f90
+++ b/flang/test/Semantics/OpenMP/implicit_linear_symbols.f90
@@ -2,7 +2,7 @@
 !RUN: %flang_fc1 -fopenmp -fopenmp-version=60 -fdebug-dump-symbols %s 2>&1 | FileCheck %s --check-prefix=NOIMPLICIT
 
 
-!IMPLICIT: k2 (OmpLinear, OmpPreDetermined): {{.*}}
+!IMPLICIT: k2 (OmpLinear, OmpLastPrivate, OmpPreDetermined): {{.*}}
 !NOIMPLICIT: k2 (OmpLastPrivate, OmpPreDetermined): {{.*}}
 subroutine implicit_linear
   integer :: k1, k2

``````````

</details>


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


More information about the flang-commits mailing list