Issue 240123
Summary [ThinLTO] A split LTO unit leaves a destructor's virtual call indirect despite a known vtable
Labels new issue
Assignees
Reporter yqtian-se
    With ThinLTO, enabling a split LTO unit leaves the virtual call in an inlined destructor indirect. The same call is direct with `-fno-split-lto-unit`, plain ThinLTO, and full LTO.

### Reproducer

`a.h`

```cpp
extern int s;
struct C { int x = 1; virtual ~C() { s = f(); } virtual int f(); };
```

`main.cpp`

```cpp
#include "a.h"
int s;
int main() { { C c; } return s; }
```

`b.cpp`

```cpp
#include "a.h"
int C::f() { return x; }
```

```text
clang++ -O2 -flto=thin -fuse-ld=lld -fwhole-program-vtables -fvisibility=hidden main.cpp b.cpp -o test
objdump -drC test
```

### Result

With the default split LTO unit, `main` calls through the vtable slot:

```asm
call *0x...(%rip)  # vtable for C+0x20
```

Adding `-fno-split-lto-unit` changes this to a direct call:

```asm
call C::f()
```

Plain ThinLTO without `-fwhole-program-vtables` and full LTO also emit a direct call. The split configuration should likewise resolve the call to `C::f()` or inline it.

### Technical details

In the ThinLTO pre-link pipeline, the destructor is inlined and the vptr store is forwarded to the load. The virtual dispatch therefore becomes a load from the constant slot `@_ZTV1C + 32`. An `llvm.type.test` for `@_ZTV1C + 16` remains, but the slot load is no longer based on the tested pointer, so WholeProgramDevirt does not recognize the call site.

In the split configuration, the imported module contains only:

```llvm
@_ZTV1C = external constant
```

The ThinLTO backend cannot fold the slot load. With `-fno-split-lto-unit`, the vtable is imported as `available_externally` with its initializer, and the same load folds to `C::f()`.

This appears related to #56001 and #185811, but the pre-link transformation and resulting IR are different.

Verified with Clang trunk commit `94601109aeb6424e4167e7209078b4090d83edfa`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to