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.
interruptIfValidLocation(Operation * op)22 static WalkResult interruptIfValidLocation(Operation *op) {
23 return op->getLoc().isa<UnknownLoc>() ? WalkResult::advance()
24 : WalkResult::interrupt();
25 }
26
DebugTranslation(Operation * module,llvm::Module & llvmModule)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 if (auto targetTripleAttr =
53 module->getAttr(LLVM::LLVMDialect::getTargetTripleAttrName())) {
54 auto targetTriple =
55 llvm::Triple(targetTripleAttr.cast<StringAttr>().getValue());
56 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
57 // Dwarf debugging files will be generated by default, unless "CodeView"
58 // is set explicitly. Windows/MSVC should use CodeView instead.
59 llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
60 }
61 }
62 }
63
64 /// Finalize the translation of debug information.
finalize()65 void DebugTranslation::finalize() { builder.finalize(); }
66
67 /// Attempt to extract a filename for the given loc.
extractFileLoc(Location loc)68 static FileLineColLoc extractFileLoc(Location loc) {
69 if (auto fileLoc = loc.dyn_cast<FileLineColLoc>())
70 return fileLoc;
71 if (auto nameLoc = loc.dyn_cast<NameLoc>())
72 return extractFileLoc(nameLoc.getChildLoc());
73 if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>())
74 return extractFileLoc(opaqueLoc.getFallbackLocation());
75 return FileLineColLoc();
76 }
77
78 /// Translate the debug information for the given function.
translate(LLVMFuncOp func,llvm::Function & llvmFunc)79 void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
80 // If the function doesn't have location information, there is nothing to
81 // translate.
82 if (!compileUnit || !func.walk(interruptIfValidLocation).wasInterrupted())
83 return;
84
85 // If we are to create debug info for the function, we need to ensure that all
86 // inlinable calls in it are with debug info, otherwise the LLVM verifier will
87 // complain. For now, be more restricted and treat all calls as inlinable.
88 const bool hasCallWithoutDebugInfo =
89 func.walk([&](LLVM::CallOp call) {
90 return call.getLoc()->walk([](Location l) {
91 return l.isa<UnknownLoc>() ? WalkResult::interrupt()
92 : WalkResult::advance();
93 });
94 })
95 .wasInterrupted();
96 if (hasCallWithoutDebugInfo)
97 return;
98
99 FileLineColLoc fileLoc = extractFileLoc(func.getLoc());
100 auto *file =
101 translateFile(fileLoc ? fileLoc.getFilename().strref() : "<unknown>");
102 unsigned line = fileLoc ? fileLoc.getLine() : 0;
103
104 // TODO: This is the bare essentials for now. We will likely end
105 // up with wrapper metadata around LLVMs metadata in the future, so this
106 // doesn't need to be smart until then.
107 llvm::DISubroutineType *type =
108 builder.createSubroutineType(builder.getOrCreateTypeArray(llvm::None));
109 llvm::DISubprogram::DISPFlags spFlags = llvm::DISubprogram::SPFlagDefinition |
110 llvm::DISubprogram::SPFlagOptimized;
111 llvm::DISubprogram *program =
112 builder.createFunction(compileUnit, func.getName(), func.getName(), file,
113 line, type, line, llvm::DINode::FlagZero, spFlags);
114 llvmFunc.setSubprogram(program);
115 builder.finalizeSubprogram(program);
116 }
117
118 //===----------------------------------------------------------------------===//
119 // Locations
120 //===----------------------------------------------------------------------===//
121
122 /// Translate the given location to an llvm debug location.
123 const llvm::DILocation *
translateLoc(Location loc,llvm::DILocalScope * scope)124 DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope) {
125 if (!compileUnit)
126 return nullptr;
127 return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
128 }
129
130 /// Translate the given location to an llvm DebugLoc.
131 const llvm::DILocation *
translateLoc(Location loc,llvm::DILocalScope * scope,const llvm::DILocation * inlinedAt)132 DebugTranslation::translateLoc(Location loc, llvm::DILocalScope *scope,
133 const llvm::DILocation *inlinedAt) {
134 // LLVM doesn't have a representation for unknown.
135 if (!scope || loc.isa<UnknownLoc>())
136 return nullptr;
137
138 // Check for a cached instance.
139 auto existingIt = locationToLoc.find(std::make_pair(loc, scope));
140 if (existingIt != locationToLoc.end())
141 return existingIt->second;
142
143 const llvm::DILocation *llvmLoc = nullptr;
144 if (auto callLoc = loc.dyn_cast<CallSiteLoc>()) {
145 // For callsites, the caller is fed as the inlinedAt for the callee.
146 const auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
147 llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
148
149 } else if (auto fileLoc = loc.dyn_cast<FileLineColLoc>()) {
150 auto *file = translateFile(fileLoc.getFilename());
151 auto *fileScope = builder.createLexicalBlockFile(scope, file);
152 llvmLoc = llvm::DILocation::get(llvmCtx, fileLoc.getLine(),
153 fileLoc.getColumn(), fileScope,
154 const_cast<llvm::DILocation *>(inlinedAt));
155
156 } else if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
157 ArrayRef<Location> locations = fusedLoc.getLocations();
158
159 // For fused locations, merge each of the nodes.
160 llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
161 for (Location locIt : locations.drop_front()) {
162 llvmLoc = llvm::DILocation::getMergedLocation(
163 llvmLoc, translateLoc(locIt, scope, inlinedAt));
164 }
165
166 } else if (auto nameLoc = loc.dyn_cast<NameLoc>()) {
167 llvmLoc = translateLoc(loc.cast<NameLoc>().getChildLoc(), scope, inlinedAt);
168
169 } else if (auto opaqueLoc = loc.dyn_cast<OpaqueLoc>()) {
170 llvmLoc = translateLoc(loc.cast<OpaqueLoc>().getFallbackLocation(), scope,
171 inlinedAt);
172 } else {
173 llvm_unreachable("unknown location kind");
174 }
175
176 locationToLoc.try_emplace(std::make_pair(loc, scope), llvmLoc);
177 return llvmLoc;
178 }
179
180 /// Create an llvm debug file for the given file path.
translateFile(StringRef fileName)181 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
182 auto *&file = fileMap[fileName];
183 if (file)
184 return file;
185
186 // Make sure the current working directory is up-to-date.
187 if (currentWorkingDir.empty())
188 llvm::sys::fs::current_path(currentWorkingDir);
189
190 StringRef directory = currentWorkingDir;
191 SmallString<128> dirBuf;
192 SmallString<128> fileBuf;
193 if (llvm::sys::path::is_absolute(fileName)) {
194 // Strip the common prefix (if it is more than just "/") from current
195 // directory and FileName for a more space-efficient encoding.
196 auto fileIt = llvm::sys::path::begin(fileName);
197 auto fileE = llvm::sys::path::end(fileName);
198 auto curDirIt = llvm::sys::path::begin(directory);
199 auto curDirE = llvm::sys::path::end(directory);
200 for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
201 llvm::sys::path::append(dirBuf, *curDirIt);
202 if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
203 // Don't strip the common prefix if it is only the root "/" since that
204 // would make LLVM diagnostic locations confusing.
205 directory = StringRef();
206 } else {
207 for (; fileIt != fileE; ++fileIt)
208 llvm::sys::path::append(fileBuf, *fileIt);
209 directory = dirBuf;
210 fileName = fileBuf;
211 }
212 }
213 return (file = builder.createFile(fileName, directory));
214 }
215