| 1 | //===- DebugImporter.cpp - LLVM to MLIR Debug conversion ------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "DebugImporter.h" |
| 10 | #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 11 | #include "mlir/IR/Attributes.h" |
| 12 | #include "mlir/IR/BuiltinAttributes.h" |
| 13 | #include "mlir/IR/Location.h" |
| 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" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | using namespace mlir::LLVM; |
| 23 | using namespace mlir::LLVM::detail; |
| 24 | |
| 25 | DebugImporter::DebugImporter(ModuleOp mlirModule, |
| 26 | bool dropDICompositeTypeElements) |
| 27 | : cache([&](llvm::DINode *node) { return createRecSelf(node); }), |
| 28 | context(mlirModule.getContext()), mlirModule(mlirModule), |
| 29 | dropDICompositeTypeElements(dropDICompositeTypeElements) {} |
| 30 | |
| 31 | Location DebugImporter::translateFuncLocation(llvm::Function *func) { |
| 32 | llvm::DISubprogram *subprogram = func->getSubprogram(); |
| 33 | if (!subprogram) |
| 34 | return UnknownLoc::get(context); |
| 35 | |
| 36 | // Add a fused location to link the subprogram information. |
| 37 | StringAttr fileName = StringAttr::get(context, bytes: subprogram->getFilename()); |
| 38 | return FusedLocWith<DISubprogramAttr>::get( |
| 39 | locs: {FileLineColLoc::get(filename: fileName, line: subprogram->getLine(), /*column=*/0)}, |
| 40 | metadata: translate(node: subprogram), context); |
| 41 | } |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // Attributes |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) { |
| 48 | return DIBasicTypeAttr::get(context, tag: node->getTag(), |
| 49 | name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 50 | sizeInBits: node->getSizeInBits(), encoding: node->getEncoding()); |
| 51 | } |
| 52 | |
| 53 | DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) { |
| 54 | std::optional<DIEmissionKind> emissionKind = |
| 55 | symbolizeDIEmissionKind(node->getEmissionKind()); |
| 56 | std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind( |
| 57 | static_cast< |
| 58 | std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>>( |
| 59 | node->getNameTableKind())); |
| 60 | SmallVector<DINodeAttr> imports; |
| 61 | if (node->getImportedEntities()) { |
| 62 | for (llvm::DIImportedEntity *importedEntity : node->getImportedEntities()) |
| 63 | if (DINodeAttr nodeAttr = |
| 64 | translate(node: static_cast<llvm::DINode *>(importedEntity))) |
| 65 | imports.push_back(Elt: nodeAttr); |
| 66 | } |
| 67 | llvm::DISourceLanguageName sourceLanguage = node->getSourceLanguage(); |
| 68 | DISourceLanguageNameAttr sourceLanguageAttr; |
| 69 | if (sourceLanguage.hasVersionedName()) { |
| 70 | sourceLanguageAttr = DISourceLanguageNameAttr::get( |
| 71 | context, /*language=*/0, name: sourceLanguage.getName(), |
| 72 | version: sourceLanguage.getVersion(), dialect: sourceLanguage.getDialect()); |
| 73 | } else { |
| 74 | sourceLanguageAttr = DISourceLanguageNameAttr::get( |
| 75 | context, language: sourceLanguage.getName(), /*name=*/0, |
| 76 | /*version=*/std::nullopt, dialect: sourceLanguage.getDialect()); |
| 77 | } |
| 78 | return DICompileUnitAttr::get( |
| 79 | context, /*recId=*/DistinctAttr{}, /*isRecSelf=*/false, |
| 80 | id: getOrCreateDistinctID(node), sourceLanguage: sourceLanguageAttr, |
| 81 | file: translate(node: node->getFile()), producer: getStringAttrOrNull(stringNode: node->getRawProducer()), |
| 82 | isOptimized: node->isOptimized(), emissionKind: emissionKind.value(), |
| 83 | isDebugInfoForProfiling: node->isDebugInfoForProfiling(), nameTableKind: nameTableKind.value(), |
| 84 | splitDebugFilename: getStringAttrOrNull(stringNode: node->getRawSplitDebugFilename()), importedEntities: imports); |
| 85 | } |
| 86 | |
| 87 | DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) { |
| 88 | std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags()); |
| 89 | SmallVector<DINodeAttr> elements; |
| 90 | |
| 91 | // A vector always requires an element. |
| 92 | bool isVectorType = flags && bitEnumContainsAll(bits: *flags, bit: DIFlags::Vector); |
| 93 | if (isVectorType || !dropDICompositeTypeElements) { |
| 94 | for (llvm::DINode *element : node->getElements()) { |
| 95 | assert(element && "expected a non-null element type" ); |
| 96 | elements.push_back(Elt: translate(node: element)); |
| 97 | } |
| 98 | } |
| 99 | // Drop the elements parameter if any of the elements are invalid. |
| 100 | if (llvm::is_contained(Range&: elements, Element: nullptr)) |
| 101 | elements.clear(); |
| 102 | DITypeAttr baseType = translate(node: node->getBaseType()); |
| 103 | // Arrays require a base type, otherwise the debug metadata is considered to |
| 104 | // be malformed. |
| 105 | if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType) |
| 106 | return nullptr; |
| 107 | return DICompositeTypeAttr::get( |
| 108 | context, tag: node->getTag(), name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 109 | file: translate(node: node->getFile()), line: node->getLine(), scope: translate(node: node->getScope()), |
| 110 | baseType, flags: flags.value_or(u: DIFlags::Zero), sizeInBits: node->getSizeInBits(), |
| 111 | alignInBits: node->getAlignInBits(), dataLocation: translateExpression(node: node->getDataLocationExp()), |
| 112 | rank: translateExpression(node: node->getRankExp()), |
| 113 | allocated: translateExpression(node: node->getAllocatedExp()), |
| 114 | associated: translateExpression(node: node->getAssociatedExp()), |
| 115 | identifier: getStringAttrOrNull(stringNode: node->getRawIdentifier()), |
| 116 | discriminator: translate(node: node->getDiscriminator()), elements); |
| 117 | } |
| 118 | |
| 119 | DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) { |
| 120 | // Return nullptr if the base type is invalid. |
| 121 | DITypeAttr baseType = translate(node: node->getBaseType()); |
| 122 | if (node->getBaseType() && !baseType) |
| 123 | return nullptr; |
| 124 | llvm::Metadata * = node->getExtraData(); |
| 125 | Attribute ; |
| 126 | if (auto * = dyn_cast_or_null<llvm::DINode>(Val: rawExtraData)) { |
| 127 | extraData = translate(node: extraDataNode); |
| 128 | } else if (auto *constantAsMetadata = |
| 129 | dyn_cast_or_null<llvm::ConstantAsMetadata>(Val: rawExtraData)) { |
| 130 | if (auto *constantInt = |
| 131 | dyn_cast<llvm::ConstantInt>(Val: constantAsMetadata->getValue())) { |
| 132 | const APInt &value = constantInt->getValue(); |
| 133 | extraData = IntegerAttr::get( |
| 134 | type: IntegerType::get(context, width: value.getBitWidth()), value); |
| 135 | } |
| 136 | } |
| 137 | return DIDerivedTypeAttr::get( |
| 138 | context, tag: node->getTag(), name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 139 | file: translate(node: node->getFile()), line: node->getLine(), scope: translate(node: node->getScope()), |
| 140 | baseType, sizeInBits: node->getSizeInBits(), alignInBits: node->getAlignInBits(), |
| 141 | offsetInBits: node->getOffsetInBits(), dwarfAddressSpace: node->getDWARFAddressSpace(), |
| 142 | flags: symbolizeDIFlags(node->getFlags()).value_or(u: DIFlags::Zero), extraData); |
| 143 | } |
| 144 | |
| 145 | DIEnumeratorAttr DebugImporter::translateImpl(llvm::DIEnumerator *node) { |
| 146 | const APInt &value = node->getValue(); |
| 147 | auto valueAttr = |
| 148 | IntegerAttr::get(type: IntegerType::get(context, width: value.getBitWidth()), value); |
| 149 | return DIEnumeratorAttr::get(context, name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 150 | value: valueAttr, isUnsigned: node->isUnsigned()); |
| 151 | } |
| 152 | |
| 153 | DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) { |
| 154 | return DIStringTypeAttr::get( |
| 155 | context, tag: node->getTag(), name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 156 | sizeInBits: node->getSizeInBits(), alignInBits: node->getAlignInBits(), |
| 157 | stringLength: translate(node: node->getStringLength()), |
| 158 | stringLengthExp: translateExpression(node: node->getStringLengthExp()), |
| 159 | stringLocationExp: translateExpression(node: node->getStringLocationExp()), encoding: node->getEncoding(), |
| 160 | charType: translate(node: node->getCharType())); |
| 161 | } |
| 162 | |
| 163 | DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) { |
| 164 | return DIFileAttr::get(context, name: node->getFilename(), directory: node->getDirectory()); |
| 165 | } |
| 166 | |
| 167 | DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) { |
| 168 | // Return nullptr if the scope or type is a cyclic dependency. |
| 169 | DIScopeAttr scope = translate(node: node->getScope()); |
| 170 | if (node->getScope() && !scope) |
| 171 | return nullptr; |
| 172 | return DILabelAttr::get(context, scope, |
| 173 | name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 174 | file: translate(node: node->getFile()), line: node->getLine()); |
| 175 | } |
| 176 | |
| 177 | DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) { |
| 178 | // Return nullptr if the scope or type is a cyclic dependency. |
| 179 | DIScopeAttr scope = translate(node: node->getScope()); |
| 180 | if (node->getScope() && !scope) |
| 181 | return nullptr; |
| 182 | return DILexicalBlockAttr::get(context, scope, file: translate(node: node->getFile()), |
| 183 | line: node->getLine(), column: node->getColumn()); |
| 184 | } |
| 185 | |
| 186 | DILexicalBlockFileAttr |
| 187 | DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) { |
| 188 | // Return nullptr if the scope or type is a cyclic dependency. |
| 189 | DIScopeAttr scope = translate(node: node->getScope()); |
| 190 | if (node->getScope() && !scope) |
| 191 | return nullptr; |
| 192 | return DILexicalBlockFileAttr::get(context, scope, file: translate(node: node->getFile()), |
| 193 | discriminator: node->getDiscriminator()); |
| 194 | } |
| 195 | |
| 196 | DIGlobalVariableAttr |
| 197 | DebugImporter::translateImpl(llvm::DIGlobalVariable *node) { |
| 198 | // Names of DIGlobalVariables can be empty. MLIR models them as null, instead |
| 199 | // of empty strings, so this special handling is necessary. |
| 200 | auto convertToStringAttr = [&](StringRef name) -> StringAttr { |
| 201 | if (name.empty()) |
| 202 | return {}; |
| 203 | return StringAttr::get(context, bytes: node->getName()); |
| 204 | }; |
| 205 | return DIGlobalVariableAttr::get( |
| 206 | context, scope: translate(node: node->getScope()), |
| 207 | name: convertToStringAttr(node->getName()), |
| 208 | linkageName: convertToStringAttr(node->getLinkageName()), file: translate(node: node->getFile()), |
| 209 | line: node->getLine(), type: translate(node: node->getType()), isLocalToUnit: node->isLocalToUnit(), |
| 210 | isDefined: node->isDefinition(), alignInBits: node->getAlignInBits()); |
| 211 | } |
| 212 | |
| 213 | DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) { |
| 214 | // Return nullptr if the scope or type is a cyclic dependency. |
| 215 | DIScopeAttr scope = translate(node: node->getScope()); |
| 216 | if (node->getScope() && !scope) |
| 217 | return nullptr; |
| 218 | return DILocalVariableAttr::get( |
| 219 | context, scope, name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 220 | file: translate(node: node->getFile()), line: node->getLine(), arg: node->getArg(), |
| 221 | alignInBits: node->getAlignInBits(), type: translate(node: node->getType()), |
| 222 | flags: symbolizeDIFlags(node->getFlags()).value_or(u: DIFlags::Zero)); |
| 223 | } |
| 224 | |
| 225 | DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) { |
| 226 | return cast<DIVariableAttr>(Val: translate(node: static_cast<llvm::DINode *>(node))); |
| 227 | } |
| 228 | |
| 229 | DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) { |
| 230 | return cast<DIScopeAttr>(Val: translate(node: static_cast<llvm::DINode *>(node))); |
| 231 | } |
| 232 | |
| 233 | DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) { |
| 234 | return DIModuleAttr::get( |
| 235 | context, file: translate(node: node->getFile()), scope: translate(node: node->getScope()), |
| 236 | name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 237 | configMacros: getStringAttrOrNull(stringNode: node->getRawConfigurationMacros()), |
| 238 | includePath: getStringAttrOrNull(stringNode: node->getRawIncludePath()), |
| 239 | apinotes: getStringAttrOrNull(stringNode: node->getRawAPINotesFile()), line: node->getLineNo(), |
| 240 | isDecl: node->getIsDecl()); |
| 241 | } |
| 242 | |
| 243 | DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) { |
| 244 | return DINamespaceAttr::get(context, name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 245 | scope: translate(node: node->getScope()), |
| 246 | exportSymbols: node->getExportSymbols()); |
| 247 | } |
| 248 | |
| 249 | DIImportedEntityAttr |
| 250 | DebugImporter::translateImpl(llvm::DIImportedEntity *node) { |
| 251 | SmallVector<DINodeAttr> elements; |
| 252 | for (llvm::DINode *element : node->getElements()) { |
| 253 | assert(element && "expected a non-null element type" ); |
| 254 | elements.push_back(Elt: translate(node: element)); |
| 255 | } |
| 256 | |
| 257 | return DIImportedEntityAttr::get( |
| 258 | context, tag: node->getTag(), scope: translate(node: node->getScope()), |
| 259 | entity: translate(node: node->getEntity()), file: translate(node: node->getFile()), line: node->getLine(), |
| 260 | name: getStringAttrOrNull(stringNode: node->getRawName()), elements); |
| 261 | } |
| 262 | |
| 263 | DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) { |
| 264 | // Only definitions require a distinct identifier. |
| 265 | mlir::DistinctAttr id; |
| 266 | if (node->isDistinct()) |
| 267 | id = getOrCreateDistinctID(node); |
| 268 | |
| 269 | // Return nullptr if the scope or type is invalid. |
| 270 | DIScopeAttr scope = translate(node: node->getScope()); |
| 271 | if (node->getScope() && !scope) |
| 272 | return nullptr; |
| 273 | std::optional<DISubprogramFlags> subprogramFlags = |
| 274 | symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags()); |
| 275 | assert(subprogramFlags && "expected valid subprogram flags" ); |
| 276 | DISubroutineTypeAttr type = translate(node: node->getType()); |
| 277 | if (node->getType() && !type) |
| 278 | return nullptr; |
| 279 | |
| 280 | // Convert the retained nodes but drop all of them if one of them is invalid. |
| 281 | SmallVector<Attribute> retainedNodes; |
| 282 | auto add = [this, &retainedNodes](llvm::DINode *retainedNode) { |
| 283 | retainedNodes.push_back(Elt: translate(node: retainedNode)); |
| 284 | }; |
| 285 | auto addGVE = [](llvm::DIGlobalVariableExpression *GVE) { |
| 286 | // FIXME Import DIGlobalVariableExpressions from retainedNodes without |
| 287 | // duplicating them. |
| 288 | }; |
| 289 | node->forEachRetainedNode(FuncLV&: add, FuncLabel&: add, FuncIE&: add, FuncType&: add, FuncGVE&: addGVE); |
| 290 | if (llvm::is_contained(Range&: retainedNodes, Element: nullptr)) |
| 291 | retainedNodes.clear(); |
| 292 | |
| 293 | SmallVector<DINodeAttr> annotations; |
| 294 | // We currently only support `string` values for annotations on the MLIR side. |
| 295 | // Theoretically we could support other primitives, but LLVM is not using |
| 296 | // other types in practice. |
| 297 | if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) { |
| 298 | for (size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) { |
| 299 | const llvm::MDTuple *tuple = cast<llvm::MDTuple>(Val: rawAnns->getOperand(I: i)); |
| 300 | if (tuple->getNumOperands() != 2) |
| 301 | continue; |
| 302 | const llvm::MDString *name = cast<llvm::MDString>(Val: tuple->getOperand(I: 0)); |
| 303 | const llvm::MDString *value = |
| 304 | dyn_cast<llvm::MDString>(Val: tuple->getOperand(I: 1)); |
| 305 | if (name && value) { |
| 306 | annotations.push_back(Elt: DIAnnotationAttr::get( |
| 307 | context, name: StringAttr::get(context, bytes: name->getString()), |
| 308 | value: StringAttr::get(context, bytes: value->getString()))); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | return DISubprogramAttr::get(context, id, compileUnit: translate(node: node->getUnit()), scope, |
| 314 | name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 315 | linkageName: getStringAttrOrNull(stringNode: node->getRawLinkageName()), |
| 316 | file: translate(node: node->getFile()), line: node->getLine(), |
| 317 | scopeLine: node->getScopeLine(), subprogramFlags: *subprogramFlags, type, |
| 318 | retainedNodes, annotations); |
| 319 | } |
| 320 | |
| 321 | DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) { |
| 322 | auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute { |
| 323 | if (data.isNull()) |
| 324 | return nullptr; |
| 325 | if (auto *constInt = dyn_cast<llvm::ConstantInt *>(Val&: data)) |
| 326 | return IntegerAttr::get(type: IntegerType::get(context, width: 64), |
| 327 | value: constInt->getSExtValue()); |
| 328 | if (auto *expr = dyn_cast<llvm::DIExpression *>(Val&: data)) |
| 329 | return translateExpression(node: expr); |
| 330 | if (auto *var = dyn_cast<llvm::DIVariable *>(Val&: data)) { |
| 331 | if (auto *local = dyn_cast<llvm::DILocalVariable>(Val: var)) |
| 332 | return translate(node: local); |
| 333 | if (auto *global = dyn_cast<llvm::DIGlobalVariable>(Val: var)) |
| 334 | return translate(node: global); |
| 335 | return nullptr; |
| 336 | } |
| 337 | return nullptr; |
| 338 | }; |
| 339 | Attribute count = getAttrOrNull(node->getCount()); |
| 340 | Attribute upperBound = getAttrOrNull(node->getUpperBound()); |
| 341 | // Either count or the upper bound needs to be present. Otherwise, the |
| 342 | // metadata is invalid. The conversion might fail due to unsupported DI nodes. |
| 343 | if (!count && !upperBound) |
| 344 | return {}; |
| 345 | return DISubrangeAttr::get(context, count, |
| 346 | lowerBound: getAttrOrNull(node->getLowerBound()), upperBound, |
| 347 | stride: getAttrOrNull(node->getStride())); |
| 348 | } |
| 349 | |
| 350 | DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) { |
| 351 | return DICommonBlockAttr::get(context, scope: translate(node: node->getScope()), |
| 352 | decl: translate(node: node->getDecl()), |
| 353 | name: getStringAttrOrNull(stringNode: node->getRawName()), |
| 354 | file: translate(node: node->getFile()), line: node->getLineNo()); |
| 355 | } |
| 356 | |
| 357 | DIGenericSubrangeAttr |
| 358 | DebugImporter::translateImpl(llvm::DIGenericSubrange *node) { |
| 359 | auto getAttrOrNull = |
| 360 | [&](llvm::DIGenericSubrange::BoundType data) -> Attribute { |
| 361 | if (data.isNull()) |
| 362 | return nullptr; |
| 363 | if (auto *expr = dyn_cast<llvm::DIExpression *>(Val&: data)) |
| 364 | return translateExpression(node: expr); |
| 365 | if (auto *var = dyn_cast<llvm::DIVariable *>(Val&: data)) { |
| 366 | if (auto *local = dyn_cast<llvm::DILocalVariable>(Val: var)) |
| 367 | return translate(node: local); |
| 368 | if (auto *global = dyn_cast<llvm::DIGlobalVariable>(Val: var)) |
| 369 | return translate(node: global); |
| 370 | return nullptr; |
| 371 | } |
| 372 | return nullptr; |
| 373 | }; |
| 374 | Attribute count = getAttrOrNull(node->getCount()); |
| 375 | Attribute upperBound = getAttrOrNull(node->getUpperBound()); |
| 376 | Attribute lowerBound = getAttrOrNull(node->getLowerBound()); |
| 377 | Attribute stride = getAttrOrNull(node->getStride()); |
| 378 | // Either count or the upper bound needs to be present. Otherwise, the |
| 379 | // metadata is invalid. |
| 380 | if (!count && !upperBound) |
| 381 | return {}; |
| 382 | return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound, |
| 383 | stride); |
| 384 | } |
| 385 | |
| 386 | DISubroutineTypeAttr |
| 387 | DebugImporter::translateImpl(llvm::DISubroutineType *node) { |
| 388 | SmallVector<DITypeAttr> types; |
| 389 | for (llvm::DIType *type : node->getTypeArray()) { |
| 390 | if (!type) { |
| 391 | // A nullptr entry may appear at the beginning or the end of the |
| 392 | // subroutine types list modeling either a void result type or the type of |
| 393 | // a variadic argument. Translate the nullptr to an explicit |
| 394 | // DINullTypeAttr since the attribute list cannot contain a nullptr entry. |
| 395 | types.push_back(Elt: DINullTypeAttr::get(ctx: context)); |
| 396 | continue; |
| 397 | } |
| 398 | types.push_back(Elt: translate(node: type)); |
| 399 | } |
| 400 | // Return nullptr if any of the types is invalid. |
| 401 | if (llvm::is_contained(Range&: types, Element: nullptr)) |
| 402 | return nullptr; |
| 403 | return DISubroutineTypeAttr::get(context, callingConvention: node->getCC(), types); |
| 404 | } |
| 405 | |
| 406 | DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) { |
| 407 | return cast<DITypeAttr>(Val: translate(node: static_cast<llvm::DINode *>(node))); |
| 408 | } |
| 409 | |
| 410 | DINodeAttr DebugImporter::translate(llvm::DINode *node) { |
| 411 | if (!node) |
| 412 | return nullptr; |
| 413 | |
| 414 | // Check for a cached instance. |
| 415 | auto cacheEntry = cache.lookupOrInit(element: node); |
| 416 | if (std::optional<DINodeAttr> result = cacheEntry.get()) |
| 417 | return *result; |
| 418 | |
| 419 | // Convert the debug metadata if possible. |
| 420 | auto translateNode = [this](llvm::DINode *node) -> DINodeAttr { |
| 421 | if (auto *casted = dyn_cast<llvm::DIBasicType>(Val: node)) |
| 422 | return translateImpl(node: casted); |
| 423 | if (auto *casted = dyn_cast<llvm::DICommonBlock>(Val: node)) |
| 424 | return translateImpl(node: casted); |
| 425 | if (auto *casted = dyn_cast<llvm::DICompileUnit>(Val: node)) |
| 426 | return translateImpl(node: casted); |
| 427 | if (auto *casted = dyn_cast<llvm::DICompositeType>(Val: node)) |
| 428 | return translateImpl(node: casted); |
| 429 | if (auto *casted = dyn_cast<llvm::DIDerivedType>(Val: node)) |
| 430 | return translateImpl(node: casted); |
| 431 | if (auto *casted = dyn_cast<llvm::DIEnumerator>(Val: node)) |
| 432 | return translateImpl(node: casted); |
| 433 | if (auto *casted = dyn_cast<llvm::DIStringType>(Val: node)) |
| 434 | return translateImpl(node: casted); |
| 435 | if (auto *casted = dyn_cast<llvm::DIFile>(Val: node)) |
| 436 | return translateImpl(node: casted); |
| 437 | if (auto *casted = dyn_cast<llvm::DIGlobalVariable>(Val: node)) |
| 438 | return translateImpl(node: casted); |
| 439 | if (auto *casted = dyn_cast<llvm::DIImportedEntity>(Val: node)) |
| 440 | return translateImpl(node: casted); |
| 441 | if (auto *casted = dyn_cast<llvm::DILabel>(Val: node)) |
| 442 | return translateImpl(node: casted); |
| 443 | if (auto *casted = dyn_cast<llvm::DILexicalBlock>(Val: node)) |
| 444 | return translateImpl(node: casted); |
| 445 | if (auto *casted = dyn_cast<llvm::DILexicalBlockFile>(Val: node)) |
| 446 | return translateImpl(node: casted); |
| 447 | if (auto *casted = dyn_cast<llvm::DILocalVariable>(Val: node)) |
| 448 | return translateImpl(node: casted); |
| 449 | if (auto *casted = dyn_cast<llvm::DIModule>(Val: node)) |
| 450 | return translateImpl(node: casted); |
| 451 | if (auto *casted = dyn_cast<llvm::DINamespace>(Val: node)) |
| 452 | return translateImpl(node: casted); |
| 453 | if (auto *casted = dyn_cast<llvm::DISubprogram>(Val: node)) |
| 454 | return translateImpl(node: casted); |
| 455 | if (auto *casted = dyn_cast<llvm::DISubrange>(Val: node)) |
| 456 | return translateImpl(node: casted); |
| 457 | if (auto *casted = dyn_cast<llvm::DIGenericSubrange>(Val: node)) |
| 458 | return translateImpl(node: casted); |
| 459 | if (auto *casted = dyn_cast<llvm::DISubroutineType>(Val: node)) |
| 460 | return translateImpl(node: casted); |
| 461 | return nullptr; |
| 462 | }; |
| 463 | if (DINodeAttr attr = translateNode(node)) { |
| 464 | // If this node was repeated, lookup its recursive ID and assign it to the |
| 465 | // base result. |
| 466 | if (cacheEntry.wasRepeated()) { |
| 467 | DistinctAttr recId = nodeToRecId.lookup(Val: node); |
| 468 | auto recType = cast<DIRecursiveTypeAttrInterface>(Val&: attr); |
| 469 | attr = cast<DINodeAttr>(Val: recType.withRecId(recId)); |
| 470 | } |
| 471 | cacheEntry.resolve(result: attr); |
| 472 | return attr; |
| 473 | } |
| 474 | cacheEntry.resolve(result: nullptr); |
| 475 | return nullptr; |
| 476 | } |
| 477 | |
| 478 | /// Get the `getRecSelf` constructor for the translated node if it participates |
| 479 | /// in CyclicReplacerCache cycle breaking (recursive composite types, |
| 480 | /// subprograms, or compile units). |
| 481 | static function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)> |
| 482 | getRecSelfConstructor(llvm::DINode *node) { |
| 483 | using CtorType = function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>; |
| 484 | return TypeSwitch<llvm::DINode *, CtorType>(node) |
| 485 | .Case(caseFn: [&](llvm::DICompositeType *) { |
| 486 | return CtorType(DICompositeTypeAttr::getRecSelf); |
| 487 | }) |
| 488 | .Case(caseFn: [&](llvm::DISubprogram *) { |
| 489 | return CtorType(DISubprogramAttr::getRecSelf); |
| 490 | }) |
| 491 | .Case(caseFn: [&](llvm::DICompileUnit *) { |
| 492 | return CtorType(DICompileUnitAttr::getRecSelf); |
| 493 | }) |
| 494 | .Default(defaultResult: CtorType()); |
| 495 | } |
| 496 | |
| 497 | std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) { |
| 498 | auto recSelfCtor = getRecSelfConstructor(node); |
| 499 | if (!recSelfCtor) |
| 500 | return std::nullopt; |
| 501 | |
| 502 | // The original node may have already been assigned a recursive ID from |
| 503 | // a different self-reference. Use that if possible. |
| 504 | DistinctAttr recId = nodeToRecId.lookup(Val: node); |
| 505 | if (!recId) { |
| 506 | recId = DistinctAttr::create(referencedAttr: UnitAttr::get(context)); |
| 507 | nodeToRecId[node] = recId; |
| 508 | } |
| 509 | DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId); |
| 510 | return cast<DINodeAttr>(Val&: recSelf); |
| 511 | } |
| 512 | |
| 513 | //===----------------------------------------------------------------------===// |
| 514 | // Locations |
| 515 | //===----------------------------------------------------------------------===// |
| 516 | |
| 517 | Location DebugImporter::translateLoc(llvm::DILocation *loc) { |
| 518 | if (!loc) |
| 519 | return UnknownLoc::get(context); |
| 520 | |
| 521 | // Get the file location of the instruction. |
| 522 | Location result = FileLineColLoc::get(context, fileName: loc->getFilename(), |
| 523 | line: loc->getLine(), column: loc->getColumn()); |
| 524 | |
| 525 | // Add scope information. |
| 526 | assert(loc->getScope() && "expected non-null scope" ); |
| 527 | result = FusedLocWith<DIScopeAttr>::get(locs: {result}, metadata: translate(node: loc->getScope()), |
| 528 | context); |
| 529 | |
| 530 | // Add call site information, if available. |
| 531 | if (llvm::DILocation *inlinedAt = loc->getInlinedAt()) |
| 532 | result = CallSiteLoc::get(callee: result, caller: translateLoc(loc: inlinedAt)); |
| 533 | |
| 534 | return result; |
| 535 | } |
| 536 | |
| 537 | DIExpressionAttr DebugImporter::translateExpression(llvm::DIExpression *node) { |
| 538 | if (!node) |
| 539 | return nullptr; |
| 540 | |
| 541 | SmallVector<DIExpressionElemAttr> ops; |
| 542 | |
| 543 | // Begin processing the operations. |
| 544 | for (const llvm::DIExpression::ExprOperand &op : node->expr_ops()) { |
| 545 | SmallVector<uint64_t> operands; |
| 546 | operands.reserve(N: op.getNumArgs()); |
| 547 | for (const auto &i : llvm::seq(Size: op.getNumArgs())) |
| 548 | operands.push_back(Elt: op.getArg(I: i)); |
| 549 | const auto attr = DIExpressionElemAttr::get(context, opcode: op.getOp(), arguments: operands); |
| 550 | ops.push_back(Elt: attr); |
| 551 | } |
| 552 | return DIExpressionAttr::get(context, operations: ops); |
| 553 | } |
| 554 | |
| 555 | DIGlobalVariableExpressionAttr DebugImporter::translateGlobalVariableExpression( |
| 556 | llvm::DIGlobalVariableExpression *node) { |
| 557 | return DIGlobalVariableExpressionAttr::get( |
| 558 | context, var: translate(node: node->getVariable()), |
| 559 | expr: translateExpression(node: node->getExpression())); |
| 560 | } |
| 561 | |
| 562 | StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) { |
| 563 | if (!stringNode) |
| 564 | return StringAttr(); |
| 565 | return StringAttr::get(context, bytes: stringNode->getString()); |
| 566 | } |
| 567 | |
| 568 | DistinctAttr DebugImporter::getOrCreateDistinctID(llvm::DINode *node) { |
| 569 | DistinctAttr &id = nodeToDistinctAttr[node]; |
| 570 | if (!id) |
| 571 | id = DistinctAttr::create(referencedAttr: UnitAttr::get(context)); |
| 572 | return id; |
| 573 | } |
| 574 | |