[PATCH] D74361: [Clang] Undef attribute for global variables

Jon Chesterfield via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 02:25:26 PST 2020


JonChesterfield marked an inline comment as done.
JonChesterfield added inline comments.


================
Comment at: clang/test/CodeGenCXX/attr-no-zero-initializer.cpp:40
+// CHECK: @unt = global %struct.nontrivial undef
+nontrivial unt [[clang::no_zero_initializer]];
----------------
Quuxplusone wrote:
> Can you explain a bit about how this interacts with C++ constructors? Will this object not have its constructor called at startup; or is it that the constructor will be called but the memory simply won't have been zeroed //before// calling the constructor?
> 
> For [P1144 relocation](https://wg21.link/p1144) (D50114, D61761, etc), Anton Zhilin and I have been discussing the possibility of a similar-sounding attribute that would skip the constructor of a local variable altogether, allowing someone to write e.g.
> ```
> T relocate(T *source) {
>     [[unconstructed]] T result;
>     memcpy(result, source, sizeof(T));
>     return result;
> }
> ```
> If your attribute does exactly that, then I'm interested. If your attribute doesn't do that, but is occupying real estate that //implies// that it does, then I'm also interested.
This change is orthogonal I think. No change to object lifetime. Without the attribute, a global is zero initialized and then the constructor is called before main. With it, the global is undef before the constructor call.

A different attribute that suppresses the con(de)structor call entirely is also of interest to me. A freestanding implementation is likely to accept global constructors without warning, and emit code for them, but not actually run them at startup. That's not a great UX.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74361/new/

https://reviews.llvm.org/D74361





More information about the cfe-commits mailing list