Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -28,8 +28,8 @@ #include "llvm/IR/Attributes.h" #include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/BasicBlock.h" -#include "llvm/IR/CallingConv.h" #include "llvm/IR/CallSite.h" +#include "llvm/IR/CallingConv.h" #include "llvm/IR/Comdat.h" #include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" @@ -40,14 +40,15 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/Function.h" +#include "llvm/IR/GVMaterializer.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalIFunc.h" #include "llvm/IR/GlobalIndirectSymbol.h" #include "llvm/IR/GlobalObject.h" #include "llvm/IR/GlobalValue.h" #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,17 @@ 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)) + if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa)) + I.dropUnknownNonDebugMetadata(TBAA->getMetadataID()); + } +} + /// 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 readIdentificationBlock(BitstreamCursor &Stream) { @@ -460,6 +473,8 @@ bool WillMaterializeAllForwardRefs = false; bool StripDebugInfo = false; + bool StripTBAA = false; + TBAAVerifier TBAAVerifyHelper; std::vector BundleTags; @@ -4450,6 +4465,18 @@ if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) F->setSubprogram(SP); + // Check if the TBAA Metadata are valid, otherwise we will need to strip them. + if (!StripTBAA) { + for (auto &I : instructions(F)) { + MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa); + if (!TBAA || TBAAVerifyHelper.visitTBAAMetadata(I, TBAA)) + continue; + StripTBAA = true; + MDLoader->setStripTBAA(StripTBAA); + stripTBAA(F->getParent()); + } + } + // Bring in any functions that this function forward-referenced via // blockaddresses. return materializeForwardReferencedFunctions(); Index: llvm/lib/Bitcode/Reader/MetadataLoader.h =================================================================== --- llvm/lib/Bitcode/Reader/MetadataLoader.h +++ llvm/lib/Bitcode/Reader/MetadataLoader.h @@ -55,6 +55,9 @@ return parseMetadata(false, /* OnDemand = */ false); } + // Set the mode to strip TBAA metadata on load + void setStripTBAA(bool StripTBAA = true); + // Return true there are remaining unresolved forward references. bool hasFwdRefs() const; Index: llvm/lib/Bitcode/Reader/MetadataLoader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/MetadataLoader.cpp +++ llvm/lib/Bitcode/Reader/MetadataLoader.cpp @@ -389,6 +389,7 @@ // Map the bitcode's custom MDKind ID to the Module's MDKind ID. DenseMap MDKindMap; + bool StripTBAA = false; bool HasSeenOldLoopTags = false; Error parseOneMetadata(SmallVectorImpl &Record, unsigned Code, PlaceholderQueue &Placeholders, StringRef Blob, @@ -429,6 +430,8 @@ Error parseMetadataKinds(); + void setStripTBAA(bool Value) { StripTBAA = Value; } + unsigned size() const { return MetadataList.size(); } void shrinkTo(unsigned N) { MetadataList.shrinkTo(N); } }; @@ -1340,6 +1343,8 @@ MD = upgradeInstructionLoopAttachment(*MD); if (I->second == LLVMContext::MD_tbaa) { + if (StripTBAA) + break; assert(!MD->isTemporary() && "should load MDs before attachments"); MD = UpgradeTBAANode(*MD); } @@ -1446,5 +1451,9 @@ return Pimpl->parseMetadataKinds(); } +void MetadataLoader::setStripTBAA(bool StripTBAA) { + return Pimpl->setStripTBAA(StripTBAA); +} + unsigned MetadataLoader::size() const { return Pimpl->size(); } void MetadataLoader::shrinkTo(unsigned N) { return Pimpl->shrinkTo(N); } Index: llvm/test/Verifier/tbaa.ll =================================================================== --- llvm/test/Verifier/tbaa.ll +++ llvm/test/Verifier/tbaa.ll @@ -1,4 +1,8 @@ ; 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 define void @f_0(i32* %ptr) { ; This part checks for the easy syntactic verifier rules.