This is an archive of the discontinued LLVM Phabricator instance.

[Propeller] Allow basic-block-sections and labels be used together by decoupling the handling of the two features.
DraftPublic

Authored by rahmanl on Feb 25 2021, 8:59 PM.
This is a draft revision that has not yet been submitted for review.

Details

Summary
Background

Today -split-machine-functions (MFS) and -fbasic-block-sections={all,list} cannot be combined with -basic-block-sections=labels (the labels option will be ignored).
The inconsistency comes from the way basic block address map -- the underlying mechanism for basic block labels -- encodes basic block addresses (https://lists.llvm.org/pipermail/llvm-dev/2020-July/143512.html). Specifically, basic block offsets are computed relative to the function begin symbol. This relies on functions being contiguous which is not the case for MFS and basic block section binaries. This means Propeller cannot use binary profiles collected from these binaries, which limits the applicability of Propeller for iterative optimization.

New Encoding for BB Address Map

To make the BB address map feature work with BB section binaries, we propose modifying the encoding of the BB address map as follows.

The current encoding emits the address of each function and its number of basic blocks, followed by basic block entries for each basic block.

Address of the functionAddress-> 8 bytes (pointer size)
Number of basic blocks in this functionNumBlocks-> ULEB128
BB entry #1
BB entry #2
...
BB entry #NumBlocks

To make this work for basic block sections, we treat each basic block section similar to a function, except that basic block sections of the same function must be encapsulated in the same structure so we can map all of them to their single function.

We modify the encoding to first emit the number of basic block sections (BB ranges) in the function. Then we emit the address map of each basic block section section as before: the base address of the section, its number of blocks, and BB entries for its basic block. The first section in the BB address map is always the function entry section.

Number of sections for this functionNumBBRanges)-> ULEB128
Section 0 begin addressBaseAddress[0]-> 8 bytes (pointer size)
Number of basic blocks in section 0NumBlocks[0]-> ULEB128
BB entries for Section 0

.
.
.

Section K begin addressBaseAddress[K]-> 8 bytes (pointer size)
Number of basic blocks in section KNumBlocks[K]-> ULEB128
BB entries for Section K

The encoding of basic block entries remains as before with the minor change that each basic block offset is now computed relative to the begin symbol of its containing BB section.

Option Processing

This patch adds a new boolean codegen option -basic-block-address-map. Correspondingly, the front-end flag -fbasic-block-address-map and LLD flag --lto-basic-block-address-map are introduced.
Analogously, we add a new TargetOption field BBAddrMap. This means BB address maps are either generated for all functions in the compiling unit, or for none (depending on TargetOptions::BBAddrMap).

This patch disables the old -fbasic-block-sections=labels value option but does not remove it. A subsequent patch will remove the obsolete option.

Pass Changes

We refactor the BasicBlockSections pass by separating the BB address map and BB sections handing to their own functions (named handleBBAddrMap and handleBBSections). handleBBSections renumbers basic blocks and places them in their assigned sections. handleBBAddrMap is invoked after handleBBSections (if requested) and only renumbers the blocks.

Tests
  • New tests added:
    1. A codgen test basic-block-labels-with-sections.ll to exercise the combination of -basic-block-address-map with -basic-block-sections=list.
    2. A driver sanity test for the -fbasic-block-address-map option.
    3. An LLD test for testing the --lto-basic-block-address-map option. This reuses the LLVM IR from lld/test/ELF/lto/basic-block-sections.ll.
  • Renamed and modified the two existing codegen tests for basic block address map (basic-block-sections-labels-functions-sections.ll and basic-block-sections-labels.ll)

Diff Detail

Event Timeline

rahmanl created this revision.Feb 25 2021, 8:59 PM
rahmanl retitled this revision from Decouple the basic block labels feature from basic block sections. to [Propeller] Decouple the basic block labels feature from '-basic-block-sections' option, to let BB labels used for BB section builds as well..Feb 26 2021, 8:45 PM
rahmanl edited the summary of this revision. (Show Details)
rahmanl edited the summary of this revision. (Show Details)Feb 26 2021, 9:02 PM
rahmanl updated this revision to Diff 330170.Mar 12 2021, 12:34 AM
  • Fix decoding in ELF.cpp
rahmanl updated this revision to Diff 330868.Mar 15 2021, 8:22 PM
  • Fix decoding in ELF.cpp
rahmanl edited the summary of this revision. (Show Details)Mar 17 2021, 5:01 PM
rahmanl updated this revision to Diff 331419.Mar 17 2021, 5:15 PM
  • Remove HasBBLabels.
rahmanl edited the summary of this revision. (Show Details)Mar 17 2021, 5:30 PM
rahmanl retitled this revision from [Propeller] Decouple the basic block labels feature from '-basic-block-sections' option, to let BB labels used for BB section builds as well. to [Propeller] Allow basic-block-sections and labels be used together by decoupling the handling of the two features..Mar 17 2021, 5:42 PM

First pass:

  • Check Description for typos like "MS" used instead of "MFS"
  • Maybe motivation can say that basic-block-sections=all and MFS cannot be used with labels
clang/include/clang/Basic/CodeGenOptions.def
93

Upstream will ask you to break up this patch, same thing happened with -funique-internal-linkage-names. I can think of the following break-up :

  1. Patch I will not delete -fbasic-block-sections=labels but just disable it and introduce the new option.
  2. Once Patch I is committed, Patch II should delete the old option.
clang/include/clang/Driver/Options.td
2625

Some bike shedding, can this be named basic block addr map, labels seems a residue from the old implementation.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
3288

This code is confusing, it seems like a remnant from the first implementation of labels before bb_addr_map?

llvm/lib/CodeGen/BasicBlockSections.cpp
51

Change comment to reflect new option.

rahmanl edited the summary of this revision. (Show Details)Mar 19 2021, 8:45 PM
rahmanl updated this revision to Diff 332504.Mar 22 2021, 8:08 PM
  • Keep, but disable the old -fbasic-block-sections=labels option.
rahmanl updated this revision to Diff 332513.Mar 22 2021, 8:46 PM
rahmanl marked 3 inline comments as done.
  • Refactor AsmPrinter.cpp
rahmanl updated this revision to Diff 332515.Mar 22 2021, 8:48 PM
  • Use -fbasic-block-address-map in option description.
rahmanl edited the summary of this revision. (Show Details)Mar 22 2021, 9:00 PM

Thanks for the comments @tmsriram. Please let me know if you have more comments and suggestions.

clang/include/clang/Basic/CodeGenOptions.def
93

Thanks. Adopted the strategy.

clang/include/clang/Driver/Options.td
2625

Thanks. Renamed to basic-block-address-map.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
3288

Actually, basic block labels (maybe local LBB symbols) are required for all basic blocks when the bb address map is requested. This is because we need to compute the basic block address offsets relative to the function entry symbol.

rahmanl updated this revision to Diff 332529.Mar 22 2021, 10:41 PM