34#define DEBUG_TYPE "mirfs-discriminators"
41 cl::desc(
"New FS discriminators encoding (incompatible with the original "
48 "Add MIR Flow Sensitive Discriminators",
68 uint64_t Ret = updateHash(std::to_string(DIL->getLine()));
69 Ret ^= updateHash(BB.
getName());
70 Ret ^= updateHash(DIL->getScope()->getSubprogram()->getLinkageName());
71 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
72 Ret ^= updateHash(std::to_string(DIL->getLine()));
73 Ret ^= updateHash(DIL->getScope()->getSubprogram()->getLinkageName());
80 std::hash<uint64_t> Hasher;
81 return Seed ^ (Hasher(Val) + 0x9e3779b9 + (
Seed << 6) + (
Seed >> 2));
84 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
86 Ret = hashCombine(Ret,
xxh3_64bits(DIL->getSubprogramLinkageName()));
100 bool HasPseudoProbe = MF.getFunction().getParent()->getNamedMetadata(
103 if (!HasPseudoProbe && !MF.getFunction().shouldEmitDebugInfoForProfiling())
107 using LocationDiscriminator =
108 std::tuple<StringRef, unsigned, unsigned, uint64_t>;
109 using BBSet = DenseSet<const MachineBasicBlock *>;
110 using LocationDiscriminatorBBMap = DenseMap<LocationDiscriminator, BBSet>;
111 using LocationDiscriminatorCurrPassMap =
112 DenseMap<LocationDiscriminator, unsigned>;
114 LocationDiscriminatorBBMap LDBM;
115 LocationDiscriminatorCurrPassMap LDCM;
119 unsigned LowBitTemp = LowBit;
120 assert(LowBit > 0 &&
"LowBit in FSDiscriminator cannot be 0");
123 unsigned BitMaskBefore =
getN1Bits(LowBitTemp);
125 unsigned BitMaskNow =
getN1Bits(HighBit);
127 unsigned BitMaskThisPass = BitMaskNow ^ BitMaskBefore;
128 unsigned NumNewD = 0;
131 << MF.getFunction().getName() <<
" Highbit=" << HighBit
134 for (MachineBasicBlock &BB : MF) {
135 for (MachineInstr &
I : BB) {
136 if (HasPseudoProbe) {
140 if (!
I.isPseudoProbe())
145 const DILocation *DIL =
I.getDebugLoc().get();
151 I.isPseudoProbe() ?
I.getOperand(1).getImm() : DIL->getLine();
157 if ((Pass == FSDiscriminatorPass::Pass1) &&
I.isPseudoProbe()) {
161 uint64_t CallStackHashVal = 0;
167 auto &BBMap = LDBM[
LD];
168 auto R = BBMap.insert(&BB);
169 if (BBMap.size() == 1)
172 unsigned DiscriminatorCurrPass;
173 DiscriminatorCurrPass =
R.second ? ++LDCM[
LD] : LDCM[
LD];
174 DiscriminatorCurrPass = DiscriminatorCurrPass << LowBit;
177 DiscriminatorCurrPass &= BitMaskThisPass;
182 << DIL->getFilename() <<
":" << DIL->getLine() <<
":"
183 << DIL->getColumn() <<
":" << Discriminator <<
" "
188 I.setDebugLoc(NewDIL);
190 LLVM_DEBUG(
dbgs() << DIL->getFilename() <<
":" << DIL->getLine() <<
":"
191 << DIL->getColumn() <<
": add FS discriminator, from "
192 << Discriminator <<
" -> " << NewD <<
"\n");
199 LLVM_DEBUG(
dbgs() <<
"Num of FS Discriminators: " << NumNewD <<
"\n");
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
Module.h This file contains the declarations for the Module class.
static uint64_t getCallStackHashV0(const MachineBasicBlock &BB, const MachineInstr &MI, const DILocation *DIL)
static uint64_t getCallStackHash(const DILocation *DIL)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static ManagedStatic< cl::opt< uint64_t >, CreateSeed > Seed
This file provides the utility functions for the sampled PGO loader base implementation.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
FunctionPass class - This class is used to implement most global optimizations.
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Representation of each machine instruction.
StringRef - Represent a constant reference to a string, i.e.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
uint64_t MD5Hash(const FunctionId &Obj)
void createFSDiscriminatorVariable(Module *M)
Create a global variable to flag FSDiscriminators are used.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI char & MIRAddFSDiscriminatorsID
This pass adds flow sensitive discriminators.
LLVM_ABI uint64_t xxh3_64bits(ArrayRef< uint8_t > data)
LLVM_ABI cl::opt< bool > EnableFSDiscriminator
LLVM_ABI FunctionPass * createMIRAddFSDiscriminatorsPass(sampleprof::FSDiscriminatorPass P)
Add Flow Sensitive Discriminators.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
cl::opt< bool > ImprovedFSDiscriminator("improved-fs-discriminator", cl::Hidden, cl::init(false), cl::desc("New FS discriminators encoding (incompatible with the original " "encoding)"))
static unsigned getN1Bits(int N)
constexpr const char * PseudoProbeDescMetadataName