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);
145DIEnumeratorAttr DebugImporter::translateImpl(llvm::DIEnumerator *node) {
146 const APInt &value = node->getValue();
148 IntegerAttr::get(IntegerType::get(context, value.getBitWidth()), value);
149 return DIEnumeratorAttr::get(context, getStringAttrOrNull(node->getRawName()),
150 valueAttr, node->isUnsigned());
153DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
154 return DIStringTypeAttr::get(
155 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
156 node->getSizeInBits(), node->getAlignInBits(),
163DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
164 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
167DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
169 DIScopeAttr scope =
translate(node->getScope());
170 if (node->getScope() && !scope)
172 return DILabelAttr::get(context, scope,
173 getStringAttrOrNull(node->getRawName()),
174 translate(node->getFile()), node->getLine());
177DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
179 DIScopeAttr scope =
translate(node->getScope());
180 if (node->getScope() && !scope)
182 return DILexicalBlockAttr::get(context, scope,
translate(node->getFile()),
183 node->getLine(), node->getColumn());
186DILexicalBlockFileAttr
187DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
189 DIScopeAttr scope =
translate(node->getScope());
190 if (node->getScope() && !scope)
192 return DILexicalBlockFileAttr::get(context, scope,
translate(node->getFile()),
193 node->getDiscriminator());
197DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
200 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
203 return StringAttr::get(context, node->getName());
205 return DIGlobalVariableAttr::get(
207 convertToStringAttr(node->getName()),
208 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
209 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
210 node->isDefinition(), node->getAlignInBits());
213DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
215 DIScopeAttr scope =
translate(node->getScope());
216 if (node->getScope() && !scope)
218 return DILocalVariableAttr::get(
219 context, scope, getStringAttrOrNull(node->getRawName()),
220 translate(node->getFile()), node->getLine(), node->getArg(),
221 node->getAlignInBits(),
translate(node->getType()),
222 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
225DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
226 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
229DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
230 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
233DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
234 return DIModuleAttr::get(
236 getStringAttrOrNull(node->getRawName()),
237 getStringAttrOrNull(node->getRawConfigurationMacros()),
238 getStringAttrOrNull(node->getRawIncludePath()),
239 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
243DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
244 return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
246 node->getExportSymbols());
250DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
251 SmallVector<DINodeAttr> elements;
252 for (llvm::DINode *element : node->getElements()) {
253 assert(element &&
"expected a non-null element type");
257 return DIImportedEntityAttr::get(
258 context, node->getTag(),
translate(node->getScope()),
260 getStringAttrOrNull(node->getRawName()), elements);
263DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
265 mlir::DistinctAttr id;
266 if (node->isDistinct())
267 id = getOrCreateDistinctID(node);
270 DIScopeAttr scope =
translate(node->getScope());
271 if (node->getScope() && !scope)
273 std::optional<DISubprogramFlags> subprogramFlags =
274 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
275 assert(subprogramFlags &&
"expected valid subprogram flags");
276 DISubroutineTypeAttr type =
translate(node->getType());
277 if (node->getType() && !type)
281 SmallVector<Attribute> retainedNodes;
282 auto add = [
this, &retainedNodes](llvm::DINode *retainedNode) {
283 retainedNodes.push_back(
translate(retainedNode));
285 auto addGVE = [](llvm::DIGlobalVariableExpression *GVE) {
290 if (llvm::is_contained(retainedNodes,
nullptr))
291 retainedNodes.clear();
293 SmallVector<DINodeAttr> annotations;
297 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
298 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
299 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
300 if (tuple->getNumOperands() != 2)
302 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
303 const llvm::MDString *value =
304 dyn_cast<llvm::MDString>(tuple->getOperand(1));
306 annotations.push_back(DIAnnotationAttr::get(
307 context, StringAttr::get(context, name->getString()),
308 StringAttr::get(context, value->getString())));
313 return DISubprogramAttr::get(context,
id,
translate(node->getUnit()), scope,
314 getStringAttrOrNull(node->getRawName()),
315 getStringAttrOrNull(node->getRawLinkageName()),
316 translate(node->getFile()), node->getLine(),
317 node->getScopeLine(), *subprogramFlags, type,
318 retainedNodes, annotations);
321DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
322 auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
325 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
326 return IntegerAttr::get(IntegerType::get(context, 64),
327 constInt->getSExtValue());
328 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
330 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
331 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
333 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
339 Attribute count = getAttrOrNull(node->getCount());
340 Attribute upperBound = getAttrOrNull(node->getUpperBound());
343 if (!count && !upperBound)
345 return DISubrangeAttr::get(context, count,
346 getAttrOrNull(node->getLowerBound()), upperBound,
347 getAttrOrNull(node->getStride()));
350DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
351 return DICommonBlockAttr::get(context,
translate(node->getScope()),
353 getStringAttrOrNull(node->getRawName()),
354 translate(node->getFile()), node->getLineNo());
358DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
360 [&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
363 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
365 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
366 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
368 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
374 Attribute count = getAttrOrNull(node->getCount());
375 Attribute upperBound = getAttrOrNull(node->getUpperBound());
376 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
377 Attribute stride = getAttrOrNull(node->getStride());
380 if (!count && !upperBound)
382 return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
387DebugImporter::translateImpl(llvm::DISubroutineType *node) {
388 SmallVector<DITypeAttr> types;
389 for (llvm::DIType *type : node->getTypeArray()) {
395 types.push_back(DINullTypeAttr::get(context));
401 if (llvm::is_contained(types,
nullptr))
403 return DISubroutineTypeAttr::get(context, node->getCC(), types);
406DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
407 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
415 auto cacheEntry = cache.lookupOrInit(node);
416 if (std::optional<DINodeAttr>
result = cacheEntry.get())
420 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
421 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
422 return translateImpl(casted);
423 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
424 return translateImpl(casted);
425 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
426 return translateImpl(casted);
427 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
428 return translateImpl(casted);
429 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
430 return translateImpl(casted);
431 if (
auto *casted = dyn_cast<llvm::DIEnumerator>(node))
432 return translateImpl(casted);
433 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
434 return translateImpl(casted);
435 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
436 return translateImpl(casted);
437 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
438 return translateImpl(casted);
439 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
440 return translateImpl(casted);
441 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
442 return translateImpl(casted);
443 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
444 return translateImpl(casted);
445 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
446 return translateImpl(casted);
447 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
448 return translateImpl(casted);
449 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
450 return translateImpl(casted);
451 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
452 return translateImpl(casted);
453 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
454 return translateImpl(casted);
455 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
456 return translateImpl(casted);
457 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
458 return translateImpl(casted);
459 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
460 return translateImpl(casted);
466 if (cacheEntry.wasRepeated()) {
468 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
469 attr = cast<DINodeAttr>(recType.withRecId(recId));
471 cacheEntry.resolve(attr);
474 cacheEntry.resolve(
nullptr);
485 .Case([&](llvm::DICompositeType *) {
486 return CtorType(DICompositeTypeAttr::getRecSelf);
488 .Case([&](llvm::DISubprogram *) {
489 return CtorType(DISubprogramAttr::getRecSelf);
491 .Case([&](llvm::DICompileUnit *) {
492 return CtorType(DICompileUnitAttr::getRecSelf);
494 .Default(CtorType());
497std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
504 DistinctAttr recId = nodeToRecId.lookup(node);
507 nodeToRecId[node] = recId;
509 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
510 return cast<DINodeAttr>(recSelf);
519 return UnknownLoc::get(context);
523 loc->getLine(), loc->getColumn());
526 assert(loc->getScope() &&
"expected non-null scope");
531 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
544 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
546 operands.reserve(op.getNumArgs());
547 for (
const auto &i : llvm::seq(op.getNumArgs()))
548 operands.push_back(op.getArg(i));
549 const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
552 return DIExpressionAttr::get(context, ops);
556 llvm::DIGlobalVariableExpression *node) {
557 return DIGlobalVariableExpressionAttr::get(
562StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
565 return StringAttr::get(context, stringNode->getString());
568DistinctAttr 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