Index: include/llvm/IR/Metadata.h =================================================================== --- include/llvm/IR/Metadata.h +++ include/llvm/IR/Metadata.h @@ -1303,7 +1303,13 @@ if (!Use) return; *Use = MD; - Use = nullptr; + + if (*Use) + MetadataTracking::track(*Use); + + Metadata *P = this; + MetadataTracking::untrack(P); + assert(!Use && "Use is still being tracked despite being untracked!"); } }; Index: test/ThinLTO/X86/lazy-load-temporaries-cleanup.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/lazy-load-temporaries-cleanup.ll @@ -0,0 +1,16 @@ +; This test ensures that metadata temporaries are properly tracked, then cleaned up + +; RUN: opt -module-summary -bitcode-mdindex-threshold=0 %s | llvm-dis -materialize-metadata -set-importing -o - | FileCheck %s +; CHECK: !named = !{!0} + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @patatino() { + ret void +} + +!named = !{!0} + +!0 = distinct !{!1, i8 0} +!1 = !{i8 1} Index: tools/llvm-dis/llvm-dis.cpp =================================================================== --- tools/llvm-dis/llvm-dis.cpp +++ tools/llvm-dis/llvm-dis.cpp @@ -51,8 +51,13 @@ DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); static cl::opt -ShowAnnotations("show-annotations", - cl::desc("Add informational comments to the .ll file")); + SetImporting("set-importing", + cl::desc("Set lazy loading to pretend to import a module"), + cl::Hidden); + +static cl::opt + ShowAnnotations("show-annotations", + cl::desc("Add informational comments to the .ll file")); static cl::opt PreserveAssemblyUseListOrder( "preserve-ll-uselistorder", @@ -142,9 +147,9 @@ static std::unique_ptr openInputFile(LLVMContext &Context) { std::unique_ptr MB = ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); - std::unique_ptr M = - ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context, - /*ShouldLazyLoadMetadata=*/true)); + std::unique_ptr M = ExitOnErr(getOwningLazyBitcodeModule( + std::move(MB), Context, + /*ShouldLazyLoadMetadata=*/true, SetImporting)); if (MaterializeMetadata) ExitOnErr(M->materializeMetadata()); else