[llvm] [DWARFLinker] Make ODR uniquing deterministic (PR #194777)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 29 06:02:13 PDT 2026


================
@@ -1419,46 +1419,42 @@ DIE *CompileUnit::allocateTypeDie(TypeEntryBody *TypeDescriptor,
                                   DIEGenerator &TypeDIEGenerator,
                                   dwarf::Tag DieTag, bool IsDeclaration,
                                   bool IsParentDeclaration) {
-  DIE *DefinitionDie = TypeDescriptor->Die;
-  // Do not allocate any new DIE if definition DIE is already met.
-  if (DefinitionDie)
-    return nullptr;
-
-  DIE *DeclarationDie = TypeDescriptor->DeclarationDie;
-  bool OldParentIsDeclaration = TypeDescriptor->ParentIsDeclaration;
-
-  if (IsDeclaration && !DeclarationDie) {
-    // Alocate declaration DIE.
-    DIE *NewDie = TypeDIEGenerator.createDIE(DieTag, 0);
-    if (TypeDescriptor->DeclarationDie.compare_exchange_strong(DeclarationDie,
-                                                               NewDie))
-      return NewDie;
-  } else if (IsDeclaration && !IsParentDeclaration && OldParentIsDeclaration) {
-    // Overwrite existing declaration DIE if it's parent is also an declaration
-    // while parent of current declaration DIE is a definition.
-    if (TypeDescriptor->ParentIsDeclaration.compare_exchange_strong(
-            OldParentIsDeclaration, false)) {
-      DIE *NewDie = TypeDIEGenerator.createDIE(DieTag, 0);
-      TypeDescriptor->DeclarationDie = NewDie;
-      return NewDie;
-    }
-  } else if (!IsDeclaration && IsParentDeclaration && !DeclarationDie) {
-    // Alocate declaration DIE since parent of current DIE is marked as
-    // declaration.
-    DIE *NewDie = TypeDIEGenerator.createDIE(DieTag, 0);
-    if (TypeDescriptor->DeclarationDie.compare_exchange_strong(DeclarationDie,
-                                                               NewDie))
-      return NewDie;
-  } else if (!IsDeclaration && !IsParentDeclaration) {
-    // Allocate definition DIE.
-    DIE *NewDie = TypeDIEGenerator.createDIE(DieTag, 0);
-    if (TypeDescriptor->Die.compare_exchange_strong(DefinitionDie, NewDie)) {
+  // Use a per-type spinlock and CU priority to ensure the earliest CU in link
+  // order always wins the type slot, producing deterministic output regardless
+  // of thread scheduling.
+  unsigned Priority = getDeterministicPriority();
+
+  // Speculatively allocate the DIE outside the lock so the critical section
+  // only covers the priority check and pointer store. If we lose, the DIE
+  // is leaked into the bump allocator.
+  DIE *NewDie = TypeDIEGenerator.createDIE(DieTag, 0);
+
+  while (TypeDescriptor->Lock.test_and_set(std::memory_order_acquire))
+    ; // spin
+
+  DIE *Result = nullptr;
+
+  if (!IsDeclaration && !IsParentDeclaration) {
+    // Definition: lowest priority wins.
+    if (Priority < TypeDescriptor->DiePriority) {
+      TypeDescriptor->DiePriority = Priority;
+      TypeDescriptor->Die = NewDie;
       TypeDescriptor->ParentIsDeclaration = false;
-      return NewDie;
+      Result = NewDie;
----------------
alx32 wrote:

This seems to let multiple CUs successfully clone into the same `TypeEntryBody` over the lifetime of a link, leaving non-canonical DIEs (and the patches they generated) reachable from `ListDebugTypeStrPatch` / `ListDebugTypeLineStrPatch`. `applyPatches` filters those via `&TypeEntry->getFinalDie() != Patch.Die`, but `DWARFLinkerImpl::forEachOutputString` only filters [`Patch.Die == nullptr`](https://github.com/JDevlieghere/llvm-project/blob/15aa622c34376c836168c0fff989c198c375395e/llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp#L1038), so strings reachable only from orphan clones would still get added to `DebugStrStrings` and assigned offsets.
Doesn't this rely on the implicit assumption that all CUs cloning the same type produce an identical string set? Should we tighten that filter to use the same `getFinalDie()` check, or document and enforce the invariant?

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


More information about the llvm-commits mailing list