================
@@ -10672,16 +10694,38 @@ checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr 
*CollapseLoopCountExpr,
       }
 
       // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
-      ExprResult Final =
-          buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
-                             IS.CounterInit, IS.NumIterations, IS.CounterStep,
-                             IS.Subtract, IS.IsNonRectangularLB, &Captures);
-      if (!Final.isUsable()) {
-        HasErrors = true;
-        break;
+      // For loop transformation directives with arithmetic counters, use
+      // (NumIters - 1) to get the value from the last iteration, not the loop
+      // exit value. For iterator-based loops (range-based for or explicit
+      // iterator loops), skip finalization entirely.
+      ExprResult Final;
+      bool IsIteratorLoop =
+          IS.IsRangeFor || !IS.CounterVar->getType()->isArithmeticType();
+      if (IsIteratorLoop && isOpenMPLoopTransformationDirective(DKind)) {
+        // Iterator-based loops in transformation directives don't need
+        // explicit finalization - the iterator is already at the end.
+        Final = nullptr;
+      } else {
+        Expr *FinalIterCount = IS.NumIterations;
+        if (isOpenMPLoopTransformationDirective(DKind)) {
+          ExprResult LastIter = SemaRef.BuildBinOp(
+              CurScope, UpdLoc, BO_Sub, IS.NumIterations,
+              SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
+          if (LastIter.isUsable()) {
+            FinalIterCount = LastIter.get();
+          }
+        }
+        Final =
+            buildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
+                               IS.CounterInit, FinalIterCount, IS.CounterStep,
+                               IS.Subtract, IS.IsNonRectangularLB, &Captures);
----------------
zahiraam wrote:

Added a test.

https://github.com/llvm/llvm-project/pull/214717
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to