llvm.org GIT mirror llvm / 9416f9c
DebugIR: Delete -debug-ir git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222945 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
19 changed file(s) with 1 addition(s) and 1465 deletion(s). Raw diff Collapse all Expand all
105105 void initializeDAHPass(PassRegistry&);
106106 void initializeDCEPass(PassRegistry&);
107107 void initializeDSEPass(PassRegistry&);
108 void initializeDebugIRPass(PassRegistry&);
109108 void initializeDebugInfoVerifierLegacyPassPass(PassRegistry &);
110109 void initializeDeadInstEliminationPass(PassRegistry&);
111110 void initializeDeadMachineInstructionElimPass(PassRegistry&);
9292 // checking on loads, stores, and other memory intrinsics.
9393 FunctionPass *createBoundsCheckingPass();
9494
95 /// createDebugIRPass - Enable interactive stepping through LLVM IR in LLDB (or
96 /// GDB) and generate a file with the LLVM IR to be
97 /// displayed in the debugger.
98 ///
99 /// Existing debug metadata is preserved (but may be modified) in order to allow
100 /// accessing variables in the original source. The line table and file
101 /// information is modified to correspond to the lines in the LLVM IR. If
102 /// Filename and Directory are empty, a file name is generated based on existing
103 /// debug information. If no debug information is available, a temporary file
104 /// name is generated.
105 ///
106 /// @param HideDebugIntrinsics Omit debug intrinsics in emitted IR source file.
107 /// @param HideDebugMetadata Omit debug metadata in emitted IR source file.
108 /// @param Directory Embed this directory in the debug information.
109 /// @param Filename Embed this file name in the debug information.
110 ModulePass *createDebugIRPass(bool HideDebugIntrinsics,
111 bool HideDebugMetadata,
112 StringRef Directory = StringRef(),
113 StringRef Filename = StringRef());
114
115 /// createDebugIRPass - Enable interactive stepping through LLVM IR in LLDB
116 /// (or GDB) with an existing IR file on disk. When creating
117 /// a DebugIR pass with this function, no source file is
118 /// output to disk and the existing one is unmodified. Debug
119 /// metadata in the Module is created/updated to point to
120 /// the existing textual IR file on disk.
121 /// NOTE: If the IR file to be debugged is not on disk, use the version of this
122 /// function with parameters in order to generate the file that will be
123 /// seen by the debugger.
124 ModulePass *createDebugIRPass();
125
12695 } // End llvm namespace
12796
12897 #endif
11 AddressSanitizer.cpp
22 BoundsChecking.cpp
33 DataFlowSanitizer.cpp
4 DebugIR.cpp
54 GCOVProfiling.cpp
65 MemorySanitizer.cpp
76 Instrumentation.cpp
+0
-617
lib/Transforms/Instrumentation/DebugIR.cpp less more
None //===--- DebugIR.cpp - Transform debug metadata to allow debugging IR -----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A Module transform pass that emits a succinct version of the IR and replaces
10 // the source file metadata to allow debuggers to step through the IR.
11 //
12 // FIXME: instead of replacing debug metadata, this pass should allow for
13 // additional metadata to be used to point capable debuggers to the IR file
14 // without destroying the mapping to the original source file.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/IR/ValueMap.h"
19 #include "DebugIR.h"
20 #include "llvm/IR/AssemblyAnnotationWriter.h"
21 #include "llvm/IR/DIBuilder.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/DebugInfo.h"
24 #include "llvm/IR/InstVisitor.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/FormattedStream.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/ToolOutputFile.h"
33 #include "llvm/Transforms/Instrumentation.h"
34 #include "llvm/Transforms/Utils/Cloning.h"
35 #include
36
37 #define STR_HELPER(x) #x
38 #define STR(x) STR_HELPER(x)
39
40 using namespace llvm;
41
42 #define DEBUG_TYPE "debug-ir"
43
44 namespace {
45
46 /// Builds a map of Value* to line numbers on which the Value appears in a
47 /// textual representation of the IR by plugging into the AssemblyWriter by
48 /// masquerading as an AssemblyAnnotationWriter.
49 class ValueToLineMap : public AssemblyAnnotationWriter {
50 ValueMap Lines;
51 typedef ValueMap::const_iterator LineIter;
52
53 void addEntry(const Value *V, formatted_raw_ostream &Out) {
54 Out.flush();
55 Lines.insert(std::make_pair(V, Out.getLine() + 1));
56 }
57
58 public:
59
60 /// Prints Module to a null buffer in order to build the map of Value pointers
61 /// to line numbers.
62 ValueToLineMap(const Module *M) {
63 raw_null_ostream ThrowAway;
64 M->print(ThrowAway, this);
65 }
66
67 // This function is called after an Instruction, GlobalValue, or GlobalAlias
68 // is printed.
69 void printInfoComment(const Value &V, formatted_raw_ostream &Out) override {
70 addEntry(&V, Out);
71 }
72
73 void emitFunctionAnnot(const Function *F,
74 formatted_raw_ostream &Out) override {
75 addEntry(F, Out);
76 }
77
78 /// If V appears on a line in the textual IR representation, sets Line to the
79 /// line number and returns true, otherwise returns false.
80 bool getLine(const Value *V, unsigned int &Line) const {
81 LineIter i = Lines.find(V);
82 if (i != Lines.end()) {
83 Line = i->second;
84 return true;
85 }
86 return false;
87 }
88 };
89
90 /// Removes debug intrisncs like llvm.dbg.declare and llvm.dbg.value.
91 class DebugIntrinsicsRemover : public InstVisitor {
92 void remove(Instruction &I) { I.eraseFromParent(); }
93
94 public:
95 static void process(Module &M) {
96 DebugIntrinsicsRemover Remover;
97 Remover.visit(&M);
98 }
99 void visitDbgDeclareInst(DbgDeclareInst &I) { remove(I); }
100 void visitDbgValueInst(DbgValueInst &I) { remove(I); }
101 void visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { remove(I); }
102 };
103
104 /// Removes debug metadata (!dbg) nodes from all instructions, and optionally
105 /// metadata named "llvm.dbg.cu" if RemoveNamedInfo is true.
106 class DebugMetadataRemover : public InstVisitor {
107 bool RemoveNamedInfo;
108
109 public:
110 static void process(Module &M, bool RemoveNamedInfo = true) {
111 DebugMetadataRemover Remover(RemoveNamedInfo);
112 Remover.run(&M);
113 }
114
115 DebugMetadataRemover(bool RemoveNamedInfo)
116 : RemoveNamedInfo(RemoveNamedInfo) {}
117
118 void visitInstruction(Instruction &I) {
119 if (I.getMetadata(LLVMContext::MD_dbg))
120 I.setMetadata(LLVMContext::MD_dbg, nullptr);
121 }
122
123 void run(Module *M) {
124 // Remove debug metadata attached to instructions
125 visit(M);
126
127 if (RemoveNamedInfo) {
128 // Remove CU named metadata (and all children nodes)
129 NamedMDNode *Node = M->getNamedMetadata("llvm.dbg.cu");
130 if (Node)
131 M->eraseNamedMetadata(Node);
132 }
133 }
134 };
135
136 /// Updates debug metadata in a Module:
137 /// - changes Filename/Directory to values provided on construction
138 /// - adds/updates line number (DebugLoc) entries associated with each
139 /// instruction to reflect the instruction's location in an LLVM IR file
140 class DIUpdater : public InstVisitor {
141 /// Builder of debug information
142 DIBuilder Builder;
143
144 /// Helper for type attributes/sizes/etc
145 DataLayout Layout;
146
147 /// Map of Value* to line numbers
148 const ValueToLineMap LineTable;
149
150 /// Map of Value* (in original Module) to Value* (in optional cloned Module)
151 const ValueToValueMapTy *VMap;
152
153 /// Directory of debug metadata
154 DebugInfoFinder Finder;
155
156 /// Source filename and directory
157 StringRef Filename;
158 StringRef Directory;
159
160 // CU nodes needed when creating DI subprograms
161 MDNode *FileNode;
162 MDNode *LexicalBlockFileNode;
163 const MDNode *CUNode;
164
165 ValueMap SubprogramDescriptors;
166 DenseMap TypeDescriptors;
167
168 public:
169 DIUpdater(Module &M, StringRef Filename = StringRef(),
170 StringRef Directory = StringRef(), const Module *DisplayM = nullptr,
171 const ValueToValueMapTy *VMap = nullptr)
172 : Builder(M), Layout(&M), LineTable(DisplayM ? DisplayM : &M), VMap(VMap),
173 Finder(), Filename(Filename), Directory(Directory), FileNode(nullptr),
174 LexicalBlockFileNode(nullptr), CUNode(nullptr) {
175 Finder.processModule(M);
176 visit(&M);
177 }
178
179 ~DIUpdater() { Builder.finalize(); }
180
181 void visitModule(Module &M) {
182 if (Finder.compile_unit_count() > 1)
183 report_fatal_error("DebugIR pass supports only a signle compile unit per "
184 "Module.");
185 createCompileUnit(Finder.compile_unit_count() == 1 ?
186 (MDNode*)*Finder.compile_units().begin() : nullptr);
187 }
188
189 void visitFunction(Function &F) {
190 if (F.isDeclaration() || findDISubprogram(&F))
191 return;
192
193 StringRef MangledName = F.getName();
194 DICompositeType Sig = createFunctionSignature(&F);
195
196 // find line of function declaration
197 unsigned Line = 0;
198 if (!findLine(&F, Line)) {
199 DEBUG(dbgs() << "WARNING: No line for Function " << F.getName().str()
200 << "\n");
201 return;
202 }
203
204 Instruction *FirstInst = F.begin()->begin();
205 unsigned ScopeLine = 0;
206 if (!findLine(FirstInst, ScopeLine)) {
207 DEBUG(dbgs() << "WARNING: No line for 1st Instruction in Function "
208 << F.getName().str() << "\n");
209 return;
210 }
211
212 bool Local = F.hasInternalLinkage();
213 bool IsDefinition = !F.isDeclaration();
214 bool IsOptimized = false;
215
216 int FuncFlags = llvm::DIDescriptor::FlagPrototyped;
217 assert(CUNode && FileNode);
218 DISubprogram Sub = Builder.createFunction(
219 DICompileUnit(CUNode), F.getName(), MangledName, DIFile(FileNode), Line,
220 Sig, Local, IsDefinition, ScopeLine, FuncFlags, IsOptimized, &F);
221 assert(Sub.isSubprogram());
222 DEBUG(dbgs() << "create subprogram mdnode " << *Sub << ": "
223 << "\n");
224
225 SubprogramDescriptors.insert(std::make_pair(&F, Sub));
226 }
227
228 void visitInstruction(Instruction &I) {
229 DebugLoc Loc(I.getDebugLoc());
230
231 /// If a ValueToValueMap is provided, use it to get the real instruction as
232 /// the line table was generated on a clone of the module on which we are
233 /// operating.
234 Value *RealInst = nullptr;
235 if (VMap)
236 RealInst = VMap->lookup(&I);
237
238 if (!RealInst)
239 RealInst = &I;
240
241 unsigned Col = 0; // FIXME: support columns
242 unsigned Line;
243 if (!LineTable.getLine(RealInst, Line)) {
244 // Instruction has no line, it may have been removed (in the module that
245 // will be passed to the debugger) so there is nothing to do here.
246 DEBUG(dbgs() << "WARNING: no LineTable entry for instruction " << RealInst
247 << "\n");
248 DEBUG(RealInst->dump());
249 return;
250 }
251
252 DebugLoc NewLoc;
253 if (!Loc.isUnknown())
254 // I had a previous debug location: re-use the DebugLoc
255 NewLoc = DebugLoc::get(Line, Col, Loc.getScope(RealInst->getContext()),
256 Loc.getInlinedAt(RealInst->getContext()));
257 else if (MDNode *scope = findScope(&I))
258 NewLoc = DebugLoc::get(Line, Col, scope, nullptr);
259 else {
260 DEBUG(dbgs() << "WARNING: no valid scope for instruction " << &I
261 << ". no DebugLoc will be present."
262 << "\n");
263 return;
264 }
265
266 addDebugLocation(I, NewLoc);
267 }
268
269 private:
270
271 void createCompileUnit(MDNode *CUToReplace) {
272 std::string Flags;
273 bool IsOptimized = false;
274 StringRef Producer;
275 unsigned RuntimeVersion(0);
276 StringRef SplitName;
277
278 if (CUToReplace) {
279 // save fields from existing CU to re-use in the new CU
280 DICompileUnit ExistingCU(CUToReplace);
281 Producer = ExistingCU.getProducer();
282 IsOptimized = ExistingCU.isOptimized();
283 Flags = ExistingCU.getFlags();
284 RuntimeVersion = ExistingCU.getRunTimeVersion();
285 SplitName = ExistingCU.getSplitDebugFilename();
286 } else {
287 Producer =
288 "LLVM Version " STR(LLVM_VERSION_MAJOR) "." STR(LLVM_VERSION_MINOR);
289 }
290
291 CUNode =
292 Builder.createCompileUnit(dwarf::DW_LANG_C99, Filename, Directory,
293 Producer, IsOptimized, Flags, RuntimeVersion);
294
295 if (CUToReplace)
296 CUToReplace->replaceAllUsesWith(const_cast(CUNode));
297
298 DICompileUnit CU(CUNode);
299 FileNode = Builder.createFile(Filename, Directory);
300 LexicalBlockFileNode = Builder.createLexicalBlockFile(CU, DIFile(FileNode));
301 }
302
303 /// Returns the MDNode* that represents the DI scope to associate with I
304 MDNode *findScope(const Instruction *I) {
305 const Function *F = I->getParent()->getParent();
306 if (MDNode *ret = findDISubprogram(F))
307 return ret;
308
309 DEBUG(dbgs() << "WARNING: Using fallback lexical block file scope "
310 << LexicalBlockFileNode << " as scope for instruction " << I
311 << "\n");
312 return LexicalBlockFileNode;
313 }
314
315 /// Returns the MDNode* that is the descriptor for F
316 MDNode *findDISubprogram(const Function *F) {
317 typedef ValueMap::const_iterator FuncNodeIter;
318 FuncNodeIter i = SubprogramDescriptors.find(F);
319 if (i != SubprogramDescriptors.end())
320 return i->second;
321
322 DEBUG(dbgs() << "searching for DI scope node for Function " << F
323 << " in a list of " << Finder.subprogram_count()
324 << " subprogram nodes"
325 << "\n");
326
327 for (DISubprogram S : Finder.subprograms()) {
328 if (S.getFunction() == F) {
329 DEBUG(dbgs() << "Found DISubprogram " << S << " for function "
330 << S.getFunction() << "\n");
331 return S;
332 }
333 }
334 DEBUG(dbgs() << "unable to find DISubprogram node for function "
335 << F->getName().str() << "\n");
336 return nullptr;
337 }
338
339 /// Sets Line to the line number on which V appears and returns true. If a
340 /// line location for V is not found, returns false.
341 bool findLine(const Value *V, unsigned &Line) {
342 if (LineTable.getLine(V, Line))
343 return true;
344
345 if (VMap) {
346 Value *mapped = VMap->lookup(V);
347 if (mapped && LineTable.getLine(mapped, Line))
348 return true;
349 }
350 return false;
351 }
352
353 std::string getTypeName(Type *T) {
354 std::string TypeName;
355 raw_string_ostream TypeStream(TypeName);
356 if (T)
357 T->print(TypeStream);
358 else
359 TypeStream << "Printing Type";
360 TypeStream.flush();
361 return TypeName;
362 }
363
364 /// Returns the MDNode that represents type T if it is already created, or 0
365 /// if it is not.
366 MDNode *getType(const Type *T) {
367 typedef DenseMap::const_iterator TypeNodeIter;
368 TypeNodeIter i = TypeDescriptors.find(T);
369 if (i != TypeDescriptors.end())
370 return i->second;
371 return nullptr;
372 }
373
374 /// Returns a DebugInfo type from an LLVM type T.
375 DIDerivedType getOrCreateType(Type *T) {
376 MDNode *N = getType(T);
377 if (N)
378 return DIDerivedType(N);
379 else if (T->isVoidTy())
380 return DIDerivedType(nullptr);
381 else if (T->isStructTy()) {
382 N = Builder.createStructType(
383 DIScope(LexicalBlockFileNode), T->getStructName(), DIFile(FileNode),
384 0, Layout.getTypeSizeInBits(T), Layout.getABITypeAlignment(T), 0,
385 DIType(nullptr), DIArray(nullptr)); // filled in later
386
387 // N is added to the map (early) so that element search below can find it,
388 // so as to avoid infinite recursion for structs that contain pointers to
389 // their own type.
390 TypeDescriptors[T] = N;
391 DICompositeType StructDescriptor(N);
392
393 SmallVector Elements;
394 for (unsigned i = 0; i < T->getStructNumElements(); ++i)
395 Elements.push_back(getOrCreateType(T->getStructElementType(i)));
396
397 // set struct elements
398 StructDescriptor.setArrays(Builder.getOrCreateArray(Elements));
399 } else if (T->isPointerTy()) {
400 Type *PointeeTy = T->getPointerElementType();
401 if (!(N = getType(PointeeTy)))
402 N = Builder.createPointerType(
403 getOrCreateType(PointeeTy), Layout.getPointerTypeSizeInBits(T),
404 Layout.getPrefTypeAlignment(T), getTypeName(T));
405 } else if (T->isArrayTy()) {
406 SmallVector Subrange;
407 Subrange.push_back(
408 Builder.getOrCreateSubrange(0, T->getArrayNumElements() - 1));
409
410 N = Builder.createArrayType(Layout.getTypeSizeInBits(T),
411 Layout.getPrefTypeAlignment(T),
412 getOrCreateType(T->getArrayElementType()),
413 Builder.getOrCreateArray(Subrange));
414 } else {
415 int encoding = llvm::dwarf::DW_ATE_signed;
416 if (T->isIntegerTy())
417 encoding = llvm::dwarf::DW_ATE_unsigned;
418 else if (T->isFloatingPointTy())
419 encoding = llvm::dwarf::DW_ATE_float;
420
421 N = Builder.createBasicType(getTypeName(T), T->getPrimitiveSizeInBits(),
422 0, encoding);
423 }
424 TypeDescriptors[T] = N;
425 return DIDerivedType(N);
426 }
427
428 /// Returns a DebugInfo type that represents a function signature for Func.
429 DICompositeType createFunctionSignature(const Function *Func) {
430 SmallVector Params;
431 DIDerivedType ReturnType(getOrCreateType(Func->getReturnType()));
432 Params.push_back(ReturnType);
433
434 const Function::ArgumentListType &Args(Func->getArgumentList());
435 for (Function::ArgumentListType::const_iterator i = Args.begin(),
436 e = Args.end();
437 i != e; ++i) {
438 Type *T(i->getType());
439 Params.push_back(getOrCreateType(T));
440 }
441
442 DITypeArray ParamArray = Builder.getOrCreateTypeArray(Params);
443 return Builder.createSubroutineType(DIFile(FileNode), ParamArray);
444 }
445
446 /// Associates Instruction I with debug location Loc.
447 void addDebugLocation(Instruction &I, DebugLoc Loc) {
448 MDNode *MD = Loc.getAsMDNode(I.getContext());
449 I.setMetadata(LLVMContext::MD_dbg, MD);
450 }
451 };
452
453 /// Sets Filename/Directory from the Module identifier and returns true, or
454 /// false if source information is not present.
455 bool getSourceInfoFromModule(const Module &M, std::string &Directory,
456 std::string &Filename) {
457 std::string PathStr(M.getModuleIdentifier());
458 if (PathStr.length() == 0 || PathStr == "")
459 return false;
460
461 Filename = sys::path::filename(PathStr);
462 SmallVector Path(PathStr.begin(), PathStr.end());
463 sys::path::remove_filename(Path);
464 Directory = StringRef(Path.data(), Path.size());
465 return true;
466 }
467
468 // Sets Filename/Directory from debug information in M and returns true, or
469 // false if no debug information available, or cannot be parsed.
470 bool getSourceInfoFromDI(const Module &M, std::string &Directory,
471 std::string &Filename) {
472 NamedMDNode *CUNode = M.getNamedMetadata("llvm.dbg.cu");
473 if (!CUNode || CUNode->getNumOperands() == 0)
474 return false;
475
476 DICompileUnit CU(CUNode->getOperand(0));
477 if (!CU.Verify())
478 return false;
479
480 Filename = CU.getFilename();
481 Directory = CU.getDirectory();
482 return true;
483 }
484
485 } // anonymous namespace
486
487 namespace llvm {
488
489 bool DebugIR::getSourceInfo(const Module &M) {
490 ParsedPath = getSourceInfoFromDI(M, Directory, Filename) ||
491 getSourceInfoFromModule(M, Directory, Filename);
492 return ParsedPath;
493 }
494
495 bool DebugIR::updateExtension(StringRef NewExtension) {
496 size_t dot = Filename.find_last_of(".");
497 if (dot == std::string::npos)
498 return false;
499
500 Filename.erase(dot);
501 Filename += NewExtension.str();
502 return true;
503 }
504
505 void DebugIR::generateFilename(std::unique_ptr &fd) {
506 SmallVector PathVec;
507 fd.reset(new int);
508 sys::fs::createTemporaryFile("debug-ir", "ll", *fd, PathVec);
509 StringRef Path(PathVec.data(), PathVec.size());
510 Filename = sys::path::filename(Path);
511 sys::path::remove_filename(PathVec);
512 Directory = StringRef(PathVec.data(), PathVec.size());
513
514 GeneratedPath = true;
515 }
516
517 std::string DebugIR::getPath() {
518 SmallVector Path;
519 sys::path::append(Path, Directory, Filename);
520 Path.resize(Filename.size() + Directory.size() + 2);
521 Path[Filename.size() + Directory.size() + 1] = '\0';
522 return std::string(Path.data());
523 }
524
525 void DebugIR::writeDebugBitcode(const Module *M, int *fd) {
526 std::unique_ptr Out;
527 std::error_code EC;
528
529 if (!fd) {
530 std::string Path = getPath();
531 Out.reset(new raw_fd_ostream(Path, EC, sys::fs::F_Text));
532 DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to file "
533 << Path << "\n");
534 } else {
535 DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to fd "
536 << *fd << "\n");
537 Out.reset(new raw_fd_ostream(*fd, true));
538 }
539
540 M->print(*Out, nullptr);
541 Out->close();
542 }
543
544 void DebugIR::createDebugInfo(Module &M, std::unique_ptr &DisplayM) {
545 if (M.getFunctionList().size() == 0)
546 // no functions -- no debug info needed
547 return;
548
549 std::unique_ptr VMap;
550
551 if (WriteSourceToDisk && (HideDebugIntrinsics || HideDebugMetadata)) {
552 VMap.reset(new ValueToValueMapTy);
553 DisplayM.reset(CloneModule(&M, *VMap));
554
555 if (HideDebugIntrinsics)
556 DebugIntrinsicsRemover::process(*DisplayM);
557
558 if (HideDebugMetadata)
559 DebugMetadataRemover::process(*DisplayM);
560 }
561
562 DIUpdater R(M, Filename, Directory, DisplayM.get(), VMap.get());
563 }
564
565 bool DebugIR::isMissingPath() { return Filename.empty() || Directory.empty(); }
566
567 bool DebugIR::runOnModule(Module &M) {
568 std::unique_ptr fd;
569
570 if (isMissingPath() && !getSourceInfo(M)) {
571 if (!WriteSourceToDisk)
572 report_fatal_error("DebugIR unable to determine file name in input. "
573 "Ensure Module contains an identifier, a valid "
574 "DICompileUnit, or construct DebugIR with "
575 "non-empty Filename/Directory parameters.");
576 else
577 generateFilename(fd);
578 }
579
580 if (!GeneratedPath && WriteSourceToDisk)
581 updateExtension(".debug-ll");
582
583 // Clear line numbers. Keep debug info (if any) if we were able to read the
584 // file name from the DICompileUnit descriptor.
585 DebugMetadataRemover::process(M, !ParsedPath);
586
587 std::unique_ptr DisplayM;
588 createDebugInfo(M, DisplayM);
589 if (WriteSourceToDisk) {
590 Module *OutputM = DisplayM.get() ? DisplayM.get() : &M;
591 writeDebugBitcode(OutputM, fd.get());
592 }
593
594 DEBUG(M.dump());
595 return true;
596 }
597
598 bool DebugIR::runOnModule(Module &M, std::string &Path) {
599 bool result = runOnModule(M);
600 Path = getPath();
601 return result;
602 }
603
604 } // llvm namespace
605
606 char DebugIR::ID = 0;
607 INITIALIZE_PASS(DebugIR, "debug-ir", "Enable debugging IR", false, false)
608
609 ModulePass *llvm::createDebugIRPass(bool HideDebugIntrinsics,
610 bool HideDebugMetadata, StringRef Directory,
611 StringRef Filename) {
612 return new DebugIR(HideDebugIntrinsics, HideDebugMetadata, Directory,
613 Filename);
614 }
615
616 ModulePass *llvm::createDebugIRPass() { return new DebugIR(); }
+0
-98
lib/Transforms/Instrumentation/DebugIR.h less more
None //===- llvm/Transforms/Instrumentation/DebugIR.h - Interface ----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the interface of the DebugIR pass. For most users,
10 // including Instrumentation.h and calling createDebugIRPass() is sufficient and
11 // there is no need to include this file.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
16 #define LLVM_LIB_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
17
18 #include "llvm/Pass.h"
19
20 namespace llvm {
21
22 class DebugIR : public llvm::ModulePass {
23 /// If true, write a source file to disk.
24 bool WriteSourceToDisk;
25
26 /// Hide certain (non-essential) debug information (only relevant if
27 /// createSource is true.
28 bool HideDebugIntrinsics;
29 bool HideDebugMetadata;
30
31 /// The location of the source file.
32 std::string Directory;
33 std::string Filename;
34
35 /// True if a temporary file name was generated.
36 bool GeneratedPath;
37
38 /// True if the file name was read from the Module.
39 bool ParsedPath;
40
41 public:
42 static char ID;
43
44 const char *getPassName() const override { return "DebugIR"; }
45
46 /// Generate a file on disk to be displayed in a debugger. If Filename and
47 /// Directory are empty, a temporary path will be generated.
48 DebugIR(bool HideDebugIntrinsics, bool HideDebugMetadata,
49 llvm::StringRef Directory, llvm::StringRef Filename)
50 : ModulePass(ID), WriteSourceToDisk(true),
51 HideDebugIntrinsics(HideDebugIntrinsics),
52 HideDebugMetadata(HideDebugMetadata), Directory(Directory),
53 Filename(Filename), GeneratedPath(false), ParsedPath(false) {}
54
55 /// Modify input in-place; do not generate additional files, and do not hide
56 /// any debug intrinsics/metadata that might be present.
57 DebugIR()
58 : ModulePass(ID), WriteSourceToDisk(false), HideDebugIntrinsics(false),
59 HideDebugMetadata(false), GeneratedPath(false), ParsedPath(false) {}
60
61 /// Run pass on M and set Path to the source file path in the output module.
62 bool runOnModule(llvm::Module &M, std::string &Path);
63 bool runOnModule(llvm::Module &M) override;
64
65 private:
66
67 /// Returns the concatenated Directory + Filename, without error checking
68 std::string getPath();
69
70 /// Attempts to read source information from debug information in M, and if
71 /// that fails, from M's identifier. Returns true on success, false otherwise.
72 bool getSourceInfo(const llvm::Module &M);
73
74 /// Replace the extension of Filename with NewExtension, and return true if
75 /// successful. Return false if extension could not be found or Filename is
76 /// empty.
77 bool updateExtension(llvm::StringRef NewExtension);
78
79 /// Generate a temporary filename and open an fd
80 void generateFilename(std::unique_ptr &fd);
81
82 /// Creates DWARF CU/Subroutine metadata
83 void createDebugInfo(llvm::Module &M,
84 std::unique_ptr &DisplayM);
85
86 /// Returns true if either Directory or Filename is missing, false otherwise.
87 bool isMissingPath();
88
89 /// Write M to disk, optionally passing in an fd to an open file which is
90 /// closed by this function after writing. If no fd is specified, a new file
91 /// is opened, written, and closed.
92 void writeDebugBitcode(const llvm::Module *M, int *fd = nullptr);
93 };
94
95 } // llvm namespace
96
97 #endif
+0
-42
test/Transforms/DebugIR/crash.ll less more
None ; ModuleID = 'crash.c'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 @.str = private unnamed_addr constant [18 x i8] c"Hello, segfault!\0A\00", align 1
5 @.str1 = private unnamed_addr constant [14 x i8] c"Now crash %d\0A\00", align 1
6
7 ; Function Attrs: nounwind uwtable
8 define i32 @main(i32 %argc, i8** %argv) #0 {
9 %1 = alloca i32, align 4 ;CHECK: !dbg
10 %2 = alloca i32, align 4 ;CHECK-NEXT: !dbg
11 %3 = alloca i8**, align 8 ;CHECK-NEXT: !dbg
12 %null_ptr = alloca i32*, align 8 ;CHECK-NEXT: !dbg
13 store i32 0, i32* %1 ;CHECK-NEXT: !dbg
14 store i32 %argc, i32* %2, align 4 ;CHECK-NEXT: !dbg
15 store i8** %argv, i8*** %3, align 8 ;CHECK-NEXT: !dbg
16 store i32* null, i32** %null_ptr, align 8 ;CHECK-NEXT: !dbg
17 %4 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([18 x i8]* @.str, i32 0, i32 0)) ;CHECK-NEXT: !dbg
18 %5 = load i32** %null_ptr, align 8 ;CHECK-NEXT: !dbg
19 %6 = load i32* %5, align 4 ;CHECK-NEXT: !dbg
20 %7 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([14 x i8]* @.str1, i32 0, i32 0), i32 %6) ;CHECK-NEXT: !dbg
21 %8 = load i32* %2, align 4 ;CHECK-NEXT: !dbg
22 ret i32 %8 ;CHECK-NEXT: !dbg
23 }
24
25 declare i32 @printf(i8*, ...) #1
26
27 attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
28 attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
29
30 ; CHECK: = metadata !{i32 14,
31 ; CHECK-NEXT: = metadata !{i32 15,
32 ; CHECK-NEXT: = metadata !{i32 16,
33 ; CHECK-NEXT: = metadata !{i32 17,
34 ; CHECK-NEXT: = metadata !{i32 18,
35 ; CHECK-NEXT: = metadata !{i32 19,
36 ; CHECK-NEXT: = metadata !{i32 20,
37 ; CHECK-NEXT: = metadata !{i32 21,
38 ; CHECK-NEXT: = metadata !{i32 22,
39 ; CHECK-NEXT: = metadata !{i32 23,
40
41 ; RUN: opt %s -debug-ir -S | FileCheck %s
+0
-127
test/Transforms/DebugIR/exception.ll less more
None ; ModuleID = 'exception.cpp'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 @_ZTIi = external constant i8*
5
6 ; Function Attrs: uwtable
7 define i32 @main(i32 %argc, i8** %argv) #0 {
8 %1 = alloca i32, align 4 ; CHECK: !dbg
9 %2 = alloca i32, align 4 ; CHECK-NEXT: !dbg
10 %3 = alloca i8**, align 8 ; CHECK-NEXT: !dbg
11 %4 = alloca i8* ; CHECK-NEXT: !dbg
12 %5 = alloca i32 ; CHECK-NEXT: !dbg
13 %e = alloca i32, align 4 ; CHECK-NEXT: !dbg
14 %6 = alloca i32 ; CHECK-NEXT: !dbg
15 store i32 0, i32* %1 ; CHECK-NEXT: !dbg
16 store i32 %argc, i32* %2, align 4 ; CHECK-NEXT: !dbg
17 store i8** %argv, i8*** %3, align 8 ; CHECK-NEXT: !dbg
18 %7 = call i8* @__cxa_allocate_exception(i64 4) #2 ; CHECK-NEXT: !dbg
19 %8 = bitcast i8* %7 to i32* ; CHECK-NEXT: !dbg
20 %9 = load i32* %2, align 4 ; CHECK-NEXT: !dbg
21 store i32 %9, i32* %8 ; CHECK-NEXT: !dbg
22 invoke void @__cxa_throw(i8* %7, i8* bitcast (i8** @_ZTIi to i8*), i8* null) #3
23 to label %31 unwind label %10 ; CHECK: !dbg
24
25 ;
26 %11 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
27 catch i8* bitcast (i8** @_ZTIi to i8*) ; CHECK: !dbg
28 %12 = extractvalue { i8*, i32 } %11, 0 ; CHECK-NEXT: !dbg
29 store i8* %12, i8** %4 ; CHECK-NEXT: !dbg
30 %13 = extractvalue { i8*, i32 } %11, 1 ; CHECK-NEXT: !dbg
31 store i32 %13, i32* %5 ; CHECK-NEXT: !dbg
32 br label %14 ; CHECK-NEXT: !dbg
33
34 ;
35 %15 = load i32* %5 ; CHECK: !dbg
36 %16 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #2 ; CHECK-NEXT: !dbg
37 %17 = icmp eq i32 %15, %16 ; CHECK-NEXT: !dbg
38 br i1 %17, label %18, label %26 ; CHECK-NEXT: !dbg
39
40 ;
41 %19 = load i8** %4 ; CHECK: !dbg
42 %20 = call i8* @__cxa_begin_catch(i8* %19) #2 ; CHECK-NEXT: !dbg
43 %21 = bitcast i8* %20 to i32* ; CHECK-NEXT: !dbg
44 %22 = load i32* %21, align 4 ; CHECK-NEXT: !dbg
45 store i32 %22, i32* %e, align 4 ; CHECK-NEXT: !dbg
46 %23 = load i32* %e, align 4 ; CHECK-NEXT: !dbg
47 store i32 %23, i32* %1 ; CHECK-NEXT: !dbg
48 store i32 1, i32* %6 ; CHECK-NEXT: !dbg
49 call void @__cxa_end_catch() #2 ; CHECK-NEXT: !dbg
50 br label %24 ; CHECK-NEXT: !dbg
51
52 ;
53 %25 = load i32* %1 ; CHECK: !dbg
54 ret i32 %25 ; CHECK-NEXT: !dbg
55
56 ;
57 %27 = load i8** %4 ; CHECK: !dbg
58 %28 = load i32* %5 ; CHECK-NEXT: !dbg
59 %29 = insertvalue { i8*, i32 } undef, i8* %27, 0 ; CHECK-NEXT: !dbg
60 %30 = insertvalue { i8*, i32 } %29, i32 %28, 1 ; CHECK-NEXT: !dbg
61 resume { i8*, i32 } %30 ; CHECK-NEXT: !dbg
62
63 ;
64 unreachable ; CHECK: !dbg
65 }
66
67 declare i8* @__cxa_allocate_exception(i64)
68
69 declare void @__cxa_throw(i8*, i8*, i8*)
70
71 declare i32 @__gxx_personality_v0(...)
72
73 ; Function Attrs: nounwind readnone
74 declare i32 @llvm.eh.typeid.for(i8*) #1
75
76 declare i8* @__cxa_begin_catch(i8*)
77
78 declare void @__cxa_end_catch()
79
80 attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
81 attributes #1 = { nounwind readnone }
82 attributes #2 = { nounwind }
83 attributes #3 = { noreturn }
84 ; CHECK: = metadata !{i32 16,
85 ; CHECK-NEXT: = metadata !{i32 17,
86 ; CHECK-NEXT: = metadata !{i32 18,
87 ; CHECK-NEXT: = metadata !{i32 19,
88 ; CHECK-NEXT: = metadata !{i32 20,
89 ; CHECK-NEXT: = metadata !{i32 21,
90 ; CHECK-NEXT: = metadata !{i32 22,
91 ; CHECK-NEXT: = metadata !{i32 24,
92
93 ; CHECK-NEXT: = metadata !{i32 28,
94 ; CHECK-NEXT: = metadata !{i32 29,
95 ; CHECK-NEXT: = metadata !{i32 30,
96 ; CHECK-NEXT: = metadata !{i32 31,
97 ; CHECK-NEXT: = metadata !{i32 32,
98 ; CHECK-NEXT: = metadata !{i32 33,
99
100 ; CHECK-NEXT: = metadata !{i32 36,
101 ; CHECK-NEXT: = metadata !{i32 37,
102 ; CHECK-NEXT: = metadata !{i32 38,
103 ; CHECK-NEXT: = metadata !{i32 39,
104
105 ; CHECK-NEXT: = metadata !{i32 42,
106 ; CHECK-NEXT: = metadata !{i32 43,
107 ; CHECK-NEXT: = metadata !{i32 44,
108 ; CHECK-NEXT: = metadata !{i32 45,
109 ; CHECK-NEXT: = metadata !{i32 46,
110 ; CHECK-NEXT: = metadata !{i32 47,
111 ; CHECK-NEXT: = metadata !{i32 48,
112 ; CHECK-NEXT: = metadata !{i32 49,
113 ; CHECK-NEXT: = metadata !{i32 50,
114 ; CHECK-NEXT: = metadata !{i32 51,
115
116 ; CHECK-NEXT: = metadata !{i32 54,
117 ; CHECK-NEXT: = metadata !{i32 55,
118
119 ; CHECK-NEXT: = metadata !{i32 58,
120 ; CHECK-NEXT: = metadata !{i32 59,
121 ; CHECK-NEXT: = metadata !{i32 60,
122 ; CHECK-NEXT: = metadata !{i32 61,
123 ; CHECK-NEXT: = metadata !{i32 62,
124 ; CHECK-NEXT: = metadata !{i32 65,
125
126 ; RUN: opt %s -debug-ir -S | FileCheck %s
+0
-51
test/Transforms/DebugIR/function.ll less more
None ; ModuleID = 'function.c'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 ; Function Attrs: nounwind uwtable
5 define void @blah(i32* %i) #0 {
6 %1 = alloca i32*, align 8 ; CHECK: !dbg
7 store i32* %i, i32** %1, align 8 ; CHECK-NEXT: !dbg
8 %2 = load i32** %1, align 8 ; CHECK-NEXT: !dbg
9 %3 = load i32* %2, align 4 ; CHECK-NEXT: !dbg
10 %4 = add nsw i32 %3, 1 ; CHECK-NEXT: !dbg
11 store i32 %4, i32* %2, align 4 ; CHECK-NEXT: !dbg
12 ret void ; CHECK-NEXT: !dbg
13 }
14
15 ; Function Attrs: nounwind uwtable
16 define i32 @main(i32 %argc, i8** %argv) #0 {
17 %1 = alloca i32, align 4 ; CHECK: !dbg
18 %2 = alloca i32, align 4 ; CHECK-NEXT: !dbg
19 %3 = alloca i8**, align 8 ; CHECK-NEXT: !dbg
20 %i = alloca i32, align 4 ; CHECK-NEXT: !dbg
21 store i32 0, i32* %1 ; CHECK-NEXT: !dbg
22 store i32 %argc, i32* %2, align 4 ; CHECK-NEXT: !dbg
23 store i8** %argv, i8*** %3, align 8 ; CHECK-NEXT: !dbg
24 store i32 7, i32* %i, align 4 ; CHECK-NEXT: !dbg
25 call void @blah(i32* %i) ; CHECK-NEXT: !dbg
26 %4 = load i32* %i, align 4 ; CHECK-NEXT: !dbg
27 ret i32 %4 ; CHECK-NEXT: !dbg
28 }
29
30 attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
31 ; CHECK: = metadata !{i32 8,
32 ; CHECK-NEXT: = metadata !{i32 9,
33 ; CHECK-NEXT: = metadata !{i32 10,
34 ; CHECK-NEXT: = metadata !{i32 11,
35 ; CHECK-NEXT: = metadata !{i32 12,
36 ; CHECK-NEXT: = metadata !{i32 13,
37
38 ; CHECK-NEXT: = metadata !{i32 18,
39 ; CHECK-NEXT: = metadata !{i32 19,
40 ; CHECK-NEXT: = metadata !{i32 20,
41 ; CHECK-NEXT: = metadata !{i32 21,
42 ; CHECK-NEXT: = metadata !{i32 22,
43 ; CHECK-NEXT: = metadata !{i32 23,
44 ; CHECK-NEXT: = metadata !{i32 24,
45 ; CHECK-NEXT: = metadata !{i32 25,
46 ; CHECK-NEXT: = metadata !{i32 26,
47 ; CHECK-NEXT: = metadata !{i32 27,
48 ; CHECK-NEXT: = metadata !{i32 28,
49
50 ; RUN: opt %s -debug-ir -S | FileCheck %s
+0
-11
test/Transforms/DebugIR/simple-addrspace.ll less more
None ; RUN: opt -debug-ir -S %s -o - | FileCheck %s
1
2 target datalayout = "e-p:64:64:64-p1:16:16:16"
3
4 define void @foo(i32 addrspace(1)*) nounwind {
5 ret void
6 }
7
8 ; Make sure the pointer size is 16
9
10 ; CHECK: metadata !"0xf\00i32 addrspace(1)*\000\0016\002\000\000"
+0
-25
test/Transforms/DebugIR/simple.ll less more
None ; ModuleID = 'simple.c'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 ; Function Attrs: nounwind uwtable
5 define i32 @main(i32 %argc, i8** %argv) #0 {
6 %1 = alloca i32, align 4 ; CHECK: !dbg
7 %2 = alloca i32, align 4 ; CHECK-NEXT: !dbg
8 %3 = alloca i8**, align 8 ; CHECK-NEXT: !dbg
9 store i32 0, i32* %1 ; CHECK-NEXT: !dbg
10 store i32 %argc, i32* %2, align 4 ; CHECK-NEXT: !dbg
11 store i8** %argv, i8*** %3, align 8 ; CHECK-NEXT: !dbg
12 %4 = load i32* %2, align 4 ; CHECK-NEXT: !dbg
13 ret i32 %4 ; CHECK-NEXT: !dbg
14 }
15
16 attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
17
18 ; CHECK: = metadata !{i32 10,
19 ; CHECK-NEXT: = metadata !{i32 11,
20 ; CHECK-NEXT: = metadata !{i32 12,
21 ; CHECK-NEXT: = metadata !{i32 13,
22 ; CHECK-NEXT: = metadata !{i32 14,
23
24 ; RUN: opt %s -debug-ir -S | FileCheck %s
+0
-24
test/Transforms/DebugIR/struct.ll less more
None ; ModuleID = 'struct.cpp'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 %struct.blah = type { i32, float, i8 }
5
6 ; Function Attrs: nounwind uwtable
7 define i32 @main() #0 {
8 %1 = alloca i32, align 4 ; CHECK: !dbg
9 %b = alloca %struct.blah, align 4 ; CHECK-NEXT: !dbg
10 store i32 0, i32* %1 ; CHECK-NEXT: !dbg
11 %2 = getelementptr inbounds %struct.blah* %b, i32 0, i32 0 ; CHECK-NEXT: !dbg
12 %3 = load i32* %2, align 4 ; CHECK-NEXT: !dbg
13 ret i32 %3 ; CHECK-NEXT: !dbg
14 }
15
16 attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
17
18 ; CHECK: = metadata !{i32 11,
19 ; CHECK-NEXT: = metadata !{i32 12,
20 ; CHECK-NEXT: = metadata !{i32 13,
21 ; CHECK-NEXT: = metadata !{i32 14,
22
23 ; RUN: opt %s -debug-ir -S | FileCheck %s
+0
-93
test/Transforms/DebugIR/vector.ll less more
None ; ModuleID = 'vector.cpp'
1 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-linux-gnu"
3
4 ; Function Attrs: noinline nounwind uwtable
5 define <4 x float> @_Z3fooDv2_fS_(double %a.coerce, double %b.coerce) #0 {
6 %1 = alloca <2 x float>, align 8 ; CHECK: !dbg
7 %2 = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
8 %3 = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
9 %4 = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
10 %c = alloca <4 x float>, align 16 ; CHECK-NEXT: !dbg
11 %5 = bitcast <2 x float>* %1 to double* ; CHECK-NEXT: !dbg
12 store double %a.coerce, double* %5, align 1 ; CHECK-NEXT: !dbg
13 %a = load <2 x float>* %1, align 8 ; CHECK-NEXT: !dbg
14 store <2 x float> %a, <2 x float>* %2, align 8 ; CHECK-NEXT: !dbg
15 %6 = bitcast <2 x float>* %3 to double* ; CHECK-NEXT: !dbg
16 store double %b.coerce, double* %6, align 1 ; CHECK-NEXT: !dbg
17 %b = load <2 x float>* %3, align 8 ; CHECK-NEXT: !dbg
18 store <2 x float> %b, <2 x float>* %4, align 8 ; CHECK-NEXT: !dbg
19 %7 = load <2 x float>* %2, align 8 ; CHECK-NEXT: !dbg
20 %8 = load <4 x float>* %c, align 16 ; CHECK-NEXT: !dbg
21 %9 = shufflevector <2 x float> %7, <2 x float> undef, <4 x i32> ; CHECK-NEXT: !dbg
22 %10 = shufflevector <4 x float> %8, <4 x float> %9, <4 x i32> ; CHECK-NEXT: !dbg
23 store <4 x float> %10, <4 x float>* %c, align 16 ; CHECK-NEXT: !dbg
24 %11 = load <2 x float>* %4, align 8 ; CHECK-NEXT: !dbg
25 %12 = load <4 x float>* %c, align 16 ; CHECK-NEXT: !dbg
26 %13 = shufflevector <2 x float> %11, <2 x float> undef, <4 x i32> ; CHECK-NEXT: !dbg
27 %14 = shufflevector <4 x float> %12, <4 x float> %13, <4 x i32> ; CHECK-NEXT: !dbg
28 store <4 x float> %14, <4 x float>* %c, align 16 ; CHECK-NEXT: !dbg
29 %15 = load <4 x float>* %c, align 16 ; CHECK-NEXT: !dbg
30 ret <4 x float> %15 ; CHECK-NEXT: !dbg
31 }
32
33 ; Function Attrs: nounwind uwtable
34 define i32 @main() #1 {
35 %1 = alloca i32, align 4 ; CHECK: !dbg
36 %a = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
37 %b = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
38 %x = alloca <4 x float>, align 16 ; CHECK-NEXT: !dbg
39 %2 = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
40 %3 = alloca <2 x float>, align 8 ; CHECK-NEXT: !dbg
41 store i32 0, i32* %1 ; CHECK-NEXT: !dbg
42 store <2 x float> , <2 x float>* %a, align 8 ; CHECK-NEXT: !dbg
43 store <2 x float> , <2 x float>* %b, align 8 ; CHECK-NEXT: !dbg
44 %4 = load <2 x float>* %a, align 8 ; CHECK-NEXT: !dbg
45 %5 = load <2 x float>* %b, align 8 ; CHECK-NEXT: !dbg
46 store <2 x float> %4, <2 x float>* %2, align 8 ; CHECK-NEXT: !dbg
47 %6 = bitcast <2 x float>* %2 to double* ; CHECK-NEXT: !dbg
48 %7 = load double* %6, align 1 ; CHECK-NEXT: !dbg
49 store <2 x float> %5, <2 x float>* %3, align 8 ; CHECK-NEXT: !dbg
50 %8 = bitcast <2 x float>* %3 to double* ; CHECK-NEXT: !dbg
51 %9 = load double* %8, align 1 ; CHECK-NEXT: !dbg
52 %10 = call <4 x float> @_Z3fooDv2_fS_(double %7, double %9) ; CHECK-NEXT: !dbg
53 store <4 x float> %10, <4 x float>* %x, align 16 ; CHECK-NEXT: !dbg
54 %11 = load <4 x float>* %x, align 16 ; CHECK-NEXT: !dbg
55 %12 = extractelement <4 x float> %11, i32 0 ; CHECK-NEXT: !dbg
56 %13 = load <4 x float>* %x, align 16 ; CHECK-NEXT: !dbg
57 %14 = extractelement <4 x float> %13, i32 1 ; CHECK-NEXT: !dbg
58 %15 = fadd float %12, %14 ; CHECK-NEXT: !dbg
59 %16 = load <4 x float>* %x, align 16 ; CHECK-NEXT: !dbg
60 %17 = extractelement <4 x float> %16, i32 2 ; CHECK-NEXT: !dbg
61 %18 = fadd float %15, %17 ; CHECK-NEXT: !dbg
62 %19 = load <4 x float>* %x, align 16 ; CHECK-NEXT: !dbg
63 %20 = extractelement <4 x float> %19, i32 3 ; CHECK-NEXT: !dbg
64 %21 = fadd float %18, %20 ; CHECK-NEXT: !dbg
65 %22 = fptosi float %21 to i32 ; CHECK-NEXT: !dbg
66 ret i32 %22 ; CHECK-NEXT: !dbg
67 }
68
69 attributes #0 = { noinline nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
70 attributes #1 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
71
72 ; CHECK: = metadata !{i32 13,
73 ; CHECK-NEXT: = metadata !{i32 14,
74 ; CHECK-NEXT: = metadata !{i32 15,
75 ; CHECK-NEXT: = metadata !{i32 16,
76 ; CHECK-NEXT: = metadata !{i32 17,
77 ; CHECK-NEXT: = metadata !{i32 18,
78 ; CHECK-NEXT: = metadata !{i32 19,
79 ; CHECK-NEXT: = metadata !{i32 20,
80 ; CHECK-NEXT: = metadata !{i32 21,
81 ; CHECK-NEXT: = metadata !{i32 22,
82 ; CHECK-NEXT: = metadata !{i32 23,
83 ; CHECK-NEXT: = metadata !{i32 24,
84 ; CHECK-NEXT: = metadata !{i32 25,
85 ; CHECK-NEXT: = metadata !{i32 26,
86 ; CHECK-NEXT: = metadata !{i32 27,
87 ; CHECK-NEXT: = metadata !{i32 28,
88 ; CHECK-NEXT: = metadata !{i32 29,
89 ; CHECK-NEXT: = metadata !{i32 30,
90 ; CHECK-NEXT: = metadata !{i32 31,
91
92 ; RUN: opt %s -debug-ir -S | FileCheck %s
7373 cl::desc("Force interpretation: disable JIT"),
7474 cl::init(false));
7575
76 cl::opt DebugIR(
77 "debug-ir", cl::desc("Generate debug information to allow debugging IR."),
78 cl::init(false));
79
8076 // The MCJIT supports building for a target address space separate from
8177 // the JIT compilation process. Use a forked process and a copying
8278 // memory manager with IPC to execute using this functionality.
413409 }
414410 }
415411
416 if (DebugIR) {
417 ModulePass *DebugIRPass = createDebugIRPass();
418 DebugIRPass->runOnModule(*Mod);
419 }
420
421412 std::string ErrorMsg;
422413 EngineBuilder builder(std::move(Owner));
423414 builder.setMArch(MArch);
306306 // Initialize passes
307307 PassRegistry &Registry = *PassRegistry::getPassRegistry();
308308 initializeCore(Registry);
309 initializeDebugIRPass(Registry);
310309 initializeScalarOpts(Registry);
311310 initializeObjCARCOpts(Registry);
312311 initializeVectorization(Registry);
None add_subdirectory(DebugIR)
1 add_subdirectory(Utils)
+0
-9
unittests/Transforms/DebugIR/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 Core
2 Instrumentation
3 Support
4 )
5
6 add_llvm_unittest(DebugIRTests
7 DebugIR.cpp
8 )
+0
-308
unittests/Transforms/DebugIR/DebugIR.cpp less more
None //===- DebugIR.cpp - Unit tests for the DebugIR pass ----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // The tests in this file verify the DebugIR pass that generates debug metadata
10 // for LLVM IR.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ADT/Triple.h"
15 #include "../lib/Transforms/Instrumentation/DebugIR.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/IR/DIBuilder.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Errc.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Transforms/Instrumentation.h"
25
26 // These tests do not depend on MCJIT, but we use the TrivialModuleBuilder
27 // helper class to construct some trivial Modules.
28 #include "../unittests/ExecutionEngine/MCJIT/MCJITTestBase.h"
29
30 #include
31
32 #include "gtest/gtest.h"
33
34 #if defined(LLVM_ON_WIN32)
35 #include
36 #define getcwd_impl _getcwd
37 #elif defined (HAVE_GETCWD)
38 #include
39 #define getcwd_impl getcwd
40 #endif // LLVM_ON_WIN32
41
42 using namespace llvm;
43 using namespace std;
44
45 namespace {
46
47 /// Insert a mock CUDescriptor with the specified producer
48 void insertCUDescriptor(Module *M, StringRef File, StringRef Dir,
49 StringRef Producer) {
50 DIBuilder B(*M);
51 B.createCompileUnit(dwarf::DW_LANG_C99, File, Dir, Producer, false, "", 0);
52 B.finalize();
53 }
54
55 /// Attempts to remove file at Path and returns true if it existed, or false if
56 /// it did not.
57 bool removeIfExists(StringRef Path) {
58 // This is an approximation, on error we don't know in general if the file
59 // existed or not.
60 std::error_code EC = sys::fs::remove(Path, false);
61 return EC != llvm::errc::no_such_file_or_directory;
62 }
63
64 char * current_dir() {
65 #if defined(LLVM_ON_WIN32) || defined(HAVE_GETCWD)
66 // calling getcwd (or _getcwd() on windows) with a null buffer makes it
67 // allocate a sufficiently sized buffer to store the current working dir.
68 return getcwd_impl(nullptr, 0);
69 #else
70 return 0;
71 #endif
72 }
73
74 class TestDebugIR : public ::testing::Test, public TrivialModuleBuilder {
75 protected:
76 TestDebugIR()
77 : TrivialModuleBuilder(sys::getProcessTriple())
78 , cwd(current_dir()) {}
79
80 ~TestDebugIR() { free(cwd); }
81
82 /// Returns a concatenated path string consisting of Dir and Filename
83 string getPath(const string &Dir, const string &Filename) {
84 SmallVector Path;
85 sys::path::append(Path, Dir, Filename);
86 Path.resize(Dir.size() + Filename.size() + 2);
87 Path[Dir.size() + Filename.size() + 1] = '\0';
88 return string(Path.data());
89 }
90
91 LLVMContext Context;
92 char *cwd;
93 std::unique_ptr M;
94 std::unique_ptr D;
95 };
96
97 // Test empty named Module that is not supposed to be output to disk.
98 TEST_F(TestDebugIR, EmptyNamedModuleNoWrite) {
99 string Dir = "MadeUpDirectory";
100 string File = "empty_module.ll";
101 string Path(getPath(Dir, File));
102
103 M.reset(createEmptyModule(Path));
104
105 // constructing DebugIR with no args should not result in any file generated.
106 D.reset(static_cast(llvm::createDebugIRPass()));
107 D->runOnModule(*M);
108
109 // verify DebugIR did not generate a file
110 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
111 }
112
113 // Test a non-empty unnamed module that is output to an autogenerated file name.
114 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToAutogeneratedFile) {
115 M.reset(createEmptyModule());
116 insertAddFunction(M.get());
117 D.reset(static_cast(llvm::createDebugIRPass(true, true)));
118
119 string Path;
120 D->runOnModule(*M, Path);
121
122 // verify DebugIR generated a file, and clean it up
123 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
124 }
125
126 // Test not specifying a name in the module -- DebugIR should generate a name
127 // and write the file contents.
128 TEST_F(TestDebugIR, EmptyModuleWriteAnonymousFile) {
129 M.reset(createEmptyModule());
130 D.reset(static_cast(llvm::createDebugIRPass(false, false)));
131
132 string Path;
133 D->runOnModule(*M, Path);
134
135 // verify DebugIR generated a file and clean it up
136 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
137 }
138
139 #ifdef HAVE_GETCWD // These tests require get_current_dir_name()
140
141 // Test empty named Module that is to be output to path specified at Module
142 // construction.
143 TEST_F(TestDebugIR, EmptyNamedModuleWriteFile) {
144 string Filename("NamedFile1");
145 string ExpectedPath(getPath(cwd, Filename));
146
147 M.reset(createEmptyModule(ExpectedPath));
148 D.reset(static_cast(llvm::createDebugIRPass(true, true)));
149
150 string Path;
151 D->runOnModule(*M, Path);
152
153 // verify DebugIR was able to correctly parse the file name from module ID
154 ASSERT_EQ(ExpectedPath, Path);
155
156 // verify DebugIR generated a file, and clean it up
157 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
158 }
159
160 // Test an empty unnamed module generates an output file whose path is specified
161 // at DebugIR construction.
162 TEST_F(TestDebugIR, EmptyUnnamedModuleWriteNamedFile) {
163 string Filename("NamedFile2");
164
165 M.reset(createEmptyModule());
166 D.reset(static_cast(llvm::createDebugIRPass(
167 false, false, StringRef(cwd), StringRef(Filename))));
168 string Path;
169 D->runOnModule(*M, Path);
170
171 string ExpectedPath(getPath(cwd, Filename));
172 ASSERT_EQ(ExpectedPath, Path);
173
174 // verify DebugIR generated a file, and clean it up
175 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
176 }
177
178 // Test an empty named module generates an output file at the path specified
179 // during DebugIR construction.
180 TEST_F(TestDebugIR, EmptyNamedModuleWriteNamedFile) {
181 string Filename("NamedFile3");
182
183 string UnexpectedPath(getPath(cwd, "UnexpectedFilename"));
184 M.reset(createEmptyModule(UnexpectedPath));
185
186 D.reset(static_cast(llvm::createDebugIRPass(
187 false, false, StringRef(cwd), StringRef(Filename))));
188 string Path;
189 D->runOnModule(*M, Path);
190
191 string ExpectedPath(getPath(cwd, Filename));
192 ASSERT_EQ(ExpectedPath, Path);
193
194 // verify DebugIR generated a file, and clean it up
195 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
196
197 // verify DebugIR did not generate a file at the path specified at Module
198 // construction.
199 ASSERT_FALSE(removeIfExists(UnexpectedPath)) << "Unexpected file " << Path;
200 }
201
202 // Test a non-empty named module that is not supposed to be output to disk
203 TEST_F(TestDebugIR, NonEmptyNamedModuleNoWrite) {
204 string Filename("NamedFile4");
205 string ExpectedPath(getPath(cwd, Filename));
206
207 M.reset(createEmptyModule(ExpectedPath));
208 insertAddFunction(M.get());
209
210 D.reset(static_cast(llvm::createDebugIRPass()));
211
212 string Path;
213 D->runOnModule(*M, Path);
214 ASSERT_EQ(ExpectedPath, Path);
215
216 // verify DebugIR did not generate a file
217 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
218 }
219
220 // Test a non-empty named module that is output to disk.
221 TEST_F(TestDebugIR, NonEmptyNamedModuleWriteFile) {
222 string Filename("NamedFile5");
223 string ExpectedPath(getPath(cwd, Filename));
224
225 M.reset(createEmptyModule(ExpectedPath));
226 insertAddFunction(M.get());
227
228 D.reset(static_cast(llvm::createDebugIRPass(true, true)));
229
230 string Path;
231 D->runOnModule(*M, Path);
232 ASSERT_EQ(ExpectedPath, Path);
233
234 // verify DebugIR generated a file, and clean it up
235 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
236 }
237
238 // Test a non-empty unnamed module is output to a path specified at DebugIR
239 // construction.
240 TEST_F(TestDebugIR, NonEmptyUnnamedModuleWriteToNamedFile) {
241 string Filename("NamedFile6");
242
243 M.reset(createEmptyModule());
244 insertAddFunction(M.get());
245
246 D.reset(static_cast(
247 llvm::createDebugIRPass(true, true, cwd, Filename)));
248 string Path;
249 D->runOnModule(*M, Path);
250
251 string ExpectedPath(getPath(cwd, Filename));
252 ASSERT_EQ(ExpectedPath, Path);
253
254 // verify DebugIR generated a file, and clean it up
255 ASSERT_TRUE(removeIfExists(Path)) << "Missing expected file at " << Path;
256 }
257
258 // Test that information inside existing debug metadata is retained
259 TEST_F(TestDebugIR, ExistingMetadataRetained) {
260 string Filename("NamedFile7");
261 string ExpectedPath(getPath(cwd, Filename));
262
263 M.reset(createEmptyModule(ExpectedPath));
264 insertAddFunction(M.get());
265
266 StringRef Producer("TestProducer");
267 insertCUDescriptor(M.get(), Filename, cwd, Producer);
268
269 DebugInfoFinder Finder;
270 Finder.processModule(*M);
271 ASSERT_EQ((unsigned)1, Finder.compile_unit_count());
272 D.reset(static_cast(llvm::createDebugIRPass()));
273
274 string Path;
275 D->runOnModule(*M, Path);
276 ASSERT_EQ(ExpectedPath, Path);
277
278 // verify DebugIR did not generate a file
279 ASSERT_FALSE(removeIfExists(Path)) << "Unexpected file " << Path;
280
281 DICompileUnit CU(*Finder.compile_units().begin());
282
283 // Verify original CU information is retained
284 ASSERT_EQ(Filename, CU.getFilename());
285 ASSERT_EQ(cwd, CU.getDirectory());
286 ASSERT_EQ(Producer, CU.getProducer());
287 }
288
289 #endif // HAVE_GETCWD
290
291 #ifdef GTEST_HAS_DEATH_TEST
292
293 // Test a non-empty unnamed module that is not supposed to be output to disk
294 // NOTE: this test is expected to die with LLVM_ERROR, and such depends on
295 // google test's "death test" mode.
296 TEST_F(TestDebugIR, NonEmptyUnnamedModuleNoWrite) {
297 M.reset(createEmptyModule(StringRef()));
298 insertAddFunction(M.get());
299 D.reset(static_cast(llvm::createDebugIRPass()));
300
301 // No name in module or on DebugIR construction ==> DebugIR should assert
302 EXPECT_DEATH(D->runOnModule(*M),
303 "DebugIR unable to determine file name in input.");
304 }
305
306 #endif // GTEST_HAS_DEATH_TEST
307 }
+0
-15
unittests/Transforms/DebugIR/Makefile less more
None ##===- unittests/Transforms/Utils/Makefile -----------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../../..
10 TESTNAME = DebugIR
11 LINK_COMPONENTS := Instrumentation
12
13 include $(LEVEL)/Makefile.config
14 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
88
99 LEVEL = ../..
1010
11 PARALLEL_DIRS = DebugIR Utils
11 PARALLEL_DIRS = Utils
1212
1313 include $(LEVEL)/Makefile.common
1414