[clang] [openmp] [OpenMP] Assign lastprivate loop counter init on empty worksharing loops (PR #243086)

Amit Tiwari via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 23:09:32 PST 2026


================
@@ -4046,6 +4016,24 @@ struct ScheduleKindModifiersTy {
 };
 } // namespace
 
+/// Return the for-init of a lastprivate loop counter. Empty loops still run
+/// that init in sequential code. Skip collapse nests and
+/// distribute: their empty lastprivate values are not a single init store.
+static const Expr *getEmptyLoopLastprivateInit(const OMPLoopDirective &S) {
+  if (S.counters().size() != 1 ||
+      isOpenMPDistributeDirective(S.getDirectiveKind()))
+    return nullptr;
+  const Decl *CounterVD =
+      cast<DeclRefExpr>(S.counters().front())->getDecl()->getCanonicalDecl();
+  for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) {
+    for (const Expr *Ref : C->varlist()) {
+      if (cast<DeclRefExpr>(Ref)->getDecl()->getCanonicalDecl() == CounterVD)
+        return S.inits().front();
----------------
loopacino wrote:

Added an assert with a comment.

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


More information about the cfe-commits mailing list