diff --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp --- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp @@ -119,13 +119,34 @@ /*Flags=*/"", /*RV=*/0); } +/// Returns a new `DINodeT` that is either distinct or not, depending on +/// `isDistinct`. +template +static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) { + if (isDistinct) + return DINodeT::getDistinct(std::forward(args)...); + return DINodeT::get(std::forward(args)...); +} + llvm::DICompositeType * DebugTranslation::translateImpl(DICompositeTypeAttr attr) { SmallVector elements; for (auto member : attr.getElements()) elements.push_back(translate(member)); - return llvm::DICompositeType::get( - llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()), + + // TODO: Use distinct attributes to model this, once they have landed. + // Depending on the tag, composite types must be distinct. + bool isDistinct = false; + switch (attr.getTag()) { + case llvm::dwarf::DW_TAG_class_type: + case llvm::dwarf::DW_TAG_enumeration_type: + case llvm::dwarf::DW_TAG_structure_type: + case llvm::dwarf::DW_TAG_union_type: + isDistinct = true; + } + + return getDistinctOrUnique( + isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()), translate(attr.getFile()), attr.getLine(), translate(attr.getScope()), translate(attr.getBaseType()), attr.getSizeInBits(), attr.getAlignInBits(), @@ -186,19 +207,10 @@ return cast(translate(DINodeAttr(attr))); } -/// Return a new subprogram that is either distinct or not, depending on -/// `isDistinct`. -template -static llvm::DISubprogram *getSubprogram(bool isDistinct, Ts &&...args) { - if (isDistinct) - return llvm::DISubprogram::getDistinct(std::forward(args)...); - return llvm::DISubprogram::get(std::forward(args)...); -} - llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) { bool isDefinition = static_cast(attr.getSubprogramFlags() & LLVM::DISubprogramFlags::Definition); - return getSubprogram( + return getDistinctOrUnique( isDefinition, llvmCtx, translate(attr.getScope()), getMDStringOrNull(attr.getName()), getMDStringOrNull(attr.getLinkageName()), translate(attr.getFile()), diff --git a/mlir/test/Target/LLVMIR/llvmir-debug.mlir b/mlir/test/Target/LLVMIR/llvmir-debug.mlir --- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir +++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir @@ -129,7 +129,7 @@ // CHECK: ![[PTR_TYPE]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: ![[BASE_TYPE:.*]], size: 64, align: 32, offset: 8) // CHECK: ![[BASE_TYPE]] = !DIBasicType(name: "si32", size: 32, encoding: DW_ATE_signed) // CHECK: ![[NAMED_TYPE]] = !DIDerivedType(tag: DW_TAG_pointer_type, name: "named", baseType: ![[BASE_TYPE:.*]]) -// CHECK: ![[COMPOSITE_TYPE]] = !DICompositeType(tag: DW_TAG_structure_type, name: "composite", file: ![[CU_FILE_LOC]], line: 42, size: 64, align: 32, elements: ![[COMPOSITE_ELEMENTS:.*]]) +// CHECK: ![[COMPOSITE_TYPE]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "composite", file: ![[CU_FILE_LOC]], line: 42, size: 64, align: 32, elements: ![[COMPOSITE_ELEMENTS:.*]]) // CHECK: ![[COMPOSITE_ELEMENTS]] = !{![[COMPOSITE_ELEMENT:.*]]} // CHECK: ![[COMPOSITE_ELEMENT]] = !DISubrange(count: 4) // CHECK: ![[VECTOR_TYPE]] = !DICompositeType(tag: DW_TAG_array_type, name: "array", file: ![[CU_FILE_LOC]], baseType: ![[ARG_TYPE]], flags: DIFlagVector, elements: ![[VECTOR_ELEMENTS:.*]])