| Issue |
235425
|
| Summary |
[WholeProgramDevirt] inherited virtual call remains indirect because the type test uses the declaring class
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
yqtian-se
|
## Description
With full LTO and `-fwhole-program-vtables`, an inherited virtual call through `B&` remains indirect when an unrelated sibling class overrides the function. Clang emits the call site's type test for `A`, which declares the function, instead of retaining the more precise receiver type `B`.
## Reproducer
```cpp
#include <cstdio>
struct A {
int x = 1;
virtual int f() { return x; }
virtual ~A() {}
};
struct B : A {};
struct C : A { int f() override { return x + 2; } };
__attribute__((noinline)) int call(B& b) {
return b.f();
}
volatile int pick = 1;
int main() {
B b1, b2;
B& b = pick ? b1 : b2;
A* c = new C;
std::printf("%d\n", call(b) + c->f());
delete c;
}
```
## Command
```text
clang++ -O2 -flto -fuse-ld=lld -fwhole-program-vtables -fvisibility=hidden testcase.cpp -o test
objdump -dC test
```
## Actual behavior
The linked executable prints `4`, but `call(B&)` still loads the vtable and ends with an indirect jump:
```asm
call(B&):
mov (%rdi),%rax
jmp *(%rax)
```
The pre-link IR contains this type test:
```llvm
%0 = tail call i1 @llvm.type.test(ptr %vtable, metadata !"_ZTS1A")
```
Consequently, WholeProgramDevirt considers both `A::f()` and the sibling override `C::f()` for the slot and does not apply single-implementation devirtualization.
## Expected behavior
WholeProgramDevirt should resolve the call in `call(B&)` to `A::f()`. The receiver has static type `B&`, and neither `B` nor any class derived from `B` in the whole program overrides `f()`. The override in the sibling class `C` cannot be reached through a `B&` receiver.
Removing `C::f()` eliminates the indirect call. Declaring the same implementation directly in `B` also eliminates the indirect call. These controls change which type is attached to the WPD call site without changing the possible dynamic types of the `B&` receiver.
## Compiler version
```text
clang version 24.0.0git (/src/clang 94601109aeb6424e4167e7209078b4090d83edfa)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/bin
Build config: +assertions
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs