[llvm] r289977 - Strip invalid TBAA when reading bitcode

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 16 11:16:29 PST 2016


Author: mehdi_amini
Date: Fri Dec 16 13:16:29 2016
New Revision: 289977

URL: http://llvm.org/viewvc/llvm-project?rev=289977&view=rev
Log:
Strip invalid TBAA when reading bitcode

This ensures backward compatibility on bitcode loading.

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

Modified:
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Reader/MetadataLoader.cpp
    llvm/trunk/lib/Bitcode/Reader/MetadataLoader.h
    llvm/trunk/test/Verifier/tbaa.ll

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=289977&r1=289976&r2=289977&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Fri Dec 16 13:16:29 2016
@@ -48,6 +48,7 @@
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/GVMaterializer.h"
 #include "llvm/IR/InlineAsm.h"
+#include "llvm/IR/InstIterator.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
@@ -60,6 +61,7 @@
 #include "llvm/IR/TrackingMDRef.h"
 #include "llvm/IR/Type.h"
 #include "llvm/IR/ValueHandle.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/Support/AtomicOrdering.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
@@ -148,6 +150,16 @@ static bool convertToString(ArrayRef<uin
   return false;
 }
 
+// Strip all the TBAA attachment for the module.
+void stripTBAA(Module *M) {
+  for (auto &F : *M) {
+    if (F.isMaterializable())
+      continue;
+    for (auto &I : instructions(F))
+      I.setMetadata(LLVMContext::MD_tbaa, nullptr);
+  }
+}
+
 /// Read the "IDENTIFICATION_BLOCK_ID" block, do some basic enforcement on the
 /// "epoch" encoded in the bitcode, and return the producer name if any.
 Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
@@ -460,6 +472,7 @@ class BitcodeReader : public BitcodeRead
   bool WillMaterializeAllForwardRefs = false;
 
   bool StripDebugInfo = false;
+  TBAAVerifier TBAAVerifyHelper;
 
   std::vector<std::string> BundleTags;
 
@@ -4449,6 +4462,17 @@ Error BitcodeReader::materialize(GlobalV
   if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F))
     F->setSubprogram(SP);
 
+  // Check if the TBAA Metadata are valid, otherwise we will need to strip them.
+  if (!MDLoader->isStrippingTBAA()) {
+    for (auto &I : instructions(F)) {
+      MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa);
+      if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA))
+        continue;
+      MDLoader->setStripTBAA(true);
+      stripTBAA(F->getParent());
+    }
+  }
+
   // Bring in any functions that this function forward-referenced via
   // blockaddresses.
   return materializeForwardReferencedFunctions();

Modified: llvm/trunk/lib/Bitcode/Reader/MetadataLoader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/MetadataLoader.cpp?rev=289977&r1=289976&r2=289977&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/MetadataLoader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/MetadataLoader.cpp Fri Dec 16 13:16:29 2016
@@ -382,6 +382,7 @@ class MetadataLoader::MetadataLoaderImpl
   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
   DenseMap<unsigned, unsigned> MDKindMap;
 
+  bool StripTBAA = false;
   bool HasSeenOldLoopTags = false;
 
   Error parseMetadataStrings(ArrayRef<uint64_t> Record, StringRef Blob,
@@ -420,6 +421,9 @@ public:
 
   Error parseMetadataKinds();
 
+  void setStripTBAA(bool Value) { StripTBAA = Value; }
+  bool isStrippingTBAA() { return StripTBAA; }
+
   unsigned size() const { return MetadataList.size(); }
   void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); }
 };
@@ -1208,6 +1212,9 @@ Error MetadataLoader::MetadataLoaderImpl
         DenseMap<unsigned, unsigned>::iterator I = MDKindMap.find(Kind);
         if (I == MDKindMap.end())
           return error("Invalid ID");
+        if (I->second == LLVMContext::MD_tbaa && StripTBAA)
+          continue;
+
         Metadata *Node = MetadataList.getMetadataFwdRef(Record[i + 1]);
         if (isa<LocalAsMetadata>(Node))
           // Drop the attachment.  This used to be legal, but there's no
@@ -1327,5 +1334,11 @@ Error MetadataLoader::parseMetadataKinds
   return Pimpl->parseMetadataKinds();
 }
 
+void MetadataLoader::setStripTBAA(bool StripTBAA) {
+  return Pimpl->setStripTBAA(StripTBAA);
+}
+
+bool MetadataLoader::isStrippingTBAA() { return Pimpl->isStrippingTBAA(); }
+
 unsigned MetadataLoader::size() const { return Pimpl->size(); }
 void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); }

Modified: llvm/trunk/lib/Bitcode/Reader/MetadataLoader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/MetadataLoader.h?rev=289977&r1=289976&r2=289977&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/MetadataLoader.h (original)
+++ llvm/trunk/lib/Bitcode/Reader/MetadataLoader.h Fri Dec 16 13:16:29 2016
@@ -52,6 +52,12 @@ public:
   // Parse a function metadata block
   Error parseFunctionMetadata() { return parseMetadata(false); }
 
+  /// Set the mode to strip TBAA metadata on load.
+  void setStripTBAA(bool StripTBAA = true);
+
+  /// Return true if the Loader is stripping TBAA metadata.
+  bool isStrippingTBAA();
+
   // Return true there are remaining unresolved forward references.
   bool hasFwdRefs() const;
 

Modified: llvm/trunk/test/Verifier/tbaa.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Verifier/tbaa.ll?rev=289977&r1=289976&r2=289977&view=diff
==============================================================================
--- llvm/trunk/test/Verifier/tbaa.ll (original)
+++ llvm/trunk/test/Verifier/tbaa.ll Fri Dec 16 13:16:29 2016
@@ -1,5 +1,9 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
+; RUN: llvm-as -disable-verify < %s 2>&1 | opt -verify -S | FileCheck %s  --check-prefix=STRIP
 
+; STRIP-NOT: tbaa
+; STRIP: @f_0
+; STRIP: Do no strip this
 define void @f_0(i32* %ptr) {
 ; This part checks for the easy syntactic verifier rules.
 
@@ -34,10 +38,10 @@ define void @f_0(i32* %ptr) {
   store i32 4, i32* %ptr, !tbaa !{!3, null, !"40", i64 0}
   store i32 5, i32* %ptr, !tbaa !{!3, !3, !"40", i64 0}
   store i32 6, i32* %ptr, !tbaa !{!3, !2, i32 40, i64 0}
-  store i32 7, i32* %ptr, !tbaa !{!3, !12, i32 40, i64 0}
+  store i32 7, i32* %ptr, !tbaa !{!3, !12, i32 40, i64 0}, !metadata !42
   ret void
 }
-
+!42 = !{!"Do no strip this!"}
 
 define void @f_1(i32* %ptr) {
 ; This part checks for more semantic verifier rules.




More information about the llvm-commits mailing list