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 return DICompileUnitAttr::get(
69 getOrCreateDistinctID(node),
70 node->getSourceLanguage().getUnversionedName(),
71 translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
72 node->isOptimized(), emissionKind.value(),
73 node->isDebugInfoForProfiling(), nameTableKind.value(),
74 getStringAttrOrNull(node->getRawSplitDebugFilename()), imports);
77DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
78 std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
79 SmallVector<DINodeAttr> elements;
82 bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
83 if (isVectorType || !dropDICompositeTypeElements) {
84 for (llvm::DINode *element : node->getElements()) {
85 assert(element &&
"expected a non-null element type");
90 if (llvm::is_contained(elements,
nullptr))
92 DITypeAttr baseType =
translate(node->getBaseType());
95 if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
97 return DICompositeTypeAttr::get(
98 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
100 baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
107DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
109 DITypeAttr baseType =
translate(node->getBaseType());
110 if (node->getBaseType() && !baseType)
112 DINodeAttr extraData =
113 translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
114 return DIDerivedTypeAttr::get(
115 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
117 baseType, node->getSizeInBits(), node->getAlignInBits(),
118 node->getOffsetInBits(), node->getDWARFAddressSpace(),
119 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero), extraData);
122DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
123 return DIStringTypeAttr::get(
124 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
125 node->getSizeInBits(), node->getAlignInBits(),
131DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
132 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
135DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
137 DIScopeAttr scope =
translate(node->getScope());
138 if (node->getScope() && !scope)
140 return DILabelAttr::get(context, scope,
141 getStringAttrOrNull(node->getRawName()),
142 translate(node->getFile()), node->getLine());
145DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
147 DIScopeAttr scope =
translate(node->getScope());
148 if (node->getScope() && !scope)
150 return DILexicalBlockAttr::get(context, scope,
translate(node->getFile()),
151 node->getLine(), node->getColumn());
154DILexicalBlockFileAttr
155DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
157 DIScopeAttr scope =
translate(node->getScope());
158 if (node->getScope() && !scope)
160 return DILexicalBlockFileAttr::get(context, scope,
translate(node->getFile()),
161 node->getDiscriminator());
165DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
168 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
171 return StringAttr::get(context, node->getName());
173 return DIGlobalVariableAttr::get(
175 convertToStringAttr(node->getName()),
176 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
177 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
178 node->isDefinition(), node->getAlignInBits());
181DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
183 DIScopeAttr scope =
translate(node->getScope());
184 if (node->getScope() && !scope)
186 return DILocalVariableAttr::get(
187 context, scope, getStringAttrOrNull(node->getRawName()),
188 translate(node->getFile()), node->getLine(), node->getArg(),
189 node->getAlignInBits(),
translate(node->getType()),
190 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
193DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
194 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
197DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
198 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
201DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
202 return DIModuleAttr::get(
204 getStringAttrOrNull(node->getRawName()),
205 getStringAttrOrNull(node->getRawConfigurationMacros()),
206 getStringAttrOrNull(node->getRawIncludePath()),
207 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
211DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
212 return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
214 node->getExportSymbols());
218DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
219 SmallVector<DINodeAttr> elements;
220 for (llvm::DINode *element : node->getElements()) {
221 assert(element &&
"expected a non-null element type");
225 return DIImportedEntityAttr::get(
226 context, node->getTag(),
translate(node->getScope()),
228 getStringAttrOrNull(node->getRawName()), elements);
231DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
233 mlir::DistinctAttr id;
234 if (node->isDistinct())
235 id = getOrCreateDistinctID(node);
238 DIScopeAttr scope =
translate(node->getScope());
239 if (node->getScope() && !scope)
241 std::optional<DISubprogramFlags> subprogramFlags =
242 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
243 assert(subprogramFlags &&
"expected valid subprogram flags");
244 DISubroutineTypeAttr type =
translate(node->getType());
245 if (node->getType() && !type)
249 SmallVector<DINodeAttr> retainedNodes;
250 for (llvm::DINode *retainedNode : node->getRetainedNodes())
251 retainedNodes.push_back(
translate(retainedNode));
252 if (llvm::is_contained(retainedNodes,
nullptr))
253 retainedNodes.clear();
255 SmallVector<DINodeAttr> annotations;
259 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
260 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
261 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
262 if (tuple->getNumOperands() != 2)
264 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
265 const llvm::MDString *value =
266 dyn_cast<llvm::MDString>(tuple->getOperand(1));
268 annotations.push_back(DIAnnotationAttr::get(
269 context, StringAttr::get(context, name->getString()),
270 StringAttr::get(context, value->getString())));
275 return DISubprogramAttr::get(context,
id,
translate(node->getUnit()), scope,
276 getStringAttrOrNull(node->getRawName()),
277 getStringAttrOrNull(node->getRawLinkageName()),
278 translate(node->getFile()), node->getLine(),
279 node->getScopeLine(), *subprogramFlags, type,
280 retainedNodes, annotations);
283DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
284 auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
287 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
288 return IntegerAttr::get(IntegerType::get(context, 64),
289 constInt->getSExtValue());
290 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
292 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
293 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
295 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
301 Attribute count = getAttrOrNull(node->getCount());
302 Attribute upperBound = getAttrOrNull(node->getUpperBound());
305 if (!count && !upperBound)
307 return DISubrangeAttr::get(context, count,
308 getAttrOrNull(node->getLowerBound()), upperBound,
309 getAttrOrNull(node->getStride()));
312DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
313 return DICommonBlockAttr::get(context,
translate(node->getScope()),
315 getStringAttrOrNull(node->getRawName()),
316 translate(node->getFile()), node->getLineNo());
320DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
322 [&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
325 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
327 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
328 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
330 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
336 Attribute count = getAttrOrNull(node->getCount());
337 Attribute upperBound = getAttrOrNull(node->getUpperBound());
338 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
339 Attribute stride = getAttrOrNull(node->getStride());
342 if (!count && !upperBound)
344 return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
349DebugImporter::translateImpl(llvm::DISubroutineType *node) {
350 SmallVector<DITypeAttr> types;
351 for (llvm::DIType *type : node->getTypeArray()) {
357 types.push_back(DINullTypeAttr::get(context));
363 if (llvm::is_contained(types,
nullptr))
365 return DISubroutineTypeAttr::get(context, node->getCC(), types);
368DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
369 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
377 auto cacheEntry = cache.lookupOrInit(node);
378 if (std::optional<DINodeAttr>
result = cacheEntry.get())
382 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
383 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
384 return translateImpl(casted);
385 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
386 return translateImpl(casted);
387 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
388 return translateImpl(casted);
389 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
390 return translateImpl(casted);
391 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
392 return translateImpl(casted);
393 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
394 return translateImpl(casted);
395 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
396 return translateImpl(casted);
397 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
398 return translateImpl(casted);
399 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
400 return translateImpl(casted);
401 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
402 return translateImpl(casted);
403 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
404 return translateImpl(casted);
405 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
406 return translateImpl(casted);
407 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
408 return translateImpl(casted);
409 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
410 return translateImpl(casted);
411 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
412 return translateImpl(casted);
413 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
414 return translateImpl(casted);
415 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
416 return translateImpl(casted);
417 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
418 return translateImpl(casted);
419 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
420 return translateImpl(casted);
426 if (cacheEntry.wasRepeated()) {
428 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
429 attr = cast<DINodeAttr>(recType.withRecId(recId));
431 cacheEntry.resolve(attr);
434 cacheEntry.resolve(
nullptr);
445 .Case([&](llvm::DICompositeType *) {
446 return CtorType(DICompositeTypeAttr::getRecSelf);
448 .Case([&](llvm::DISubprogram *) {
449 return CtorType(DISubprogramAttr::getRecSelf);
451 .Case([&](llvm::DICompileUnit *) {
452 return CtorType(DICompileUnitAttr::getRecSelf);
454 .Default(CtorType());
457std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
464 DistinctAttr recId = nodeToRecId.lookup(node);
467 nodeToRecId[node] = recId;
469 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
470 return cast<DINodeAttr>(recSelf);
479 return UnknownLoc::get(context);
483 loc->getLine(), loc->getColumn());
486 assert(loc->getScope() &&
"expected non-null scope");
491 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
504 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
506 operands.reserve(op.getNumArgs());
507 for (
const auto &i : llvm::seq(op.getNumArgs()))
508 operands.push_back(op.getArg(i));
509 const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
512 return DIExpressionAttr::get(context, ops);
516 llvm::DIGlobalVariableExpression *node) {
517 return DIGlobalVariableExpressionAttr::get(
522StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
525 return StringAttr::get(context, stringNode->getString());
528DistinctAttr 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