diff options
author | Eli Friedman <efriedma@quicinc.com> | 2020-05-14 12:59:45 -0700 |
---|---|---|
committer | Eli Friedman <efriedma@quicinc.com> | 2020-05-14 13:03:50 -0700 |
commit | 4532a50899b1748a64320abce7a69ac10b801a34 (patch) | |
tree | 4f55926f57022b0d8aa615b551ef6999148dbe7e /llvm/lib/Bitcode/Reader/BitcodeReader.cpp | |
parent | 2b920ae78c1d3fd36aeb7e77ca8de18a36b92344 (diff) | |
download | llvm-4532a50899b1748a64320abce7a69ac10b801a34.zip llvm-4532a50899b1748a64320abce7a69ac10b801a34.tar.gz llvm-4532a50899b1748a64320abce7a69ac10b801a34.tar.bz2 |
Infer alignment of unmarked loads in IR/bitcode parsing.
For IR generated by a compiler, this is really simple: you just take the
datalayout from the beginning of the file, and apply it to all the IR
later in the file. For optimization testcases that don't care about the
datalayout, this is also really simple: we just use the default
datalayout.
The complexity here comes from the fact that some LLVM tools allow
overriding the datalayout: some tools have an explicit flag for this,
some tools will infer a datalayout based on the code generation target.
Supporting this properly required plumbing through a bunch of new
machinery: we want to allow overriding the datalayout after the
datalayout is parsed from the file, but before we use any information
from it. Therefore, IR/bitcode parsing now has a callback to allow tools
to compute the datalayout at the appropriate time.
Not sure if I covered all the LLVM tools that want to use the callback.
(clang? lli? Misc IR manipulation tools like llvm-link?). But this is at
least enough for all the LLVM regression tests, and IR without a
datalayout is not something frontends should generate.
This change had some sort of weird effects for certain CodeGen
regression tests: if the datalayout is overridden with a datalayout with
a different program or stack address space, we now parse IR based on the
overridden datalayout, instead of the one written in the file (or the
default one, if none is specified). This broke a few AVR tests, and one
AMDGPU test.
Outside the CodeGen tests I mentioned, the test changes are all just
fixing CHECK lines and moving around datalayout lines in weird places.
Differential Revision: https://reviews.llvm.org/D78403
Diffstat (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp')
-rw-r--r-- | llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 55 |
1 files changed, 38 insertions, 17 deletions
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 9e5f0e0..bdc0fa7 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -577,8 +577,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); @@ -723,7 +726,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); @@ -3419,7 +3424,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; @@ -3442,6 +3448,10 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, 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. @@ -3695,11 +3705,12 @@ Error BitcodeReader::parseModule(uint64_t ResumeBit, } 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) { @@ -4832,7 +4843,11 @@ 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); + if (!Align && !Ty->isSized()) + 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; } @@ -4869,7 +4884,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; } @@ -6466,7 +6483,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; @@ -6491,8 +6509,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) { @@ -6510,7 +6528,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. @@ -6676,19 +6695,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) { |