Skip to content

Commit

Permalink
[ThinLTO] Fix lazy-loading of MDString instruction attachments
Browse files Browse the repository at this point in the history
CFI is using intrinsics that takes MDString as arguments, and this
was broken during lazy-loading of metadata.

Differential Revision: https://reviews.llvm.org/D28916

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292641 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
joker-eph committed Jan 20, 2017
1 parent bae951d commit 85b2e0b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class BitcodeReader : public BitcodeReaderBase, public GVMaterializer {
}

Metadata *getFnMetadataByID(unsigned ID) {
return MDLoader->getMetadataFwdRef(ID);
return MDLoader->getMetadataFwdRefOrLoad(ID);
}

BasicBlock *getBasicBlock(unsigned ID) const {
Expand Down
21 changes: 17 additions & 4 deletions lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,21 @@ class MetadataLoader::MetadataLoaderImpl {
Error parseMetadata(bool ModuleLevel);

bool hasFwdRefs() const { return MetadataList.hasFwdRefs(); }
Metadata *getMetadataFwdRef(unsigned Idx) {
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 MetadataList.getMetadataFwdRef(ID);
}

MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
Expand Down Expand Up @@ -1730,8 +1743,8 @@ bool MetadataLoader::hasFwdRefs() const { return Pimpl->hasFwdRefs(); }

/// Return the given metadata, creating a replaceable forward reference if
/// necessary.
Metadata *MetadataLoader::getMetadataFwdRef(unsigned Idx) {
return Pimpl->getMetadataFwdRef(Idx);
Metadata *MetadataLoader::getMetadataFwdRefOrLoad(unsigned Idx) {
return Pimpl->getMetadataFwdRefOrLoad(Idx);
}

MDNode *MetadataLoader::getMDNodeFwdRefOrNull(unsigned Idx) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Bitcode/Reader/MetadataLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MetadataLoader {

/// Return the given metadata, creating a replaceable forward reference if
/// necessary.
Metadata *getMetadataFwdRef(unsigned Idx);
Metadata *getMetadataFwdRefOrLoad(unsigned Idx);

MDNode *getMDNodeFwdRefOrNull(unsigned Idx);

Expand Down
10 changes: 7 additions & 3 deletions test/ThinLTO/X86/lazyload_metadata.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -34,6 +35,7 @@ define void @globalfunc1(i32 %arg) {
; 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
}
Expand All @@ -43,6 +45,8 @@ define void @globalfunc3(i32 %arg) {
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"}
Expand All @@ -51,4 +55,4 @@ define void @globalfunc3(i32 %arg) {
!6 = !{!9}
!7 = !{!"7"}
!8 = !{!"8"}
!9 = !{!6}
!9 = !{!6}

0 comments on commit 85b2e0b

Please sign in to comment.