Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/SpeculateAnalyses.cpp
35266 views
//===-- SpeculateAnalyses.cpp --*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/SpeculateAnalyses.h"9#include "llvm/ADT/ArrayRef.h"10#include "llvm/ADT/DenseMap.h"11#include "llvm/ADT/STLExtras.h"12#include "llvm/ADT/SmallVector.h"13#include "llvm/Analysis/BlockFrequencyInfo.h"14#include "llvm/Analysis/BranchProbabilityInfo.h"15#include "llvm/Analysis/CFG.h"16#include "llvm/IR/PassManager.h"17#include "llvm/Passes/PassBuilder.h"18#include "llvm/Support/ErrorHandling.h"1920#include <algorithm>2122namespace {23using namespace llvm;24SmallVector<const BasicBlock *, 8> findBBwithCalls(const Function &F,25bool IndirectCall = false) {26SmallVector<const BasicBlock *, 8> BBs;2728auto findCallInst = [&IndirectCall](const Instruction &I) {29if (auto Call = dyn_cast<CallBase>(&I))30return Call->isIndirectCall() ? IndirectCall : true;31else32return false;33};34for (auto &BB : F)35if (findCallInst(*BB.getTerminator()) ||36llvm::any_of(BB.instructionsWithoutDebug(), findCallInst))37BBs.emplace_back(&BB);3839return BBs;40}41} // namespace4243// Implementations of Queries shouldn't need to lock the resources44// such as LLVMContext, each argument (function) has a non-shared LLVMContext45// Plus, if Queries contain states necessary locking scheme should be provided.46namespace llvm {47namespace orc {4849// Collect direct calls only50void SpeculateQuery::findCalles(const BasicBlock *BB,51DenseSet<StringRef> &CallesNames) {52assert(BB != nullptr && "Traversing Null BB to find calls?");5354auto getCalledFunction = [&CallesNames](const CallBase *Call) {55auto CalledValue = Call->getCalledOperand()->stripPointerCasts();56if (auto DirectCall = dyn_cast<Function>(CalledValue))57CallesNames.insert(DirectCall->getName());58};59for (auto &I : BB->instructionsWithoutDebug())60if (auto CI = dyn_cast<CallInst>(&I))61getCalledFunction(CI);6263if (auto II = dyn_cast<InvokeInst>(BB->getTerminator()))64getCalledFunction(II);65}6667bool SpeculateQuery::isStraightLine(const Function &F) {68return llvm::all_of(F, [](const BasicBlock &BB) {69return BB.getSingleSuccessor() != nullptr;70});71}7273// BlockFreqQuery Implementations7475size_t BlockFreqQuery::numBBToGet(size_t numBB) {76// small CFG77if (numBB < 4)78return numBB;79// mid-size CFG80else if (numBB < 20)81return (numBB / 2);82else83return (numBB / 2) + (numBB / 4);84}8586BlockFreqQuery::ResultTy BlockFreqQuery::operator()(Function &F) {87DenseMap<StringRef, DenseSet<StringRef>> CallerAndCalles;88DenseSet<StringRef> Calles;89SmallVector<std::pair<const BasicBlock *, uint64_t>, 8> BBFreqs;9091PassBuilder PB;92FunctionAnalysisManager FAM;93PB.registerFunctionAnalyses(FAM);9495auto IBBs = findBBwithCalls(F);9697if (IBBs.empty())98return std::nullopt;99100auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);101102for (const auto I : IBBs)103BBFreqs.push_back({I, BFI.getBlockFreq(I).getFrequency()});104105assert(IBBs.size() == BBFreqs.size() && "BB Count Mismatch");106107llvm::sort(BBFreqs, [](decltype(BBFreqs)::const_reference BBF,108decltype(BBFreqs)::const_reference BBS) {109return BBF.second > BBS.second ? true : false;110});111112// ignoring number of direct calls in a BB113auto Topk = numBBToGet(BBFreqs.size());114115for (size_t i = 0; i < Topk; i++)116findCalles(BBFreqs[i].first, Calles);117118assert(!Calles.empty() && "Running Analysis on Function with no calls?");119120CallerAndCalles.insert({F.getName(), std::move(Calles)});121122return CallerAndCalles;123}124125// SequenceBBQuery Implementation126std::size_t SequenceBBQuery::getHottestBlocks(std::size_t TotalBlocks) {127if (TotalBlocks == 1)128return TotalBlocks;129return TotalBlocks / 2;130}131132// FIXME : find good implementation.133SequenceBBQuery::BlockListTy134SequenceBBQuery::rearrangeBB(const Function &F, const BlockListTy &BBList) {135BlockListTy RearrangedBBSet;136137for (auto &Block : F)138if (llvm::is_contained(BBList, &Block))139RearrangedBBSet.push_back(&Block);140141assert(RearrangedBBSet.size() == BBList.size() &&142"BasicBlock missing while rearranging?");143return RearrangedBBSet;144}145146void SequenceBBQuery::traverseToEntryBlock(const BasicBlock *AtBB,147const BlockListTy &CallerBlocks,148const BackEdgesInfoTy &BackEdgesInfo,149const BranchProbabilityInfo *BPI,150VisitedBlocksInfoTy &VisitedBlocks) {151auto Itr = VisitedBlocks.find(AtBB);152if (Itr != VisitedBlocks.end()) { // already visited.153if (!Itr->second.Upward)154return;155Itr->second.Upward = false;156} else {157// Create hint for newly discoverd blocks.158WalkDirection BlockHint;159BlockHint.Upward = false;160// FIXME: Expensive Check161if (llvm::is_contained(CallerBlocks, AtBB))162BlockHint.CallerBlock = true;163VisitedBlocks.insert(std::make_pair(AtBB, BlockHint));164}165166const_pred_iterator PIt = pred_begin(AtBB), EIt = pred_end(AtBB);167// Move this check to top, when we have code setup to launch speculative168// compiles for function in entry BB, this triggers the speculative compiles169// before running the program.170if (PIt == EIt) // No Preds.171return;172173DenseSet<const BasicBlock *> PredSkipNodes;174175// Since we are checking for predecessor's backedges, this Block176// occurs in second position.177for (auto &I : BackEdgesInfo)178if (I.second == AtBB)179PredSkipNodes.insert(I.first);180181// Skip predecessors which source of back-edges.182for (; PIt != EIt; ++PIt)183// checking EdgeHotness is cheaper184if (BPI->isEdgeHot(*PIt, AtBB) && !PredSkipNodes.count(*PIt))185traverseToEntryBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,186VisitedBlocks);187}188189void SequenceBBQuery::traverseToExitBlock(const BasicBlock *AtBB,190const BlockListTy &CallerBlocks,191const BackEdgesInfoTy &BackEdgesInfo,192const BranchProbabilityInfo *BPI,193VisitedBlocksInfoTy &VisitedBlocks) {194auto Itr = VisitedBlocks.find(AtBB);195if (Itr != VisitedBlocks.end()) { // already visited.196if (!Itr->second.Downward)197return;198Itr->second.Downward = false;199} else {200// Create hint for newly discoverd blocks.201WalkDirection BlockHint;202BlockHint.Downward = false;203// FIXME: Expensive Check204if (llvm::is_contained(CallerBlocks, AtBB))205BlockHint.CallerBlock = true;206VisitedBlocks.insert(std::make_pair(AtBB, BlockHint));207}208209const_succ_iterator PIt = succ_begin(AtBB), EIt = succ_end(AtBB);210if (PIt == EIt) // No succs.211return;212213// If there are hot edges, then compute SuccSkipNodes.214DenseSet<const BasicBlock *> SuccSkipNodes;215216// Since we are checking for successor's backedges, this Block217// occurs in first position.218for (auto &I : BackEdgesInfo)219if (I.first == AtBB)220SuccSkipNodes.insert(I.second);221222for (; PIt != EIt; ++PIt)223if (BPI->isEdgeHot(AtBB, *PIt) && !SuccSkipNodes.count(*PIt))224traverseToExitBlock(*PIt, CallerBlocks, BackEdgesInfo, BPI,225VisitedBlocks);226}227228// Get Block frequencies for blocks and take most frequently executed block,229// walk towards the entry block from those blocks and discover the basic blocks230// with call.231SequenceBBQuery::BlockListTy232SequenceBBQuery::queryCFG(Function &F, const BlockListTy &CallerBlocks) {233234BlockFreqInfoTy BBFreqs;235VisitedBlocksInfoTy VisitedBlocks;236BackEdgesInfoTy BackEdgesInfo;237238PassBuilder PB;239FunctionAnalysisManager FAM;240PB.registerFunctionAnalyses(FAM);241242auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);243244llvm::FindFunctionBackedges(F, BackEdgesInfo);245246for (const auto I : CallerBlocks)247BBFreqs.push_back({I, BFI.getBlockFreq(I).getFrequency()});248249llvm::sort(BBFreqs, [](decltype(BBFreqs)::const_reference Bbf,250decltype(BBFreqs)::const_reference Bbs) {251return Bbf.second > Bbs.second;252});253254ArrayRef<std::pair<const BasicBlock *, uint64_t>> HotBlocksRef(BBFreqs);255HotBlocksRef =256HotBlocksRef.drop_back(BBFreqs.size() - getHottestBlocks(BBFreqs.size()));257258BranchProbabilityInfo *BPI =259FAM.getCachedResult<BranchProbabilityAnalysis>(F);260261// visit NHotBlocks,262// traverse upwards to entry263// traverse downwards to end.264265for (auto I : HotBlocksRef) {266traverseToEntryBlock(I.first, CallerBlocks, BackEdgesInfo, BPI,267VisitedBlocks);268traverseToExitBlock(I.first, CallerBlocks, BackEdgesInfo, BPI,269VisitedBlocks);270}271272BlockListTy MinCallerBlocks;273for (auto &I : VisitedBlocks)274if (I.second.CallerBlock)275MinCallerBlocks.push_back(std::move(I.first));276277return rearrangeBB(F, MinCallerBlocks);278}279280SpeculateQuery::ResultTy SequenceBBQuery::operator()(Function &F) {281// reduce the number of lists!282DenseMap<StringRef, DenseSet<StringRef>> CallerAndCalles;283DenseSet<StringRef> Calles;284BlockListTy SequencedBlocks;285BlockListTy CallerBlocks;286287CallerBlocks = findBBwithCalls(F);288if (CallerBlocks.empty())289return std::nullopt;290291if (isStraightLine(F))292SequencedBlocks = rearrangeBB(F, CallerBlocks);293else294SequencedBlocks = queryCFG(F, CallerBlocks);295296for (const auto *BB : SequencedBlocks)297findCalles(BB, Calles);298299CallerAndCalles.insert({F.getName(), std::move(Calles)});300return CallerAndCalles;301}302303} // namespace orc304} // namespace llvm305306307