Skip to content

release/23.x: [LTO] Sort DefinedGlobals in LTO cache key (#210025)#210274

Merged
dyung merged 1 commit into
llvm:release/23.xfrom
llvmbot:issue210025
Jul 17, 2026
Merged

release/23.x: [LTO] Sort DefinedGlobals in LTO cache key (#210025)#210274
dyung merged 1 commit into
llvm:release/23.xfrom
llvmbot:issue210025

Conversation

@llvmbot

@llvmbot llvmbot commented Jul 17, 2026

Copy link
Copy Markdown
Member

Backport 90e31c4

Requested by: @nikic

@llvmbot

llvmbot commented Jul 17, 2026

Copy link
Copy Markdown
Member Author

@nocchijiang @teresajohnson What do you think about merging this PR to the release branch?

@llvmorg-github-actions llvmorg-github-actions Bot added the LTO Link time optimization (regular/full LTO or ThinLTO) label Jul 17, 2026
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-lto

Author: llvmbot

Changes

Backport 90e31c4

Requested by: @nikic


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

3 Files Affected:

  • (modified) llvm/lib/LTO/LTO.cpp (+8-3)
  • (added) llvm/test/ThinLTO/X86/Inputs/cache-defined-globals-order.ll (+6)
  • (added) llvm/test/ThinLTO/X86/cache-defined-globals-order.ll (+27)
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index b0fd1d77a2d66..c8c5b0880819a 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -304,9 +304,14 @@ std::string llvm::computeLTOCacheKey(
     }
   };
 
-  // Include the hash for the linkage type to reflect internalization and weak
-  // resolution, and collect any used type identifier resolutions.
-  for (auto &GS : DefinedGlobals) {
+  // Sort the defined globals by GUID to be independent of the insertion order,
+  // which may depend on the order that modules are added.
+  SmallVector<std::pair<GlobalValue::GUID, GlobalValueSummary *>>
+      SortedDefinedGlobals(DefinedGlobals.begin(), DefinedGlobals.end());
+  llvm::sort(SortedDefinedGlobals, llvm::less_first());
+  for (auto &GS : SortedDefinedGlobals) {
+    // Include the hash for the linkage type to reflect internalization and weak
+    // resolution, and collect any used type identifier resolutions.
     GlobalValue::LinkageTypes Linkage = GS.second->linkage();
     Hasher.update(
         ArrayRef<uint8_t>((const uint8_t *)&Linkage, sizeof(Linkage)));
diff --git a/llvm/test/ThinLTO/X86/Inputs/cache-defined-globals-order.ll b/llvm/test/ThinLTO/X86/Inputs/cache-defined-globals-order.ll
new file mode 100644
index 0000000000000..5a527c9bae924
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/Inputs/cache-defined-globals-order.ll
@@ -0,0 +1,6 @@
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define linkonce_odr void @shared29() {
+  ret void
+}
diff --git a/llvm/test/ThinLTO/X86/cache-defined-globals-order.ll b/llvm/test/ThinLTO/X86/cache-defined-globals-order.ll
new file mode 100644
index 0000000000000..84f71f20f9e6d
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/cache-defined-globals-order.ll
@@ -0,0 +1,27 @@
+; RUN: rm -rf %t && mkdir -p %t
+; RUN: opt -module-hash -module-summary %s -o %t/t.bc
+; RUN: opt -module-hash -module-summary %S/Inputs/cache-defined-globals-order.ll -o %t/a.bc
+
+; Tests that the LTO cache key is insensitive to the order of the modules.
+; The linkonce_odr function @shared29 is defined in both t.bc and a.bc, so
+; it gets a different position in the combined index depending on which
+; module is processed first, which results in a different insertion order
+; for DefinedGlobals.
+
+; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/t.bc %t/a.bc -r=%t/t.bc,main,plx -r=%t/t.bc,shared29,plx -r=%t/a.bc,shared29,lx
+; RUN: ls %t/cache | count 2
+
+; RUN: llvm-lto2 run -cache-dir %t/cache -o %t.o %t/a.bc %t/t.bc -r=%t/t.bc,main,plx -r=%t/t.bc,shared29,plx -r=%t/a.bc,shared29,lx
+; RUN: ls %t/cache | count 2
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @main() {
+  call void @shared29()
+  ret void
+}
+
+define linkonce_odr void @shared29() {
+  ret void
+}

@dyung dyung moved this from Needs Triage to Needs Review in LLVM Release Status Jul 17, 2026

@teresajohnson teresajohnson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@github-project-automation github-project-automation Bot moved this from Needs Review to Needs Merge in LLVM Release Status Jul 17, 2026
Sort the DefindeGlobals by GUID when computing the LTO cache key, to
avoid making the cache key dependent on the insertion order. This fixes
large compile-time regressions in rust incremental builds.

Alternatively one could make collectDefinedGVSummariesPerModule() work
on the sortedRange(), but as that is also used in other places, it
probably makes sense to sort locally.

The issue was introduced in 760bb06.

(cherry picked from commit 90e31c4)
@dyung
dyung merged commit 44addbd into llvm:release/23.x Jul 17, 2026
2 of 4 checks passed
@github-project-automation github-project-automation Bot moved this from Needs Merge to Done in LLVM Release Status Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LTO Link time optimization (regular/full LTO or ThinLTO)

Projects

Development

Successfully merging this pull request may close these issues.

4 participants