[PATCH] D68063: Propeller: LLVM support for basic block sections

Sriraman Tallam via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 25 17:02:21 PDT 2019


tmsriram created this revision.
tmsriram added a reviewer: mehdi_amini.
Herald added subscribers: tschuett, hiraditya, aprantl.
Herald added a project: LLVM.

This is part of the Propeller framework to do post link code layout optimizations. 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.

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 bytes. The number of basic blocks can be potentially very large compared to the size of functions and can bloat object sizes significantly. For instance, the clang binary contains 1.5M basic blocks from approximately 700K functions.

3. 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 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 byte. 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 everywhere.

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.

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), we force relocations against symbols and not sections. Moreover, in cases where the range is represented in DWARF as start and length, we defer the length calculation to the link stage, 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. Since the number of basic blocks is large, the labeling bloats the symbol table sizes and the string table sizes significantly. While the binary size does increase this does not affect performance as the symbol table is not loaded in memory during run-timea, the 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.bb.foo", "aa.bb.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 %.


Repository:
  rL LLVM

https://reviews.llvm.org/D68063

Files:
  llvm/include/llvm/CodeGen/AsmPrinterHandler.h
  llvm/include/llvm/CodeGen/CommandFlags.inc
  llvm/include/llvm/CodeGen/MachineBasicBlock.h
  llvm/include/llvm/CodeGen/MachineFunction.h
  llvm/include/llvm/CodeGen/TargetFrameLowering.h
  llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
  llvm/include/llvm/IR/BasicBlock.h
  llvm/include/llvm/IR/DebugInfoMetadata.h
  llvm/include/llvm/IR/Function.h
  llvm/include/llvm/MC/MCAsmInfo.h
  llvm/include/llvm/MC/MCDwarf.h
  llvm/include/llvm/MC/MCTargetOptions.h
  llvm/include/llvm/Target/TargetLoweringObjectFile.h
  llvm/include/llvm/Target/TargetMachine.h
  llvm/include/llvm/Target/TargetOptions.h
  llvm/include/llvm/Transforms/Utils/ModuleUtils.h
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
  llvm/lib/CodeGen/AsmPrinter/DwarfException.h
  llvm/lib/CodeGen/CFIInstrInserter.cpp
  llvm/lib/CodeGen/CodeGenPrepare.cpp
  llvm/lib/CodeGen/LLVMTargetMachine.cpp
  llvm/lib/CodeGen/MachineBasicBlock.cpp
  llvm/lib/CodeGen/MachineBlockPlacement.cpp
  llvm/lib/CodeGen/MachineFunction.cpp
  llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
  llvm/lib/IR/BasicBlock.cpp
  llvm/lib/IR/Globals.cpp
  llvm/lib/IR/Mangler.cpp
  llvm/lib/MC/ELFObjectWriter.cpp
  llvm/lib/MC/MCDwarf.cpp
  llvm/lib/MC/MCTargetOptions.cpp
  llvm/lib/Target/AArch64/AArch64FrameLowering.h
  llvm/lib/Target/TargetLoweringObjectFile.cpp
  llvm/lib/Target/X86/X86FrameLowering.cpp
  llvm/lib/Target/X86/X86FrameLowering.h
  llvm/lib/Transforms/Utils/ModuleUtils.cpp
  llvm/test/CodeGen/X86/basicblock-sections.ll
  llvm/test/DebugInfo/X86/basicblock-sections-cfi.ll
  llvm/test/DebugInfo/X86/basicblock-sections-cfiinstr.ll
  llvm/test/DebugInfo/X86/basicblock-sections_1.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68063.221865.patch
Type: text/x-patch
Size: 83710 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190926/6765f5f4/attachment.bin>


More information about the llvm-commits mailing list