Nvc++ 26.5 miscompilation when using a class-type pointer wrapper at -O2 (type-based alias analysis)

Godbolt link: click HERE
---------------

Summary

This was found with the Boost.Intrusive test suite: slist_test (an intrusive singly linked list with a fancy void_pointer) segfaults or produces wrong lists when Boost is built with nvc++ -fast (b2’s pgi toolset release build), while it passes with gcc, clang and MSVC and under all sanitizers.

Attached a reduced program that reproduces the bug: it builds a singly linked list of three nodes and then walks it. The link field is a class type that wraps a raw pointer (my_ptr<node>). At -O2, -O3 and -fast the list comes out empty: the store prev->next_ = n that links the first element to the header node is lost. EDG 6.x (GNU mode) compiles fine in Godbolt.

The trigger is a struct that holds a node (which contains a my_ptr<node>) followed by an unused member of the same type my_ptr<node>. Removing that member, moving it before the node, or giving it any other type (node*, void*, long, my_ptr<int>, a different wrapper struct) makes the program correct.

Reproducer (nvcpp_fancyptr_min.cpp)

#include <cstdio>

template <class T>
struct my_ptr
{
   T *m_ptr;
   my_ptr() : m_ptr(0) {}
   T *operator->() const { return m_ptr; }
};

struct node { my_ptr<node> next_; int value_; };

struct slist
{
   node         root_;   // header node: root_.next_ is the first element
   my_ptr<node> last_;   // never read or written - but required for the bug

   my_ptr<node> root_ptr() { my_ptr<node> p; p.m_ptr = &root_; return p; }

   void assign(node *f, node *l)
   {
      my_ptr<node> prev = root_ptr();
      for (; f != l; ++f) {
         my_ptr<node> n; n.m_ptr = f;
         n->next_    = prev->next_;    // link n after prev
         prev->next_ = n;
         prev = n;
      }
   }
};

int main()
{
   node values[5];
   for (int i = 0; i < 5; ++i) values[i].value_ = i + 1;

   slist l;
   l.assign(values + 2, values + 5);       // expected list: 3 -> 4 -> 5

   int got[16], n = 0;
   for (my_ptr<node> it = l.root_.next_; it.m_ptr != 0 && n < 16; it = it->next_, ++n)
      got[n] = it->value_;

   const bool ok = n == 3 && got[0] == 3 && got[1] == 4 && got[2] == 5;
   std::printf("got [");
   for (int k = 0; k < n; ++k) std::printf("%s%d", k ? "," : "", got[k]);
   std::printf("]  expected [3,4,5]  %s\n", ok ? "ok" : "<<< WRONG");
   return !ok;
}

Results

nvc++ -std=c++17 -O0                        got [3,4,5]  ok
nvc++ -std=c++17 -O1                        got [3,4,5]  ok
nvc++ -std=c++17 -O2                        got []       <<< WRONG
nvc++ -std=c++17 -O3                        got []       <<< WRONG
nvc++ -std=c++17 -fast                      got []       <<< WRONG
nvc++ -std=c++17 -fast -fno-strict-aliasing got [3,4,5]  ok
nvc++ -std=c++17 -fast -alias=traditional   got [3,4,5]  ok
nvc++ -std=c++17 -fast -Mnoautoinline       got []       <<< WRONG
g++ 15.2 -O3                                ok
clang++ 21.1 -O3                            ok
clang++ 21.1 -O2 -fsanitize=type            ok, no report
clang++ 21.1 -O2 -fsanitize=address,undefined  ok, no report

Hi igaztanaga,

Thanks for the report! I was able to reproduce the error here and wrote a issue report, TPR #38962. We’ll have engineering investigate.

-Mat