Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 1 | //===- AddDiscriminators.cpp - Insert DWARF path discriminators -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file adds DWARF discriminators to the IR. Path discriminators are |
| 11 | // used to decide what CFG path was taken inside sub-graphs whose instructions |
| 12 | // share the same line and column number information. |
| 13 | // |
| 14 | // The main user of this is the sample profiler. Instruction samples are |
| 15 | // mapped to line number information. Since a single line may be spread |
| 16 | // out over several basic blocks, discriminators add more precise location |
| 17 | // for the samples. |
| 18 | // |
| 19 | // For example, |
| 20 | // |
| 21 | // 1 #define ASSERT(P) |
| 22 | // 2 if (!(P)) |
| 23 | // 3 abort() |
| 24 | // ... |
| 25 | // 100 while (true) { |
| 26 | // 101 ASSERT (sum < 0); |
| 27 | // 102 ... |
| 28 | // 130 } |
| 29 | // |
| 30 | // when converted to IR, this snippet looks something like: |
| 31 | // |
| 32 | // while.body: ; preds = %entry, %if.end |
| 33 | // %0 = load i32* %sum, align 4, !dbg !15 |
| 34 | // %cmp = icmp slt i32 %0, 0, !dbg !15 |
| 35 | // br i1 %cmp, label %if.end, label %if.then, !dbg !15 |
| 36 | // |
| 37 | // if.then: ; preds = %while.body |
| 38 | // call void @abort(), !dbg !15 |
| 39 | // br label %if.end, !dbg !15 |
| 40 | // |
| 41 | // Notice that all the instructions in blocks 'while.body' and 'if.then' |
| 42 | // have exactly the same debug information. When this program is sampled |
| 43 | // at runtime, the profiler will assume that all these instructions are |
| 44 | // equally frequent. This, in turn, will consider the edge while.body->if.then |
| 45 | // to be frequently taken (which is incorrect). |
| 46 | // |
| 47 | // By adding a discriminator value to the instructions in block 'if.then', |
| 48 | // we can distinguish instructions at line 101 with discriminator 0 from |
| 49 | // the instructions at line 101 with discriminator 1. |
| 50 | // |
| 51 | // For more details about DWARF discriminators, please visit |
| 52 | // http://wiki.dwarfstd.org/index.php?title=Path_Discriminators |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 53 | // |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 56 | #include "llvm/Transforms/Utils/AddDiscriminators.h" |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/DenseMap.h" |
Dehao Chen | 46f8fbb | 2016-04-14 18:37:18 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/DenseSet.h" |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringRef.h" |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 60 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 61 | #include "llvm/IR/DebugInfoMetadata.h" |
| 62 | #include "llvm/IR/Function.h" |
| 63 | #include "llvm/IR/Instruction.h" |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 64 | #include "llvm/IR/Instructions.h" |
Pavel Labath | 978060c | 2015-11-16 10:40:38 +0000 | [diff] [blame] | 65 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 66 | #include "llvm/IR/PassManager.h" |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 67 | #include "llvm/Pass.h" |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Casting.h" |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 69 | #include "llvm/Support/CommandLine.h" |
| 70 | #include "llvm/Support/Debug.h" |
| 71 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 72 | #include "llvm/Transforms/Utils.h" |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 73 | #include <utility> |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 74 | |
| 75 | using namespace llvm; |
| 76 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 77 | #define DEBUG_TYPE "add-discriminators" |
| 78 | |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 79 | // Command line option to disable discriminator generation even in the |
| 80 | // presence of debug information. This is only needed when debugging |
| 81 | // debug info generation issues. |
| 82 | static cl::opt<bool> NoDiscriminators( |
| 83 | "no-discriminators", cl::init(false), |
| 84 | cl::desc("Disable generation of discriminator information.")); |
| 85 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 86 | namespace { |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 87 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 88 | // The legacy pass of AddDiscriminators. |
| 89 | struct AddDiscriminatorsLegacyPass : public FunctionPass { |
Dehao Chen | 7ddf786 | 2015-10-29 21:25:33 +0000 | [diff] [blame] | 90 | static char ID; // Pass identification, replacement for typeid |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 91 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 92 | AddDiscriminatorsLegacyPass() : FunctionPass(ID) { |
| 93 | initializeAddDiscriminatorsLegacyPassPass(*PassRegistry::getPassRegistry()); |
Dehao Chen | 7ddf786 | 2015-10-29 21:25:33 +0000 | [diff] [blame] | 94 | } |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 95 | |
Dehao Chen | 7ddf786 | 2015-10-29 21:25:33 +0000 | [diff] [blame] | 96 | bool runOnFunction(Function &F) override; |
| 97 | }; |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 98 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 99 | } // end anonymous namespace |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 100 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 101 | char AddDiscriminatorsLegacyPass::ID = 0; |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 102 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 103 | INITIALIZE_PASS_BEGIN(AddDiscriminatorsLegacyPass, "add-discriminators", |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 104 | "Add DWARF path discriminators", false, false) |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 105 | INITIALIZE_PASS_END(AddDiscriminatorsLegacyPass, "add-discriminators", |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 106 | "Add DWARF path discriminators", false, false) |
| 107 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 108 | // Create the legacy AddDiscriminatorsPass. |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 109 | FunctionPass *llvm::createAddDiscriminatorsPass() { |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 110 | return new AddDiscriminatorsLegacyPass(); |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Andrea Di Biagio | 8e26936 | 2017-04-11 19:07:30 +0000 | [diff] [blame] | 113 | static bool shouldHaveDiscriminator(const Instruction *I) { |
| 114 | return !isa<IntrinsicInst>(I) || isa<MemIntrinsic>(I); |
| 115 | } |
| 116 | |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 117 | /// \brief Assign DWARF discriminators. |
| 118 | /// |
| 119 | /// To assign discriminators, we examine the boundaries of every |
| 120 | /// basic block and its successors. Suppose there is a basic block B1 |
| 121 | /// with successor B2. The last instruction I1 in B1 and the first |
| 122 | /// instruction I2 in B2 are located at the same file and line number. |
| 123 | /// This situation is illustrated in the following code snippet: |
| 124 | /// |
| 125 | /// if (i < 10) x = i; |
| 126 | /// |
| 127 | /// entry: |
| 128 | /// br i1 %cmp, label %if.then, label %if.end, !dbg !10 |
| 129 | /// if.then: |
| 130 | /// %1 = load i32* %i.addr, align 4, !dbg !10 |
| 131 | /// store i32 %1, i32* %x, align 4, !dbg !10 |
| 132 | /// br label %if.end, !dbg !10 |
| 133 | /// if.end: |
| 134 | /// ret void, !dbg !12 |
| 135 | /// |
| 136 | /// Notice how the branch instruction in block 'entry' and all the |
| 137 | /// instructions in block 'if.then' have the exact same debug location |
| 138 | /// information (!dbg !10). |
| 139 | /// |
| 140 | /// To distinguish instructions in block 'entry' from instructions in |
| 141 | /// block 'if.then', we generate a new lexical block for all the |
| 142 | /// instruction in block 'if.then' that share the same file and line |
| 143 | /// location with the last instruction of block 'entry'. |
| 144 | /// |
| 145 | /// This new lexical block will have the same location information as |
| 146 | /// the previous one, but with a new DWARF discriminator value. |
| 147 | /// |
| 148 | /// One of the main uses of this discriminator value is in runtime |
| 149 | /// sample profilers. It allows the profiler to distinguish instructions |
| 150 | /// at location !dbg !10 that execute on different basic blocks. This is |
| 151 | /// important because while the predicate 'if (x < 10)' may have been |
| 152 | /// executed millions of times, the assignment 'x = i' may have only |
| 153 | /// executed a handful of times (meaning that the entry->if.then edge is |
| 154 | /// seldom taken). |
| 155 | /// |
| 156 | /// If we did not have discriminator information, the profiler would |
| 157 | /// assign the same weight to both blocks 'entry' and 'if.then', which |
| 158 | /// in turn will make it conclude that the entry->if.then edge is very |
| 159 | /// hot. |
| 160 | /// |
| 161 | /// To decide where to create new discriminator values, this function |
| 162 | /// traverses the CFG and examines instruction at basic block boundaries. |
| 163 | /// If the last instruction I1 of a block B1 is at the same file and line |
| 164 | /// location as instruction I2 of successor B2, then it creates a new |
| 165 | /// lexical block for I2 and all the instruction in B2 that share the same |
| 166 | /// file and line location as I2. This new lexical block will have a |
| 167 | /// different discriminator number than I1. |
Xinliang David Li | 1e16d61 | 2016-06-15 22:20:56 +0000 | [diff] [blame] | 168 | static bool addDiscriminators(Function &F) { |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 169 | // If the function has debug information, but the user has disabled |
| 170 | // discriminators, do nothing. |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 171 | // Simlarly, if the function has no debug info, do nothing. |
Dehao Chen | 6e0c844 | 2016-10-07 15:21:31 +0000 | [diff] [blame] | 172 | if (NoDiscriminators || !F.getSubprogram()) |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 173 | return false; |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 174 | |
| 175 | bool Changed = false; |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 176 | |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 177 | using Location = std::pair<StringRef, unsigned>; |
| 178 | using BBSet = DenseSet<const BasicBlock *>; |
| 179 | using LocationBBMap = DenseMap<Location, BBSet>; |
| 180 | using LocationDiscriminatorMap = DenseMap<Location, unsigned>; |
| 181 | using LocationSet = DenseSet<Location>; |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 182 | |
| 183 | LocationBBMap LBM; |
Dehao Chen | 939993f | 2016-02-29 18:59:48 +0000 | [diff] [blame] | 184 | LocationDiscriminatorMap LDM; |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 185 | |
| 186 | // Traverse all instructions in the function. If the source line location |
| 187 | // of the instruction appears in other basic block, assign a new |
| 188 | // discriminator for this instruction. |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 189 | for (BasicBlock &B : F) { |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 190 | for (auto &I : B.getInstList()) { |
Andrea Di Biagio | 8e26936 | 2017-04-11 19:07:30 +0000 | [diff] [blame] | 191 | // Not all intrinsic calls should have a discriminator. |
| 192 | // We want to avoid a non-deterministic assignment of discriminators at |
| 193 | // different debug levels. We still allow discriminators on memory |
| 194 | // intrinsic calls because those can be early expanded by SROA into |
| 195 | // pairs of loads and stores, and the expanded load/store instructions |
| 196 | // should have a valid discriminator. |
| 197 | if (!shouldHaveDiscriminator(&I)) |
Duncan P. N. Exon Smith | ec819c0 | 2015-03-30 19:49:49 +0000 | [diff] [blame] | 198 | continue; |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 199 | const DILocation *DIL = I.getDebugLoc(); |
| 200 | if (!DIL) |
| 201 | continue; |
| 202 | Location L = std::make_pair(DIL->getFilename(), DIL->getLine()); |
| 203 | auto &BBMap = LBM[L]; |
Dehao Chen | e713000 | 2016-10-26 15:48:45 +0000 | [diff] [blame] | 204 | auto R = BBMap.insert(&B); |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 205 | if (BBMap.size() == 1) |
| 206 | continue; |
Adrian Prantl | 28d2d28 | 2016-10-24 18:23:51 +0000 | [diff] [blame] | 207 | // If we could insert more than one block with the same line+file, a |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 208 | // discriminator is needed to distinguish both instructions. |
Dehao Chen | 2ca9be3 | 2016-11-08 16:32:32 +0000 | [diff] [blame] | 209 | // Only the lowest 7 bits are used to represent a discriminator to fit |
| 210 | // it in 1 byte ULEB128 representation. |
Dehao Chen | fb02f71 | 2017-02-10 21:09:07 +0000 | [diff] [blame] | 211 | unsigned Discriminator = R.second ? ++LDM[L] : LDM[L]; |
| 212 | I.setDebugLoc(DIL->setBaseDiscriminator(Discriminator)); |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 213 | DEBUG(dbgs() << DIL->getFilename() << ":" << DIL->getLine() << ":" |
Dehao Chen | 2ca9be3 | 2016-11-08 16:32:32 +0000 | [diff] [blame] | 214 | << DIL->getColumn() << ":" << Discriminator << " " << I |
| 215 | << "\n"); |
Dehao Chen | 23e2278 | 2015-11-19 19:53:05 +0000 | [diff] [blame] | 216 | Changed = true; |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
Dehao Chen | 3656e30 | 2015-11-09 17:30:38 +0000 | [diff] [blame] | 219 | |
| 220 | // Traverse all instructions and assign new discriminators to call |
| 221 | // instructions with the same lineno that are in the same basic block. |
| 222 | // Sample base profile needs to distinguish different function calls within |
| 223 | // a same source line for correct profile annotation. |
| 224 | for (BasicBlock &B : F) { |
Dehao Chen | 46f8fbb | 2016-04-14 18:37:18 +0000 | [diff] [blame] | 225 | LocationSet CallLocations; |
Dehao Chen | 3656e30 | 2015-11-09 17:30:38 +0000 | [diff] [blame] | 226 | for (auto &I : B.getInstList()) { |
| 227 | CallInst *Current = dyn_cast<CallInst>(&I); |
Andrea Di Biagio | 8e26936 | 2017-04-11 19:07:30 +0000 | [diff] [blame] | 228 | // We bypass intrinsic calls for the following two reasons: |
| 229 | // 1) We want to avoid a non-deterministic assigment of |
| 230 | // discriminators. |
| 231 | // 2) We want to minimize the number of base discriminators used. |
Dehao Chen | 17c6afc | 2016-08-05 17:56:49 +0000 | [diff] [blame] | 232 | if (!Current || isa<IntrinsicInst>(&I)) |
Pavel Labath | 978060c | 2015-11-16 10:40:38 +0000 | [diff] [blame] | 233 | continue; |
| 234 | |
| 235 | DILocation *CurrentDIL = Current->getDebugLoc(); |
Dehao Chen | 34cc676 | 2016-04-14 19:46:38 +0000 | [diff] [blame] | 236 | if (!CurrentDIL) |
| 237 | continue; |
Dehao Chen | 46f8fbb | 2016-04-14 18:37:18 +0000 | [diff] [blame] | 238 | Location L = |
| 239 | std::make_pair(CurrentDIL->getFilename(), CurrentDIL->getLine()); |
| 240 | if (!CallLocations.insert(L).second) { |
Dehao Chen | fb02f71 | 2017-02-10 21:09:07 +0000 | [diff] [blame] | 241 | unsigned Discriminator = ++LDM[L]; |
| 242 | Current->setDebugLoc(CurrentDIL->setBaseDiscriminator(Discriminator)); |
Dehao Chen | 46f8fbb | 2016-04-14 18:37:18 +0000 | [diff] [blame] | 243 | Changed = true; |
Dehao Chen | 3656e30 | 2015-11-09 17:30:38 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
Diego Novillo | f5041ce | 2014-03-03 20:06:11 +0000 | [diff] [blame] | 247 | return Changed; |
| 248 | } |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 249 | |
| 250 | bool AddDiscriminatorsLegacyPass::runOnFunction(Function &F) { |
| 251 | return addDiscriminators(F); |
| 252 | } |
Eugene Zelenko | 6cadde7 | 2017-10-17 21:27:42 +0000 | [diff] [blame] | 253 | |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 254 | PreservedAnalyses AddDiscriminatorsPass::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 255 | FunctionAnalysisManager &AM) { |
Xinliang David Li | 1e16d61 | 2016-06-15 22:20:56 +0000 | [diff] [blame] | 256 | if (!addDiscriminators(F)) |
| 257 | return PreservedAnalyses::all(); |
| 258 | |
| 259 | // FIXME: should be all() |
| 260 | return PreservedAnalyses::none(); |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 261 | } |