adds std::launder which is supposed to be some kind of aliasing optimization

What is unclear to me is if we really need compiler support for that.
I have unfortunately not found many examples:

mentions something like:
#include <new>
foo ()
  struct X { const int n; };
  union U { X x; float f; };
  U u = {{ 1 }};
  int a = u.x.n;
  X *p = new (&u.x) X {2};
  int b = u.x.n;        // UB, needs std::launder(&u.x.n)
  return a + b;
but g++ handles it as returning 3 even without that.
So, do we need to do anything here even in the current gcc aliasing model,
which is very permissive (my understanding is that usually we treat all
writes as possibly placement new-ish changes)?

Then I found something like:
which of course doesn't work with -flifetime-dse=2, I'd strongly hope that
all those attempts there are UB even with std::launder.

Adding __builtin_launder as void * -> void * builtin (ECF_CONST) or perhaps
typegeneric one that returns the same pointer as given to it (and not
teaching alias analysis about what that builtin does) is certainly possible,
the question is how to expand it at RTL time (does it also need to be some
kind of opt barrier, say like __asm ("" : "+g" (ptr));, or not?


Reply via email to