Index: include/llvm/IR/Metadata.h =================================================================== --- include/llvm/IR/Metadata.h +++ include/llvm/IR/Metadata.h @@ -19,18 +19,18 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" -#include "llvm/ADT/ilist_node.h" -#include "llvm/ADT/iterator_range.h" #include "llvm/ADT/None.h" #include "llvm/ADT/PointerUnion.h" -#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/ilist_node.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/IR/Constant.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Value.h" -#include "llvm/Support/Casting.h" #include "llvm/Support/CBindingWrapping.h" +#include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" #include #include @@ -139,7 +139,7 @@ // Specialized opaque metadata conversions. inline Metadata **unwrap(LLVMMetadataRef *MDs) { - return reinterpret_cast(MDs); + return reinterpret_cast(MDs); } #define HANDLE_METADATA(CLASS) class CLASS; @@ -418,8 +418,7 @@ class LocalAsMetadata : public ValueAsMetadata { friend class ValueAsMetadata; - LocalAsMetadata(Value *Local) - : ValueAsMetadata(LocalAsMetadataKind, Local) { + LocalAsMetadata(Value *Local) : ValueAsMetadata(LocalAsMetadataKind, Local) { assert(!isa(Local) && "Expected local value"); } @@ -656,16 +655,14 @@ }; // Specialize DenseMapInfo for AAMDNodes. -template<> -struct DenseMapInfo { +template <> struct DenseMapInfo { static inline AAMDNodes getEmptyKey() { - return AAMDNodes(DenseMapInfo::getEmptyKey(), - nullptr, nullptr); + return AAMDNodes(DenseMapInfo::getEmptyKey(), nullptr, nullptr); } static inline AAMDNodes getTombstoneKey() { - return AAMDNodes(DenseMapInfo::getTombstoneKey(), - nullptr, nullptr); + return AAMDNodes(DenseMapInfo::getTombstoneKey(), nullptr, + nullptr); } static unsigned getHashValue(const AAMDNodes &Val) { @@ -1278,6 +1275,11 @@ if (!Use) return; *Use = MD; + + if (*Use) + MetadataTracking::track(*Use); + + // Equivalent to MetadataTracking::untrack Use = nullptr; } }; @@ -1301,13 +1303,13 @@ explicit NamedMDNode(const Twine &N); - template - class op_iterator_impl : - public std::iterator { + template + class op_iterator_impl + : public std::iterator { const NamedMDNode *Node = nullptr; unsigned Idx = 0; - op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) { } + op_iterator_impl(const NamedMDNode *N, unsigned i) : Node(N), Idx(i) {} friend class NamedMDNode; @@ -1373,13 +1375,15 @@ // typedef op_iterator_impl op_iterator; op_iterator op_begin() { return op_iterator(this, 0); } - op_iterator op_end() { return op_iterator(this, getNumOperands()); } + op_iterator op_end() { return op_iterator(this, getNumOperands()); } typedef op_iterator_impl const_op_iterator; const_op_iterator op_begin() const { return const_op_iterator(this, 0); } - const_op_iterator op_end() const { return const_op_iterator(this, getNumOperands()); } + const_op_iterator op_end() const { + return const_op_iterator(this, getNumOperands()); + } - inline iterator_range operands() { + inline iterator_range operands() { return make_range(op_begin(), op_end()); } inline iterator_range operands() const { Index: test/ThinLTO/X86/Inputs/lazy-load-temporaries-cleanup.b.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/Inputs/lazy-load-temporaries-cleanup.b.ll @@ -0,0 +1,11 @@ +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: test/ThinLTO/X86/lazy-load-temporaries-cleanup.ll =================================================================== --- /dev/null +++ test/ThinLTO/X86/lazy-load-temporaries-cleanup.ll @@ -0,0 +1,3 @@ +; This test ensures that metadata temporaries are properly tracked, then cleaned up + +; RUN: opt -module-summary -bitcode-mdindex-threshold=0 %S/Inputs/lazy-load-temporaries-cleanup.b.ll | llvm-dis -materialize-metadata -set-importing Index: tools/llvm-dis/llvm-dis.cpp =================================================================== --- tools/llvm-dis/llvm-dis.cpp +++ tools/llvm-dis/llvm-dis.cpp @@ -16,13 +16,13 @@ // //===----------------------------------------------------------------------===// -#include "llvm/IR/LLVMContext.h" #include "llvm/Bitcode/BitcodeReader.h" #include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/DebugInfo.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/Support/CommandLine.h" @@ -38,21 +38,26 @@ using namespace llvm; static cl::opt -InputFilename(cl::Positional, cl::desc(""), cl::init("-")); + InputFilename(cl::Positional, cl::desc(""), cl::init("-")); -static cl::opt -OutputFilename("o", cl::desc("Override output filename"), - cl::value_desc("filename")); +static cl::opt OutputFilename("o", + cl::desc("Override output filename"), + cl::value_desc("filename")); -static cl::opt -Force("f", cl::desc("Enable binary output on terminals")); +static cl::opt Force("f", cl::desc("Enable binary output on terminals")); + +static cl::opt DontPrint("disable-output", + cl::desc("Don't output the .ll file"), + cl::Hidden); static cl::opt -DontPrint("disable-output", cl::desc("Don't output the .ll file"), cl::Hidden); + 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")); + ShowAnnotations("show-annotations", + cl::desc("Add informational comments to the .ll file")); static cl::opt PreserveAssemblyUseListOrder( "preserve-ll-uselistorder", @@ -77,7 +82,7 @@ public: void emitFunctionAnnot(const Function *F, formatted_raw_ostream &OS) override { - OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses + OS << "; [#uses=" << F->getNumUses() << ']'; // Output # uses OS << '\n'; } void printInfoComment(const Value &V, formatted_raw_ostream &OS) override { @@ -96,7 +101,7 @@ OS << ";"; } OS << " [debug line = "; - printDebugLoc(DL,OS); + printDebugLoc(DL, OS); OS << "]"; } if (const DbgDeclareInst *DDI = dyn_cast(I)) { @@ -105,8 +110,7 @@ OS << ";"; } OS << " [debug variable = " << DDI->getVariable()->getName() << "]"; - } - else if (const DbgValueInst *DVI = dyn_cast(I)) { + } else if (const DbgValueInst *DVI = dyn_cast(I)) { if (!Padded) { OS.PadToColumn(50); OS << ";"; @@ -123,10 +127,18 @@ raw_ostream &OS = errs(); OS << (char *)Context << ": "; switch (DI.getSeverity()) { - case DS_Error: OS << "error: "; break; - case DS_Warning: OS << "warning: "; break; - case DS_Remark: OS << "remark: "; break; - case DS_Note: OS << "note: "; break; + case DS_Error: + OS << "error: "; + break; + case DS_Warning: + OS << "warning: "; + break; + case DS_Remark: + OS << "remark: "; + break; + case DS_Note: + OS << "note: "; + break; } DiagnosticPrinterRawOStream DP(OS); @@ -142,9 +154,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 @@ -160,7 +172,7 @@ ExitOnErr.setBanner(std::string(argv[0]) + ": error: "); LLVMContext Context; - llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. + llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. Context.setDiagnosticHandler(diagnosticHandler, argv[0]);