This is part of the Propeller framework to do post link code layout optimizations.the parent patch of a series of patches to enable Basic Block Sections support in LLVM which is the building block for the Propeller post link optimization framework. Please see the RFC here: https://groups.google.com/forum/#!msg/llvm-dev/ef3mKzAdJ7U/1shV64BYBAAJ and the detailed RFC doc here: https://github.com/google/llvm-propeller/blob/plo-dev/Propeller_RFC.pdf.
This is one in the series of patches for Propeller.We introduce a new compiler option, -fbasicblock-sections=, which places every basic block in a unique ELF text section in the object file along with a symbol labeling the basic block. The linker can then order the basic block sections in any arbitrary sequence which when done correctly can encapsulate block layout, function layout and function splitting optimizations. However, there are a couple of challenges to be addressed for this to be feasible:
This patch adds support for Basic Block Sections in LLVM.
We introduce a new compiler option, -fbasicblock-sections, which places every basic block in a unique ELF text section in the object file along with a symbol labelling the basic block. The linker can then order the basic block sections in any arbitrary sequence which when done correctly can encapsulate block layout, function layout and function splitting optimizations. However, there are a couple of challenges to be addressed for this to be feasible:
1.# The compiler must not allow any implicit fall-through between any two adjacent basic blocks as they could be reordered at link time to be non-adjacent. In other words, the compiler must make a fall-through between adjacent basic blocks explicit by retaining the direct jump instruction that jumps to the next basic block. These branches can only be removed later in the linking phase after the final ordering is performed as determined by Propeller
2. Each additional section added to an object file bloats its size by tens of bytesby the linker after the blocks have been reordered.
# All inter-basic block branch targets would now need to be resolved by the linker as they cannot be calculated during compile time. The number of basic blocks can be potentially very large compared to the size of functions and can bloat object sizes significantlyis is done using static relocations which bloats the size of the object files. For instanceFurther, the clang binary contains 1.5M basic blocks from approximately 700K functions.
3ompiler tries to use short branch instructions on some ISAs for branch offsets that can be accommodated in one byte. All inter-basic block branch targets would now need to be resolved by the linker as they cannot be calculated during compile time.This is not possible with basic block sections as the offset is not determined at compile time, This is done using static relocations which bloats the size of the object files. Further, the compiler tries to use short branch instructions on some ISAs for branch offsets that can be accommodated in one byteand long branch instructions have to be used everywhere.
# Each additional section bloats object file sizes by tens of bytes. The number of basic blocks can be potentially very large compared to the size of functions and can bloat object sizes significantly. This is not possible with basic block sections as the offset is not determined at compile time, and long branch instructions have to be used everywhereOption fbasicblock-sections= also takes a file path which can be used to specify a subset of basic blocks that needs unique sections to keep the bloats small.
# Debug Info and CFI need special handling and will be presented as separate patches.
4. Debug Information (DebugInfo) and Call Frame Information (CFI) emission needs special handling with basic block sections. DebugInfo needs to be emitted with more relocations as basic block sections can break a function into potentially several disjoint pieces, and CFI needs to be emitted per basic block. This also bloats the object file and binary sizes significantly.
Updating DebugInfo and CFI:
Generating correct debug information (DebugInfo) and Call Frame Information (CFI) with basic block sections is challenging. Since basic blocks coming from different functions can be arbitrarily reordered and mixed together, we must appropriately update the DebugInfo and CFI.**Basic Block Labels**
DebugInfo is easier compared to CFI as we can leverage the DW_AT_ranges tag which allows description of a possibly non-contiguous range of addresses occupied by an entity. Thus, every basic block section forces a separate entry in DW_AT_ranges, plus two relocations pointing to symbols at the start and end of the basic block, respectively. DebugInfo will bloat object file sizes further with basic block sections.
On the other hand, CFI doesn’t provide any easy way to specify non-contiguous range of addresses occupied by a function – the DWARF standard explicitly requires emitting separate CFI Frame Descriptor Entries for each contiguous fragment of a function. Thus, the CFI information for all callee-saved registers (possibly including the frame pointer, if necessary) have to be emitted along with redefining the Call Frame Address (CFA), viz. where the current frame starts.
This causes a significant bloat of the .eh_frame sections, which is partially mitigated by de-duplicating common CFI instructions to the CFI Common Information Entry. We only de-duplicate CFI instructions with offset 0 from the beginning of the CFI frame, i.e. those that describe the CFI state before entering the frame.
Having support for non-contiguous ranges in CFI would significantly minimize the size overheads and complexity of supporting basic block sections.
To allow easy basic block rewriting in the linker (e.g. removing unnecessary fall-through jumps)With -fbasicblock-sections=labels, we force relocations against symbols and notor when a basic block is placed in a unique sections., Moreover, in cases where the range is represented in DWARF as start and length, we defer the length calculation to the link stage,it is labelled with a symbol. This allows easy mapping of virtual addresses from
by emitting an appropriate SIZE relocation instead of hardcoding the length directly in the object file by the compiler.
Labeling Basic Blocks :
Every basic block is labelled with a unique symbol as this allows easy mapping of virtual addresses from PMU profiles back to the corresponding basic blocks.PMU profiles back to the corresponding basic blocks. Since the number of basic blocks is large, Sincethe labeling bloats the number of basic blocks is large,symbol table sizes and the string table sizes significantly. the labeling bloatsWhile the symbol tablebinary sizes and the string table sizes significantly. does increase, While the binary size does increase thisit does not affect performance as the symbol table is not loaded in memory during run-timea,. theThe string table size bloat is kept very minimal using a unary naming scheme that uses string suffix compression. The basic blocks for function foo are named "a.bbBB.foo", "aa.bbBB.foo", . ... . This turns out to be very good for string table sizes and the bloat in the string table size for a very large binary is only 8 %.
~8 %. The naming also allows using the --symbol-ordering-file option in LLD to arbitrarily reorder the sections.