<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/59810>59810</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [pstl] std::exclusive_scan with execution policy does not work in-place
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lahwaacz
      </td>
    </tr>
</table>

<pre>
    The [exclusive.scan#8](https://eel.is/c++draft/exclusive.scan#8) section of the standard explicitly says:

> Remarks: result may be equal to first.

So there should be no problem with using std::exclusive_scan in "in-place" mode overwriting the storage. However, with the libstdc++ implementation that comes with GCC 12.2.0, it does not work with the overloads that use an execution policy, even if it is std::execution::seq. Consider this example:

```cpp
#include <algorithm>
#include <numeric>
#include <execution>
#include <vector>
#include <cassert>

int main()
{
    const int size = 10;
 std::vector<int> vec(size);

    // without execution policy
 std::fill(vec.begin(), vec.end(), 1);
    std::exclusive_scan(vec.begin(), vec.end(), vec.begin(), 0);
    assert(vec[0] == 0); // the first element should be 0
    assert(vec[size-1] == size-1); // the last element should be the sum

    // sequential execution policy
 std::fill(vec.begin(), vec.end(), 1);
 std::exclusive_scan(std::execution::seq, vec.begin(), vec.end(), vec.begin(), 0);
    assert(vec[0] == 0); // the first element should be 0
 assert(vec[size-1] == size-1); // the last element should be the sum
}
```

Note that the code fails on the last of the asserts. The std::exclusive_scan overload without a policy works and there is also no problem with using std::inclusive_scan in-place with any policy.

See also https://stackoverflow.com/q/74932677/4180822 where I asked about this behavior.

Bug report copy-pasted from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108236 because they use LLVM's PSTL headers.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVl9v4zYM_zTKC1HDlpM4fsjDJblsA27DsDvs9SBLjK1VllxRTpr79INsp82lzbaXAwYUbiuSP1L88Y8Eka4t4potNmyxm4k-N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