[clang] [Clang] No longer require complete types with __builtin_launder (PR #91070)

Mital Ashok via cfe-commits cfe-commits at lists.llvm.org
Fri May 10 05:46:18 PDT 2024


MitalAshok wrote:

Here's one scenario where it could make a difference in codegen <https://godbolt.org/z/dxvjzsWKr>:

```c++
// Header
struct X;
extern X x;
void f(X&);
inline void g() {
    f(x);
}
inline void h() {
    f(*__builtin_launder(__builtin_addressof(x)));
}

// Source
struct X {
    virtual void mem() { __builtin_printf("X::mem()\n"); }
};
void f(X& x) {
    x.mem();
}
void(*p[2])() = { g, h };  // Force inline functions to be generated
```

Even though Clang currently doesn't do this optimization, with `-fstrict-vtable-pointers`, `g()` is allowed to to devirtualise the call.

Also, we can't "complete" the type at all. See <https://github.com/llvm/llvm-project/pull/91070/files#diff-a8ef8d65dd30e14c0c43287a86c22b072ad29d04e85c2807602de9bcf2a26af1R160-R164>, where the template is not allowed to be instantiated by `launder`.

Maybe we could delay the code-gen to the end of the TU to see if it's completed by then? I do think it's an incredibly rare situation for some incomplete type to be `std::launder`ed and then later completed in the same TU.


https://github.com/llvm/llvm-project/pull/91070


More information about the cfe-commits mailing list