Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/ImportedFunctionsInliningStatistics.cpp
35232 views
//===-- ImportedFunctionsInliningStats.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//===----------------------------------------------------------------------===//7// Generating inliner statistics for imported functions, mostly useful for8// ThinLTO.9//===----------------------------------------------------------------------===//1011#include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"12#include "llvm/ADT/STLExtras.h"13#include "llvm/IR/Function.h"14#include "llvm/IR/Module.h"15#include "llvm/Support/CommandLine.h"16#include "llvm/Support/Debug.h"17#include "llvm/Support/raw_ostream.h"18#include <algorithm>19#include <iomanip>20#include <sstream>21#include <string>2223using namespace llvm;2425namespace llvm {26cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(27"inliner-function-import-stats",28cl::init(InlinerFunctionImportStatsOpts::No),29cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",30"basic statistics"),31clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",32"printing of statistics for each inlined function")),33cl::Hidden, cl::desc("Enable inliner stats for imported functions"));34} // namespace llvm3536ImportedFunctionsInliningStatistics::InlineGraphNode &37ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {3839auto &ValueLookup = NodesMap[F.getName()];40if (!ValueLookup) {41ValueLookup = std::make_unique<InlineGraphNode>();42ValueLookup->Imported = F.hasMetadata("thinlto_src_module");43}44return *ValueLookup;45}4647void ImportedFunctionsInliningStatistics::recordInline(const Function &Caller,48const Function &Callee) {4950InlineGraphNode &CallerNode = createInlineGraphNode(Caller);51InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);52CalleeNode.NumberOfInlines++;5354if (!CallerNode.Imported && !CalleeNode.Imported) {55// Direct inline from not imported callee to not imported caller, so we56// don't have to add this to graph. It might be very helpful if you wanna57// get the inliner statistics in compile step where there are no imported58// functions. In this case the graph would be empty.59CalleeNode.NumberOfRealInlines++;60return;61}6263CallerNode.InlinedCallees.push_back(&CalleeNode);64if (!CallerNode.Imported) {65// We could avoid second lookup, but it would make the code ultra ugly.66auto It = NodesMap.find(Caller.getName());67assert(It != NodesMap.end() && "The node should be already there.");68// Save Caller as a starting node for traversal. The string has to be one69// from map because Caller can disappear (and function name with it).70NonImportedCallers.push_back(It->first());71}72}7374void ImportedFunctionsInliningStatistics::setModuleInfo(const Module &M) {75ModuleName = M.getName();76for (const auto &F : M.functions()) {77if (F.isDeclaration())78continue;79AllFunctions++;80ImportedFunctions += int(F.hasMetadata("thinlto_src_module"));81}82}83static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All,84const char *PercentageOfMsg,85bool LineEnd = true) {86double Result = 0;87if (All != 0)88Result = 100 * static_cast<double>(Fraction) / All;8990std::stringstream Str;91Str << std::setprecision(4) << Msg << ": " << Fraction << " [" << Result92<< "% of " << PercentageOfMsg << "]";93if (LineEnd)94Str << "\n";95return Str.str();96}9798void ImportedFunctionsInliningStatistics::dump(const bool Verbose) {99calculateRealInlines();100NonImportedCallers.clear();101102int32_t InlinedImportedFunctionsCount = 0;103int32_t InlinedNotImportedFunctionsCount = 0;104105int32_t InlinedImportedFunctionsToImportingModuleCount = 0;106int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;107108const auto SortedNodes = getSortedNodes();109std::string Out;110Out.reserve(5000);111raw_string_ostream Ostream(Out);112113Ostream << "------- Dumping inliner stats for [" << ModuleName114<< "] -------\n";115116if (Verbose)117Ostream << "-- List of inlined functions:\n";118119for (const auto &Node : SortedNodes) {120assert(Node->second->NumberOfInlines >= Node->second->NumberOfRealInlines);121if (Node->second->NumberOfInlines == 0)122continue;123124if (Node->second->Imported) {125InlinedImportedFunctionsCount++;126InlinedImportedFunctionsToImportingModuleCount +=127int(Node->second->NumberOfRealInlines > 0);128} else {129InlinedNotImportedFunctionsCount++;130InlinedNotImportedFunctionsToImportingModuleCount +=131int(Node->second->NumberOfRealInlines > 0);132}133134if (Verbose)135Ostream << "Inlined "136<< (Node->second->Imported ? "imported " : "not imported ")137<< "function [" << Node->first() << "]"138<< ": #inlines = " << Node->second->NumberOfInlines139<< ", #inlines_to_importing_module = "140<< Node->second->NumberOfRealInlines << "\n";141}142143auto InlinedFunctionsCount =144InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;145auto NotImportedFuncCount = AllFunctions - ImportedFunctions;146auto ImportedNotInlinedIntoModule =147ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;148149Ostream << "-- Summary:\n"150<< "All functions: " << AllFunctions151<< ", imported functions: " << ImportedFunctions << "\n"152<< getStatString("inlined functions", InlinedFunctionsCount,153AllFunctions, "all functions")154<< getStatString("imported functions inlined anywhere",155InlinedImportedFunctionsCount, ImportedFunctions,156"imported functions")157<< getStatString("imported functions inlined into importing module",158InlinedImportedFunctionsToImportingModuleCount,159ImportedFunctions, "imported functions",160/*LineEnd=*/false)161<< getStatString(", remaining", ImportedNotInlinedIntoModule,162ImportedFunctions, "imported functions")163<< getStatString("non-imported functions inlined anywhere",164InlinedNotImportedFunctionsCount,165NotImportedFuncCount, "non-imported functions")166<< getStatString(167"non-imported functions inlined into importing module",168InlinedNotImportedFunctionsToImportingModuleCount,169NotImportedFuncCount, "non-imported functions");170Ostream.flush();171dbgs() << Out;172}173174void ImportedFunctionsInliningStatistics::calculateRealInlines() {175// Removing duplicated Callers.176llvm::sort(NonImportedCallers);177NonImportedCallers.erase(llvm::unique(NonImportedCallers),178NonImportedCallers.end());179180for (const auto &Name : NonImportedCallers) {181auto &Node = *NodesMap[Name];182if (!Node.Visited)183dfs(Node);184}185}186187void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {188assert(!GraphNode.Visited);189GraphNode.Visited = true;190for (auto *const InlinedFunctionNode : GraphNode.InlinedCallees) {191InlinedFunctionNode->NumberOfRealInlines++;192if (!InlinedFunctionNode->Visited)193dfs(*InlinedFunctionNode);194}195}196197ImportedFunctionsInliningStatistics::SortedNodesTy198ImportedFunctionsInliningStatistics::getSortedNodes() {199SortedNodesTy SortedNodes;200SortedNodes.reserve(NodesMap.size());201for (const NodesMapTy::value_type &Node : NodesMap)202SortedNodes.push_back(&Node);203204llvm::sort(SortedNodes, [&](const SortedNodesTy::value_type &Lhs,205const SortedNodesTy::value_type &Rhs) {206if (Lhs->second->NumberOfInlines != Rhs->second->NumberOfInlines)207return Lhs->second->NumberOfInlines > Rhs->second->NumberOfInlines;208if (Lhs->second->NumberOfRealInlines != Rhs->second->NumberOfRealInlines)209return Lhs->second->NumberOfRealInlines >210Rhs->second->NumberOfRealInlines;211return Lhs->first() < Rhs->first();212});213return SortedNodes;214}215216217