MLIR  18.0.0git
DebugTranslation.cpp
Go to the documentation of this file.
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"
11 #include "llvm/ADT/TypeSwitch.h"
12 #include "llvm/IR/Metadata.h"
13 #include "llvm/IR/Module.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace mlir;
18 using namespace mlir::LLVM;
19 using namespace mlir::LLVM::detail;
20 
21 /// A utility walker that interrupts if the operation has valid debug
22 /// information.
24  return isa<UnknownLoc>(op->getLoc()) ? WalkResult::advance()
26 }
27 
28 DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
29  : debugEmissionIsEnabled(false), llvmModule(llvmModule),
30  llvmCtx(llvmModule.getContext()) {
31  // If the module has no location information, there is nothing to do.
32  if (!module->walk(interruptIfValidLocation).wasInterrupted())
33  return;
34  debugEmissionIsEnabled = true;
35 
36  // TODO: The version information should be encoded on the LLVM module itself,
37  // not implicitly set here.
38 
39  // Mark this module as having debug information.
40  StringRef debugVersionKey = "Debug Info Version";
41  if (!llvmModule.getModuleFlag(debugVersionKey))
42  llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
43  llvm::DEBUG_METADATA_VERSION);
44 
45  if (auto targetTripleAttr = module->getDiscardableAttr(
46  LLVM::LLVMDialect::getTargetTripleAttrName())) {
47  auto targetTriple =
48  llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
49  if (targetTriple.isKnownWindowsMSVCEnvironment()) {
50  // Dwarf debugging files will be generated by default, unless "CodeView"
51  // is set explicitly. Windows/MSVC should use CodeView instead.
52  llvmModule.addModuleFlag(llvm::Module::Warning, "CodeView", 1);
53  }
54  }
55 }
56 
57 /// Finalize the translation of debug information.
59 
60 /// Translate the debug information for the given function.
61 void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
62  if (!debugEmissionIsEnabled)
63  return;
64 
65  // Look for a sub program attached to the function.
66  auto spLoc =
67  func.getLoc()->findInstanceOf<FusedLocWith<LLVM::DISubprogramAttr>>();
68  if (!spLoc)
69  return;
70  llvmFunc.setSubprogram(translate(spLoc.getMetadata()));
71 }
72 
73 //===----------------------------------------------------------------------===//
74 // Attributes
75 //===----------------------------------------------------------------------===//
76 
77 llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
78  // A DINullTypeAttr at the beginning of the subroutine types list models
79  // a void result type. If it is at the end, it models a variadic function.
80  // Translate the explicit DINullTypeAttr to a nullptr since LLVM IR metadata
81  // does not have an explicit void result type nor a variadic type
82  // representation.
83  return nullptr;
84 }
85 
86 llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
87  if (!stringAttr || stringAttr.empty())
88  return nullptr;
89  return llvm::MDString::get(llvmCtx, stringAttr);
90 }
91 
92 llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
94  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
95  attr.getSizeInBits(),
96  /*AlignInBits=*/0, attr.getEncoding(), llvm::DINode::FlagZero);
97 }
98 
99 llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
100  llvm::DIBuilder builder(llvmModule);
101  return builder.createCompileUnit(
102  attr.getSourceLanguage(), translate(attr.getFile()),
103  attr.getProducer() ? attr.getProducer().getValue() : "",
104  attr.getIsOptimized(),
105  /*Flags=*/"", /*RV=*/0);
106 }
107 
108 /// Returns a new `DINodeT` that is either distinct or not, depending on
109 /// `isDistinct`.
110 template <class DINodeT, class... Ts>
111 static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) {
112  if (isDistinct)
113  return DINodeT::getDistinct(std::forward<Ts>(args)...);
114  return DINodeT::get(std::forward<Ts>(args)...);
115 }
116 
117 llvm::DICompositeType *
118 DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
120  for (auto member : attr.getElements())
121  elements.push_back(translate(member));
122 
123  // TODO: Use distinct attributes to model this, once they have landed.
124  // Depending on the tag, composite types must be distinct.
125  bool isDistinct = false;
126  switch (attr.getTag()) {
127  case llvm::dwarf::DW_TAG_class_type:
128  case llvm::dwarf::DW_TAG_enumeration_type:
129  case llvm::dwarf::DW_TAG_structure_type:
130  case llvm::dwarf::DW_TAG_union_type:
131  isDistinct = true;
132  }
133 
134  return getDistinctOrUnique<llvm::DICompositeType>(
135  isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
136  translate(attr.getFile()), attr.getLine(), translate(attr.getScope()),
137  translate(attr.getBaseType()), attr.getSizeInBits(),
138  attr.getAlignInBits(),
139  /*OffsetInBits=*/0,
140  /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
141  llvm::MDNode::get(llvmCtx, elements),
142  /*RuntimeLang=*/0, /*VTableHolder=*/nullptr);
143 }
144 
145 llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
147  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
148  /*File=*/nullptr, /*Line=*/0,
149  /*Scope=*/nullptr, translate(attr.getBaseType()), attr.getSizeInBits(),
150  attr.getAlignInBits(), attr.getOffsetInBits(),
151  /*DWARFAddressSpace=*/std::nullopt, /*Flags=*/llvm::DINode::FlagZero);
152 }
153 
154 llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
155  return llvm::DIFile::get(llvmCtx, getMDStringOrNull(attr.getName()),
156  getMDStringOrNull(attr.getDirectory()));
157 }
158 
159 llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
160  return llvm::DILabel::get(llvmCtx, translate(attr.getScope()),
161  getMDStringOrNull(attr.getName()),
162  translate(attr.getFile()), attr.getLine());
163 }
164 
165 llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
166  return llvm::DILexicalBlock::getDistinct(llvmCtx, translate(attr.getScope()),
167  translate(attr.getFile()),
168  attr.getLine(), attr.getColumn());
169 }
170 
171 llvm::DILexicalBlockFile *
172 DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
173  return llvm::DILexicalBlockFile::getDistinct(
174  llvmCtx, translate(attr.getScope()), translate(attr.getFile()),
175  attr.getDiscriminator());
176 }
177 
178 llvm::DILocalScope *DebugTranslation::translateImpl(DILocalScopeAttr attr) {
179  return cast<llvm::DILocalScope>(translate(DINodeAttr(attr)));
180 }
181 
182 llvm::DILocalVariable *
183 DebugTranslation::translateImpl(DILocalVariableAttr attr) {
185  llvmCtx, translate(attr.getScope()), getMDStringOrNull(attr.getName()),
186  translate(attr.getFile()), attr.getLine(), translate(attr.getType()),
187  attr.getArg(),
188  /*Flags=*/llvm::DINode::FlagZero, attr.getAlignInBits(),
189  /*Annotations=*/nullptr);
190 }
191 
192 llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
193  return cast<llvm::DIScope>(translate(DINodeAttr(attr)));
194 }
195 
196 llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
197  bool isDefinition = static_cast<bool>(attr.getSubprogramFlags() &
198  LLVM::DISubprogramFlags::Definition);
199  return getDistinctOrUnique<llvm::DISubprogram>(
200  isDefinition, llvmCtx, translate(attr.getScope()),
201  getMDStringOrNull(attr.getName()),
202  getMDStringOrNull(attr.getLinkageName()), translate(attr.getFile()),
203  attr.getLine(), translate(attr.getType()), attr.getScopeLine(),
204  /*ContainingType=*/nullptr, /*VirtualIndex=*/0,
205  /*ThisAdjustment=*/0, llvm::DINode::FlagZero,
206  static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
207  translate(attr.getCompileUnit()));
208 }
209 
210 llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
211  return llvm::DIModule::get(
212  llvmCtx, translate(attr.getFile()), translate(attr.getScope()),
213  getMDStringOrNull(attr.getName()),
214  getMDStringOrNull(attr.getConfigMacros()),
215  getMDStringOrNull(attr.getIncludePath()),
216  getMDStringOrNull(attr.getApinotes()), attr.getLine(), attr.getIsDecl());
217 }
218 
219 llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
220  return llvm::DINamespace::get(llvmCtx, translate(attr.getScope()),
221  getMDStringOrNull(attr.getName()),
222  attr.getExportSymbols());
223 }
224 
225 llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
226  auto getMetadataOrNull = [&](IntegerAttr attr) -> llvm::Metadata * {
227  if (!attr)
228  return nullptr;
229  return llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
230  llvm::Type::getInt64Ty(llvmCtx), attr.getInt()));
231  };
232  return llvm::DISubrange::get(llvmCtx, getMetadataOrNull(attr.getCount()),
233  getMetadataOrNull(attr.getLowerBound()),
234  getMetadataOrNull(attr.getUpperBound()),
235  getMetadataOrNull(attr.getStride()));
236 }
237 
238 llvm::DISubroutineType *
239 DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
240  // Concatenate the result and argument types into a single array.
242  for (DITypeAttr type : attr.getTypes())
243  types.push_back(translate(type));
245  llvmCtx, llvm::DINode::FlagZero, attr.getCallingConvention(),
246  llvm::DITypeRefArray(llvm::MDNode::get(llvmCtx, types)));
247 }
248 
249 llvm::DIType *DebugTranslation::translateImpl(DITypeAttr attr) {
250  return cast<llvm::DIType>(translate(DINodeAttr(attr)));
251 }
252 
254  if (!attr)
255  return nullptr;
256  // Check for a cached instance.
257  if (llvm::DINode *node = attrToNode.lookup(attr))
258  return node;
259 
260  llvm::DINode *node =
262  .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
263  DIDerivedTypeAttr, DIFileAttr, DILabelAttr, DILexicalBlockAttr,
264  DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
265  DINamespaceAttr, DINullTypeAttr, DISubprogramAttr,
266  DISubrangeAttr, DISubroutineTypeAttr>(
267  [&](auto attr) { return translateImpl(attr); });
268  attrToNode.insert({attr, node});
269  return node;
270 }
271 
272 //===----------------------------------------------------------------------===//
273 // Locations
274 //===----------------------------------------------------------------------===//
275 
276 /// Translate the given location to an llvm debug location.
278  llvm::DILocalScope *scope) {
279  if (!debugEmissionIsEnabled)
280  return nullptr;
281  return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
282 }
283 
284 /// Translate the given location to an llvm DebugLoc.
285 llvm::DILocation *DebugTranslation::translateLoc(Location loc,
286  llvm::DILocalScope *scope,
287  llvm::DILocation *inlinedAt) {
288  // LLVM doesn't have a representation for unknown.
289  if (isa<UnknownLoc>(loc))
290  return nullptr;
291 
292  // Check for a cached instance.
293  auto existingIt = locationToLoc.find(std::make_tuple(loc, scope, inlinedAt));
294  if (existingIt != locationToLoc.end())
295  return existingIt->second;
296 
297  llvm::DILocation *llvmLoc = nullptr;
298  if (auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
299  // For callsites, the caller is fed as the inlinedAt for the callee.
300  auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
301  llvmLoc = translateLoc(callLoc.getCallee(), scope, callerLoc);
302 
303  } else if (auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
304  // A scope of a DILocation cannot be null.
305  if (!scope)
306  return nullptr;
307  llvmLoc =
308  llvm::DILocation::get(llvmCtx, fileLoc.getLine(), fileLoc.getColumn(),
309  scope, const_cast<llvm::DILocation *>(inlinedAt));
310 
311  } else if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
312  ArrayRef<Location> locations = fusedLoc.getLocations();
313 
314  // Check for a scope encoded with the location.
315  if (auto scopedAttr =
316  dyn_cast_or_null<LLVM::DILocalScopeAttr>(fusedLoc.getMetadata()))
317  scope = translate(scopedAttr);
318 
319  // For fused locations, merge each of the nodes.
320  llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
321  for (Location locIt : locations.drop_front()) {
322  llvmLoc = llvm::DILocation::getMergedLocation(
323  llvmLoc, translateLoc(locIt, scope, inlinedAt));
324  }
325 
326  } else if (auto nameLoc = dyn_cast<NameLoc>(loc)) {
327  llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);
328 
329  } else if (auto opaqueLoc = dyn_cast<OpaqueLoc>(loc)) {
330  llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
331  } else {
332  llvm_unreachable("unknown location kind");
333  }
334 
335  locationToLoc.try_emplace(std::make_tuple(loc, scope, inlinedAt), llvmLoc);
336  return llvmLoc;
337 }
338 
339 /// Create an llvm debug file for the given file path.
340 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
341  auto *&file = fileMap[fileName];
342  if (file)
343  return file;
344 
345  // Make sure the current working directory is up-to-date.
346  if (currentWorkingDir.empty())
347  llvm::sys::fs::current_path(currentWorkingDir);
348 
349  StringRef directory = currentWorkingDir;
350  SmallString<128> dirBuf;
351  SmallString<128> fileBuf;
352  if (llvm::sys::path::is_absolute(fileName)) {
353  // Strip the common prefix (if it is more than just "/") from current
354  // directory and FileName for a more space-efficient encoding.
355  auto fileIt = llvm::sys::path::begin(fileName);
356  auto fileE = llvm::sys::path::end(fileName);
357  auto curDirIt = llvm::sys::path::begin(directory);
358  auto curDirE = llvm::sys::path::end(directory);
359  for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
360  llvm::sys::path::append(dirBuf, *curDirIt);
361  if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
362  // Don't strip the common prefix if it is only the root "/" since that
363  // would make LLVM diagnostic locations confusing.
364  directory = StringRef();
365  } else {
366  for (; fileIt != fileE; ++fileIt)
367  llvm::sys::path::append(fileBuf, *fileIt);
368  directory = dirBuf;
369  fileName = fileBuf;
370  }
371  }
372  return (file = llvm::DIFile::get(llvmCtx, fileName, directory));
373 }
static DINodeT * getDistinctOrUnique(bool isDistinct, Ts &&...args)
Returns a new DINodeT that is either distinct or not, depending on isDistinct.
static WalkResult interruptIfValidLocation(Operation *op)
A utility walker that interrupts if the operation has valid debug information.
static MLIRContext * getContext(OpFoldResult val)
This class represents a fused location whose metadata is known to be an instance of the given type.
Definition: Location.h:145
This class represents a LLVM attribute that describes a local debug info scope.
Definition: LLVMAttrs.h:46
This class represents the base attribute for all debug info attributes.
Definition: LLVMAttrs.h:27
This class represents a LLVM attribute that describes a debug info scope.
Definition: LLVMAttrs.h:36
This class represents a LLVM attribute that describes a debug info type.
Definition: LLVMAttrs.h:55
void finalize()
Finalize the translation of debug information.
void translate(LLVMFuncOp func, llvm::Function &llvmFunc)
Translate the debug information for the given function.
DebugTranslation(Operation *module, llvm::Module &llvmModule)
llvm::DILocation * translateLoc(Location loc, llvm::DILocalScope *scope)
Translate the given location to an llvm debug location.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Attribute getDiscardableAttr(StringRef name)
Access a discardable attribute by name, returns an null Attribute if the discardable attribute does n...
Definition: Operation.h:448
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Definition: Operation.h:776
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:223
A utility result that is used to signal how to proceed with an ongoing walk:
Definition: Visitors.h:34
static WalkResult advance()
Definition: Visitors.h:52
static WalkResult interrupt()
Definition: Visitors.h:51
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...