From ea3364bf85ef72001b8db3c6fd2dc34f9311b111 Mon Sep 17 00:00:00 2001 From: Quentin Colombet Date: Thu, 18 Apr 2019 18:28:30 +0000 Subject: [BlockExtractor] Extend the file format to support the grouping of basic blocks Prior to this patch, each basic block listed in the extrack-blocks-file would be extracted to a different function. This patch adds the support for comma separated list of basic blocks to form group. When the region formed by a group is not extractable, e.g., not single entry, all the blocks of that group are left untouched. Let us see this new format in action (comments are not part of the file format): ;; funcName bbName[,bbName...] foo bb1 ;; Extract bb1 in its own function foo bb2,bb3 ;; Extract bb2,bb3 in their own function bar bb1,bb4 ;; Extract bb1,bb4 in their own function bar bb2 ;; Extract bb2 in its own function Assuming all regions are extractable, this will create one function and thus one call per region. Differential Revision: https://reviews.llvm.org/D60746 llvm-svn: 358701 --- llvm/lib/Transforms/IPO/BlockExtractor.cpp | 85 ++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 28 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/IPO/BlockExtractor.cpp b/llvm/lib/Transforms/IPO/BlockExtractor.cpp index eb12d032c52..31c09ebe43f 100644 --- a/llvm/lib/Transforms/IPO/BlockExtractor.cpp +++ b/llvm/lib/Transforms/IPO/BlockExtractor.cpp @@ -22,6 +22,7 @@ #include "llvm/Transforms/IPO.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/CodeExtractor.h" + using namespace llvm; #define DEBUG_TYPE "block-extractor" @@ -35,19 +36,25 @@ static cl::opt BlockExtractorFile( cl::opt BlockExtractorEraseFuncs("extract-blocks-erase-funcs", cl::desc("Erase the existing functions"), cl::Hidden); - namespace { class BlockExtractor : public ModulePass { - SmallVector Blocks; + SmallVector, 4> GroupsOfBlocks; bool EraseFunctions; - SmallVector, 32> BlocksByName; + /// Map a function name to groups of blocks. + SmallVector>, 4> + BlocksByName; public: static char ID; BlockExtractor(const SmallVectorImpl &BlocksToExtract, bool EraseFunctions) - : ModulePass(ID), Blocks(BlocksToExtract.begin(), BlocksToExtract.end()), - EraseFunctions(EraseFunctions) { + : ModulePass(ID), EraseFunctions(EraseFunctions) { + // We want one group per element of the input list. + for (BasicBlock *BB : BlocksToExtract) { + SmallVector NewGroup; + NewGroup.push_back(BB); + GroupsOfBlocks.push_back(NewGroup); + } if (!BlockExtractorFile.empty()) loadFile(); } @@ -81,8 +88,17 @@ void BlockExtractor::loadFile() { Buf->getBuffer().split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); for (const auto &Line : Lines) { - auto FBPair = Line.split(' '); - BlocksByName.push_back({FBPair.first, FBPair.second}); + SmallVector LineSplit; + Line.split(LineSplit, ' ', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + if (LineSplit.empty()) + continue; + SmallVector BBNames; + LineSplit[1].split(BBNames, ',', /*MaxSplit=*/-1, + /*KeepEmpty=*/false); + if (BBNames.empty()) + report_fatal_error("Missing bbs name"); + BlocksByName.push_back({LineSplit[0], {BBNames.begin(), BBNames.end()}}); } } @@ -129,33 +145,46 @@ bool BlockExtractor::runOnModule(Module &M) { } // Get all the blocks specified in the input file. + unsigned NextGroupIdx = GroupsOfBlocks.size(); + GroupsOfBlocks.resize(NextGroupIdx + BlocksByName.size()); for (const auto &BInfo : BlocksByName) { Function *F = M.getFunction(BInfo.first); if (!F) report_fatal_error("Invalid function name specified in the input file"); - auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) { - return BB.getName().equals(BInfo.second); - }); - if (Res == F->end()) - report_fatal_error("Invalid block name specified in the input file"); - Blocks.push_back(&*Res); + for (const auto &BBInfo : BInfo.second) { + auto Res = llvm::find_if(*F, [&](const BasicBlock &BB) { + return BB.getName().equals(BBInfo); + }); + if (Res == F->end()) + report_fatal_error("Invalid block name specified in the input file"); + GroupsOfBlocks[NextGroupIdx].push_back(&*Res); + } + ++NextGroupIdx; } - // Extract basic blocks. - for (BasicBlock *BB : Blocks) { - // Check if the module contains BB. - if (BB->getParent()->getParent() != &M) - report_fatal_error("Invalid basic block"); - LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting " - << BB->getParent()->getName() << ":" << BB->getName() - << "\n"); - SmallVector BlocksToExtractVec; - BlocksToExtractVec.push_back(BB); - if (const InvokeInst *II = dyn_cast(BB->getTerminator())) - BlocksToExtractVec.push_back(II->getUnwindDest()); - CodeExtractor(BlocksToExtractVec).extractCodeRegion(); - ++NumExtracted; - Changed = true; + // Extract each group of basic blocks. + for (auto &BBs : GroupsOfBlocks) { + SmallVector BlocksToExtractVec; + for (BasicBlock *BB : BBs) { + // Check if the module contains BB. + if (BB->getParent()->getParent() != &M) + report_fatal_error("Invalid basic block"); + LLVM_DEBUG(dbgs() << "BlockExtractor: Extracting " + << BB->getParent()->getName() << ":" << BB->getName() + << "\n"); + BlocksToExtractVec.push_back(BB); + if (const InvokeInst *II = dyn_cast(BB->getTerminator())) + BlocksToExtractVec.push_back(II->getUnwindDest()); + ++NumExtracted; + Changed = true; + } + Function *F = CodeExtractor(BlocksToExtractVec).extractCodeRegion(); + if (F) + LLVM_DEBUG(dbgs() << "Extracted group '" << (*BBs.begin())->getName() + << "' in: " << F->getName() << '\n'); + else + LLVM_DEBUG(dbgs() << "Failed to extract for group '" + << (*BBs.begin())->getName() << "'\n"); } // Erase the functions. -- cgit v1.2.3