Index: llvm/lib/Bitcode/Reader/MetadataLoader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -489,6 +489,22 @@ return MetadataList.getMetadataFwdRef(Idx); } + Metadata *getMetadataFwdRefOrLoad(unsigned ID) { + if (ID < MDStringRef.size()) + return lazyLoadOneMDString(ID); + if (auto *MD = MetadataList.lookup(ID)) + return MD; + // If lazy-loading is enabled, we try recursively to load the operand + // instead of creating a temporary. + if (ID < (MDStringRef.size() + GlobalMetadataBitPosIndex.size())) { + PlaceholderQueue Placeholders; + lazyLoadOneMetadata(ID, Placeholders); + resolveForwardRefsAndPlaceholders(Placeholders); + return MetadataList.lookup(ID); + } + return getMetadataFwdRef(ID); + } + MDNode *getMDNodeFwdRefOrNull(unsigned Idx) { return MetadataList.getMDNodeFwdRefOrNull(Idx); } @@ -1728,7 +1744,7 @@ /// Return the given metadata, creating a replaceable forward reference if /// necessary. Metadata *MetadataLoader::getMetadataFwdRef(unsigned Idx) { - return Pimpl->getMetadataFwdRef(Idx); + return Pimpl->getMetadataFwdRefOrLoad(Idx); } MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) { Index: llvm/test/ThinLTO/X86/lazyload_metadata.ll =================================================================== --- llvm/test/ThinLTO/X86/lazyload_metadata.ll +++ llvm/test/ThinLTO/X86/lazyload_metadata.ll @@ -11,19 +11,20 @@ ; RUN: -o /dev/null -stats \ ; RUN: 2>&1 | FileCheck %s -check-prefix=LAZY ; LAZY: 49 bitcode-reader - Number of Metadata records loaded -; LAZY: 1 bitcode-reader - Number of MDStrings loaded +; LAZY: 2 bitcode-reader - Number of MDStrings loaded ; RUN: llvm-lto -thinlto-action=import %t2.bc -thinlto-index=%t3.bc \ ; RUN: -o /dev/null -disable-ondemand-mds-loading -stats \ ; RUN: 2>&1 | FileCheck %s -check-prefix=NOTLAZY ; NOTLAZY: 58 bitcode-reader - Number of Metadata records loaded -; NOTLAZY: 6 bitcode-reader - Number of MDStrings loaded +; NOTLAZY: 7 bitcode-reader - Number of MDStrings loaded target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.11.0" define void @globalfunc1(i32 %arg) { + %x = call i1 @llvm.type.test(i8* undef, metadata !"typeid1") %tmp = add i32 %arg, 0, !metadata !2 ret void } @@ -34,6 +35,7 @@ ; These function are not imported and so we don't want to load their metadata. define void @globalfunc2(i32 %arg) { + %x = call i1 @llvm.type.test(i8* undef, metadata !"typeid1") %tmp = add i32 %arg, 0, !metadata !1 ret void } @@ -43,6 +45,8 @@ ret void } +declare i1 @llvm.type.test(i8* %ptr, metadata %bitset) nounwind readnone + !1 = !{!2, !3, !4, !5, !6, !7, !8, !9} !2 = !{!"Hello World"} !3 = !{!"3"} @@ -51,4 +55,4 @@ !6 = !{!9} !7 = !{!"7"} !8 = !{!"8"} -!9 = !{!6} +!9 = !{!6} \ No newline at end of file