Issue 242857
Summary [OpenMP][libomp] Dynamically scheduled loop after a cancelled `omp for` skips iterations / corrupts its reduction (OMP_CANCELLATION=true)
Labels new issue
Assignees
Reporter mclam009
    ### Summary

With `OMP_CANCELLATION=true`, after a dynamically scheduled worksharing loop is cancelled with `#pragma omp cancel for`, the **next** dynamically scheduled worksharing loop does not execute all of its iterations — even when it is in a new `parallel` region and contains no cancellation constructs. In the reproducer below the second loop typically runs 18–65 % of its iterations, and occasionally its `reduction(+:…)` result is garbage (e.g. `604878606531520` for a 10,000,000-iteration loop). `schedule(static)` / `schedule(static, chunk)` loops are not affected, GCC's libgomp is not affected, and the problem disappears without `OMP_CANCELLATION=true` or with one thread.

The cancelled loop has no `nowait` or `ordered` clause, so per the OpenMP specification every thread leaves it at its end, and a subsequent worksharing loop must execute all of its iterations.

### Reproducer (`t5.cpp`)

```cpp
#include <omp.h>
#include <cstdio>
int main() {
    const int N = 10'000'000;
    int bad = 0;
    for (int rep = 0; rep < 20; ++rep) {
        #pragma omp parallel
        {
            #pragma omp for schedule(dynamic, 1000)
            for (int i = 0; i < N; ++i) {
                #pragma omp cancellation point for
                if (i == N / 10) {
                    #pragma omp cancel for
                }
            }
        }
        long count = 0;  // Loop B: no cancel constructs; must execute all N iterations.
        #pragma omp parallel for schedule(dynamic, 1000) reduction(+:count)
        for (int i = 0; i < N; ++i) count += 1;
        if (count != N) { ++bad; std::printf("rep %d: loop B ran %ld of %d iterations\n", rep, count, N); }
    }
    std::printf("bad reps: %d / 20\n", bad);
}
```

```console
$ clang++ -O2 -fopenmp t5.cpp -o t5
$ OMP_CANCELLATION=true ./t5
rep 0: loop B ran 4379000 of 10000000 iterations
rep 1: loop B ran 1765000 of 10000000 iterations
rep 2: loop B ran 3193000 of 10000000 iterations
...
rep 19: loop B ran 604878606531520 of 10000000 iterations
bad reps: 20 / 20
```

**Expected:** `bad reps: 0 / 20`.

### Observations

- 60/60 bad repetitions over three runs (12 threads): 56 runs executed too few iterations, 4 returned a corrupted reduction value larger than N.
- Reproduces with `OMP_NUM_THREADS=2`; never with `OMP_NUM_THREADS=1`; reproduces at `-O0` as well as `-O2`.
- `OMP_CANCELLATION` unset: 0/20.
- Same source with GCC 16.2.0 / libgomp and `OMP_CANCELLATION=true`: 0/20.
- Schedule dependence (variant where the loop after the cancelled one contains `#pragma omp cancellation point for` but no `cancel`; 40 repetitions each): `dynamic, 1000`, `guided` and `nonmonotonic:dynamic` end early in 40/40; `static` and `static, 1000` in 0/40. In that variant a third, plain loop that follows is unaffected — it appears to be the first dynamically scheduled loop after the cancelled one that consumes the stale state.

### Environment

- Homebrew LLVM 23.1.5 (`clang++` and its bundled `libomp.dylib`)
- macOS 26.7.3 (25G342), Apple M2 Max, 12 cores
- Not yet tested on Linux.

Happy to test a patch or provide more data.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to