[PATCH] D107534: Garbage-collect empty basic blocks after code generation.

Rahman Lavaee via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 8 11:11:32 PDT 2023


rahmanl created this revision.
rahmanl added a reviewer: tmsriram.
Herald added subscribers: hiraditya, mgorny.
rahmanl updated this revision to Diff 364355.
rahmanl added a comment.
Herald added subscribers: libc-commits, wrengr, Chia-hungDuan, ormris, dcaballe, cota, teijeong, frasercrmck, dexonsmith, rdzhabarov, tatianashp, ThomasRaoux, dang, jdoerfert, AlexeySotkin, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, kerbowa, liufengdb, aartbik, lucyrfox, mgester, arpith-jacob, csigg, antiagainst, shauheen, mravishankar, rriddle, mehdi_amini, luismarques, apazos, sameer.abuasal, pengfei, s.egerton, dmgreen, Jim, jocewei, PkmX, jfb, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, niosHD, cryptoad, sabuasal, simoncook, johnrusso, rbar, asb, kbarton, fedor.sergeev, nhaehnle, jvesely, nemanjai, qcolombet, jholewinski.
Herald added a reviewer: bollu.
Herald added a reviewer: antiagainst.
Herald added a reviewer: aartbik.
Herald added a reviewer: sscalpone.
Herald added a reviewer: ftynse.
Herald added a reviewer: aartbik.
Herald added a reviewer: aartbik.
Herald added a reviewer: awarzynski.
Herald added a reviewer: clementval.
Herald added a project: libc-project.
rahmanl updated this revision to Diff 364356.
rahmanl updated this revision to Diff 364357.
rahmanl updated this revision to Diff 364360.
rahmanl removed reviewers: bollu, antiagainst, aartbik, sscalpone, ftynse, awarzynski, clementval.
rahmanl removed a project: libc-project.
rahmanl removed subscribers: jholewinski, qcolombet, nemanjai, jvesely, nhaehnle, fedor.sergeev, kbarton, asb, rbar, johnrusso, simoncook, sabuasal, cryptoad, niosHD, jrtc27, zzheng, edward-jones, rogfer01, MartinMosbeck, brucehoult, the_o, jfb, PkmX, jocewei, Jim, dmgreen, s.egerton, pengfei, sameer.abuasal, apazos, luismarques, mehdi_amini, rriddle, mravishankar, shauheen, antiagainst, csigg, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, kerbowa, Joonsoo, grosul1, Kayjukh, jurahul, msifontes, AlexeySotkin, jdoerfert, dang, ThomasRaoux, tatianashp, rdzhabarov, dexonsmith, frasercrmck, teijeong, cota, dcaballe, ormris, Chia-hungDuan, wrengr, libc-commits, mgorny, hiraditya.
rahmanl updated this revision to Diff 548122.
Herald added a project: All.
rahmanl updated this revision to Diff 548287.
rahmanl retitled this revision from "Garbage-collect empty basic blocks for Propeller." to "Garbage-collect empty basic blocks after code generation.".
rahmanl edited the summary of this revision.
rahmanl added a reviewer: mtrofin.
rahmanl published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Add comments.


rahmanl added a comment.

Add comments.


rahmanl added a comment.

rebase.


rahmanl added a comment.

Revert unrelated changes.


rahmanl added a comment.

Update and rebase the patch.


rahmanl added a comment.

Add one more test.


Propeller maps profiles back to Machine IR via basic block addresses that are stored in the `llvm_bb_addr_map` section.
Empty basic blocks (basic blocks without real code) obfuscate the profile mapping because their addresses collide with their next basic blocks.
For instance, the fallthrough block of an empty block should always be adjacent to it. Otherwise, a completely unnecessary jump would be added.
This patch adds a MachineFunctionPass named `GCEmptyBasicBlocks` which attempts to garbage-collect the empty blocks before the `BasicBlockSections` pass.
This pass removes each empty basic block after redirecting its incoming edges and uses to its fall-through block.
The garbage-collection is not complete. We keep the empty block in two cases:

1. The empty block is an exception handling pad.
2. The empty block is the last block of the function.

The first one is extremely rare in production code(no cases for the clang binary) . Removing the block in this case requires modifying the exception handling structure which is not worth the additional complexity in the pass.
The second case does not need to be handled since no valid branches can jump to the block.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107534

Files:
  llvm/include/llvm/CodeGen/Passes.h
  llvm/include/llvm/InitializePasses.h
  llvm/lib/CodeGen/CMakeLists.txt
  llvm/lib/CodeGen/GCEmptyBasicBlocks.cpp
  llvm/lib/CodeGen/TargetPassConfig.cpp
  llvm/test/CodeGen/X86/basic-block-sections-eh.ll
  llvm/test/CodeGen/X86/basic-block-sections-empty-blocks.ll
  llvm/test/CodeGen/X86/gc-empty-blocks.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107534.548287.patch
Type: text/x-patch
Size: 9805 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230808/69e6e8b6/attachment-0001.bin>


More information about the llvm-commits mailing list