[clang] [CIR][OpenMP] Add support for host_eval so that SPMD kernels can be used (PR #229259)

Akash Banerjee via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 11:54:34 PST 2026


================
@@ -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;
----------------
TIFitis wrote:

Thanks for fixing the region-isolation failure, but the fallback loop still uses a trip count computed outside the parallel region.

The loop now evaluates its step inside the parallel region, but still consumes the trip count computed outside that region. In this reproducer, `getStep()` returns 1 outside the parallel region and 2 inside it. CIR therefore executes four iterations with step 2, visiting `0, 2, 4, 6` instead of `0, 2`.

```c
#include <omp.h>
#include <stdio.h>

static int seen[8];
int getStep(void) { return omp_get_num_threads(); }
void work(int i) { seen[i] = 1; }

int main(void) {
  omp_set_dynamic(0);
#pragma omp target
#pragma omp parallel num_threads(2)
#pragma omp for
  for (int i = 0; i < 4; i += getStep())
    work(i);

  int failed = 0;
  printf("visited:");
  for (int i = 0; i < 8; ++i) {
    if (seen[i]) printf(" %d", i);
    if (seen[i] != (i == 0 || i == 2)) failed = 1;
  }
  printf("\nexpected: 0 2\n");
  return failed;
}
```

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


More information about the cfe-commits mailing list