Source code
Revision control
Copy as Markdown
Other Tools
From 72e032ac6e74250dae781eeed4a7e3298edcaef6 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson@google.com>
Date: Wed, 4 Nov 2026 08:57:49 -0800
Subject: [PATCH] [Bitcode] Assign GUIDs when reading bitcode that predates
!guid metadata (#238614)
Enable backwards compatibility with legacy bitcode as required for LLVM.
Bitcode produced before #184065 has no !guid metadata, so loading it
into LTO leaves definitions without a GUID. Regular LTO then asserts in
GlobalValue::getGUID() once an internal symbol is promoted and renamed
(e.g. WPD on vf.llvm.merged), and the ThinLTO backend silently skips
dropping dead symbols.
Fix this as a bitcode upgrade so that regular LTO, in-process and
distributed ThinLTO, and libLTO are all covered without touching LTO or
IRMover. When no MODULE_CODE_GUIDLIST record is seen, compute each
definition's GUID from its original name and linkage at load time and
record it in the module's value-to-GUID map. The !guid metadata is
attached when it becomes legal: for globals when module metadata is
materialized, and for functions when their body is. This matches what
AssignGUIDPass would have produced when the bitcode was written.
Current writers always emit MODULE_CODE_GUIDLIST, even when empty, so
llvm-as | llvm-dis round trips of IR without !guid are unchanged. Only
tests that disassemble checked-in legacy .bc files see new !guid
attachments; their CHECK lines are updated.
Assisted-by: Gemini
---
llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 70 ++++++++++++++++++
llvm/test/Bitcode/branch-weight.ll | 8 +-
llvm/test/Bitcode/compatibility-3.6.ll | 32 ++++----
llvm/test/Bitcode/compatibility-3.7.ll | 40 +++++-----
llvm/test/Bitcode/compatibility-3.8.ll | 40 +++++-----
llvm/test/Bitcode/compatibility-3.9.ll | 40 +++++-----
llvm/test/Bitcode/compatibility-4.0.ll | 40 +++++-----
llvm/test/Bitcode/compatibility-5.0.ll | 40 +++++-----
llvm/test/Bitcode/compatibility-6.0.ll | 40 +++++-----
.../Bitcode/constexpr-scalable-vector-gep.ll | 2 +-
.../Bitcode/constexpr-to-instr-metadata-2.ll | 8 +-
.../Bitcode/constexpr-to-instr-metadata.ll | 4 +-
llvm/test/Bitcode/constexpr-to-instr.ll | 36 ++++-----
llvm/test/Bitcode/dead-on-return-upgrade.ll | 2 +-
llvm/test/Bitcode/dilocalvariable-3.9.ll | 4 +-
llvm/test/Bitcode/dityperefs-3.8.ll | 34 ++++-----
.../Bitcode/function-local-metadata.3.5.ll | 6 +-
llvm/test/Bitcode/highLevelStructure.3.2.ll | 8 +-
.../test/Bitcode/intrinsics-struct-upgrade.ll | 2 +-
llvm/test/Bitcode/legacy-guid-upgrade.ll | 56 ++++++++++++++
llvm/test/Bitcode/legacy-guid-upgrade.ll.bc | Bin 0 -> 2176 bytes
llvm/test/Bitcode/linkage-types-3.2.ll | 50 ++++++-------
llvm/test/Bitcode/memInstructions.3.2.ll | 32 ++++----
llvm/test/Bitcode/metadata.3.5.ll | 10 +--
.../upgrade-arc-attachedcall-bundle.ll | 4 +-
llvm/test/Bitcode/upgrade-cu-locals.ll | 34 ++++-----
llvm/test/Bitcode/weak-macho-3.5.ll | 2 +-
llvm/test/Bitcode/x86_intr-upgrade.test | 2 +-
.../LTO/X86/Inputs/legacy-guid-upgrade-wpd.bc | Bin 0 -> 2520 bytes
llvm/test/LTO/X86/legacy-guid-upgrade.ll | 69 +++++++++++++++++
llvm/test/LTO/X86/mix-opaque-typed.ll | 2 +-
.../X86/Inputs/legacy-guid-upgrade-foo.bc | Bin 0 -> 2060 bytes
.../X86/Inputs/legacy-guid-upgrade-main.bc | Bin 0 -> 1972 bytes
llvm/test/ThinLTO/X86/legacy-guid-upgrade.ll | 70 ++++++++++++++++++
34 files changed, 526 insertions(+), 261 deletions(-)
create mode 100644 llvm/test/Bitcode/legacy-guid-upgrade.ll
create mode 100644 llvm/test/Bitcode/legacy-guid-upgrade.ll.bc
create mode 100644 llvm/test/LTO/X86/Inputs/legacy-guid-upgrade-wpd.bc
create mode 100644 llvm/test/LTO/X86/legacy-guid-upgrade.ll
create mode 100644 llvm/test/ThinLTO/X86/Inputs/legacy-guid-upgrade-foo.bc
create mode 100644 llvm/test/ThinLTO/X86/Inputs/legacy-guid-upgrade-main.bc
create mode 100644 llvm/test/ThinLTO/X86/legacy-guid-upgrade.ll
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index c57323ed4152..550b2118d6e5 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -700,6 +700,15 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
/// A list of GUIDs defined by this module. Indexed by ValueID.
std::vector<GlobalValue::GUID> GUIDList;
+ /// True if a MODULE_CODE_GUIDLIST record was seen. Bitcode produced before
+ /// GUIDs were assigned and recorded in metadata has no such record (it is
+ /// emitted unconditionally, even when empty, by newer writers). For such
+ /// legacy bitcode we assign GUIDs during upgrade (see upgradeGUIDs()).
+ bool SeenGUIDList = false;
+
+ /// True once legacy GUIDs have been computed for this module.
+ bool UpgradedGUIDs = false;
+
/// Mirrors ParserCallbacks::SkipDebugIntrinsicUpgrade. When set, debug
/// intrinsic calls (llvm.dbg.*) are not auto-upgraded to non-instruction
/// debug records by globalCleanup(); the caller is expected to perform the
@@ -946,6 +955,8 @@ private:
Error typeCheckLoadStoreInst(Type *ValType, Type *PtrType);
Error parseFunctionBody(Function *F);
Error globalCleanup();
+ void upgradeGUIDs();
+ void attachUpgradedGUIDMetadata(GlobalObject &GO);
Error resolveGlobalAndIndirectSymbolInits();
Error parseUseLists();
Error findFunctionInStream(
@@ -4031,6 +4042,13 @@ Error BitcodeReader::materializeMetadata() {
UpgradeCFIFunctionsMetadata(*TheModule);
+ // Attach !guid metadata to global variable definitions of legacy bitcode.
+ // Functions are handled when their bodies are materialized, as unmaterialized
+ // functions may not carry metadata.
+ if (UpgradedGUIDs)
+ for (GlobalVariable &GV : TheModule->globals())
+ attachUpgradedGUIDMetadata(GV);
+
DeferredMetadataInfo.clear();
return Error::success();
}
@@ -4099,6 +4117,15 @@ Error BitcodeReader::globalCleanup() {
TheModule->insertGUID(Value, GUID);
}
+ // Legacy bitcode predating GUID assignment has no MODULE_CODE_GUIDLIST
+ // record. Compute GUIDs from the original names and linkages now, before
+ // any client (e.g. LTO) can mutate them. Names are only known once the
+ // string table or the value symbol table has been read, and globalCleanup()
+ // can be invoked before the VST is parsed for older bitcode, so wait until
+ // then.
+ if (!SeenGUIDList && !UpgradedGUIDs && (UseStrtab || SeenValueSymbolTable))
+ upgradeGUIDs();
+
// Force deallocation of memory for these vectors to favor the client that
// want lazy deserialization.
std::vector<std::pair<GlobalVariable *, unsigned>>().swap(GlobalInits);
@@ -4106,6 +4133,43 @@ Error BitcodeReader::globalCleanup() {
return Error::success();
}
+/// Record the GUIDs of all definitions in a module that predates GUID
+/// assignment, mirroring what AssignGUIDPass would have computed at the time
+/// the module was produced. The GUIDs are stored in the module's value-to-GUID
+/// map, which serves unmaterialized (lazily loaded) values; the corresponding
+/// !guid metadata is attached once metadata and function bodies are
+/// materialized (see attachUpgradedGUIDMetadata()).
+void BitcodeReader::upgradeGUIDs() {
+ assert(!SeenGUIDList && !UpgradedGUIDs);
+ UpgradedGUIDs = true;
+ for (GlobalVariable &GV : TheModule->globals()) {
+ if (GV.isDeclaration())
+ continue;
+ TheModule->insertGUID(&GV, GV.getGUIDOrFallback());
+ }
+ for (Function &F : TheModule->functions()) {
+ if (F.isDeclaration())
+ continue;
+ TheModule->insertGUID(&F, F.getGUIDOrFallback());
+ }
+}
+
+/// Attach !guid metadata to \p GO from the GUID recorded by upgradeGUIDs(), so
+/// that the GUID survives operations that only transfer metadata (e.g. module
+/// linking and cloning). Must only be called once \p GO can legally carry
+/// metadata, i.e. after module metadata is loaded and, for functions, after
+/// the body is materialized.
+void BitcodeReader::attachUpgradedGUIDMetadata(GlobalObject &GO) {
+ assert(UpgradedGUIDs);
+ if (GO.isDeclaration() || GO.getMetadata(LLVMContext::MD_unique_id))
+ return;
+ if (auto GUID = TheModule->getGUID(&GO))
+ GO.setMetadata(
+ LLVMContext::MD_unique_id,
+ MDNode::get(Context, {ConstantAsMetadata::get(ConstantInt::get(
+ Type::getInt64Ty(Context), *GUID))}));
+}
+
/// Support for lazy parsing of function bodies. This is required if we
/// either have an old bitcode file without a VST forward declaration record,
/// or if we have an anonymous function being materialized, since anonymous
@@ -4920,6 +4984,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit,
// MODULE_CODE_GUIDLIST: [i64 x N]
case bitc::MODULE_CODE_GUIDLIST:
assert(Record.size() % 2 == 0);
+ SeenGUIDList = true;
GUIDList.reserve(GUIDList.size() + Record.size() / 2);
for (size_t i = 0; i < Record.size(); i += 2)
GUIDList.push_back(Record[i] << 32 | Record[i + 1]);
@@ -7180,6 +7245,11 @@ Error BitcodeReader::materialize(GlobalValue *GV) {
if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
F->setSubprogram(SP);
+ // Finish the GUID upgrade for legacy bitcode now that the function can
+ // carry metadata.
+ if (UpgradedGUIDs)
+ attachUpgradedGUIDMetadata(*F);
+
// Check if the TBAA Metadata are valid, otherwise we will need to strip them.
if (!MDLoader->isStrippingTBAA()) {
for (auto &I : instructions(F)) {