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(),
105 getStringAttrOrNull(node->getRawIdentifier()),
106 translate(node->getDiscriminator()), elements);
109DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
111 DITypeAttr baseType =
translate(node->getBaseType());
112 if (node->getBaseType() && !baseType)
114 llvm::Metadata *rawExtraData = node->getExtraData();
116 if (
auto *extraDataNode = dyn_cast_or_null<llvm::DINode>(rawExtraData)) {
118 }
else if (
auto *constantAsMetadata =
119 dyn_cast_or_null<llvm::ConstantAsMetadata>(rawExtraData)) {
120 if (
auto *constantInt =
121 dyn_cast<llvm::ConstantInt>(constantAsMetadata->getValue())) {
122 const APInt &value = constantInt->getValue();
123 extraData = IntegerAttr::get(
124 IntegerType::get(context, value.getBitWidth()), value);
127 return DIDerivedTypeAttr::get(
128 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
130 baseType, node->getSizeInBits(), node->getAlignInBits(),
131 node->getOffsetInBits(), node->getDWARFAddressSpace(),
132 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero), extraData);
135DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
136 return DIStringTypeAttr::get(
137 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
138 node->getSizeInBits(), node->getAlignInBits(),
144DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
145 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
148DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
150 DIScopeAttr scope =
translate(node->getScope());
151 if (node->getScope() && !scope)
153 return DILabelAttr::get(context, scope,
154 getStringAttrOrNull(node->getRawName()),
155 translate(node->getFile()), node->getLine());
158DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
160 DIScopeAttr scope =
translate(node->getScope());
161 if (node->getScope() && !scope)
163 return DILexicalBlockAttr::get(context, scope,
translate(node->getFile()),
164 node->getLine(), node->getColumn());
167DILexicalBlockFileAttr
168DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
170 DIScopeAttr scope =
translate(node->getScope());
171 if (node->getScope() && !scope)
173 return DILexicalBlockFileAttr::get(context, scope,
translate(node->getFile()),
174 node->getDiscriminator());
178DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
181 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
184 return StringAttr::get(context, node->getName());
186 return DIGlobalVariableAttr::get(
188 convertToStringAttr(node->getName()),
189 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
190 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
191 node->isDefinition(), node->getAlignInBits());
194DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
196 DIScopeAttr scope =
translate(node->getScope());
197 if (node->getScope() && !scope)
199 return DILocalVariableAttr::get(
200 context, scope, getStringAttrOrNull(node->getRawName()),
201 translate(node->getFile()), node->getLine(), node->getArg(),
202 node->getAlignInBits(),
translate(node->getType()),
203 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
206DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
207 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
210DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
211 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
214DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
215 return DIModuleAttr::get(
217 getStringAttrOrNull(node->getRawName()),
218 getStringAttrOrNull(node->getRawConfigurationMacros()),
219 getStringAttrOrNull(node->getRawIncludePath()),
220 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
224DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
225 return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
227 node->getExportSymbols());
231DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
232 SmallVector<DINodeAttr> elements;
233 for (llvm::DINode *element : node->getElements()) {
234 assert(element &&
"expected a non-null element type");
238 return DIImportedEntityAttr::get(
239 context, node->getTag(),
translate(node->getScope()),
241 getStringAttrOrNull(node->getRawName()), elements);
244DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
246 mlir::DistinctAttr id;
247 if (node->isDistinct())
248 id = getOrCreateDistinctID(node);
251 DIScopeAttr scope =
translate(node->getScope());
252 if (node->getScope() && !scope)
254 std::optional<DISubprogramFlags> subprogramFlags =
255 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
256 assert(subprogramFlags &&
"expected valid subprogram flags");
257 DISubroutineTypeAttr type =
translate(node->getType());
258 if (node->getType() && !type)
262 SmallVector<DINodeAttr> retainedNodes;
263 for (llvm::DINode *retainedNode : node->getRetainedNodes())
264 retainedNodes.push_back(
translate(retainedNode));
265 if (llvm::is_contained(retainedNodes,
nullptr))
266 retainedNodes.clear();
268 SmallVector<DINodeAttr> annotations;
272 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
273 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
274 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
275 if (tuple->getNumOperands() != 2)
277 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
278 const llvm::MDString *value =
279 dyn_cast<llvm::MDString>(tuple->getOperand(1));
281 annotations.push_back(DIAnnotationAttr::get(
282 context, StringAttr::get(context, name->getString()),
283 StringAttr::get(context, value->getString())));
288 return DISubprogramAttr::get(context,
id,
translate(node->getUnit()), scope,
289 getStringAttrOrNull(node->getRawName()),
290 getStringAttrOrNull(node->getRawLinkageName()),
291 translate(node->getFile()), node->getLine(),
292 node->getScopeLine(), *subprogramFlags, type,
293 retainedNodes, annotations);
296DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
297 auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
300 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
301 return IntegerAttr::get(IntegerType::get(context, 64),
302 constInt->getSExtValue());
303 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
305 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
306 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
308 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
314 Attribute count = getAttrOrNull(node->getCount());
315 Attribute upperBound = getAttrOrNull(node->getUpperBound());
318 if (!count && !upperBound)
320 return DISubrangeAttr::get(context, count,
321 getAttrOrNull(node->getLowerBound()), upperBound,
322 getAttrOrNull(node->getStride()));
325DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
326 return DICommonBlockAttr::get(context,
translate(node->getScope()),
328 getStringAttrOrNull(node->getRawName()),
329 translate(node->getFile()), node->getLineNo());
333DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
335 [&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
338 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
340 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
341 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
343 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
349 Attribute count = getAttrOrNull(node->getCount());
350 Attribute upperBound = getAttrOrNull(node->getUpperBound());
351 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
352 Attribute stride = getAttrOrNull(node->getStride());
355 if (!count && !upperBound)
357 return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
362DebugImporter::translateImpl(llvm::DISubroutineType *node) {
363 SmallVector<DITypeAttr> types;
364 for (llvm::DIType *type : node->getTypeArray()) {
370 types.push_back(DINullTypeAttr::get(context));
376 if (llvm::is_contained(types,
nullptr))
378 return DISubroutineTypeAttr::get(context, node->getCC(), types);
381DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
382 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
390 auto cacheEntry = cache.lookupOrInit(node);
391 if (std::optional<DINodeAttr>
result = cacheEntry.get())
395 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
396 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
397 return translateImpl(casted);
398 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
399 return translateImpl(casted);
400 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
401 return translateImpl(casted);
402 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
403 return translateImpl(casted);
404 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
405 return translateImpl(casted);
406 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
407 return translateImpl(casted);
408 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
409 return translateImpl(casted);
410 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
411 return translateImpl(casted);
412 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
413 return translateImpl(casted);
414 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
415 return translateImpl(casted);
416 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
417 return translateImpl(casted);
418 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
419 return translateImpl(casted);
420 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
421 return translateImpl(casted);
422 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
423 return translateImpl(casted);
424 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
425 return translateImpl(casted);
426 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
427 return translateImpl(casted);
428 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
429 return translateImpl(casted);
430 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
431 return translateImpl(casted);
432 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
433 return translateImpl(casted);
439 if (cacheEntry.wasRepeated()) {
441 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
442 attr = cast<DINodeAttr>(recType.withRecId(recId));
444 cacheEntry.resolve(attr);
447 cacheEntry.resolve(
nullptr);
458 .Case([&](llvm::DICompositeType *) {
459 return CtorType(DICompositeTypeAttr::getRecSelf);
461 .Case([&](llvm::DISubprogram *) {
462 return CtorType(DISubprogramAttr::getRecSelf);
464 .Case([&](llvm::DICompileUnit *) {
465 return CtorType(DICompileUnitAttr::getRecSelf);
467 .Default(CtorType());
470std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
477 DistinctAttr recId = nodeToRecId.lookup(node);
480 nodeToRecId[node] = recId;
482 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
483 return cast<DINodeAttr>(recSelf);
492 return UnknownLoc::get(context);
496 loc->getLine(), loc->getColumn());
499 assert(loc->getScope() &&
"expected non-null scope");
504 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
517 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
519 operands.reserve(op.getNumArgs());
520 for (
const auto &i : llvm::seq(op.getNumArgs()))
521 operands.push_back(op.getArg(i));
522 const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
525 return DIExpressionAttr::get(context, ops);
529 llvm::DIGlobalVariableExpression *node) {
530 return DIGlobalVariableExpressionAttr::get(
535StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
538 return StringAttr::get(context, stringNode->getString());
541DistinctAttr 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