[PATCH] D28916: [ThinLTO] Fix lazy-loading of MDString instruction attachments

Mehdi AMINI via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 13:46:53 PST 2017


mehdi_amini created this revision.

CFI is using intrinsics that takes MDString as arguments, and this
was broken during lazy-loading of metadata.


https://reviews.llvm.org/D28916

Files:
  llvm/lib/Bitcode/Reader/MetadataLoader.cpp
  llvm/test/ThinLTO/X86/lazyload_metadata.ll


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,12 +45,14 @@
   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"}
 !4 = !{!"4"}
 !5 = !{!"5"}
 !6 = !{!9}
 !7 = !{!"7"}
 !8 = !{!"8"}
-!9 = !{!6}
+!9 = !{!6}
\ No newline at end of file
Index: llvm/lib/Bitcode/Reader/MetadataLoader.cpp
===================================================================
--- llvm/lib/Bitcode/Reader/MetadataLoader.cpp
+++ llvm/lib/Bitcode/Reader/MetadataLoader.cpp
@@ -489,6 +489,21 @@
     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);
+      return MetadataList.lookup(ID);
+    }
+    return getMetadataFwdRef(ID);
+  }
+
   MDNode *getMDNodeFwdRefOrNull(unsigned Idx) {
     return MetadataList.getMDNodeFwdRefOrNull(Idx);
   }
@@ -1728,7 +1743,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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28916.85023.patch
Type: text/x-patch
Size: 2987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170119/ccbaba3e/attachment.bin>


More information about the llvm-commits mailing list