https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125415

            Bug ID: 125415
           Summary: [g++][OpenMP] unexpected behavior with iterator loop
                    update "it = it - 1U" in omp parallel for
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: itou.tetsuya at fujitsu dot com
  Target Milestone: ---

The following C++ program uses a std::vector<int>::iterator in a decreasing
loop.
With OpenMP enabled, the loop using:

  it = it - 1U

does not execute as expected, and cnt remains 0.

Equivalent forms work correctly:
- without -fopenmp
- with "it = it - 1"
- with "it -= 1U"

This suggests an OpenMP loop handling/code generation bug for the iterator
update expression "it = it - 1U".

Test case:

#include <iostream>
#include <vector>
#include <omp.h>

int main() {
    const int n = 10;
    std::vector<int> vec(n);
    int cnt = 0;

#pragma omp parallel for schedule(static, 1)
    for (std::vector<int>::iterator it = vec.end();
         it > vec.begin();
         it = it - 1U) {
#pragma omp atomic
        cnt++;
    }

    if (cnt != n)
        std::cout << "test2 ng: it=it-1U: cnt=" << cnt << std::endl;

    std::cout << "pass" << std::endl;
    return 0;
}

Expected result:
pass

Actual result:
  test2 ng: it=it-1U: cnt=0
  pass

How to reproduce:
  g++ -fopenmp test.cc -o test
  ./test


https://godbolt.org/z/Mv138Pzq9

Reply via email to