LLVM: lib/CodeGen/MIRFSDiscriminator.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
30
31using namespace llvm;
34
35#define DEBUG_TYPE "mirfs-discriminators"
36
37
38
41 cl::desc("New FS discriminators encoding (incompatible with the original "
42 "encoding)"));
44
46 "Add MIR Flow Sensitive Discriminators",
47 false, false)
48
50
53}
54
55
56
57
62 if (Str.empty())
63 return 0;
65 };
66 uint64_t Ret = updateHash(std::to_string(DIL->getLine()));
67 Ret ^= updateHash(BB.getName());
68 Ret ^= updateHash(DIL->getScope()->getSubprogram()->getLinkageName());
69 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
70 Ret ^= updateHash(std::to_string(DIL->getLine()));
71 Ret ^= updateHash(DIL->getScope()->getSubprogram()->getLinkageName());
72 }
73 return Ret;
74}
75
78 std::hash<uint64_t> Hasher;
79 return Seed ^ (Hasher(Val) + 0x9e3779b9 + (Seed << 6) + (Seed >> 2));
80 };
82 for (DIL = DIL->getInlinedAt(); DIL; DIL = DIL->getInlinedAt()) {
84 Ret = hashCombine(Ret, xxh3_64bits(DIL->getSubprogramLinkageName()));
85 }
86 return Ret;
87}
88
89
90
91
92
93
94bool MIRAddFSDiscriminators::runOnMachineFunction(MachineFunction &MF) {
96 return false;
97
98 bool HasPseudoProbe = MF.getFunction().getParent()->getNamedMetadata(
100
101 if (!HasPseudoProbe && !MF.getFunction().shouldEmitDebugInfoForProfiling())
102 return false;
103
105 using LocationDiscriminator =
106 std::tuple<StringRef, unsigned, unsigned, uint64_t>;
107 using BBSet = DenseSet<const MachineBasicBlock *>;
108 using LocationDiscriminatorBBMap = DenseMap<LocationDiscriminator, BBSet>;
109 using LocationDiscriminatorCurrPassMap =
110 DenseMap<LocationDiscriminator, unsigned>;
111
112 LocationDiscriminatorBBMap LDBM;
113 LocationDiscriminatorCurrPassMap LDCM;
114
115
116
117 unsigned LowBitTemp = LowBit;
118 assert(LowBit > 0 && "LowBit in FSDiscriminator cannot be 0");
120 LowBitTemp -= 1;
121 unsigned BitMaskBefore = getN1Bits(LowBitTemp);
122
123 unsigned BitMaskNow = getN1Bits(HighBit);
124
125 unsigned BitMaskThisPass = BitMaskNow ^ BitMaskBefore;
126 unsigned NumNewD = 0;
127
128 LLVM_DEBUG(dbgs() << "MIRAddFSDiscriminators working on Func: "
129 << MF.getFunction().getName() << " Highbit=" << HighBit
130 << "\n");
131
132 for (MachineBasicBlock &BB : MF) {
133 for (MachineInstr &I : BB) {
134 if (HasPseudoProbe) {
135
136
137
138 if (.isPseudoProbe())
139 continue;
141 continue;
142 }
143 const DILocation *DIL = I.getDebugLoc().get();
144 if (!DIL)
145 continue;
146
147
148 unsigned LineNo =
149 I.isPseudoProbe() ? I.getOperand(1).getImm() : DIL->getLine();
150 if (LineNo == 0)
151 continue;
152 unsigned Discriminator = DIL->getDiscriminator();
153
154
155 if ((Pass == FSDiscriminatorPass::Pass1) && I.isPseudoProbe()) {
158 }
159 uint64_t CallStackHashVal = 0;
162
163 LocationDiscriminator LD{DIL->getFilename(), LineNo, Discriminator,
164 CallStackHashVal};
165 auto &BBMap = LDBM[LD];
166 auto R = BBMap.insert(&BB);
167 if (BBMap.size() == 1)
168 continue;
169
170 unsigned DiscriminatorCurrPass;
171 DiscriminatorCurrPass = R.second ? ++LDCM[LD] : LDCM[LD];
172 DiscriminatorCurrPass = DiscriminatorCurrPass << LowBit;
175 DiscriminatorCurrPass &= BitMaskThisPass;
176 unsigned NewD = Discriminator | DiscriminatorCurrPass;
178 if (!NewDIL) {
179 LLVM_DEBUG(dbgs() << "Could not encode discriminator: "
180 << DIL->getFilename() << ":" << DIL->getLine() << ":"
181 << DIL->getColumn() << ":" << Discriminator << " "
182 << I << "\n");
183 continue;
184 }
185
186 I.setDebugLoc(NewDIL);
187 NumNewD++;
188 LLVM_DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":"
189 << DIL->getColumn() << ": add FS discriminator, from "
190 << Discriminator << " -> " << NewD << "\n");
192 }
193 }
194
197 LLVM_DEBUG(dbgs() << "Num of FS Discriminators: " << NumNewD << "\n");
198 (void) NumNewD;
199 }
200
202}
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)
Definition MIRFSDiscriminator.cpp:58
static uint64_t getCallStackHash(const DILocation *DIL)
Definition MIRFSDiscriminator.cpp:76
#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.
cl::opt< bool > ImprovedFSDiscriminator
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.
static unsigned getN1Bits(int N)
constexpr const char * PseudoProbeDescMetadataName