MLIR  20.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::DIExpression *
87 DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
88  if (!attr)
89  return nullptr;
90  return translateExpression(attr);
91 }
92 
93 llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
94  if (!stringAttr || stringAttr.empty())
95  return nullptr;
96  return llvm::MDString::get(llvmCtx, stringAttr);
97 }
98 
99 llvm::MDTuple *
100 DebugTranslation::getMDTupleOrNull(ArrayRef<DINodeAttr> elements) {
101  if (elements.empty())
102  return nullptr;
103  SmallVector<llvm::Metadata *> llvmElements = llvm::to_vector(
104  llvm::map_range(elements, [&](DINodeAttr attr) -> llvm::Metadata * {
105  return translate(attr);
106  }));
107  return llvm::MDNode::get(llvmCtx, llvmElements);
108 }
109 
110 llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
111  return llvm::DIBasicType::get(
112  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
113  attr.getSizeInBits(),
114  /*AlignInBits=*/0, attr.getEncoding(), llvm::DINode::FlagZero);
115 }
116 
117 llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
118  llvm::DIBuilder builder(llvmModule);
119  return builder.createCompileUnit(
120  attr.getSourceLanguage(), translate(attr.getFile()),
121  attr.getProducer() ? attr.getProducer().getValue() : "",
122  attr.getIsOptimized(),
123  /*Flags=*/"", /*RV=*/0, /*SplitName=*/{},
124  static_cast<llvm::DICompileUnit::DebugEmissionKind>(
125  attr.getEmissionKind()),
126  0, true, false,
127  static_cast<llvm::DICompileUnit::DebugNameTableKind>(
128  attr.getNameTableKind()));
129 }
130 
131 /// Returns a new `DINodeT` that is either distinct or not, depending on
132 /// `isDistinct`.
133 template <class DINodeT, class... Ts>
134 static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) {
135  if (isDistinct)
136  return DINodeT::getDistinct(std::forward<Ts>(args)...);
137  return DINodeT::get(std::forward<Ts>(args)...);
138 }
139 
140 llvm::TempDICompositeType
141 DebugTranslation::translateTemporaryImpl(DICompositeTypeAttr attr) {
142  return llvm::DICompositeType::getTemporary(
143  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()), nullptr,
144  attr.getLine(), nullptr, nullptr, attr.getSizeInBits(),
145  attr.getAlignInBits(),
146  /*OffsetInBits=*/0,
147  /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
148  /*Elements=*/nullptr, /*RuntimeLang=*/0,
149  /*VTableHolder=*/nullptr);
150 }
151 
152 llvm::TempDISubprogram
153 DebugTranslation::translateTemporaryImpl(DISubprogramAttr attr) {
154  return llvm::DISubprogram::getTemporary(
155  llvmCtx, /*Scope=*/nullptr, /*Name=*/{}, /*LinkageName=*/{},
156  /*File=*/nullptr, attr.getLine(), /*Type=*/nullptr,
157  /*ScopeLine=*/0, /*ContainingType=*/nullptr, /*VirtualIndex=*/0,
158  /*ThisAdjustment=*/0, llvm::DINode::FlagZero,
159  static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
160  /*Unit=*/nullptr);
161 }
162 
163 llvm::DICompositeType *
164 DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
165  // TODO: Use distinct attributes to model this, once they have landed.
166  // Depending on the tag, composite types must be distinct.
167  bool isDistinct = false;
168  switch (attr.getTag()) {
169  case llvm::dwarf::DW_TAG_class_type:
170  case llvm::dwarf::DW_TAG_enumeration_type:
171  case llvm::dwarf::DW_TAG_structure_type:
172  case llvm::dwarf::DW_TAG_union_type:
173  isDistinct = true;
174  }
175 
176  return getDistinctOrUnique<llvm::DICompositeType>(
177  isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
178  translate(attr.getFile()), attr.getLine(), translate(attr.getScope()),
179  translate(attr.getBaseType()), attr.getSizeInBits(),
180  attr.getAlignInBits(),
181  /*OffsetInBits=*/0,
182  /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
183  getMDTupleOrNull(attr.getElements()),
184  /*RuntimeLang=*/0, /*VTableHolder=*/nullptr,
185  /*TemplateParams=*/nullptr, /*Identifier=*/nullptr,
186  /*Discriminator=*/nullptr,
187  getExpressionAttrOrNull(attr.getDataLocation()),
188  getExpressionAttrOrNull(attr.getAssociated()),
189  getExpressionAttrOrNull(attr.getAllocated()),
190  getExpressionAttrOrNull(attr.getRank()));
191 }
192 
193 llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
195  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
196  /*File=*/nullptr, /*Line=*/0,
197  /*Scope=*/nullptr, translate(attr.getBaseType()), attr.getSizeInBits(),
198  attr.getAlignInBits(), attr.getOffsetInBits(),
199  attr.getDwarfAddressSpace(), /*PtrAuthData=*/std::nullopt,
200  /*Flags=*/llvm::DINode::FlagZero, translate(attr.getExtraData()));
201 }
202 
203 llvm::DIStringType *DebugTranslation::translateImpl(DIStringTypeAttr attr) {
205  llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
206  translate(attr.getStringLength()),
207  getExpressionAttrOrNull(attr.getStringLengthExp()),
208  getExpressionAttrOrNull(attr.getStringLocationExp()),
209  attr.getSizeInBits(), attr.getAlignInBits(), attr.getEncoding());
210 }
211 
212 llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
213  return llvm::DIFile::get(llvmCtx, getMDStringOrNull(attr.getName()),
214  getMDStringOrNull(attr.getDirectory()));
215 }
216 
217 llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
218  return llvm::DILabel::get(llvmCtx, translate(attr.getScope()),
219  getMDStringOrNull(attr.getName()),
220  translate(attr.getFile()), attr.getLine());
221 }
222 
223 llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
224  return llvm::DILexicalBlock::getDistinct(llvmCtx, translate(attr.getScope()),
225  translate(attr.getFile()),
226  attr.getLine(), attr.getColumn());
227 }
228 
229 llvm::DILexicalBlockFile *
230 DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
231  return llvm::DILexicalBlockFile::getDistinct(
232  llvmCtx, translate(attr.getScope()), translate(attr.getFile()),
233  attr.getDiscriminator());
234 }
235 
236 llvm::DILocalScope *DebugTranslation::translateImpl(DILocalScopeAttr attr) {
237  return cast<llvm::DILocalScope>(translate(DINodeAttr(attr)));
238 }
239 
240 llvm::DIVariable *DebugTranslation::translateImpl(DIVariableAttr attr) {
241  return cast<llvm::DIVariable>(translate(DINodeAttr(attr)));
242 }
243 
244 llvm::DILocalVariable *
245 DebugTranslation::translateImpl(DILocalVariableAttr attr) {
247  llvmCtx, translate(attr.getScope()), getMDStringOrNull(attr.getName()),
248  translate(attr.getFile()), attr.getLine(), translate(attr.getType()),
249  attr.getArg(), static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
250  attr.getAlignInBits(),
251  /*Annotations=*/nullptr);
252 }
253 
254 llvm::DIGlobalVariable *
255 DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
256  return llvm::DIGlobalVariable::getDistinct(
257  llvmCtx, translate(attr.getScope()), getMDStringOrNull(attr.getName()),
258  getMDStringOrNull(attr.getLinkageName()), translate(attr.getFile()),
259  attr.getLine(), translate(attr.getType()), attr.getIsLocalToUnit(),
260  attr.getIsDefined(), nullptr, nullptr, attr.getAlignInBits(), nullptr);
261 }
262 
263 llvm::DINode *
264 DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
265  DistinctAttr recursiveId = attr.getRecId();
266  if (auto *iter = recursiveNodeMap.find(recursiveId);
267  iter != recursiveNodeMap.end()) {
268  return iter->second;
269  }
270  assert(!attr.getIsRecSelf() && "unbound DI recursive self reference");
271 
272  auto setRecursivePlaceholder = [&](llvm::DINode *placeholder) {
273  recursiveNodeMap.try_emplace(recursiveId, placeholder);
274  };
275 
276  llvm::DINode *result =
278  .Case<DICompositeTypeAttr>([&](auto attr) {
279  auto temporary = translateTemporaryImpl(attr);
280  setRecursivePlaceholder(temporary.get());
281  // Must call `translateImpl` directly instead of `translate` to
282  // avoid handling the recursive interface again.
283  auto *concrete = translateImpl(attr);
284  temporary->replaceAllUsesWith(concrete);
285  return concrete;
286  })
287  .Case<DISubprogramAttr>([&](auto attr) {
288  auto temporary = translateTemporaryImpl(attr);
289  setRecursivePlaceholder(temporary.get());
290  // Must call `translateImpl` directly instead of `translate` to
291  // avoid handling the recursive interface again.
292  auto *concrete = translateImpl(attr);
293  temporary->replaceAllUsesWith(concrete);
294  return concrete;
295  });
296 
297  assert(recursiveNodeMap.back().first == recursiveId &&
298  "internal inconsistency: unexpected recursive translation stack");
299  recursiveNodeMap.pop_back();
300 
301  return result;
302 }
303 
304 llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
305  return cast<llvm::DIScope>(translate(DINodeAttr(attr)));
306 }
307 
308 llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
309  if (auto iter = distinctAttrToNode.find(attr.getId());
310  iter != distinctAttrToNode.end())
311  return cast<llvm::DISubprogram>(iter->second);
312 
313  llvm::DIScope *scope = translate(attr.getScope());
314  llvm::DIFile *file = translate(attr.getFile());
315  llvm::DIType *type = translate(attr.getType());
316  llvm::DICompileUnit *compileUnit = translate(attr.getCompileUnit());
317 
318  // Check again after recursive calls in case this distinct node recurses back
319  // to itself.
320  if (auto iter = distinctAttrToNode.find(attr.getId());
321  iter != distinctAttrToNode.end())
322  return cast<llvm::DISubprogram>(iter->second);
323 
324  bool isDefinition = static_cast<bool>(attr.getSubprogramFlags() &
325  LLVM::DISubprogramFlags::Definition);
326 
327  llvm::DISubprogram *node = getDistinctOrUnique<llvm::DISubprogram>(
328  isDefinition, llvmCtx, scope, getMDStringOrNull(attr.getName()),
329  getMDStringOrNull(attr.getLinkageName()), file, attr.getLine(), type,
330  attr.getScopeLine(),
331  /*ContainingType=*/nullptr, /*VirtualIndex=*/0,
332  /*ThisAdjustment=*/0, llvm::DINode::FlagZero,
333  static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
334  compileUnit, /*TemplateParams=*/nullptr, /*Declaration=*/nullptr,
335  getMDTupleOrNull(attr.getRetainedNodes()));
336  if (attr.getId())
337  distinctAttrToNode.try_emplace(attr.getId(), node);
338  return node;
339 }
340 
341 llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
342  return llvm::DIModule::get(
343  llvmCtx, translate(attr.getFile()), translate(attr.getScope()),
344  getMDStringOrNull(attr.getName()),
345  getMDStringOrNull(attr.getConfigMacros()),
346  getMDStringOrNull(attr.getIncludePath()),
347  getMDStringOrNull(attr.getApinotes()), attr.getLine(), attr.getIsDecl());
348 }
349 
350 llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
351  return llvm::DINamespace::get(llvmCtx, translate(attr.getScope()),
352  getMDStringOrNull(attr.getName()),
353  attr.getExportSymbols());
354 }
355 
356 llvm::DIImportedEntity *
357 DebugTranslation::translateImpl(DIImportedEntityAttr attr) {
359  llvmCtx, attr.getTag(), translate(attr.getScope()),
360  translate(attr.getEntity()), translate(attr.getFile()), attr.getLine(),
361  getMDStringOrNull(attr.getName()), getMDTupleOrNull(attr.getElements()));
362 }
363 
364 llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
365  auto getMetadataOrNull = [&](Attribute attr) -> llvm::Metadata * {
366  if (!attr)
367  return nullptr;
368 
369  llvm::Metadata *metadata =
371  .Case([&](IntegerAttr intAttr) {
372  return llvm::ConstantAsMetadata::get(llvm::ConstantInt::getSigned(
373  llvm::Type::getInt64Ty(llvmCtx), intAttr.getInt()));
374  })
375  .Case([&](LLVM::DIExpressionAttr expr) {
376  return translateExpression(expr);
377  })
378  .Case([&](LLVM::DILocalVariableAttr local) {
379  return translate(local);
380  })
381  .Case<>([&](LLVM::DIGlobalVariableAttr global) {
382  return translate(global);
383  })
384  .Default([&](Attribute attr) { return nullptr; });
385  return metadata;
386  };
387  return llvm::DISubrange::get(llvmCtx, getMetadataOrNull(attr.getCount()),
388  getMetadataOrNull(attr.getLowerBound()),
389  getMetadataOrNull(attr.getUpperBound()),
390  getMetadataOrNull(attr.getStride()));
391 }
392 
393 llvm::DISubroutineType *
394 DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
395  // Concatenate the result and argument types into a single array.
397  for (DITypeAttr type : attr.getTypes())
398  types.push_back(translate(type));
400  llvmCtx, llvm::DINode::FlagZero, attr.getCallingConvention(),
401  llvm::DITypeRefArray(llvm::MDNode::get(llvmCtx, types)));
402 }
403 
404 llvm::DIType *DebugTranslation::translateImpl(DITypeAttr attr) {
405  return cast<llvm::DIType>(translate(DINodeAttr(attr)));
406 }
407 
409  if (!attr)
410  return nullptr;
411  // Check for a cached instance.
412  if (llvm::DINode *node = attrToNode.lookup(attr))
413  return node;
414 
415  llvm::DINode *node = nullptr;
416  // Recursive types go through a dedicated handler. All other types are
417  // dispatched directly to their specific handlers.
418  if (auto recTypeAttr = dyn_cast<DIRecursiveTypeAttrInterface>(attr))
419  if (recTypeAttr.getRecId())
420  node = translateRecursive(recTypeAttr);
421 
422  if (!node)
424  .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
425  DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
426  DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
427  DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
428  DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
429  DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
430  [&](auto attr) { return translateImpl(attr); });
431 
432  if (node && !node->isTemporary())
433  attrToNode.insert({attr, node});
434  return node;
435 }
436 
437 //===----------------------------------------------------------------------===//
438 // Locations
439 //===----------------------------------------------------------------------===//
440 
441 /// Translate the given location to an llvm debug location.
442 llvm::DILocation *DebugTranslation::translateLoc(Location loc,
443  llvm::DILocalScope *scope) {
444  if (!debugEmissionIsEnabled)
445  return nullptr;
446  return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
447 }
448 
449 llvm::DIExpression *
450 DebugTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
452  if (attr) {
453  // Append operations their operands to the list.
454  for (const DIExpressionElemAttr &op : attr.getOperations()) {
455  ops.push_back(op.getOpcode());
456  append_range(ops, op.getArguments());
457  }
458  }
459  return llvm::DIExpression::get(llvmCtx, ops);
460 }
461 
462 llvm::DIGlobalVariableExpression *
463 DebugTranslation::translateGlobalVariableExpression(
464  LLVM::DIGlobalVariableExpressionAttr attr) {
466  llvmCtx, translate(attr.getVar()), translateExpression(attr.getExpr()));
467 }
468 
469 /// Translate the given location to an llvm DebugLoc.
470 llvm::DILocation *DebugTranslation::translateLoc(Location loc,
471  llvm::DILocalScope *scope,
472  llvm::DILocation *inlinedAt) {
473  // LLVM doesn't have a representation for unknown.
474  if (isa<UnknownLoc>(loc))
475  return nullptr;
476 
477  // Check for a cached instance.
478  auto existingIt = locationToLoc.find(std::make_tuple(loc, scope, inlinedAt));
479  if (existingIt != locationToLoc.end())
480  return existingIt->second;
481 
482  llvm::DILocation *llvmLoc = nullptr;
483  if (auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
484  // For callsites, the caller is fed as the inlinedAt for the callee.
485  auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
486  // If the caller scope is not translatable, the overall callsite cannot be
487  // represented in LLVM (the callee scope may not match the parent function).
488  if (!callerLoc) {
489  // If there is an inlinedAt scope (an outer caller), skip to that
490  // directly. Otherwise, cannot translate.
491  if (!inlinedAt)
492  return nullptr;
493  callerLoc = inlinedAt;
494  }
495  llvmLoc = translateLoc(callLoc.getCallee(), nullptr, callerLoc);
496  // Fallback: Ignore callee if it has no debug scope.
497  if (!llvmLoc)
498  llvmLoc = callerLoc;
499 
500  } else if (auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
501  // A scope of a DILocation cannot be null.
502  if (!scope)
503  return nullptr;
504  llvmLoc =
505  llvm::DILocation::get(llvmCtx, fileLoc.getLine(), fileLoc.getColumn(),
506  scope, const_cast<llvm::DILocation *>(inlinedAt));
507 
508  } else if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
509  ArrayRef<Location> locations = fusedLoc.getLocations();
510 
511  // Check for a scope encoded with the location.
512  if (auto scopedAttr =
513  dyn_cast_or_null<LLVM::DILocalScopeAttr>(fusedLoc.getMetadata()))
514  scope = translate(scopedAttr);
515 
516  // For fused locations, merge each of the nodes.
517  llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
518  for (Location locIt : locations.drop_front()) {
519  llvmLoc = llvm::DILocation::getMergedLocation(
520  llvmLoc, translateLoc(locIt, scope, inlinedAt));
521  }
522 
523  } else if (auto nameLoc = dyn_cast<NameLoc>(loc)) {
524  llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);
525 
526  } else if (auto opaqueLoc = dyn_cast<OpaqueLoc>(loc)) {
527  llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
528  } else {
529  llvm_unreachable("unknown location kind");
530  }
531 
532  locationToLoc.try_emplace(std::make_tuple(loc, scope, inlinedAt), llvmLoc);
533  return llvmLoc;
534 }
535 
536 /// Create an llvm debug file for the given file path.
537 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
538  auto *&file = fileMap[fileName];
539  if (file)
540  return file;
541 
542  // Make sure the current working directory is up-to-date.
543  if (currentWorkingDir.empty())
544  llvm::sys::fs::current_path(currentWorkingDir);
545 
546  StringRef directory = currentWorkingDir;
547  SmallString<128> dirBuf;
548  SmallString<128> fileBuf;
549  if (llvm::sys::path::is_absolute(fileName)) {
550  // Strip the common prefix (if it is more than just "/") from current
551  // directory and FileName for a more space-efficient encoding.
552  auto fileIt = llvm::sys::path::begin(fileName);
553  auto fileE = llvm::sys::path::end(fileName);
554  auto curDirIt = llvm::sys::path::begin(directory);
555  auto curDirE = llvm::sys::path::end(directory);
556  for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
557  llvm::sys::path::append(dirBuf, *curDirIt);
558  if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
559  // Don't strip the common prefix if it is only the root "/" since that
560  // would make LLVM diagnostic locations confusing.
561  directory = StringRef();
562  } else {
563  for (; fileIt != fileE; ++fileIt)
564  llvm::sys::path::append(fileBuf, *fileIt);
565  directory = dirBuf;
566  fileName = fileBuf;
567  }
568  }
569  return (file = llvm::DIFile::get(llvmCtx, fileName, directory));
570 }
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)
Attributes are known-constant values of operations.
Definition: Attributes.h:25
An attribute that associates a referenced attribute with a unique identifier.
This class represents a fused location whose metadata is known to be an instance of the given type.
Definition: Location.h:148
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
This class represents a LLVM attribute that describes a debug info variable.
Definition: LLVMAttrs.h:64
void finalize()
Finalize the translation of debug information.
void translate(LLVMFuncOp func, llvm::Function &llvmFunc)
Translate the debug information for the given function.
llvm::DIExpression * translateExpression(LLVM::DIExpressionAttr attr)
Translates the given DWARF expression metadata to to LLVM.
DebugTranslation(Operation *module, llvm::Module &llvmModule)
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:793
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:33
static WalkResult advance()
Definition: Visitors.h:51
static WalkResult interrupt()
Definition: Visitors.h:50
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...