This is an archive of the discontinued LLVM Phabricator instance.

[NFC][BlockPlacement]Add an option to renumber blocks based on function layout order.
ClosedPublic

Authored by mingmingl on Nov 4 2022, 2:52 PM.

Details

Summary

Use case:

  • When block layout is visualized after MBP pass, the basic blocks are labeled in layout order; meanwhile blocks could be numbered in a different order.
  • As a result, it's hard to map between the graph and pass output. With this option on, the basic blocks are renumbered in function layout order.

This option is only useful when a function is to be visualized (i.e., when view options are on) to make it debugging only.

Use https://godbolt.org/z/5WTW36bMr as an example:

  • As MBP pass output (shown in godbolt output window), func2 is in a basic block numbered 2 (bb.2), and func1 is in a basic block numbered 3 (bb.3); bb.3 is a block with higher block frequency than bb.2, and bb.3 is placed before bb.2 in the functin layout.
  • Use [1] to get the dot graph (graph uploaded in [2]), the blocks are re-numbered.
    • func1 is in 'if.end' block, and labeled 1 in visualized dot; func2 is in 'if.then' blocks, and labeled 3 --> the labeled number and bb number won't map.
    • DOTGraphTraits<MachineBlockFrequencyInfo *>::getNodeLabel is where labeled numbers are based on function layout number, and called by graph writer. So call 'MachineFunction::RenumberBlocks' would make labeled number (in dot graph) and block number (in pass output) consistent with each other.

[1] ./bin/clang++ -O3 -S -mllvm -view-block-layout-with-bfi=count -mllvm -view-bfi-func-name=_Z9func_loopv -mllvm -print-after=block-placement -mllvm -filter-print-funcs=_Z9func_loopv test.c

[2]

Diff Detail

Event Timeline

mingmingl created this revision.Nov 4 2022, 2:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2022, 2:52 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
mingmingl requested review of this revision.Nov 4 2022, 2:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 4 2022, 2:52 PM
mingmingl edited the summary of this revision. (Show Details)Nov 4 2022, 2:52 PM

Does MIR text output show MBB number in the dump?

or show an example how the mapping is made harder without renumbering.

mingmingl edited the summary of this revision. (Show Details)Nov 6 2022, 9:11 AM

Does MIR text output show MBB number in the dump?

Yes, the basic blocks are named in format bb.<N>. Added an example in the summary.

or show an example how the mapping is made harder without renumbering.

Added an example in Phabricator summary.

mingmingl edited the summary of this revision. (Show Details)Nov 6 2022, 9:13 AM

I see. The DOT CFG graph dump uses the layout order instead of the MBB number. Another way to fix this is to use the following in the node label: BBName[MBBNum][layout-order], but making the MBB num and layout number consistent under an option is fine to me.

davidxl accepted this revision.Nov 6 2022, 9:17 PM

lgtm

This revision is now accepted and ready to land.Nov 6 2022, 9:17 PM

I see. The DOT CFG graph dump uses the layout order instead of the MBB number. Another way to fix this is to use the following in the node label: BBName[MBBNum][layout-order], but making the MBB num and layout number consistent under an option is fine to me.

Using 'BBName[MBBNum][layout-order]' would keep more information; but it requires more implementation work.

Going to submit this. Thanks for reviews!