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

>From 308f7198f9a9555cee47eb644db232617899ab8a Mon Sep 17 00:00:00 2001
From: Jan Leyonberg <[email protected]>
Date: Wed, 7 Oct 2026 13:13:03 -0400
Subject: [PATCH] [CIR][OpenMP] Add support for host_eval so that SPMD kernels
 can be used

This patch adds support for host_eval so that SPMD kernels and in the future
num_threads and other clauses can be implemented correctly.

Assisted-by: Cursor / Claude Sonnet 5 High
---
 clang/lib/CIR/CodeGen/CIRGenFunction.h        |  26 +++
 clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp    | 164 ++++++++++++++++--
 .../CIR/CodeGenOpenMP/target-parallel-for.c   |  82 +++++----
 3 files changed, 227 insertions(+), 45 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h 
b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index 6c0a2a5d083ca..79220ab73f156 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -2867,6 +2867,32 @@ class CIRGenFunction : public CIRGenTypeCache {
   //                         OpenMP Emission
   
//===--------------------------------------------------------------------===//
 public:
+  /// The enclosing omp.target's host-evaluated loop bounds -- a normalized
+  /// [0, tripCount) range matching emitOMPLoopNest's convention -- forwarded
+  /// as host_eval block arguments and consumed once by the nested
+  /// omp.wsloop.
+  struct OMPHostEvalBounds {
+    mlir::Value zero;
+    mlir::Value tripCount;
+    mlir::Value one;
+    bool applied = false;
+  };
+  std::optional<OMPHostEvalBounds> ompHostEvalBounds;
+
+  /// An RAII object to set (and then restore) ompHostEvalBounds for the
+  /// extent of a nested loop directive's emission.
+  struct OMPHostEvalBoundsRAII {
+    CIRGenFunction &cgf;
+    std::optional<OMPHostEvalBounds> oldBounds;
+
+    OMPHostEvalBoundsRAII(CIRGenFunction &cgf,
+                          std::optional<OMPHostEvalBounds> newBounds)
+        : cgf(cgf), oldBounds(std::move(cgf.ompHostEvalBounds)) {
+      cgf.ompHostEvalBounds = std::move(newBounds);
+    }
+    ~OMPHostEvalBoundsRAII() { cgf.ompHostEvalBounds = std::move(oldBounds); }
+  };
+
   /// 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
diff --git a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp 
b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
index e41ab8153cda4..8135f0882f69c 100644
--- a/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenStmtOpenMP.cpp
@@ -52,6 +52,68 @@ checkSynthesizedClauses(CIRGenFunction &cgf, const 
OMPExecutableDirective &s,
   return res;
 }
 
+/// Returns \p s's nested OpenMP directive -- its body after unwrapping any
+/// single-statement compounds -- or null if the body isn't itself a single
+/// OpenMP directive.
+static const OMPExecutableDirective *
+getSingleNestedOMPDirective(const OMPExecutableDirective &s) {
+  const Stmt *body =
+      s.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers(
+          /*IgnoreCaptured=*/true);
+  return dyn_cast<OMPExecutableDirective>(body);
+}
+
+/// Returns true if \p dir is a leaf construct kind that participates in
+/// CIR's "combined" op marking (see hasCombinableNestedLeaf).
+static bool isCombinableLeaf(llvm::omp::Directive dir) {
+  switch (dir) {
+  case llvm::omp::OMPD_target:
+  case llvm::omp::OMPD_parallel:
+  case llvm::omp::OMPD_for:
+    return true;
+  default:
+    return false;
+  }
+}
+
+/// Returns true if \p s's body is itself an OpenMP directive that combines
+/// with \p s for codegen purposes, e.g. explicitly-nested `target` +
+/// `parallel` written as separate pragmas rather than `target parallel`.
+static bool hasCombinableNestedLeaf(const OMPExecutableDirective &s) {
+  const OMPExecutableDirective *nested = getSingleNestedOMPDirective(s);
+  if (!nested)
+    return false;
+  return isCombinableLeaf(
+      llvm::omp::getLeafConstructsOrSelf(nested->getDirectiveKind()).front());
+}
+
+/// Finds the loop directive nested (possibly transitively) inside \p s.
+static const OMPLoopDirective *
+findNestedOMPLoopDirective(const OMPExecutableDirective &s) {
+  const OMPExecutableDirective *nested = getSingleNestedOMPDirective(s);
+  if (!nested)
+    return nullptr;
+  if (const auto *loopDir = dyn_cast<OMPLoopDirective>(nested))
+    return loopDir;
+  return findNestedOMPLoopDirective(*nested);
+}
+
+/// Returns true if \p s is a target SPMD construct: a combined
+/// `target parallel for`, or an explicitly nested `target` whose body is a
+/// parallel directive.
+static bool isTargetSPMD(const OMPExecutableDirective &s) {
+  switch (s.getDirectiveKind()) {
+  case llvm::omp::OMPD_target_parallel_for:
+    return true;
+  case llvm::omp::OMPD_target: {
+    const OMPExecutableDirective *nested = getSingleNestedOMPDirective(s);
+    return nested && isOpenMPParallelDirective(nested->getDirectiveKind());
+  }
+  default:
+    return false;
+  }
+}
+
 static mlir::LogicalResult
 emitParallelClauses(CIRGenFunction &cgf, CIRGenModule &cgm,
                     CIRGenBuilderTy &builder, mlir::Location loc,
@@ -113,7 +175,8 @@ emitParallelOp(CIRGenFunction &cgf, const DirectiveTy &s,
          "reduction(task: ...) should already be rejected as NYI");
 
   auto parallelOp = mlir::omp::ParallelOp::create(builder, begin, clauseOps);
-  if (!omp::isLastItemInQueue(item, queue))
+  if (!omp::isLastItemInQueue(item, queue) ||
+      hasCombinableNestedLeaf(static_cast<const OMPExecutableDirective &>(s)))
     parallelOp.setCombined(true);
 
   mlir::Block &block = parallelOp.getRegion().emplaceBlock();
@@ -294,6 +357,28 @@ static Address emitLoopPrivateCounter(CIRGenFunction &cgf, 
CIRGenModule &cgm,
   return moldAddr;
 }
 
+/// Computes a target SPMD loop's normalized `[0, tripCount)` bounds at the
+/// host insertion point, as builtin-integer values for the enclosing
+/// omp.target's host_eval operands. All three are forwarded even though
+/// only the trip count is runtime-dependent, since the dialect requires an
+/// SPMD loop_nest's bounds to be host-evaluated in full.
+static std::optional<CIRGenFunction::OMPHostEvalBounds>
+emitHostEvalLoopBounds(CIRGenFunction &cgf, const OMPLoopDirective &s) {
+  if (emitLoopPreInits(cgf, s.getPreInits()).failed())
+    return std::nullopt;
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  mlir::Location loc = cgf.getLoc(s.getBeginLoc());
+  mlir::Value tripCountCir = cgf.emitScalarExpr(s.getNumIterations());
+  auto cirIntType = mlir::cast<cir::IntType>(tripCountCir.getType());
+  CIRGenFunction::OMPHostEvalBounds hev;
+  hev.zero = builder.createBuiltinIntCast(
+      loc, builder.getConstInt(loc, cirIntType, 0));
+  hev.tripCount = builder.createBuiltinIntCast(loc, tripCountCir);
+  hev.one = builder.createBuiltinIntCast(
+      loc, builder.getConstInt(loc, cirIntType, 1));
+  return hev;
+}
+
 /// Lowers an OMPLoopDirective's `for` directive to an omp.wsloop +
 /// omp.loop_nest.
 static mlir::LogicalResult
@@ -314,7 +399,12 @@ emitOMPWorksharingLoop(CIRGenFunction &cgf, const 
OMPLoopDirective &s,
   const CapturedStmt *capturedStmt = s.getInnermostCapturedStmt();
   const auto *forStmt = cast<ForStmt>(capturedStmt->getCapturedStmt());
 
-  if (emitLoopPreInits(cgf, s.getPreInits()).failed())
+  // True when the enclosing omp.target already computed this loop's trip
+  // count (see emitHostEvalLoopBounds) and forwarded it via
+  // cgf.ompHostEvalBounds.
+  bool consumingHostEval =
+      cgf.ompHostEvalBounds && !cgf.ompHostEvalBounds->applied;
+  if (!consumingHostEval && emitLoopPreInits(cgf, s.getPreInits()).failed())
     return mlir::failure();
 
   // Allocate storage for Sema's normalized 0-based loop counter.
@@ -338,13 +428,22 @@ emitOMPWorksharingLoop(CIRGenFunction &cgf, const 
OMPLoopDirective &s,
         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);
+  mlir::Value zero, tripCount, one;
+  if (consumingHostEval) {
+    // Consume the host_eval bounds forwarded by the enclosing omp.target.
+    cgf.ompHostEvalBounds->applied = true;
+    zero = cgf.ompHostEvalBounds->zero;
+    tripCount = cgf.ompHostEvalBounds->tripCount;
+    one = cgf.ompHostEvalBounds->one;
+  } else {
+    mlir::Value tripCountCir = cgf.emitScalarExpr(s.getNumIterations());
+    auto cirIntType = mlir::cast<cir::IntType>(tripCountCir.getType());
+    zero = builder.createBuiltinIntCast(
+        loc, builder.getConstInt(loc, cirIntType, 0));
+    one = builder.createBuiltinIntCast(loc,
+                                       builder.getConstInt(loc, cirIntType, 
1));
+    tripCount = builder.createBuiltinIntCast(loc, tripCountCir);
+  }
 
   auto wsloopOp = mlir::omp::WsloopOp::create(builder, loc, clauseOps);
   mlir::Block *innerBlock = new mlir::Block();
@@ -606,25 +705,62 @@ emitTargetOp(CIRGenFunction &cgf, const DirectiveTy &s,
   if (mlir::failed(emitOMPTargetImplicitCaptures(cgf, s, mapSyms)))
     return mlir::failure();
 
-  // Use generic for now.
+  const auto &execDir = static_cast<const OMPExecutableDirective &>(s);
+  bool isSPMD = isTargetSPMD(execDir);
+
+  std::optional<CIRGenFunction::OMPHostEvalBounds> hostEval;
+  if (isSPMD && !cgf.getCIRGenModule().getLangOpts().OpenMPIsTargetDevice) {
+    const auto *loopDir = dyn_cast<OMPLoopDirective>(&execDir);
+    if (!loopDir)
+      loopDir = findNestedOMPLoopDirective(execDir);
+    if (!loopDir || !(hostEval = emitHostEvalLoopBounds(cgf, *loopDir))) {
+      cgf.getCIRGenModule().errorNYI(
+          s.getSourceRange(), "OpenMP target host-evaluated loop bounds");
+      return mlir::failure();
+    }
+    clauseOps.hostEvalVars.push_back(hostEval->zero);
+    clauseOps.hostEvalVars.push_back(hostEval->tripCount);
+    clauseOps.hostEvalVars.push_back(hostEval->one);
+  }
+
   clauseOps.kernelType = mlir::omp::TargetExecModeAttr::get(
-      &cgf.getMLIRContext(), mlir::omp::TargetExecMode::generic);
+      &cgf.getMLIRContext(), isSPMD ? mlir::omp::TargetExecMode::spmd
+                                    : mlir::omp::TargetExecMode::generic);
 
   auto targetOp = mlir::omp::TargetOp::create(builder, begin, clauseOps);
-  if (!omp::isLastItemInQueue(item, queue))
+  if (!omp::isLastItemInQueue(item, queue) || hasCombinableNestedLeaf(execDir))
     targetOp.setCombined(true);
 
+  // Block arguments must be added in the order BlockArgOpenMPOpInterface
+  // expects: host_eval arguments precede map arguments.
   mlir::Block &block = targetOp.getRegion().emplaceBlock();
+  for (mlir::Value hostEvalVar : clauseOps.hostEvalVars)
+    block.addArgument(hostEvalVar.getType(), begin);
   for (mlir::Value mapVar : clauseOps.mapVars)
     block.addArgument(mapVar.getType(), begin);
 
+  auto argIface = mlir::cast<mlir::omp::BlockArgOpenMPOpInterface>(*targetOp);
+  llvm::MutableArrayRef<mlir::BlockArgument> mapBlockArgs =
+      argIface.getMapBlockArgs();
   llvm::SmallVector<std::pair<const VarDecl *, Address>> savedAddrs;
   for (auto [idx, vd] : llvm::enumerate(mapSyms)) {
     Address origAddr = cgf.getAddrOfLocalVar(vd);
     savedAddrs.push_back({vd, origAddr});
-    mlir::Value blockArg = block.getArgument(idx);
-    cgf.replaceAddrOfLocalVar(vd, Address(blockArg, origAddr.getAlignment()));
+    cgf.replaceAddrOfLocalVar(
+        vd, Address(mapBlockArgs[idx], origAddr.getAlignment()));
+  }
+
+  // Forward the host_eval block arguments to the nested loop's emission.
+  std::optional<CIRGenFunction::OMPHostEvalBounds> newHostEvalBounds;
+  if (hostEval) {
+    llvm::MutableArrayRef<mlir::BlockArgument> hostEvalBlockArgs =
+        argIface.getHostEvalBlockArgs();
+    newHostEvalBounds = CIRGenFunction::OMPHostEvalBounds{
+        hostEvalBlockArgs[0], hostEvalBlockArgs[1], hostEvalBlockArgs[2],
+        /*applied=*/false};
   }
+  CIRGenFunction::OMPHostEvalBoundsRAII hostEvalGuard(
+      cgf, std::move(newHostEvalBounds));
 
   mlir::LogicalResult res = emitOMPRegionBody<mlir::omp::TerminatorOp>(
       cgf, block, begin, end, emitBody);
diff --git a/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c 
b/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c
index f0112b773cc86..9c95fc58fe4fa 100644
--- a/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c
+++ b/clang/test/CIR/CodeGenOpenMP/target-parallel-for.c
@@ -21,8 +21,7 @@
 // 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.
+// offloading driver. The kernel entry sequence is the same on both paths.
 // 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.
@@ -41,6 +40,8 @@
 
 void during(int);
 
+// This is a target SPMD construct, so the host also computes the loop's
+// trip count and forwards it via host_eval block arguments.
 void target_parallel_for() {
 #pragma omp target
 #pragma omp parallel
@@ -51,14 +52,11 @@ void target_parallel_for() {
 }
 
 // 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.
+// The trip count is computed before the omp.target, from the original
+// bounds/step: tripCount = (10 - (0 - 1 + 1)) / 1 = 10. Then 0, the trip
+// count, and 1 are each cast to a builtin integer and forwarded as the
+// omp.target's host_eval operands.
 // 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
@@ -68,14 +66,21 @@ void target_parallel_for() {
 // 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-NEXT: %[[ZERO_CIR:.*]] = cir.const #cir.int<0> : !s32i
+// CIR-HOST-NEXT: %[[ZERO:.*]] = cir.builtin_int_cast %[[ZERO_CIR]] : !s32i -> 
i32
+// CIR-HOST-NEXT: %[[TRIPCOUNT:.*]] = cir.builtin_int_cast %[[TRIPCOUNT_CIR]] 
: !s32i -> i32
+// CIR-HOST-NEXT: %[[ONE_CIR:.*]] = cir.const #cir.int<1> : !s32i
+// CIR-HOST-NEXT: %[[ONE:.*]] = cir.builtin_int_cast %[[ONE_CIR]] : !s32i -> 
i32
+// CIR-HOST-NEXT: omp.target kernel_type(spmd) host_eval(%[[ZERO]] -> 
%[[AZERO:.*]], %[[TRIPCOUNT]] -> %[[ATRIPCOUNT:.*]], %[[ONE]] -> %[[AONE:.*]] : 
i32, i32, i32) {
+// CIR-HOST-NEXT: 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>
 
-// 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]]) {
+// The loop inside the target region uses the host_eval block arguments
+// instead of recomputing the bounds.
+// CIR-HOST-NEXT: omp.wsloop private(@{{.*}} %[[I_ALLOCA]] -> %[[I_PRIV:.*]] : 
!cir.ptr<!s32i>) {
+// CIR-HOST-NEXT: omp.loop_nest (%[[IV:.*]]) : i32 = (%[[AZERO]]) to 
(%[[ATRIPCOUNT]]) step (%[[AONE]]) {
 // 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>
@@ -88,7 +93,7 @@ void target_parallel_for() {
 // CIR-HOST: }
 
 // CIR-DEVICE: cir.func{{.*}}@target_parallel_for
-// CIR-DEVICE: omp.target kernel_type(generic) {
+// CIR-DEVICE: omp.target kernel_type(spmd) {
 // CIR-DEVICE: omp.parallel {
 
 // The two allocas and their address-space casts can be emitted in either
@@ -99,7 +104,8 @@ void target_parallel_for() {
 // 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.
+// The device has no host_eval operands, so it recomputes the same trip
+// count inside the region.
 // 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
@@ -131,7 +137,9 @@ void target_parallel_for() {
 // 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.
+// inside omp.target. This is a target SPMD construct, so the omp.target is
+// marked kernel_type(spmd) with host_eval'd loop bounds, just like the
+// explicitly nested directives above.
 void combined_target_parallel_for() {
 #pragma omp target parallel for
   for (int i = 0; i < 10; i++) {
@@ -140,9 +148,6 @@ void combined_target_parallel_for() {
 }
 
 // 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
@@ -154,8 +159,11 @@ void combined_target_parallel_for() {
 // 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.target kernel_type(spmd) host_eval(%{{.*}} -> %[[AZERO:.*]], 
%[[TRIPCOUNT]] -> %[[ATRIPCOUNT:.*]], %{{.*}} -> %[[AONE:.*]] : i32, i32, i32) {
+// CIR-HOST: omp.parallel {
+// CIR-HOST: %[[CI_ALLOCA:.*]] = cir.alloca "i" align(4) : !cir.ptr<!s32i>
 // CIR-HOST: omp.wsloop private(@{{.*}} %[[CI_ALLOCA]] -> %{{.*}} : 
!cir.ptr<!s32i>) {
-// CIR-HOST-NEXT: omp.loop_nest (%[[CIV:.*]]) : i32 = (%{{.*}}) to 
(%[[TRIPCOUNT]]) step (%{{.*}}) {
+// CIR-HOST-NEXT: omp.loop_nest (%[[CIV:.*]]) : i32 = (%[[AZERO]]) to 
(%[[ATRIPCOUNT]]) step (%[[AONE]]) {
 // CIR-HOST: cir.call @{{.*}}during
 // CIR-HOST: omp.yield
 // CIR-HOST: }
@@ -166,7 +174,7 @@ void combined_target_parallel_for() {
 // CIR-HOST: } {omp.combined}
 
 // CIR-DEVICE: cir.func{{.*}}@combined_target_parallel_for
-// CIR-DEVICE: omp.target kernel_type(generic) {
+// CIR-DEVICE: omp.target kernel_type(spmd) {
 // CIR-DEVICE: omp.parallel {
 // tripCount = (10 - (0 - 1 + 1)) / 1 = 10
 // CIR-DEVICE: %[[HI:.*]] = cir.const #cir.int<10> : !s32i
@@ -192,9 +200,16 @@ void combined_target_parallel_for() {
 
 // 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.
+// both paths. This is a target SPMD construct, so CIR's kernel-launch
+// arguments carry the host_eval'd trip count (10) in the trip count field
+// (index 8); OGCG doesn't forward a host-computed trip count here, so it
+// leaves that field 0.
 //
 // LLVM-HOST-LABEL: define {{.*}} void @target_parallel_for(
+// LLVM-HOST:         %[[KARGS:.*]] = alloca %struct.__tgt_kernel_arguments, 
align 8
+// LLVM-HOST:         %[[TRIPCOUNT_GEP:.*]] = getelementptr inbounds nuw 
%struct.__tgt_kernel_arguments, ptr %[[KARGS]], i32 0, i32 8
+// LLVM-HOST-CIR-NEXT:  store i64 10, ptr %[[TRIPCOUNT_GEP]], align 8
+// LLVM-HOST-OGCG-NEXT: store i64 0, ptr %[[TRIPCOUNT_GEP]], align 8
 // LLVM-HOST:         call i32 @__tgt_target_kernel(
 // LLVM-HOST:         br i1 %{{.*}}, label %[[FAILED:[^,]+]], label %{{.*}}
 // LLVM-HOST:       {{^}}[[FAILED]]:
@@ -207,6 +222,9 @@ void combined_target_parallel_for() {
 // 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:         %[[KARGS2:.*]] = alloca 
%struct.__tgt_kernel_arguments, align 8
+// LLVM-HOST-CIR:         %[[TRIPCOUNT_GEP2:.*]] = getelementptr inbounds nuw 
%struct.__tgt_kernel_arguments, ptr %[[KARGS2]], i32 0, i32 8
+// LLVM-HOST-CIR-NEXT:    store i64 10, ptr %[[TRIPCOUNT_GEP2]], align 8
 // LLVM-HOST-CIR:         call i32 @__tgt_target_kernel(
 // LLVM-HOST-CIR:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label 
%{{.*}}
 // LLVM-HOST-CIR:       {{^}}[[FAILED2]]:
@@ -265,6 +283,9 @@ void combined_target_parallel_for() {
 // 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:         %[[KARGS2:.*]] = alloca 
%struct.__tgt_kernel_arguments, align 8
+// LLVM-HOST-OGCG:         %[[TRIPCOUNT_GEP2:.*]] = getelementptr inbounds nuw 
%struct.__tgt_kernel_arguments, ptr %[[KARGS2]], i32 0, i32 8
+// LLVM-HOST-OGCG-NEXT:    store i64 0, ptr %[[TRIPCOUNT_GEP2]], align 8
 // LLVM-HOST-OGCG:         call i32 @__tgt_target_kernel(
 // LLVM-HOST-OGCG:         br i1 %{{.*}}, label %[[FAILED2:[^,]+]], label 
%{{.*}}
 // LLVM-HOST-OGCG:       {{^}}[[FAILED2]]:
@@ -327,10 +348,10 @@ void combined_target_parallel_for() {
 // 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)
+// Unlike target-parallel.c's generic kernel, this is an SPMD kernel, so
+// neither path needs a wrapper here: CIR also passes null, since every
+// thread already runs the parallel region directly.
+// LLVM-DEVICE:       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()
 
@@ -356,8 +377,7 @@ void combined_target_parallel_for() {
 // 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_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-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to