[llvm] [llvm][MetadataLoader] Make sure we correctly load DW_APPLE_ENUM_KIND from bitcode (PR #132374)

Michael Buch via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 21 16:30:19 PDT 2025


https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/132374

>From bf49ba31515253564e57c444aca25096037028e9 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 21 Mar 2025 11:16:29 +0000
Subject: [PATCH 1/3] [llvm][MetadataLoader] Make sure we correctly load
 DW_APPLE_ENUM_KIND from bitcode

This was pointed out in
https://github.com/llvm/llvm-project/pull/124752#issuecomment-2730052773

There was not test that roundtrips this attribute through LLVM bitcode, so this was never caught.
---
 llvm/lib/Bitcode/Reader/MetadataLoader.cpp           | 4 ++--
 llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
index 1baf0a9214e00..12794d3346e3f 100644
--- a/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ b/llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -1704,8 +1704,8 @@ Error MetadataLoader::MetadataLoaderImpl::parseOneMetadata(
       }
     }
 
-    if (Record.size() > 25 && Record[25] != dwarf::DW_APPLE_ENUM_KIND_invalid)
-      EnumKind = Record[25];
+    if (Record.size() > 24 && Record[24] != dwarf::DW_APPLE_ENUM_KIND_invalid)
+      EnumKind = Record[24];
 
     DICompositeType *CT = nullptr;
     if (Identifier)
diff --git a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
index 399d80c778072..4f219a7e7b12e 100644
--- a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
+++ b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
@@ -1,5 +1,6 @@
-; RUN: llc < %s -filetype=obj -o %t
-; RUN: llvm-dwarfdump -v %t | FileCheck %s
+; RUN: clang++ %s -c -g -emit-llvm -o %t.bc
+; RUN: llc %t.bc -filetype=obj -o %t.o
+; RUN: llvm-dwarfdump -v %t.o | FileCheck %s
 
 ; C++ source to regenerate:
 ; enum __attribute__((enum_extensibility(open))) OpenEnum {

>From 228d30a5909c6e59e8326d9864e99fbb5cae2287 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 21 Mar 2025 15:40:20 +0000
Subject: [PATCH 2/3] fixup! remove clang dependency; roundtrip through
 llvm-dis/llvm-as instead

---
 llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
index 4f219a7e7b12e..750e7709a326c 100644
--- a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
+++ b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
@@ -1,6 +1,7 @@
-; RUN: clang++ %s -c -g -emit-llvm -o %t.bc
-; RUN: llc %t.bc -filetype=obj -o %t.o
-; RUN: llvm-dwarfdump -v %t.o | FileCheck %s
+; RUN: llc < %s -filetype=obj -o %t
+; RUN: llvm-dwarfdump -v %t | FileCheck %s
+;
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s --check-prefix=CHECK-METADATA
 
 ; C++ source to regenerate:
 ; enum __attribute__((enum_extensibility(open))) OpenEnum {
@@ -13,6 +14,8 @@
 ; 
 ; $ clang++ -O0 -g debug-info-enum-kind.cpp -c
 
+; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Open
+; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Closed
 
 ; CHECK: .debug_abbrev contents:
 

>From c8d308b7d2bfd0f8b58e3b0105b69c0d73f9d00b Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 21 Mar 2025 23:29:36 +0000
Subject: [PATCH 3/3] fixup! move rountrip test to Bitcode directory

---
 llvm/test/Bitcode/DW_AT_APPLE_enum_kind.ll    | 42 +++++++++++++++++++
 .../AArch64/DW_AT_APPLE_enum_kind.ll          |  5 ---
 2 files changed, 42 insertions(+), 5 deletions(-)
 create mode 100644 llvm/test/Bitcode/DW_AT_APPLE_enum_kind.ll

diff --git a/llvm/test/Bitcode/DW_AT_APPLE_enum_kind.ll b/llvm/test/Bitcode/DW_AT_APPLE_enum_kind.ll
new file mode 100644
index 0000000000000..f25b9f66ca8f2
--- /dev/null
+++ b/llvm/test/Bitcode/DW_AT_APPLE_enum_kind.ll
@@ -0,0 +1,42 @@
+; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s --check-prefix=CHECK-METADATA
+
+; C++ source to regenerate:
+; enum __attribute__((enum_extensibility(open))) OpenEnum {
+;   oe1
+; } oe;
+; 
+; enum __attribute__((enum_extensibility(closed))) ClosedEnum {
+;   ce1
+; } ce;
+; 
+; $ clang++ -O0 -g debug-info-enum-kind.cpp -c
+
+; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Open
+; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Closed
+
+source_filename = "enum.cpp"
+target triple = "arm64-apple-macosx"
+
+ at oe = global i32 0, align 4, !dbg !0
+ at ce = global i32 0, align 4, !dbg !13
+
+!llvm.dbg.cu = !{!2}
+!llvm.module.flags = !{!15, !16}
+
+!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
+!1 = distinct !DIGlobalVariable(name: "oe", scope: !2, file: !3, line: 3, type: !5, isLocal: false, isDefinition: true)
+!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !3, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !12, splitDebugInlining: false, nameTableKind: Apple, sysroot: "/")
+!3 = !DIFile(filename: "enum.cpp", directory: "/tmp")
+!4 = !{!5, !9}
+!5 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "OpenEnum", file: !3, line: 1, baseType: !6, size: 32, elements: !7, identifier: "_ZTS8OpenEnum", enumKind: DW_APPLE_ENUM_KIND_Open)
+!6 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
+!7 = !{!8}
+!8 = !DIEnumerator(name: "oe1", value: 0, isUnsigned: true)
+!9 = !DICompositeType(tag: DW_TAG_enumeration_type, name: "ClosedEnum", file: !3, line: 5, baseType: !6, size: 32, elements: !10, identifier: "_ZTS10ClosedEnum", enumKind: DW_APPLE_ENUM_KIND_Closed)
+!10 = !{!11}
+!11 = !DIEnumerator(name: "ce1", value: 0, isUnsigned: true)
+!12 = !{!0, !13}
+!13 = !DIGlobalVariableExpression(var: !14, expr: !DIExpression())
+!14 = distinct !DIGlobalVariable(name: "ce", scope: !2, file: !3, line: 7, type: !9, isLocal: false, isDefinition: true)
+!15 = !{i32 7, !"Dwarf Version", i32 5}
+!16 = !{i32 2, !"Debug Info Version", i32 3}
diff --git a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
index 750e7709a326c..8440d97b24dbf 100644
--- a/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
+++ b/llvm/test/DebugInfo/AArch64/DW_AT_APPLE_enum_kind.ll
@@ -1,8 +1,6 @@
 ; RUN: llc < %s -filetype=obj -o %t
 ; RUN: llvm-dwarfdump -v %t | FileCheck %s
 ;
-; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s --check-prefix=CHECK-METADATA
-
 ; C++ source to regenerate:
 ; enum __attribute__((enum_extensibility(open))) OpenEnum {
 ;   oe1
@@ -14,9 +12,6 @@
 ; 
 ; $ clang++ -O0 -g debug-info-enum-kind.cpp -c
 
-; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Open
-; CHECK-METADATA: enumKind: DW_APPLE_ENUM_KIND_Closed
-
 ; CHECK: .debug_abbrev contents:
 
 ; CHECK: [3] DW_TAG_enumeration_type DW_CHILDREN_yes



More information about the llvm-commits mailing list