Commit e7eb43f
committed
[X86][ELF][LLD] Add R_X86_64_PCNEXT32 relocation and MO_PCNEXT32 flag for code prefetching
This adds support for the proposed R_X86_64_PCNEXT32 relocation and the @PCNEXT32 (MO_PCNEXT32) target operand flag in LLVM and LLD, including MC and LLD test coverage.
x86-64 psABI proposal discussion: https://groups.google.com/g/x86-64-abi/c/v_alXxMCTgI
## Relocation Behavior & Calculation
| Symbol State | Calculation | Notes |
| ------------ | ----------- | ----- |
| Defined | S + A - P | Identical to R_X86_64_PC32 |
| Undefined | 0 | No diagnostic error reported |
A 0 PC-relative displacement targets the instruction immediately following the relocated field. Consequently, an undefined reference degrades cleanly to a harmless, position-independent reference to the next instruction (a valid instruction boundary).
## Motivation
LLVM's Code-prefetch insertion (#166324) inserts prefetchit1 target(%rip) ahead of an anticipated instruction cache or front-end miss:
prefetchit1 __llvm_prefetch_target_<fn>_<bbid>_<cs>(%rip)
The target is an address inside a callee (often in another translation unit), so the compiler synthesizes a target symbol at that address and uses a PC-relative reference resolved by the linker.
However, the referenced target symbol may be undefined in the final build if the profile's <function, bb_id, callsite_index> triple fails to match the final image (due to function renaming, block structure changes, or altered callsite counts), or if the target is simply not linked in.
Previously, avoiding an undefined-symbol error required emitting a weak fallback definition immediately after the prefetch:
prefetchit1 __llvm_prefetch_target_foo_x_y(%rip)
.weak __llvm_prefetch_target_foo_x_y
__llvm_prefetch_target_foo_x_y:
While this allows standard ELF resolution without linker modifications (a real STB_GLOBAL overrides the weak fallback, and an absent target degrades to the fallback immediately following the instruction), it prevents the prefetch target itself from being a weak definition—the linker may mistakenly resolve against an undesired weak fallback rather than the desired weak target in another translation unit.
With R_X86_64_PCNEXT32, undefined targets resolve directly to displacement 0 (pointing to the next instruction without linker errors), removing the need for fallback definitions and allowing weak target definitions to function correctly.1 parent 9ec7974 commit e7eb43f
15 files changed
Lines changed: 89 additions & 3 deletions
File tree
- lld
- ELF
- Arch
- test/ELF
- llvm
- docs
- include/llvm/BinaryFormat/ELFRelocs
- lib/Target/X86
- MCTargetDesc
- test/MC/X86
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
576 | 576 | | |
577 | 577 | | |
578 | 578 | | |
| 579 | + | |
| 580 | + | |
579 | 581 | | |
580 | 582 | | |
581 | 583 | | |
| |||
654 | 656 | | |
655 | 657 | | |
656 | 658 | | |
657 | | - | |
| 659 | + | |
658 | 660 | | |
659 | 661 | | |
660 | 662 | | |
| |||
682 | 684 | | |
683 | 685 | | |
684 | 686 | | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
685 | 690 | | |
686 | 691 | | |
687 | 692 | | |
| |||
1182 | 1187 | | |
1183 | 1188 | | |
1184 | 1189 | | |
| 1190 | + | |
1185 | 1191 | | |
1186 | 1192 | | |
1187 | 1193 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
921 | 921 | | |
922 | 922 | | |
923 | 923 | | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
924 | 928 | | |
925 | 929 | | |
926 | 930 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
846 | 846 | | |
847 | 847 | | |
848 | 848 | | |
849 | | - | |
| 849 | + | |
| 850 | + | |
850 | 851 | | |
851 | 852 | | |
852 | 853 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
| 129 | + | |
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
764 | 764 | | |
765 | 765 | | |
766 | 766 | | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
767 | 778 | | |
768 | 779 | | |
769 | 780 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
49 | 50 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
397 | 402 | | |
398 | 403 | | |
399 | 404 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
212 | 212 | | |
213 | 213 | | |
214 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
215 | 218 | | |
216 | 219 | | |
217 | 220 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
0 commit comments