[llvm] [DebugInfo] Add Verifier check for local enums in CU's enums field (PR #185228)

via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 7 12:33:45 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Vladislav Dzhidzhoev (dzhidzhoev)

<details>
<summary>Changes</summary>

Since #<!-- -->165032, DwarfDebug asserts if function-local enums are present in the enums field of DICompileUnit.
This patch adds a check to the Verifier to detect such invalid IR earlier.

Incorrect occurence of a local enum in DICompileUnit's enums field in `llvm/test/DebugInfo/COFF/enum-co.ll` is fixed.

This change is extracted from https://reviews.llvm.org/D144008.

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


2 Files Affected:

- (modified) llvm/lib/IR/Verifier.cpp (+4) 
- (modified) llvm/test/DebugInfo/COFF/enum-co.ll (+3-2) 


``````````diff
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index f986f5406b2b3..0bf3141289680 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1574,6 +1574,10 @@ void Verifier::visitDICompileUnit(const DICompileUnit &N) {
       auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
       CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
               "invalid enum type", &N, N.getEnumTypes(), Op);
+      if (Enum->getScope())
+        CheckDI(!isa<DILocalScope>(Enum->getScope()),
+                "function-local enum in a DICompileUnit's enum list", &N,
+                N.getEnumTypes(), Op);
     }
   }
   if (auto *Array = N.getRawRetainedTypes()) {
diff --git a/llvm/test/DebugInfo/COFF/enum-co.ll b/llvm/test/DebugInfo/COFF/enum-co.ll
index a334b353e009a..d333577550494 100644
--- a/llvm/test/DebugInfo/COFF/enum-co.ll
+++ b/llvm/test/DebugInfo/COFF/enum-co.ll
@@ -134,7 +134,7 @@ attributes #1 = { nounwind readnone speculatable }
 
 !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 8.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, nameTableKind: None)
 !1 = !DIFile(filename: "enum-co.cpp", directory: "D:\5Cupstream\5Cllvm\5Ctest\5CDebugInfo\5CCOFF", checksumkind: CSK_MD5, checksum: "2e53b90441669acca735bad28ed3a1ab")
-!2 = !{!3, !8, !13, !18}
+!2 = !{!3, !8, !18}
 !3 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "Enum", file: !1, line: 4, baseType: !4, size: 32, elements: !5, identifier: ".?AW4Enum@@")
 !4 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
 !5 = !{!6, !7}
@@ -146,7 +146,7 @@ attributes #1 = { nounwind readnone speculatable }
 !11 = !DIEnumerator(name: "BLUE", value: 1)
 !12 = !DIEnumerator(name: "NOTCARE", value: 2)
 !13 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "ScopedEnum", scope: !14, file: !1, line: 11, baseType: !4, size: 32, elements: !5, identifier: ".?AW4ScopedEnum@?1??Func@@YAXXZ@")
-!14 = distinct !DISubprogram(name: "Func", linkageName: "?Func@@YAXXZ", scope: !1, file: !1, line: 10, type: !15, isLocal: false, isDefinition: true, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !17)
+!14 = distinct !DISubprogram(name: "Func", linkageName: "?Func@@YAXXZ", scope: !1, file: !1, line: 10, type: !15, isLocal: false, isDefinition: true, scopeLine: 10, flags: DIFlagPrototyped, isOptimized: false, unit: !0, retainedNodes: !47)
 !15 = !DISubroutineType(types: !16)
 !16 = !{null}
 !17 = !{}
@@ -179,3 +179,4 @@ attributes #1 = { nounwind readnone speculatable }
 !44 = !DILocalVariable(name: "S", scope: !14, file: !1, line: 20, type: !20)
 !45 = !DILocation(line: 20, scope: !14)
 !46 = !DILocation(line: 21, scope: !14, isImplicitCode: true)
+!47 = !{!13}

``````````

</details>


https://github.com/llvm/llvm-project/pull/185228


More information about the llvm-commits mailing list