003 File Manager
Current Path:
/usr/src/contrib/llvm-project/llvm/lib/Transforms/IPO
usr
/
src
/
contrib
/
llvm-project
/
llvm
/
lib
/
Transforms
/
IPO
/
📁
..
📄
AlwaysInliner.cpp
(6.51 KB)
📄
ArgumentPromotion.cpp
(44.76 KB)
📄
Attributor.cpp
(81.04 KB)
📄
AttributorAttributes.cpp
(269.7 KB)
📄
BarrierNoopPass.cpp
(1.67 KB)
📄
BlockExtractor.cpp
(8.17 KB)
📄
CalledValuePropagation.cpp
(17.71 KB)
📄
ConstantMerge.cpp
(9.59 KB)
📄
CrossDSOCFI.cpp
(5.98 KB)
📄
DeadArgumentElimination.cpp
(42.84 KB)
📄
ElimAvailExtern.cpp
(3.14 KB)
📄
ExtractGV.cpp
(5.2 KB)
📄
ForceFunctionAttrs.cpp
(4.79 KB)
📄
FunctionAttrs.cpp
(55.56 KB)
📄
FunctionImport.cpp
(55.28 KB)
📄
GlobalDCE.cpp
(15.55 KB)
📄
GlobalOpt.cpp
(119.78 KB)
📄
GlobalSplit.cpp
(6.92 KB)
📄
HotColdSplitting.cpp
(26.27 KB)
📄
IPConstantPropagation.cpp
(10.68 KB)
📄
IPO.cpp
(4.94 KB)
📄
InferFunctionAttrs.cpp
(2.89 KB)
📄
InlineSimple.cpp
(4.21 KB)
📄
Inliner.cpp
(44.77 KB)
📄
Internalize.cpp
(9.52 KB)
📄
LoopExtractor.cpp
(7.14 KB)
📄
LowerTypeTests.cpp
(83.98 KB)
📄
MergeFunctions.cpp
(34.12 KB)
📄
OpenMPOpt.cpp
(52.75 KB)
📄
PartialInlining.cpp
(55.67 KB)
📄
PassManagerBuilder.cpp
(49.21 KB)
📄
PruneEH.cpp
(9.54 KB)
📄
SCCP.cpp
(3.22 KB)
📄
SampleProfile.cpp
(77.15 KB)
📄
StripDeadPrototypes.cpp
(2.73 KB)
📄
StripSymbols.cpp
(11.7 KB)
📄
SyntheticCountsPropagation.cpp
(5.64 KB)
📄
ThinLTOBitcodeWriter.cpp
(20.16 KB)
📄
WholeProgramDevirt.cpp
(85.02 KB)
Editing: SyntheticCountsPropagation.cpp
//=- SyntheticCountsPropagation.cpp - Propagate function counts --*- C++ -*-=// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements a transformation that synthesizes entry counts for // functions and attaches !prof metadata to functions with the synthesized // counts. The presence of !prof metadata with counter name set to // 'synthesized_function_entry_count' indicate that the value of the counter is // an estimation of the likely execution count of the function. This transform // is applied only in non PGO mode as functions get 'real' profile-based // function entry counts in the PGO mode. // // The transformation works by first assigning some initial values to the entry // counts of all functions and then doing a top-down traversal of the // callgraph-scc to propagate the counts. For each function the set of callsites // and their relative block frequency is gathered. The relative block frequency // multiplied by the entry count of the caller and added to the callee's entry // count. For non-trivial SCCs, the new counts are computed from the previous // counts and updated in one shot. // //===----------------------------------------------------------------------===// #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/Analysis/SyntheticCountsUtils.h" #include "llvm/IR/Function.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Module.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; using Scaled64 = ScaledNumber<uint64_t>; using ProfileCount = Function::ProfileCount; #define DEBUG_TYPE "synthetic-counts-propagation" /// Initial synthetic count assigned to functions. cl::opt<int> InitialSyntheticCount("initial-synthetic-count", cl::Hidden, cl::init(10), cl::ZeroOrMore, cl::desc("Initial value of synthetic entry count.")); /// Initial synthetic count assigned to inline functions. static cl::opt<int> InlineSyntheticCount( "inline-synthetic-count", cl::Hidden, cl::init(15), cl::ZeroOrMore, cl::desc("Initial synthetic entry count for inline functions.")); /// Initial synthetic count assigned to cold functions. static cl::opt<int> ColdSyntheticCount( "cold-synthetic-count", cl::Hidden, cl::init(5), cl::ZeroOrMore, cl::desc("Initial synthetic entry count for cold functions.")); // Assign initial synthetic entry counts to functions. static void initializeCounts(Module &M, function_ref<void(Function *, uint64_t)> SetCount) { auto MayHaveIndirectCalls = [](Function &F) { for (auto *U : F.users()) { if (!isa<CallInst>(U) && !isa<InvokeInst>(U)) return true; } return false; }; for (Function &F : M) { uint64_t InitialCount = InitialSyntheticCount; if (F.isDeclaration()) continue; if (F.hasFnAttribute(Attribute::AlwaysInline) || F.hasFnAttribute(Attribute::InlineHint)) { // Use a higher value for inline functions to account for the fact that // these are usually beneficial to inline. InitialCount = InlineSyntheticCount; } else if (F.hasLocalLinkage() && !MayHaveIndirectCalls(F)) { // Local functions without inline hints get counts only through // propagation. InitialCount = 0; } else if (F.hasFnAttribute(Attribute::Cold) || F.hasFnAttribute(Attribute::NoInline)) { // Use a lower value for noinline and cold functions. InitialCount = ColdSyntheticCount; } SetCount(&F, InitialCount); } } PreservedAnalyses SyntheticCountsPropagation::run(Module &M, ModuleAnalysisManager &MAM) { FunctionAnalysisManager &FAM = MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); DenseMap<Function *, Scaled64> Counts; // Set initial entry counts. initializeCounts( M, [&](Function *F, uint64_t Count) { Counts[F] = Scaled64(Count, 0); }); // Edge includes information about the source. Hence ignore the first // parameter. auto GetCallSiteProfCount = [&](const CallGraphNode *, const CallGraphNode::CallRecord &Edge) { Optional<Scaled64> Res = None; if (!Edge.first) return Res; CallBase &CB = *cast<CallBase>(*Edge.first); Function *Caller = CB.getCaller(); auto &BFI = FAM.getResult<BlockFrequencyAnalysis>(*Caller); // Now compute the callsite count from relative frequency and // entry count: BasicBlock *CSBB = CB.getParent(); Scaled64 EntryFreq(BFI.getEntryFreq(), 0); Scaled64 BBCount(BFI.getBlockFreq(CSBB).getFrequency(), 0); BBCount /= EntryFreq; BBCount *= Counts[Caller]; return Optional<Scaled64>(BBCount); }; CallGraph CG(M); // Propgate the entry counts on the callgraph. SyntheticCountsUtils<const CallGraph *>::propagate( &CG, GetCallSiteProfCount, [&](const CallGraphNode *N, Scaled64 New) { auto F = N->getFunction(); if (!F || F->isDeclaration()) return; Counts[F] += New; }); // Set the counts as metadata. for (auto Entry : Counts) { Entry.first->setEntryCount(ProfileCount( Entry.second.template toInt<uint64_t>(), Function::PCT_Synthetic)); } return PreservedAnalyses::all(); }
Upload File
Create Folder