[clang] [OpenMP] Restore loop variable values after loop-transformation constructs. (PR #212853)
Zahira Ammarguellat via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 30 07:00:33 PDT 2026
================
@@ -15033,6 +15033,48 @@ static Expr *makeFloorIVRef(Sema &SemaRef, ArrayRef<VarDecl *> FloorIndVars,
OrigCntVar->getExprLoc());
}
+/// Build loop variable finalization statement from HelperExprs.Finals.
+/// Returns a CompoundStmt containing all finalization statements, or nullptr
+/// if there are no finalization statements.
+/// Note: Loops with non-arithmetic loop variables (e.g., iterators) are skipped
+/// because finalization only applies to integer/floating-point counters.
+static Stmt *
+buildLoopFinalization(ASTContext &Context,
+ ArrayRef<OMPLoopBasedDirective::HelperExprs> LoopHelpers,
+ ArrayRef<Stmt *> LoopStmts) {
+ assert(LoopHelpers.size() == LoopStmts.size() &&
+ "LoopHelpers and LoopStmts must have the same size");
+ SmallVector<Stmt *, 8> FinalizationStmts;
+ for (const auto &[Helper, LoopStmt] : llvm::zip(LoopHelpers, LoopStmts)) {
+ // Skip finalization for range-based for loops (CXXForRangeStmt) since
+ // their loop variables are iterators, not integer counters.
+ if (isa<CXXForRangeStmt>(LoopStmt))
+ continue;
+
+ // For ForStmt, check if the loop variable is arithmetic.
----------------
zahiraam wrote:
The original commit (#208533) did restore all datatypes without filtering - it collected all `Finals` from `LoopHelper`s. However, this caused `libomp` runtime test failures (`tile/iterfor.cpp, reverse/foreach.cpp, interchange/iterfor.cpp`, etc.). The issue is that` Finals` computed in Sema uses the arithmetic formula `final_value = lower_bound + num_iterations * step`, which works for arithmetic types but doesn't translate correctly for iterators. The tests failed because the finalization generated unexpected iterator operations.
This reland restricts finalization to arithmetic types only (via isArithmeticType() check), and also skips range-based for loops (CXXForRangeStmt), which makes the tests pass. We can consider doing it for iterators in a follow-up PR.
https://github.com/llvm/llvm-project/pull/212853
More information about the cfe-commits
mailing list