[Mlir-commits] [mlir] 3dc1b16 - [mlir][LLVM] Export DICompositeType as distinct MD when necessary

Christian Ulmann llvmlistbot at llvm.org
Mon Jul 10 00:00:52 PDT 2023


Author: Christian Ulmann
Date: 2023-07-10T07:00:10Z
New Revision: 3dc1b1623931940819b8efdd7021840706e33c7f

URL: https://github.com/llvm/llvm-project/commit/3dc1b1623931940819b8efdd7021840706e33c7f
DIFF: https://github.com/llvm/llvm-project/commit/3dc1b1623931940819b8efdd7021840706e33c7f.diff

LOG: [mlir][LLVM] Export DICompositeType as distinct MD when necessary

This commit ensures that DICompositeTypes are exported as distinct
metadata nodes for structures, classes, unions, and enumerations.
Not emitting these as distinct metadata can potentially cause linking
issues involving debug sections.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D154712

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/DebugTranslation.cpp
    mlir/test/Target/LLVMIR/llvmir-debug.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
index e421a117691e96..b76a48eb02dca9 100644
--- a/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/DebugTranslation.cpp
@@ -119,13 +119,34 @@ llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
       /*Flags=*/"", /*RV=*/0);
 }
 
+/// Returns a new `DINodeT` that is either distinct or not, depending on
+/// `isDistinct`.
+template <class DINodeT, class... Ts>
+static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) {
+  if (isDistinct)
+    return DINodeT::getDistinct(std::forward<Ts>(args)...);
+  return DINodeT::get(std::forward<Ts>(args)...);
+}
+
 llvm::DICompositeType *
 DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
   SmallVector<llvm::Metadata *> 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<llvm::DICompositeType>(
+      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 @@ llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
   return cast<llvm::DIScope>(translate(DINodeAttr(attr)));
 }
 
-/// Return a new subprogram that is either distinct or not, depending on
-/// `isDistinct`.
-template <class... Ts>
-static llvm::DISubprogram *getSubprogram(bool isDistinct, Ts &&...args) {
-  if (isDistinct)
-    return llvm::DISubprogram::getDistinct(std::forward<Ts>(args)...);
-  return llvm::DISubprogram::get(std::forward<Ts>(args)...);
-}
-
 llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
   bool isDefinition = static_cast<bool>(attr.getSubprogramFlags() &
                                         LLVM::DISubprogramFlags::Definition);
-  return getSubprogram(
+  return getDistinctOrUnique<llvm::DISubprogram>(
       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
index 8fbe5cc73ad159..4ea6ab1e1d6ecf 100644
--- a/mlir/test/Target/LLVMIR/llvmir-debug.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir-debug.mlir
@@ -129,7 +129,7 @@ llvm.func @empty_types() {
 // 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:.*]])


        


More information about the Mlir-commits mailing list