1547 Commits

Author SHA1 Message Date
yavtuk
973cea7554 [BOLT] Search section based on relocation symbol
We need to search referenced section based on relocations symbol section
to properly match end section symbols. For example on some binaries we
can observe that init_array_end/fini_array_end might be "placed" in to
the gap and since no section could be found for address the relocation
would be skipped resulting in wrong ADRP imm after emitting new text
resulting in binary sigsegv.

Credits for the test to Vladislav Khmelevskii aka yota9.

(cherry picked from commit 0776fc32b15dc76c6b43c41fc4471552833265de)
2023-03-28 08:55:27 +02:00
Denis Revunov
a18482ac50 [BOLT][AArch64] Replace NOP with adrp in AdrRelaxationPass to preserve relative offsets.
Avoid replacing one adr instruction with two adrp+add by utilizing linker-provided nops
when they are present. By doing so we preserve relative offsets of next instructions
in a function which reduces chances to break undetected jump tables. This commit makes
release-mode lld-linked clang, lld and etc work after BOLT.

Reviewed By: rafauler, yota9

Differential Revision: https://reviews.llvm.org/D143887
2023-03-28 08:55:13 +02:00
Rafael Auler
c2ef3257df [BOLT][NFC] Remove C-style out of bounds array ref
Old code breaks build with libstdc++ with assertions. Fix it.

(cherry picked from commit 7768f63e5b7adc8a92b8f8041e9c1d298b011128)
2023-03-28 08:55:06 +02:00
Job Noorman
0b41062746 [BOLT] Reject symbols pointing to section end
Sometimes, symbols are present that point to the end of a section (i.e.,
one-past the highest valid address). Currently, BOLT either rejects
those symbols when they don't point to another existing section, or errs
when they do and the other section is not executable. I suppose BOLT
would accept the symbol when it points to an executable section.

In any case, these symbols should not be considered while discovering
functions and should not result in an error. This patch implements that.

Note that this patch checks explicitly for symbols whose value equals
the end of their section. It might make more sense to verify that the
symbol's value is within [section start, section end). However, I'm not
sure if this could every happen *and* its value does not equal the end.

Another way to implement this is to verify that the BinarySection we
find at the symbol's address actually corresponds to the symbol's
section. I'm not sure what the best approach is so feedback is welcome.

Reviewed By: yota9, rafauler

Differential Revision: https://reviews.llvm.org/D146215

(cherry picked from commit 54ab9541492d808ae4cf9130dd052d602b78ee32)
2023-03-24 08:44:46 +01:00
Vladislav Khmelevsky
32b8cc7031 [BOLT] Fix data reoder for aarch64
Use proper relocation for aarch64

Differential Revision: https://reviews.llvm.org/D144095
2023-03-24 08:44:37 +01:00
Sebastian Pop
bb6a97c15a [AArch64] fix bug #55005 handle DW_CFA_GNU_NegateRAState
GCC on AArch64 uses DW_CFA_GNU_NegateRAState for return address signing.

Differential Revision: https://reviews.llvm.org/D142572

(cherry picked from commit 9921197920fc3e9ad9605bd8fe0e835ca2dd41a5)
2023-03-23 15:33:14 +01:00
Amir Ayupov
1de305da42 [BOLT][Instrumentation] Preserve red zone for functions with tail calls only
Allow a function with tail calls only to clobber its red zone.

Fixes https://github.com/llvm/llvm-project/issues/61114.

Reviewed By: #bolt, yota9

Differential Revision: https://reviews.llvm.org/D145202

(cherry picked from commit 1e1dfbb94a20d81693176d885b5f773072bf0786)
2023-03-06 08:26:57 +01:00
Alexander Yermolovich
f230099c13 [BOLT][DWARF] Reuse entries in .debug_addr when not modified
In some binaries produced with ThinLTO there are CUs that share entry in
.debug_addr. Before we would generate a new entry for each. Which lead to binary
size increase. This changes the behavior so that we re-use entries in
.debug_addr.

Reviewed By: maksfb

Differential Revision: https://reviews.llvm.org/D142425
2023-01-24 15:55:03 -08:00
Benjamin Kramer
7557b83aa5 [BOLT] Use range-based implicit def/use accessors. NFCI 2023-01-24 23:12:41 +01:00
Jay Foad
dc4cb724c7 [BOLT] Fix build error after D142214 2023-01-23 13:02:21 +00:00
Amir Ayupov
dbb7316e02 [BOLT][NFC] Move getLTOCommonName to Utils
Reuse getLTOCommonName in components other than Profile (to be used in Core)

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D142259
2023-01-20 19:52:14 -08:00
Amir Ayupov
e5e07b01d8 [BOLT] Handle __uniq suffix added by -funique-internal-linkage-names
In profile matching, if `.__uniq` suffix added for internal linkage
symbols with `-funique-internal-linkage-names` prevents BOLT from
matching to a binary function, try to strip the suffix and perform
fuzzy name matching.

Follow-up to D124117.

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D142073
2023-01-20 19:23:06 -08:00
Amir Ayupov
86b47f1438 [BOLT][NFC] Move out ReorderFunctions::printStats
Break out stats-printing code from ReorderFunctions::reorder for brevity.

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D142250
2023-01-20 15:44:30 -08:00
Alexander Yermolovich
124ca880aa [BOLT][DWARF] Change loclist encoding to use base_addrx
Doing the same thing as for rangelists. Changing loclists to use base_addrx, it
slightly increases .debug_loclists, but reduces .debug_addr section.

| section             | clang-16.bolt.base | clang-16.bolt | raw       |  % |
| debug_loclists |  198208                  |  203398          | 5190     | 102%  |
| .debug_addr    | 14415808              | 14351448        | -64360 |99.5% |

Reviewed By: maksfb

Differential Revision: https://reviews.llvm.org/D141969
2023-01-20 11:30:34 -08:00
Amir Ayupov
69a9bbf106 [BOLT][NFC] Replace ambiguous BinarySection::isReadOnly with isWritable
Address feedback in https://reviews.llvm.org/D102284#2755060

Reviewed By: yota9

Differential Revision: https://reviews.llvm.org/D141733
2023-01-18 14:53:07 -08:00
Amir Ayupov
43f382a9f4 [BOLT][NFC] Simplify handleRelocation
Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D132089
2023-01-18 14:19:35 -08:00
Amir Ayupov
4a7966ea1b [BOLT][NFC] DataAggregator code cleanup
Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D139794
2023-01-18 13:44:44 -08:00
Amir Ayupov
706606a7df [BOLT][NFC] Remove AArch64 override for evaluateBranch
Use MCInstrAnalysis implementation

Reviewed By: #bolt, rafauler, yota9

Differential Revision: https://reviews.llvm.org/D141983
2023-01-18 13:22:57 -08:00
Arthur Eubanks
59bc64c667 [bolt][test] Add REQUIRES: asserts to jt-symbol-disambiguation-3.s
Or else it unexpectedly passes in non-assert builds of bolt.
2023-01-16 17:50:46 -08:00
Joe Loser
a288d7f937 [llvm][ADT] Replace uses of makeMutableArrayRef with deduction guides
Similar to how `makeArrayRef` is deprecated in favor of deduction guides, do the
same for `makeMutableArrayRef`.

Once all of the places in-tree are using the deduction guides for
`MutableArrayRef`, we can mark `makeMutableArrayRef` as deprecated.

Differential Revision: https://reviews.llvm.org/D141814
2023-01-16 14:49:37 -07:00
Maksim Panchenko
27cf96c4ec [BOLT] Minor refactoring for -print-sorted-by option
Only display used values for -print-sorted-by option when printing help.

Differential Revision: https://reviews.llvm.org/D141209
2023-01-12 13:25:36 -08:00
Rafael Auler
e09f6f41ca [BOLT] Add test case triggering JT assertion
Current case that triggers BOLT assertion. Marked XFAIL.
In this test case, we reproduce the behavior seen in gcc where the
base address of a jump table is decremented by some number and ends up
at the exact addess of a jump table from another function. After
linking, the instruction references another jump table and that
confuses BOLT.

Reviewed By: #bolt, Amir

Differential Revision: https://reviews.llvm.org/D138245
2023-01-11 16:50:16 -08:00
Alexander Yermolovich
7fc7934023 [llvm][dwwarf] Change CU/TU index to 64-bit
Changed contribution data structure to 64 bit. I added the 32bit and 64bit
accessors to make it explicit where we use 32bit and where we use 64bit. Also to
make sure sure we catch all the cases where this data structure is used.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D139379
2023-01-11 15:07:11 -08:00
Alexander Yermolovich
6a4a697e17 Revert "[llvm][dwwarf] Change CU/TU index to 64-bit"
This reverts commit fa3fa4d0d42326005dfd5887bf047b86904d3be6.
2023-01-11 14:41:24 -08:00
Denis Revunov
c8ff968b84 [BOLT][AArch64] add lock to FixRelaxations pass
Since the pass is multithreaded, BC.Ctx must be protected. Otherwise we get crashes when processing.

Reviewed by: yota9

Differential Revision: https://reviews.llvm.org/D141465
2023-01-11 05:05:42 -05:00
Alexander Yermolovich
fa3fa4d0d4 [llvm][dwwarf] Change CU/TU index to 64-bit
Changed contribution data structure to 64 bit. I added the 32bit and 64bit
accessors to make it explicit where we use 32bit and where we use 64bit. Also to
make sure sure we catch all the cases where this data structure is used.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D139379
2023-01-10 10:33:52 -08:00
serge-sans-paille
984b800a03
Move from llvm::makeArrayRef to ArrayRef deduction guides - last part
This is a follow-up to https://reviews.llvm.org/D140896, split into
several parts as it touches a lot of files.

Differential Revision: https://reviews.llvm.org/D141298
2023-01-10 11:47:43 +01:00
hezuoqiang
e3b47d31ae [BOLT] Modify the print option to a meaningful value
Using the option `-print-sorted-by=.` cause to core dump, so change to a legal value.

Reviewed By: maksfb

Differential Revision: https://reviews.llvm.org/D140847
2023-01-09 19:05:21 -08:00
Alexander Yermolovich
e22ff52c10 [BOLT][DWARF] Change rangelists to use DW_RLE_offset_pair
Before we always used DW_RLE_startx_length. This is not very efficient and leads
to bigger .debug_addr section. Changed it to use
DW_RLE_base_addressx/DW_RLE_offset_pair.

clang-16 build in debug mode
llvm-bolt ran on it with --update-debug-sections
| section | before | after | diff | % decrease |
| .debug_rnglists | 32732292 | 31986051 | -746241 | 2.3% |
| .debug_addr | 14415808 | 14184128 |  -231680 | 1.6% |

Reviewed By: maksfb

Differential Revision: https://reviews.llvm.org/D140439
2023-01-06 13:45:43 -08:00
Amir Ayupov
be08bb7755 [BOLT][CMake] Add merge-fdata to bolt component
Build and install `merge-fdata` tool as part of `bolt` component:
```
$ ninja bolt
# builds llvm-bolt, perf2bolt and merge-fdata

$ cmake --install . --component bolt --prefix $HOME/test-install-bolt
-- Install configuration: "Release"
-- Install configuration: "Release"
-- Installing: /home/aaupov/test-install-bolt/lib/libbolt_rt_instr.a
-- Installing: /home/aaupov/test-install-bolt/lib/libbolt_rt_hugify.a
-- Installing: /home/aaupov/test-install-bolt/lib/libbolt_rt_instr_osx.a
-- Installing: /home/aaupov/test-install-bolt/bin/llvm-bolt
-- Installing: /home/aaupov/test-install-bolt/bin/perf2bolt
-- Installing: /home/aaupov/test-install-bolt/bin/llvm-boltdiff
-- Installing: /home/aaupov/test-install-bolt/bin/merge-fdata
```

Fixes #57249.

Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D139972
2023-01-03 17:40:36 -08:00
Amir Ayupov
75c069584a [BOLT][Docs] Add Sphinx documentation
Add stub Sphinx documentation, with configuration copy-pasted from lld and
index page converted from bolt/README.md.

Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D140156
2023-01-03 17:39:09 -08:00
Amir Ayupov
f40d25dd8d [BOLT][NFC] Use llvm::reverse
Use llvm::reverse instead of `for (auto I = rbegin(), E = rend(); I != E; ++I)`

Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D140516
2023-01-03 17:32:11 -08:00
Amir Ayupov
6b05a62a6b [BOLT] Check no-LBR samples in mayHaveProfileData
No-LBR mode wasn't tested and slipped when mayHaveProfileData was added for
Lite mode. This enables processing of profiles collected without LBR and
converted with `perf2bolt -nl` option.

Test Plan:
bin/llvm-lit -a tools/bolt/test/X86/nolbr.s
https://github.com/rafaelauler/bolt-tests/pull/20

Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D140256
2023-01-03 14:43:36 -08:00
Kazu Hirata
e8d6c537ac [BOLT] Use std::optional instead of llvm::Optional (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-02 18:40:21 -08:00
Amir Ayupov
703d94d8f0 [BOLT] Respect -function-order in lite mode
Process functions listed in -function-order file even in lite mode.

Reviewed By: #bolt, maksfb

Differential Revision: https://reviews.llvm.org/D140435
2022-12-28 20:50:20 -08:00
Amir Ayupov
0224bdce92 [BOLT][TEST] Limit iterations in X86/exceptions-pic.test
The test has 3 invocations with 1M iterations each, which adds delay to fast
check-bolt testing. Reduce the number to 1K.

Reviewed By: #bolt, rafauler

Differential Revision: https://reviews.llvm.org/D139651
2022-12-22 19:47:28 -08:00
Vladislav Khmelevsky
17ed8f2928 [BOLT][AArch64] Handle adrp+ld64 linker relaxations
Linker might relax adrp + ldr got address loading to adrp + add for
local non-preemptible symbols (e.g. hidden/protected symbols in
executable). As usually linker doesn't change relocations properly after
relaxation, so we have to handle such cases by ourselves. To do that
during relocations reading we change LD64 reloc to ADD if instruction
mismatch found and introduce FixRelaxationPass that searches for ADRP+ADD
pairs and after performing some checks we're replacing ADRP target symbol
to already fixed ADDs one.

Vladislav Khmelevsky,
Advanced Software Technology Lab, Huawei

Differential Revision: https://reviews.llvm.org/D138097
2022-12-23 01:20:18 +04:00
Amir Ayupov
5bcd980137 [BOLT][NFC] Make DWOId std::optional
Reviewed By: #bolt, ayermolo

Differential Revision: https://reviews.llvm.org/D140450
2022-12-21 10:40:08 -08:00
Maksim Panchenko
be9d3edee8 [BOLT][NFC] Remove unused PrintInstructions argument
PrintInstructions was unused in BinaryFunction::print() and dump().

Reviewed By: Amir

Differential Revision: https://reviews.llvm.org/D140440
2022-12-20 15:57:13 -08:00
Archibald Elliott
f09cf34d00 [Support] Move TargetParsers to new component
This is a fairly large changeset, but it can be broken into a few
pieces:
- `llvm/Support/*TargetParser*` are all moved from the LLVM Support
  component into a new LLVM Component called "TargetParser". This
  potentially enables using tablegen to maintain this information, as
  is shown in https://reviews.llvm.org/D137517. This cannot currently
  be done, as llvm-tblgen relies on LLVM's Support component.
- This also moves two files from Support which use and depend on
  information in the TargetParser:
  - `llvm/Support/Host.{h,cpp}` which contains functions for inspecting
    the current Host machine for info about it, primarily to support
    getting the host triple, but also for `-mcpu=native` support in e.g.
    Clang. This is fairly tightly intertwined with the information in
    `X86TargetParser.h`, so keeping them in the same component makes
    sense.
  - `llvm/ADT/Triple.h` and `llvm/Support/Triple.cpp`, which contains
    the target triple parser and representation. This is very intertwined
    with the Arm target parser, because the arm architecture version
    appears in canonical triples on arm platforms.
- I moved the relevant unittests to their own directory.

And so, we end up with a single component that has all the information
about the following, which to me seems like a unified component:
- Triples that LLVM Knows about
- Architecture names and CPUs that LLVM knows about
- CPU detection logic for LLVM

Given this, I have also moved `RISCVISAInfo.h` into this component, as
it seems to me to be part of that same set of functionality.

If you get link errors in your components after this patch, you likely
need to add TargetParser into LLVM_LINK_COMPONENTS in CMake.

Differential Revision: https://reviews.llvm.org/D137838
2022-12-20 11:05:50 +00:00
Kazu Hirata
4501133d96 Ensure newlines at the end of files (NFC) 2022-12-16 23:36:51 -08:00
Guillaume Chatelet
828ce42a59 [Alignment] Use Align in SectionRef::getAlignment()
Differential Revision: https://reviews.llvm.org/D139110
2022-12-16 12:09:57 +00:00
Alexander Yermolovich
2afc90a2de [BOLT][DWARF] Fix for Handle zero size DW_TAG_inlined_subroutine
Managed to introduce an error when changing code to fix other tests and the unit
test was no adequate due to --nostdlib being passed in in llvm testing
enviroment.
Original diff: https://reviews.llvm.org/D132059

Updated a test to make sure that original address and the new address are
different.

Reviewed By: maksfb, #bolt

Differential Revision: https://reviews.llvm.org/D132782
2022-12-14 09:02:43 -08:00
Matt Arsenault
765f3cafa1 bolt: Update more sys::Wait calls 2022-12-14 12:00:41 -05:00
Matt Arsenault
6be2db6ca5 bolt: Try to fix build after sys::Program API change
Hopefully fixes build after 15a6e3c636977dc962a415c067182e6d57242116
2022-12-14 11:56:13 -05:00
Amir Ayupov
9cbd2959c1 [BOLT] Fix broken unittests 2022-12-11 22:34:40 -08:00
Amir Ayupov
1628daf6e7 [BOLT][NFC] Use std::optional in ShrinkWrapping 2022-12-11 22:13:47 -08:00
Amir Ayupov
76cfea0c47 [BOLT][NFC] Use std::optional for readDWARFExpressionTargetReg 2022-12-11 22:13:47 -08:00
Amir Ayupov
34e7d65f79 [BOLT][NFC] Use std::optional in DWARFRewriter 2022-12-11 22:13:47 -08:00
Amir Ayupov
72528ee4b4 [BOLT][NFC] Use std::optional in has*NameRegex 2022-12-11 22:13:47 -08:00