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();
38 StringAttr fileName =
StringAttr::get(context, subprogram->getFilename());
49 DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
51 node->getSizeInBits(), node->getEncoding());
54 DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
55 std::optional<DIEmissionKind> emissionKind =
56 symbolizeDIEmissionKind(node->getEmissionKind());
57 std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind(
59 std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>
>(
60 node->getNameTableKind()));
62 context, getOrCreateDistinctID(node), node->getSourceLanguage(),
63 translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
64 node->isOptimized(), emissionKind.value(), nameTableKind.value());
67 DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
68 std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
72 bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
73 if (isVectorType || !dropDICompositeTypeElements) {
74 for (llvm::DINode *element : node->getElements()) {
75 assert(element &&
"expected a non-null element type");
80 if (llvm::is_contained(elements,
nullptr))
85 if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
88 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
90 baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
97 DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
100 if (node->getBaseType() && !baseType)
103 translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
105 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
106 baseType, node->getSizeInBits(), node->getAlignInBits(),
107 node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
110 DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
112 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
113 node->getSizeInBits(), node->getAlignInBits(),
119 DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
120 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
123 DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
126 if (node->getScope() && !scope)
129 getStringAttrOrNull(node->getRawName()),
130 translate(node->getFile()), node->getLine());
133 DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
136 if (node->getScope() && !scope)
139 node->getLine(), node->getColumn());
142 DILexicalBlockFileAttr
143 DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
146 if (node->getScope() && !scope)
149 node->getDiscriminator());
153 DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
156 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
163 convertToStringAttr(node->getName()),
164 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
165 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
166 node->isDefinition(), node->getAlignInBits());
169 DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
172 if (node->getScope() && !scope)
175 context, scope, getStringAttrOrNull(node->getRawName()),
176 translate(node->getFile()), node->getLine(), node->getArg(),
177 node->getAlignInBits(),
translate(node->getType()),
178 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
181 DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
182 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
185 DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
186 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
189 DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
192 getStringAttrOrNull(node->getRawName()),
193 getStringAttrOrNull(node->getRawConfigurationMacros()),
194 getStringAttrOrNull(node->getRawIncludePath()),
195 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
199 DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
202 node->getExportSymbols());
206 DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
208 for (llvm::DINode *element : node->getElements()) {
209 assert(element &&
"expected a non-null element type");
214 context, node->getTag(),
translate(node->getScope()),
216 getStringAttrOrNull(node->getRawName()), elements);
219 DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
222 if (node->isDistinct())
223 id = getOrCreateDistinctID(node);
227 if (node->getScope() && !scope)
229 std::optional<DISubprogramFlags> subprogramFlags =
230 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
231 assert(subprogramFlags &&
"expected valid subprogram flags");
232 DISubroutineTypeAttr type =
translate(node->getType());
233 if (node->getType() && !type)
238 for (llvm::DINode *retainedNode : node->getRetainedNodes())
239 retainedNodes.push_back(
translate(retainedNode));
240 if (llvm::is_contained(retainedNodes,
nullptr))
241 retainedNodes.clear();
247 if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
248 for (
size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
249 const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
250 if (tuple->getNumOperands() != 2)
252 const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
253 const llvm::MDString *value =
254 dyn_cast<llvm::MDString>(tuple->getOperand(1));
264 getStringAttrOrNull(node->getRawName()),
265 getStringAttrOrNull(node->getRawLinkageName()),
266 translate(node->getFile()), node->getLine(),
267 node->getScopeLine(), *subprogramFlags, type,
268 retainedNodes, annotations);
271 DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
275 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
277 constInt->getSExtValue());
278 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
280 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
281 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
283 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
289 Attribute count = getAttrOrNull(node->getCount());
290 Attribute upperBound = getAttrOrNull(node->getUpperBound());
293 if (!count && !upperBound)
296 getAttrOrNull(node->getLowerBound()), upperBound,
297 getAttrOrNull(node->getStride()));
300 DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
303 getStringAttrOrNull(node->getRawName()),
304 translate(node->getFile()), node->getLineNo());
307 DIGenericSubrangeAttr
308 DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
313 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
315 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
316 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
318 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
324 Attribute count = getAttrOrNull(node->getCount());
325 Attribute upperBound = getAttrOrNull(node->getUpperBound());
326 Attribute lowerBound = getAttrOrNull(node->getLowerBound());
327 Attribute stride = getAttrOrNull(node->getStride());
330 if (!count && !upperBound)
337 DebugImporter::translateImpl(llvm::DISubroutineType *node) {
339 for (llvm::DIType *type : node->getTypeArray()) {
351 if (llvm::is_contained(types,
nullptr))
356 DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
357 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
365 auto cacheEntry = cache.lookupOrInit(node);
366 if (std::optional<DINodeAttr> result = cacheEntry.get())
370 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
371 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
372 return translateImpl(casted);
373 if (
auto *casted = dyn_cast<llvm::DICommonBlock>(node))
374 return translateImpl(casted);
375 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
376 return translateImpl(casted);
377 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
378 return translateImpl(casted);
379 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
380 return translateImpl(casted);
381 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
382 return translateImpl(casted);
383 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
384 return translateImpl(casted);
385 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
386 return translateImpl(casted);
387 if (
auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
388 return translateImpl(casted);
389 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
390 return translateImpl(casted);
391 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
392 return translateImpl(casted);
393 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
394 return translateImpl(casted);
395 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
396 return translateImpl(casted);
397 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
398 return translateImpl(casted);
399 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
400 return translateImpl(casted);
401 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
402 return translateImpl(casted);
403 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
404 return translateImpl(casted);
405 if (
auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
406 return translateImpl(casted);
407 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
408 return translateImpl(casted);
414 if (cacheEntry.wasRepeated()) {
416 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
417 attr = cast<DINodeAttr>(recType.withRecId(recId));
419 cacheEntry.resolve(attr);
422 cacheEntry.resolve(
nullptr);
432 .Case([&](llvm::DICompositeType *) {
433 return CtorType(DICompositeTypeAttr::getRecSelf);
435 .Case([&](llvm::DISubprogram *) {
436 return CtorType(DISubprogramAttr::getRecSelf);
438 .Default(CtorType());
441 std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
451 nodeToRecId[node] = recId;
453 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
454 return cast<DINodeAttr>(recSelf);
467 loc->getLine(), loc->getColumn());
470 assert(loc->getScope() &&
"expected non-null scope");
475 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
488 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
490 operands.reserve(op.getNumArgs());
491 for (
const auto &i : llvm::seq(op.getNumArgs()))
492 operands.push_back(op.getArg(i));
500 llvm::DIGlobalVariableExpression *node) {
506 StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
512 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...