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(
68 context, getOrCreateDistinctID(node),
69 node->getSourceLanguage().getUnversionedName(),
70 translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
71 node->isOptimized(), emissionKind.value(),
72 node->isDebugInfoForProfiling(), nameTableKind.value(),
73 getStringAttrOrNull(node->getRawSplitDebugFilename()), imports);
76DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
77 std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
81 bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
82 if (isVectorType || !dropDICompositeTypeElements) {
83 for (llvm::DINode *element : node->getElements()) {
84 assert(element &&
"expected a non-null element type");
89 if (llvm::is_contained(elements,
nullptr))
91 DITypeAttr baseType =
translate(node->getBaseType());
94 if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
96 return DICompositeTypeAttr::get(
97 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
99 baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
106DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
108 DITypeAttr baseType =
translate(node->getBaseType());
109 if (node->getBaseType() && !baseType)
111 DINodeAttr extraData =
112 translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
113 return DIDerivedTypeAttr::get(
114 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
116 baseType, node->getSizeInBits(), node->getAlignInBits(),
117 node->getOffsetInBits(), node->getDWARFAddressSpace(),
118 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero), extraData);
121DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
122 return DIStringTypeAttr::get(
123 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
124 node->getSizeInBits(), node->getAlignInBits(),
130DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
131 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
134DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
136 DIScopeAttr scope =
translate(node->getScope());
137 if (node->getScope() && !scope)
139 return DILabelAttr::get(context, scope,
140 getStringAttrOrNull(node->getRawName()),
141 translate(node->getFile()), node->getLine());
144DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
146 DIScopeAttr scope =
translate(node->getScope());
147 if (node->getScope() && !scope)
149 return DILexicalBlockAttr::get(context, scope,
translate(node->getFile()),
150 node->getLine(), node->getColumn());
153DILexicalBlockFileAttr
154DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
156 DIScopeAttr scope =
translate(node->getScope());
157 if (node->getScope() && !scope)
159 return DILexicalBlockFileAttr::get(context, scope,
translate(node->getFile()),
160 node->getDiscriminator());
164DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
167 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
170 return StringAttr::get(context, node->getName());
172 return DIGlobalVariableAttr::get(
174 convertToStringAttr(node->getName()),
175 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
176 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
177 node->isDefinition(), node->getAlignInBits());
180DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
182 DIScopeAttr scope =
translate(node->getScope());
183 if (node->getScope() && !scope)
185 return DILocalVariableAttr::get(
186 context, scope, getStringAttrOrNull(node->getRawName()),
187 translate(node->getFile()), node->getLine(), node->getArg(),
188 node->getAlignInBits(),
translate(node->getType()),
189 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
192DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
193 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
196DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
197 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
200DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
201 return DIModuleAttr::get(
203 getStringAttrOrNull(node->getRawName()),
204 getStringAttrOrNull(node->getRawConfigurationMacros()),
205 getStringAttrOrNull(node->getRawIncludePath()),
206 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
210DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
211 return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
213 node->getExportSymbols());
217DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
218 SmallVector<DINodeAttr> elements;
219 for (llvm::DINode *element : node->getElements()) {
220 assert(element &&
"expected a non-null element type");
224 return DIImportedEntityAttr::get(
225 context, node->getTag(),
translate(node->getScope()),
227 getStringAttrOrNull(node->getRawName()), elements);
230DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
232 mlir::DistinctAttr id;
233 if (node->isDistinct())
234 id = getOrCreateDistinctID(node);
237 DIScopeAttr scope =
translate(node->getScope());
238 if (node->getScope() && !scope)
240 std::optional<DISubprogramFlags> subprogramFlags =
241 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
242 assert(subprogramFlags &&
"expected valid subprogram flags");
243 DISubroutineTypeAttr type =
translate(node->getType());
244 if (node->getType() && !type)
248 SmallVector<DINodeAttr> retainedNodes;
249 for (llvm::DINode *retainedNode : node->getRetainedNodes())
250 retainedNodes.push_back(
translate(retainedNode));
251 if (llvm::is_contained(retainedNodes,
nullptr))
252 retainedNodes.clear();
254 SmallVector<DINodeAttr> annotations;
258 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
259 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
260 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
261 if (tuple->getNumOperands() != 2)
263 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
264 const llvm::MDString *value =
265 dyn_cast<llvm::MDString>(tuple->getOperand(1));
267 annotations.push_back(DIAnnotationAttr::get(
268 context, StringAttr::get(context, name->getString()),
269 StringAttr::get(context, value->getString())));
274 return DISubprogramAttr::get(context,
id,
translate(node->getUnit()), scope,
275 getStringAttrOrNull(node->getRawName()),
276 getStringAttrOrNull(node->getRawLinkageName()),
277 translate(node->getFile()), node->getLine(),
278 node->getScopeLine(), *subprogramFlags, type,
279 retainedNodes, annotations);
282DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
283 auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
286 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
287 return IntegerAttr::get(IntegerType::get(context, 64),
288 constInt->getSExtValue());
289 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
291 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
292 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
294 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
300 Attribute count = getAttrOrNull(node->getCount());
301 Attribute upperBound = getAttrOrNull(node->getUpperBound());
304 if (!count && !upperBound)
306 return DISubrangeAttr::get(context, count,
307 getAttrOrNull(node->getLowerBound()), upperBound,
308 getAttrOrNull(node->getStride()));
311DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
312 return DICommonBlockAttr::get(context,
translate(node->getScope()),
314 getStringAttrOrNull(node->getRawName()),
315 translate(node->getFile()), node->getLineNo());
319DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
321 [&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
324 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
326 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
327 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
329 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
335 Attribute count = getAttrOrNull(node->getCount());
336 Attribute upperBound = getAttrOrNull(node->getUpperBound());
337 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
338 Attribute stride = getAttrOrNull(node->getStride());
341 if (!count && !upperBound)
343 return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
348DebugImporter::translateImpl(llvm::DISubroutineType *node) {
349 SmallVector<DITypeAttr> types;
350 for (llvm::DIType *type : node->getTypeArray()) {
356 types.push_back(DINullTypeAttr::get(context));
362 if (llvm::is_contained(types,
nullptr))
364 return DISubroutineTypeAttr::get(context, node->getCC(), types);
367DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
368 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
376 auto cacheEntry = cache.lookupOrInit(node);
377 if (std::optional<DINodeAttr>
result = cacheEntry.get())
381 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
382 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
383 return translateImpl(casted);
384 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
385 return translateImpl(casted);
386 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
387 return translateImpl(casted);
388 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
389 return translateImpl(casted);
390 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
391 return translateImpl(casted);
392 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
393 return translateImpl(casted);
394 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
395 return translateImpl(casted);
396 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
397 return translateImpl(casted);
398 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
399 return translateImpl(casted);
400 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
401 return translateImpl(casted);
402 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
403 return translateImpl(casted);
404 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
405 return translateImpl(casted);
406 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
407 return translateImpl(casted);
408 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
409 return translateImpl(casted);
410 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
411 return translateImpl(casted);
412 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
413 return translateImpl(casted);
414 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
415 return translateImpl(casted);
416 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
417 return translateImpl(casted);
418 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
419 return translateImpl(casted);
425 if (cacheEntry.wasRepeated()) {
427 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
428 attr = cast<DINodeAttr>(recType.withRecId(recId));
430 cacheEntry.resolve(attr);
433 cacheEntry.resolve(
nullptr);
443 .Case([&](llvm::DICompositeType *) {
444 return CtorType(DICompositeTypeAttr::getRecSelf);
446 .Case([&](llvm::DISubprogram *) {
447 return CtorType(DISubprogramAttr::getRecSelf);
449 .Default(CtorType());
452std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
459 DistinctAttr recId = nodeToRecId.lookup(node);
462 nodeToRecId[node] = recId;
464 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
465 return cast<DINodeAttr>(recSelf);
474 return UnknownLoc::get(context);
478 loc->getLine(), loc->getColumn());
481 assert(loc->getScope() &&
"expected non-null scope");
486 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
499 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
501 operands.reserve(op.getNumArgs());
502 for (
const auto &i : llvm::seq(op.getNumArgs()))
503 operands.push_back(op.getArg(i));
504 const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
507 return DIExpressionAttr::get(context, ops);
511 llvm::DIGlobalVariableExpression *node) {
512 return DIGlobalVariableExpressionAttr::get(
517StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
520 return StringAttr::get(context, stringNode->getString());
523DistinctAttr DebugImporter::getOrCreateDistinctID(llvm::DINode *node) {
static function_ref< DIRecursiveTypeAttrInterface(DistinctAttr)> getRecSelfConstructor(llvm::DINode *node)
Get the getRecSelf constructor for the translated type of node if its translated DITypeAttr supports ...
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