14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/ScopeExit.h"
16 #include "llvm/ADT/SetOperations.h"
17 #include "llvm/ADT/TypeSwitch.h"
18 #include "llvm/BinaryFormat/Dwarf.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DebugInfoMetadata.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/ErrorHandling.h"
30 bool dropDICompositeTypeElements)
31 : cache([&](
llvm::DINode *node) {
return createRecSelf(node); }),
32 context(mlirModule.getContext()), mlirModule(mlirModule),
33 dropDICompositeTypeElements(dropDICompositeTypeElements) {}
36 llvm::DISubprogram *subprogram = func->getSubprogram();
42 StringAttr fileName =
StringAttr::get(context, subprogram->getFilename());
53 DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
55 node->getSizeInBits(), node->getEncoding());
58 DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
59 std::optional<DIEmissionKind> emissionKind =
60 symbolizeDIEmissionKind(node->getEmissionKind());
61 std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind(
63 std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>
>(
64 node->getNameTableKind()));
66 context, getOrCreateDistinctID(node), node->getSourceLanguage(),
67 translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
68 node->isOptimized(), emissionKind.value(), nameTableKind.value());
71 DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
72 std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
76 bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
77 if (isVectorType || !dropDICompositeTypeElements) {
78 for (llvm::DINode *element : node->getElements()) {
79 assert(element &&
"expected a non-null element type");
84 if (llvm::is_contained(elements,
nullptr))
89 if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
92 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
94 baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
95 node->getAlignInBits(), elements,
102 DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
105 if (node->getBaseType() && !baseType)
108 translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
110 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
111 baseType, node->getSizeInBits(), node->getAlignInBits(),
112 node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
115 DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
117 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
118 node->getSizeInBits(), node->getAlignInBits(),
124 DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
125 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
128 DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
131 if (node->getScope() && !scope)
134 getStringAttrOrNull(node->getRawName()),
135 translate(node->getFile()), node->getLine());
138 DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
141 if (node->getScope() && !scope)
144 node->getLine(), node->getColumn());
147 DILexicalBlockFileAttr
148 DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
151 if (node->getScope() && !scope)
154 node->getDiscriminator());
158 DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
161 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
168 convertToStringAttr(node->getName()),
169 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
170 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
171 node->isDefinition(), node->getAlignInBits());
174 DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
177 if (node->getScope() && !scope)
180 context, scope, getStringAttrOrNull(node->getRawName()),
181 translate(node->getFile()), node->getLine(), node->getArg(),
182 node->getAlignInBits(),
translate(node->getType()),
183 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
186 DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
187 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
190 DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
191 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
194 DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
197 getStringAttrOrNull(node->getRawName()),
198 getStringAttrOrNull(node->getRawConfigurationMacros()),
199 getStringAttrOrNull(node->getRawIncludePath()),
200 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
204 DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
207 node->getExportSymbols());
211 DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
213 for (llvm::DINode *element : node->getElements()) {
214 assert(element &&
"expected a non-null element type");
219 context, node->getTag(),
translate(node->getScope()),
221 getStringAttrOrNull(node->getRawName()), elements);
224 DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
227 if (node->isDistinct())
228 id = getOrCreateDistinctID(node);
232 if (node->getScope() && !scope)
234 std::optional<DISubprogramFlags> subprogramFlags =
235 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
236 assert(subprogramFlags &&
"expected valid subprogram flags");
237 DISubroutineTypeAttr type =
translate(node->getType());
238 if (node->getType() && !type)
243 for (llvm::DINode *retainedNode : node->getRetainedNodes())
244 retainedNodes.push_back(
translate(retainedNode));
245 if (llvm::is_contained(retainedNodes,
nullptr))
246 retainedNodes.clear();
252 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
253 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
254 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
255 if (tuple->getNumOperands() != 2)
257 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
258 const llvm::MDString *value =
259 dyn_cast<llvm::MDString>(tuple->getOperand(1));
269 getStringAttrOrNull(node->getRawName()),
270 getStringAttrOrNull(node->getRawLinkageName()),
271 translate(node->getFile()), node->getLine(),
272 node->getScopeLine(), *subprogramFlags, type,
273 retainedNodes, annotations);
276 DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
280 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
282 constInt->getSExtValue());
283 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
285 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
286 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
288 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
294 Attribute count = getAttrOrNull(node->getCount());
295 Attribute upperBound = getAttrOrNull(node->getUpperBound());
298 if (!count && !upperBound)
301 getAttrOrNull(node->getLowerBound()), upperBound,
302 getAttrOrNull(node->getStride()));
305 DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
308 getStringAttrOrNull(node->getRawName()),
309 translate(node->getFile()), node->getLineNo());
312 DIGenericSubrangeAttr
313 DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
318 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
320 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
321 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
323 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
329 Attribute count = getAttrOrNull(node->getCount());
330 Attribute upperBound = getAttrOrNull(node->getUpperBound());
331 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
332 Attribute stride = getAttrOrNull(node->getStride());
335 if (!count && !upperBound)
342 DebugImporter::translateImpl(llvm::DISubroutineType *node) {
344 for (llvm::DIType *type : node->getTypeArray()) {
356 if (llvm::is_contained(types,
nullptr))
361 DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
362 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
370 auto cacheEntry = cache.lookupOrInit(node);
371 if (std::optional<DINodeAttr> result = cacheEntry.get())
375 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
376 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
377 return translateImpl(casted);
378 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
379 return translateImpl(casted);
380 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
381 return translateImpl(casted);
382 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
383 return translateImpl(casted);
384 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
385 return translateImpl(casted);
386 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
387 return translateImpl(casted);
388 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
389 return translateImpl(casted);
390 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
391 return translateImpl(casted);
392 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
393 return translateImpl(casted);
394 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
395 return translateImpl(casted);
396 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
397 return translateImpl(casted);
398 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
399 return translateImpl(casted);
400 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
401 return translateImpl(casted);
402 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
403 return translateImpl(casted);
404 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
405 return translateImpl(casted);
406 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
407 return translateImpl(casted);
408 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
409 return translateImpl(casted);
410 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
411 return translateImpl(casted);
412 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
413 return translateImpl(casted);
419 if (cacheEntry.wasRepeated()) {
421 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
422 attr = cast<DINodeAttr>(recType.withRecId(recId));
424 cacheEntry.resolve(attr);
427 cacheEntry.resolve(
nullptr);
437 .Case([&](llvm::DICompositeType *) {
438 return CtorType(DICompositeTypeAttr::getRecSelf);
440 .Case([&](llvm::DISubprogram *) {
441 return CtorType(DISubprogramAttr::getRecSelf);
443 .Default(CtorType());
446 std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
456 nodeToRecId[node] = recId;
458 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
459 return cast<DINodeAttr>(recSelf);
472 loc->getLine(), loc->getColumn());
475 assert(loc->getScope() &&
"expected non-null scope");
480 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
493 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
495 operands.reserve(op.getNumArgs());
496 for (
const auto &i : llvm::seq(op.getNumArgs()))
497 operands.push_back(op.getArg(i));
505 llvm::DIGlobalVariableExpression *node) {
511 StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
517 DistinctAttr 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 ...
Attributes are known-constant values of operations.
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 the base attribute for all debug info attributes.
This class represents a LLVM attribute that describes a debug info scope.
This class represents a LLVM attribute that describes a debug info type.
This class represents a LLVM attribute that describes a debug info variable.
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.
BoundType
The type of bound: equal, lower bound or upper bound.
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...