14#include "llvm/ADT/STLExtras.h"
15#include "llvm/ADT/TypeSwitch.h"
16#include "llvm/BinaryFormat/Dwarf.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/DebugInfoMetadata.h"
19#include "llvm/IR/Metadata.h"
26 bool dropDICompositeTypeElements)
27 : cache([&](
llvm::DINode *node) {
return createRecSelf(node); }),
28 context(mlirModule.getContext()), mlirModule(mlirModule),
29 dropDICompositeTypeElements(dropDICompositeTypeElements) {}
32 llvm::DISubprogram *subprogram =
func->getSubprogram();
34 return UnknownLoc::get(context);
37 StringAttr fileName = StringAttr::get(context, subprogram->getFilename());
47DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
48 return DIBasicTypeAttr::get(context, node->getTag(),
49 getStringAttrOrNull(node->getRawName()),
50 node->getSizeInBits(), node->getEncoding());
53DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
54 std::optional<DIEmissionKind> emissionKind =
55 symbolizeDIEmissionKind(node->getEmissionKind());
56 std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind(
58 std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>
>(
59 node->getNameTableKind()));
61 if (node->getImportedEntities()) {
62 for (llvm::DIImportedEntity *importedEntity : node->getImportedEntities())
64 translate(
static_cast<llvm::DINode *
>(importedEntity)))
65 imports.push_back(nodeAttr);
67 llvm::DISourceLanguageName sourceLanguage = node->getSourceLanguage();
68 DISourceLanguageNameAttr sourceLanguageAttr;
69 if (sourceLanguage.hasVersionedName()) {
70 sourceLanguageAttr = DISourceLanguageNameAttr::get(
71 context, 0, sourceLanguage.getName(),
72 sourceLanguage.getVersion(), sourceLanguage.getDialect());
74 sourceLanguageAttr = DISourceLanguageNameAttr::get(
75 context, sourceLanguage.getName(), 0,
76 std::nullopt, sourceLanguage.getDialect());
78 return DICompileUnitAttr::get(
79 context, DistinctAttr{},
false,
80 getOrCreateDistinctID(node), sourceLanguageAttr,
81 translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
82 node->isOptimized(), emissionKind.value(),
83 node->isDebugInfoForProfiling(), nameTableKind.value(),
84 getStringAttrOrNull(node->getRawSplitDebugFilename()), imports);
87DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
88 std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
89 SmallVector<DINodeAttr> elements;
92 bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
93 if (isVectorType || !dropDICompositeTypeElements) {
94 for (llvm::DINode *element : node->getElements()) {
95 assert(element &&
"expected a non-null element type");
100 if (llvm::is_contained(elements,
nullptr))
102 DITypeAttr baseType =
translate(node->getBaseType());
105 if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
107 return DICompositeTypeAttr::get(
108 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
110 baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
115 getStringAttrOrNull(node->getRawIdentifier()),
116 translate(node->getDiscriminator()), elements);
119DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
121 DITypeAttr baseType =
translate(node->getBaseType());
122 if (node->getBaseType() && !baseType)
124 llvm::Metadata *rawExtraData = node->getExtraData();
126 if (
auto *extraDataNode = dyn_cast_or_null<llvm::DINode>(rawExtraData)) {
128 }
else if (
auto *constantAsMetadata =
129 dyn_cast_or_null<llvm::ConstantAsMetadata>(rawExtraData)) {
130 if (
auto *constantInt =
131 dyn_cast<llvm::ConstantInt>(constantAsMetadata->getValue())) {
132 const APInt &value = constantInt->getValue();
133 extraData = IntegerAttr::get(
134 IntegerType::get(context, value.getBitWidth()), value);
137 return DIDerivedTypeAttr::get(
138 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
140 baseType, node->getSizeInBits(), node->getAlignInBits(),
141 node->getOffsetInBits(), node->getDWARFAddressSpace(),
142 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero), extraData);
145DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
146 return DIStringTypeAttr::get(
147 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
148 node->getSizeInBits(), node->getAlignInBits(),
155DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
156 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
159DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
161 DIScopeAttr scope =
translate(node->getScope());
162 if (node->getScope() && !scope)
164 return DILabelAttr::get(context, scope,
165 getStringAttrOrNull(node->getRawName()),
166 translate(node->getFile()), node->getLine());
169DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
171 DIScopeAttr scope =
translate(node->getScope());
172 if (node->getScope() && !scope)
174 return DILexicalBlockAttr::get(context, scope,
translate(node->getFile()),
175 node->getLine(), node->getColumn());
178DILexicalBlockFileAttr
179DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
181 DIScopeAttr scope =
translate(node->getScope());
182 if (node->getScope() && !scope)
184 return DILexicalBlockFileAttr::get(context, scope,
translate(node->getFile()),
185 node->getDiscriminator());
189DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
192 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
195 return StringAttr::get(context, node->getName());
197 return DIGlobalVariableAttr::get(
199 convertToStringAttr(node->getName()),
200 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
201 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
202 node->isDefinition(), node->getAlignInBits());
205DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
207 DIScopeAttr scope =
translate(node->getScope());
208 if (node->getScope() && !scope)
210 return DILocalVariableAttr::get(
211 context, scope, getStringAttrOrNull(node->getRawName()),
212 translate(node->getFile()), node->getLine(), node->getArg(),
213 node->getAlignInBits(),
translate(node->getType()),
214 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
217DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
218 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
221DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
222 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
225DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
226 return DIModuleAttr::get(
228 getStringAttrOrNull(node->getRawName()),
229 getStringAttrOrNull(node->getRawConfigurationMacros()),
230 getStringAttrOrNull(node->getRawIncludePath()),
231 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
235DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
236 return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
238 node->getExportSymbols());
242DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
243 SmallVector<DINodeAttr> elements;
244 for (llvm::DINode *element : node->getElements()) {
245 assert(element &&
"expected a non-null element type");
249 return DIImportedEntityAttr::get(
250 context, node->getTag(),
translate(node->getScope()),
252 getStringAttrOrNull(node->getRawName()), elements);
255DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
257 mlir::DistinctAttr id;
258 if (node->isDistinct())
259 id = getOrCreateDistinctID(node);
262 DIScopeAttr scope =
translate(node->getScope());
263 if (node->getScope() && !scope)
265 std::optional<DISubprogramFlags> subprogramFlags =
266 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
267 assert(subprogramFlags &&
"expected valid subprogram flags");
268 DISubroutineTypeAttr type =
translate(node->getType());
269 if (node->getType() && !type)
273 SmallVector<Attribute> retainedNodes;
274 auto add = [
this, &retainedNodes](llvm::DINode *retainedNode) {
275 retainedNodes.push_back(
translate(retainedNode));
277 auto addGVE = [](llvm::DIGlobalVariableExpression *GVE) {
282 if (llvm::is_contained(retainedNodes,
nullptr))
283 retainedNodes.clear();
285 SmallVector<DINodeAttr> annotations;
289 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
290 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
291 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
292 if (tuple->getNumOperands() != 2)
294 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
295 const llvm::MDString *value =
296 dyn_cast<llvm::MDString>(tuple->getOperand(1));
298 annotations.push_back(DIAnnotationAttr::get(
299 context, StringAttr::get(context, name->getString()),
300 StringAttr::get(context, value->getString())));
305 return DISubprogramAttr::get(context,
id,
translate(node->getUnit()), scope,
306 getStringAttrOrNull(node->getRawName()),
307 getStringAttrOrNull(node->getRawLinkageName()),
308 translate(node->getFile()), node->getLine(),
309 node->getScopeLine(), *subprogramFlags, type,
310 retainedNodes, annotations);
313DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
314 auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
317 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
318 return IntegerAttr::get(IntegerType::get(context, 64),
319 constInt->getSExtValue());
320 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
322 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
323 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
325 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
331 Attribute count = getAttrOrNull(node->getCount());
332 Attribute upperBound = getAttrOrNull(node->getUpperBound());
335 if (!count && !upperBound)
337 return DISubrangeAttr::get(context, count,
338 getAttrOrNull(node->getLowerBound()), upperBound,
339 getAttrOrNull(node->getStride()));
342DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
343 return DICommonBlockAttr::get(context,
translate(node->getScope()),
345 getStringAttrOrNull(node->getRawName()),
346 translate(node->getFile()), node->getLineNo());
350DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
352 [&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
355 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
357 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
358 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
360 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
366 Attribute count = getAttrOrNull(node->getCount());
367 Attribute upperBound = getAttrOrNull(node->getUpperBound());
368 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
369 Attribute stride = getAttrOrNull(node->getStride());
372 if (!count && !upperBound)
374 return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
379DebugImporter::translateImpl(llvm::DISubroutineType *node) {
380 SmallVector<DITypeAttr> types;
381 for (llvm::DIType *type : node->getTypeArray()) {
387 types.push_back(DINullTypeAttr::get(context));
393 if (llvm::is_contained(types,
nullptr))
395 return DISubroutineTypeAttr::get(context, node->getCC(), types);
398DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
399 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
407 auto cacheEntry = cache.lookupOrInit(node);
408 if (std::optional<DINodeAttr>
result = cacheEntry.get())
412 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
413 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
414 return translateImpl(casted);
415 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
416 return translateImpl(casted);
417 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
418 return translateImpl(casted);
419 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
420 return translateImpl(casted);
421 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
422 return translateImpl(casted);
423 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
424 return translateImpl(casted);
425 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
426 return translateImpl(casted);
427 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
428 return translateImpl(casted);
429 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
430 return translateImpl(casted);
431 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
432 return translateImpl(casted);
433 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
434 return translateImpl(casted);
435 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
436 return translateImpl(casted);
437 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
438 return translateImpl(casted);
439 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
440 return translateImpl(casted);
441 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
442 return translateImpl(casted);
443 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
444 return translateImpl(casted);
445 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
446 return translateImpl(casted);
447 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
448 return translateImpl(casted);
449 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
450 return translateImpl(casted);
456 if (cacheEntry.wasRepeated()) {
458 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
459 attr = cast<DINodeAttr>(recType.withRecId(recId));
461 cacheEntry.resolve(attr);
464 cacheEntry.resolve(
nullptr);
475 .Case([&](llvm::DICompositeType *) {
476 return CtorType(DICompositeTypeAttr::getRecSelf);
478 .Case([&](llvm::DISubprogram *) {
479 return CtorType(DISubprogramAttr::getRecSelf);
481 .Case([&](llvm::DICompileUnit *) {
482 return CtorType(DICompileUnitAttr::getRecSelf);
484 .Default(CtorType());
487std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
494 DistinctAttr recId = nodeToRecId.lookup(node);
497 nodeToRecId[node] = recId;
499 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
500 return cast<DINodeAttr>(recSelf);
509 return UnknownLoc::get(context);
513 loc->getLine(), loc->getColumn());
516 assert(loc->getScope() &&
"expected non-null scope");
521 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
534 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
536 operands.reserve(op.getNumArgs());
537 for (
const auto &i : llvm::seq(op.getNumArgs()))
538 operands.push_back(op.getArg(i));
539 const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
542 return DIExpressionAttr::get(context, ops);
546 llvm::DIGlobalVariableExpression *node) {
547 return DIGlobalVariableExpressionAttr::get(
552StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
555 return StringAttr::get(context, stringNode->getString());
558DistinctAttr DebugImporter::getOrCreateDistinctID(llvm::DINode *node) {
static function_ref< DIRecursiveTypeAttrInterface(DistinctAttr)> getRecSelfConstructor(llvm::DINode *node)
Get the getRecSelf constructor for the translated node if it participates in CyclicReplacerCache cycl...
An attribute that associates a referenced attribute with a unique identifier.
static DistinctAttr create(Attribute referencedAttr)
Creates a distinct attribute that associates a referenced attribute with a unique identifier.
static FileLineColLoc get(StringAttr filename, unsigned line, unsigned column)
This class represents a fused location whose metadata is known to be an instance of the given type.
This class represents the base attribute for all debug info attributes.
DINodeAttr translate(llvm::DINode *node)
Translates the given LLVM debug metadata to MLIR.
DIExpressionAttr translateExpression(llvm::DIExpression *node)
Translates the LLVM DWARF expression metadata to MLIR.
DIGlobalVariableExpressionAttr translateGlobalVariableExpression(llvm::DIGlobalVariableExpression *node)
Translates the LLVM DWARF global variable expression metadata to MLIR.
Location translateLoc(llvm::DILocation *loc)
Translates the given LLVM debug location to an MLIR location.
Location translateFuncLocation(llvm::Function *func)
Translates the debug information for the given function into a Location.
DebugImporter(ModuleOp mlirModule, bool dropDICompositeTypeElements)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
llvm::TypeSwitch< T, ResultT > TypeSwitch
llvm::function_ref< Fn > function_ref