Precompiled header "inline constexpr" with a lambda converted to a function pointer miscompiling
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
When using precompiled headers with an `inline constexpr` variable which has a function pointer inside can cause code gen issues
Using clang 22.1.3 (also tried 19.1.5 with the same issue)
```
clang version 22.1.3 (https://github.com/llvm/llvm-project e9846648fd6183ee6d8cbdb4502213fcf902a211)
```
pch.h
```c++
#pragma once
struct vtable_t
{
void (*destructor_ptr)() = +[](){};
};
inline constexpr vtable_t empty_vtable{};
```
test.cpp
```c++
#include "pch.h"
void (*otherfp)();
void testfunc()
{
const vtable_t * vptr = &empty_vtable;
otherfp();
vptr->destructor_ptr();
}
```
Compiling
```
clang -x c++-header -std=c++20 -O2 -Xclang -emit-pch pch.h -o pch.pch
clang -c -std=c++20 -O2 -include-pch pch.pch test.cpp -o test-pch.obj
clang -c -std=c++20 -O2 test.cpp -o test-nopch.obj
```
With pch x86-64 output
```
0000000000000000 :
0: 48 83 ec 28 subq $0x28, %rsp
4: ff 15 00 00 00 00 callq *(%rip) # 0xa
0000000000000006: IMAGE_REL_AMD64_REL32 ?otherfp@@3P6AXXZEA
a: cc int3
```
Without pch (still same header file)
```
0000000000000000 :
0: 48 ff 25 00 00 00 00 jmpq *(%rip) # 0x7
0000000000000003: IMAGE_REL_AMD64_REL32 ?otherfp@@3P6AXXZEA
```
Contributor guide
Research direction
Start by reproducing the issue with pch.h and test.cpp using the two clang compilation commands, then compare the generated x86-64 output for PCH and non-PCH builds. Trace the compiler path responsible for preserving the call through empty_vtable and verify that the PCH build emits the same required behavior as the non-PCH build.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100