File: | include/llvm/Support/Error.h |
Warning: | line 200, column 5 Potential leak of memory pointed to by 'Payload._M_t._M_head_impl' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- InstrProfiling.cpp - Frontend instrumentation based profiling -----===// | |||
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 | // | |||
9 | // This pass lowers instrprof_* intrinsics emitted by a frontend for profiling. | |||
10 | // It also builds the data structures and initialization code needed for | |||
11 | // updating execution counts and emitting the profile at runtime. | |||
12 | // | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #include "llvm/Transforms/Instrumentation/InstrProfiling.h" | |||
16 | #include "llvm/ADT/ArrayRef.h" | |||
17 | #include "llvm/ADT/SmallVector.h" | |||
18 | #include "llvm/ADT/StringRef.h" | |||
19 | #include "llvm/ADT/Triple.h" | |||
20 | #include "llvm/ADT/Twine.h" | |||
21 | #include "llvm/Analysis/BlockFrequencyInfo.h" | |||
22 | #include "llvm/Analysis/BranchProbabilityInfo.h" | |||
23 | #include "llvm/Analysis/LoopInfo.h" | |||
24 | #include "llvm/Analysis/TargetLibraryInfo.h" | |||
25 | #include "llvm/IR/Attributes.h" | |||
26 | #include "llvm/IR/BasicBlock.h" | |||
27 | #include "llvm/IR/Constant.h" | |||
28 | #include "llvm/IR/Constants.h" | |||
29 | #include "llvm/IR/DerivedTypes.h" | |||
30 | #include "llvm/IR/Dominators.h" | |||
31 | #include "llvm/IR/Function.h" | |||
32 | #include "llvm/IR/GlobalValue.h" | |||
33 | #include "llvm/IR/GlobalVariable.h" | |||
34 | #include "llvm/IR/IRBuilder.h" | |||
35 | #include "llvm/IR/Instruction.h" | |||
36 | #include "llvm/IR/Instructions.h" | |||
37 | #include "llvm/IR/IntrinsicInst.h" | |||
38 | #include "llvm/IR/Module.h" | |||
39 | #include "llvm/IR/Type.h" | |||
40 | #include "llvm/Pass.h" | |||
41 | #include "llvm/ProfileData/InstrProf.h" | |||
42 | #include "llvm/Support/Casting.h" | |||
43 | #include "llvm/Support/CommandLine.h" | |||
44 | #include "llvm/Support/Error.h" | |||
45 | #include "llvm/Support/ErrorHandling.h" | |||
46 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | |||
47 | #include "llvm/Transforms/Utils/ModuleUtils.h" | |||
48 | #include "llvm/Transforms/Utils/SSAUpdater.h" | |||
49 | #include <algorithm> | |||
50 | #include <cassert> | |||
51 | #include <cstddef> | |||
52 | #include <cstdint> | |||
53 | #include <string> | |||
54 | ||||
55 | using namespace llvm; | |||
56 | ||||
57 | #define DEBUG_TYPE"instrprof" "instrprof" | |||
58 | ||||
59 | // The start and end values of precise value profile range for memory | |||
60 | // intrinsic sizes | |||
61 | cl::opt<std::string> MemOPSizeRange( | |||
62 | "memop-size-range", | |||
63 | cl::desc("Set the range of size in memory intrinsic calls to be profiled " | |||
64 | "precisely, in a format of <start_val>:<end_val>"), | |||
65 | cl::init("")); | |||
66 | ||||
67 | // The value that considered to be large value in memory intrinsic. | |||
68 | cl::opt<unsigned> MemOPSizeLarge( | |||
69 | "memop-size-large", | |||
70 | cl::desc("Set large value thresthold in memory intrinsic size profiling. " | |||
71 | "Value of 0 disables the large value profiling."), | |||
72 | cl::init(8192)); | |||
73 | ||||
74 | namespace { | |||
75 | ||||
76 | cl::opt<bool> DoNameCompression("enable-name-compression", | |||
77 | cl::desc("Enable name string compression"), | |||
78 | cl::init(true)); | |||
79 | ||||
80 | cl::opt<bool> DoHashBasedCounterSplit( | |||
81 | "hash-based-counter-split", | |||
82 | cl::desc("Rename counter variable of a comdat function based on cfg hash"), | |||
83 | cl::init(true)); | |||
84 | ||||
85 | cl::opt<bool> ValueProfileStaticAlloc( | |||
86 | "vp-static-alloc", | |||
87 | cl::desc("Do static counter allocation for value profiler"), | |||
88 | cl::init(true)); | |||
89 | ||||
90 | cl::opt<double> NumCountersPerValueSite( | |||
91 | "vp-counters-per-site", | |||
92 | cl::desc("The average number of profile counters allocated " | |||
93 | "per value profiling site."), | |||
94 | // This is set to a very small value because in real programs, only | |||
95 | // a very small percentage of value sites have non-zero targets, e.g, 1/30. | |||
96 | // For those sites with non-zero profile, the average number of targets | |||
97 | // is usually smaller than 2. | |||
98 | cl::init(1.0)); | |||
99 | ||||
100 | cl::opt<bool> AtomicCounterUpdateAll( | |||
101 | "instrprof-atomic-counter-update-all", cl::ZeroOrMore, | |||
102 | cl::desc("Make all profile counter updates atomic (for testing only)"), | |||
103 | cl::init(false)); | |||
104 | ||||
105 | cl::opt<bool> AtomicCounterUpdatePromoted( | |||
106 | "atomic-counter-update-promoted", cl::ZeroOrMore, | |||
107 | cl::desc("Do counter update using atomic fetch add " | |||
108 | " for promoted counters only"), | |||
109 | cl::init(false)); | |||
110 | ||||
111 | // If the option is not specified, the default behavior about whether | |||
112 | // counter promotion is done depends on how instrumentaiton lowering | |||
113 | // pipeline is setup, i.e., the default value of true of this option | |||
114 | // does not mean the promotion will be done by default. Explicitly | |||
115 | // setting this option can override the default behavior. | |||
116 | cl::opt<bool> DoCounterPromotion("do-counter-promotion", cl::ZeroOrMore, | |||
117 | cl::desc("Do counter register promotion"), | |||
118 | cl::init(false)); | |||
119 | cl::opt<unsigned> MaxNumOfPromotionsPerLoop( | |||
120 | cl::ZeroOrMore, "max-counter-promotions-per-loop", cl::init(20), | |||
121 | cl::desc("Max number counter promotions per loop to avoid" | |||
122 | " increasing register pressure too much")); | |||
123 | ||||
124 | // A debug option | |||
125 | cl::opt<int> | |||
126 | MaxNumOfPromotions(cl::ZeroOrMore, "max-counter-promotions", cl::init(-1), | |||
127 | cl::desc("Max number of allowed counter promotions")); | |||
128 | ||||
129 | cl::opt<unsigned> SpeculativeCounterPromotionMaxExiting( | |||
130 | cl::ZeroOrMore, "speculative-counter-promotion-max-exiting", cl::init(3), | |||
131 | cl::desc("The max number of exiting blocks of a loop to allow " | |||
132 | " speculative counter promotion")); | |||
133 | ||||
134 | cl::opt<bool> SpeculativeCounterPromotionToLoop( | |||
135 | cl::ZeroOrMore, "speculative-counter-promotion-to-loop", cl::init(false), | |||
136 | cl::desc("When the option is false, if the target block is in a loop, " | |||
137 | "the promotion will be disallowed unless the promoted counter " | |||
138 | " update can be further/iteratively promoted into an acyclic " | |||
139 | " region.")); | |||
140 | ||||
141 | cl::opt<bool> IterativeCounterPromotion( | |||
142 | cl::ZeroOrMore, "iterative-counter-promotion", cl::init(true), | |||
143 | cl::desc("Allow counter promotion across the whole loop nest.")); | |||
144 | ||||
145 | class InstrProfilingLegacyPass : public ModulePass { | |||
146 | InstrProfiling InstrProf; | |||
147 | ||||
148 | public: | |||
149 | static char ID; | |||
150 | ||||
151 | InstrProfilingLegacyPass() : ModulePass(ID) {} | |||
152 | InstrProfilingLegacyPass(const InstrProfOptions &Options, bool IsCS = false) | |||
153 | : ModulePass(ID), InstrProf(Options, IsCS) {} | |||
154 | ||||
155 | StringRef getPassName() const override { | |||
156 | return "Frontend instrumentation-based coverage lowering"; | |||
157 | } | |||
158 | ||||
159 | bool runOnModule(Module &M) override { | |||
160 | return InstrProf.run(M, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI()); | |||
161 | } | |||
162 | ||||
163 | void getAnalysisUsage(AnalysisUsage &AU) const override { | |||
164 | AU.setPreservesCFG(); | |||
165 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | |||
166 | } | |||
167 | }; | |||
168 | ||||
169 | /// | |||
170 | /// A helper class to promote one counter RMW operation in the loop | |||
171 | /// into register update. | |||
172 | /// | |||
173 | /// RWM update for the counter will be sinked out of the loop after | |||
174 | /// the transformation. | |||
175 | /// | |||
176 | class PGOCounterPromoterHelper : public LoadAndStorePromoter { | |||
177 | public: | |||
178 | PGOCounterPromoterHelper( | |||
179 | Instruction *L, Instruction *S, SSAUpdater &SSA, Value *Init, | |||
180 | BasicBlock *PH, ArrayRef<BasicBlock *> ExitBlocks, | |||
181 | ArrayRef<Instruction *> InsertPts, | |||
182 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands, | |||
183 | LoopInfo &LI) | |||
184 | : LoadAndStorePromoter({L, S}, SSA), Store(S), ExitBlocks(ExitBlocks), | |||
185 | InsertPts(InsertPts), LoopToCandidates(LoopToCands), LI(LI) { | |||
186 | assert(isa<LoadInst>(L))((isa<LoadInst>(L)) ? static_cast<void> (0) : __assert_fail ("isa<LoadInst>(L)", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Transforms/Instrumentation/InstrProfiling.cpp" , 186, __PRETTY_FUNCTION__)); | |||
187 | assert(isa<StoreInst>(S))((isa<StoreInst>(S)) ? static_cast<void> (0) : __assert_fail ("isa<StoreInst>(S)", "/build/llvm-toolchain-snapshot-9~svn362543/lib/Transforms/Instrumentation/InstrProfiling.cpp" , 187, __PRETTY_FUNCTION__)); | |||
188 | SSA.AddAvailableValue(PH, Init); | |||
189 | } | |||
190 | ||||
191 | void doExtraRewritesBeforeFinalDeletion() override { | |||
192 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { | |||
193 | BasicBlock *ExitBlock = ExitBlocks[i]; | |||
194 | Instruction *InsertPos = InsertPts[i]; | |||
195 | // Get LiveIn value into the ExitBlock. If there are multiple | |||
196 | // predecessors, the value is defined by a PHI node in this | |||
197 | // block. | |||
198 | Value *LiveInValue = SSA.GetValueInMiddleOfBlock(ExitBlock); | |||
199 | Value *Addr = cast<StoreInst>(Store)->getPointerOperand(); | |||
200 | Type *Ty = LiveInValue->getType(); | |||
201 | IRBuilder<> Builder(InsertPos); | |||
202 | if (AtomicCounterUpdatePromoted) | |||
203 | // automic update currently can only be promoted across the current | |||
204 | // loop, not the whole loop nest. | |||
205 | Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, LiveInValue, | |||
206 | AtomicOrdering::SequentiallyConsistent); | |||
207 | else { | |||
208 | LoadInst *OldVal = Builder.CreateLoad(Ty, Addr, "pgocount.promoted"); | |||
209 | auto *NewVal = Builder.CreateAdd(OldVal, LiveInValue); | |||
210 | auto *NewStore = Builder.CreateStore(NewVal, Addr); | |||
211 | ||||
212 | // Now update the parent loop's candidate list: | |||
213 | if (IterativeCounterPromotion) { | |||
214 | auto *TargetLoop = LI.getLoopFor(ExitBlock); | |||
215 | if (TargetLoop) | |||
216 | LoopToCandidates[TargetLoop].emplace_back(OldVal, NewStore); | |||
217 | } | |||
218 | } | |||
219 | } | |||
220 | } | |||
221 | ||||
222 | private: | |||
223 | Instruction *Store; | |||
224 | ArrayRef<BasicBlock *> ExitBlocks; | |||
225 | ArrayRef<Instruction *> InsertPts; | |||
226 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates; | |||
227 | LoopInfo &LI; | |||
228 | }; | |||
229 | ||||
230 | /// A helper class to do register promotion for all profile counter | |||
231 | /// updates in a loop. | |||
232 | /// | |||
233 | class PGOCounterPromoter { | |||
234 | public: | |||
235 | PGOCounterPromoter( | |||
236 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCands, | |||
237 | Loop &CurLoop, LoopInfo &LI, BlockFrequencyInfo *BFI) | |||
238 | : LoopToCandidates(LoopToCands), ExitBlocks(), InsertPts(), L(CurLoop), | |||
239 | LI(LI), BFI(BFI) { | |||
240 | ||||
241 | SmallVector<BasicBlock *, 8> LoopExitBlocks; | |||
242 | SmallPtrSet<BasicBlock *, 8> BlockSet; | |||
243 | L.getExitBlocks(LoopExitBlocks); | |||
244 | ||||
245 | for (BasicBlock *ExitBlock : LoopExitBlocks) { | |||
246 | if (BlockSet.insert(ExitBlock).second) { | |||
247 | ExitBlocks.push_back(ExitBlock); | |||
248 | InsertPts.push_back(&*ExitBlock->getFirstInsertionPt()); | |||
249 | } | |||
250 | } | |||
251 | } | |||
252 | ||||
253 | bool run(int64_t *NumPromoted) { | |||
254 | // Skip 'infinite' loops: | |||
255 | if (ExitBlocks.size() == 0) | |||
256 | return false; | |||
257 | unsigned MaxProm = getMaxNumOfPromotionsInLoop(&L); | |||
258 | if (MaxProm == 0) | |||
259 | return false; | |||
260 | ||||
261 | unsigned Promoted = 0; | |||
262 | for (auto &Cand : LoopToCandidates[&L]) { | |||
263 | ||||
264 | SmallVector<PHINode *, 4> NewPHIs; | |||
265 | SSAUpdater SSA(&NewPHIs); | |||
266 | Value *InitVal = ConstantInt::get(Cand.first->getType(), 0); | |||
267 | ||||
268 | // If BFI is set, we will use it to guide the promotions. | |||
269 | if (BFI) { | |||
270 | auto *BB = Cand.first->getParent(); | |||
271 | auto InstrCount = BFI->getBlockProfileCount(BB); | |||
272 | if (!InstrCount) | |||
273 | continue; | |||
274 | auto PreheaderCount = BFI->getBlockProfileCount(L.getLoopPreheader()); | |||
275 | // If the average loop trip count is not greater than 1.5, we skip | |||
276 | // promotion. | |||
277 | if (PreheaderCount && | |||
278 | (PreheaderCount.getValue() * 3) >= (InstrCount.getValue() * 2)) | |||
279 | continue; | |||
280 | } | |||
281 | ||||
282 | PGOCounterPromoterHelper Promoter(Cand.first, Cand.second, SSA, InitVal, | |||
283 | L.getLoopPreheader(), ExitBlocks, | |||
284 | InsertPts, LoopToCandidates, LI); | |||
285 | Promoter.run(SmallVector<Instruction *, 2>({Cand.first, Cand.second})); | |||
286 | Promoted++; | |||
287 | if (Promoted >= MaxProm) | |||
288 | break; | |||
289 | ||||
290 | (*NumPromoted)++; | |||
291 | if (MaxNumOfPromotions != -1 && *NumPromoted >= MaxNumOfPromotions) | |||
292 | break; | |||
293 | } | |||
294 | ||||
295 | LLVM_DEBUG(dbgs() << Promoted << " counters promoted for loop (depth="do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instrprof")) { dbgs() << Promoted << " counters promoted for loop (depth=" << L.getLoopDepth() << ")\n"; } } while (false) | |||
296 | << L.getLoopDepth() << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instrprof")) { dbgs() << Promoted << " counters promoted for loop (depth=" << L.getLoopDepth() << ")\n"; } } while (false); | |||
297 | return Promoted != 0; | |||
298 | } | |||
299 | ||||
300 | private: | |||
301 | bool allowSpeculativeCounterPromotion(Loop *LP) { | |||
302 | SmallVector<BasicBlock *, 8> ExitingBlocks; | |||
303 | L.getExitingBlocks(ExitingBlocks); | |||
304 | // Not considierered speculative. | |||
305 | if (ExitingBlocks.size() == 1) | |||
306 | return true; | |||
307 | if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting) | |||
308 | return false; | |||
309 | return true; | |||
310 | } | |||
311 | ||||
312 | // Returns the max number of Counter Promotions for LP. | |||
313 | unsigned getMaxNumOfPromotionsInLoop(Loop *LP) { | |||
314 | // We can't insert into a catchswitch. | |||
315 | SmallVector<BasicBlock *, 8> LoopExitBlocks; | |||
316 | LP->getExitBlocks(LoopExitBlocks); | |||
317 | if (llvm::any_of(LoopExitBlocks, [](BasicBlock *Exit) { | |||
318 | return isa<CatchSwitchInst>(Exit->getTerminator()); | |||
319 | })) | |||
320 | return 0; | |||
321 | ||||
322 | if (!LP->hasDedicatedExits()) | |||
323 | return 0; | |||
324 | ||||
325 | BasicBlock *PH = LP->getLoopPreheader(); | |||
326 | if (!PH) | |||
327 | return 0; | |||
328 | ||||
329 | SmallVector<BasicBlock *, 8> ExitingBlocks; | |||
330 | LP->getExitingBlocks(ExitingBlocks); | |||
331 | ||||
332 | // If BFI is set, we do more aggressive promotions based on BFI. | |||
333 | if (BFI) | |||
334 | return (unsigned)-1; | |||
335 | ||||
336 | // Not considierered speculative. | |||
337 | if (ExitingBlocks.size() == 1) | |||
338 | return MaxNumOfPromotionsPerLoop; | |||
339 | ||||
340 | if (ExitingBlocks.size() > SpeculativeCounterPromotionMaxExiting) | |||
341 | return 0; | |||
342 | ||||
343 | // Whether the target block is in a loop does not matter: | |||
344 | if (SpeculativeCounterPromotionToLoop) | |||
345 | return MaxNumOfPromotionsPerLoop; | |||
346 | ||||
347 | // Now check the target block: | |||
348 | unsigned MaxProm = MaxNumOfPromotionsPerLoop; | |||
349 | for (auto *TargetBlock : LoopExitBlocks) { | |||
350 | auto *TargetLoop = LI.getLoopFor(TargetBlock); | |||
351 | if (!TargetLoop) | |||
352 | continue; | |||
353 | unsigned MaxPromForTarget = getMaxNumOfPromotionsInLoop(TargetLoop); | |||
354 | unsigned PendingCandsInTarget = LoopToCandidates[TargetLoop].size(); | |||
355 | MaxProm = | |||
356 | std::min(MaxProm, std::max(MaxPromForTarget, PendingCandsInTarget) - | |||
357 | PendingCandsInTarget); | |||
358 | } | |||
359 | return MaxProm; | |||
360 | } | |||
361 | ||||
362 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> &LoopToCandidates; | |||
363 | SmallVector<BasicBlock *, 8> ExitBlocks; | |||
364 | SmallVector<Instruction *, 8> InsertPts; | |||
365 | Loop &L; | |||
366 | LoopInfo &LI; | |||
367 | BlockFrequencyInfo *BFI; | |||
368 | }; | |||
369 | ||||
370 | } // end anonymous namespace | |||
371 | ||||
372 | PreservedAnalyses InstrProfiling::run(Module &M, ModuleAnalysisManager &AM) { | |||
373 | auto &TLI = AM.getResult<TargetLibraryAnalysis>(M); | |||
374 | if (!run(M, TLI)) | |||
| ||||
375 | return PreservedAnalyses::all(); | |||
376 | ||||
377 | return PreservedAnalyses::none(); | |||
378 | } | |||
379 | ||||
380 | char InstrProfilingLegacyPass::ID = 0; | |||
381 | INITIALIZE_PASS_BEGIN(static void *initializeInstrProfilingLegacyPassPassOnce(PassRegistry &Registry) { | |||
382 | InstrProfilingLegacyPass, "instrprof",static void *initializeInstrProfilingLegacyPassPassOnce(PassRegistry &Registry) { | |||
383 | "Frontend instrumentation-based coverage lowering.", false, false)static void *initializeInstrProfilingLegacyPassPassOnce(PassRegistry &Registry) { | |||
384 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | |||
385 | INITIALIZE_PASS_END(PassInfo *PI = new PassInfo( "Frontend instrumentation-based coverage lowering." , "instrprof", &InstrProfilingLegacyPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<InstrProfilingLegacyPass>), false, false ); Registry.registerPass(*PI, true); return PI; } static llvm ::once_flag InitializeInstrProfilingLegacyPassPassFlag; void llvm ::initializeInstrProfilingLegacyPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeInstrProfilingLegacyPassPassFlag , initializeInstrProfilingLegacyPassPassOnce, std::ref(Registry )); } | |||
386 | InstrProfilingLegacyPass, "instrprof",PassInfo *PI = new PassInfo( "Frontend instrumentation-based coverage lowering." , "instrprof", &InstrProfilingLegacyPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<InstrProfilingLegacyPass>), false, false ); Registry.registerPass(*PI, true); return PI; } static llvm ::once_flag InitializeInstrProfilingLegacyPassPassFlag; void llvm ::initializeInstrProfilingLegacyPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeInstrProfilingLegacyPassPassFlag , initializeInstrProfilingLegacyPassPassOnce, std::ref(Registry )); } | |||
387 | "Frontend instrumentation-based coverage lowering.", false, false)PassInfo *PI = new PassInfo( "Frontend instrumentation-based coverage lowering." , "instrprof", &InstrProfilingLegacyPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<InstrProfilingLegacyPass>), false, false ); Registry.registerPass(*PI, true); return PI; } static llvm ::once_flag InitializeInstrProfilingLegacyPassPassFlag; void llvm ::initializeInstrProfilingLegacyPassPass(PassRegistry &Registry ) { llvm::call_once(InitializeInstrProfilingLegacyPassPassFlag , initializeInstrProfilingLegacyPassPassOnce, std::ref(Registry )); } | |||
388 | ||||
389 | ModulePass * | |||
390 | llvm::createInstrProfilingLegacyPass(const InstrProfOptions &Options, | |||
391 | bool IsCS) { | |||
392 | return new InstrProfilingLegacyPass(Options, IsCS); | |||
393 | } | |||
394 | ||||
395 | static InstrProfIncrementInst *castToIncrementInst(Instruction *Instr) { | |||
396 | InstrProfIncrementInst *Inc = dyn_cast<InstrProfIncrementInstStep>(Instr); | |||
397 | if (Inc) | |||
398 | return Inc; | |||
399 | return dyn_cast<InstrProfIncrementInst>(Instr); | |||
400 | } | |||
401 | ||||
402 | bool InstrProfiling::lowerIntrinsics(Function *F) { | |||
403 | bool MadeChange = false; | |||
404 | PromotionCandidates.clear(); | |||
405 | for (BasicBlock &BB : *F) { | |||
406 | for (auto I = BB.begin(), E = BB.end(); I != E;) { | |||
407 | auto Instr = I++; | |||
408 | InstrProfIncrementInst *Inc = castToIncrementInst(&*Instr); | |||
409 | if (Inc) { | |||
410 | lowerIncrement(Inc); | |||
411 | MadeChange = true; | |||
412 | } else if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(Instr)) { | |||
413 | lowerValueProfileInst(Ind); | |||
414 | MadeChange = true; | |||
415 | } | |||
416 | } | |||
417 | } | |||
418 | ||||
419 | if (!MadeChange) | |||
420 | return false; | |||
421 | ||||
422 | promoteCounterLoadStores(F); | |||
423 | return true; | |||
424 | } | |||
425 | ||||
426 | bool InstrProfiling::isCounterPromotionEnabled() const { | |||
427 | if (DoCounterPromotion.getNumOccurrences() > 0) | |||
428 | return DoCounterPromotion; | |||
429 | ||||
430 | return Options.DoCounterPromotion; | |||
431 | } | |||
432 | ||||
433 | void InstrProfiling::promoteCounterLoadStores(Function *F) { | |||
434 | if (!isCounterPromotionEnabled()) | |||
435 | return; | |||
436 | ||||
437 | DominatorTree DT(*F); | |||
438 | LoopInfo LI(DT); | |||
439 | DenseMap<Loop *, SmallVector<LoadStorePair, 8>> LoopPromotionCandidates; | |||
440 | ||||
441 | std::unique_ptr<BlockFrequencyInfo> BFI; | |||
442 | if (Options.UseBFIInPromotion) { | |||
443 | std::unique_ptr<BranchProbabilityInfo> BPI; | |||
444 | BPI.reset(new BranchProbabilityInfo(*F, LI, TLI)); | |||
445 | BFI.reset(new BlockFrequencyInfo(*F, *BPI, LI)); | |||
446 | } | |||
447 | ||||
448 | for (const auto &LoadStore : PromotionCandidates) { | |||
449 | auto *CounterLoad = LoadStore.first; | |||
450 | auto *CounterStore = LoadStore.second; | |||
451 | BasicBlock *BB = CounterLoad->getParent(); | |||
452 | Loop *ParentLoop = LI.getLoopFor(BB); | |||
453 | if (!ParentLoop) | |||
454 | continue; | |||
455 | LoopPromotionCandidates[ParentLoop].emplace_back(CounterLoad, CounterStore); | |||
456 | } | |||
457 | ||||
458 | SmallVector<Loop *, 4> Loops = LI.getLoopsInPreorder(); | |||
459 | ||||
460 | // Do a post-order traversal of the loops so that counter updates can be | |||
461 | // iteratively hoisted outside the loop nest. | |||
462 | for (auto *Loop : llvm::reverse(Loops)) { | |||
463 | PGOCounterPromoter Promoter(LoopPromotionCandidates, *Loop, LI, BFI.get()); | |||
464 | Promoter.run(&TotalCountersPromoted); | |||
465 | } | |||
466 | } | |||
467 | ||||
468 | /// Check if the module contains uses of any profiling intrinsics. | |||
469 | static bool containsProfilingIntrinsics(Module &M) { | |||
470 | if (auto *F = M.getFunction( | |||
471 | Intrinsic::getName(llvm::Intrinsic::instrprof_increment))) | |||
472 | if (!F->use_empty()) | |||
473 | return true; | |||
474 | if (auto *F = M.getFunction( | |||
475 | Intrinsic::getName(llvm::Intrinsic::instrprof_increment_step))) | |||
476 | if (!F->use_empty()) | |||
477 | return true; | |||
478 | if (auto *F = M.getFunction( | |||
479 | Intrinsic::getName(llvm::Intrinsic::instrprof_value_profile))) | |||
480 | if (!F->use_empty()) | |||
481 | return true; | |||
482 | return false; | |||
483 | } | |||
484 | ||||
485 | bool InstrProfiling::run(Module &M, const TargetLibraryInfo &TLI) { | |||
486 | this->M = &M; | |||
487 | this->TLI = &TLI; | |||
488 | NamesVar = nullptr; | |||
489 | NamesSize = 0; | |||
490 | ProfileDataMap.clear(); | |||
491 | UsedVars.clear(); | |||
492 | getMemOPSizeRangeFromOption(MemOPSizeRange, MemOPSizeRangeStart, | |||
493 | MemOPSizeRangeLast); | |||
494 | TT = Triple(M.getTargetTriple()); | |||
495 | ||||
496 | // Emit the runtime hook even if no counters are present. | |||
497 | bool MadeChange = emitRuntimeHook(); | |||
498 | ||||
499 | // Improve compile time by avoiding linear scans when there is no work. | |||
500 | GlobalVariable *CoverageNamesVar = | |||
501 | M.getNamedGlobal(getCoverageUnusedNamesVarName()); | |||
502 | if (!containsProfilingIntrinsics(M) && !CoverageNamesVar) | |||
503 | return MadeChange; | |||
504 | ||||
505 | // We did not know how many value sites there would be inside | |||
506 | // the instrumented function. This is counting the number of instrumented | |||
507 | // target value sites to enter it as field in the profile data variable. | |||
508 | for (Function &F : M) { | |||
509 | InstrProfIncrementInst *FirstProfIncInst = nullptr; | |||
510 | for (BasicBlock &BB : F) | |||
511 | for (auto I = BB.begin(), E = BB.end(); I != E; I++) | |||
512 | if (auto *Ind = dyn_cast<InstrProfValueProfileInst>(I)) | |||
513 | computeNumValueSiteCounts(Ind); | |||
514 | else if (FirstProfIncInst == nullptr) | |||
515 | FirstProfIncInst = dyn_cast<InstrProfIncrementInst>(I); | |||
516 | ||||
517 | // Value profiling intrinsic lowering requires per-function profile data | |||
518 | // variable to be created first. | |||
519 | if (FirstProfIncInst != nullptr) | |||
520 | static_cast<void>(getOrCreateRegionCounters(FirstProfIncInst)); | |||
521 | } | |||
522 | ||||
523 | for (Function &F : M) | |||
524 | MadeChange |= lowerIntrinsics(&F); | |||
525 | ||||
526 | if (CoverageNamesVar) { | |||
527 | lowerCoverageData(CoverageNamesVar); | |||
528 | MadeChange = true; | |||
529 | } | |||
530 | ||||
531 | if (!MadeChange) | |||
532 | return false; | |||
533 | ||||
534 | emitVNodes(); | |||
535 | emitNameData(); | |||
536 | emitRegistration(); | |||
537 | emitUses(); | |||
538 | emitInitialization(); | |||
539 | return true; | |||
540 | } | |||
541 | ||||
542 | static FunctionCallee | |||
543 | getOrInsertValueProfilingCall(Module &M, const TargetLibraryInfo &TLI, | |||
544 | bool IsRange = false) { | |||
545 | LLVMContext &Ctx = M.getContext(); | |||
546 | auto *ReturnTy = Type::getVoidTy(M.getContext()); | |||
547 | ||||
548 | AttributeList AL; | |||
549 | if (auto AK = TLI.getExtAttrForI32Param(false)) | |||
550 | AL = AL.addParamAttribute(M.getContext(), 2, AK); | |||
551 | ||||
552 | if (!IsRange) { | |||
553 | Type *ParamTypes[] = { | |||
554 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType | |||
555 | #include "llvm/ProfileData/InstrProfData.inc" | |||
556 | }; | |||
557 | auto *ValueProfilingCallTy = | |||
558 | FunctionType::get(ReturnTy, makeArrayRef(ParamTypes), false); | |||
559 | return M.getOrInsertFunction(getInstrProfValueProfFuncName(), | |||
560 | ValueProfilingCallTy, AL); | |||
561 | } else { | |||
562 | Type *RangeParamTypes[] = { | |||
563 | #define VALUE_RANGE_PROF 1 | |||
564 | #define VALUE_PROF_FUNC_PARAM(ParamType, ParamName, ParamLLVMType) ParamLLVMType | |||
565 | #include "llvm/ProfileData/InstrProfData.inc" | |||
566 | #undef VALUE_RANGE_PROF | |||
567 | }; | |||
568 | auto *ValueRangeProfilingCallTy = | |||
569 | FunctionType::get(ReturnTy, makeArrayRef(RangeParamTypes), false); | |||
570 | return M.getOrInsertFunction(getInstrProfValueRangeProfFuncName(), | |||
571 | ValueRangeProfilingCallTy, AL); | |||
572 | } | |||
573 | } | |||
574 | ||||
575 | void InstrProfiling::computeNumValueSiteCounts(InstrProfValueProfileInst *Ind) { | |||
576 | GlobalVariable *Name = Ind->getName(); | |||
577 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); | |||
578 | uint64_t Index = Ind->getIndex()->getZExtValue(); | |||
579 | auto It = ProfileDataMap.find(Name); | |||
580 | if (It == ProfileDataMap.end()) { | |||
581 | PerFunctionProfileData PD; | |||
582 | PD.NumValueSites[ValueKind] = Index + 1; | |||
583 | ProfileDataMap[Name] = PD; | |||
584 | } else if (It->second.NumValueSites[ValueKind] <= Index) | |||
585 | It->second.NumValueSites[ValueKind] = Index + 1; | |||
586 | } | |||
587 | ||||
588 | void InstrProfiling::lowerValueProfileInst(InstrProfValueProfileInst *Ind) { | |||
589 | GlobalVariable *Name = Ind->getName(); | |||
590 | auto It = ProfileDataMap.find(Name); | |||
591 | assert(It != ProfileDataMap.end() && It->second.DataVar &&((It != ProfileDataMap.end() && It->second.DataVar && "value profiling detected in function with no counter incerement" ) ? static_cast<void> (0) : __assert_fail ("It != ProfileDataMap.end() && It->second.DataVar && \"value profiling detected in function with no counter incerement\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Transforms/Instrumentation/InstrProfiling.cpp" , 592, __PRETTY_FUNCTION__)) | |||
592 | "value profiling detected in function with no counter incerement")((It != ProfileDataMap.end() && It->second.DataVar && "value profiling detected in function with no counter incerement" ) ? static_cast<void> (0) : __assert_fail ("It != ProfileDataMap.end() && It->second.DataVar && \"value profiling detected in function with no counter incerement\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Transforms/Instrumentation/InstrProfiling.cpp" , 592, __PRETTY_FUNCTION__)); | |||
593 | ||||
594 | GlobalVariable *DataVar = It->second.DataVar; | |||
595 | uint64_t ValueKind = Ind->getValueKind()->getZExtValue(); | |||
596 | uint64_t Index = Ind->getIndex()->getZExtValue(); | |||
597 | for (uint32_t Kind = IPVK_First; Kind < ValueKind; ++Kind) | |||
598 | Index += It->second.NumValueSites[Kind]; | |||
599 | ||||
600 | IRBuilder<> Builder(Ind); | |||
601 | bool IsRange = (Ind->getValueKind()->getZExtValue() == | |||
602 | llvm::InstrProfValueKind::IPVK_MemOPSize); | |||
603 | CallInst *Call = nullptr; | |||
604 | if (!IsRange) { | |||
605 | Value *Args[3] = {Ind->getTargetValue(), | |||
606 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), | |||
607 | Builder.getInt32(Index)}; | |||
608 | Call = Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI), Args); | |||
609 | } else { | |||
610 | Value *Args[6] = { | |||
611 | Ind->getTargetValue(), | |||
612 | Builder.CreateBitCast(DataVar, Builder.getInt8PtrTy()), | |||
613 | Builder.getInt32(Index), | |||
614 | Builder.getInt64(MemOPSizeRangeStart), | |||
615 | Builder.getInt64(MemOPSizeRangeLast), | |||
616 | Builder.getInt64(MemOPSizeLarge == 0 ? INT64_MIN(-9223372036854775807L -1) : MemOPSizeLarge)}; | |||
617 | Call = | |||
618 | Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args); | |||
619 | } | |||
620 | if (auto AK = TLI->getExtAttrForI32Param(false)) | |||
621 | Call->addParamAttr(2, AK); | |||
622 | Ind->replaceAllUsesWith(Call); | |||
623 | Ind->eraseFromParent(); | |||
624 | } | |||
625 | ||||
626 | void InstrProfiling::lowerIncrement(InstrProfIncrementInst *Inc) { | |||
627 | GlobalVariable *Counters = getOrCreateRegionCounters(Inc); | |||
628 | ||||
629 | IRBuilder<> Builder(Inc); | |||
630 | uint64_t Index = Inc->getIndex()->getZExtValue(); | |||
631 | Value *Addr = Builder.CreateConstInBoundsGEP2_64(Counters->getValueType(), | |||
632 | Counters, 0, Index); | |||
633 | ||||
634 | if (Options.Atomic || AtomicCounterUpdateAll) { | |||
635 | Builder.CreateAtomicRMW(AtomicRMWInst::Add, Addr, Inc->getStep(), | |||
636 | AtomicOrdering::Monotonic); | |||
637 | } else { | |||
638 | Value *IncStep = Inc->getStep(); | |||
639 | Value *Load = Builder.CreateLoad(IncStep->getType(), Addr, "pgocount"); | |||
640 | auto *Count = Builder.CreateAdd(Load, Inc->getStep()); | |||
641 | auto *Store = Builder.CreateStore(Count, Addr); | |||
642 | if (isCounterPromotionEnabled()) | |||
643 | PromotionCandidates.emplace_back(cast<Instruction>(Load), Store); | |||
644 | } | |||
645 | Inc->eraseFromParent(); | |||
646 | } | |||
647 | ||||
648 | void InstrProfiling::lowerCoverageData(GlobalVariable *CoverageNamesVar) { | |||
649 | ConstantArray *Names = | |||
650 | cast<ConstantArray>(CoverageNamesVar->getInitializer()); | |||
651 | for (unsigned I = 0, E = Names->getNumOperands(); I < E; ++I) { | |||
652 | Constant *NC = Names->getOperand(I); | |||
653 | Value *V = NC->stripPointerCasts(); | |||
654 | assert(isa<GlobalVariable>(V) && "Missing reference to function name")((isa<GlobalVariable>(V) && "Missing reference to function name" ) ? static_cast<void> (0) : __assert_fail ("isa<GlobalVariable>(V) && \"Missing reference to function name\"" , "/build/llvm-toolchain-snapshot-9~svn362543/lib/Transforms/Instrumentation/InstrProfiling.cpp" , 654, __PRETTY_FUNCTION__)); | |||
655 | GlobalVariable *Name = cast<GlobalVariable>(V); | |||
656 | ||||
657 | Name->setLinkage(GlobalValue::PrivateLinkage); | |||
658 | ReferencedNames.push_back(Name); | |||
659 | NC->dropAllReferences(); | |||
660 | } | |||
661 | CoverageNamesVar->eraseFromParent(); | |||
662 | } | |||
663 | ||||
664 | /// Get the name of a profiling variable for a particular function. | |||
665 | static std::string getVarName(InstrProfIncrementInst *Inc, StringRef Prefix) { | |||
666 | StringRef NamePrefix = getInstrProfNameVarPrefix(); | |||
667 | StringRef Name = Inc->getName()->getName().substr(NamePrefix.size()); | |||
668 | Function *F = Inc->getParent()->getParent(); | |||
669 | Module *M = F->getParent(); | |||
670 | if (!DoHashBasedCounterSplit || !isIRPGOFlagSet(M) || | |||
671 | !canRenameComdatFunc(*F)) | |||
672 | return (Prefix + Name).str(); | |||
673 | uint64_t FuncHash = Inc->getHash()->getZExtValue(); | |||
674 | SmallVector<char, 24> HashPostfix; | |||
675 | if (Name.endswith((Twine(".") + Twine(FuncHash)).toStringRef(HashPostfix))) | |||
676 | return (Prefix + Name).str(); | |||
677 | return (Prefix + Name + "." + Twine(FuncHash)).str(); | |||
678 | } | |||
679 | ||||
680 | static inline bool shouldRecordFunctionAddr(Function *F) { | |||
681 | // Check the linkage | |||
682 | bool HasAvailableExternallyLinkage = F->hasAvailableExternallyLinkage(); | |||
683 | if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() && | |||
684 | !HasAvailableExternallyLinkage) | |||
685 | return true; | |||
686 | ||||
687 | // A function marked 'alwaysinline' with available_externally linkage can't | |||
688 | // have its address taken. Doing so would create an undefined external ref to | |||
689 | // the function, which would fail to link. | |||
690 | if (HasAvailableExternallyLinkage && | |||
691 | F->hasFnAttribute(Attribute::AlwaysInline)) | |||
692 | return false; | |||
693 | ||||
694 | // Prohibit function address recording if the function is both internal and | |||
695 | // COMDAT. This avoids the profile data variable referencing internal symbols | |||
696 | // in COMDAT. | |||
697 | if (F->hasLocalLinkage() && F->hasComdat()) | |||
698 | return false; | |||
699 | ||||
700 | // Check uses of this function for other than direct calls or invokes to it. | |||
701 | // Inline virtual functions have linkeOnceODR linkage. When a key method | |||
702 | // exists, the vtable will only be emitted in the TU where the key method | |||
703 | // is defined. In a TU where vtable is not available, the function won't | |||
704 | // be 'addresstaken'. If its address is not recorded here, the profile data | |||
705 | // with missing address may be picked by the linker leading to missing | |||
706 | // indirect call target info. | |||
707 | return F->hasAddressTaken() || F->hasLinkOnceLinkage(); | |||
708 | } | |||
709 | ||||
710 | static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) { | |||
711 | // Don't do this for Darwin. compiler-rt uses linker magic. | |||
712 | if (TT.isOSDarwin()) | |||
713 | return false; | |||
714 | // Use linker script magic to get data/cnts/name start/end. | |||
715 | if (TT.isOSLinux() || TT.isOSFreeBSD() || TT.isOSNetBSD() || | |||
716 | TT.isOSFuchsia() || TT.isPS4CPU() || TT.isOSWindows()) | |||
717 | return false; | |||
718 | ||||
719 | return true; | |||
720 | } | |||
721 | ||||
722 | GlobalVariable * | |||
723 | InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { | |||
724 | GlobalVariable *NamePtr = Inc->getName(); | |||
725 | auto It = ProfileDataMap.find(NamePtr); | |||
726 | PerFunctionProfileData PD; | |||
727 | if (It != ProfileDataMap.end()) { | |||
728 | if (It->second.RegionCounters) | |||
729 | return It->second.RegionCounters; | |||
730 | PD = It->second; | |||
731 | } | |||
732 | ||||
733 | // Match the linkage and visibility of the name global, except on COFF, where | |||
734 | // the linkage must be local and consequentially the visibility must be | |||
735 | // default. | |||
736 | Function *Fn = Inc->getParent()->getParent(); | |||
737 | GlobalValue::LinkageTypes Linkage = NamePtr->getLinkage(); | |||
738 | GlobalValue::VisibilityTypes Visibility = NamePtr->getVisibility(); | |||
739 | if (TT.isOSBinFormatCOFF()) { | |||
740 | Linkage = GlobalValue::InternalLinkage; | |||
741 | Visibility = GlobalValue::DefaultVisibility; | |||
742 | } | |||
743 | ||||
744 | // Move the name variable to the right section. Place them in a COMDAT group | |||
745 | // if the associated function is a COMDAT. This will make sure that only one | |||
746 | // copy of counters of the COMDAT function will be emitted after linking. Keep | |||
747 | // in mind that this pass may run before the inliner, so we need to create a | |||
748 | // new comdat group for the counters and profiling data. If we use the comdat | |||
749 | // of the parent function, that will result in relocations against discarded | |||
750 | // sections. | |||
751 | Comdat *Cmdt = nullptr; | |||
752 | GlobalValue::LinkageTypes CounterLinkage = Linkage; | |||
753 | if (needsComdatForCounter(*Fn, *M)) { | |||
754 | StringRef CmdtPrefix = getInstrProfComdatPrefix(); | |||
755 | if (TT.isOSBinFormatCOFF()) { | |||
756 | // For COFF, the comdat group name must be the name of a symbol in the | |||
757 | // group. Use the counter variable name, and upgrade its linkage to | |||
758 | // something externally visible, like linkonce_odr. | |||
759 | CmdtPrefix = getInstrProfCountersVarPrefix(); | |||
760 | CounterLinkage = GlobalValue::LinkOnceODRLinkage; | |||
761 | } | |||
762 | Cmdt = M->getOrInsertComdat(getVarName(Inc, CmdtPrefix)); | |||
763 | } | |||
764 | ||||
765 | uint64_t NumCounters = Inc->getNumCounters()->getZExtValue(); | |||
766 | LLVMContext &Ctx = M->getContext(); | |||
767 | ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters); | |||
768 | ||||
769 | // Create the counters variable. | |||
770 | auto *CounterPtr = | |||
771 | new GlobalVariable(*M, CounterTy, false, Linkage, | |||
772 | Constant::getNullValue(CounterTy), | |||
773 | getVarName(Inc, getInstrProfCountersVarPrefix())); | |||
774 | CounterPtr->setVisibility(Visibility); | |||
775 | CounterPtr->setSection( | |||
776 | getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat())); | |||
777 | CounterPtr->setAlignment(8); | |||
778 | CounterPtr->setComdat(Cmdt); | |||
779 | CounterPtr->setLinkage(CounterLinkage); | |||
780 | ||||
781 | auto *Int8PtrTy = Type::getInt8PtrTy(Ctx); | |||
782 | // Allocate statically the array of pointers to value profile nodes for | |||
783 | // the current function. | |||
784 | Constant *ValuesPtrExpr = ConstantPointerNull::get(Int8PtrTy); | |||
785 | if (ValueProfileStaticAlloc && !needsRuntimeRegistrationOfSectionRange(TT)) { | |||
786 | uint64_t NS = 0; | |||
787 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) | |||
788 | NS += PD.NumValueSites[Kind]; | |||
789 | if (NS) { | |||
790 | ArrayType *ValuesTy = ArrayType::get(Type::getInt64Ty(Ctx), NS); | |||
791 | ||||
792 | auto *ValuesVar = | |||
793 | new GlobalVariable(*M, ValuesTy, false, Linkage, | |||
794 | Constant::getNullValue(ValuesTy), | |||
795 | getVarName(Inc, getInstrProfValuesVarPrefix())); | |||
796 | ValuesVar->setVisibility(Visibility); | |||
797 | ValuesVar->setSection( | |||
798 | getInstrProfSectionName(IPSK_vals, TT.getObjectFormat())); | |||
799 | ValuesVar->setAlignment(8); | |||
800 | ValuesVar->setComdat(Cmdt); | |||
801 | ValuesPtrExpr = | |||
802 | ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx)); | |||
803 | } | |||
804 | } | |||
805 | ||||
806 | // Create data variable. | |||
807 | auto *Int16Ty = Type::getInt16Ty(Ctx); | |||
808 | auto *Int16ArrayTy = ArrayType::get(Int16Ty, IPVK_Last + 1); | |||
809 | Type *DataTypes[] = { | |||
810 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) LLVMType, | |||
811 | #include "llvm/ProfileData/InstrProfData.inc" | |||
812 | }; | |||
813 | auto *DataTy = StructType::get(Ctx, makeArrayRef(DataTypes)); | |||
814 | ||||
815 | Constant *FunctionAddr = shouldRecordFunctionAddr(Fn) | |||
816 | ? ConstantExpr::getBitCast(Fn, Int8PtrTy) | |||
817 | : ConstantPointerNull::get(Int8PtrTy); | |||
818 | ||||
819 | Constant *Int16ArrayVals[IPVK_Last + 1]; | |||
820 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) | |||
821 | Int16ArrayVals[Kind] = ConstantInt::get(Int16Ty, PD.NumValueSites[Kind]); | |||
822 | ||||
823 | Constant *DataVals[] = { | |||
824 | #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, | |||
825 | #include "llvm/ProfileData/InstrProfData.inc" | |||
826 | }; | |||
827 | auto *Data = new GlobalVariable(*M, DataTy, false, Linkage, | |||
828 | ConstantStruct::get(DataTy, DataVals), | |||
829 | getVarName(Inc, getInstrProfDataVarPrefix())); | |||
830 | Data->setVisibility(Visibility); | |||
831 | Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat())); | |||
832 | Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT8); | |||
833 | Data->setComdat(Cmdt); | |||
834 | ||||
835 | PD.RegionCounters = CounterPtr; | |||
836 | PD.DataVar = Data; | |||
837 | ProfileDataMap[NamePtr] = PD; | |||
838 | ||||
839 | // Mark the data variable as used so that it isn't stripped out. | |||
840 | UsedVars.push_back(Data); | |||
841 | // Now that the linkage set by the FE has been passed to the data and counter | |||
842 | // variables, reset Name variable's linkage and visibility to private so that | |||
843 | // it can be removed later by the compiler. | |||
844 | NamePtr->setLinkage(GlobalValue::PrivateLinkage); | |||
845 | // Collect the referenced names to be used by emitNameData. | |||
846 | ReferencedNames.push_back(NamePtr); | |||
847 | ||||
848 | return CounterPtr; | |||
849 | } | |||
850 | ||||
851 | void InstrProfiling::emitVNodes() { | |||
852 | if (!ValueProfileStaticAlloc) | |||
853 | return; | |||
854 | ||||
855 | // For now only support this on platforms that do | |||
856 | // not require runtime registration to discover | |||
857 | // named section start/end. | |||
858 | if (needsRuntimeRegistrationOfSectionRange(TT)) | |||
859 | return; | |||
860 | ||||
861 | size_t TotalNS = 0; | |||
862 | for (auto &PD : ProfileDataMap) { | |||
863 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) | |||
864 | TotalNS += PD.second.NumValueSites[Kind]; | |||
865 | } | |||
866 | ||||
867 | if (!TotalNS) | |||
868 | return; | |||
869 | ||||
870 | uint64_t NumCounters = TotalNS * NumCountersPerValueSite; | |||
871 | // Heuristic for small programs with very few total value sites. | |||
872 | // The default value of vp-counters-per-site is chosen based on | |||
873 | // the observation that large apps usually have a low percentage | |||
874 | // of value sites that actually have any profile data, and thus | |||
875 | // the average number of counters per site is low. For small | |||
876 | // apps with very few sites, this may not be true. Bump up the | |||
877 | // number of counters in this case. | |||
878 | #define INSTR_PROF_MIN_VAL_COUNTS10 10 | |||
879 | if (NumCounters < INSTR_PROF_MIN_VAL_COUNTS10) | |||
880 | NumCounters = std::max(INSTR_PROF_MIN_VAL_COUNTS10, (int)NumCounters * 2); | |||
881 | ||||
882 | auto &Ctx = M->getContext(); | |||
883 | Type *VNodeTypes[] = { | |||
884 | #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Init) LLVMType, | |||
885 | #include "llvm/ProfileData/InstrProfData.inc" | |||
886 | }; | |||
887 | auto *VNodeTy = StructType::get(Ctx, makeArrayRef(VNodeTypes)); | |||
888 | ||||
889 | ArrayType *VNodesTy = ArrayType::get(VNodeTy, NumCounters); | |||
890 | auto *VNodesVar = new GlobalVariable( | |||
891 | *M, VNodesTy, false, GlobalValue::PrivateLinkage, | |||
892 | Constant::getNullValue(VNodesTy), getInstrProfVNodesVarName()); | |||
893 | VNodesVar->setSection( | |||
894 | getInstrProfSectionName(IPSK_vnodes, TT.getObjectFormat())); | |||
895 | UsedVars.push_back(VNodesVar); | |||
896 | } | |||
897 | ||||
898 | void InstrProfiling::emitNameData() { | |||
899 | std::string UncompressedData; | |||
900 | ||||
901 | if (ReferencedNames.empty()) | |||
902 | return; | |||
903 | ||||
904 | std::string CompressedNameStr; | |||
905 | if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, | |||
906 | DoNameCompression)) { | |||
907 | report_fatal_error(toString(std::move(E)), false); | |||
908 | } | |||
909 | ||||
910 | auto &Ctx = M->getContext(); | |||
911 | auto *NamesVal = ConstantDataArray::getString( | |||
912 | Ctx, StringRef(CompressedNameStr), false); | |||
913 | NamesVar = new GlobalVariable(*M, NamesVal->getType(), true, | |||
914 | GlobalValue::PrivateLinkage, NamesVal, | |||
915 | getInstrProfNamesVarName()); | |||
916 | NamesSize = CompressedNameStr.size(); | |||
917 | NamesVar->setSection( | |||
918 | getInstrProfSectionName(IPSK_name, TT.getObjectFormat())); | |||
919 | // On COFF, it's important to reduce the alignment down to 1 to prevent the | |||
920 | // linker from inserting padding before the start of the names section or | |||
921 | // between names entries. | |||
922 | NamesVar->setAlignment(1); | |||
923 | UsedVars.push_back(NamesVar); | |||
924 | ||||
925 | for (auto *NamePtr : ReferencedNames) | |||
926 | NamePtr->eraseFromParent(); | |||
927 | } | |||
928 | ||||
929 | void InstrProfiling::emitRegistration() { | |||
930 | if (!needsRuntimeRegistrationOfSectionRange(TT)) | |||
931 | return; | |||
932 | ||||
933 | // Construct the function. | |||
934 | auto *VoidTy = Type::getVoidTy(M->getContext()); | |||
935 | auto *VoidPtrTy = Type::getInt8PtrTy(M->getContext()); | |||
936 | auto *Int64Ty = Type::getInt64Ty(M->getContext()); | |||
937 | auto *RegisterFTy = FunctionType::get(VoidTy, false); | |||
938 | auto *RegisterF = Function::Create(RegisterFTy, GlobalValue::InternalLinkage, | |||
939 | getInstrProfRegFuncsName(), M); | |||
940 | RegisterF->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); | |||
941 | if (Options.NoRedZone) | |||
942 | RegisterF->addFnAttr(Attribute::NoRedZone); | |||
943 | ||||
944 | auto *RuntimeRegisterTy = FunctionType::get(VoidTy, VoidPtrTy, false); | |||
945 | auto *RuntimeRegisterF = | |||
946 | Function::Create(RuntimeRegisterTy, GlobalVariable::ExternalLinkage, | |||
947 | getInstrProfRegFuncName(), M); | |||
948 | ||||
949 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", RegisterF)); | |||
950 | for (Value *Data : UsedVars) | |||
951 | if (Data != NamesVar && !isa<Function>(Data)) | |||
952 | IRB.CreateCall(RuntimeRegisterF, IRB.CreateBitCast(Data, VoidPtrTy)); | |||
953 | ||||
954 | if (NamesVar) { | |||
955 | Type *ParamTypes[] = {VoidPtrTy, Int64Ty}; | |||
956 | auto *NamesRegisterTy = | |||
957 | FunctionType::get(VoidTy, makeArrayRef(ParamTypes), false); | |||
958 | auto *NamesRegisterF = | |||
959 | Function::Create(NamesRegisterTy, GlobalVariable::ExternalLinkage, | |||
960 | getInstrProfNamesRegFuncName(), M); | |||
961 | IRB.CreateCall(NamesRegisterF, {IRB.CreateBitCast(NamesVar, VoidPtrTy), | |||
962 | IRB.getInt64(NamesSize)}); | |||
963 | } | |||
964 | ||||
965 | IRB.CreateRetVoid(); | |||
966 | } | |||
967 | ||||
968 | bool InstrProfiling::emitRuntimeHook() { | |||
969 | // We expect the linker to be invoked with -u<hook_var> flag for linux, | |||
970 | // for which case there is no need to emit the user function. | |||
971 | if (TT.isOSLinux()) | |||
972 | return false; | |||
973 | ||||
974 | // If the module's provided its own runtime, we don't need to do anything. | |||
975 | if (M->getGlobalVariable(getInstrProfRuntimeHookVarName())) | |||
976 | return false; | |||
977 | ||||
978 | // Declare an external variable that will pull in the runtime initialization. | |||
979 | auto *Int32Ty = Type::getInt32Ty(M->getContext()); | |||
980 | auto *Var = | |||
981 | new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage, | |||
982 | nullptr, getInstrProfRuntimeHookVarName()); | |||
983 | ||||
984 | // Make a function that uses it. | |||
985 | auto *User = Function::Create(FunctionType::get(Int32Ty, false), | |||
986 | GlobalValue::LinkOnceODRLinkage, | |||
987 | getInstrProfRuntimeHookVarUseFuncName(), M); | |||
988 | User->addFnAttr(Attribute::NoInline); | |||
989 | if (Options.NoRedZone) | |||
990 | User->addFnAttr(Attribute::NoRedZone); | |||
991 | User->setVisibility(GlobalValue::HiddenVisibility); | |||
992 | if (TT.supportsCOMDAT()) | |||
993 | User->setComdat(M->getOrInsertComdat(User->getName())); | |||
994 | ||||
995 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", User)); | |||
996 | auto *Load = IRB.CreateLoad(Int32Ty, Var); | |||
997 | IRB.CreateRet(Load); | |||
998 | ||||
999 | // Mark the user variable as used so that it isn't stripped out. | |||
1000 | UsedVars.push_back(User); | |||
1001 | return true; | |||
1002 | } | |||
1003 | ||||
1004 | void InstrProfiling::emitUses() { | |||
1005 | if (!UsedVars.empty()) | |||
1006 | appendToUsed(*M, UsedVars); | |||
1007 | } | |||
1008 | ||||
1009 | void InstrProfiling::emitInitialization() { | |||
1010 | // Create ProfileFileName variable. Don't don't this for the | |||
1011 | // context-sensitive instrumentation lowering: This lowering is after | |||
1012 | // LTO/ThinLTO linking. Pass PGOInstrumentationGenCreateVar should | |||
1013 | // have already create the variable before LTO/ThinLTO linking. | |||
1014 | if (!IsCS) | |||
1015 | createProfileFileNameVar(*M, Options.InstrProfileOutput); | |||
1016 | Function *RegisterF = M->getFunction(getInstrProfRegFuncsName()); | |||
1017 | if (!RegisterF) | |||
1018 | return; | |||
1019 | ||||
1020 | // Create the initialization function. | |||
1021 | auto *VoidTy = Type::getVoidTy(M->getContext()); | |||
1022 | auto *F = Function::Create(FunctionType::get(VoidTy, false), | |||
1023 | GlobalValue::InternalLinkage, | |||
1024 | getInstrProfInitFuncName(), M); | |||
1025 | F->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); | |||
1026 | F->addFnAttr(Attribute::NoInline); | |||
1027 | if (Options.NoRedZone) | |||
1028 | F->addFnAttr(Attribute::NoRedZone); | |||
1029 | ||||
1030 | // Add the basic block and the necessary calls. | |||
1031 | IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F)); | |||
1032 | IRB.CreateCall(RegisterF, {}); | |||
1033 | IRB.CreateRetVoid(); | |||
1034 | ||||
1035 | appendToGlobalCtors(*M, F, 0); | |||
1036 | } |
1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// | |||
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 | // | |||
9 | // This file defines an API used to report recoverable errors. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #ifndef LLVM_SUPPORT_ERROR_H | |||
14 | #define LLVM_SUPPORT_ERROR_H | |||
15 | ||||
16 | #include "llvm-c/Error.h" | |||
17 | #include "llvm/ADT/STLExtras.h" | |||
18 | #include "llvm/ADT/SmallVector.h" | |||
19 | #include "llvm/ADT/StringExtras.h" | |||
20 | #include "llvm/ADT/Twine.h" | |||
21 | #include "llvm/Config/abi-breaking.h" | |||
22 | #include "llvm/Support/AlignOf.h" | |||
23 | #include "llvm/Support/Compiler.h" | |||
24 | #include "llvm/Support/Debug.h" | |||
25 | #include "llvm/Support/ErrorHandling.h" | |||
26 | #include "llvm/Support/ErrorOr.h" | |||
27 | #include "llvm/Support/Format.h" | |||
28 | #include "llvm/Support/raw_ostream.h" | |||
29 | #include <algorithm> | |||
30 | #include <cassert> | |||
31 | #include <cstdint> | |||
32 | #include <cstdlib> | |||
33 | #include <functional> | |||
34 | #include <memory> | |||
35 | #include <new> | |||
36 | #include <string> | |||
37 | #include <system_error> | |||
38 | #include <type_traits> | |||
39 | #include <utility> | |||
40 | #include <vector> | |||
41 | ||||
42 | namespace llvm { | |||
43 | ||||
44 | class ErrorSuccess; | |||
45 | ||||
46 | /// Base class for error info classes. Do not extend this directly: Extend | |||
47 | /// the ErrorInfo template subclass instead. | |||
48 | class ErrorInfoBase { | |||
49 | public: | |||
50 | virtual ~ErrorInfoBase() = default; | |||
51 | ||||
52 | /// Print an error message to an output stream. | |||
53 | virtual void log(raw_ostream &OS) const = 0; | |||
54 | ||||
55 | /// Return the error message as a string. | |||
56 | virtual std::string message() const { | |||
57 | std::string Msg; | |||
58 | raw_string_ostream OS(Msg); | |||
59 | log(OS); | |||
60 | return OS.str(); | |||
61 | } | |||
62 | ||||
63 | /// Convert this error to a std::error_code. | |||
64 | /// | |||
65 | /// This is a temporary crutch to enable interaction with code still | |||
66 | /// using std::error_code. It will be removed in the future. | |||
67 | virtual std::error_code convertToErrorCode() const = 0; | |||
68 | ||||
69 | // Returns the class ID for this type. | |||
70 | static const void *classID() { return &ID; } | |||
71 | ||||
72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. | |||
73 | virtual const void *dynamicClassID() const = 0; | |||
74 | ||||
75 | // Check whether this instance is a subclass of the class identified by | |||
76 | // ClassID. | |||
77 | virtual bool isA(const void *const ClassID) const { | |||
78 | return ClassID == classID(); | |||
79 | } | |||
80 | ||||
81 | // Check whether this instance is a subclass of ErrorInfoT. | |||
82 | template <typename ErrorInfoT> bool isA() const { | |||
83 | return isA(ErrorInfoT::classID()); | |||
84 | } | |||
85 | ||||
86 | private: | |||
87 | virtual void anchor(); | |||
88 | ||||
89 | static char ID; | |||
90 | }; | |||
91 | ||||
92 | /// Lightweight error class with error context and mandatory checking. | |||
93 | /// | |||
94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states | |||
95 | /// are represented by setting the pointer to a ErrorInfoBase subclass | |||
96 | /// instance containing information describing the failure. Success is | |||
97 | /// represented by a null pointer value. | |||
98 | /// | |||
99 | /// Instances of Error also contains a 'Checked' flag, which must be set | |||
100 | /// before the destructor is called, otherwise the destructor will trigger a | |||
101 | /// runtime error. This enforces at runtime the requirement that all Error | |||
102 | /// instances be checked or returned to the caller. | |||
103 | /// | |||
104 | /// There are two ways to set the checked flag, depending on what state the | |||
105 | /// Error instance is in. For Error instances indicating success, it | |||
106 | /// is sufficient to invoke the boolean conversion operator. E.g.: | |||
107 | /// | |||
108 | /// @code{.cpp} | |||
109 | /// Error foo(<...>); | |||
110 | /// | |||
111 | /// if (auto E = foo(<...>)) | |||
112 | /// return E; // <- Return E if it is in the error state. | |||
113 | /// // We have verified that E was in the success state. It can now be safely | |||
114 | /// // destroyed. | |||
115 | /// @endcode | |||
116 | /// | |||
117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' | |||
118 | /// without testing the return value will raise a runtime error, even if foo | |||
119 | /// returns success. | |||
120 | /// | |||
121 | /// For Error instances representing failure, you must use either the | |||
122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: | |||
123 | /// | |||
124 | /// @code{.cpp} | |||
125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { | |||
126 | /// // Custom error info. | |||
127 | /// }; | |||
128 | /// | |||
129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } | |||
130 | /// | |||
131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. | |||
132 | /// auto NewE = | |||
133 | /// handleErrors(E, | |||
134 | /// [](const MyErrorInfo &M) { | |||
135 | /// // Deal with the error. | |||
136 | /// }, | |||
137 | /// [](std::unique_ptr<OtherError> M) -> Error { | |||
138 | /// if (canHandle(*M)) { | |||
139 | /// // handle error. | |||
140 | /// return Error::success(); | |||
141 | /// } | |||
142 | /// // Couldn't handle this error instance. Pass it up the stack. | |||
143 | /// return Error(std::move(M)); | |||
144 | /// ); | |||
145 | /// // Note - we must check or return NewE in case any of the handlers | |||
146 | /// // returned a new error. | |||
147 | /// @endcode | |||
148 | /// | |||
149 | /// The handleAllErrors function is identical to handleErrors, except | |||
150 | /// that it has a void return type, and requires all errors to be handled and | |||
151 | /// no new errors be returned. It prevents errors (assuming they can all be | |||
152 | /// handled) from having to be bubbled all the way to the top-level. | |||
153 | /// | |||
154 | /// *All* Error instances must be checked before destruction, even if | |||
155 | /// they're moved-assigned or constructed from Success values that have already | |||
156 | /// been checked. This enforces checking through all levels of the call stack. | |||
157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { | |||
158 | // Both ErrorList and FileError need to be able to yank ErrorInfoBase | |||
159 | // pointers out of this class to add to the error list. | |||
160 | friend class ErrorList; | |||
161 | friend class FileError; | |||
162 | ||||
163 | // handleErrors needs to be able to set the Checked flag. | |||
164 | template <typename... HandlerTs> | |||
165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); | |||
166 | ||||
167 | // Expected<T> needs to be able to steal the payload when constructed from an | |||
168 | // error. | |||
169 | template <typename T> friend class Expected; | |||
170 | ||||
171 | // wrap needs to be able to steal the payload. | |||
172 | friend LLVMErrorRef wrap(Error); | |||
173 | ||||
174 | protected: | |||
175 | /// Create a success value. Prefer using 'Error::success()' for readability | |||
176 | Error() { | |||
177 | setPtr(nullptr); | |||
178 | setChecked(false); | |||
179 | } | |||
180 | ||||
181 | public: | |||
182 | /// Create a success value. | |||
183 | static ErrorSuccess success(); | |||
184 | ||||
185 | // Errors are not copy-constructable. | |||
186 | Error(const Error &Other) = delete; | |||
187 | ||||
188 | /// Move-construct an error value. The newly constructed error is considered | |||
189 | /// unchecked, even if the source error had been checked. The original error | |||
190 | /// becomes a checked Success value, regardless of its original state. | |||
191 | Error(Error &&Other) { | |||
192 | setChecked(true); | |||
193 | *this = std::move(Other); | |||
194 | } | |||
195 | ||||
196 | /// Create an error value. Prefer using the 'make_error' function, but | |||
197 | /// this constructor can be useful when "re-throwing" errors from handlers. | |||
198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { | |||
199 | setPtr(Payload.release()); | |||
200 | setChecked(false); | |||
| ||||
201 | } | |||
202 | ||||
203 | // Errors are not copy-assignable. | |||
204 | Error &operator=(const Error &Other) = delete; | |||
205 | ||||
206 | /// Move-assign an error value. The current error must represent success, you | |||
207 | /// you cannot overwrite an unhandled error. The current error is then | |||
208 | /// considered unchecked. The source error becomes a checked success value, | |||
209 | /// regardless of its original state. | |||
210 | Error &operator=(Error &&Other) { | |||
211 | // Don't allow overwriting of unchecked values. | |||
212 | assertIsChecked(); | |||
213 | setPtr(Other.getPtr()); | |||
214 | ||||
215 | // This Error is unchecked, even if the source error was checked. | |||
216 | setChecked(false); | |||
217 | ||||
218 | // Null out Other's payload and set its checked bit. | |||
219 | Other.setPtr(nullptr); | |||
220 | Other.setChecked(true); | |||
221 | ||||
222 | return *this; | |||
223 | } | |||
224 | ||||
225 | /// Destroy a Error. Fails with a call to abort() if the error is | |||
226 | /// unchecked. | |||
227 | ~Error() { | |||
228 | assertIsChecked(); | |||
229 | delete getPtr(); | |||
230 | } | |||
231 | ||||
232 | /// Bool conversion. Returns true if this Error is in a failure state, | |||
233 | /// and false if it is in an accept state. If the error is in a Success state | |||
234 | /// it will be considered checked. | |||
235 | explicit operator bool() { | |||
236 | setChecked(getPtr() == nullptr); | |||
237 | return getPtr() != nullptr; | |||
238 | } | |||
239 | ||||
240 | /// Check whether one error is a subclass of another. | |||
241 | template <typename ErrT> bool isA() const { | |||
242 | return getPtr() && getPtr()->isA(ErrT::classID()); | |||
243 | } | |||
244 | ||||
245 | /// Returns the dynamic class id of this error, or null if this is a success | |||
246 | /// value. | |||
247 | const void* dynamicClassID() const { | |||
248 | if (!getPtr()) | |||
249 | return nullptr; | |||
250 | return getPtr()->dynamicClassID(); | |||
251 | } | |||
252 | ||||
253 | private: | |||
254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
255 | // assertIsChecked() happens very frequently, but under normal circumstances | |||
256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch | |||
257 | // of debug prints can cause the function to be too large for inlining. So | |||
258 | // it's important that we define this function out of line so that it can't be | |||
259 | // inlined. | |||
260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) | |||
261 | void fatalUncheckedError() const; | |||
262 | #endif | |||
263 | ||||
264 | void assertIsChecked() { | |||
265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) | |||
267 | fatalUncheckedError(); | |||
268 | #endif | |||
269 | } | |||
270 | ||||
271 | ErrorInfoBase *getPtr() const { | |||
272 | return reinterpret_cast<ErrorInfoBase*>( | |||
273 | reinterpret_cast<uintptr_t>(Payload) & | |||
274 | ~static_cast<uintptr_t>(0x1)); | |||
275 | } | |||
276 | ||||
277 | void setPtr(ErrorInfoBase *EI) { | |||
278 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
279 | Payload = reinterpret_cast<ErrorInfoBase*>( | |||
280 | (reinterpret_cast<uintptr_t>(EI) & | |||
281 | ~static_cast<uintptr_t>(0x1)) | | |||
282 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); | |||
283 | #else | |||
284 | Payload = EI; | |||
285 | #endif | |||
286 | } | |||
287 | ||||
288 | bool getChecked() const { | |||
289 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
290 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; | |||
291 | #else | |||
292 | return true; | |||
293 | #endif | |||
294 | } | |||
295 | ||||
296 | void setChecked(bool V) { | |||
297 | Payload = reinterpret_cast<ErrorInfoBase*>( | |||
298 | (reinterpret_cast<uintptr_t>(Payload) & | |||
299 | ~static_cast<uintptr_t>(0x1)) | | |||
300 | (V ? 0 : 1)); | |||
301 | } | |||
302 | ||||
303 | std::unique_ptr<ErrorInfoBase> takePayload() { | |||
304 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); | |||
305 | setPtr(nullptr); | |||
306 | setChecked(true); | |||
307 | return Tmp; | |||
308 | } | |||
309 | ||||
310 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { | |||
311 | if (auto P = E.getPtr()) | |||
312 | P->log(OS); | |||
313 | else | |||
314 | OS << "success"; | |||
315 | return OS; | |||
316 | } | |||
317 | ||||
318 | ErrorInfoBase *Payload = nullptr; | |||
319 | }; | |||
320 | ||||
321 | /// Subclass of Error for the sole purpose of identifying the success path in | |||
322 | /// the type system. This allows to catch invalid conversion to Expected<T> at | |||
323 | /// compile time. | |||
324 | class ErrorSuccess final : public Error {}; | |||
325 | ||||
326 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } | |||
327 | ||||
328 | /// Make a Error instance representing failure using the given error info | |||
329 | /// type. | |||
330 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { | |||
331 | return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); | |||
332 | } | |||
333 | ||||
334 | /// Base class for user error types. Users should declare their error types | |||
335 | /// like: | |||
336 | /// | |||
337 | /// class MyError : public ErrorInfo<MyError> { | |||
338 | /// .... | |||
339 | /// }; | |||
340 | /// | |||
341 | /// This class provides an implementation of the ErrorInfoBase::kind | |||
342 | /// method, which is used by the Error RTTI system. | |||
343 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> | |||
344 | class ErrorInfo : public ParentErrT { | |||
345 | public: | |||
346 | using ParentErrT::ParentErrT; // inherit constructors | |||
347 | ||||
348 | static const void *classID() { return &ThisErrT::ID; } | |||
349 | ||||
350 | const void *dynamicClassID() const override { return &ThisErrT::ID; } | |||
351 | ||||
352 | bool isA(const void *const ClassID) const override { | |||
353 | return ClassID == classID() || ParentErrT::isA(ClassID); | |||
354 | } | |||
355 | }; | |||
356 | ||||
357 | /// Special ErrorInfo subclass representing a list of ErrorInfos. | |||
358 | /// Instances of this class are constructed by joinError. | |||
359 | class ErrorList final : public ErrorInfo<ErrorList> { | |||
360 | // handleErrors needs to be able to iterate the payload list of an | |||
361 | // ErrorList. | |||
362 | template <typename... HandlerTs> | |||
363 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); | |||
364 | ||||
365 | // joinErrors is implemented in terms of join. | |||
366 | friend Error joinErrors(Error, Error); | |||
367 | ||||
368 | public: | |||
369 | void log(raw_ostream &OS) const override { | |||
370 | OS << "Multiple errors:\n"; | |||
371 | for (auto &ErrPayload : Payloads) { | |||
372 | ErrPayload->log(OS); | |||
373 | OS << "\n"; | |||
374 | } | |||
375 | } | |||
376 | ||||
377 | std::error_code convertToErrorCode() const override; | |||
378 | ||||
379 | // Used by ErrorInfo::classID. | |||
380 | static char ID; | |||
381 | ||||
382 | private: | |||
383 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, | |||
384 | std::unique_ptr<ErrorInfoBase> Payload2) { | |||
385 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 386, __PRETTY_FUNCTION__)) | |||
386 | "ErrorList constructor payloads should be singleton errors")((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 386, __PRETTY_FUNCTION__)); | |||
387 | Payloads.push_back(std::move(Payload1)); | |||
388 | Payloads.push_back(std::move(Payload2)); | |||
389 | } | |||
390 | ||||
391 | static Error join(Error E1, Error E2) { | |||
392 | if (!E1) | |||
393 | return E2; | |||
394 | if (!E2) | |||
395 | return E1; | |||
396 | if (E1.isA<ErrorList>()) { | |||
397 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); | |||
398 | if (E2.isA<ErrorList>()) { | |||
399 | auto E2Payload = E2.takePayload(); | |||
400 | auto &E2List = static_cast<ErrorList &>(*E2Payload); | |||
401 | for (auto &Payload : E2List.Payloads) | |||
402 | E1List.Payloads.push_back(std::move(Payload)); | |||
403 | } else | |||
404 | E1List.Payloads.push_back(E2.takePayload()); | |||
405 | ||||
406 | return E1; | |||
407 | } | |||
408 | if (E2.isA<ErrorList>()) { | |||
409 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); | |||
410 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); | |||
411 | return E2; | |||
412 | } | |||
413 | return Error(std::unique_ptr<ErrorList>( | |||
414 | new ErrorList(E1.takePayload(), E2.takePayload()))); | |||
415 | } | |||
416 | ||||
417 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; | |||
418 | }; | |||
419 | ||||
420 | /// Concatenate errors. The resulting Error is unchecked, and contains the | |||
421 | /// ErrorInfo(s), if any, contained in E1, followed by the | |||
422 | /// ErrorInfo(s), if any, contained in E2. | |||
423 | inline Error joinErrors(Error E1, Error E2) { | |||
424 | return ErrorList::join(std::move(E1), std::move(E2)); | |||
425 | } | |||
426 | ||||
427 | /// Tagged union holding either a T or a Error. | |||
428 | /// | |||
429 | /// This class parallels ErrorOr, but replaces error_code with Error. Since | |||
430 | /// Error cannot be copied, this class replaces getError() with | |||
431 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the | |||
432 | /// error class type. | |||
433 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { | |||
434 | template <class T1> friend class ExpectedAsOutParameter; | |||
435 | template <class OtherT> friend class Expected; | |||
436 | ||||
437 | static const bool isRef = std::is_reference<T>::value; | |||
438 | ||||
439 | using wrap = std::reference_wrapper<typename std::remove_reference<T>::type>; | |||
440 | ||||
441 | using error_type = std::unique_ptr<ErrorInfoBase>; | |||
442 | ||||
443 | public: | |||
444 | using storage_type = typename std::conditional<isRef, wrap, T>::type; | |||
445 | using value_type = T; | |||
446 | ||||
447 | private: | |||
448 | using reference = typename std::remove_reference<T>::type &; | |||
449 | using const_reference = const typename std::remove_reference<T>::type &; | |||
450 | using pointer = typename std::remove_reference<T>::type *; | |||
451 | using const_pointer = const typename std::remove_reference<T>::type *; | |||
452 | ||||
453 | public: | |||
454 | /// Create an Expected<T> error value from the given Error. | |||
455 | Expected(Error Err) | |||
456 | : HasError(true) | |||
457 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
458 | // Expected is unchecked upon construction in Debug builds. | |||
459 | , Unchecked(true) | |||
460 | #endif | |||
461 | { | |||
462 | assert(Err && "Cannot create Expected<T> from Error success value.")((Err && "Cannot create Expected<T> from Error success value." ) ? static_cast<void> (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 462, __PRETTY_FUNCTION__)); | |||
463 | new (getErrorStorage()) error_type(Err.takePayload()); | |||
464 | } | |||
465 | ||||
466 | /// Forbid to convert from Error::success() implicitly, this avoids having | |||
467 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise | |||
468 | /// but triggers the assertion above. | |||
469 | Expected(ErrorSuccess) = delete; | |||
470 | ||||
471 | /// Create an Expected<T> success value from the given OtherT value, which | |||
472 | /// must be convertible to T. | |||
473 | template <typename OtherT> | |||
474 | Expected(OtherT &&Val, | |||
475 | typename std::enable_if<std::is_convertible<OtherT, T>::value>::type | |||
476 | * = nullptr) | |||
477 | : HasError(false) | |||
478 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
479 | // Expected is unchecked upon construction in Debug builds. | |||
480 | , Unchecked(true) | |||
481 | #endif | |||
482 | { | |||
483 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); | |||
484 | } | |||
485 | ||||
486 | /// Move construct an Expected<T> value. | |||
487 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } | |||
488 | ||||
489 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT | |||
490 | /// must be convertible to T. | |||
491 | template <class OtherT> | |||
492 | Expected(Expected<OtherT> &&Other, | |||
493 | typename std::enable_if<std::is_convertible<OtherT, T>::value>::type | |||
494 | * = nullptr) { | |||
495 | moveConstruct(std::move(Other)); | |||
496 | } | |||
497 | ||||
498 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT | |||
499 | /// isn't convertible to T. | |||
500 | template <class OtherT> | |||
501 | explicit Expected( | |||
502 | Expected<OtherT> &&Other, | |||
503 | typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * = | |||
504 | nullptr) { | |||
505 | moveConstruct(std::move(Other)); | |||
506 | } | |||
507 | ||||
508 | /// Move-assign from another Expected<T>. | |||
509 | Expected &operator=(Expected &&Other) { | |||
510 | moveAssign(std::move(Other)); | |||
511 | return *this; | |||
512 | } | |||
513 | ||||
514 | /// Destroy an Expected<T>. | |||
515 | ~Expected() { | |||
516 | assertIsChecked(); | |||
517 | if (!HasError) | |||
518 | getStorage()->~storage_type(); | |||
519 | else | |||
520 | getErrorStorage()->~error_type(); | |||
521 | } | |||
522 | ||||
523 | /// Return false if there is an error. | |||
524 | explicit operator bool() { | |||
525 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
526 | Unchecked = HasError; | |||
527 | #endif | |||
528 | return !HasError; | |||
529 | } | |||
530 | ||||
531 | /// Returns a reference to the stored T value. | |||
532 | reference get() { | |||
533 | assertIsChecked(); | |||
534 | return *getStorage(); | |||
535 | } | |||
536 | ||||
537 | /// Returns a const reference to the stored T value. | |||
538 | const_reference get() const { | |||
539 | assertIsChecked(); | |||
540 | return const_cast<Expected<T> *>(this)->get(); | |||
541 | } | |||
542 | ||||
543 | /// Check that this Expected<T> is an error of type ErrT. | |||
544 | template <typename ErrT> bool errorIsA() const { | |||
545 | return HasError && (*getErrorStorage())->template isA<ErrT>(); | |||
546 | } | |||
547 | ||||
548 | /// Take ownership of the stored error. | |||
549 | /// After calling this the Expected<T> is in an indeterminate state that can | |||
550 | /// only be safely destructed. No further calls (beside the destructor) should | |||
551 | /// be made on the Expected<T> vaule. | |||
552 | Error takeError() { | |||
553 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
554 | Unchecked = false; | |||
555 | #endif | |||
556 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); | |||
557 | } | |||
558 | ||||
559 | /// Returns a pointer to the stored T value. | |||
560 | pointer operator->() { | |||
561 | assertIsChecked(); | |||
562 | return toPointer(getStorage()); | |||
563 | } | |||
564 | ||||
565 | /// Returns a const pointer to the stored T value. | |||
566 | const_pointer operator->() const { | |||
567 | assertIsChecked(); | |||
568 | return toPointer(getStorage()); | |||
569 | } | |||
570 | ||||
571 | /// Returns a reference to the stored T value. | |||
572 | reference operator*() { | |||
573 | assertIsChecked(); | |||
574 | return *getStorage(); | |||
575 | } | |||
576 | ||||
577 | /// Returns a const reference to the stored T value. | |||
578 | const_reference operator*() const { | |||
579 | assertIsChecked(); | |||
580 | return *getStorage(); | |||
581 | } | |||
582 | ||||
583 | private: | |||
584 | template <class T1> | |||
585 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { | |||
586 | return &a == &b; | |||
587 | } | |||
588 | ||||
589 | template <class T1, class T2> | |||
590 | static bool compareThisIfSameType(const T1 &a, const T2 &b) { | |||
591 | return false; | |||
592 | } | |||
593 | ||||
594 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { | |||
595 | HasError = Other.HasError; | |||
596 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
597 | Unchecked = true; | |||
598 | Other.Unchecked = false; | |||
599 | #endif | |||
600 | ||||
601 | if (!HasError) | |||
602 | new (getStorage()) storage_type(std::move(*Other.getStorage())); | |||
603 | else | |||
604 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); | |||
605 | } | |||
606 | ||||
607 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { | |||
608 | assertIsChecked(); | |||
609 | ||||
610 | if (compareThisIfSameType(*this, Other)) | |||
611 | return; | |||
612 | ||||
613 | this->~Expected(); | |||
614 | new (this) Expected(std::move(Other)); | |||
615 | } | |||
616 | ||||
617 | pointer toPointer(pointer Val) { return Val; } | |||
618 | ||||
619 | const_pointer toPointer(const_pointer Val) const { return Val; } | |||
620 | ||||
621 | pointer toPointer(wrap *Val) { return &Val->get(); } | |||
622 | ||||
623 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } | |||
624 | ||||
625 | storage_type *getStorage() { | |||
626 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 626, __PRETTY_FUNCTION__)); | |||
627 | return reinterpret_cast<storage_type *>(TStorage.buffer); | |||
628 | } | |||
629 | ||||
630 | const storage_type *getStorage() const { | |||
631 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 631, __PRETTY_FUNCTION__)); | |||
632 | return reinterpret_cast<const storage_type *>(TStorage.buffer); | |||
633 | } | |||
634 | ||||
635 | error_type *getErrorStorage() { | |||
636 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 636, __PRETTY_FUNCTION__)); | |||
637 | return reinterpret_cast<error_type *>(ErrorStorage.buffer); | |||
638 | } | |||
639 | ||||
640 | const error_type *getErrorStorage() const { | |||
641 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 641, __PRETTY_FUNCTION__)); | |||
642 | return reinterpret_cast<const error_type *>(ErrorStorage.buffer); | |||
643 | } | |||
644 | ||||
645 | // Used by ExpectedAsOutParameter to reset the checked flag. | |||
646 | void setUnchecked() { | |||
647 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
648 | Unchecked = true; | |||
649 | #endif | |||
650 | } | |||
651 | ||||
652 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
653 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) | |||
654 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) | |||
655 | void fatalUncheckedExpected() const { | |||
656 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; | |||
657 | if (HasError) { | |||
658 | dbgs() << "Unchecked Expected<T> contained error:\n"; | |||
659 | (*getErrorStorage())->log(dbgs()); | |||
660 | } else | |||
661 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " | |||
662 | "values in success mode must still be checked prior to being " | |||
663 | "destroyed).\n"; | |||
664 | abort(); | |||
665 | } | |||
666 | #endif | |||
667 | ||||
668 | void assertIsChecked() { | |||
669 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
670 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) | |||
671 | fatalUncheckedExpected(); | |||
672 | #endif | |||
673 | } | |||
674 | ||||
675 | union { | |||
676 | AlignedCharArrayUnion<storage_type> TStorage; | |||
677 | AlignedCharArrayUnion<error_type> ErrorStorage; | |||
678 | }; | |||
679 | bool HasError : 1; | |||
680 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
681 | bool Unchecked : 1; | |||
682 | #endif | |||
683 | }; | |||
684 | ||||
685 | /// Report a serious error, calling any installed error handler. See | |||
686 | /// ErrorHandling.h. | |||
687 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, | |||
688 | bool gen_crash_diag = true); | |||
689 | ||||
690 | /// Report a fatal error if Err is a failure value. | |||
691 | /// | |||
692 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
693 | /// is known that the Error will always be a success value. E.g. | |||
694 | /// | |||
695 | /// @code{.cpp} | |||
696 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
697 | /// // true. If DoFallibleOperation is false then foo always returns | |||
698 | /// // Error::success(). | |||
699 | /// Error foo(bool DoFallibleOperation); | |||
700 | /// | |||
701 | /// cantFail(foo(false)); | |||
702 | /// @endcode | |||
703 | inline void cantFail(Error Err, const char *Msg = nullptr) { | |||
704 | if (Err) { | |||
705 | if (!Msg) | |||
706 | Msg = "Failure value returned from cantFail wrapped call"; | |||
707 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 707); | |||
708 | } | |||
709 | } | |||
710 | ||||
711 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and | |||
712 | /// returns the contained value. | |||
713 | /// | |||
714 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
715 | /// is known that the Error will always be a success value. E.g. | |||
716 | /// | |||
717 | /// @code{.cpp} | |||
718 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
719 | /// // true. If DoFallibleOperation is false then foo always returns an int. | |||
720 | /// Expected<int> foo(bool DoFallibleOperation); | |||
721 | /// | |||
722 | /// int X = cantFail(foo(false)); | |||
723 | /// @endcode | |||
724 | template <typename T> | |||
725 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { | |||
726 | if (ValOrErr) | |||
727 | return std::move(*ValOrErr); | |||
728 | else { | |||
729 | if (!Msg) | |||
730 | Msg = "Failure value returned from cantFail wrapped call"; | |||
731 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 731); | |||
732 | } | |||
733 | } | |||
734 | ||||
735 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and | |||
736 | /// returns the contained reference. | |||
737 | /// | |||
738 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
739 | /// is known that the Error will always be a success value. E.g. | |||
740 | /// | |||
741 | /// @code{.cpp} | |||
742 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
743 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. | |||
744 | /// Expected<Bar&> foo(bool DoFallibleOperation); | |||
745 | /// | |||
746 | /// Bar &X = cantFail(foo(false)); | |||
747 | /// @endcode | |||
748 | template <typename T> | |||
749 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { | |||
750 | if (ValOrErr) | |||
751 | return *ValOrErr; | |||
752 | else { | |||
753 | if (!Msg) | |||
754 | Msg = "Failure value returned from cantFail wrapped call"; | |||
755 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 755); | |||
756 | } | |||
757 | } | |||
758 | ||||
759 | /// Helper for testing applicability of, and applying, handlers for | |||
760 | /// ErrorInfo types. | |||
761 | template <typename HandlerT> | |||
762 | class ErrorHandlerTraits | |||
763 | : public ErrorHandlerTraits<decltype( | |||
764 | &std::remove_reference<HandlerT>::type::operator())> {}; | |||
765 | ||||
766 | // Specialization functions of the form 'Error (const ErrT&)'. | |||
767 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { | |||
768 | public: | |||
769 | static bool appliesTo(const ErrorInfoBase &E) { | |||
770 | return E.template isA<ErrT>(); | |||
771 | } | |||
772 | ||||
773 | template <typename HandlerT> | |||
774 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
775 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 775, __PRETTY_FUNCTION__)); | |||
776 | return H(static_cast<ErrT &>(*E)); | |||
777 | } | |||
778 | }; | |||
779 | ||||
780 | // Specialization functions of the form 'void (const ErrT&)'. | |||
781 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { | |||
782 | public: | |||
783 | static bool appliesTo(const ErrorInfoBase &E) { | |||
784 | return E.template isA<ErrT>(); | |||
785 | } | |||
786 | ||||
787 | template <typename HandlerT> | |||
788 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
789 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 789, __PRETTY_FUNCTION__)); | |||
790 | H(static_cast<ErrT &>(*E)); | |||
791 | return Error::success(); | |||
792 | } | |||
793 | }; | |||
794 | ||||
795 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. | |||
796 | template <typename ErrT> | |||
797 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { | |||
798 | public: | |||
799 | static bool appliesTo(const ErrorInfoBase &E) { | |||
800 | return E.template isA<ErrT>(); | |||
801 | } | |||
802 | ||||
803 | template <typename HandlerT> | |||
804 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
805 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 805, __PRETTY_FUNCTION__)); | |||
806 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); | |||
807 | return H(std::move(SubE)); | |||
808 | } | |||
809 | }; | |||
810 | ||||
811 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. | |||
812 | template <typename ErrT> | |||
813 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { | |||
814 | public: | |||
815 | static bool appliesTo(const ErrorInfoBase &E) { | |||
816 | return E.template isA<ErrT>(); | |||
817 | } | |||
818 | ||||
819 | template <typename HandlerT> | |||
820 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
821 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 821, __PRETTY_FUNCTION__)); | |||
822 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); | |||
823 | H(std::move(SubE)); | |||
824 | return Error::success(); | |||
825 | } | |||
826 | }; | |||
827 | ||||
828 | // Specialization for member functions of the form 'RetT (const ErrT&)'. | |||
829 | template <typename C, typename RetT, typename ErrT> | |||
830 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> | |||
831 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
832 | ||||
833 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. | |||
834 | template <typename C, typename RetT, typename ErrT> | |||
835 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> | |||
836 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
837 | ||||
838 | // Specialization for member functions of the form 'RetT (const ErrT&)'. | |||
839 | template <typename C, typename RetT, typename ErrT> | |||
840 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> | |||
841 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
842 | ||||
843 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. | |||
844 | template <typename C, typename RetT, typename ErrT> | |||
845 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> | |||
846 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
847 | ||||
848 | /// Specialization for member functions of the form | |||
849 | /// 'RetT (std::unique_ptr<ErrT>)'. | |||
850 | template <typename C, typename RetT, typename ErrT> | |||
851 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> | |||
852 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; | |||
853 | ||||
854 | /// Specialization for member functions of the form | |||
855 | /// 'RetT (std::unique_ptr<ErrT>) const'. | |||
856 | template <typename C, typename RetT, typename ErrT> | |||
857 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> | |||
858 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; | |||
859 | ||||
860 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { | |||
861 | return Error(std::move(Payload)); | |||
862 | } | |||
863 | ||||
864 | template <typename HandlerT, typename... HandlerTs> | |||
865 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, | |||
866 | HandlerT &&Handler, HandlerTs &&... Handlers) { | |||
867 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) | |||
868 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), | |||
869 | std::move(Payload)); | |||
870 | return handleErrorImpl(std::move(Payload), | |||
871 | std::forward<HandlerTs>(Handlers)...); | |||
872 | } | |||
873 | ||||
874 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any | |||
875 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and | |||
876 | /// returned. | |||
877 | /// Because this function returns an error, its result must also be checked | |||
878 | /// or returned. If you intend to handle all errors use handleAllErrors | |||
879 | /// (which returns void, and will abort() on unhandled errors) instead. | |||
880 | template <typename... HandlerTs> | |||
881 | Error handleErrors(Error E, HandlerTs &&... Hs) { | |||
882 | if (!E) | |||
883 | return Error::success(); | |||
884 | ||||
885 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); | |||
886 | ||||
887 | if (Payload->isA<ErrorList>()) { | |||
888 | ErrorList &List = static_cast<ErrorList &>(*Payload); | |||
889 | Error R; | |||
890 | for (auto &P : List.Payloads) | |||
891 | R = ErrorList::join( | |||
892 | std::move(R), | |||
893 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); | |||
894 | return R; | |||
895 | } | |||
896 | ||||
897 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); | |||
898 | } | |||
899 | ||||
900 | /// Behaves the same as handleErrors, except that by contract all errors | |||
901 | /// *must* be handled by the given handlers (i.e. there must be no remaining | |||
902 | /// errors after running the handlers, or llvm_unreachable is called). | |||
903 | template <typename... HandlerTs> | |||
904 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { | |||
905 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); | |||
906 | } | |||
907 | ||||
908 | /// Check that E is a non-error, then drop it. | |||
909 | /// If E is an error, llvm_unreachable will be called. | |||
910 | inline void handleAllErrors(Error E) { | |||
911 | cantFail(std::move(E)); | |||
912 | } | |||
913 | ||||
914 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. | |||
915 | /// | |||
916 | /// If the incoming value is a success value it is returned unmodified. If it | |||
917 | /// is a failure value then it the contained error is passed to handleErrors. | |||
918 | /// If handleErrors is able to handle the error then the RecoveryPath functor | |||
919 | /// is called to supply the final result. If handleErrors is not able to | |||
920 | /// handle all errors then the unhandled errors are returned. | |||
921 | /// | |||
922 | /// This utility enables the follow pattern: | |||
923 | /// | |||
924 | /// @code{.cpp} | |||
925 | /// enum FooStrategy { Aggressive, Conservative }; | |||
926 | /// Expected<Foo> foo(FooStrategy S); | |||
927 | /// | |||
928 | /// auto ResultOrErr = | |||
929 | /// handleExpected( | |||
930 | /// foo(Aggressive), | |||
931 | /// []() { return foo(Conservative); }, | |||
932 | /// [](AggressiveStrategyError&) { | |||
933 | /// // Implicitly conusme this - we'll recover by using a conservative | |||
934 | /// // strategy. | |||
935 | /// }); | |||
936 | /// | |||
937 | /// @endcode | |||
938 | template <typename T, typename RecoveryFtor, typename... HandlerTs> | |||
939 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, | |||
940 | HandlerTs &&... Handlers) { | |||
941 | if (ValOrErr) | |||
942 | return ValOrErr; | |||
943 | ||||
944 | if (auto Err = handleErrors(ValOrErr.takeError(), | |||
945 | std::forward<HandlerTs>(Handlers)...)) | |||
946 | return std::move(Err); | |||
947 | ||||
948 | return RecoveryPath(); | |||
949 | } | |||
950 | ||||
951 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner | |||
952 | /// will be printed before the first one is logged. A newline will be printed | |||
953 | /// after each error. | |||
954 | /// | |||
955 | /// This function is compatible with the helpers from Support/WithColor.h. You | |||
956 | /// can pass any of them as the OS. Please consider using them instead of | |||
957 | /// including 'error: ' in the ErrorBanner. | |||
958 | /// | |||
959 | /// This is useful in the base level of your program to allow clean termination | |||
960 | /// (allowing clean deallocation of resources, etc.), while reporting error | |||
961 | /// information to the user. | |||
962 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); | |||
963 | ||||
964 | /// Write all error messages (if any) in E to a string. The newline character | |||
965 | /// is used to separate error messages. | |||
966 | inline std::string toString(Error E) { | |||
967 | SmallVector<std::string, 2> Errors; | |||
968 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { | |||
969 | Errors.push_back(EI.message()); | |||
970 | }); | |||
971 | return join(Errors.begin(), Errors.end(), "\n"); | |||
972 | } | |||
973 | ||||
974 | /// Consume a Error without doing anything. This method should be used | |||
975 | /// only where an error can be considered a reasonable and expected return | |||
976 | /// value. | |||
977 | /// | |||
978 | /// Uses of this method are potentially indicative of design problems: If it's | |||
979 | /// legitimate to do nothing while processing an "error", the error-producer | |||
980 | /// might be more clearly refactored to return an Optional<T>. | |||
981 | inline void consumeError(Error Err) { | |||
982 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); | |||
983 | } | |||
984 | ||||
985 | /// Helper for converting an Error to a bool. | |||
986 | /// | |||
987 | /// This method returns true if Err is in an error state, or false if it is | |||
988 | /// in a success state. Puts Err in a checked state in both cases (unlike | |||
989 | /// Error::operator bool(), which only does this for success states). | |||
990 | inline bool errorToBool(Error Err) { | |||
991 | bool IsError = static_cast<bool>(Err); | |||
992 | if (IsError) | |||
993 | consumeError(std::move(Err)); | |||
994 | return IsError; | |||
995 | } | |||
996 | ||||
997 | /// Helper for Errors used as out-parameters. | |||
998 | /// | |||
999 | /// This helper is for use with the Error-as-out-parameter idiom, where an error | |||
1000 | /// is passed to a function or method by reference, rather than being returned. | |||
1001 | /// In such cases it is helpful to set the checked bit on entry to the function | |||
1002 | /// so that the error can be written to (unchecked Errors abort on assignment) | |||
1003 | /// and clear the checked bit on exit so that clients cannot accidentally forget | |||
1004 | /// to check the result. This helper performs these actions automatically using | |||
1005 | /// RAII: | |||
1006 | /// | |||
1007 | /// @code{.cpp} | |||
1008 | /// Result foo(Error &Err) { | |||
1009 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set | |||
1010 | /// // <body of foo> | |||
1011 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. | |||
1012 | /// } | |||
1013 | /// @endcode | |||
1014 | /// | |||
1015 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be | |||
1016 | /// used with optional Errors (Error pointers that are allowed to be null). If | |||
1017 | /// ErrorAsOutParameter took an Error reference, an instance would have to be | |||
1018 | /// created inside every condition that verified that Error was non-null. By | |||
1019 | /// taking an Error pointer we can just create one instance at the top of the | |||
1020 | /// function. | |||
1021 | class ErrorAsOutParameter { | |||
1022 | public: | |||
1023 | ErrorAsOutParameter(Error *Err) : Err(Err) { | |||
1024 | // Raise the checked bit if Err is success. | |||
1025 | if (Err) | |||
1026 | (void)!!*Err; | |||
1027 | } | |||
1028 | ||||
1029 | ~ErrorAsOutParameter() { | |||
1030 | // Clear the checked bit. | |||
1031 | if (Err && !*Err) | |||
1032 | *Err = Error::success(); | |||
1033 | } | |||
1034 | ||||
1035 | private: | |||
1036 | Error *Err; | |||
1037 | }; | |||
1038 | ||||
1039 | /// Helper for Expected<T>s used as out-parameters. | |||
1040 | /// | |||
1041 | /// See ErrorAsOutParameter. | |||
1042 | template <typename T> | |||
1043 | class ExpectedAsOutParameter { | |||
1044 | public: | |||
1045 | ExpectedAsOutParameter(Expected<T> *ValOrErr) | |||
1046 | : ValOrErr(ValOrErr) { | |||
1047 | if (ValOrErr) | |||
1048 | (void)!!*ValOrErr; | |||
1049 | } | |||
1050 | ||||
1051 | ~ExpectedAsOutParameter() { | |||
1052 | if (ValOrErr) | |||
1053 | ValOrErr->setUnchecked(); | |||
1054 | } | |||
1055 | ||||
1056 | private: | |||
1057 | Expected<T> *ValOrErr; | |||
1058 | }; | |||
1059 | ||||
1060 | /// This class wraps a std::error_code in a Error. | |||
1061 | /// | |||
1062 | /// This is useful if you're writing an interface that returns a Error | |||
1063 | /// (or Expected) and you want to call code that still returns | |||
1064 | /// std::error_codes. | |||
1065 | class ECError : public ErrorInfo<ECError> { | |||
1066 | friend Error errorCodeToError(std::error_code); | |||
1067 | ||||
1068 | virtual void anchor() override; | |||
1069 | ||||
1070 | public: | |||
1071 | void setErrorCode(std::error_code EC) { this->EC = EC; } | |||
1072 | std::error_code convertToErrorCode() const override { return EC; } | |||
1073 | void log(raw_ostream &OS) const override { OS << EC.message(); } | |||
1074 | ||||
1075 | // Used by ErrorInfo::classID. | |||
1076 | static char ID; | |||
1077 | ||||
1078 | protected: | |||
1079 | ECError() = default; | |||
1080 | ECError(std::error_code EC) : EC(EC) {} | |||
1081 | ||||
1082 | std::error_code EC; | |||
1083 | }; | |||
1084 | ||||
1085 | /// The value returned by this function can be returned from convertToErrorCode | |||
1086 | /// for Error values where no sensible translation to std::error_code exists. | |||
1087 | /// It should only be used in this situation, and should never be used where a | |||
1088 | /// sensible conversion to std::error_code is available, as attempts to convert | |||
1089 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic | |||
1090 | ///error to try to convert such a value). | |||
1091 | std::error_code inconvertibleErrorCode(); | |||
1092 | ||||
1093 | /// Helper for converting an std::error_code to a Error. | |||
1094 | Error errorCodeToError(std::error_code EC); | |||
1095 | ||||
1096 | /// Helper for converting an ECError to a std::error_code. | |||
1097 | /// | |||
1098 | /// This method requires that Err be Error() or an ECError, otherwise it | |||
1099 | /// will trigger a call to abort(). | |||
1100 | std::error_code errorToErrorCode(Error Err); | |||
1101 | ||||
1102 | /// Convert an ErrorOr<T> to an Expected<T>. | |||
1103 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { | |||
1104 | if (auto EC = EO.getError()) | |||
1105 | return errorCodeToError(EC); | |||
1106 | return std::move(*EO); | |||
1107 | } | |||
1108 | ||||
1109 | /// Convert an Expected<T> to an ErrorOr<T>. | |||
1110 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { | |||
1111 | if (auto Err = E.takeError()) | |||
1112 | return errorToErrorCode(std::move(Err)); | |||
1113 | return std::move(*E); | |||
1114 | } | |||
1115 | ||||
1116 | /// This class wraps a string in an Error. | |||
1117 | /// | |||
1118 | /// StringError is useful in cases where the client is not expected to be able | |||
1119 | /// to consume the specific error message programmatically (for example, if the | |||
1120 | /// error message is to be presented to the user). | |||
1121 | /// | |||
1122 | /// StringError can also be used when additional information is to be printed | |||
1123 | /// along with a error_code message. Depending on the constructor called, this | |||
1124 | /// class can either display: | |||
1125 | /// 1. the error_code message (ECError behavior) | |||
1126 | /// 2. a string | |||
1127 | /// 3. the error_code message and a string | |||
1128 | /// | |||
1129 | /// These behaviors are useful when subtyping is required; for example, when a | |||
1130 | /// specific library needs an explicit error type. In the example below, | |||
1131 | /// PDBError is derived from StringError: | |||
1132 | /// | |||
1133 | /// @code{.cpp} | |||
1134 | /// Expected<int> foo() { | |||
1135 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, | |||
1136 | /// "Additional information"); | |||
1137 | /// } | |||
1138 | /// @endcode | |||
1139 | /// | |||
1140 | class StringError : public ErrorInfo<StringError> { | |||
1141 | public: | |||
1142 | static char ID; | |||
1143 | ||||
1144 | // Prints EC + S and converts to EC | |||
1145 | StringError(std::error_code EC, const Twine &S = Twine()); | |||
1146 | ||||
1147 | // Prints S and converts to EC | |||
1148 | StringError(const Twine &S, std::error_code EC); | |||
1149 | ||||
1150 | void log(raw_ostream &OS) const override; | |||
1151 | std::error_code convertToErrorCode() const override; | |||
1152 | ||||
1153 | const std::string &getMessage() const { return Msg; } | |||
1154 | ||||
1155 | private: | |||
1156 | std::string Msg; | |||
1157 | std::error_code EC; | |||
1158 | const bool PrintMsgOnly = false; | |||
1159 | }; | |||
1160 | ||||
1161 | /// Create formatted StringError object. | |||
1162 | template <typename... Ts> | |||
1163 | Error createStringError(std::error_code EC, char const *Fmt, | |||
1164 | const Ts &... Vals) { | |||
1165 | std::string Buffer; | |||
1166 | raw_string_ostream Stream(Buffer); | |||
1167 | Stream << format(Fmt, Vals...); | |||
1168 | return make_error<StringError>(Stream.str(), EC); | |||
1169 | } | |||
1170 | ||||
1171 | Error createStringError(std::error_code EC, char const *Msg); | |||
1172 | ||||
1173 | /// This class wraps a filename and another Error. | |||
1174 | /// | |||
1175 | /// In some cases, an error needs to live along a 'source' name, in order to | |||
1176 | /// show more detailed information to the user. | |||
1177 | class FileError final : public ErrorInfo<FileError> { | |||
1178 | ||||
1179 | friend Error createFileError(const Twine &, Error); | |||
1180 | friend Error createFileError(const Twine &, size_t, Error); | |||
1181 | ||||
1182 | public: | |||
1183 | void log(raw_ostream &OS) const override { | |||
1184 | assert(Err && !FileName.empty() && "Trying to log after takeError().")((Err && !FileName.empty() && "Trying to log after takeError()." ) ? static_cast<void> (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1184, __PRETTY_FUNCTION__)); | |||
1185 | OS << "'" << FileName << "': "; | |||
1186 | if (Line.hasValue()) | |||
1187 | OS << "line " << Line.getValue() << ": "; | |||
1188 | Err->log(OS); | |||
1189 | } | |||
1190 | ||||
1191 | Error takeError() { return Error(std::move(Err)); } | |||
1192 | ||||
1193 | std::error_code convertToErrorCode() const override; | |||
1194 | ||||
1195 | // Used by ErrorInfo::classID. | |||
1196 | static char ID; | |||
1197 | ||||
1198 | private: | |||
1199 | FileError(const Twine &F, Optional<size_t> LineNum, | |||
1200 | std::unique_ptr<ErrorInfoBase> E) { | |||
1201 | assert(E && "Cannot create FileError from Error success value.")((E && "Cannot create FileError from Error success value." ) ? static_cast<void> (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1201, __PRETTY_FUNCTION__)); | |||
1202 | assert(!F.isTriviallyEmpty() &&((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1203, __PRETTY_FUNCTION__)) | |||
1203 | "The file name provided to FileError must not be empty.")((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1203, __PRETTY_FUNCTION__)); | |||
1204 | FileName = F.str(); | |||
1205 | Err = std::move(E); | |||
1206 | Line = std::move(LineNum); | |||
1207 | } | |||
1208 | ||||
1209 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { | |||
1210 | return Error( | |||
1211 | std::unique_ptr<FileError>(new FileError(F, Line, E.takePayload()))); | |||
1212 | } | |||
1213 | ||||
1214 | std::string FileName; | |||
1215 | Optional<size_t> Line; | |||
1216 | std::unique_ptr<ErrorInfoBase> Err; | |||
1217 | }; | |||
1218 | ||||
1219 | /// Concatenate a source file path and/or name with an Error. The resulting | |||
1220 | /// Error is unchecked. | |||
1221 | inline Error createFileError(const Twine &F, Error E) { | |||
1222 | return FileError::build(F, Optional<size_t>(), std::move(E)); | |||
1223 | } | |||
1224 | ||||
1225 | /// Concatenate a source file path and/or name with line number and an Error. | |||
1226 | /// The resulting Error is unchecked. | |||
1227 | inline Error createFileError(const Twine &F, size_t Line, Error E) { | |||
1228 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); | |||
1229 | } | |||
1230 | ||||
1231 | /// Concatenate a source file path and/or name with a std::error_code | |||
1232 | /// to form an Error object. | |||
1233 | inline Error createFileError(const Twine &F, std::error_code EC) { | |||
1234 | return createFileError(F, errorCodeToError(EC)); | |||
1235 | } | |||
1236 | ||||
1237 | /// Concatenate a source file path and/or name with line number and | |||
1238 | /// std::error_code to form an Error object. | |||
1239 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { | |||
1240 | return createFileError(F, Line, errorCodeToError(EC)); | |||
1241 | } | |||
1242 | ||||
1243 | Error createFileError(const Twine &F, ErrorSuccess) = delete; | |||
1244 | ||||
1245 | /// Helper for check-and-exit error handling. | |||
1246 | /// | |||
1247 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. | |||
1248 | /// | |||
1249 | class ExitOnError { | |||
1250 | public: | |||
1251 | /// Create an error on exit helper. | |||
1252 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) | |||
1253 | : Banner(std::move(Banner)), | |||
1254 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} | |||
1255 | ||||
1256 | /// Set the banner string for any errors caught by operator(). | |||
1257 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } | |||
1258 | ||||
1259 | /// Set the exit-code mapper function. | |||
1260 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { | |||
1261 | this->GetExitCode = std::move(GetExitCode); | |||
1262 | } | |||
1263 | ||||
1264 | /// Check Err. If it's in a failure state log the error(s) and exit. | |||
1265 | void operator()(Error Err) const { checkError(std::move(Err)); } | |||
1266 | ||||
1267 | /// Check E. If it's in a success state then return the contained value. If | |||
1268 | /// it's in a failure state log the error(s) and exit. | |||
1269 | template <typename T> T operator()(Expected<T> &&E) const { | |||
1270 | checkError(E.takeError()); | |||
1271 | return std::move(*E); | |||
1272 | } | |||
1273 | ||||
1274 | /// Check E. If it's in a success state then return the contained reference. If | |||
1275 | /// it's in a failure state log the error(s) and exit. | |||
1276 | template <typename T> T& operator()(Expected<T&> &&E) const { | |||
1277 | checkError(E.takeError()); | |||
1278 | return *E; | |||
1279 | } | |||
1280 | ||||
1281 | private: | |||
1282 | void checkError(Error Err) const { | |||
1283 | if (Err) { | |||
1284 | int ExitCode = GetExitCode(Err); | |||
1285 | logAllUnhandledErrors(std::move(Err), errs(), Banner); | |||
1286 | exit(ExitCode); | |||
1287 | } | |||
1288 | } | |||
1289 | ||||
1290 | std::string Banner; | |||
1291 | std::function<int(const Error &)> GetExitCode; | |||
1292 | }; | |||
1293 | ||||
1294 | /// Conversion from Error to LLVMErrorRef for C error bindings. | |||
1295 | inline LLVMErrorRef wrap(Error Err) { | |||
1296 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); | |||
1297 | } | |||
1298 | ||||
1299 | /// Conversion from LLVMErrorRef to Error for C error bindings. | |||
1300 | inline Error unwrap(LLVMErrorRef ErrRef) { | |||
1301 | return Error(std::unique_ptr<ErrorInfoBase>( | |||
1302 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); | |||
1303 | } | |||
1304 | ||||
1305 | } // end namespace llvm | |||
1306 | ||||
1307 | #endif // LLVM_SUPPORT_ERROR_H |