diff options
Diffstat (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp')
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 404 |
1 files changed, 265 insertions, 139 deletions
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 33464412edc5..659e26c2bd25 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -27,7 +27,6 @@ #include "llvm/IR/Attributes.h" #include "llvm/IR/AutoUpgrade.h" #include "llvm/IR/BasicBlock.h" -#include "llvm/IR/CallSite.h" #include "llvm/IR/CallingConv.h" #include "llvm/IR/Comdat.h" #include "llvm/IR/Constant.h" @@ -153,8 +152,7 @@ static bool convertToString(ArrayRef<uint64_t> Record, unsigned Idx, if (Idx > Record.size()) return true; - for (unsigned i = Idx, e = Record.size(); i != e; ++i) - Result += (char)Record[i]; + Result.append(Record.begin() + Idx, Record.end()); return false; } @@ -578,8 +576,11 @@ public: /// Main interface to parsing a bitcode buffer. /// \returns true if an error occurred. - Error parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata = false, - bool IsImporting = false); + Error parseBitcodeInto( + Module *M, bool ShouldLazyLoadMetadata = false, bool IsImporting = false, + DataLayoutCallbackTy DataLayoutCallback = [](std::string) { + return None; + }); static uint64_t decodeSignRotatedValue(uint64_t V); @@ -724,7 +725,9 @@ private: /// a corresponding error code. Error parseAlignmentValue(uint64_t Exponent, MaybeAlign &Alignment); Error parseAttrKind(uint64_t Code, Attribute::AttrKind *Kind); - Error parseModule(uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false); + Error parseModule( + uint64_t ResumeBit, bool ShouldLazyLoadMetadata = false, + DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; }); Error parseComdatRecord(ArrayRef<uint64_t> Record); Error parseGlobalVarRecord(ArrayRef<uint64_t> Record); @@ -859,7 +862,7 @@ BitcodeReader::BitcodeReader(BitstreamCursor Stream, StringRef Strtab, LLVMContext &Context) : BitcodeReaderBase(std::move(Stream), Strtab), Context(Context), ValueList(Context, Stream.SizeInBytes()) { - this->ProducerIdentification = ProducerIdentification; + this->ProducerIdentification = std::string(ProducerIdentification); } Error BitcodeReader::materializeForwardReferencedFunctions() { @@ -985,8 +988,10 @@ static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags, // Decode the flags for GlobalVariable in the summary static GlobalVarSummary::GVarFlags getDecodedGVarFlags(uint64_t RawFlags) { - return GlobalVarSummary::GVarFlags((RawFlags & 0x1) ? true : false, - (RawFlags & 0x2) ? true : false); + return GlobalVarSummary::GVarFlags( + (RawFlags & 0x1) ? true : false, (RawFlags & 0x2) ? true : false, + (RawFlags & 0x4) ? true : false, + (GlobalObject::VCallVisibility)(RawFlags >> 3)); } static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) { @@ -1216,6 +1221,8 @@ StructType *BitcodeReader::createIdentifiedStructType(LLVMContext &Context) { static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { switch (Val) { case Attribute::EndAttrKinds: + case Attribute::EmptyKey: + case Attribute::TombstoneKey: llvm_unreachable("Synthetic enumerators which should never get here"); case Attribute::None: return 0; @@ -1281,25 +1288,10 @@ static uint64_t getRawAttributeMask(Attribute::AttrKind Val) { return 1ULL << 62; case Attribute::NoFree: return 1ULL << 63; - case Attribute::NoSync: - llvm_unreachable("nosync attribute not supported in raw format"); - break; - case Attribute::Dereferenceable: - llvm_unreachable("dereferenceable attribute not supported in raw format"); - break; - case Attribute::DereferenceableOrNull: - llvm_unreachable("dereferenceable_or_null attribute not supported in raw " - "format"); - break; - case Attribute::ArgMemOnly: - llvm_unreachable("argmemonly attribute not supported in raw format"); - break; - case Attribute::AllocSize: - llvm_unreachable("allocsize not supported in raw format"); - break; - case Attribute::SanitizeMemTag: - llvm_unreachable("sanitize_memtag attribute not supported in raw format"); - break; + default: + // Other attributes are not supported in the raw format, + // as we ran out of space. + return 0; } llvm_unreachable("Unsupported attribute type"); } @@ -1309,13 +1301,6 @@ static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { for (Attribute::AttrKind I = Attribute::None; I != Attribute::EndAttrKinds; I = Attribute::AttrKind(I + 1)) { - if (I == Attribute::SanitizeMemTag || - I == Attribute::Dereferenceable || - I == Attribute::DereferenceableOrNull || - I == Attribute::ArgMemOnly || - I == Attribute::AllocSize || - I == Attribute::NoSync) - continue; if (uint64_t A = (Val & getRawAttributeMask(I))) { if (I == Attribute::Alignment) B.addAlignmentAttr(1ULL << ((A >> 16) - 1)); @@ -1332,8 +1317,6 @@ static void addRawAttributeValue(AttrBuilder &B, uint64_t Val) { /// 'encodeLLVMAttributesForBitcode'. static void decodeLLVMAttributesForBitcode(AttrBuilder &B, uint64_t EncodedAttrs) { - // FIXME: Remove in 4.0. - // The alignment is stored as a 16-bit raw value from bits 31--16. We shift // the bits above 31 down by 11 bits. unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16; @@ -1384,7 +1367,7 @@ Error BitcodeReader::parseAttributeBlock() { default: // Default behavior: ignore. break; case bitc::PARAMATTR_CODE_ENTRY_OLD: // ENTRY: [paramidx0, attr0, ...] - // FIXME: Remove in 4.0. + // Deprecated, but still needed to read old bitcode files. if (Record.size() & 1) return error("Invalid record"); @@ -1461,6 +1444,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) { return Attribute::NoInline; case bitc::ATTR_KIND_NO_RECURSE: return Attribute::NoRecurse; + case bitc::ATTR_KIND_NO_MERGE: + return Attribute::NoMerge; case bitc::ATTR_KIND_NON_LAZY_BIND: return Attribute::NonLazyBind; case bitc::ATTR_KIND_NON_NULL: @@ -1481,6 +1466,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) { return Attribute::NoCfCheck; case bitc::ATTR_KIND_NO_UNWIND: return Attribute::NoUnwind; + case bitc::ATTR_KIND_NULL_POINTER_IS_VALID: + return Attribute::NullPointerIsValid; case bitc::ATTR_KIND_OPT_FOR_FUZZING: return Attribute::OptForFuzzing; case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE: @@ -1541,6 +1528,10 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) { return Attribute::ImmArg; case bitc::ATTR_KIND_SANITIZE_MEMTAG: return Attribute::SanitizeMemTag; + case bitc::ATTR_KIND_PREALLOCATED: + return Attribute::Preallocated; + case bitc::ATTR_KIND_NOUNDEF: + return Attribute::NoUndef; } } @@ -1656,12 +1647,15 @@ Error BitcodeReader::parseAttributeGroupBlock() { Attribute::AttrKind Kind; if (Error Err = parseAttrKind(Record[++i], &Kind)) return Err; - if (Kind == Attribute::ByVal) + if (Kind == Attribute::ByVal) { B.addByValAttr(HasType ? getTypeByID(Record[++i]) : nullptr); + } else if (Kind == Attribute::Preallocated) { + B.addPreallocatedAttr(getTypeByID(Record[++i])); + } } } - UpgradeFramePointerAttributes(B); + UpgradeAttributes(B); MAttributeGroups[GrpID] = AttributeList::get(Context, Idx, B); break; } @@ -1727,6 +1721,9 @@ Error BitcodeReader::parseTypeTableBody() { case bitc::TYPE_CODE_HALF: // HALF ResultTy = Type::getHalfTy(Context); break; + case bitc::TYPE_CODE_BFLOAT: // BFLOAT + ResultTy = Type::getBFloatTy(Context); + break; case bitc::TYPE_CODE_FLOAT: // FLOAT ResultTy = Type::getFloatTy(Context); break; @@ -1780,7 +1777,7 @@ Error BitcodeReader::parseTypeTableBody() { break; } case bitc::TYPE_CODE_FUNCTION_OLD: { - // FIXME: attrid is dead, remove it in LLVM 4.0 + // Deprecated, but still needed to read old bitcode files. // FUNCTION: [vararg, attrid, retty, paramty x N] if (Record.size() < 3) return error("Invalid record"); @@ -2314,7 +2311,7 @@ Error BitcodeReader::resolveGlobalAndIndirectSymbolInits() { return Error::success(); } -static APInt readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { +APInt llvm::readWideAPInt(ArrayRef<uint64_t> Vals, unsigned TypeBits) { SmallVector<uint64_t, 8> Words(Vals.size()); transform(Vals, Words.begin(), BitcodeReader::decodeSignRotatedValue); @@ -2333,6 +2330,16 @@ Error BitcodeReader::parseConstants() { Type *CurFullTy = Type::getInt32Ty(Context); unsigned NextCstNo = ValueList.size(); + struct DelayedShufTy { + VectorType *OpTy; + VectorType *RTy; + Type *CurFullTy; + uint64_t Op0Idx; + uint64_t Op1Idx; + uint64_t Op2Idx; + unsigned CstNo; + }; + std::vector<DelayedShufTy> DelayedShuffles; while (true) { Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); if (!MaybeEntry) @@ -2344,11 +2351,35 @@ Error BitcodeReader::parseConstants() { case BitstreamEntry::Error: return error("Malformed block"); case BitstreamEntry::EndBlock: + // Once all the constants have been read, go through and resolve forward + // references. + // + // We have to treat shuffles specially because they don't have three + // operands anymore. We need to convert the shuffle mask into an array, + // and we can't convert a forward reference. + for (auto &DelayedShuffle : DelayedShuffles) { + VectorType *OpTy = DelayedShuffle.OpTy; + VectorType *RTy = DelayedShuffle.RTy; + uint64_t Op0Idx = DelayedShuffle.Op0Idx; + uint64_t Op1Idx = DelayedShuffle.Op1Idx; + uint64_t Op2Idx = DelayedShuffle.Op2Idx; + uint64_t CstNo = DelayedShuffle.CstNo; + Constant *Op0 = ValueList.getConstantFwdRef(Op0Idx, OpTy); + Constant *Op1 = ValueList.getConstantFwdRef(Op1Idx, OpTy); + Type *ShufTy = + VectorType::get(Type::getInt32Ty(Context), RTy->getElementCount()); + Constant *Op2 = ValueList.getConstantFwdRef(Op2Idx, ShufTy); + if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2)) + return error("Invalid shufflevector operands"); + SmallVector<int, 16> Mask; + ShuffleVectorInst::getShuffleMask(Op2, Mask); + Value *V = ConstantExpr::getShuffleVector(Op0, Op1, Mask); + ValueList.assignValue(V, CstNo, DelayedShuffle.CurFullTy); + } + if (NextCstNo != ValueList.size()) return error("Invalid constant reference"); - // Once all the constants have been read, go through and resolve forward - // references. ValueList.resolveConstantForwardRefs(); return Error::success(); case BitstreamEntry::Record: @@ -2404,6 +2435,9 @@ Error BitcodeReader::parseConstants() { if (CurTy->isHalfTy()) V = ConstantFP::get(Context, APFloat(APFloat::IEEEhalf(), APInt(16, (uint16_t)Record[0]))); + else if (CurTy->isBFloatTy()) + V = ConstantFP::get(Context, APFloat(APFloat::BFloat(), + APInt(16, (uint32_t)Record[0]))); else if (CurTy->isFloatTy()) V = ConstantFP::get(Context, APFloat(APFloat::IEEEsingle(), APInt(32, (uint32_t)Record[0]))); @@ -2469,7 +2503,11 @@ Error BitcodeReader::parseConstants() { if (Record.empty()) return error("Invalid record"); - Type *EltTy = cast<SequentialType>(CurTy)->getElementType(); + Type *EltTy; + if (auto *Array = dyn_cast<ArrayType>(CurTy)) + EltTy = Array->getElementType(); + else + EltTy = cast<VectorType>(CurTy)->getElementType(); if (EltTy->isIntegerTy(8)) { SmallVector<uint8_t, 16> Elts(Record.begin(), Record.end()); if (isa<VectorType>(CurTy)) @@ -2497,21 +2535,27 @@ Error BitcodeReader::parseConstants() { } else if (EltTy->isHalfTy()) { SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); if (isa<VectorType>(CurTy)) - V = ConstantDataVector::getFP(Context, Elts); + V = ConstantDataVector::getFP(EltTy, Elts); + else + V = ConstantDataArray::getFP(EltTy, Elts); + } else if (EltTy->isBFloatTy()) { + SmallVector<uint16_t, 16> Elts(Record.begin(), Record.end()); + if (isa<VectorType>(CurTy)) + V = ConstantDataVector::getFP(EltTy, Elts); else - V = ConstantDataArray::getFP(Context, Elts); + V = ConstantDataArray::getFP(EltTy, Elts); } else if (EltTy->isFloatTy()) { SmallVector<uint32_t, 16> Elts(Record.begin(), Record.end()); if (isa<VectorType>(CurTy)) - V = ConstantDataVector::getFP(Context, Elts); + V = ConstantDataVector::getFP(EltTy, Elts); else - V = ConstantDataArray::getFP(Context, Elts); + V = ConstantDataArray::getFP(EltTy, Elts); } else if (EltTy->isDoubleTy()) { SmallVector<uint64_t, 16> Elts(Record.begin(), Record.end()); if (isa<VectorType>(CurTy)) - V = ConstantDataVector::getFP(Context, Elts); + V = ConstantDataVector::getFP(EltTy, Elts); else - V = ConstantDataArray::getFP(Context, Elts); + V = ConstantDataArray::getFP(EltTy, Elts); } else { return error("Invalid type for value"); } @@ -2629,12 +2673,13 @@ Error BitcodeReader::parseConstants() { Type *SelectorTy = Type::getInt1Ty(Context); - // The selector might be an i1 or an <n x i1> + // The selector might be an i1, an <n x i1>, or a <vscale x n x i1> // Get the type from the ValueList before getting a forward ref. if (VectorType *VTy = dyn_cast<VectorType>(CurTy)) if (Value *V = ValueList[Record[0]]) if (SelectorTy != V->getType()) - SelectorTy = VectorType::get(SelectorTy, VTy->getNumElements()); + SelectorTy = VectorType::get(SelectorTy, + VTy->getElementCount()); V = ConstantExpr::getSelect(ValueList.getConstantFwdRef(Record[0], SelectorTy), @@ -2657,8 +2702,10 @@ Error BitcodeReader::parseConstants() { if (!IdxTy) return error("Invalid record"); Op1 = ValueList.getConstantFwdRef(Record[3], IdxTy); - } else // TODO: Remove with llvm 4.0 + } else { + // Deprecated, but still needed to read old bitcode files. Op1 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); + } if (!Op1) return error("Invalid record"); V = ConstantExpr::getExtractElement(Op0, Op1); @@ -2678,8 +2725,10 @@ Error BitcodeReader::parseConstants() { if (!IdxTy) return error("Invalid record"); Op2 = ValueList.getConstantFwdRef(Record[3], IdxTy); - } else // TODO: Remove with llvm 4.0 + } else { + // Deprecated, but still needed to read old bitcode files. Op2 = ValueList.getConstantFwdRef(Record[2], Type::getInt32Ty(Context)); + } if (!Op2) return error("Invalid record"); V = ConstantExpr::getInsertElement(Op0, Op1, Op2); @@ -2689,13 +2738,10 @@ Error BitcodeReader::parseConstants() { VectorType *OpTy = dyn_cast<VectorType>(CurTy); if (Record.size() < 3 || !OpTy) return error("Invalid record"); - Constant *Op0 = ValueList.getConstantFwdRef(Record[0], OpTy); - Constant *Op1 = ValueList.getConstantFwdRef(Record[1], OpTy); - Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), - OpTy->getNumElements()); - Constant *Op2 = ValueList.getConstantFwdRef(Record[2], ShufTy); - V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); - break; + DelayedShuffles.push_back( + {OpTy, OpTy, CurFullTy, Record[0], Record[1], Record[2], NextCstNo}); + ++NextCstNo; + continue; } case bitc::CST_CODE_CE_SHUFVEC_EX: { // [opty, opval, opval, opval] VectorType *RTy = dyn_cast<VectorType>(CurTy); @@ -2703,13 +2749,10 @@ Error BitcodeReader::parseConstants() { dyn_cast_or_null<VectorType>(getTypeByID(Record[0])); if (Record.size() < 4 || !RTy || !OpTy) return error("Invalid record"); - Constant *Op0 = ValueList.getConstantFwdRef(Record[1], OpTy); - Constant *Op1 = ValueList.getConstantFwdRef(Record[2], OpTy); - Type *ShufTy = VectorType::get(Type::getInt32Ty(Context), - RTy->getNumElements()); - Constant *Op2 = ValueList.getConstantFwdRef(Record[3], ShufTy); - V = ConstantExpr::getShuffleVector(Op0, Op1, Op2); - break; + DelayedShuffles.push_back( + {OpTy, RTy, CurFullTy, Record[1], Record[2], Record[3], NextCstNo}); + ++NextCstNo; + continue; } case bitc::CST_CODE_CE_CMP: { // CE_CMP: [opty, opval, opval, pred] if (Record.size() < 4) @@ -2727,7 +2770,7 @@ Error BitcodeReader::parseConstants() { break; } // This maintains backward compatibility, pre-asm dialect keywords. - // FIXME: Remove with the 4.0 release. + // Deprecated, but still needed to read old bitcode files. case bitc::CST_CODE_INLINEASM_OLD: { if (Record.size() < 2) return error("Invalid record"); @@ -2967,6 +3010,7 @@ Error BitcodeReader::globalCleanup() { return error("Malformed global initializer set"); // Look for intrinsic functions which need to be upgraded at some point + // and functions that need to have their function attributes upgraded. for (Function &F : *TheModule) { MDLoader->upgradeDebugIntrinsics(F); Function *NewFn; @@ -2977,6 +3021,8 @@ Error BitcodeReader::globalCleanup() { // loaded in the same LLVMContext (LTO scenario). In this case we should // remangle intrinsics names as well. RemangledIntrinsics[&F] = Remangled.getValue(); + // Look for functions that rely on old function attribute behavior. + UpgradeFunctionAttributes(F); } // Look for global variables which need to be renamed. @@ -3125,8 +3171,8 @@ Error BitcodeReader::parseGlobalVarRecord(ArrayRef<uint64_t> Record) { } GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility; // Local linkage must have default visibility. + // auto-upgrade `hidden` and `protected` for old bitcode. if (Record.size() > 6 && !GlobalValue::isLocalLinkage(Linkage)) - // FIXME: Change to an error if non-default in 4.0. Visibility = getDecodedVisibility(Record[6]); GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal; @@ -3255,8 +3301,8 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) { Func->setSection(SectionTable[Record[6] - 1]); } // Local linkage must have default visibility. + // auto-upgrade `hidden` and `protected` for old bitcode. if (!Func->hasLocalLinkage()) - // FIXME: Change to an error if non-default in 4.0. Func->setVisibility(getDecodedVisibility(Record[7])); if (Record.size() > 8 && Record[8]) { if (Record[8] - 1 >= GCTable.size()) @@ -3363,12 +3409,11 @@ Error BitcodeReader::parseGlobalIndirectSymbolRecord( assert(NewGA->getValueType() == flattenPointerTypes(FullTy) && "Incorrect fully structured type provided for GlobalIndirectSymbol"); - // Old bitcode files didn't have visibility field. // Local linkage must have default visibility. + // auto-upgrade `hidden` and `protected` for old bitcode. if (OpNum != Record.size()) { auto VisInd = OpNum++; if (!NewGA->hasLocalLinkage()) - // FIXME: Change to an error if non-default in 4.0. NewGA->setVisibility(getDecodedVisibility(Record[VisInd])); } if (BitCode == bitc::MODULE_CODE_ALIAS || @@ -3402,7 +3447,8 @@ Error BitcodeReader::parseGlobalIndirectSymbolRecord( } Error BitcodeReader::parseModule(uint64_t ResumeBit, - bool ShouldLazyLoadMetadata) { + bool ShouldLazyLoadMetadata, + DataLayoutCallbackTy DataLayoutCallback) { if (ResumeBit) { if (Error JumpFailed = Stream.JumpToBit(ResumeBit)) return JumpFailed; @@ -3411,6 +3457,26 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, SmallVector<uint64_t, 64> Record; + // Parts of bitcode parsing depend on the datalayout. Make sure we + // finalize the datalayout before we run any of that code. + bool ResolvedDataLayout = false; + auto ResolveDataLayout = [&] { + if (ResolvedDataLayout) + return; + + // datalayout and triple can't be parsed after this point. + ResolvedDataLayout = true; + + // Upgrade data layout string. + std::string DL = llvm::UpgradeDataLayoutString( + TheModule->getDataLayoutStr(), TheModule->getTargetTriple()); + TheModule->setDataLayout(DL); + + if (auto LayoutOverride = + DataLayoutCallback(TheModule->getTargetTriple())) + TheModule->setDataLayout(*LayoutOverride); + }; + // Read all the records for this module. while (true) { Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance(); @@ -3422,6 +3488,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, case BitstreamEntry::Error: return error("Malformed block"); case BitstreamEntry::EndBlock: + ResolveDataLayout(); return globalCleanup(); case BitstreamEntry::SubBlock: @@ -3486,6 +3553,8 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, return Err; break; case bitc::FUNCTION_BLOCK_ID: + ResolveDataLayout(); + // If this is the first function body we've seen, reverse the // FunctionsWithBodies list. if (!SeenFirstFunctionBody) { @@ -3572,6 +3641,8 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, break; } case bitc::MODULE_CODE_TRIPLE: { // TRIPLE: [strchr x N] + if (ResolvedDataLayout) + return error("target triple too late in module"); std::string S; if (convertToString(Record, 0, S)) return error("Invalid record"); @@ -3579,6 +3650,8 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, break; } case bitc::MODULE_CODE_DATALAYOUT: { // DATALAYOUT: [strchr x N] + if (ResolvedDataLayout) + return error("datalayout too late in module"); std::string S; if (convertToString(Record, 0, S)) return error("Invalid record"); @@ -3593,7 +3666,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, break; } case bitc::MODULE_CODE_DEPLIB: { // DEPLIB: [strchr x N] - // FIXME: Remove in 4.0. + // Deprecated, but still needed to read old bitcode files. std::string S; if (convertToString(Record, 0, S)) return error("Invalid record"); @@ -3623,6 +3696,7 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, return Err; break; case bitc::MODULE_CODE_FUNCTION: + ResolveDataLayout(); if (Error Err = parseFunctionRecord(Record)) return Err; break; @@ -3650,20 +3724,16 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, break; } Record.clear(); - - // Upgrade data layout string. - std::string DL = llvm::UpgradeDataLayoutString( - TheModule->getDataLayoutStr(), TheModule->getTargetTriple()); - TheModule->setDataLayout(DL); } } Error BitcodeReader::parseBitcodeInto(Module *M, bool ShouldLazyLoadMetadata, - bool IsImporting) { + bool IsImporting, + DataLayoutCallbackTy DataLayoutCallback) { TheModule = M; MDLoader = MetadataLoader(Stream, *M, ValueList, IsImporting, [&](unsigned ID) { return getTypeByID(ID); }); - return parseModule(0, ShouldLazyLoadMetadata); + return parseModule(0, ShouldLazyLoadMetadata, DataLayoutCallback); } Error BitcodeReader::typeCheckLoadStoreInst(Type *ValType, Type *PtrType) { @@ -4135,7 +4205,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) { if (!Vec->getType()->isVectorTy()) return error("Invalid type for value"); I = ExtractElementInst::Create(Vec, Idx); - FullTy = FullTy->getVectorElementType(); + FullTy = cast<VectorType>(FullTy)->getElementType(); InstructionList.push_back(I); break; } @@ -4167,9 +4237,11 @@ Error BitcodeReader::parseFunctionBody(Function *F) { return error("Invalid record"); if (!Vec1->getType()->isVectorTy() || !Vec2->getType()->isVectorTy()) return error("Invalid type for value"); + I = new ShuffleVectorInst(Vec1, Vec2, Mask); - FullTy = VectorType::get(FullTy->getVectorElementType(), - Mask->getType()->getVectorNumElements()); + FullTy = + VectorType::get(cast<VectorType>(FullTy)->getElementType(), + cast<VectorType>(Mask->getType())->getElementCount()); InstructionList.push_back(I); break; } @@ -4763,7 +4835,13 @@ Error BitcodeReader::parseFunctionBody(Function *F) { const DataLayout &DL = TheModule->getDataLayout(); unsigned AS = DL.getAllocaAddrSpace(); - AllocaInst *AI = new AllocaInst(Ty, AS, Size, Align); + SmallPtrSet<Type *, 4> Visited; + if (!Align && !Ty->isSized(&Visited)) + return error("alloca of unsized type"); + if (!Align) + Align = DL.getPrefTypeAlign(Ty); + + AllocaInst *AI = new AllocaInst(Ty, AS, Size, *Align); AI->setUsedWithInAlloca(InAlloca); AI->setSwiftError(SwiftError); I = AI; @@ -4794,7 +4872,12 @@ Error BitcodeReader::parseFunctionBody(Function *F) { MaybeAlign Align; if (Error Err = parseAlignmentValue(Record[OpNum], Align)) return Err; - I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align); + SmallPtrSet<Type *, 4> Visited; + if (!Align && !Ty->isSized(&Visited)) + return error("load of unsized type"); + if (!Align) + Align = TheModule->getDataLayout().getABITypeAlign(Ty); + I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align); InstructionList.push_back(I); break; } @@ -4831,7 +4914,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) { MaybeAlign Align; if (Error Err = parseAlignmentValue(Record[OpNum], Align)) return Err; - I = new LoadInst(Ty, Op, "", Record[OpNum + 1], Align, Ordering, SSID); + if (!Align) + return error("Alignment missing from atomic load"); + I = new LoadInst(Ty, Op, "", Record[OpNum + 1], *Align, Ordering, SSID); InstructionList.push_back(I); break; } @@ -4853,7 +4938,12 @@ Error BitcodeReader::parseFunctionBody(Function *F) { MaybeAlign Align; if (Error Err = parseAlignmentValue(Record[OpNum], Align)) return Err; - I = new StoreInst(Val, Ptr, Record[OpNum + 1], Align); + SmallPtrSet<Type *, 4> Visited; + if (!Align && !Val->getType()->isSized(&Visited)) + return error("store of unsized type"); + if (!Align) + Align = TheModule->getDataLayout().getABITypeAlign(Val->getType()); + I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align); InstructionList.push_back(I); break; } @@ -4886,7 +4976,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) { MaybeAlign Align; if (Error Err = parseAlignmentValue(Record[OpNum], Align)) return Err; - I = new StoreInst(Val, Ptr, Record[OpNum + 1], Align, Ordering, SSID); + if (!Align) + return error("Alignment missing from atomic store"); + I = new StoreInst(Val, Ptr, Record[OpNum + 1], *Align, Ordering, SSID); InstructionList.push_back(I); break; } @@ -4930,8 +5022,10 @@ Error BitcodeReader::parseFunctionBody(Function *F) { else FailureOrdering = getDecodedOrdering(Record[OpNum + 3]); - I = new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering, FailureOrdering, - SSID); + Align Alignment( + TheModule->getDataLayout().getTypeStoreSize(Cmp->getType())); + I = new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment, SuccessOrdering, + FailureOrdering, SSID); FullTy = StructType::get(Context, {FullTy, Type::getInt1Ty(Context)}); cast<AtomicCmpXchgInst>(I)->setVolatile(Record[OpNum]); @@ -4968,7 +5062,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) { Ordering == AtomicOrdering::Unordered) return error("Invalid record"); SyncScope::ID SSID = getDecodedSyncScopeID(Record[OpNum + 3]); - I = new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID); + Align Alignment( + TheModule->getDataLayout().getTypeStoreSize(Val->getType())); + I = new AtomicRMWInst(Operation, Ptr, Val, Alignment, Ordering, SSID); FullTy = getPointerElementFlatType(FullTy); cast<AtomicRMWInst>(I)->setVolatile(Record[OpNum+1]); InstructionList.push_back(I); @@ -5161,8 +5257,8 @@ Error BitcodeReader::parseFunctionBody(Function *F) { !FullTy->isPointerTy() && !isa<StructType>(FullTy) && !isa<ArrayType>(FullTy) && (!isa<VectorType>(FullTy) || - FullTy->getVectorElementType()->isFloatingPointTy() || - FullTy->getVectorElementType()->isIntegerTy()) && + cast<VectorType>(FullTy)->getElementType()->isFloatingPointTy() || + cast<VectorType>(FullTy)->getElementType()->isIntegerTy()) && "Structured types must be assigned with corresponding non-opaque " "pointer type"); } @@ -5277,7 +5373,7 @@ Error BitcodeReader::materialize(GlobalValue *GV) { for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end(); UI != UE;) // Don't expect any other users than call sites - CallSite(*UI++).setCalledFunction(I.second); + cast<CallBase>(*UI++)->setCalledFunction(I.second); // Finish fn->subprogram upgrade for materialized functions. if (DISubprogram *SP = MDLoader->lookupSubprogramForFunction(F)) @@ -5294,6 +5390,9 @@ Error BitcodeReader::materialize(GlobalValue *GV) { } } + // Look for functions that rely on old function attribute behavior. + UpgradeFunctionAttributes(*F); + // Bring in any functions that this function forward-referenced via // blockaddresses. return materializeForwardReferencedFunctions(); @@ -5722,6 +5821,41 @@ static void parseTypeIdSummaryRecord(ArrayRef<uint64_t> Record, parseWholeProgramDevirtResolution(Record, Strtab, Slot, TypeId); } +static std::vector<FunctionSummary::ParamAccess> +parseParamAccesses(ArrayRef<uint64_t> Record) { + auto ReadRange = [&]() { + APInt Lower(FunctionSummary::ParamAccess::RangeWidth, + BitcodeReader::decodeSignRotatedValue(Record.front())); + Record = Record.drop_front(); + APInt Upper(FunctionSummary::ParamAccess::RangeWidth, + BitcodeReader::decodeSignRotatedValue(Record.front())); + Record = Record.drop_front(); + ConstantRange Range{Lower, Upper}; + assert(!Range.isFullSet()); + assert(!Range.isUpperSignWrapped()); + return Range; + }; + + std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; + while (!Record.empty()) { + PendingParamAccesses.emplace_back(); + FunctionSummary::ParamAccess &ParamAccess = PendingParamAccesses.back(); + ParamAccess.ParamNo = Record.front(); + Record = Record.drop_front(); + ParamAccess.Use = ReadRange(); + ParamAccess.Calls.resize(Record.front()); + Record = Record.drop_front(); + for (auto &Call : ParamAccess.Calls) { + Call.ParamNo = Record.front(); + Record = Record.drop_front(); + Call.Callee = Record.front(); + Record = Record.drop_front(); + Call.Offsets = ReadRange(); + } + } + return PendingParamAccesses; +} + void ModuleSummaryIndexBitcodeReader::parseTypeIdCompatibleVtableInfo( ArrayRef<uint64_t> Record, size_t &Slot, TypeIdCompatibleVtableInfo &TypeId) { @@ -5799,6 +5933,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { PendingTypeCheckedLoadVCalls; std::vector<FunctionSummary::ConstVCall> PendingTypeTestAssumeConstVCalls, PendingTypeCheckedLoadConstVCalls; + std::vector<FunctionSummary::ParamAccess> PendingParamAccesses; while (true) { Expected<BitstreamEntry> MaybeEntry = Stream.advanceSkippingSubblocks(); @@ -5832,35 +5967,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { default: // Default behavior: ignore. break; case bitc::FS_FLAGS: { // [flags] - uint64_t Flags = Record[0]; - // Scan flags. - assert(Flags <= 0x3f && "Unexpected bits in flag"); - - // 1 bit: WithGlobalValueDeadStripping flag. - // Set on combined index only. - if (Flags & 0x1) - TheIndex.setWithGlobalValueDeadStripping(); - // 1 bit: SkipModuleByDistributedBackend flag. - // Set on combined index only. - if (Flags & 0x2) - TheIndex.setSkipModuleByDistributedBackend(); - // 1 bit: HasSyntheticEntryCounts flag. - // Set on combined index only. - if (Flags & 0x4) - TheIndex.setHasSyntheticEntryCounts(); - // 1 bit: DisableSplitLTOUnit flag. - // Set on per module indexes. It is up to the client to validate - // the consistency of this flag across modules being linked. - if (Flags & 0x8) - TheIndex.setEnableSplitLTOUnit(); - // 1 bit: PartiallySplitLTOUnits flag. - // Set on combined index only. - if (Flags & 0x10) - TheIndex.setPartiallySplitLTOUnits(); - // 1 bit: WithAttributePropagation flag. - // Set on combined index only. - if (Flags & 0x20) - TheIndex.setWithAttributePropagation(); + TheIndex.setFlags(Record[0]); break; } case bitc::FS_VALUE_GUID: { // [valueid, refguid] @@ -5925,7 +6032,8 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), - std::move(PendingTypeCheckedLoadConstVCalls)); + std::move(PendingTypeCheckedLoadConstVCalls), + std::move(PendingParamAccesses)); auto VIAndOriginalGUID = getValueInfoFromValueId(ValueID); FS->setModulePath(getThisModule()->first()); FS->setOriginalName(VIAndOriginalGUID.second); @@ -5965,7 +6073,9 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { uint64_t RawFlags = Record[1]; unsigned RefArrayStart = 2; GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, - /* WriteOnly */ false); + /* WriteOnly */ false, + /* Constant */ false, + GlobalObject::VCallVisibilityPublic); auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); if (Version >= 5) { GVF = getDecodedGVarFlags(Record[2]); @@ -6065,7 +6175,8 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { std::move(PendingTypeTestAssumeVCalls), std::move(PendingTypeCheckedLoadVCalls), std::move(PendingTypeTestAssumeConstVCalls), - std::move(PendingTypeCheckedLoadConstVCalls)); + std::move(PendingTypeCheckedLoadConstVCalls), + std::move(PendingParamAccesses)); LastSeenSummary = FS.get(); LastSeenGUID = VI.getGUID(); FS->setModulePath(ModuleIdMap[ModuleId]); @@ -6101,7 +6212,9 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { uint64_t RawFlags = Record[2]; unsigned RefArrayStart = 3; GlobalVarSummary::GVarFlags GVF(/* ReadOnly */ false, - /* WriteOnly */ false); + /* WriteOnly */ false, + /* Constant */ false, + GlobalObject::VCallVisibilityPublic); auto Flags = getDecodedGVSummaryFlags(RawFlags, Version); if (Version >= 5) { GVF = getDecodedGVarFlags(Record[3]); @@ -6181,6 +6294,15 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) { case bitc::FS_TYPE_ID_METADATA: parseTypeIdCompatibleVtableSummaryRecord(Record); break; + + case bitc::FS_BLOCK_COUNT: + TheIndex.addBlockCount(Record[0]); + break; + + case bitc::FS_PARAM_ACCESS: { + PendingParamAccesses = parseParamAccesses(Record); + break; + } } } llvm_unreachable("Exit infinite loop"); @@ -6452,7 +6574,8 @@ llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { /// everything. Expected<std::unique_ptr<Module>> BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, - bool ShouldLazyLoadMetadata, bool IsImporting) { + bool ShouldLazyLoadMetadata, bool IsImporting, + DataLayoutCallbackTy DataLayoutCallback) { BitstreamCursor Stream(Buffer); std::string ProducerIdentification; @@ -6477,8 +6600,8 @@ BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, M->setMaterializer(R); // Delay parsing Metadata if ShouldLazyLoadMetadata is true. - if (Error Err = - R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, IsImporting)) + if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata, + IsImporting, DataLayoutCallback)) return std::move(Err); if (MaterializeAll) { @@ -6496,7 +6619,8 @@ BitcodeModule::getModuleImpl(LLVMContext &Context, bool MaterializeAll, Expected<std::unique_ptr<Module>> BitcodeModule::getLazyModule(LLVMContext &Context, bool ShouldLazyLoadMetadata, bool IsImporting) { - return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting); + return getModuleImpl(Context, false, ShouldLazyLoadMetadata, IsImporting, + [](StringRef) { return None; }); } // Parse the specified bitcode buffer and merge the index into CombinedIndex. @@ -6662,19 +6786,21 @@ Expected<std::unique_ptr<Module>> llvm::getOwningLazyBitcodeModule( } Expected<std::unique_ptr<Module>> -BitcodeModule::parseModule(LLVMContext &Context) { - return getModuleImpl(Context, true, false, false); +BitcodeModule::parseModule(LLVMContext &Context, + DataLayoutCallbackTy DataLayoutCallback) { + return getModuleImpl(Context, true, false, false, DataLayoutCallback); // TODO: Restore the use-lists to the in-memory state when the bitcode was // written. We must defer until the Module has been fully materialized. } -Expected<std::unique_ptr<Module>> llvm::parseBitcodeFile(MemoryBufferRef Buffer, - LLVMContext &Context) { +Expected<std::unique_ptr<Module>> +llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, + DataLayoutCallbackTy DataLayoutCallback) { Expected<BitcodeModule> BM = getSingleModule(Buffer); if (!BM) return BM.takeError(); - return BM->parseModule(Context); + return BM->parseModule(Context, DataLayoutCallback); } Expected<std::string> llvm::getBitcodeTargetTriple(MemoryBufferRef Buffer) { |