aboutsummaryrefslogtreecommitdiff
path: root/bolt/lib/Passes/IdenticalCodeFolding.cpp
AgeCommit message (Collapse)AuthorFilesLines
2024-12-16[BOLT] Add support for safe-icf (#116275)Alexander Yermolovich1-2/+105
Identical Code Folding (ICF) folds functions that are identical into one function, and updates symbol addresses to the new address. This reduces the size of a binary, but can lead to problems. For example when function pointers are compared. This can be done either explicitly in the code or generated IR by optimization passes like Indirect Call Promotion (ICP). After ICF what used to be two different addresses become the same address. This can lead to a different code path being taken. This is where safe ICF comes in. Linker (LLD) does it using address significant section generated by clang. If symbol is in it, or an object doesn't have this section symbols are not folded. BOLT does not have the information regarding which objects do not have this section, so can't re-use this mechanism. This implementation scans code section and conservatively marks functions symbols as unsafe. It treats symbols as unsafe if they are used in non-control flow instruction. It also scans through the data relocation sections and does the same for relocations that reference a function symbol. The latter handles the case when function pointer is stored in a local or global variable, etc. If a relocation address points within a vtable these symbols are skipped.
2024-05-31[BOLT] Remove mutable from BB::LayoutIndex (#93224)shaw young1-1/+4
Removed mutability from BB::LayoutIndex, subsequently removed const from BB::SetLayout, and changed BF::dfs to track visited blocks with a set as opposed to tracking and altering LayoutIndexes for more consistent code.
2024-03-05Use the new ThreadPoolInterface base class instead of the concrete ↵Mehdi Amini1-1/+1
implementation (NFC) (#84056)
2024-02-12[BOLT][NFC] Log through JournalingStreams (#81524)Amir Ayupov1-8/+8
Make core BOLT functionality more friendly to being used as a library instead of in our standalone driver llvm-bolt. To accomplish this, we augment BinaryContext with journaling streams that are to be used by most BOLT code whenever something needs to be logged to the screen. Users of the library can decide if logs should be printed to a file, no file or to the screen, as before. To illustrate this, this patch adds a new option `--log-file` that allows the user to redirect BOLT logging to a file on disk or completely hide it by using `--log-file=/dev/null`. Future BOLT code should now use `BinaryContext::outs()` for printing important messages instead of `llvm::outs()`. A new test log.test enforces this by verifying that no strings are print to screen once the `--log-file` option is used. In previous patches we also added a new BOLTError class to report common and fatal errors, so code shouldn't call exit(1) now. To easily handle problems as before (by quitting with exit(1)), callers can now use `BinaryContext::logBOLTErrorsAndQuitOnFatal(Error)` whenever code needs to deal with BOLT errors. To test this, we have fatal.s that checks we are correctly quitting and printing a fatal error to the screen. Because this is a significant change by itself, not all code was yet ported. Code from Profiler libs (DataAggregator and friends) still print errors directly to screen. Co-authored-by: Rafael Auler <rafaelauler@fb.com> Test Plan: NFC
2024-02-12[BOLT][NFC] Return Error from BinaryFunctionPass::runOnFunctions (#81521)Amir Ayupov1-1/+3
As part of the effort to refactor old error handling code that would directly call exit(1), in this patch we change the interface to `BinaryFunctionPass` to return an Error on `runOnFunctions()`. This gives passes the ability to report a serious problem to the caller (RewriteInstance class), so the caller may decide how to best handle the exceptional situation. Co-authored-by: Rafael Auler <rafaelauler@fb.com> Test Plan: NFC
2023-12-11[BOLT] Provide backwards compatibility for YAML profile with std::hash (#74253)Amir Ayupov1-3/+3
Provide backwards compatibility for YAML profile that uses `std::hash`: xxh3 hash is the default for newly produced profile (sets `std-hash: false`), whereas the profile that doesn't specify `std-hash` will be treated as `std-hash: true`, preserving old behavior.
2023-07-18[BOLT][NFC] Rename icf-dfs option variable to ICFUseDFSAmir Ayupov1-5/+5
Rename to avoid collision with profile-use-dfs. Differential Revision: https://reviews.llvm.org/D155513
2023-05-02[BOLT][NFC] Add hash computation for basic blocksspupyrev1-68/+1
Extending yaml profile format with block hashes, which are used for stale profile matching. To avoid duplication of the code, created a new class with a collection of utilities for computing hashes. Reviewed By: Amir Differential Revision: https://reviews.llvm.org/D144306
2023-02-27[BOLT] Change call count output for ICFMaksim Panchenko1-5/+7
ICF optimization runs multiple passes and the order in which functions are folded could be dependent on the order they are being processed. This order is indeterministic as functions are intermediately stored in std::unordered_map<>. Note that this order is mostly stable, but is not guaranteed to be and can change e.g. after switching to a different C++ library implementation. Because the processing (and folding) order is indeterministic, the previous way of calculating merged function call count could produce different results. Change the way we calculate the ICF call count to make it independent of the function folding/processing order. Mostly NFC as the output binary should remain the same, the change affects only the console output. Reviewed By: yota9 Differential Revision: https://reviews.llvm.org/D144807
2023-02-06[BOLT][NFC] Replace anonymous namespace functions with staticAmir Ayupov1-17/+15
Follow LLVM Coding Standards guideline on using anonymous namespaces (https://llvm.org/docs/CodingStandards.html#anonymous-namespaces) and use `static` modifier for function definitions. Reviewed By: #bolt, maksfb Differential Revision: https://reviews.llvm.org/D143124
2022-12-06[BOLT][NFC] Use std::optional in MCPlusBuilderAmir Ayupov1-2/+2
Reviewed By: maksfb, #bolt Differential Revision: https://reviews.llvm.org/D139260
2022-08-24[BOLT] Towards FunctionLayout const-correctnessFabian Parzefall1-10/+11
A const-qualified reference to function layout allows accessing non-const qualified basic blocks on a const-qualified function. This patch adds or removes const-qualifiers where necessary to indicate where basic blocks are used in a non-const manner. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D132049
2022-08-24Revert "[BOLT] Towards FunctionLayout const-correctness"Fabian Parzefall1-11/+10
This reverts commit 587d2653420d75ef10f30bd612d86f1e08fe9ea7.
2022-08-24[BOLT] Towards FunctionLayout const-correctnessFabian Parzefall1-10/+11
A const-qualified reference to function layout allows accessing non-const qualified basic blocks on a const-qualified function. This patch adds or removes const-qualifiers where necessary to indicate where basic blocks are used in a non-const manner. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D132049
2022-08-18[BOLT][AArch64] Ignore functions with islandsInfo during VeneerEliminarion ↵Denis Revunov1-0/+3
and ICF Differential Revision: https://reviews.llvm.org/D131881 Reviewed By: yota9
2022-07-30Use nullptr instead of 0 (NFC)Kazu Hirata1-1/+1
Identified with modernize-use-nullptr.
2022-07-16[BOLT] Add function layout classFabian Parzefall1-6/+12
This patch adds a dedicated class to keep track of each function's layout. It also lays the groundwork for splitting functions into multiple fragments (as opposed to a strict hot/cold split). Reviewed By: maksfb Differential Revision: https://reviews.llvm.org/D129518
2022-06-23[BOLT][NFC] Use range-based STL wrappersAmir Ayupov1-5/+4
Replace `std::` algorithms taking begin/end iterators with `llvm::` counterparts accepting ranges. Reviewed By: rafauler Differential Revision: https://reviews.llvm.org/D128154
2022-06-05[bolt] Remove unneeded cl::ZeroOrMore for cl::opt optionsFangrui Song1-6/+3
2021-12-28[BOLT][NFC] Fix braces usage in PassesAmir Ayupov1-20/+12
Summary: Refactor bolt/*/Passes to follow the braces rule for if/else/loop from [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html). (cherry picked from FBD33344642)
2021-12-21[BOLT][NFC] Fix file-description commentsMaksim Panchenko1-1/+3
Summary: Fix comments at the start of source files. (cherry picked from FBD33274597)
2021-12-14[BOLT][NFC] Reformat with clang-formatMaksim Panchenko1-20/+16
Summary: Selectively apply clang-format to BOLT code base. (cherry picked from FBD33119052)
2021-12-08[BOLT] Use more ADT data structures for BinaryFunctionMaksim Panchenko1-2/+2
Summary: Switched members of BinaryFunction to ADT where it was possible and made sense. As a result, the size of BinaryFunction on x86-64 Linux reduced from 1624 bytes to 1448. (cherry picked from FBD32981555)
2021-10-08Rebase: [NFC] Refactor sources to be buildable in shared modeRafael Auler1-0/+593
Summary: Moves source files into separate components, and make explicit component dependency on each other, so LLVM build system knows how to build BOLT in BUILD_SHARED_LIBS=ON. Please use the -c merge.renamelimit=230 git option when rebasing your work on top of this change. To achieve this, we create a new library to hold core IR files (most classes beginning with Binary in their names), a new library to hold Utils, some command line options shared across both RewriteInstance and core IR files, a new library called Rewrite to hold most classes concerned with running top-level functions coordinating the binary rewriting process, and a new library called Profile to hold classes dealing with profile reading and writing. To remove the dependency from BinaryContext into X86-specific classes, we do some refactoring on the BinaryContext constructor to receive a reference to the specific backend directly from RewriteInstance. Then, the dependency on X86 or AArch64-specific classes is transfered to the Rewrite library. We can't have the Core library depend on targets because targets depend on Core (which would create a cycle). Files implementing the entry point of a tool are transferred to the tools/ folder. All header files are transferred to the include/ folder. The src/ folder was renamed to lib/. (cherry picked from FBD32746834)