Issue 242584
Summary [Clang][OpenMP] Incorrect trip-count codegen skips loop execution
Labels clang
Assignees
Reporter amad00r
    Clang generates an incorrect trip count for this loop, causing it to be skipped:

```c
// clang -fopenmp --offload-arch=native -O3 %s
int main(void) {
    #pragma omp target teams distribute parallel for
    for (unsigned long long i = 0; i < ~0ULL; i += 0x1111111111111111ULL)
 __builtin_trap();
    return 0;
}
```

The loop condition is true initially, so execution should reach __builtin_trap() on the first iteration. Instead, the generated trip count is zero and the program returns normally.

Without the trap, the loop would execute 15 iterations. Its final increment reaches ~0ULL exactly, so the original loop counter does not overflow.

The overflow occurs in the generated trip-count calculation: `((Upper - Lower - 1) + Step) / Step`

For these bounds, adding Step wraps the unsigned 64-bit numerator to a value smaller than Step. The subsequent division therefore produces zero.

For a nonempty loop, computing the following equivalent _expression_ would avoid the overflowing addition: `((Upper - Lower - 1) / Step) + 1`

The affected calculation is in calculateNumIters in clang/lib/Sema/SemaOpenMP.cpp, used by ordinary OpenMP lowering.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to