[llvm] [DWARFLinker][Parallel] Stop the qualified name walk at the unit root (PR #219747)

Arcadiy Ivanov via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 19:14:08 PDT 2026


https://github.com/arcivanov created https://github.com/llvm/llvm-project/pull/219747

Depends on #219615.

The Apple accelerator tables carry a `DW_ATOM_qual_name_hash` for every type, a hash of the type's fully qualified name that a consumer looks types up by. `hashFullyQualifiedName` builds it by walking up the parents and stopping at the unit root, and it recognized the unit root by testing the parent's tag against `DW_TAG_compile_unit` alone.

`DW_TAG_partial_unit` is the root tag `dwz` emits, so this is the ordinary shape of a Fedora, RHEL or Debian debuginfo package. A partial unit root is a valid parent that is not tagged `DW_TAG_compile_unit`, so the walk did not stop there. It recursed into the root and folded the root's `DW_AT_name` — a source file path, not an identifier — into the hash as though the unit were an enclosing scope of the type.

The classic backend ends the same walk on `ParentIdx == 0` and is already correct whatever tag the root carries. The two backends therefore produced different hashes for the same type on the same input, which is the one thing an accelerator table hash cannot afford.

`hashFullyQualifiedName` now tests the parent's position. `CompileUnit::isUnitRootDIE` is the helper #219615 introduces for the two `cloneDIE()` gates, reused here rather than respelled; index zero is the unit root whatever tag it carries, which is the invariant the tag comparison stood in for.

DWARFv5 section 3.1.1 settles which of the two backends was right. It places the containing scope of a partial unit's declarations at the entries that import it rather than at the unit itself, and item 2 of the same section makes the root's `DW_AT_name` the path of a source file. A type at unit scope in a partial unit therefore has no enclosing scope to contribute a component, and classic's hash was the correct one all along.

Measured on the test input's `struct Foo` at unit scope, the parallel backend produced `0xf9a84c7e` under a partial unit root against `0x0c3993dd` under a compile unit root. Both roots now give `0x0c3993dd`, which is what classic gives for both and what the qualified name `Foo` hashes to.

Only `dsymutil` reaches this code. `llvm-dwarfutil` accepts `--build-accelerator=none` and `--build-accelerator=DWARF` and nothing else, and the hash is written solely into the Apple tables, so it is computed and discarded there.

The test assembles one source twice with `llvm-mc --defsym`, changing only the root tag and the unit type, and links each of the two objects with both backends. All four runs are checked against a single `CHECK` block, which is the claim being made: the two backends agree with each other, and neither varies with the root tag.

The input carries a `struct Foo` at unit scope and a `struct Bar` inside a namespace, and both are pinned. The pair is what separates ending the walk at the unit root from ending it unconditionally — a fix that simply stopped recursing gets `Foo` right and `Bar` wrong, since `ns` has to be folded in. Only `Atom[0]`, the output DIE offset, is left unmatched, because the two backends lay the linked unit out differently.

The two partial unit runs pass `--verify-dwarf=none`. Both backends leave a partial unit root without a `DW_AT_str_offsets_base` and record `DW_UT_compile` in the unit header whatever tag the root carries, so `dsymutil`'s default output verification rejects either link on grounds unrelated to this hash. That is #219365, fixed by #219398, and stacking this change on that one as well seemed the wrong trade for a check the fix here does not need. The two compile unit control runs keep verification on.

Known limit, since it will be asked: the test cannot distinguish the positional predicate from a tag list naming all four unit root tags. Those four are every root tag that exists, so there is no fifth to feed it.

Fixes #219636.


>From be6e72d198c28c4321ca528fbe946e4d26229059 Mon Sep 17 00:00:00 2001
From: Arcadiy Ivanov <arcadiy at ivanov.biz>
Date: Fri, 28 Aug 2026 22:31:55 -0400
Subject: [PATCH 1/2] [DWARFLinker][Parallel] Identify a unit root by position,
 not by tag

cloneDIE() has two gates that must recognize a unit's root DIE. The root
is never assigned a type entry, so it must not be cloned into the
artificial type unit; and because it is not cloned there, the recursion
into its type children has to be enabled by a separate test. Both gates
asked whether the tag was DW_TAG_compile_unit.

A DW_TAG_partial_unit root answers no to both. That is the unit dwz
emits, so it arrives through any Fedora, RHEL or Debian debuginfo
package, and it reaches the linker on the same path a compile unit root
does. Marking a type for the artificial type unit sets KeepTypeChildren
on every ancestor of that type, unit root included, and
needToPlaceInTypeTable() is true for anything carrying that flag, so the
first gate let the partial unit root through and
createTypeDIEandCloneAttributes() asked for the type entry that no unit
root is ever assigned - an assertion failure on Entry != nullptr with
assertions enabled, a null dereference without them. Where the root's
only kept content is types, the second gate then dropped the whole
subtree after its type names had already been registered, which fails
later at patch application with "No data for type <name>".

Both gates now test the DIE index instead of the tag. The root is always
at index zero in DWARFUnit's DIE array whatever its tag, which is the
invariant the tag comparison was standing in for. The producer side is
already tag-agnostic - getTypeDeduplicationCandidate() returns nullopt
for every unit tag and assignTypeNames() names only children of
getDebugInfoEntry(0) - so this makes the consumer match.

Only the parallel backend is affected.

Fixes #219613.

Assisted-by: Claude Code
---
 .../Parallel/DWARFLinkerCompileUnit.cpp       |   6 +-
 .../Parallel/DWARFLinkerCompileUnit.h         |   5 +
 .../X86/dwarf5-partial-unit-namespace.test    | 178 ++++++++++++++++++
 .../X86/dwarf5-partial-unit-types-only.test   | 168 +++++++++++++++++
 4 files changed, 353 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-namespace.test
 create mode 100644 llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-types-only.test

diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
index 4138a9fd69aca..126cac340a49d 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
@@ -1419,8 +1419,7 @@ std::pair<DIE *, TypeEntry *> CompileUnit::cloneDIE(
 
   bool NeedToClonePlainDIE = Info.needToKeepInPlainDwarf();
   bool NeedToCloneTypeDIE =
-      (InputDieEntry->getTag() != dwarf::DW_TAG_compile_unit) &&
-      Info.needToPlaceInTypeTable();
+      !isUnitRootDIE(InputDieIdx) && Info.needToPlaceInTypeTable();
   std::pair<DIE *, TypeEntry *> ClonedDIE;
 
   DIEGenerator PlainDIEGenerator(Allocator, *this);
@@ -1449,8 +1448,7 @@ std::pair<DIE *, TypeEntry *> CompileUnit::cloneDIE(
       (ClonedDIE.first && Info.getKeepPlainChildren());
 
   bool HasTypeChildrenToClone =
-      ((ClonedDIE.second ||
-        InputDieEntry->getTag() == dwarf::DW_TAG_compile_unit) &&
+      ((ClonedDIE.second || isUnitRootDIE(InputDieIdx)) &&
        Info.getKeepTypeChildren());
 
   // Recursively clone children.
diff --git a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
index c8c1b8822aa64..70b90727f4df4 100644
--- a/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
+++ b/llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.h
@@ -335,6 +335,11 @@ class alignas(8) CompileUnit : public DwarfUnit {
   ///
   /// @{
 
+  /// \p DieIdx index of the DIE.
+  /// \returns true if the DIE is the unit's root, which is always at index
+  /// zero, whatever tag it carries.
+  static bool isUnitRootDIE(uint32_t DieIdx) { return DieIdx == 0; }
+
   /// \p Idx index of the DIE.
   /// \returns DieInfo descriptor.
   DIEInfo &getDIEInfo(unsigned Idx) { return DieInfoArray[Idx]; }
diff --git a/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-namespace.test b/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-namespace.test
new file mode 100644
index 0000000000000..357d829a34b2c
--- /dev/null
+++ b/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-namespace.test
@@ -0,0 +1,178 @@
+## A DW_TAG_partial_unit root - what dwz emits, and what Fedora, RHEL and
+## Debian debuginfo packaging therefore ships - was kept out of the artificial
+## type unit by nothing but a test on its tag, so it was cloned as a type DIE
+## and asked for the type entry that no unit root is ever assigned. Reaching
+## that point needs a scope between the type and the unit root, because the
+## walk that looks for a type's enclosing scope stops at a namespace but runs
+## past a unit root. A DW_TAG_compile_unit root is linked alongside as the
+## control.
+##
+## namespace ns {
+##   struct S { int m1; };
+## }
+##
+## ns::S foo1() { ... }
+
+# RUN: yaml2obj %s -DROOT=DW_TAG_compile_unit -DUNITTYPE=DW_UT_compile -o %t.cu.o
+# RUN: llvm-dwarfutil %t.cu.o %t.cu.classic
+# RUN: llvm-dwarfdump --debug-info %t.cu.classic | FileCheck %s --check-prefixes=CLASSIC,CLASSIC-CU
+# RUN: llvm-dwarfutil --linker parallel %t.cu.o %t.cu.parallel
+# RUN: llvm-dwarfdump --debug-info %t.cu.parallel | FileCheck %s --check-prefixes=PARALLEL,PARALLEL-CU
+
+# RUN: yaml2obj %s -DROOT=DW_TAG_partial_unit -DUNITTYPE=DW_UT_partial -o %t.pu.o
+# RUN: llvm-dwarfutil %t.pu.o %t.pu.classic
+# RUN: llvm-dwarfdump --debug-info %t.pu.classic | FileCheck %s --check-prefixes=CLASSIC,CLASSIC-PU
+# RUN: llvm-dwarfutil --linker parallel %t.pu.o %t.pu.parallel
+# RUN: llvm-dwarfdump --debug-info %t.pu.parallel | FileCheck %s --check-prefixes=PARALLEL,PARALLEL-PU
+
+## The classic backend leaves the type where it found it, and the root tag
+## survives linking. llvm-dwarfdump qualifies a type name with the names of its
+## enclosing scopes and a partial unit root is one, which a compile unit root
+## is not, so the type is printed as "U01::ns::S" for the partial unit run and
+## as "ns::S" for the control. The reference target is the same DIE either way.
+# CLASSIC-CU:     DW_TAG_compile_unit
+# CLASSIC-PU:     DW_TAG_partial_unit
+# CLASSIC:        DW_TAG_subprogram
+# CLASSIC-NEXT:     DW_AT_name ("foo1")
+# CLASSIC-CU:       DW_AT_type (0x00000000[[S:[0-9a-f]{8}]] "ns::S")
+# CLASSIC-PU:       DW_AT_type (0x00000000[[S:[0-9a-f]{8}]] "U01::ns::S")
+# CLASSIC:        DW_TAG_namespace
+# CLASSIC-NEXT:     DW_AT_name ("ns")
+# CLASSIC:      0x[[S]]:     DW_TAG_structure_type
+# CLASSIC-NEXT:   DW_AT_name ("S")
+# CLASSIC:          DW_TAG_member
+# CLASSIC-NEXT:       DW_AT_name ("m1")
+
+## The parallel backend puts the type in the artificial type unit, under a copy
+## of the namespace that held it, and the unit's reference resolves there. The
+## unit root itself stays out of that unit, which is what this pins: it has no
+## name of its own to be deduplicated under, and cloning it as a type DIE is
+## what used to crash.
+# PARALLEL:      DW_AT_name ("__artificial_type_unit")
+# PARALLEL:      DW_TAG_namespace
+# PARALLEL-NEXT:   DW_AT_name ("ns")
+# PARALLEL:      0x[[S:[0-9a-f]{8}]]:     DW_TAG_structure_type
+# PARALLEL-NEXT:   DW_AT_name ("S")
+# PARALLEL:          DW_TAG_member
+# PARALLEL-NEXT:       DW_AT_name ("m1")
+# PARALLEL-CU:   DW_TAG_compile_unit
+# PARALLEL-PU:   DW_TAG_partial_unit
+# PARALLEL-NEXT:   DW_AT_producer ("by_hand")
+# PARALLEL-NEXT:   DW_AT_name ("U01")
+# PARALLEL:      DW_TAG_subprogram
+# PARALLEL-NEXT:   DW_AT_name ("foo1")
+# PARALLEL:        DW_AT_type (0x00000000[[S]] "ns::S")
+
+## One DWARFv5 unit with a function in .text returning struct S, which is
+## defined in namespace ns. The root carries its own DW_AT_str_offsets_base, so
+## the strings the linker rewrites into DW_FORM_strx stay resolvable on output;
+## the linker does not synthesize that attribute for a partial unit root by
+## itself.
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2LSB
+  Type:     ET_REL
+  Machine:  EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x1130
+    Size:            0x10
+  - Name:            .debug_str_offsets
+    Type:            SHT_PROGBITS
+    Flags:           [  ]
+    Content:        "0400000005000000"
+DWARF:
+  debug_abbrev:
+    - Table:
+      - Tag:      [[ROOT]]
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_producer
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_language
+            Form:      DW_FORM_data2
+          - Attribute: DW_AT_low_pc
+            Form:      DW_FORM_addr
+          - Attribute: DW_AT_high_pc
+            Form:      DW_FORM_data8
+          - Attribute: DW_AT_str_offsets_base
+            Form:      DW_FORM_sec_offset
+      - Tag:      DW_TAG_subprogram
+        Children: DW_CHILDREN_no
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_low_pc
+            Form:      DW_FORM_addr
+          - Attribute: DW_AT_high_pc
+            Form:      DW_FORM_data8
+          - Attribute: DW_AT_type
+            Form:      DW_FORM_ref4
+      - Tag:      DW_TAG_namespace
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+      - Tag:      DW_TAG_structure_type
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_byte_size
+            Form:      DW_FORM_data1
+      - Tag:      DW_TAG_member
+        Children: DW_CHILDREN_no
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_type
+            Form:      DW_FORM_ref4
+          - Attribute: DW_AT_data_member_location
+            Form:      DW_FORM_data1
+      - Tag:      DW_TAG_base_type
+        Children: DW_CHILDREN_no
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+  debug_info:
+    - Version:  5
+      UnitType: [[UNITTYPE]]
+      Entries:
+        - AbbrCode: 1
+          Values:
+            - CStr: by_hand
+            - CStr: U01
+            - Value: 0x04
+            - Value: 0x1130
+            - Value: 0x10
+            - Value: 0x8
+        - AbbrCode: 2
+          Values:
+            - CStr: foo1
+            - Value: 0x1130
+            - Value: 0x10
+            - Value: 0x4d
+        - AbbrCode: 3
+          Values:
+            - CStr: ns
+        - AbbrCode: 4
+          Values:
+            - CStr: S
+            - Value: 0x8
+        - AbbrCode: 5
+          Values:
+            - CStr: m1
+            - Value: 0x5c
+            - Value: 0x0
+        - AbbrCode: 0
+        - AbbrCode: 0
+        - AbbrCode: 6
+          Values:
+            - CStr: int
+        - AbbrCode: 0
+...
diff --git a/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-types-only.test b/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-types-only.test
new file mode 100644
index 0000000000000..312b8d0e1921c
--- /dev/null
+++ b/llvm/test/tools/llvm-dwarfutil/ELF/X86/dwarf5-partial-unit-types-only.test
@@ -0,0 +1,168 @@
+## A unit root is never assigned a type entry, so it is never cloned into the
+## artificial type unit and the recursion into its type children is allowed by
+## a separate test - which named DW_TAG_compile_unit only. A
+## DW_TAG_partial_unit root holding types and no code, the unit dwz produces to
+## carry the definitions it hoisted out of the compile units, therefore reached
+## neither branch and its subtree was dropped after the type name had already
+## been registered. A DW_TAG_compile_unit root is linked alongside as the
+## control.
+##
+## U01:  namespace ns { struct S {}; }
+## U02:  ns::S foo1() { ... }   // DW_FORM_ref_addr reference into U01
+##
+## The classic backend is not linked here. It fails on this input for an
+## unrelated reason, a cross-unit reference to a DIE it never gave an output
+## unit, which is a separate defect from the one this test covers.
+
+# RUN: yaml2obj %s -DROOT=DW_TAG_compile_unit -DUNITTYPE=DW_UT_compile -o %t.cu.o
+# RUN: llvm-dwarfutil --linker parallel %t.cu.o %t.cu.out
+# RUN: llvm-dwarfdump --debug-info %t.cu.out | FileCheck %s --implicit-check-not='DW_AT_name ("U01")'
+
+# RUN: yaml2obj %s -DROOT=DW_TAG_partial_unit -DUNITTYPE=DW_UT_partial -o %t.pu.o
+# RUN: llvm-dwarfutil --linker parallel %t.pu.o %t.pu.out
+# RUN: llvm-dwarfdump --debug-info %t.pu.out | FileCheck %s --check-prefixes=CHECK,CHECK-PU
+
+## The type reaches the artificial type unit under a copy of the namespace that
+## held it, and the second unit's reference resolves into the type unit rather
+## than to a dropped DIE.
+# CHECK:           DW_AT_name ("__artificial_type_unit")
+# CHECK:           DW_TAG_namespace
+# CHECK-NEXT:        DW_AT_name ("ns")
+# CHECK:         0x[[S:[0-9a-f]{8}]]:     DW_TAG_structure_type
+# CHECK-NEXT:        DW_AT_name ("S")
+# CHECK-NEXT:        DW_AT_byte_size (0x08)
+
+## The first unit is emptied either way, since its only child moved to the type
+## unit, but the two roots are then treated differently: a compile unit with
+## nothing left is dropped whole, while a partial unit root is still emitted.
+## That difference is the linker's existing behaviour for a unit with no live
+## contents, not something this fix introduces, so it is pinned rather than
+## made uniform.
+# CHECK-PU:        DW_TAG_partial_unit
+# CHECK-PU-NEXT:     DW_AT_producer ("by_hand")
+# CHECK-PU-NEXT:     DW_AT_name ("U01")
+# CHECK:           DW_TAG_compile_unit
+# CHECK-NEXT:        DW_AT_producer ("by_hand")
+# CHECK-NEXT:        DW_AT_name ("U02")
+# CHECK:           DW_TAG_subprogram
+# CHECK-NEXT:        DW_AT_name ("foo1")
+# CHECK:             DW_AT_type (0x00000000[[S]] "ns::S")
+
+## Two DWARFv5 units. The first defines struct S in namespace ns and has no
+## code of its own; the second has the only function, which returns S through a
+## DW_FORM_ref_addr reference into the first. S is deliberately memberless, so
+## that the first unit holds nothing the linker would keep in plain DWARF - a
+## member would pull in a DW_TAG_base_type, which is always kept, and that
+## alone would give the root plain children to descend for. Each root carries
+## its own DW_AT_str_offsets_base, so the strings the linker rewrites into
+## DW_FORM_strx stay resolvable on output; the linker does not synthesize that
+## attribute for a partial unit root by itself.
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2LSB
+  Type:     ET_REL
+  Machine:  EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Address:         0x1130
+    Size:            0x10
+  - Name:            .debug_str_offsets
+    Type:            SHT_PROGBITS
+    Flags:           [  ]
+    Content:        "0400000005000000"
+DWARF:
+  debug_abbrev:
+    - ID:    0
+      Table:
+      - Tag:      [[ROOT]]
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_producer
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_language
+            Form:      DW_FORM_data2
+          - Attribute: DW_AT_str_offsets_base
+            Form:      DW_FORM_sec_offset
+      - Tag:      DW_TAG_namespace
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+      - Tag:      DW_TAG_structure_type
+        Children: DW_CHILDREN_no
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_byte_size
+            Form:      DW_FORM_data1
+      - Tag:      DW_TAG_compile_unit
+        Children: DW_CHILDREN_yes
+        Attributes:
+          - Attribute: DW_AT_producer
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_language
+            Form:      DW_FORM_data2
+          - Attribute: DW_AT_low_pc
+            Form:      DW_FORM_addr
+          - Attribute: DW_AT_high_pc
+            Form:      DW_FORM_data8
+          - Attribute: DW_AT_str_offsets_base
+            Form:      DW_FORM_sec_offset
+      - Tag:      DW_TAG_subprogram
+        Children: DW_CHILDREN_no
+        Attributes:
+          - Attribute: DW_AT_name
+            Form:      DW_FORM_string
+          - Attribute: DW_AT_low_pc
+            Form:      DW_FORM_addr
+          - Attribute: DW_AT_high_pc
+            Form:      DW_FORM_data8
+          - Attribute: DW_AT_type
+            Form:      DW_FORM_ref_addr
+  debug_info:
+    - Version:  5
+      UnitType: [[UNITTYPE]]
+      AbbrevTableID: 0
+      Entries:
+        - AbbrCode: 1
+          Values:
+            - CStr: by_hand
+            - CStr: U01
+            - Value: 0x04
+            - Value: 0x8
+        - AbbrCode: 2
+          Values:
+            - CStr: ns
+        - AbbrCode: 3
+          Values:
+            - CStr: S
+            - Value: 0x8
+        - AbbrCode: 0
+        - AbbrCode: 0
+    - Version:  5
+      UnitType: DW_UT_compile
+      AbbrevTableID: 0
+      Entries:
+        - AbbrCode: 4
+          Values:
+            - CStr: by_hand
+            - CStr: U02
+            - Value: 0x04
+            - Value: 0x1130
+            - Value: 0x10
+            - Value: 0x8
+        - AbbrCode: 5
+          Values:
+            - CStr: foo1
+            - Value: 0x1130
+            - Value: 0x10
+            - Value: 0x23
+        - AbbrCode: 0
+...

>From 23f4c439ca95d9f20051426ec15be2a72bbac68b Mon Sep 17 00:00:00 2001
From: Arcadiy Ivanov <arcadiy at ivanov.biz>
Date: Sat, 29 Aug 2026 21:55:38 -0400
Subject: [PATCH 2/2] [DWARFLinker][Parallel] Stop the qualified name walk at
 the unit root

The Apple accelerator tables carry a DW_ATOM_qual_name_hash for every
type, a hash of the type's fully qualified name that a consumer looks
types up by. hashFullyQualifiedName builds it by walking up the parents
and stopping at the unit root, which it recognized by testing the
parent's tag against DW_TAG_compile_unit alone. A DW_TAG_partial_unit
root is a valid parent carrying a different tag, so the walk did not
stop there. It recursed into the root and folded the root's DW_AT_name,
a source file path, into the hash as though it were an enclosing scope.

Test the parent's position instead. CompileUnit::isUnitRootDIE, the
helper #219615 adds for the two cloneDIE() gates, is true for index
zero, which is the unit root whatever tag it carries, and it is the
predicate the classic backend already applies here as ParentIdx == 0.
The two backends produced different hashes for the same type on the
same input before this, which is the one thing an accelerator table
hash cannot afford; they now agree, and neither varies with the root
tag.

DWARFv5 section 3.1.1 places the containing scope of a partial unit's
declarations at the entries that import it rather than at the unit, and
item 2 of that section makes the root's DW_AT_name a source file path
rather than an identifier, so the root contributes no component to a
qualified name. Measured on the new test's struct Foo at unit scope,
the hash was 0xf9a84c7e under a partial unit root against 0x0c3993dd
under a compile unit root; it is 0x0c3993dd for both now, and for both
backends.

Only dsymutil reaches this code. llvm-dwarfutil accepts
--build-accelerator=none and --build-accelerator=DWARF and nothing
else, and the hash is written solely into the Apple tables, so it is
computed and discarded there.

The test disables output verification on its two partial unit runs.
Both backends leave a partial unit root without a
DW_AT_str_offsets_base and record DW_UT_compile in the unit header
whatever tag the root carries, so dsymutil's default output
verification rejects either link on grounds unrelated to the hash;
that is #219365. The two compile unit control runs keep verification
on. Reaching the hash at all needs #219615, the preceding change in
this series and the one this change is stacked on, without which a
type under a partial unit root faults before any accelerator entry is
written.

Fixes #219636

Assisted-by: Claude Code
---
 .../Parallel/AcceleratorRecordsSaver.cpp      |   3 +-
 ...e-types-qualified-name-hash-partial-unit.s | 179 ++++++++++++++++++
 2 files changed, 181 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/dsymutil/X86/apple-types-qualified-name-hash-partial-unit.s

diff --git a/llvm/lib/DWARFLinker/Parallel/AcceleratorRecordsSaver.cpp b/llvm/lib/DWARFLinker/Parallel/AcceleratorRecordsSaver.cpp
index ab73a3fce8b4a..4f94061311ac5 100644
--- a/llvm/lib/DWARFLinker/Parallel/AcceleratorRecordsSaver.cpp
+++ b/llvm/lib/DWARFLinker/Parallel/AcceleratorRecordsSaver.cpp
@@ -51,7 +51,8 @@ static uint32_t hashFullyQualifiedName(CompileUnit &InputCU, DWARFDie &InputDIE,
     Name = "(anonymous namespace)";
 
   DWARFDie ParentDie = InputDIE.getParent();
-  if (!ParentDie.isValid() || ParentDie.getTag() == dwarf::DW_TAG_compile_unit)
+  if (!ParentDie.isValid() ||
+      CompileUnit::isUnitRootDIE(CU->getDIEIndex(ParentDie)))
     return djbHash(Name ? Name : "", djbHash(ChildRecurseDepth ? "" : "::"));
 
   return djbHash(
diff --git a/llvm/test/tools/dsymutil/X86/apple-types-qualified-name-hash-partial-unit.s b/llvm/test/tools/dsymutil/X86/apple-types-qualified-name-hash-partial-unit.s
new file mode 100644
index 0000000000000..347edb6634531
--- /dev/null
+++ b/llvm/test/tools/dsymutil/X86/apple-types-qualified-name-hash-partial-unit.s
@@ -0,0 +1,179 @@
+## The Apple accelerator tables carry a DW_ATOM_qual_name_hash for every type,
+## a hash of the type's fully qualified name built by walking up the parents and
+## stopping at the unit root. A partial unit is a unit root that contributes no
+## scope of its own: DWARFv5 section 3.1.1 places the containing scope of a
+## partial unit's declarations at the entries that import it, not at the unit,
+## and section 3.1.1 item 2 makes the root's own DW_AT_name a source file path
+## rather than an identifier. The walk therefore has to end on a partial unit
+## root exactly as it does on a compile unit root, and the hash has to come out
+## the same either way.
+
+# RUN: llvm-mc -triple x86_64-apple-darwin -filetype=obj --defsym ROOT=17 --defsym UNITTYPE=1 %s -o %t.cu.o
+# RUN: llvm-mc -triple x86_64-apple-darwin -filetype=obj --defsym ROOT=60 --defsym UNITTYPE=3 %s -o %t.pu.o
+
+# RUN: echo '---' > %t.cu.map
+# RUN: echo "triple:          'x86_64-apple-darwin'" >> %t.cu.map
+# RUN: echo 'objects:'  >> %t.cu.map
+# RUN: echo " -  filename: '%t.cu.o'" >> %t.cu.map
+# RUN: echo '    symbols:' >> %t.cu.map
+# RUN: echo '      - { sym: _foo, objAddr: 0x0, binAddr: 0x10000, size: 0x1 }' >> %t.cu.map
+# RUN: echo '...' >> %t.cu.map
+
+# RUN: echo '---' > %t.pu.map
+# RUN: echo "triple:          'x86_64-apple-darwin'" >> %t.pu.map
+# RUN: echo 'objects:'  >> %t.pu.map
+# RUN: echo " -  filename: '%t.pu.o'" >> %t.pu.map
+# RUN: echo '    symbols:' >> %t.pu.map
+# RUN: echo '      - { sym: _foo, objAddr: 0x0, binAddr: 0x10000, size: 0x1 }' >> %t.pu.map
+# RUN: echo '...' >> %t.pu.map
+
+# RUN: dsymutil --linker=parallel -accelerator=Apple -y %t.cu.map -f -o %t.cu.parallel.dSYM
+# RUN: llvm-dwarfdump --apple-types %t.cu.parallel.dSYM | FileCheck %s
+
+# RUN: dsymutil --linker=parallel -accelerator=Apple --verify-dwarf=none -y %t.pu.map -f -o %t.pu.parallel.dSYM
+# RUN: llvm-dwarfdump --apple-types %t.pu.parallel.dSYM | FileCheck %s
+
+# RUN: dsymutil --linker=classic -accelerator=Apple -y %t.cu.map -f -o %t.cu.classic.dSYM
+# RUN: llvm-dwarfdump --apple-types %t.cu.classic.dSYM | FileCheck %s
+
+# RUN: dsymutil --linker=classic -accelerator=Apple --verify-dwarf=none -y %t.pu.map -f -o %t.pu.classic.dSYM
+# RUN: llvm-dwarfdump --apple-types %t.pu.classic.dSYM | FileCheck %s
+
+## The two partial unit runs disable output verification, which is on by default
+## and unrelated to the hash. Both backends leave a partial unit root without a
+## DW_AT_str_offsets_base and record DW_UT_compile in the unit header whatever
+## the root tag is, so --verify rejects either output on grounds this test is
+## not about. That is issue #219365. The compile unit control runs keep
+## verification on, so the input and the tool are still held to it here.
+
+## One CHECK block serves all four runs, which is the claim being made: the two
+## backends agree with each other, and neither varies with the root tag. Atom[0]
+## is the output DIE offset and is deliberately left unmatched, since the two
+## backends lay out the linked unit differently.
+
+## Foo sits at unit scope, so its qualified name is "Foo" alone and the root
+## contributes nothing to it. Bar sits inside a namespace, so "ns" has to be
+## folded in. The pair is what separates ending the walk at the unit root from
+## ending it unconditionally: a fix that simply stopped recursing would get Foo
+## right and Bar wrong.
+
+## Bucket assignment is the djb hash of the short name, so the two names land in
+## this order in every run.
+
+# CHECK:      String: {{.*}} "Bar"
+# CHECK-NEXT: Data 0 [
+# CHECK-NEXT:   Atom[0]:
+# CHECK-NEXT:   Atom[1]: 0x0013 (DW_TAG_structure_type)
+# CHECK-NEXT:   Atom[2]: 0x00
+# CHECK-NEXT:   Atom[3]: 0x27c1798f
+
+# CHECK:      String: {{.*}} "Foo"
+# CHECK-NEXT: Data 0 [
+# CHECK-NEXT:   Atom[0]:
+# CHECK-NEXT:   Atom[1]: 0x0013 (DW_TAG_structure_type)
+# CHECK-NEXT:   Atom[2]: 0x00
+# CHECK-NEXT:   Atom[3]: 0x0c3993dd
+
+	.section	__TEXT,__text,regular,pure_instructions
+	.globl	_foo
+_foo:
+Lfunc_begin0:
+	retq
+Lfunc_end0:
+
+	.section	__DWARF,__debug_abbrev,regular,debug
+Lsection_abbrev:
+	.byte	1                       ## Abbreviation Code
+	.byte	ROOT                    ## DW_TAG_compile_unit / DW_TAG_partial_unit
+	.byte	1                       ## DW_CHILDREN_yes
+	.byte	37                      ## DW_AT_producer
+	.byte	8                       ## DW_FORM_string
+	.byte	19                      ## DW_AT_language
+	.byte	5                       ## DW_FORM_data2
+	.byte	3                       ## DW_AT_name
+	.byte	8                       ## DW_FORM_string
+	.byte	0, 0
+
+	.byte	2                       ## Abbreviation Code
+	.byte	46                      ## DW_TAG_subprogram
+	.byte	1                       ## DW_CHILDREN_yes
+	.byte	3                       ## DW_AT_name
+	.byte	8                       ## DW_FORM_string
+	.byte	73                      ## DW_AT_type
+	.byte	19                      ## DW_FORM_ref4
+	.byte	17                      ## DW_AT_low_pc
+	.byte	1                       ## DW_FORM_addr
+	.byte	18                      ## DW_AT_high_pc
+	.byte	6                       ## DW_FORM_data4
+	.byte	0, 0
+
+	.byte	3                       ## Abbreviation Code
+	.byte	5                       ## DW_TAG_formal_parameter
+	.byte	0                       ## DW_CHILDREN_no
+	.byte	3                       ## DW_AT_name
+	.byte	8                       ## DW_FORM_string
+	.byte	73                      ## DW_AT_type
+	.byte	19                      ## DW_FORM_ref4
+	.byte	0, 0
+
+	.byte	4                       ## Abbreviation Code
+	.byte	19                      ## DW_TAG_structure_type
+	.byte	0                       ## DW_CHILDREN_no
+	.byte	3                       ## DW_AT_name
+	.byte	8                       ## DW_FORM_string
+	.byte	11                      ## DW_AT_byte_size
+	.byte	11                      ## DW_FORM_data1
+	.byte	0, 0
+
+	.byte	5                       ## Abbreviation Code
+	.byte	57                      ## DW_TAG_namespace
+	.byte	1                       ## DW_CHILDREN_yes
+	.byte	3                       ## DW_AT_name
+	.byte	8                       ## DW_FORM_string
+	.byte	0, 0
+
+	.byte	0                       ## EOM(3)
+
+	.section	__DWARF,__debug_info,regular,debug
+Lsection_info:
+	.long	Lcu_end - Lcu_start     ## Length of Unit
+Lcu_start:
+	.short	5                       ## DWARF version number
+	.byte	UNITTYPE                ## DW_UT_compile / DW_UT_partial
+	.byte	8                       ## Address Size (in bytes)
+	.long	0                       ## Offset Into Abbrev. Section
+
+	.byte	1                       ## Abbrev [1] root
+	.asciz	"hand-written"          ## DW_AT_producer
+	.short	0x0004                  ## DW_AT_language (DW_LANG_C_plus_plus)
+	.asciz	"dwz-common.h"          ## DW_AT_name
+
+	.byte	2                       ## Abbrev [2] DW_TAG_subprogram
+	.asciz	"foo"                   ## DW_AT_name
+	.long	Lfootype - Lsection_info ## DW_AT_type
+	.quad	Lfunc_begin0            ## DW_AT_low_pc
+	.long	Lfunc_end0 - Lfunc_begin0 ## DW_AT_high_pc
+
+	.byte	3                       ## Abbrev [3] DW_TAG_formal_parameter
+	.asciz	"b"                     ## DW_AT_name
+	.long	Lbartype - Lsection_info ## DW_AT_type
+
+	.byte	0                       ## End Of Children Mark (subprogram)
+
+Lfootype:
+	.byte	4                       ## Abbrev [4] DW_TAG_structure_type
+	.asciz	"Foo"                   ## DW_AT_name
+	.byte	8                       ## DW_AT_byte_size
+
+	.byte	5                       ## Abbrev [5] DW_TAG_namespace
+	.asciz	"ns"                    ## DW_AT_name
+
+Lbartype:
+	.byte	4                       ## Abbrev [4] DW_TAG_structure_type
+	.asciz	"Bar"                   ## DW_AT_name
+	.byte	4                       ## DW_AT_byte_size
+
+	.byte	0                       ## End Of Children Mark (namespace)
+
+	.byte	0                       ## End Of Children Mark (root)
+Lcu_end:



More information about the llvm-commits mailing list