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

Michael Klemm via flang-commits flang-commits at lists.llvm.org
Sat May 9 07:36:35 PDT 2026


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

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

>From 0641744cb51597ae49d4b792b129bb0a6de6e5c7 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Sat, 9 May 2026 16:34:59 +0200
Subject: [PATCH] [flang][OpenMP] Fix standalone SIMD directive post-loop
 variable value

The OpenMP API requires that the last value of a loop variable of a
standalone SIMD directive is preserved in the orginal variable.
---
 flang/lib/Semantics/resolve-directives.cpp            | 11 ++++++++++-
 .../test/Semantics/OpenMP/implicit_linear_symbols.f90 |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

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



More information about the flang-commits mailing list