1 //===- DebugTranslation.cpp - MLIR to LLVM Debug conversion ---------------===//
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 #include "DebugTranslation.h"
10 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11 #include "llvm/IR/Metadata.h"
12 #include "llvm/IR/Module.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 
16 using namespace mlir;
17 using namespace mlir::LLVM;
18 using namespace mlir::LLVM::detail;
19 
20 /// A utility walker that interrupts if the operation has valid debug
21 /// information.
22 static WalkResult interruptIfValidLocation(Operation *op) {
23   return op->getLoc().isa<UnknownLoc>() ? WalkResult::advance()
24                                         : WalkResult::interrupt();
25 }
26 
27 DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
28     : builder(llvmModule), llvmCtx(llvmModule.getContext()),
29       compileUnit(nullptr) {
30 
31   // If the module has no location information, there is nothing to do.
32   if (!module->walk(interruptIfValidLocation).wasInterrupted())
33     return;
34 
35   // TODO: Several parts of this are incorrect. Different source
36   // languages may interpret different parts of the debug information
37   // differently. Frontends will also want to pipe in various information, like
38   // flags. This is fine for now as we only emit line-table information and not
39   // types or variables. This should disappear as the debug information story
40   // evolves; i.e. when we have proper attributes for LLVM debug metadata.
41   compileUnit = builder.createCompileUnit(
42       llvm::dwarf::DW_LANG_C,
43       builder.createFile(llvmModule.getModuleIdentifier(), "/"),
44       /*Producer=*/"mlir", /*isOptimized=*/true, /*Flags=*/"", /*RV=*/0);
45 
46   // Mark this module as having debug information.
47   StringRef debugVersionKey = "Debug Info Version";
48   if (!llvmModule.getModuleFlag(debugVersionKey))
49     llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
50                              llvm::DEBUG_METADATA_VERSION);
51 }
52 
53 /// Finalize the translation of debug information.
54 void DebugTranslation::finalize() { builder.finalize(); }
55 
56 /// Attempt to extract a filename for the given loc.
57 static FileLineColLoc extractFileLoc(Location loc) {
58   if (auto fileLoc = loc.dyn_cast<FileLineColLoc>())
59     return fileLoc;
60   if (auto nameLoc = loc.dyn_cast<NameLoc>())
61     return extractFileLoc(nameLoc.getChildLoc());
62   if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>())
63     return extractFileLoc(opaqueLoc.getFallbackLocation());
64   return FileLineColLoc();
65 }
66 
67 /// Translate the debug information for the given function.
68 void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
69   // If the function doesn't have location information, there is nothing to
70   // translate.
71   if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted())
72     return;
73 
74   // If we are to create debug info for the function, we need to ensure that all
75   // inlinable calls in it are with debug info, otherwise the LLVM verifier will
76   // complain. For now, be more restricted and treat all calls as inlinable.
77   const bool hasCallWithoutDebugInfo =
78       func.walk([](LLVM::CallOp call) {
79             return call.getLoc().isa<UnknownLoc>() ? WalkResult::interrupt()
80                                                    : WalkResult::advance();
81           })
82           .wasInterrupted();
83   if (hasCallWithoutDebugInfo)
84     return;
85 
86   FileLineColLoc fileLoc = extractFileLoc(func.getLoc());
87   auto *file = translateFile(fileLoc ? fileLoc.getFilename() : "<unknown>");
88   unsigned line = fileLoc ? fileLoc.getLine() : 0;
89 
90   // TODO: This is the bare essentials for now. We will likely end
91   // up with wrapper metadata around LLVMs metadata in the future, so this
92   // doesn't need to be smart until then.
93   llvm::DISubroutineType *type =
94       builder.createSubroutineType(builder.getOrCreateTypeArray(llvm::None));
95   llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition |
96                                           llvm::DISubprogram::SPFlagOptimized;
97   llvm::DISubprogram *program =
98       builder.createFunction(compileUnit, func.getName(), func.getName(), file,
99                              line, type, line, llvm::DINode::FlagZero, spFlags);
100   llvmFunc.setSubprogram(program);
101   builder.finalizeSubprogram(program);
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // Locations
106 //===----------------------------------------------------------------------===//
107 
108 /// Translate the given location to an llvm debug location.
109 const llvm::DILocation *
110 DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) {
111   if (!compileUnit)
112     return nullptr;
113   return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
114 }
115 
116 /// Translate the given location to an llvm DebugLoc.
117 const llvm::DILocation *
118 DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope,
119                                const llvm::DILocation *inlinedAt) {
120   // LLVM doesn't have a representation for unknown.
121   if (!scope || loc.isa<UnknownLoc>())
122     return nullptr;
123 
124   // Check for a cached instance.
125   auto existingIt = locationToLoc.find(std::make_pair(loc, scope));
126   if (existingIt != locationToLoc.end())
127     return existingIt->second;
128 
129   const llvm::DILocation *llvmLoc = nullptr;
130   if (auto callLoc = loc.dyn_cast<CallSiteLoc>()) {
131     // For callsites, the caller is fed as the inlinedAt for the callee.
132     const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
133     llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
134 
135   } else if (auto fileLoc = loc.dyn_cast<FileLineColLoc>()) {
136     auto *file = translateFile(fileLoc.getFilename());
137     auto *fileScope = builder.createLexicalBlockFile(scope, file);
138     llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(),
139                                     fileLoc.getColumn(), fileScope,
140                                     const_cast<llvm::DILocation *>(inlinedAt));
141 
142   } else if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
143     ArrayRef<Location> locations = fusedLoc.getLocations();
144 
145     // For fused locations, merge each of the nodes.
146     llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
147     for (Location locIt : locations.drop_front()) {
148       llvmLoc = llvm::DILocation::getMergedLocation(
149           llvmLoc, translateLoc(locIt, scope, inlinedAt));
150     }
151 
152   } else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
153     llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
154 
155   } else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
156     llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
157                            inlinedAt);
158   } else {
159     llvm_unreachable("unknown location kind");
160   }
161 
162   locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc);
163   return llvmLoc;
164 }
165 
166 /// Create an llvm debug file for the given file path.
167 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
168   auto *&file = fileMap[fileName];
169   if (file)
170     return file;
171 
172   // Make sure the current working directory is up-to-date.
173   if (currentWorkingDir.empty())
174     llvm::sys::fs::current_path(currentWorkingDir);
175 
176   StringRef directory = currentWorkingDir;
177   SmallString<128> dirBuf;
178   SmallString<128> fileBuf;
179   if (llvm::sys::path::is_absolute(fileName)) {
180     // Strip the common prefix (if it is more than just "/") from current
181     // directory and FileName for a more space-efficient encoding.
182     auto fileIt = llvm::sys::path::begin(fileName);
183     auto fileE = llvm::sys::path::end(fileName);
184     auto curDirIt = llvm::sys::path::begin(directory);
185     auto curDirE = llvm::sys::path::end(directory);
186     for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
187       llvm::sys::path::append(dirBuf, *curDirIt);
188     if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
189       // Don't strip the common prefix if it is only the root "/"  since that
190       // would make LLVM diagnostic locations confusing.
191       directory = StringRef();
192     } else {
193       for (; fileIt != fileE; ++fileIt)
194         llvm::sys::path::append(fileBuf, *fileIt);
195       directory = dirBuf;
196       fileName = fileBuf;
197     }
198   }
199   return (file = builder.createFile(fileName, directory));
200 }
201