LLVM 19.0.0git
CGProfile.cpp
Go to the documentation of this file.
1//===-- CGProfile.cpp -----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "llvm/ADT/MapVector.h"
15#include "llvm/IR/Constants.h"
16#include "llvm/IR/MDBuilder.h"
17#include "llvm/IR/PassManager.h"
20#include <optional>
21
22using namespace llvm;
23
24static bool
26 MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
27 if (Counts.empty())
28 return false;
29
30 LLVMContext &Context = M.getContext();
31 MDBuilder MDB(Context);
32 std::vector<Metadata *> Nodes;
33
34 for (auto E : Counts) {
35 Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
36 ValueAsMetadata::get(E.first.second),
37 MDB.createConstant(ConstantInt::get(
38 Type::getInt64Ty(Context), E.second))};
39 Nodes.push_back(MDNode::get(Context, Vals));
40 }
41
42 M.addModuleFlag(Module::Append, "CG Profile",
44 return true;
45}
46
48 bool InLTO) {
50 InstrProfSymtab Symtab;
51 auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
52 Function *CalledF, uint64_t NewCount) {
53 if (NewCount == 0)
54 return;
55 if (!CalledF || !TTI.isLoweredToCall(CalledF) ||
56 CalledF->hasDLLImportStorageClass())
57 return;
58 uint64_t &Count = Counts[std::make_pair(F, CalledF)];
59 Count = SaturatingAdd(Count, NewCount);
60 };
61 // Ignore error here. Indirect calls are ignored if this fails.
62 (void)(bool)Symtab.create(M, InLTO);
63 for (auto &F : M) {
64 // Avoid extra cost of running passes for BFI when the function doesn't have
65 // entry count.
66 if (F.isDeclaration() || !F.getEntryCount())
67 continue;
69 if (BFI.getEntryFreq() == BlockFrequency(0))
70 continue;
72 for (auto &BB : F) {
73 std::optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
74 if (!BBCount)
75 continue;
76 for (auto &I : BB) {
77 CallBase *CB = dyn_cast<CallBase>(&I);
78 if (!CB)
79 continue;
80 if (CB->isIndirectCall()) {
81 InstrProfValueData ValueData[8];
82 uint32_t ActualNumValueData;
83 uint64_t TotalC;
84 if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
85 ValueData, ActualNumValueData, TotalC))
86 continue;
87 for (const auto &VD :
88 ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
89 UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
90 }
91 continue;
92 }
93 UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
94 }
95 }
96 }
97
98 return addModuleFlags(M, Counts);
99}
100
104 runCGProfilePass(M, FAM, InLTO);
105
106 return PreservedAnalyses::all();
107}
static bool addModuleFlags(Module &M, MapVector< std::pair< Function *, Function * >, uint64_t > &Counts)
Definition: CGProfile.cpp:25
static bool runCGProfilePass(Module &M, FunctionAnalysisManager &FAM, bool InLTO)
Definition: CGProfile.cpp:47
This file provides the interface for LLVM's Call Graph Profile pass.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
LLVMContext & Context
FunctionAnalysisManager FAM
ModuleAnalysisManager MAM
This header defines various interfaces for pass management in LLVM.
This pass exposes codegen information to IR-level passes.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:500
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Analysis pass which computes BlockFrequencyInfo.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: CGProfile.cpp:101
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1461
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1709
bool isIndirectCall() const
Return true if the callsite is an indirect call.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:658
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
Definition: InstrProf.h:429
Function * getFunction(uint64_t FuncMD5Hash)
Return function from the name's md5 hash. Return nullptr if not found.
Definition: InstrProf.h:598
Error create(object::SectionRef &Section)
Create InstrProfSymtab from an object file section which contains function PGO names.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
Definition: MDBuilder.cpp:24
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition: Metadata.h:1509
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
@ Append
Appends the two values, which are required to be metadata nodes.
Definition: Module.h:139
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
static IntegerType * getInt64Ty(LLVMContext &C)
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:495
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueKind ValueKind, uint32_t MaxNumValueData, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC, bool GetNoICPValue=false)
Extract the value profile data from Inst which is annotated with value profile meta data.
Definition: InstrProf.cpp:1219
TargetTransformInfo TTI
std::enable_if_t< std::is_unsigned_v< T >, T > SaturatingAdd(T X, T Y, bool *ResultOverflowed=nullptr)
Add two unsigned integers, X and Y, of type T.
Definition: MathExtras.h:478