Skip to content

Conversation

@ykhatav
Copy link
Contributor

@ykhatav ykhatav commented Apr 29, 2025

Unused types are retained in the debug info when -fno-eliminate-unused-debug-types is specified.
However, unused nested enums were not being emitted even with this option.
This patch fixes the missing emission of unused nested enums with -fno-eliminate-unused-debug-types

@ykhatav ykhatav requested review from bwyma and stevemerr April 29, 2025 16:35
@ykhatav ykhatav changed the title fix emiision of nested unused enum types with -fno-eliminate-unused-d… Emit nested unused enum types with -fno-eliminate-unused-debug-types May 5, 2025
@ykhatav ykhatav marked this pull request as ready for review May 5, 2025 17:44
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels May 5, 2025
@ykhatav ykhatav requested a review from pogo59 May 5, 2025 17:44
@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-clang-codegen

Author: ykhatav (ykhatav)

Changes

Unused types are retained in the debug info when -fno-eliminate-unused-debug-types is specified.
However, unused nested enums were not being emitted even with this option.
This patch fixes the missing emission of unused nested enums with -fno-eliminate-unused-debug-types


Full diff: https://github.com/llvm/llvm-project/pull/137818.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-1)
  • (added) clang/test/CodeGen/unused_nested_enump.cpp (+23)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index e917f3c42da06..d63829010b3de 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -7105,7 +7105,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
     }
     // Emit any static data members, they may be definitions.
     for (auto *I : CRD->decls())
-      if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
+      if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I) || isa<EnumDecl>(I))
         EmitTopLevelDecl(I);
     break;
   }
diff --git a/clang/test/CodeGen/unused_nested_enump.cpp b/clang/test/CodeGen/unused_nested_enump.cpp
new file mode 100644
index 0000000000000..7689b5bd7561b
--- /dev/null
+++ b/clang/test/CodeGen/unused_nested_enump.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -debug-info-kind=unused-types  -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - %s | FileCheck --check-prefix=NOUNUSEDTYPE %s
+
+struct Type {
+    enum { Unused };
+    int value = 0;
+};
+int main() {
+    Type t;
+    return t.value;
+}
+
+// CHECK:  !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME: scope: ![[STRUCT:[0-9]+]]
+// CHECK-SAME: elements: ![[ELEMENTS:[0-9]+]]
+
+// CHECK: ![[STRUCT]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Type"
+
+// CHECK: ![[ELEMENTS]] = !{![[ENUMERATOR:[0-9]+]]}
+// CHECK: ![[ENUMERATOR]] = !DIEnumerator(name: "Unused", value: 0
+
+
+// NOUNUSEDTYPE-NOT: !DIEnumerator(name: "Unused"

@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-clang

Author: ykhatav (ykhatav)

Changes

Unused types are retained in the debug info when -fno-eliminate-unused-debug-types is specified.
However, unused nested enums were not being emitted even with this option.
This patch fixes the missing emission of unused nested enums with -fno-eliminate-unused-debug-types


Full diff: https://github.com/llvm/llvm-project/pull/137818.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-1)
  • (added) clang/test/CodeGen/unused_nested_enump.cpp (+23)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index e917f3c42da06..d63829010b3de 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -7105,7 +7105,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
     }
     // Emit any static data members, they may be definitions.
     for (auto *I : CRD->decls())
-      if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
+      if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I) || isa<EnumDecl>(I))
         EmitTopLevelDecl(I);
     break;
   }
diff --git a/clang/test/CodeGen/unused_nested_enump.cpp b/clang/test/CodeGen/unused_nested_enump.cpp
new file mode 100644
index 0000000000000..7689b5bd7561b
--- /dev/null
+++ b/clang/test/CodeGen/unused_nested_enump.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -debug-info-kind=unused-types  -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -debug-info-kind=limited -emit-llvm -o - %s | FileCheck --check-prefix=NOUNUSEDTYPE %s
+
+struct Type {
+    enum { Unused };
+    int value = 0;
+};
+int main() {
+    Type t;
+    return t.value;
+}
+
+// CHECK:  !DICompositeType(tag: DW_TAG_enumeration_type
+// CHECK-SAME: scope: ![[STRUCT:[0-9]+]]
+// CHECK-SAME: elements: ![[ELEMENTS:[0-9]+]]
+
+// CHECK: ![[STRUCT]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Type"
+
+// CHECK: ![[ELEMENTS]] = !{![[ENUMERATOR:[0-9]+]]}
+// CHECK: ![[ENUMERATOR]] = !DIEnumerator(name: "Unused", value: 0
+
+
+// NOUNUSEDTYPE-NOT: !DIEnumerator(name: "Unused"

@ykhatav ykhatav requested a review from dwblaikie May 5, 2025 17:44
@ykhatav
Copy link
Contributor Author

ykhatav commented May 12, 2025

Ping!

@ykhatav
Copy link
Contributor Author

ykhatav commented May 12, 2025

FYI fixes: #64160

@ykhatav ykhatav merged commit 3cf280c into llvm:main May 13, 2025
15 checks passed
@ykhatav ykhatav deleted the nested-unused-enum-type1 branch May 13, 2025 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants