[clang] [CIR][OpenMP] Add support for the OpenMP 'for' directive (PR #229256)

Jan Leyonberg via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 12:54:56 PST 2026


https://github.com/jsjodin updated https://github.com/llvm/llvm-project/pull/229256

>From 605a3d845c00c223e394fc6cb85272064205ff2d Mon Sep 17 00:00:00 2001
From: Jan Leyonberg <jan_sjodin at yahoo.com>
Date: Fri, 2 Oct 2026 10:55:22 -0400
Subject: [PATCH] [CIR][OpenMP] Add support for the OpenMP 'for' directive

This patch adds support for wsloop in ClangIR: the `for` directive and its
combined forms `parallel for` and `target parallel for`. This is lowered to an
omp.wsloop + omp.loop_nest, nested utilizing the existing queue-based
decomposition.

Assisted-by: Cursor / Claude Sonnet 5 High
---
 .../CIR/Dialect/Builder/CIRBaseBuilder.h      |   7 +
 clang/lib/CIR/CodeGen/CIRGenFunction.h        |   7 +
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp    | 440 ++++++++++++++----
 .../lib/CIR/Dialect/Transforms/FlattenCFG.cpp |   7 +-
 clang/test/CIR/CodeGenOpenMP/for-loop-forms.c | 341 ++++++++++++++
 clang/test/CIR/CodeGenOpenMP/for.c            | 211 +++++++++
 clang/test/CIR/CodeGenOpenMP/parallel-for.c   | 117 +++++
 .../CIR/CodeGenOpenMP/region-cleanups-O2.c    | 140 ++++++
 .../CIR/CodeGenOpenMP/target-parallel-for.c   | 374 +++++++++++++++
 .../test/CIR/CodeGenOpenMP/target-parallel.c  | 137 ++++++
 10 files changed, 1692 insertions(+), 89 deletions(-)
 create mode 100644 clang/test/CIR/CodeGenOpenMP/for-loop-forms.c
 create mode 100644 clang/test/CIR/CodeGenOpenMP/for.c
 create mode 100644 clang/test/CIR/CodeGenOpenMP/parallel-for.c
 create mode 100644 clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
 create mode 100644 clang/test/CIR/CodeGenOpenMP/target-parallel-for.c

diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 489ed57695d299..4d0176ba25f8d0 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -742,6 +742,13 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
     return createBuiltinIntCast(src.getLoc(), src, newTy);
   }
 
+  /// Casts \p src to the builtin MLIR integer type with the same width as
+  /// its own CIR integer type.
+  mlir::Value createBuiltinIntCast(mlir::Location loc, mlir::Value src) {
+    auto cirIntTy = mlir::cast<cir::IntType>(src.getType());
+    return createBuiltinIntCast(loc, src, getIntegerType(cirIntTy.getWidth()));
+  }
+
   mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy) {
     return createCast(cir::CastKind::int_to_ptr, src, newTy);
   }
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index df01669410bf3a..6c0a2a5d083ca4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -2867,6 +2867,13 @@ class CIRGenFunction : public CIRGenTypeCache {
   //                         OpenMP Emission
   //===--------------------------------------------------------------------===//
 public:
+  /// Emits a Sema-synthesized OpenMP helper variable (loop counter, iteration
+  /// variable or pre-init capture) without lifetime markers. These variables
+  /// are emitted between nested combined/composite OpenMP ops, which must be
+  /// direct children of each other's regions, so they must not open a
+  /// lifetime cleanup scope.
+  void emitOMPNoLifetimeVarDecl(const VarDecl &d);
+
   mlir::LogicalResult emitOMPScopeDirective(const OMPScopeDirective &s);
   mlir::LogicalResult emitOMPErrorDirective(const OMPErrorDirective &s);
   mlir::LogicalResult emitOMPParallelDirective(const OMPParallelDirective &s);
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index 9d937f5ed67f90..e41ab8153cda4f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -20,6 +20,7 @@
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/Frontend/OpenMP/OMP.h"
 #include "llvm/Frontend/OpenMP/OMPConstants.h"
+#include "llvm/Support/SaveAndRestore.h"
 using namespace clang;
 using namespace clang::CIRGen;
 
@@ -69,6 +70,27 @@ emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
       llvm::omp::Directive::OMPD_parallel);
 }
 
+/// Emits the contents of an OpenMP region op's \p block in a new lexical scope
+/// and terminates the block with a \p TermOp. The scope's cleanups (e.g.
+/// lifetime markers of locals declared in the region) are emitted before the
+/// terminator; destroying the scope instead would make it terminate the block
+/// with a cir.yield.
+template <typename TermOp>
+static mlir::LogicalResult
+emitOMPRegionBody(CIRGenFunction &cgf, mlir::Block &block, mlir::Location begin,
+                  mlir::Location end,
+                  llvm::function_ref<mlir::LogicalResult()> emitBody) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  mlir::OpBuilder::InsertionGuard guard(builder);
+  builder.setInsertionPointToEnd(&block);
+
+  CIRGenFunction::LexicalScope ls{cgf, begin, &block};
+  mlir::LogicalResult res = emitBody();
+  ls.forceCleanup();
+  TermOp::create(builder, end);
+  return res;
+}
+
 template <typename DirectiveTy>
 static mlir::LogicalResult
 emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
@@ -79,16 +101,6 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
   CIRGenBuilderTy &builder = cgf.getBuilder();
   CIRGenModule &cgm = cgf.getCIRGenModule();
 
-  auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
-  if (!omp::isLastItemInQueue(item, queue))
-    parallelOp.setCombined(true);
-
-  mlir::Block &block = parallelOp.getRegion().emplaceBlock();
-  mlir::OpBuilder::InsertionGuard guard(builder);
-  builder.setInsertionPointToEnd(&block);
-
-  CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
-
   // hasCancel() reports a nested cancel/cancellation point directive in the
   // body, not a clause on `parallel`, so it can't be an emitParallelClauses()
   // NYI check.
@@ -100,9 +112,34 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
   assert(!s.getTaskReductionRefExpr() &&
          "reduction(task: ...) should already be rejected as NYI");
 
-  mlir::LogicalResult res = emitBody();
-  mlir::omp::TerminatorOp::create(builder, end);
-  return res;
+  auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
+  if (!omp::isLastItemInQueue(item, queue))
+    parallelOp.setCombined(true);
+
+  mlir::Block &block = parallelOp.getRegion().emplaceBlock();
+  return emitOMPRegionBody<mlir::omp::TerminatorOp>(cgf, block, begin, end,
+                                                    emitBody);
+}
+
+/// Checks, emits clauses for, and emits an omp.parallel for the `parallel`
+/// directive at \p parallelItem, deferring to \p emitBody for the region's
+/// contents.
+template <typename DirectiveTy>
+static mlir::LogicalResult emitParallel(
+    CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue,
+    omp::ConstructQueue::const_iterator parallelItem, mlir::Location begin,
+    mlir::Location end, llvm::function_ref<mlir::LogicalResult()> emitBody) {
+  if (mlir::failed(checkSynthesizedClauses(cgf, s, parallelItem)))
+    return mlir::failure();
+
+  mlir::omp::ParallelOperands parallelOps;
+  if (mlir::failed(emitParallelClauses(cgf, cgf.getCIRGenModule(),
+                                       cgf.getBuilder(), begin,
+                                       parallelItem->clauses, parallelOps)))
+    return mlir::failure();
+
+  return emitParallelOp(cgf, s, queue, parallelItem, begin, end, parallelOps,
+                        emitBody);
 }
 
 mlir::LogicalResult
@@ -114,24 +151,226 @@ CIRGenFunction::emitOMPParallelDirective(const OMPParallelDirective &s) {
       omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
   omp::ConstructQueue::const_iterator item = queue.begin();
 
-  if (mlir::failed(checkSynthesizedClauses(*this, s, item)))
-    return mlir::failure();
+  return emitParallel(
+      *this, s, queue, item, begin, end, [&]() -> mlir::LogicalResult {
+        // emitStmt() rejects CapturedStmt directly; the
+        // parent construct must unwrap it, so emit the
+        // inner statement instead.
+        const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_parallel);
+        return emitStmt(cs->getCapturedStmt(),
+                        /*useCurrentScope=*/true);
+      });
+}
 
-  mlir::omp::ParallelOperands clauseOps;
-  if (mlir::failed(emitParallelClauses(*this, getCIRGenModule(), builder, begin,
-                                       item->clauses, clauseOps)))
-    return mlir::failure();
+void CIRGenFunction::emitOMPNoLifetimeVarDecl(const VarDecl &d) {
+  llvm::SaveAndRestore<bool> noLifetimeMarkers(shouldEmitLifetimeMarkers,
+                                               false);
+  emitVarDecl(d);
+}
+
+/// Emits a single Sema-generated pre-init statement for an OpenMP loop
+/// directive: a VarDecl's DeclStmt is lowered directly (it has no
+/// initializer CIRGenFunction::emitStmt can handle on its own), everything
+/// else goes through the general statement emitter.
+static mlir::LogicalResult emitLoopPreInit(CIRGenFunction &cgf,
+                                           const Stmt *preInit) {
+  if (const auto *declStmt = dyn_cast<DeclStmt>(preInit)) {
+    for (const Decl *d : declStmt->decls())
+      cgf.emitOMPNoLifetimeVarDecl(cast<VarDecl>(*d));
+    return mlir::success();
+  }
+  return cgf.emitStmt(preInit, /*useCurrentScope=*/true);
+}
+
+/// Emits the Sema-generated pre-init statements for an OpenMP loop directive.
+static mlir::LogicalResult emitLoopPreInits(CIRGenFunction &cgf,
+                                            const Stmt *preInits) {
+  if (!preInits)
+    return mlir::success();
+
+  if (const auto *compound = dyn_cast<CompoundStmt>(preInits)) {
+    for (const Stmt *stmt : compound->body())
+      if (emitLoopPreInit(cgf, stmt).failed())
+        return mlir::failure();
+    return mlir::success();
+  }
+
+  return emitLoopPreInit(cgf, preInits);
+}
+
+/// Emits an omp.loop_nest that iterates a normalized `[0, tripCount)` range
+/// and recomputes the real induction variable each iteration using the given
+/// update expression.
+static mlir::LogicalResult
+emitOMPLoopNest(CIRGenFunction &cgf, const ForStmt &forStmt, mlir::Value lb,
+                mlir::Value ub, mlir::Value step, const VarDecl *ivDecl,
+                const Expr *update) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  mlir::Location loc = cgf.getLoc(forStmt.getSourceRange());
 
-  return emitParallelOp(
-      *this, s, queue, item, begin, end, clauseOps,
+  auto loopNestOp = mlir::omp::LoopNestOp::create(
+      builder, loc, /*collapse_num_loops=*/1, lb, ub, step,
+      /*loop_inclusive=*/false, /*tile_sizes=*/nullptr);
+  mlir::Block &block = loopNestOp.getRegion().emplaceBlock();
+  block.addArgument(lb.getType(), loc);
+
+  return emitOMPRegionBody<mlir::omp::YieldOp>(
+      cgf, block, loc, cgf.getLoc(forStmt.getEndLoc()),
       [&]() -> mlir::LogicalResult {
-        // emitStmt() rejects CapturedStmt directly; the parent construct
-        // must unwrap it, so emit the inner statement instead.
-        const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_parallel);
-        return emitStmt(cs->getCapturedStmt(), /*useCurrentScope=*/true);
+        // Store the normalized counter block argument into the normalized
+        // loop variable's storage location, then let Sema's update expression
+        // recompute the real induction variable from it.
+        mlir::Value iv = block.getArgument(0);
+        Address ivAddr = cgf.getAddrOfLocalVar(ivDecl);
+        mlir::Value civVal =
+            builder.createBuiltinIntCast(loc, iv, ivAddr.getElementType());
+        builder.createStore(loc, civVal, ivAddr);
+        cgf.emitIgnoredExpr(update);
+
+        if (!forStmt.getBody())
+          return mlir::success();
+        return cgf.emitStmt(forStmt.getBody(), /*useCurrentScope=*/true);
       });
 }
 
+/// Evaluates the clauses allowed on the `for` directive; none are supported
+/// yet, so every eligible clause is reported as NYI.
+static mlir::LogicalResult
+emitWsloopClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
+                  CIRGenBuilderTy &builder, mlir::Location loc,
+                  llvm::ArrayRef<const OMPClause *> clauses,
+                  mlir::omp::WsloopOperands &clauseOps) {
+  OpenMPClauseEmitter ce(cgf, cgm, builder, loc, clauses);
+  return ce.emitNYI</*supported=*/>(
+      /*nyi=*/OpenMPNYIClauseList<
+          OMPAllocateClause, OMPCollapseClause, OMPFirstprivateClause,
+          OMPLastprivateClause, OMPLinearClause, OMPNowaitClause,
+          OMPOrderClause, OMPOrderedClause, OMPPrivateClause,
+          OMPReductionClause, OMPScheduleClause>{},
+      llvm::omp::Directive::OMPD_for);
+}
+
+/// Creates a module-level `omp.private` op (a "privatizer") for a scalar
+/// of CIR type elemTy.
+static mlir::omp::PrivateClauseOp emitScalarPrivatizer(CIRGenModule &cgm,
+                                                       mlir::Location loc,
+                                                       llvm::StringRef baseName,
+                                                       mlir::Type elemTy) {
+  // getGlobalValue() is an O(1) cache lookup, unlike
+  // mlir::SymbolTable::lookupSymbolIn()'s O(n) module scan.
+  unsigned uniquingCounter = 0;
+  llvm::SmallString<64> name = mlir::SymbolTable::generateSymbolName<64>(
+      (baseName + ".privatizer").str(),
+      [&](llvm::StringRef candidate) {
+        return cgm.getGlobalValue(candidate) != nullptr;
+      },
+      uniquingCounter);
+
+  mlir::ModuleOp mod = cgm.getModule();
+  mlir::OpBuilder b(mod.getContext());
+  b.setInsertionPointToEnd(mod.getBody());
+  auto privatizer = mlir::omp::PrivateClauseOp::create(
+      b, loc, mlir::TypeRange{}, b.getStringAttr(name),
+      mlir::TypeAttr::get(elemTy));
+  cgm.insertGlobalSymbol(privatizer);
+  return privatizer;
+}
+
+/// Registers a loop counter as predetermined-private on the associated loop
+/// wrapper op.
+static Address emitLoopPrivateCounter(CIRGenFunction &cgf, CIRGenModule &cgm,
+                                      mlir::Location loc,
+                                      const VarDecl *privateVd,
+                                      mlir::omp::PrivateClauseOps &clauseOps) {
+  cgf.emitOMPNoLifetimeVarDecl(*privateVd);
+  Address moldAddr = cgf.getAddrOfLocalVar(privateVd);
+
+  mlir::omp::PrivateClauseOp privatizer = emitScalarPrivatizer(
+      cgm, loc, privateVd->getName(), moldAddr.getElementType());
+
+  clauseOps.privateVars.push_back(moldAddr.getPointer());
+  clauseOps.privateSyms.push_back(
+      mlir::FlatSymbolRefAttr::get(privatizer.getSymNameAttr()));
+  return moldAddr;
+}
+
+/// Lowers an OMPLoopDirective's `for` directive to an omp.wsloop +
+/// omp.loop_nest.
+static mlir::LogicalResult
+emitOMPWorksharingLoop(CIRGenFunction &cgf, const OMPLoopDirective &s,
+                       omp::ConstructQueue::const_iterator item) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  CIRGenModule &cgm = cgf.getCIRGenModule();
+  mlir::Location loc = cgf.getLoc(s.getBeginLoc());
+
+  if (mlir::failed(checkSynthesizedClauses(cgf, s, item)))
+    return mlir::failure();
+
+  mlir::omp::WsloopOperands clauseOps;
+  if (emitWsloopClauses(cgf, cgm, builder, loc, item->clauses, clauseOps)
+          .failed())
+    return mlir::failure();
+
+  const CapturedStmt *capturedStmt = s.getInnermostCapturedStmt();
+  const auto *forStmt = cast<ForStmt>(capturedStmt->getCapturedStmt());
+
+  if (emitLoopPreInits(cgf, s.getPreInits()).failed())
+    return mlir::failure();
+
+  // Allocate storage for Sema's normalized 0-based loop counter.
+  const auto *ivDecl =
+      cast<VarDecl>(cast<DeclRefExpr>(s.getIterationVariable())->getDecl());
+  cgf.emitOMPNoLifetimeVarDecl(*ivDecl);
+  // Each counter is rebound below to the loop wrapper's private block
+  // argument; these restore the counters' original addresses when this
+  // function returns, so code after the loop doesn't refer to the region.
+  llvm::SmallVector<CIRGenFunction::DeclMapRevertingRAII, 4> counterShadows;
+  llvm::SmallVector<std::pair<const VarDecl *, Address>, 4> counterMolds;
+  counterShadows.reserve(s.counters().size());
+  counterMolds.reserve(s.counters().size());
+  for (auto [counter, privateCounter] :
+       llvm::zip_equal(s.counters(), s.private_counters())) {
+    const auto *vd = cast<VarDecl>(cast<DeclRefExpr>(counter)->getDecl());
+    const auto *privateVd =
+        cast<VarDecl>(cast<DeclRefExpr>(privateCounter)->getDecl());
+    counterShadows.emplace_back(cgf, vd);
+    counterMolds.emplace_back(
+        vd, emitLoopPrivateCounter(cgf, cgm, loc, privateVd, clauseOps));
+  }
+
+  mlir::Value tripCountCir = cgf.emitScalarExpr(s.getNumIterations());
+  auto cirIntType = mlir::cast<cir::IntType>(tripCountCir.getType());
+  mlir::Value zero = builder.createBuiltinIntCast(
+      loc, builder.getConstInt(loc, cirIntType, 0));
+  mlir::Value one = builder.createBuiltinIntCast(
+      loc, builder.getConstInt(loc, cirIntType, 1));
+  mlir::Value tripCount = builder.createBuiltinIntCast(loc, tripCountCir);
+
+  auto wsloopOp = mlir::omp::WsloopOp::create(builder, loc, clauseOps);
+  mlir::Block *innerBlock = new mlir::Block();
+  // Ensures the block arguments match BlockArgOpenMPOpInterface's expected
+  // order.
+  for (auto &counterMold : counterMolds)
+    innerBlock->addArgument(counterMold.second.getPointer().getType(), loc);
+  wsloopOp.getRegion().push_back(innerBlock);
+
+  mlir::OpBuilder::InsertionGuard guard(builder);
+  builder.setInsertionPointToStart(innerBlock);
+
+  // Bind each counter to its private block argument.
+  for (unsigned idx = 0; idx < counterMolds.size(); ++idx) {
+    const VarDecl *vd = counterMolds[idx].first;
+    Address moldAddr = counterMolds[idx].second;
+    Address privAddr(innerBlock->getArgument(idx), moldAddr.getElementType(),
+                     moldAddr.getAlignment());
+    cgf.replaceAddrOfLocalVar(vd, privAddr);
+    cgf.symbolTable.insert(vd, privAddr.getPointer());
+  }
+
+  return emitOMPLoopNest(cgf, *forStmt, zero, tripCount, one, ivDecl,
+                         s.updates()[0]);
+}
+
 mlir::LogicalResult
 CIRGenFunction::emitOMPTaskwaitDirective(const OMPTaskwaitDirective &s) {
   getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPTaskwaitDirective");
@@ -181,8 +420,9 @@ CIRGenFunction::emitOMPFuseDirective(const OMPFuseDirective &s) {
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPForDirective(const OMPForDirective &s) {
-  getCIRGenModule().errorNYI(s.getSourceRange(), "OpenMP OMPForDirective");
-  return mlir::failure();
+  omp::ConstructQueue queue =
+      omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+  return emitOMPWorksharingLoop(*this, s, queue.begin());
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPForSimdDirective(const OMPForSimdDirective &s) {
@@ -216,9 +456,22 @@ CIRGenFunction::emitOMPCriticalDirective(const OMPCriticalDirective &s) {
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPParallelForDirective(const OMPParallelForDirective &s) {
-  getCIRGenModule().errorNYI(s.getSourceRange(),
-                             "OpenMP OMPParallelForDirective");
-  return mlir::failure();
+  mlir::Location begin = getLoc(s.getBeginLoc());
+  mlir::Location end = getLoc(s.getEndLoc());
+
+  omp::ConstructQueue queue =
+      omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+  omp::ConstructQueue::const_iterator parallelItem = queue.begin();
+  assert(parallelItem->id == llvm::omp::OMPD_parallel &&
+         "expected 'parallel' to be the outermost directive");
+
+  return emitParallel(
+      *this, s, queue, parallelItem, begin, end, [&]() -> mlir::LogicalResult {
+        omp::ConstructQueue::const_iterator forItem = std::next(parallelItem);
+        assert(forItem != queue.end() && forItem->id == llvm::omp::OMPD_for &&
+               "expected a 'for' directive nested in 'parallel'");
+        return emitOMPWorksharingLoop(*this, s, forItem);
+      });
 }
 mlir::LogicalResult CIRGenFunction::emitOMPParallelForSimdDirective(
     const OMPParallelForSimdDirective &s) {
@@ -365,11 +618,6 @@ emitTargetOp(CIRGenFunction &cgf, const DirectiveTy &s,
   for (mlir::Value mapVar : clauseOps.mapVars)
     block.addArgument(mapVar.getType(), begin);
 
-  mlir::OpBuilder::InsertionGuard guard(builder);
-  builder.setInsertionPointToEnd(&block);
-
-  CIRGenFunction::LexicalScope ls{cgf, begin, builder.getInsertionBlock()};
-
   llvm::SmallVector<std::pair<const VarDecl *, Address>> savedAddrs;
   for (auto [idx, vd] : llvm::enumerate(mapSyms)) {
     Address origAddr = cgf.getAddrOfLocalVar(vd);
@@ -378,8 +626,8 @@ emitTargetOp(CIRGenFunction &cgf, const DirectiveTy &s,
     cgf.replaceAddrOfLocalVar(vd, Address(blockArg, origAddr.getAlignment()));
   }
 
-  mlir::LogicalResult res = emitBody();
-  mlir::omp::TerminatorOp::create(builder, end);
+  mlir::LogicalResult res = emitOMPRegionBody<mlir::omp::TerminatorOp>(
+      cgf, block, begin, end, emitBody);
 
   for (auto &[vd, addr] : savedAddrs)
     cgf.replaceAddrOfLocalVar(vd, addr);
@@ -387,6 +635,28 @@ emitTargetOp(CIRGenFunction &cgf, const DirectiveTy &s,
   return res;
 }
 
+/// Checks, emits clauses for, and emits an omp.target for the `target`
+/// directive at \p targetItem, deferring to \p emitBody for the region's
+/// contents.
+template <typename DirectiveTy>
+static mlir::LogicalResult emitTarget(
+    CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue,
+    omp::ConstructQueue::const_iterator targetItem, mlir::Location begin,
+    mlir::Location end, llvm::function_ref<mlir::LogicalResult()> emitBody) {
+  if (mlir::failed(checkSynthesizedClauses(cgf, s, targetItem)))
+    return mlir::failure();
+
+  mlir::omp::TargetExtOperands targetOps;
+  llvm::SmallVector<const VarDecl *> mapSyms;
+  if (mlir::failed(emitTargetClauses(cgf, cgf.getCIRGenModule(),
+                                     cgf.getBuilder(), begin,
+                                     targetItem->clauses, targetOps, mapSyms)))
+    return mlir::failure();
+
+  return emitTargetOp(cgf, s, queue, targetItem, begin, end, targetOps, mapSyms,
+                      emitBody);
+}
+
 mlir::LogicalResult
 CIRGenFunction::emitOMPTargetDirective(const OMPTargetDirective &s) {
   mlir::Location begin = getLoc(s.getBeginLoc());
@@ -396,22 +666,12 @@ CIRGenFunction::emitOMPTargetDirective(const OMPTargetDirective &s) {
       omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
   omp::ConstructQueue::const_iterator item = queue.begin();
 
-  if (mlir::failed(checkSynthesizedClauses(*this, s, item)))
-    return mlir::failure();
-
-  mlir::omp::TargetExtOperands clauseOps;
-  llvm::SmallVector<const VarDecl *> mapSyms;
-  if (mlir::failed(emitTargetClauses(*this, getCIRGenModule(), builder, begin,
-                                     item->clauses, clauseOps, mapSyms)))
-    return mlir::failure();
-
-  return emitTargetOp(*this, s, queue, item, begin, end, clauseOps, mapSyms,
-                      [&]() -> mlir::LogicalResult {
-                        const CapturedStmt *cs =
-                            s.getCapturedStmt(llvm::omp::OMPD_target);
-                        return emitStmt(cs->getCapturedStmt(),
-                                        /*useCurrentScope=*/true);
-                      });
+  return emitTarget(
+      *this, s, queue, item, begin, end, [&]() -> mlir::LogicalResult {
+        const CapturedStmt *cs = s.getCapturedStmt(llvm::omp::OMPD_target);
+        return emitStmt(cs->getCapturedStmt(),
+                        /*useCurrentScope=*/true);
+      });
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPTeamsDirective(const OMPTeamsDirective &s) {
@@ -456,49 +716,55 @@ mlir::LogicalResult CIRGenFunction::emitOMPTargetParallelDirective(
       omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
   omp::ConstructQueue::const_iterator targetItem = queue.begin();
   assert(targetItem->id == llvm::omp::OMPD_target &&
-         "expected 'target' to be the outermost leaf");
-
-  if (mlir::failed(checkSynthesizedClauses(*this, s, targetItem)))
-    return mlir::failure();
-
-  mlir::omp::TargetExtOperands targetOps;
-  llvm::SmallVector<const VarDecl *> mapSyms;
-  if (mlir::failed(emitTargetClauses(*this, getCIRGenModule(), builder, begin,
-                                     targetItem->clauses, targetOps, mapSyms)))
-    return mlir::failure();
+         "expected 'target' to be the outermost directive");
 
-  return emitTargetOp(
-      *this, s, queue, targetItem, begin, end, targetOps, mapSyms,
-      [&]() -> mlir::LogicalResult {
+  return emitTarget(
+      *this, s, queue, targetItem, begin, end, [&]() -> mlir::LogicalResult {
         omp::ConstructQueue::const_iterator parallelItem =
             std::next(targetItem);
         assert(parallelItem != queue.end() &&
                parallelItem->id == llvm::omp::OMPD_parallel &&
-               "expected a 'parallel' leaf nested in 'target'");
-
-        if (mlir::failed(checkSynthesizedClauses(*this, s, parallelItem)))
-          return mlir::failure();
-
-        mlir::omp::ParallelOperands parallelOps;
-        if (mlir::failed(emitParallelClauses(*this, getCIRGenModule(), builder,
-                                             begin, parallelItem->clauses,
-                                             parallelOps)))
-          return mlir::failure();
-
-        return emitParallelOp(*this, s, queue, parallelItem, begin, end,
-                              parallelOps, [&]() -> mlir::LogicalResult {
-                                const CapturedStmt *cs =
-                                    s.getCapturedStmt(llvm::omp::OMPD_parallel);
-                                return emitStmt(cs->getCapturedStmt(),
-                                                /*useCurrentScope=*/true);
-                              });
+               "expected a 'parallel' directive nested in 'target'");
+
+        return emitParallel(*this, s, queue, parallelItem, begin, end,
+                            [&]() -> mlir::LogicalResult {
+                              const CapturedStmt *cs =
+                                  s.getCapturedStmt(llvm::omp::OMPD_parallel);
+                              return emitStmt(cs->getCapturedStmt(),
+                                              /*useCurrentScope=*/true);
+                            });
       });
 }
 mlir::LogicalResult CIRGenFunction::emitOMPTargetParallelForDirective(
     const OMPTargetParallelForDirective &s) {
-  getCIRGenModule().errorNYI(s.getSourceRange(),
-                             "OpenMP OMPTargetParallelForDirective");
-  return mlir::failure();
+  mlir::Location begin = getLoc(s.getBeginLoc());
+  mlir::Location end = getLoc(s.getEndLoc());
+
+  omp::ConstructQueue queue =
+      omp::buildConstructQueue(getContext().getLangOpts().OpenMP, s);
+  omp::ConstructQueue::const_iterator targetItem = queue.begin();
+  assert(targetItem->id == llvm::omp::OMPD_target &&
+         "expected 'target' to be the outermost directive");
+
+  return emitTarget(
+      *this, s, queue, targetItem, begin, end, [&]() -> mlir::LogicalResult {
+        omp::ConstructQueue::const_iterator parallelItem =
+            std::next(targetItem);
+        assert(parallelItem != queue.end() &&
+               parallelItem->id == llvm::omp::OMPD_parallel &&
+               "expected a 'parallel' directive nested in 'target'");
+
+        return emitParallel(
+            *this, s, queue, parallelItem, begin, end,
+            [&]() -> mlir::LogicalResult {
+              omp::ConstructQueue::const_iterator forItem =
+                  std::next(parallelItem);
+              assert(forItem != queue.end() &&
+                     forItem->id == llvm::omp::OMPD_for &&
+                     "expected a 'for' directive nested in 'parallel'");
+              return emitOMPWorksharingLoop(*this, s, forItem);
+            });
+      });
 }
 mlir::LogicalResult
 CIRGenFunction::emitOMPTaskLoopDirective(const OMPTaskLoopDirective &s) {
diff --git a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
index 8a54faaee5129b..c4a07c0185d091 100644
--- a/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp
@@ -70,11 +70,14 @@ void walkRegionSkipping(
 /// until its nested structured ops are flat.
 ///
 /// CaseOps are excluded because they are structural children of SwitchOp and
-/// are handled by the SwitchOp flattening pattern.
+/// are handled by the SwitchOp flattening pattern. Ops from other dialects
+/// (e.g. OpenMP and OpenACC constructs) keep their regions after this pass, so
+/// they never block their parent; CIR ops nested inside them are still found.
 static bool hasNestedOpsToFlatten(mlir::Region &region) {
   return region
       .walk([](mlir::Operation *op) {
-        if (op->getNumRegions() > 0 && !isa<cir::CaseOp>(op))
+        if (op->getNumRegions() > 0 && !isa<cir::CaseOp>(op) &&
+            isa<cir::CIRDialect>(op->getDialect()))
           return mlir::WalkResult::interrupt();
         return mlir::WalkResult::advance();
       })
diff --git a/clang/test/CIR/CodeGenOpenMP/for-loop-forms.c b/clang/test/CIR/CodeGenOpenMP/for-loop-forms.c
new file mode 100644
index 00000000000000..5247dfc9848ca8
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/for-loop-forms.c
@@ -0,0 +1,341 @@
+// RUN: %clang_cc1 -fopenmp -emit-cir -fclangir %s -o - | FileCheck %s --check-prefix=CIR
+
+void during(int);
+int getLB(void);
+
+// All the loop forms below normalize to an omp.loop_nest iterating a
+// `[0, tripCount)` range, where tripCount is computed from the original
+// bounds/step. With constant bounds, tripCount is computed as
+// `(hi - (lo - step + 1)) / step`, where `hi`/`lo` are the larger/smaller
+// bound and `step` is the step's magnitude.
+
+// Decreasing loop: `i > 0; i--` (induction variable on the left, plain
+// unary decrement).
+void dec_unary() {
+  // CIR: cir.func{{.*}}@{{.*}}dec_unary
+#pragma omp for
+  for (int i = 20; i > 0; i--) {
+    during(i);
+  }
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (20 - (0 - 1 + 1)) / 1 = 20
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[STEP_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[STEP:.*]] = cir.builtin_int_cast %[[STEP_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[STEP]]) {
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // The update expression recomputes `i = 20 - 1 * iv`.
+  // CIR: %[[C20:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR: %[[IV_RELOAD:.*]] = cir.load align(4) %[[IV_ALLOCA]] : !cir.ptr<!s32i>, !s32i
+  // CIR: %[[C1:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[MUL:.*]] = cir.mul nsw %[[IV_RELOAD]], %[[C1]] : !s32i
+  // CIR: %[[SUB:.*]] = cir.sub nsw %[[C20]], %[[MUL]] : !s32i
+  // CIR: cir.store align(4) %[[SUB]], %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+}
+
+// Decreasing loop using compound assignment: `i -= 2`.
+void dec_compound() {
+  // CIR: cir.func{{.*}}@{{.*}}dec_compound
+#pragma omp for
+  for (int i = 20; i > 0; i -= 2) {
+    during(i);
+  }
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (20 - (0 - 2 + 1)) / 2 = 10
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[STEP_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[STEP:.*]] = cir.builtin_int_cast %[[STEP_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[STEP]]) {
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // The update expression recomputes `i = 20 - 2 * iv`.
+  // CIR: %[[C20:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR: %[[IV_RELOAD:.*]] = cir.load align(4) %[[IV_ALLOCA]] : !cir.ptr<!s32i>, !s32i
+  // CIR: %[[C2:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR: %[[MUL:.*]] = cir.mul nsw %[[IV_RELOAD]], %[[C2]] : !s32i
+  // CIR: %[[SUB:.*]] = cir.sub nsw %[[C20]], %[[MUL]] : !s32i
+  // CIR: cir.store align(4) %[[SUB]], %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+}
+
+// Decreasing loop using the `i = i - 2` canonical assignment form.
+void dec_assign_sub() {
+  // CIR: cir.func{{.*}}@{{.*}}dec_assign_sub
+#pragma omp for
+  for (int i = 20; i > 0; i = i - 2) {
+    during(i);
+  }
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (20 - (0 - 2 + 1)) / 2 = 10
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[STEP_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[STEP:.*]] = cir.builtin_int_cast %[[STEP_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[STEP]]) {
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // The update expression recomputes `i = 20 - 2 * iv`.
+  // CIR: %[[C20:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR: %[[IV_RELOAD:.*]] = cir.load align(4) %[[IV_ALLOCA]] : !cir.ptr<!s32i>, !s32i
+  // CIR: %[[C2:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR: %[[MUL:.*]] = cir.mul nsw %[[IV_RELOAD]], %[[C2]] : !s32i
+  // CIR: %[[SUB:.*]] = cir.sub nsw %[[C20]], %[[MUL]] : !s32i
+  // CIR: cir.store align(4) %[[SUB]], %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+}
+
+// Increasing loop using the `i = 2 + i` canonical assignment form.
+void inc_assign_add_rev() {
+  // CIR: cir.func{{.*}}@{{.*}}inc_assign_add_rev
+#pragma omp for
+  for (int i = 0; i < 20; i = 2 + i) {
+    during(i);
+  }
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (20 - (0 - 2 + 1)) / 2 = 10
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[STEP_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[STEP:.*]] = cir.builtin_int_cast %[[STEP_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[STEP]]) {
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // The update expression recomputes `i = 0 + 2 * iv`.
+  // CIR: %[[C0:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[IV_RELOAD:.*]] = cir.load align(4) %[[IV_ALLOCA]] : !cir.ptr<!s32i>, !s32i
+  // CIR: %[[C2:.*]] = cir.const #cir.int<2> : !s32i
+  // CIR: %[[MUL:.*]] = cir.mul nsw %[[IV_RELOAD]], %[[C2]] : !s32i
+  // CIR: %[[ADD:.*]] = cir.add nsw %[[C0]], %[[MUL]] : !s32i
+  // CIR: cir.store align(4) %[[ADD]], %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+}
+
+// Increasing loop with the upper bound on the left of the comparison
+// (`20 > i`).
+void ub_on_left() {
+  // CIR: cir.func{{.*}}@{{.*}}ub_on_left
+#pragma omp for
+  for (int i = 0; 20 > i; i++) {
+    during(i);
+  }
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (20 - (0 - 1 + 1)) / 1 = 20
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<20> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[STEP_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[STEP:.*]] = cir.builtin_int_cast %[[STEP_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[STEP]]) {
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // The update expression recomputes `i = 0 + 1 * iv`.
+  // CIR: %[[C0:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[IV_RELOAD:.*]] = cir.load align(4) %[[IV_ALLOCA]] : !cir.ptr<!s32i>, !s32i
+  // CIR: %[[C1:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[MUL:.*]] = cir.mul nsw %[[IV_RELOAD]], %[[C1]] : !s32i
+  // CIR: %[[ADD:.*]] = cir.add nsw %[[C0]], %[[MUL]] : !s32i
+  // CIR: cir.store align(4) %[[ADD]], %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+}
+
+// Loop with a side-effecting init expression (a function call, `getLB()`).
+void side_effecting_init() {
+  // CIR: cir.func{{.*}}@{{.*}}side_effecting_init
+#pragma omp for
+  for (int i = getLB(); i < 10; i++) {
+    during(i);
+  }
+  // CIR: %[[CAPTURE:.*]] = cir.alloca ".capture_expr." align(4) init : !cir.ptr<!s32i>
+  // CIR-NEXT: %[[LAST_ITER:.*]] = cir.alloca ".capture_expr." align(4) init : !cir.ptr<!u32i>
+  // CIR: %[[CALL:.*]] = cir.call @{{.*}}getLB{{.*}}() : {{.*}} -> !s32i
+  // CIR-NEXT: cir.store{{.*}} %[[CALL]], %[[CAPTURE]]
+  // With a non-constant start, the computation is done in unsigned
+  // arithmetic: lastIter = (10 - lb - 1 + 1) / 1 - 1 is captured, and
+  // tripCount = lastIter + 1.
+  // CIR-NEXT: %[[UB:.*]] = cir.const #cir.int<10> : !u32i
+  // CIR-NEXT: %[[LB:.*]] = cir.load{{.*}} %[[CAPTURE]] : !cir.ptr<!s32i>, !s32i
+  // CIR-NEXT: %[[LB_U:.*]] = cir.cast integral %[[LB]] : !s32i -> !u32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub %[[UB]], %[[LB_U]] : !u32i
+  // CIR-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !u32i
+  // CIR-NEXT: %[[SPAN_M1:.*]] = cir.sub %[[SPAN]], %[[ONE_A]] : !u32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !u32i
+  // CIR-NEXT: %[[SPAN_ADJ:.*]] = cir.add %[[SPAN_M1]], %[[STEP_A]] : !u32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !u32i
+  // CIR-NEXT: %[[NUM_ITERS:.*]] = cir.div %[[SPAN_ADJ]], %[[STEP_B]] : !u32i
+  // CIR-NEXT: %[[ONE_B:.*]] = cir.const #cir.int<1> : !u32i
+  // CIR-NEXT: %[[LAST:.*]] = cir.sub %[[NUM_ITERS]], %[[ONE_B]] : !u32i
+  // CIR-NEXT: cir.store{{.*}} %[[LAST]], %[[LAST_ITER]] : !u32i, !cir.ptr<!u32i>
+  // CIR-NEXT: %[[LAST_RELOAD:.*]] = cir.load{{.*}} %[[LAST_ITER]] : !cir.ptr<!u32i>, !u32i
+  // CIR-NEXT: %[[ONE_C:.*]] = cir.const #cir.int<1> : !u32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.add %[[LAST_RELOAD]], %[[ONE_C]] : !u32i
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !u32i -> i32
+  // CIR-NOT: cir.call @{{.*}}getLB
+  // CIR: omp.wsloop private(@{{.*}} %{{.*}} -> %{{.*}} : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%{{.*}}) : i32 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+  // CIR-NOT: cir.call @{{.*}}getLB
+  // CIR: cir.call @{{.*}}during
+}
+
+// Loop with a pointer induction variable (`int *p`).
+void pointer_induction_var() {
+  // CIR: cir.func{{.*}}@{{.*}}pointer_induction_var
+  int a[10];
+#pragma omp for
+  for (int *p = a; p < a + 10; ++p) {
+    during(*p);
+  }
+  // CIR: %[[BEGIN:.*]] = cir.alloca ".capture_expr." align(8) init : !cir.ptr<!cir.ptr<!s32i>>
+  // CIR-NEXT: %[[END:.*]] = cir.alloca ".capture_expr." align(8) init : !cir.ptr<!cir.ptr<!s32i>>
+  // CIR-NEXT: %[[LAST_ITER:.*]] = cir.alloca ".capture_expr." align(8) init : !cir.ptr<!s64i>
+  // CIR: %[[P_ALLOCA:.*]] = cir.alloca "p" align(8) : !cir.ptr<!cir.ptr<!s32i>>
+  // The span is the pointer difference between the captured bounds:
+  // lastIter = ((a + 10) - a - 1 + 1) / 1 - 1 is captured, and
+  // tripCount = lastIter + 1.
+  // CIR: %[[END_VAL:.*]] = cir.load{{.*}} %[[END]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+  // CIR-NEXT: %[[BEGIN_VAL:.*]] = cir.load{{.*}} %[[BEGIN]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+  // CIR-NEXT: %[[SPAN:.*]] = cir.ptr_diff %[[END_VAL]], %[[BEGIN_VAL]] : !cir.ptr<!s32i> -> !s64i
+  // CIR-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !s64i
+  // CIR-NEXT: %[[SPAN_M1:.*]] = cir.sub nsw %[[SPAN]], %[[ONE_A]] : !s64i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s64i
+  // CIR-NEXT: %[[SPAN_ADJ:.*]] = cir.add nsw %[[SPAN_M1]], %[[STEP_A]] : !s64i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s64i
+  // CIR-NEXT: %[[NUM_ITERS:.*]] = cir.div %[[SPAN_ADJ]], %[[STEP_B]] : !s64i
+  // CIR-NEXT: %[[ONE_B:.*]] = cir.const #cir.int<1> : !s64i
+  // CIR-NEXT: %[[LAST:.*]] = cir.sub nsw %[[NUM_ITERS]], %[[ONE_B]] : !s64i
+  // CIR-NEXT: cir.store{{.*}} %[[LAST]], %[[LAST_ITER]] : !s64i, !cir.ptr<!s64i>
+  // CIR-NEXT: %[[LAST_RELOAD:.*]] = cir.load{{.*}} %[[LAST_ITER]] : !cir.ptr<!s64i>, !s64i
+  // CIR-NEXT: %[[ONE_C:.*]] = cir.const #cir.int<1> : !s64i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.add nsw %[[LAST_RELOAD]], %[[ONE_C]] : !s64i
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s64i -> i64
+  // CIR: omp.wsloop private(@{{.*}} %[[P_ALLOCA]] -> %[[P_PRIV:.*]] : !cir.ptr<!cir.ptr<!s32i>>) {
+  // CIR-NEXT: omp.loop_nest (%{{.*}}) : i64 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+  // CIR: %[[P_BASE:.*]] = cir.load{{.*}} : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+  // CIR: %[[P_NEXT:.*]] = cir.ptr_stride %[[P_BASE]], %{{.*}} : (!cir.ptr<!s32i>, !s64i) -> !cir.ptr<!s32i>
+  // CIR: cir.store{{.*}} %[[P_NEXT]], %[[P_PRIV]]
+  // CIR: cir.call @{{.*}}during
+}
+
+// Pre-declared induction variable (plain assignment, no DeclStmt) with a
+// `!=` condition.
+void predeclared_var_and_not_equal_cond() {
+  // CIR: cir.func{{.*}}@{{.*}}predeclared_var_and_not_equal_cond
+  int i;
+#pragma omp for
+  for (i = 0; i != 10; i++) {
+    during(i);
+  }
+  // CIR: cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_MOLD:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // A `!=` condition is treated like `<`.
+  // tripCount = (10 - (0 - 1 + 1)) / 1 = 10
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_MOLD]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%{{.*}}) : i32 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+  // CIR: cir.store{{.*}} %{{.*}}, %[[IV_ALLOCA]]
+  // CIR: cir.store{{.*}} %{{.*}}, %[[I_PRIV]]
+  // CIR: cir.call @{{.*}}during
+}
+
+// Same pre-declared counter and `!=` condition, inside a combined
+// `parallel for`.
+void predeclared_var_inside_parallel_for() {
+  // CIR: cir.func{{.*}}@{{.*}}predeclared_var_inside_parallel_for
+  int i;
+#pragma omp parallel for
+  for (i = 0; i != 10; i++) {
+    during(i);
+  }
+  // CIR: cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // CIR: omp.parallel {
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_MOLD:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+  // tripCount = (10 - (0 - 1 + 1)) / 1 = 10
+  // CIR-NEXT: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+  // CIR-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+  // CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE]] : !s32i
+  // CIR-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+  // CIR-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+  // CIR: omp.wsloop private(@{{.*}} %[[I_MOLD]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%{{.*}}) : i32 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+  // CIR: cir.store{{.*}} %{{.*}}, %[[IV_ALLOCA]]
+  // CIR: cir.store{{.*}} %{{.*}}, %[[I_PRIV]]
+  // CIR: cir.call @{{.*}}during
+}
diff --git a/clang/test/CIR/CodeGenOpenMP/for.c b/clang/test/CIR/CodeGenOpenMP/for.c
new file mode 100644
index 00000000000000..14ecd9284a8140
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/for.c
@@ -0,0 +1,211 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-OGCG
+
+void before(int);
+void during(int);
+void after(int);
+
+void emit_simple_for() {
+  int j = 5;
+  before(j);
+#pragma omp parallel
+  {
+#pragma omp for
+    for (int i = 0; i < 10; i++) {
+      during(j);
+    }
+  }
+  after(j);
+}
+
+// The before()/after() calls around the parallel region are identical on
+// both paths.
+//
+// LLVM-LABEL: define{{.*}} void @emit_simple_for()
+// LLVM:         %[[J_SLOT:.*]] = alloca i32, align 4
+// LLVM:         store i32 5, ptr %[[J_SLOT]], align 4
+// LLVM:         %[[J_VAL:.*]] = load i32, ptr %[[J_SLOT]], align 4
+// LLVM-NEXT:    call void @before(i32 noundef %[[J_VAL]])
+
+// CIR packs the captured variable(s) into a struct and passes a single
+// pointer to that struct as the __kmpc_fork_call vararg; OGCG instead
+// passes a direct pointer to each captured variable as its own vararg.
+// The forked function's signature and body differ accordingly (GEP+load
+// from the struct vs a plain by-reference parameter).
+// LLVM-CIR:         %[[GEP0:.*]] = getelementptr { ptr }, ptr %[[STRUCT_ARG:.*]], i32 0, i32 0
+// LLVM-CIR-NEXT:    store ptr %[[J_SLOT]], ptr %[[GEP0]], align 8
+// LLVM-CIR:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[SIMPLE_OUTLINED:[^,]+]], ptr %[[STRUCT_ARG]])
+
+// LLVM-OGCG:        call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[SIMPLE_OUTLINED:[^,]+]], ptr %[[J_SLOT]])
+
+// LLVM:         %[[J_VAL2:.*]] = load i32, ptr %[[J_SLOT]], align 4
+// LLVM-NEXT:    call void @after(i32 noundef %[[J_VAL2]])
+// LLVM-NEXT:    ret void
+
+// The worksharing loop: CIR uses the unsigned-trip-count
+// __kmpc_for_static_init_4u/fini pair and synthesizes a canonical
+// omp_loop.header/cond/body/inc/exit skeleton; OGCG uses the signed
+// __kmpc_for_static_init_4/fini pair with a classic cond.true/false/end
+// clamp and an omp.inner.for.cond/body/inc skeleton.
+//
+// LLVM-CIR:       define internal void @[[SIMPLE_OUTLINED]](
+// LLVM-CIR-SAME:    ptr noalias %{{.*}}, ptr noalias %{{.*}}, ptr noalias nofreeobj %[[STRUCT:.*]])
+// LLVM-CIR:         %[[GEP_J:.*]] = getelementptr { ptr }, ptr %[[STRUCT]], i32 0, i32 0
+// LLVM-CIR-NEXT:    %[[J_PTR:.*]] = load ptr, ptr %[[GEP_J]], align 8
+// LLVM-CIR-NEXT:    %[[LASTITER:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[LB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[UB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[STRIDE:.*]] = alloca i32, align 4
+// LLVM-CIR:         store i32 0, ptr %[[LB]], align 4
+// LLVM-CIR-NEXT:    store i32 9, ptr %[[UB]], align 4
+// LLVM-CIR-NEXT:    store i32 1, ptr %[[STRIDE]], align 4
+// LLVM-CIR:         %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-CIR-SAME:      ptr {{.*}}, i32 %[[TID]], i32 34,
+// LLVM-CIR-SAME:      ptr %[[LASTITER]], ptr %[[LB]], ptr %[[UB]], ptr %[[STRIDE]],
+// LLVM-CIR-SAME:      i32 1, i32 0)
+// LLVM-CIR:         %[[IV:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT:[^,]+]], %{{.*}} ]
+// LLVM-CIR:         %[[LOOP_CMP:.*]] = icmp ult i32 %[[IV]], %{{.*}}
+// LLVM-CIR-NEXT:    br i1 %[[LOOP_CMP]], label %[[LOOP_BODY:[^,]+]], label %[[LOOP_EXIT:[^ ,]+]]
+// LLVM-CIR:       {{^}}[[LOOP_EXIT]]:
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID]])
+// LLVM-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-CIR:       {{^}}[[LOOP_BODY]]:
+// LLVM-CIR:         %[[J_VAL3:.*]] = load i32, ptr %[[J_PTR]], align 4
+// LLVM-CIR-NEXT:    call void @during(i32 noundef %[[J_VAL3]])
+// LLVM-CIR:         %[[NEXT]] = add nuw i32 %[[IV]], 1
+// LLVM-CIR:         ret void
+
+// LLVM-OGCG:       define internal void @[[SIMPLE_OUTLINED]](
+// LLVM-OGCG-SAME:    ptr noalias noundef %[[GTID:[^,]+]], ptr noalias noundef %{{[^,]+}},
+// LLVM-OGCG-SAME:    ptr nofreeobj noundef nonnull align 4 dereferenceable(4) %[[J:[^)]+]])
+// LLVM-OGCG:         %[[GTID_ADDR:.*]] = alloca ptr, align 8
+// LLVM-OGCG-NEXT:    %{{.*}} = alloca ptr, align 8
+// LLVM-OGCG-NEXT:    %[[J_ADDR:.*]] = alloca ptr, align 8
+// LLVM-OGCG:         store ptr %[[GTID]], ptr %[[GTID_ADDR]], align 8
+// LLVM-OGCG:         store ptr %[[J]], ptr %[[J_ADDR]], align 8
+// LLVM-OGCG:         %[[J_LOADED:.*]] = load ptr, ptr %[[J_ADDR]], align 8
+// LLVM-OGCG-NEXT:    store i32 0, ptr %[[OMP_LB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 9, ptr %[[OMP_UB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 1, ptr %[[OMP_STRIDE:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 0, ptr %[[OMP_IS_LAST:.*]], align 4
+// LLVM-OGCG:         %[[GTID_VAL_PTR:.*]] = load ptr, ptr %[[GTID_ADDR]], align 8
+// LLVM-OGCG-NEXT:    %[[TID2:.*]] = load i32, ptr %[[GTID_VAL_PTR]], align 4
+// LLVM-OGCG-NEXT:    call void @__kmpc_for_static_init_4(
+// LLVM-OGCG-SAME:      ptr {{.*}}, i32 %[[TID2]], i32 34,
+// LLVM-OGCG-SAME:      ptr %[[OMP_IS_LAST]], ptr %[[OMP_LB]], ptr %[[OMP_UB]], ptr %[[OMP_STRIDE]],
+// LLVM-OGCG-SAME:      i32 1, i32 1)
+// LLVM-OGCG:         %[[UB_VAL:.*]] = load i32, ptr %[[OMP_UB]], align 4
+// LLVM-OGCG-NEXT:    %[[CMP:.*]] = icmp sgt i32 %[[UB_VAL]], 9
+// LLVM-OGCG-NEXT:    br i1 %[[CMP]], label %[[COND_TRUE:[^,]+]], label %[[COND_FALSE:[^ ,]+]]
+// LLVM-OGCG:         %[[COND:.*]] = phi i32 [ 9, %[[COND_TRUE]] ], [ %{{.*}}, %[[COND_FALSE]] ]
+// LLVM-OGCG-NEXT:    store i32 %[[COND]], ptr %[[OMP_UB]], align 4
+// LLVM-OGCG:         %[[CMP1:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[CMP1]], label %[[FOR_BODY:[^,]+]], label %{{.*}}
+// LLVM-OGCG:       {{^}}[[FOR_BODY]]:
+// LLVM-OGCG:         %[[J_VAL3:.*]] = load i32, ptr %[[J_LOADED]], align 4
+// LLVM-OGCG-NEXT:    call void @during(i32 noundef %[[J_VAL3]])
+// LLVM-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID2]])
+// LLVM-OGCG-NEXT:    call void @__kmpc_barrier(ptr {{.*}}, i32 %[[TID2]])
+// LLVM-OGCG-NEXT:    ret void
+
+void emit_for_with_vars() {
+  int j = 5;
+  before(j);
+#pragma omp parallel
+  {
+    int lb = 1;
+    long ub = 10;
+    short step = 1;
+#pragma omp for
+    for (int i = 0; i < ub; i = i + step) {
+      during(j);
+    }
+  }
+  after(j);
+}
+
+// LLVM-LABEL: define{{.*}} void @emit_for_with_vars()
+
+// Same CIR-vs-OGCG capture-ABI split as emit_simple_for above.
+// LLVM-CIR:      %[[STRUCT_ARG2:.*]] = alloca { ptr }, align 8
+
+// LLVM:         call void @before(i32
+
+// LLVM-CIR:      call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[VARS_OUTLINED:[^,]+]], ptr %[[STRUCT_ARG2]])
+
+// LLVM-OGCG:     call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[VARS_OUTLINED:[^,]+]], ptr {{.*}})
+
+// LLVM:         call void @after(i32
+// LLVM-NEXT:    ret void
+
+// Loop bounds aren't known until runtime (`ub` and `step` are variables, not
+// constants), so both paths have to compute the number of iterations on the
+// fly. We check the loop setup, during() call (one per iteration), and the
+// matching init/fini/barrier call betwen CIR and OGCG.
+//
+// LLVM-CIR:       define internal void @[[VARS_OUTLINED]](
+// LLVM-CIR:         store i32 1, ptr %[[LB:.*]], align 4
+// LLVM-CIR-NEXT:    store i64 10, ptr %[[UB:.*]], align 8
+// LLVM-CIR-NEXT:    store i16 1, ptr %[[STEP:.*]], align 2
+// LLVM-CIR:         %{{.*}} = sext i16 %{{.*}} to i32
+// LLVM-CIR:         %{{.*}} = sext i32 %{{.*}} to i64
+// LLVM-CIR:         %{{.*}} = sext i16 %{{.*}} to i64
+// LLVM-CIR:         %{{.*}} = sdiv i64
+// LLVM-CIR:         %[[TRIPCOUNT:.*]] = select i1 %{{.*}}, i32 0, i32 %{{.*}}
+// LLVM-CIR:         store i32 0, ptr %[[LB2:.*]], align 4
+// LLVM-CIR-NEXT:    %[[TRIPM1:.*]] = sub i32 %[[TRIPCOUNT]], 1
+// LLVM-CIR-NEXT:    store i32 %[[TRIPM1]], ptr %[[UB2:.*]], align 4
+// LLVM-CIR-NEXT:    store i32 1, ptr %[[STRIDE2:.*]], align 4
+// LLVM-CIR:         %[[TID3:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-CIR-SAME:      ptr {{.*}}, i32 %[[TID3]], i32 34,
+// LLVM-CIR-SAME:      ptr %{{.*}}, ptr %[[LB2]], ptr %[[UB2]], ptr %[[STRIDE2]],
+// LLVM-CIR-SAME:      i32 1, i32 0)
+// LLVM-CIR:         %[[IV2:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT2:[^,]+]], %{{.*}} ]
+// LLVM-CIR:         %[[LOOP_CMP2:.*]] = icmp ult i32 %[[IV2]], %{{.*}}
+// LLVM-CIR-NEXT:    br i1 %[[LOOP_CMP2]], label %[[LOOP_BODY2:[^,]+]], label %[[LOOP_EXIT2:[^ ,]+]]
+// LLVM-CIR:       {{^}}[[LOOP_EXIT2]]:
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID3]])
+// LLVM-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-CIR:       {{^}}[[LOOP_BODY2]]:
+// LLVM-CIR:         call void @during(i32
+// LLVM-CIR:         %[[NEXT2]] = add nuw i32 %[[IV2]], 1
+// LLVM-CIR:         ret void
+
+// LLVM-OGCG:       define internal void @[[VARS_OUTLINED]](
+// LLVM-OGCG-SAME:    ptr noalias noundef %{{[^,]+}}, ptr noalias noundef %{{[^,]+}},
+// LLVM-OGCG-SAME:    ptr nofreeobj noundef nonnull align 4 dereferenceable(4) %{{[^)]+}})
+// LLVM-OGCG:         %[[LB_VAR:.*]] = alloca i32, align 4
+// LLVM-OGCG-NEXT:    %[[UB_VAR:.*]] = alloca i64, align 8
+// LLVM-OGCG-NEXT:    %[[STEP_VAR:.*]] = alloca i16, align 2
+// LLVM-OGCG:         store i32 1, ptr %[[LB_VAR]], align 4
+// LLVM-OGCG-NEXT:    store i64 10, ptr %[[UB_VAR]], align 8
+// LLVM-OGCG-NEXT:    store i16 1, ptr %[[STEP_VAR]], align 2
+// LLVM-OGCG:         %{{.*}} = sext i16 %{{.*}} to i32
+// LLVM-OGCG:         %{{.*}} = sext i32 %{{.*}} to i64
+// LLVM-OGCG:         %{{.*}} = sext i16 %{{.*}} to i64
+// LLVM-OGCG:         %{{.*}} = sdiv i64
+// LLVM-OGCG:         %[[PRECOND:.*]] = icmp slt i64 0, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[PRECOND]], label %[[PRECOND_THEN:[^,]+]], label %[[PRECOND_END:[^ ,]+]]
+// LLVM-OGCG:       {{^}}[[PRECOND_THEN]]:
+// LLVM-OGCG-NEXT:    store i32 0, ptr %[[OMP_LB2:.*]], align 4
+// LLVM-OGCG:         store i32 1, ptr %[[OMP_STRIDE2:.*]], align 4
+// LLVM-OGCG:         call void @__kmpc_for_static_init_4(
+// LLVM-OGCG-SAME:      ptr {{.*}}, i32 %{{.*}}, i32 34,
+// LLVM-OGCG-SAME:      ptr %{{.*}}, ptr %[[OMP_LB2]], ptr %[[OMP_UB2:[^,]+]], ptr %[[OMP_STRIDE2]],
+// LLVM-OGCG-SAME:      i32 1, i32 1)
+// LLVM-OGCG:         %[[CMP9:.*]] = icmp sgt i32 %{{.*}}, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[CMP9]], label %[[COND_TRUE2:[^,]+]], label %[[COND_FALSE2:[^ ,]+]]
+// LLVM-OGCG:         %[[COND2:.*]] = phi i32 [ %{{.*}}, %[[COND_TRUE2]] ], [ %{{.*}}, %[[COND_FALSE2]] ]
+// LLVM-OGCG-NEXT:    store i32 %[[COND2]], ptr %[[OMP_UB2]], align 4
+// LLVM-OGCG:         %[[CMP11:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[CMP11]], label %[[FOR_BODY2:[^,]+]], label %{{.*}}
+// LLVM-OGCG:       {{^}}[[FOR_BODY2]]:
+// LLVM-OGCG:         call void @during(i32
+// LLVM-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %{{.*}})
+// LLVM-OGCG:       {{^}}[[PRECOND_END]]:
+// LLVM-OGCG:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-OGCG-NEXT:    ret void
diff --git a/clang/test/CIR/CodeGenOpenMP/parallel-for.c b/clang/test/CIR/CodeGenOpenMP/parallel-for.c
new file mode 100644
index 00000000000000..1cda528beb9d2c
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/parallel-for.c
@@ -0,0 +1,117 @@
+// RUN: %clang_cc1 -fopenmp -emit-cir -fclangir %s -o - | FileCheck %s --check-prefix=CIR
+
+// Lowering to LLVM IR: the fork_call wrapper is identical on both paths.
+// The worksharing-loop body itself uses a different runtime API and
+// control-flow shape on each path.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM,LLVM-OGCG
+
+void during(int);
+
+void parallel_for() {
+  // CIR: cir.func{{.*}}@{{.*}}parallel_for
+#pragma omp parallel for
+  for (int i = 0; i < 10; i++) {
+    during(i);
+  }
+
+  // CIR: omp.parallel {
+
+  // CIR: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+  // CIR: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+
+  // The loop bounds are normalized to a `[0, tripCount)` range; tripCount is
+  // computed from the original bounds/step.
+  // CIR: %[[C10_CIR:.*]] = cir.const #cir.int<10> : !s32i
+  // CIR: %[[C0_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[C1_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[LBM1:.*]] = cir.sub nsw %[[C0_CIR]], %[[C1_CIR]] : !s32i
+  // CIR: %[[C1B:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[LBM1P1:.*]] = cir.add nsw %[[LBM1]], %[[C1B]] : !s32i
+  // CIR: %[[SPAN:.*]] = cir.sub nsw %[[C10_CIR]], %[[LBM1P1]] : !s32i
+  // CIR: %[[C1C:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[C1C]] : !s32i
+  // CIR: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+  // CIR: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+  // CIR: %[[ONE_CIR:.*]] = cir.const #cir.int<1> : !s32i
+  // CIR: %[[ONE:.*]] = cir.builtin_int_cast %[[ONE_CIR]] : !s32i -> i32
+  // CIR: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+
+  // CIR: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+  // CIR-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[ONE]]) {
+
+  // CIR: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+  // CIR: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.store align(4) %{{.*}}, %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+  // CIR: cir.call @{{.*}}during
+
+  // CIR: omp.yield
+  // CIR: }
+  // CIR: }
+  // CIR: omp.terminator
+  // The parallel is a non-innermost leaf of the combined construct.
+  // CIR: } {omp.combined}
+}
+
+// LLVM-LABEL: define {{.*}} void @parallel_for()
+// LLVM:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 0, ptr @[[OUTLINED:[^)]+]])
+
+// The worksharing loop itself: CIR uses the unsigned-trip-count
+// __kmpc_for_static_init_4u/fini pair and synthesizes a canonical
+// omp_loop.header/cond/body/inc/exit skeleton.
+// OGCG uses the signed
+// __kmpc_for_static_init_4/fini pair with a classic cond.true/false/end
+// upper-bound clamp and an omp.inner.for.cond/body/inc skeleton.
+// CIR also emits a trailing barrier after the loop that OGCG omits here,
+// this is an optimization opportunity that the OMP MLIR lowering could
+// also take advantage of in the future.
+//
+// LLVM-CIR:       define internal void @[[OUTLINED]](
+// LLVM-CIR-SAME:    ptr noalias %{{.*}}, ptr noalias %{{.*}})
+// LLVM-CIR:         %[[LASTITER:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[LB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[UB:.*]] = alloca i32, align 4
+// LLVM-CIR-NEXT:    %[[STRIDE:.*]] = alloca i32, align 4
+// LLVM-CIR:         store i32 0, ptr %[[LB]], align 4
+// LLVM-CIR-NEXT:    store i32 9, ptr %[[UB]], align 4
+// LLVM-CIR-NEXT:    store i32 1, ptr %[[STRIDE]], align 4
+// LLVM-CIR:         %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-CIR-SAME:      ptr {{.*}}, i32 %[[TID]], i32 34,
+// LLVM-CIR-SAME:      ptr %[[LASTITER]], ptr %[[LB]], ptr %[[UB]], ptr %[[STRIDE]],
+// LLVM-CIR-SAME:      i32 1, i32 0)
+// LLVM-CIR:         %[[IV:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT:[^,]+]], %{{.*}} ]
+// LLVM-CIR:         %[[LOOP_CMP:.*]] = icmp ult i32 %[[IV]], %{{.*}}
+// LLVM-CIR-NEXT:    br i1 %[[LOOP_CMP]], label %[[LOOP_BODY:[^,]+]], label %[[LOOP_EXIT:[^ ,]+]]
+// LLVM-CIR:       {{^}}[[LOOP_EXIT]]:
+// LLVM-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID]])
+// LLVM-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-CIR:       {{^}}[[LOOP_BODY]]:
+// LLVM-CIR:         call void @during(i32
+// LLVM-CIR:         %[[NEXT]] = add nuw i32 %[[IV]], 1
+// LLVM-CIR:         ret void
+
+// LLVM-OGCG:       define internal void @[[OUTLINED]](
+// LLVM-OGCG-SAME:    ptr noalias noundef %{{[^,]+}}, ptr noalias noundef %{{[^)]+}})
+// LLVM-OGCG:         store i32 0, ptr %[[OMP_LB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 9, ptr %[[OMP_UB:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 1, ptr %[[OMP_STRIDE:.*]], align 4
+// LLVM-OGCG-NEXT:    store i32 0, ptr %[[OMP_IS_LAST:.*]], align 4
+// LLVM-OGCG:         %[[TID2:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-OGCG-NEXT:    call void @__kmpc_for_static_init_4(
+// LLVM-OGCG-SAME:      ptr {{.*}}, i32 %[[TID2]], i32 34,
+// LLVM-OGCG-SAME:      ptr %[[OMP_IS_LAST]], ptr %[[OMP_LB]], ptr %[[OMP_UB]], ptr %[[OMP_STRIDE]],
+// LLVM-OGCG-SAME:      i32 1, i32 1)
+// LLVM-OGCG:         %[[UB_VAL:.*]] = load i32, ptr %[[OMP_UB]], align 4
+// LLVM-OGCG-NEXT:    %[[CMP:.*]] = icmp sgt i32 %[[UB_VAL]], 9
+// LLVM-OGCG-NEXT:    br i1 %[[CMP]], label %[[COND_TRUE:[^,]+]], label %[[COND_FALSE:[^ ,]+]]
+// LLVM-OGCG:         %[[COND:.*]] = phi i32 [ 9, %[[COND_TRUE]] ], [ %{{.*}}, %[[COND_FALSE]] ]
+// LLVM-OGCG-NEXT:    store i32 %[[COND]], ptr %[[OMP_UB]], align 4
+// LLVM-OGCG:         %[[CMP1:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-OGCG-NEXT:    br i1 %[[CMP1]], label %[[FOR_BODY:[^,]+]], label %{{.*}}
+// LLVM-OGCG:       {{^}}[[FOR_BODY]]:
+// LLVM-OGCG:         call void @during(i32
+// LLVM-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID2]])
+// LLVM-OGCG-NEXT:    ret void
diff --git a/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c b/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
new file mode 100644
index 00000000000000..b50eeaec0c4870
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/region-cleanups-O2.c
@@ -0,0 +1,140 @@
+// REQUIRES: amdgpu-registered-target
+
+// At -O1 and above, locals get lifetime markers whose end markers are emitted
+// from a cir.cleanup.scope. These tests check that such cleanup scopes stay
+// inside the OpenMP region they belong to, and that FlattenCFG can flatten a
+// cleanup scope or loop containing an OpenMP construct.
+
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -O2 -emit-cir -fclangir %s -o - | FileCheck %s --check-prefix=CIR
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -O2 -emit-llvm -fclangir %s -o - | FileCheck %s --check-prefix=LLVM
+
+void use(int);
+
+// A local declared in the parallel region is ended inside the region, before
+// the region's terminator.
+void local_in_parallel(int n) {
+#pragma omp parallel
+  {
+    int x = n;
+    use(x);
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_in_parallel
+// CIR:         omp.parallel {
+// CIR:           %[[X:.*]] = cir.alloca "x"
+// CIR:           cir.lifetime.start %[[X]]
+// CIR:           cir.cleanup.scope {
+// CIR:             cir.call @use(
+// CIR:             cir.yield
+// CIR:           } cleanup normal {
+// CIR:             cir.lifetime.end %[[X]]
+// CIR:             cir.yield
+// CIR:           }
+// CIR-NEXT:      omp.terminator
+// CIR-NEXT:    }
+
+// Same for a target region.
+void local_in_target(int n) {
+#pragma omp target map(tofrom : n)
+  {
+    int x = n;
+    n = x + 1;
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_in_target
+// CIR:         omp.target
+// CIR:           %[[X:.*]] = cir.alloca "x"
+// CIR:           cir.lifetime.start %[[X]]
+// CIR:           cir.cleanup.scope {
+// CIR:           } cleanup normal {
+// CIR:             cir.lifetime.end %[[X]]
+// CIR:           }
+// CIR-NEXT:      omp.terminator
+// CIR-NEXT:    }
+
+// A local declared before the construct puts the omp.parallel inside the
+// local's cleanup scope, which must still be flattened.
+int local_before_parallel(void) {
+  int s = 0;
+#pragma omp parallel
+  use(s);
+  return s;
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_before_parallel
+// CIR:         cir.cleanup.scope {
+// CIR:           omp.parallel {
+// CIR:         } cleanup normal {
+// CIR:           cir.lifetime.end
+
+// LLVM-LABEL: define{{.*}} i32 @local_before_parallel(
+// LLVM:         call void @llvm.lifetime.start.p0(ptr {{.*}}%[[S:[0-9]+]])
+// LLVM:         call void {{.*}}@__kmpc_fork_call(
+// LLVM:         call void @llvm.lifetime.end.p0(ptr {{.*}}%[[S]])
+
+// A loop containing an OpenMP construct must also be flattened.
+void parallel_in_loop(int n) {
+  for (int i = 0; i < n; i = i + 1) {
+#pragma omp parallel
+    use(i);
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@parallel_in_loop
+// CIR:         cir.for : cond {
+// CIR:         } body {
+// CIR:           omp.parallel {
+
+// LLVM-LABEL: define{{.*}} void @parallel_in_loop(
+// LLVM:         br i1 {{.*}}, label %[[BODY:.*]], label
+// LLVM:       [[BODY]]:
+// LLVM:         call void {{.*}}@__kmpc_fork_call(
+// LLVM:         br i1 {{.*}}, label %[[BODY]], label
+
+// A local in the loop body is ended inside the omp.loop_nest, before its
+// yield.
+void local_in_for(int *a, int n) {
+#pragma omp for
+  for (int i = 0; i < n; i = i + 1) {
+    int t = i * 2;
+    a[i] = t;
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_in_for
+// CIR:         omp.wsloop
+// CIR:           omp.loop_nest
+// CIR:             %[[T:.*]] = cir.alloca "t"
+// CIR:             cir.lifetime.start %[[T]]
+// CIR:             cir.cleanup.scope {
+// CIR:             } cleanup normal {
+// CIR:               cir.lifetime.end %[[T]]
+// CIR:             }
+// CIR-NEXT:        omp.yield
+
+// Sema's loop helper variables (pre-init captures, the iteration variable and
+// the private counter) are emitted without lifetime markers, so nothing comes
+// between the combined omp.parallel and its omp.wsloop.
+void local_in_parallel_for(int *a, int n) {
+#pragma omp parallel for
+  for (int i = 0; i < n; i = i + 1) {
+    int t = i * 2;
+    a[i] = t;
+  }
+}
+
+// CIR-LABEL: cir.func{{.*}}@local_in_parallel_for
+// CIR:         omp.parallel {
+// CIR-NOT:       cir.lifetime.start
+// CIR-NOT:       cir.cleanup.scope
+// CIR:           omp.wsloop
+// CIR:             omp.loop_nest
+// CIR:               cir.lifetime.start
+// CIR:               cir.cleanup.scope {
+
+// LLVM-LABEL: define{{.*}} void @local_in_parallel_for(
+// LLVM:         call void {{.*}}@__kmpc_fork_call(
diff --git a/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c b/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c
new file mode 100644
index 00000000000000..f0112b773cc869
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c
@@ -0,0 +1,374 @@
+// REQUIRES: amdgpu-registered-target
+
+// Host compilation (x86 host, AMDGPU offload target).
+// RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR-HOST
+
+// Device compilation (AMDGPU): allocas live in the private address space.
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -emit-cir -fclangir %s -o - \
+// RUN:   | FileCheck %s --check-prefix=CIR-DEVICE
+
+// Host-side lowering to LLVM IR: mostly identical between CIR and OGCG.
+// The loop body is lowered through a different OpenMP runtime API on each
+// path (see the LLVM-HOST-CIR/LLVM-HOST-OGCG split below), and the two
+// paths emit functions in a different order (same as target-parallel.c).
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-OGCG
+
+// Device-side lowering to LLVM IR: compiled in two steps to mirror the
+// offloading driver. The kernel entry sequence is the same on both paths,
+// aside from the wrapper-vs-null difference seen in target-parallel.c.
+// Past that point, CIR and OGCG lower the worksharing loop through
+// different runtime APIs, so they're checked separately under
+// LLVM-DEVICE-CIR/LLVM-DEVICE-OGCG.
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -fclangir -emit-llvm-bc %s -o %t-cir-host.bc
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -emit-llvm-bc %s -o %t-ogcg-host.bc
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-cir-host.bc \
+// RUN:   -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-DEVICE,LLVM-DEVICE-CIR
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-ogcg-host.bc \
+// RUN:   -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-DEVICE,LLVM-DEVICE-OGCG
+
+void during(int);
+
+void target_parallel_for() {
+#pragma omp target
+#pragma omp parallel
+#pragma omp for
+  for (int i = 0; i < 10; i++) {
+    during(i);
+  }
+}
+
+// CIR-HOST: cir.func{{.*}}@target_parallel_for
+// CIR-HOST: omp.target kernel_type(generic) {
+// CIR-HOST: omp.parallel {
+
+// CIR-HOST: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i>
+// CIR-HOST: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+
+// The loop bounds are normalized to a `[0, tripCount)` range, with
+// tripCount = (10 - (0 - 1 + 1)) / 1 = 10.
+// CIR-HOST-NEXT: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+// CIR-HOST-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-HOST-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+// CIR-HOST-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE_A]] : !s32i
+// CIR-HOST-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+// CIR-HOST-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+// CIR-HOST: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-HOST: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+// CIR-HOST: %[[ONE_CIR:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST: %[[ONE:.*]] = cir.builtin_int_cast %[[ONE_CIR]] : !s32i -> i32
+// CIR-HOST: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+
+// CIR-HOST: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+// CIR-HOST-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[ONE]]) {
+// CIR-HOST: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+// CIR-HOST: cir.store align(4) %[[IV_CIR]], %[[IV_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+// CIR-HOST: cir.store align(4) %{{.*}}, %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+// CIR-HOST: cir.call @{{.*}}during
+// CIR-HOST: omp.yield
+// CIR-HOST: }
+// CIR-HOST: }
+// CIR-HOST: omp.terminator
+// CIR-HOST: omp.terminator
+// CIR-HOST: }
+
+// CIR-DEVICE: cir.func{{.*}}@target_parallel_for
+// CIR-DEVICE: omp.target kernel_type(generic) {
+// CIR-DEVICE: omp.parallel {
+
+// The two allocas and their address-space casts can be emitted in either
+// relative order, so match them unordered (CHECK-DAG) rather than assuming
+// a specific interleaving.
+// CIR-DEVICE-DAG: %[[IV_ALLOCA:.*]] = cir.alloca ".omp.iv" align(4) : !cir.ptr<!s32i, target_address_space(5)>
+// CIR-DEVICE-DAG: %[[IV_CAST:.*]] = cir.cast address_space %[[IV_ALLOCA]] : !cir.ptr<!s32i, target_address_space(5)> -> !cir.ptr<!s32i>
+// CIR-DEVICE-DAG: %[[I_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i, target_address_space(5)>
+// CIR-DEVICE-DAG: %[[I_CAST:.*]] = cir.cast address_space %[[I_ALLOCA]] : !cir.ptr<!s32i, target_address_space(5)> -> !cir.ptr<!s32i>
+
+// Same trip count computation as on the host.
+// CIR-DEVICE: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+// CIR-DEVICE-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-DEVICE-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+// CIR-DEVICE-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE_A]] : !s32i
+// CIR-DEVICE-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+// CIR-DEVICE-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+// CIR-DEVICE: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-DEVICE: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> i32
+// CIR-DEVICE: %[[ONE_CIR:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE: %[[ONE:.*]] = cir.builtin_int_cast %[[ONE_CIR]] : !s32i -> i32
+// CIR-DEVICE: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+
+// CIR-DEVICE: omp.wsloop private(@{{.*}} %[[I_CAST]] -> %[[I_PRIV:.*]] : !cir.ptr<!s32i>) {
+// CIR-DEVICE-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[ZERO]]) to (%[[TRIPCOUNT]]) step (%[[ONE]]) {
+// CIR-DEVICE: %[[IV_CIR:.*]] = cir.builtin_int_cast %[[IV]] : i32 -> !s32i
+// CIR-DEVICE: cir.store align(4) %[[IV_CIR]], %[[IV_CAST]] : !s32i, !cir.ptr<!s32i>
+// CIR-DEVICE: cir.store align(4) %{{.*}}, %[[I_PRIV]] : !s32i, !cir.ptr<!s32i>
+// CIR-DEVICE: cir.call @{{.*}}during
+// CIR-DEVICE: omp.yield
+// CIR-DEVICE: }
+// CIR-DEVICE: }
+// CIR-DEVICE: omp.terminator
+// CIR-DEVICE: omp.terminator
+// CIR-DEVICE: }
+
+// The combined `target parallel for` directive decomposes into `target`,
+// `parallel` and `for` leaves and lowers to the same nesting as the explicit
+// target/parallel/for above: an omp.wsloop + omp.loop_nest inside omp.parallel
+// inside omp.target.
+void combined_target_parallel_for() {
+#pragma omp target parallel for
+  for (int i = 0; i < 10; i++) {
+    during(i);
+  }
+}
+
+// CIR-HOST: cir.func{{.*}}@combined_target_parallel_for
+// CIR-HOST: omp.target kernel_type(generic) {
+// CIR-HOST: omp.parallel {
+// CIR-HOST: %[[CI_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
+// tripCount = (10 - (0 - 1 + 1)) / 1 = 10
+// CIR-HOST-NEXT: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+// CIR-HOST-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-HOST-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+// CIR-HOST-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE_A]] : !s32i
+// CIR-HOST-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+// CIR-HOST-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+// CIR-HOST: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+// CIR-HOST: omp.wsloop private(@{{.*}} %[[CI_ALLOCA]] -> %{{.*}} : !cir.ptr<!s32i>) {
+// CIR-HOST-NEXT: omp.loop_nest (%[[CIV:.*]]) : i32 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+// CIR-HOST: cir.call @{{.*}}during
+// CIR-HOST: omp.yield
+// CIR-HOST: }
+// CIR-HOST: }
+// CIR-HOST: omp.terminator
+// CIR-HOST: } {omp.combined}
+// CIR-HOST: omp.terminator
+// CIR-HOST: } {omp.combined}
+
+// CIR-DEVICE: cir.func{{.*}}@combined_target_parallel_for
+// CIR-DEVICE: omp.target kernel_type(generic) {
+// CIR-DEVICE: omp.parallel {
+// tripCount = (10 - (0 - 1 + 1)) / 1 = 10
+// CIR-DEVICE: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
+// CIR-DEVICE-NEXT: %[[LO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-DEVICE-NEXT: %[[STEP_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[LO_MINUS_STEP:.*]] = cir.sub nsw %[[LO]], %[[STEP_A]] : !s32i
+// CIR-DEVICE-NEXT: %[[ONE_A:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[ADJ_LO:.*]] = cir.add nsw %[[LO_MINUS_STEP]], %[[ONE_A]] : !s32i
+// CIR-DEVICE-NEXT: %[[SPAN:.*]] = cir.sub nsw %[[HI]], %[[ADJ_LO]] : !s32i
+// CIR-DEVICE-NEXT: %[[STEP_B:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-DEVICE-NEXT: %[[TRIPCOUNT_CIR:.*]] = cir.div %[[SPAN]], %[[STEP_B]] : !s32i
+// CIR-DEVICE: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] : !s32i -> i32
+// CIR-DEVICE: omp.wsloop private(@{{.*}} %{{.*}} -> %{{.*}} : !cir.ptr<!s32i>) {
+// CIR-DEVICE-NEXT: omp.loop_nest (%{{.*}}) : i32 = (%{{.*}}) to (%[[TRIPCOUNT]]) step (%{{.*}}) {
+// CIR-DEVICE: cir.call @{{.*}}during
+// CIR-DEVICE: omp.yield
+// CIR-DEVICE: }
+// CIR-DEVICE: }
+// CIR-DEVICE: omp.terminator
+// CIR-DEVICE: } {omp.combined}
+// CIR-DEVICE: omp.terminator
+// CIR-DEVICE: } {omp.combined}
+
+// Host wrappers: the __tgt_target_kernel call, the offload-failed fallback
+// branch, and the call into the offloading-entry function are identical on
+// both paths.
+//
+// LLVM-HOST-LABEL: define {{.*}} void @target_parallel_for(
+// LLVM-HOST:         call i32 @__tgt_target_kernel(
+// LLVM-HOST:         br i1 %{{.*}}, label %[[FAILED:[^,]+]], label %{{.*}}
+// LLVM-HOST:       {{^}}[[FAILED]]:
+// LLVM-HOST-NEXT:    call void @__omp_offloading_{{.*}}_target_parallel_for_l{{[0-9]+}}(
+
+// CIR groups all its helper functions together up front, since the
+// MLIR-to-LLVM translation emits them during the lowering pass itself;
+// OGCG's direct AST walk emits each one in source order instead,
+// interleaved with other constructs (same as target-parallel.c). The
+// checks are the same either way, just placed where each path actually
+// emits them (compare the LLVM-HOST-OGCG copy of this block further down).
+// LLVM-HOST-CIR-LABEL: define {{.*}} void @combined_target_parallel_for(
+// LLVM-HOST-CIR:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-CIR:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-CIR:       {{^}}[[FAILED2]]:
+// LLVM-HOST-CIR-NEXT:    call void @__omp_offloading_{{.*}}_combined_target_parallel_for_l{{[0-9]+}}(
+
+// The outer wrapper forks the parallel region via __kmpc_fork_call.
+//
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_target_parallel_for_l{{[0-9]+}}(
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 0, ptr @[[OUTLINED:[^)]+]])
+
+// The worksharing-loop body: CIR and OGCG lower the loop through different
+// runtime APIs here, but both call during() once per iteration and end
+// with a barrier.
+// LLVM-HOST-CIR:       define internal void @[[OUTLINED]](
+// LLVM-HOST-CIR:         store i32 0, ptr %[[LB:.*]], align 4
+// LLVM-HOST-CIR-NEXT:    store i32 9, ptr %[[UB:.*]], align 4
+// LLVM-HOST-CIR-NEXT:    store i32 1, ptr %[[STRIDE:.*]], align 4
+// LLVM-HOST-CIR:         %[[TID:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-HOST-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-HOST-CIR-SAME:      ptr {{.*}}, i32 %[[TID]], i32 34,
+// LLVM-HOST-CIR-SAME:      ptr %{{.*}}, ptr %[[LB]], ptr %[[UB]], ptr %[[STRIDE]],
+// LLVM-HOST-CIR-SAME:      i32 1, i32 0)
+// LLVM-HOST-CIR:         %[[IV:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT:[^,]+]], %{{.*}} ]
+// LLVM-HOST-CIR:         %[[LOOP_CMP:.*]] = icmp ult i32 %[[IV]], %{{.*}}
+// LLVM-HOST-CIR-NEXT:    br i1 %[[LOOP_CMP]], label %[[LOOP_BODY:[^,]+]], label %[[LOOP_EXIT:[^ ,]+]]
+// LLVM-HOST-CIR:       {{^}}[[LOOP_EXIT]]:
+// LLVM-HOST-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID]])
+// LLVM-HOST-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-HOST-CIR:       {{^}}[[LOOP_BODY]]:
+// LLVM-HOST-CIR:         call void @during(i32
+// LLVM-HOST-CIR:         %[[NEXT]] = add nuw i32 %[[IV]], 1
+
+// LLVM-HOST-OGCG:       define internal void @[[OUTLINED]](
+// LLVM-HOST-OGCG:         store i32 0, ptr %[[OMP_LB:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 9, ptr %[[OMP_UB:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 1, ptr %[[OMP_STRIDE:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 0, ptr %[[OMP_IS_LAST:.*]], align 4
+// LLVM-HOST-OGCG:         %[[TID2:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-HOST-OGCG-NEXT:    call void @__kmpc_for_static_init_4(
+// LLVM-HOST-OGCG-SAME:      ptr {{.*}}, i32 %[[TID2]], i32 34,
+// LLVM-HOST-OGCG-SAME:      ptr %[[OMP_IS_LAST]], ptr %[[OMP_LB]], ptr %[[OMP_UB]], ptr %[[OMP_STRIDE]],
+// LLVM-HOST-OGCG-SAME:      i32 1, i32 1)
+// LLVM-HOST-OGCG:         %[[UB_VAL:.*]] = load i32, ptr %[[OMP_UB]], align 4
+// LLVM-HOST-OGCG-NEXT:    %[[CMP:.*]] = icmp sgt i32 %[[UB_VAL]], 9
+// LLVM-HOST-OGCG-NEXT:    br i1 %[[CMP]], label %[[COND_TRUE:[^,]+]], label %[[COND_FALSE:[^ ,]+]]
+// LLVM-HOST-OGCG:         %[[COND:.*]] = phi i32 [ 9, %[[COND_TRUE]] ], [ %{{.*}}, %[[COND_FALSE]] ]
+// LLVM-HOST-OGCG-NEXT:    store i32 %[[COND]], ptr %[[OMP_UB]], align 4
+// LLVM-HOST-OGCG:         %[[CMP1:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-HOST-OGCG-NEXT:    br i1 %[[CMP1]], label %[[FOR_BODY:[^,]+]], label %{{.*}}
+// LLVM-HOST-OGCG:       {{^}}[[FOR_BODY]]:
+// LLVM-HOST-OGCG:         call void @during(i32
+// LLVM-HOST-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID2]])
+// LLVM-HOST-OGCG-NEXT:    call void @__kmpc_barrier(ptr {{.*}}, i32 %[[TID2]])
+
+// OGCG's copy of combined_target_parallel_for's host wrapper: identical
+// content to the LLVM-HOST-CIR block above, just emitted after
+// target_parallel_for's entry+outlined functions instead of before them.
+// LLVM-HOST-OGCG-LABEL: define {{.*}} void @combined_target_parallel_for(
+// LLVM-HOST-OGCG:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-OGCG:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-OGCG:       {{^}}[[FAILED2]]:
+// LLVM-HOST-OGCG-NEXT:    call void @__omp_offloading_{{.*}}_combined_target_parallel_for_l{{[0-9]+}}(
+
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_combined_target_parallel_for_l{{[0-9]+}}(
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 0, ptr @[[OUTLINED2:[^)]+]])
+
+// LLVM-HOST-CIR:       define internal void @[[OUTLINED2]](
+// LLVM-HOST-CIR:         store i32 0, ptr %[[LB2:.*]], align 4
+// LLVM-HOST-CIR-NEXT:    store i32 9, ptr %[[UB2:.*]], align 4
+// LLVM-HOST-CIR-NEXT:    store i32 1, ptr %[[STRIDE2:.*]], align 4
+// LLVM-HOST-CIR:         %[[TID3:.*]] = call i32 @__kmpc_global_thread_num(ptr {{.*}})
+// LLVM-HOST-CIR-NEXT:    call void @__kmpc_for_static_init_4u(
+// LLVM-HOST-CIR-SAME:      ptr {{.*}}, i32 %[[TID3]], i32 34,
+// LLVM-HOST-CIR-SAME:      ptr %{{.*}}, ptr %[[LB2]], ptr %[[UB2]], ptr %[[STRIDE2]],
+// LLVM-HOST-CIR-SAME:      i32 1, i32 0)
+// LLVM-HOST-CIR:         %[[IV2:.*]] = phi i32 [ 0, %{{.*}} ], [ %[[NEXT2:[^,]+]], %{{.*}} ]
+// LLVM-HOST-CIR:         %[[LOOP_CMP2:.*]] = icmp ult i32 %[[IV2]], %{{.*}}
+// LLVM-HOST-CIR-NEXT:    br i1 %[[LOOP_CMP2]], label %[[LOOP_BODY2:[^,]+]], label %[[LOOP_EXIT2:[^ ,]+]]
+// LLVM-HOST-CIR:       {{^}}[[LOOP_EXIT2]]:
+// LLVM-HOST-CIR-NEXT:    call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID3]])
+// LLVM-HOST-CIR:         call void @__kmpc_barrier(ptr {{.*}}, i32 %{{.*}})
+// LLVM-HOST-CIR:       {{^}}[[LOOP_BODY2]]:
+// LLVM-HOST-CIR:         call void @during(i32
+// LLVM-HOST-CIR:         %[[NEXT2]] = add nuw i32 %[[IV2]], 1
+
+// Unlike the non-combined form above, OGCG does not emit a trailing
+// barrier after this combined construct's worksharing loop: since the
+// loop is the entire body of the parallel region, the implicit join at
+// the end of the parallel region already synchronizes the threads. CIR
+// doesn't yet apply that optimization, so it emits one either way.
+// LLVM-HOST-OGCG:       define internal void @[[OUTLINED2]](
+// LLVM-HOST-OGCG:         store i32 0, ptr %[[OMP_LB2:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 9, ptr %[[OMP_UB2:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 1, ptr %[[OMP_STRIDE2:.*]], align 4
+// LLVM-HOST-OGCG-NEXT:    store i32 0, ptr %[[OMP_IS_LAST2:.*]], align 4
+// LLVM-HOST-OGCG:         %[[TID4:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-HOST-OGCG-NEXT:    call void @__kmpc_for_static_init_4(
+// LLVM-HOST-OGCG-SAME:      ptr {{.*}}, i32 %[[TID4]], i32 34,
+// LLVM-HOST-OGCG-SAME:      ptr %[[OMP_IS_LAST2]], ptr %[[OMP_LB2]], ptr %[[OMP_UB2]], ptr %[[OMP_STRIDE2]],
+// LLVM-HOST-OGCG-SAME:      i32 1, i32 1)
+// LLVM-HOST-OGCG:         %[[UB_VAL2:.*]] = load i32, ptr %[[OMP_UB2]], align 4
+// LLVM-HOST-OGCG-NEXT:    %[[CMP2:.*]] = icmp sgt i32 %[[UB_VAL2]], 9
+// LLVM-HOST-OGCG-NEXT:    br i1 %[[CMP2]], label %[[COND_TRUE2:[^,]+]], label %[[COND_FALSE2:[^ ,]+]]
+// LLVM-HOST-OGCG:         %[[COND2:.*]] = phi i32 [ 9, %[[COND_TRUE2]] ], [ %{{.*}}, %[[COND_FALSE2]] ]
+// LLVM-HOST-OGCG-NEXT:    store i32 %[[COND2]], ptr %[[OMP_UB2]], align 4
+// LLVM-HOST-OGCG:         %[[CMP3:.*]] = icmp sle i32 %{{.*}}, %{{.*}}
+// LLVM-HOST-OGCG-NEXT:    br i1 %[[CMP3]], label %[[FOR_BODY2:[^,]+]], label %{{.*}}
+// LLVM-HOST-OGCG:       {{^}}[[FOR_BODY2]]:
+// LLVM-HOST-OGCG:         call void @during(i32
+// LLVM-HOST-OGCG:         call void @__kmpc_for_static_fini(ptr {{.*}}, i32 %[[TID4]])
+// LLVM-HOST-OGCG-NEXT:    ret void
+
+// Device kernel entry: __kmpc_target_init/__kmpc_target_deinit and the
+// branch into the user code are identical on both paths.
+//
+// LLVM-DEVICE-LABEL: define weak_odr protected amdgpu_kernel void @__omp_offloading_{{.*}}_target_parallel_for_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE]]:
+
+// CIR passes a wrapper function to __kmpc_parallel_60; OGCG passes null
+// instead, since it doesn't need one here (same as target-parallel.c).
+// LLVM-DEVICE-CIR:   call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED:[^,]+]], ptr @{{[^,]+}}, ptr {{.*}}, i64 0, i32 0)
+// LLVM-DEVICE-OGCG:  call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED:[^,]+]], ptr null, ptr {{.*}}, i64 0, i32 0)
+
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+
+// Past the kernel entry, CIR and OGCG use completely different device
+// runtime APIs for the loop (a newer driver+callback API for CIR vs one
+// big function for OGCG), so there isn't much to share beyond the kernel
+// entry checked above.
+//
+// LLVM-DEVICE-CIR:       define internal void @[[DEV_OUTLINED]](
+// LLVM-DEVICE-CIR:         call void @__kmpc_for_static_loop_4u(ptr {{.*}}, ptr @[[DEV_LOOP_BODY:[^,]+]],
+// LLVM-DEVICE-CIR:         call void @__kmpc_barrier(
+
+// LLVM-DEVICE-CIR:       define internal void @[[DEV_LOOP_BODY]](i32
+// LLVM-DEVICE-CIR:         call void @during(i32
+
+// LLVM-DEVICE-OGCG:       define internal void @[[DEV_OUTLINED]](
+// LLVM-DEVICE-OGCG:         call void @__kmpc_for_static_init_4(
+// LLVM-DEVICE-OGCG:         call void @during(i32
+// LLVM-DEVICE-OGCG:         call void @__kmpc_for_static_fini(
+
+// LLVM-DEVICE-LABEL: define weak_odr protected amdgpu_kernel void @__omp_offloading_{{.*}}_combined_target_parallel_for_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE2:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE2]]:
+
+// LLVM-DEVICE-CIR:   call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED2:[^,]+]], ptr @{{[^,]+}}, ptr {{.*}}, i64 0, i32 0)
+// LLVM-DEVICE-OGCG:  call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED2:[^,]+]], ptr null, ptr {{.*}}, i64 0, i32 0)
+
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+
+// LLVM-DEVICE-CIR:       define internal void @[[DEV_OUTLINED2]](
+// LLVM-DEVICE-CIR:         call void @__kmpc_for_static_loop_4u(ptr {{.*}}, ptr @[[DEV_LOOP_BODY2:[^,]+]],
+// LLVM-DEVICE-CIR:         call void @__kmpc_barrier(
+
+// LLVM-DEVICE-CIR:       define internal void @[[DEV_LOOP_BODY2]](i32
+// LLVM-DEVICE-CIR:         call void @during(i32
+
+// LLVM-DEVICE-OGCG:       define internal void @[[DEV_OUTLINED2]](
+// LLVM-DEVICE-OGCG:         call void @__kmpc_for_static_init_4(
+// LLVM-DEVICE-OGCG:         call void @during(i32
+// LLVM-DEVICE-OGCG:         call void @__kmpc_for_static_fini(
diff --git a/clang/test/CIR/CodeGenOpenMP/target-parallel.c b/clang/test/CIR/CodeGenOpenMP/target-parallel.c
index 3aad305e5a1b27..a7e1d032950f80 100644
--- a/clang/test/CIR/CodeGenOpenMP/target-parallel.c
+++ b/clang/test/CIR/CodeGenOpenMP/target-parallel.c
@@ -10,6 +10,33 @@
 // RUN:   -emit-cir -fclangir %s -o - \
 // RUN:   | FileCheck %s --check-prefix=CIR-DEVICE
 
+// Host-side lowering to LLVM IR: identical on both paths, so LLVM-HOST
+// checks both. The only difference is where the two helper functions end
+// up relative to each other, checked below under
+// LLVM-HOST-CIR/LLVM-HOST-OGCG.
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp \
+// RUN:   -fopenmp-targets=amdgcn-amd-amdhsa -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-HOST,LLVM-HOST-OGCG
+
+// Device-side lowering to LLVM IR: compiled in two steps to mirror the
+// offloading driver. Same LLVM-DEVICE/LLVM-DEVICE-CIR/LLVM-DEVICE-OGCG
+// split as above.
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -fclangir -emit-llvm-bc %s -o %t-cir-host.bc
+// RUN: %clang_cc1 -fopenmp -triple x86_64-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa \
+// RUN:   -emit-llvm-bc %s -o %t-ogcg-host.bc
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-cir-host.bc \
+// RUN:   -fclangir -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-DEVICE,LLVM-DEVICE-CIR
+// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fopenmp -fopenmp-is-target-device \
+// RUN:   -fopenmp-host-ir-file-path %t-ogcg-host.bc \
+// RUN:   -emit-llvm %s -o - \
+// RUN:   | FileCheck %s --check-prefixes=LLVM-DEVICE,LLVM-DEVICE-OGCG
+
 void use(int);
 
 // The combined 'target parallel' directive lowers to an omp.parallel nested
@@ -57,3 +84,113 @@ void target_parallel_proc_bind(int x) {
     use(x);
   }
 }
+
+// Host wrappers: the __tgt_target_kernel call, the offload-failed fallback
+// branch, and the call into the offloading-entry function are identical on
+// both paths (the entry symbol name is derived from file+line, not from the
+// lowering path).
+//
+// LLVM-HOST-LABEL: define {{.*}} void @target_parallel(
+// LLVM-HOST-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST:         call i32 @__tgt_target_kernel(
+// LLVM-HOST:         br i1 %{{.*}}, label %[[FAILED:[^,]+]], label %{{.*}}
+// LLVM-HOST:       {{^}}[[FAILED]]:
+// LLVM-HOST-NEXT:    call void @__omp_offloading_{{.*}}_target_parallel_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// CIR groups all its helper functions together up front, since the
+// MLIR-to-LLVM translation emits them during the lowering pass itself;
+// OGCG's direct AST walk emits each one in source order instead,
+// interleaved with other constructs. The checks are the same either way,
+// just placed where each path actually emits them (compare the
+// LLVM-HOST-OGCG copy of this block further down).
+// LLVM-HOST-CIR-LABEL: define {{.*}} void @target_parallel_proc_bind(
+// LLVM-HOST-CIR-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST-CIR:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST-CIR:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST-CIR:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-CIR:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-CIR:       {{^}}[[FAILED2]]:
+// LLVM-HOST-CIR-NEXT:    call void @__omp_offloading_{{.*}}_target_parallel_proc_bind_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// Outlined target-entry functions: forks the parallel region via
+// __kmpc_fork_call, with a __kmpc_push_proc_bind call right before it for
+// the proc_bind clause.
+//
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_target_parallel_l{{[0-9]+}}(
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[OUTLINED:[^,]+]], ptr {{.*}})
+
+// LLVM-HOST:       define internal void @[[OUTLINED]](
+// LLVM-HOST:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-HOST:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-HOST:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-HOST:         ret void
+
+// OGCG's copy of target_parallel_proc_bind's host wrapper: identical
+// content to the LLVM-HOST-CIR block above, just emitted after
+// target_parallel's entry+outlined functions instead of before them.
+// LLVM-HOST-OGCG-LABEL: define {{.*}} void @target_parallel_proc_bind(
+// LLVM-HOST-OGCG-SAME:  i32 noundef %[[ARG:[^,)]+]]
+// LLVM-HOST-OGCG:         %[[X_ADDR:.*]] = alloca i32, align 4
+// LLVM-HOST-OGCG:         store i32 {{.*}}, ptr %[[X_ADDR]], align 4
+// LLVM-HOST-OGCG:         call i32 @__tgt_target_kernel(
+// LLVM-HOST-OGCG:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label %{{.*}}
+// LLVM-HOST-OGCG:       {{^}}[[FAILED2]]:
+// LLVM-HOST-OGCG-NEXT:    call void @__omp_offloading_{{.*}}_target_parallel_proc_bind_l{{[0-9]+}}(ptr %[[X_ADDR]], ptr null)
+
+// LLVM-HOST-LABEL: define internal void @__omp_offloading_{{.*}}_target_parallel_proc_bind_l{{[0-9]+}}(
+// LLVM-HOST:         call i32 @__kmpc_global_thread_num(
+// LLVM-HOST:         call void @__kmpc_push_proc_bind(ptr {{.*}}, i32 {{.*}}, i32 4)
+// LLVM-HOST:         call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr {{.*}}, i32 1, ptr @[[OUTLINED2:[^,]+]], ptr {{.*}})
+
+// LLVM-HOST:       define internal void @[[OUTLINED2]](
+// LLVM-HOST:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-HOST:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-HOST:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-HOST:         ret void
+
+// Device kernel entry: __kmpc_target_init/__kmpc_target_deinit and the
+// branch into the user code are identical on both paths.
+//
+// LLVM-DEVICE-LABEL: define weak_odr protected amdgpu_kernel void @__omp_offloading_{{.*}}_target_parallel_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE]]:
+// LLVM-DEVICE:         call i32 @__kmpc_global_thread_num(
+
+// CIR's outlining always builds a dedicated wrapper function for the
+// parallel region; OGCG's codegen only builds one when it's actually
+// needed, so it passes a null function pointer here instead.
+// LLVM-DEVICE-CIR:   call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED:[^,]+]], ptr @{{[^,]+}}, ptr {{.*}}, i64 1, i32 0)
+// LLVM-DEVICE-OGCG:  call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED:[^,]+]], ptr null, ptr {{.*}}, i64 1, i32 0)
+
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+// LLVM-DEVICE:         ret void
+
+// The forked function's body (loading the captured arg and calling @use) is
+// identical on both paths.
+//
+// LLVM-DEVICE:       define internal void @[[DEV_OUTLINED]](
+// LLVM-DEVICE:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-DEVICE:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-DEVICE:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-DEVICE:         ret void
+
+// LLVM-DEVICE-LABEL: define weak_odr protected amdgpu_kernel void @__omp_offloading_{{.*}}_target_parallel_proc_bind_l{{[0-9]+}}(
+// LLVM-DEVICE:         call i32 @__kmpc_target_init(
+// LLVM-DEVICE:         br i1 %{{.*}}, label %[[USER_CODE2:[^,]+]], label %{{.*}}
+// LLVM-DEVICE:       {{^}}[[USER_CODE2]]:
+// LLVM-DEVICE:         call i32 @__kmpc_global_thread_num(
+
+// LLVM-DEVICE-CIR:   call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED2:[^,]+]], ptr @{{[^,]+}}, ptr {{.*}}, i64 1, i32 0)
+// LLVM-DEVICE-OGCG:  call void @__kmpc_parallel_60(ptr {{.*}}, i32 {{.*}}, i32 1, i32 -1, i32 -1, ptr @[[DEV_OUTLINED2:[^,]+]], ptr null, ptr {{.*}}, i64 1, i32 0)
+
+// LLVM-DEVICE:         call void @__kmpc_target_deinit()
+// LLVM-DEVICE:         ret void
+
+// LLVM-DEVICE:       define internal void @[[DEV_OUTLINED2]](
+// LLVM-DEVICE:         %[[P:.*]] = load ptr, ptr %{{.*}}, align 8
+// LLVM-DEVICE:         %[[V:.*]] = load i32, ptr %[[P]], align 4
+// LLVM-DEVICE:         call void @use(i32 {{.*}} %[[V]])
+// LLVM-DEVICE:         ret void



More information about the cfe-commits mailing list