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(), {},
93 getStringAttrOrNull(node->getRawName()),
translate(node->getFile()),
94 node->getLine(),
translate(node->getScope()), baseType,
95 flags.value_or(DIFlags::Zero), node->getSizeInBits(),
96 node->getAlignInBits(), elements,
103 DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
106 if (node->getBaseType() && !baseType)
109 translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
111 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
112 baseType, node->getSizeInBits(), node->getAlignInBits(),
113 node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
116 DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
118 context, node->getTag(), getStringAttrOrNull(node->getRawName()),
119 node->getSizeInBits(), node->getAlignInBits(),
125 DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
126 return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
129 DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
132 if (node->getScope() && !scope)
135 getStringAttrOrNull(node->getRawName()),
136 translate(node->getFile()), node->getLine());
139 DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
142 if (node->getScope() && !scope)
145 node->getLine(), node->getColumn());
148 DILexicalBlockFileAttr
149 DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
152 if (node->getScope() && !scope)
155 node->getDiscriminator());
159 DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
162 auto convertToStringAttr = [&](StringRef name) -> StringAttr {
169 convertToStringAttr(node->getName()),
170 convertToStringAttr(node->getLinkageName()),
translate(node->getFile()),
171 node->getLine(),
translate(node->getType()), node->isLocalToUnit(),
172 node->isDefinition(), node->getAlignInBits());
175 DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
178 if (node->getScope() && !scope)
181 context, scope, getStringAttrOrNull(node->getRawName()),
182 translate(node->getFile()), node->getLine(), node->getArg(),
183 node->getAlignInBits(),
translate(node->getType()),
184 symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
187 DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
188 return cast<DIVariableAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
191 DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
192 return cast<DIScopeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
195 DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
198 getStringAttrOrNull(node->getRawName()),
199 getStringAttrOrNull(node->getRawConfigurationMacros()),
200 getStringAttrOrNull(node->getRawIncludePath()),
201 getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
205 DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
208 node->getExportSymbols());
211 DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
214 if (node->isDistinct())
215 id = getOrCreateDistinctID(node);
218 if (node->getScope() && !scope)
220 std::optional<DISubprogramFlags> subprogramFlags =
221 symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
222 assert(subprogramFlags &&
"expected valid subprogram flags");
223 DISubroutineTypeAttr type =
translate(node->getType());
224 if (node->getType() && !type)
227 getStringAttrOrNull(node->getRawName()),
228 getStringAttrOrNull(node->getRawLinkageName()),
229 translate(node->getFile()), node->getLine(),
230 node->getScopeLine(), *subprogramFlags, type);
233 DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
237 if (
auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
239 constInt->getSExtValue());
240 if (
auto *expr = dyn_cast<llvm::DIExpression *>(data))
242 if (
auto *var = dyn_cast<llvm::DIVariable *>(data)) {
243 if (
auto *local = dyn_cast<llvm::DILocalVariable>(var))
245 if (
auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
251 Attribute count = getAttrOrNull(node->getCount());
252 Attribute upperBound = getAttrOrNull(node->getUpperBound());
255 if (!count && !upperBound)
258 getAttrOrNull(node->getLowerBound()), upperBound,
259 getAttrOrNull(node->getStride()));
263 DebugImporter::translateImpl(llvm::DISubroutineType *node) {
265 for (llvm::DIType *type : node->getTypeArray()) {
277 if (llvm::is_contained(types,
nullptr))
282 DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
283 return cast<DITypeAttr>(
translate(
static_cast<llvm::DINode *
>(node)));
291 auto cacheEntry = cache.lookupOrInit(node);
292 if (std::optional<DINodeAttr> result = cacheEntry.get())
296 auto translateNode = [
this](llvm::DINode *node) ->
DINodeAttr {
297 if (
auto *casted = dyn_cast<llvm::DIBasicType>(node))
298 return translateImpl(casted);
299 if (
auto *casted = dyn_cast<llvm::DICompileUnit>(node))
300 return translateImpl(casted);
301 if (
auto *casted = dyn_cast<llvm::DICompositeType>(node))
302 return translateImpl(casted);
303 if (
auto *casted = dyn_cast<llvm::DIDerivedType>(node))
304 return translateImpl(casted);
305 if (
auto *casted = dyn_cast<llvm::DIStringType>(node))
306 return translateImpl(casted);
307 if (
auto *casted = dyn_cast<llvm::DIFile>(node))
308 return translateImpl(casted);
309 if (
auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
310 return translateImpl(casted);
311 if (
auto *casted = dyn_cast<llvm::DILabel>(node))
312 return translateImpl(casted);
313 if (
auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
314 return translateImpl(casted);
315 if (
auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
316 return translateImpl(casted);
317 if (
auto *casted = dyn_cast<llvm::DILocalVariable>(node))
318 return translateImpl(casted);
319 if (
auto *casted = dyn_cast<llvm::DIModule>(node))
320 return translateImpl(casted);
321 if (
auto *casted = dyn_cast<llvm::DINamespace>(node))
322 return translateImpl(casted);
323 if (
auto *casted = dyn_cast<llvm::DISubprogram>(node))
324 return translateImpl(casted);
325 if (
auto *casted = dyn_cast<llvm::DISubrange>(node))
326 return translateImpl(casted);
327 if (
auto *casted = dyn_cast<llvm::DISubroutineType>(node))
328 return translateImpl(casted);
334 if (cacheEntry.wasRepeated()) {
336 auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
337 attr = cast<DINodeAttr>(recType.withRecId(recId));
339 cacheEntry.resolve(attr);
342 cacheEntry.resolve(
nullptr);
352 .Case([&](llvm::DICompositeType *) {
353 return CtorType(DICompositeTypeAttr::getRecSelf);
355 .Default(CtorType());
358 std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
368 nodeToRecId[node] = recId;
370 DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
371 return cast<DINodeAttr>(recSelf);
384 loc->getLine(), loc->getColumn());
387 assert(loc->getScope() &&
"expected non-null scope");
392 if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
405 for (
const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
407 operands.reserve(op.getNumArgs());
408 for (
const auto &i : llvm::seq(op.getNumArgs()))
409 operands.push_back(op.getArg(i));
417 llvm::DIGlobalVariableExpression *node) {
423 StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
429 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.
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...
Include the generated interface declarations.
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...