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

            Bug ID: 113662
           Summary: [13/14 Regression] Wrong code for std::sort with fancy
                    pointer
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ostash at ostash dot kiev.ua
  Target Milestone: ---

Hello,

For the following snippet compiled with "-O3 -std=c++20"
---
#include <boost/container/vector.hpp>
#include <boost/interprocess/offset_ptr.hpp>

#include <algorithm>
#include <iostream>

struct Foo
{
public:
  uint32_t m1; 
  uint32_t m2;
  uint8_t m3;
};

bool operator<(const Foo& lhs, const Foo& rhs)
{
  return lhs.m1 < rhs.m1;
}

template <typename T>
class MyAllocator 
{
public:
  using value_type = T;
  using pointer = boost::interprocess::offset_ptr<T>;

  boost::interprocess::offset_ptr<T> allocate( std::size_t n ) {
      return boost::interprocess::offset_ptr<T>(a.allocate(n));
  }
  void deallocate(  boost::interprocess::offset_ptr<T> p, std::size_t n ) {
      a.deallocate(p.get(), n);
  }

  std::allocator<T> a;
};

int main()
{
    boost::container::vector<Foo, MyAllocator<Foo>> vec;
    vec.emplace_back().m1 = 4748;
    vec.emplace_back().m1 = 4687;
    vec.emplace_back().m1 = 4717;
    vec.emplace_back().m1 = 4779;

    std::cout << "before: " <<  vec.size() << '\n';
    for (const auto& x : vec)
        std::cout << std::to_string(x.m1) << '\n';

    std::sort(vec.begin(), vec.end());

    std::cout << "after: " <<  vec.size() << '\n';
    for (const auto& x : vec)
        std::cout << std::to_string(x.m1) << '\n';
}
---

we receive the following output:
---
before: 4
4748
4687
4717
4779
after: 4
4687
4717
4717
4779
---

I've managed to bisect this issue to the following commit:
429a7a88438cc80e7c58d9f63d44838089899b12 is the first bad commit
commit 429a7a88438cc80e7c58d9f63d44838089899b12
Author: Andrew MacLeod <amacl...@redhat.com>
Date:   Tue Mar 28 12:16:34 2023 -0400
   Add recursive GORI recompuations with a depth limit.
           PR tree-optimization/109154
           gcc/
           * gimple-range-gori.cc (gori_compute::may_recompute_p): Add depth
limit.
           * gimple-range-gori.h (may_recompute_p): Add depth param.
           * params.opt (ranger-recompute-depth): New param.
           gcc/testsuite/
           * gcc.dg/Walloca-13.c: Remove bogus warning that is now fixed.
gcc/gimple-range-gori.cc          | 30 ++++++++++++++++++++++--------
gcc/gimple-range-gori.h           |  4 ++--
gcc/params.opt                    |  5 +++++
gcc/testsuite/gcc.dg/Walloca-13.c |  2 +-
4 files changed, 30 insertions(+), 11 deletions(-)

I tried different versions of Boost to ensure that the problem is not coming
from offset_ptr. It looks like that it is possible to reproduce issue with "-O2
-ftree-partial-pre".

Everything works fine with std::vector or std::allocator.

I'd be glad to perform other tests if needed.

Reply via email to