36#define DEBUG_TYPE "mir-fs-discriminators"
42 cl::desc(
"New FS discriminators encoding (incompatible with the original "
47 "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()));
109 assert(LowBit < HighBit &&
"HighBit needs to be greater than Lowbit");
111 bool ProbeLocationChanged =
false;
112 using LocationDiscriminator =
113 std::tuple<StringRef, unsigned, unsigned, uint64_t>;
116 using LocationDiscriminatorCurrPassMap =
119 LocationDiscriminatorBBMap LDBM;
120 LocationDiscriminatorCurrPassMap LDCM;
124 unsigned LowBitTemp = LowBit;
125 assert(LowBit > 0 &&
"LowBit in FSDiscriminator cannot be 0");
128 unsigned BitMaskBefore =
getN1Bits(LowBitTemp);
130 unsigned BitMaskNow =
getN1Bits(HighBit);
132 unsigned BitMaskThisPass = BitMaskNow ^ BitMaskBefore;
133 unsigned NumNewD = 0;
141 if (HasPseudoProbe) {
145 if (!
I.isPseudoProbe())
156 I.isPseudoProbe() ?
I.getOperand(1).getImm() : DIL->getLine();
159 unsigned Discriminator = DIL->getDiscriminator();
162 if ((
Pass == FSDiscriminatorPass::Pass1) &&
I.isPseudoProbe()) {
165 ProbeLocationChanged |= DIL != ResetDIL;
166 I.setDebugLoc(ResetDIL);
172 LocationDiscriminator LD{DIL->getFilename(), LineNo, Discriminator,
174 auto &BBMap = LDBM[LD];
175 auto R = BBMap.insert(&BB);
176 if (BBMap.size() == 1)
179 unsigned DiscriminatorCurrPass;
180 DiscriminatorCurrPass = R.second ? ++LDCM[LD] : LDCM[LD];
181 DiscriminatorCurrPass = DiscriminatorCurrPass << LowBit;
184 DiscriminatorCurrPass &= BitMaskThisPass;
185 unsigned NewD = Discriminator | DiscriminatorCurrPass;
189 << DIL->getFilename() <<
":" << DIL->getLine() <<
":"
190 << DIL->getColumn() <<
":" << Discriminator <<
" "
195 I.setDebugLoc(NewDIL);
197 LLVM_DEBUG(
dbgs() << DIL->getFilename() <<
":" << DIL->getLine() <<
":"
198 << DIL->getColumn() <<
": add FS discriminator, from "
199 << Discriminator <<
" -> " << NewD <<
"\n");
206 LLVM_DEBUG(
dbgs() <<
"Num of FS Discriminators: " << NumNewD <<
"\n");
210 return Changed || ProbeLocationChanged;
213bool MIRAddFSDiscriminatorsLegacy::runOnMachineFunction(
MachineFunction &MF) {
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 bool addFSDiscriminators(MachineFunction &MF, FSDiscriminatorPass Pass)
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.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Represents analyses that only rely on functions' control flow.
const DILocation * cloneWithDiscriminator(unsigned Discriminator) const
Returns a new DILocation with updated Discriminator.
Implements a dense probed hash-table based set.
FunctionPass class - This class is used to implement most global optimizations.
bool shouldEmitDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Module * getParent()
Get the module that this global value is contained inside of...
FSDiscriminatorPass Stage
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MachineFunctionAM)
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
NamedMDNode * getNamedMetadata(StringRef Name) const
Return the first NamedMDNode in the module with the specified name.
Pass interface - Implemented by all 'passes'.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Represent a constant reference to a string, i.e.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Pass manager infrastructure for declaring and invalidating analyses.
initializer< Ty > init(const Ty &Val)
uint64_t MD5Hash(const FunctionId &Obj)
LLVM_ABI void createFSDiscriminatorVariable(Module *M)
Create a global variable to flag FSDiscriminators are used.
This is an optimization pass for GlobalISel generic memory operations.
static unsigned getFSPassBitBegin(sampleprof::FSDiscriminatorPass P)
LLVM_ABI char & MIRAddFSDiscriminatorsID
This pass adds flow sensitive discriminators.
LLVM_ABI cl::opt< bool > ImprovedFSDiscriminator
uint64_t xxh3_64bits(ArrayRef< uint8_t > data)
Inline ArrayRef overloads of the xxhash entry points declared out-of-line in llvm/Support/xxhash....
LLVM_ABI cl::opt< bool > EnableFSDiscriminator
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
static unsigned getFSPassBitEnd(sampleprof::FSDiscriminatorPass P)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI FunctionPass * createMIRAddFSDiscriminatorsLegacyPass(sampleprof::FSDiscriminatorPass P)
Add Flow Sensitive Discriminators.
static unsigned getN1Bits(int N)
constexpr const char * PseudoProbeDescMetadataName