diff options
Diffstat (limited to 'llvm/lib')
| -rw-r--r-- | llvm/lib/CGData/CodeGenData.cpp | 30 | ||||
| -rw-r--r-- | llvm/lib/CGData/CodeGenDataReader.cpp | 63 | ||||
| -rw-r--r-- | llvm/lib/CGData/CodeGenDataWriter.cpp | 30 |
3 files changed, 89 insertions, 34 deletions
diff --git a/llvm/lib/CGData/CodeGenData.cpp b/llvm/lib/CGData/CodeGenData.cpp index 2a3a74c8bc37..88dcdfd1f931 100644 --- a/llvm/lib/CGData/CodeGenData.cpp +++ b/llvm/lib/CGData/CodeGenData.cpp @@ -14,6 +14,7 @@ #include "llvm/Bitcode/BitcodeWriter.h" #include "llvm/CGData/CodeGenDataReader.h" #include "llvm/CGData/OutlinedHashTreeRecord.h" +#include "llvm/CGData/StableFunctionMapRecord.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Caching.h" #include "llvm/Support/CommandLine.h" @@ -163,6 +164,8 @@ CodeGenData &CodeGenData::getInstance() { auto Reader = ReaderOrErr->get(); if (Reader->hasOutlinedHashTree()) Instance->publishOutlinedHashTree(Reader->releaseOutlinedHashTree()); + if (Reader->hasStableFunctionMap()) + Instance->publishStableFunctionMap(Reader->releaseStableFunctionMap()); } }); return *Instance; @@ -185,18 +188,14 @@ Expected<Header> Header::readFromBuffer(const unsigned char *Curr) { return make_error<CGDataError>(cgdata_error::unsupported_version); H.DataKind = endian::readNext<uint32_t, endianness::little, unaligned>(Curr); - switch (H.Version) { - // When a new field is added to the header add a case statement here to - // compute the size as offset of the new field + size of the new field. This - // relies on the field being added to the end of the list. - static_assert(IndexedCGData::CGDataVersion::CurrentVersion == Version1, - "Please update the size computation below if a new field has " - "been added to the header, if not add a case statement to " - "fall through to the latest version."); - case 1ull: - H.OutlinedHashTreeOffset = + static_assert(IndexedCGData::CGDataVersion::CurrentVersion == Version2, + "Please update the offset computation below if a new field has " + "been added to the header."); + H.OutlinedHashTreeOffset = + endian::readNext<uint64_t, endianness::little, unaligned>(Curr); + if (H.Version >= 2) + H.StableFunctionMapOffset = endian::readNext<uint64_t, endianness::little, unaligned>(Curr); - } return H; } @@ -257,6 +256,7 @@ std::unique_ptr<Module> loadModuleForTwoRounds(BitcodeModule &OrigModule, Expected<stable_hash> mergeCodeGenData(ArrayRef<StringRef> ObjFiles) { OutlinedHashTreeRecord GlobalOutlineRecord; + StableFunctionMapRecord GlobalStableFunctionMapRecord; stable_hash CombinedHash = 0; for (auto File : ObjFiles) { if (File.empty()) @@ -270,12 +270,18 @@ Expected<stable_hash> mergeCodeGenData(ArrayRef<StringRef> ObjFiles) { std::unique_ptr<object::ObjectFile> &Obj = BinOrErr.get(); if (auto E = CodeGenDataReader::mergeFromObjectFile( - Obj.get(), GlobalOutlineRecord, &CombinedHash)) + Obj.get(), GlobalOutlineRecord, GlobalStableFunctionMapRecord, + &CombinedHash)) return E; } + GlobalStableFunctionMapRecord.finalize(); + if (!GlobalOutlineRecord.empty()) cgdata::publishOutlinedHashTree(std::move(GlobalOutlineRecord.HashTree)); + if (!GlobalStableFunctionMapRecord.empty()) + cgdata::publishStableFunctionMap( + std::move(GlobalStableFunctionMapRecord.FunctionMap)); return CombinedHash; } diff --git a/llvm/lib/CGData/CodeGenDataReader.cpp b/llvm/lib/CGData/CodeGenDataReader.cpp index 2f2481ea60f8..ebeb4ae36f99 100644 --- a/llvm/lib/CGData/CodeGenDataReader.cpp +++ b/llvm/lib/CGData/CodeGenDataReader.cpp @@ -32,10 +32,40 @@ setupMemoryBuffer(const Twine &Filename, vfs::FileSystem &FS) { Error CodeGenDataReader::mergeFromObjectFile( const object::ObjectFile *Obj, OutlinedHashTreeRecord &GlobalOutlineRecord, + StableFunctionMapRecord &GlobalFunctionMapRecord, stable_hash *CombinedHash) { Triple TT = Obj->makeTriple(); auto CGOutLineName = getCodeGenDataSectionName(CG_outline, TT.getObjectFormat(), false); + auto CGMergeName = + getCodeGenDataSectionName(CG_merge, TT.getObjectFormat(), false); + + auto processSectionContents = [&](const StringRef &Name, + const StringRef &Contents) { + if (Name != CGOutLineName && Name != CGMergeName) + return; + if (CombinedHash) + *CombinedHash = stable_hash_combine(*CombinedHash, xxh3_64bits(Contents)); + auto *Data = reinterpret_cast<const unsigned char *>(Contents.data()); + auto *EndData = Data + Contents.size(); + // In case dealing with an executable that has concatenated cgdata, + // we want to merge them into a single cgdata. + // Although it's not a typical workflow, we support this scenario + // by looping over all data in the sections. + if (Name == CGOutLineName) { + while (Data != EndData) { + OutlinedHashTreeRecord LocalOutlineRecord; + LocalOutlineRecord.deserialize(Data); + GlobalOutlineRecord.merge(LocalOutlineRecord); + } + } else if (Name == CGMergeName) { + while (Data != EndData) { + StableFunctionMapRecord LocalFunctionMapRecord; + LocalFunctionMapRecord.deserialize(Data); + GlobalFunctionMapRecord.merge(LocalFunctionMapRecord); + } + } + }; for (auto &Section : Obj->sections()) { Expected<StringRef> NameOrErr = Section.getName(); @@ -44,23 +74,7 @@ Error CodeGenDataReader::mergeFromObjectFile( Expected<StringRef> ContentsOrErr = Section.getContents(); if (!ContentsOrErr) return ContentsOrErr.takeError(); - auto *Data = reinterpret_cast<const unsigned char *>(ContentsOrErr->data()); - auto *EndData = Data + ContentsOrErr->size(); - - if (*NameOrErr == CGOutLineName) { - if (CombinedHash) - *CombinedHash = - stable_hash_combine(*CombinedHash, xxh3_64bits(*ContentsOrErr)); - // In case dealing with an executable that has concatenated cgdata, - // we want to merge them into a single cgdata. - // Although it's not a typical workflow, we support this scenario. - while (Data != EndData) { - OutlinedHashTreeRecord LocalOutlineRecord; - LocalOutlineRecord.deserialize(Data); - GlobalOutlineRecord.merge(LocalOutlineRecord); - } - } - // TODO: Add support for other cgdata sections. + processSectionContents(*NameOrErr, *ContentsOrErr); } return Error::success(); @@ -69,7 +83,8 @@ Error CodeGenDataReader::mergeFromObjectFile( Error IndexedCodeGenDataReader::read() { using namespace support; - // The smallest header with the version 1 is 24 bytes + // The smallest header with the version 1 is 24 bytes. + // Do not update this value even with the new version of the header. const unsigned MinHeaderSize = 24; if (DataBuffer->getBufferSize() < MinHeaderSize) return error(cgdata_error::bad_header); @@ -87,6 +102,12 @@ Error IndexedCodeGenDataReader::read() { return error(cgdata_error::eof); HashTreeRecord.deserialize(Ptr); } + if (hasStableFunctionMap()) { + const unsigned char *Ptr = Start + Header.StableFunctionMapOffset; + if (Ptr >= End) + return error(cgdata_error::eof); + FunctionMapRecord.deserialize(Ptr); + } return success(); } @@ -152,6 +173,8 @@ Error TextCodeGenDataReader::read() { StringRef Str = Line->drop_front().rtrim(); if (Str.equals_insensitive("outlined_hash_tree")) DataKind |= CGDataKind::FunctionOutlinedHashTree; + else if (Str.equals_insensitive("stable_function_map")) + DataKind |= CGDataKind::StableFunctionMergingMap; else return error(cgdata_error::bad_header); } @@ -170,8 +193,8 @@ Error TextCodeGenDataReader::read() { yaml::Input YOS(StringRef(Pos, Size)); if (hasOutlinedHashTree()) HashTreeRecord.deserializeYAML(YOS); - - // TODO: Add more yaml cgdata in order + if (hasStableFunctionMap()) + FunctionMapRecord.deserializeYAML(YOS); return Error::success(); } diff --git a/llvm/lib/CGData/CodeGenDataWriter.cpp b/llvm/lib/CGData/CodeGenDataWriter.cpp index 5f638be0fefe..3a392036198a 100644 --- a/llvm/lib/CGData/CodeGenDataWriter.cpp +++ b/llvm/lib/CGData/CodeGenDataWriter.cpp @@ -52,6 +52,13 @@ void CodeGenDataWriter::addRecord(OutlinedHashTreeRecord &Record) { DataKind |= CGDataKind::FunctionOutlinedHashTree; } +void CodeGenDataWriter::addRecord(StableFunctionMapRecord &Record) { + assert(Record.FunctionMap && "empty function map in the record"); + FunctionMapRecord.FunctionMap = std::move(Record.FunctionMap); + + DataKind |= CGDataKind::StableFunctionMergingMap; +} + Error CodeGenDataWriter::write(raw_fd_ostream &OS) { CGDataOStream COS(OS); return writeImpl(COS); @@ -68,8 +75,11 @@ Error CodeGenDataWriter::writeHeader(CGDataOStream &COS) { if (static_cast<bool>(DataKind & CGDataKind::FunctionOutlinedHashTree)) Header.DataKind |= static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree); - + if (static_cast<bool>(DataKind & CGDataKind::StableFunctionMergingMap)) + Header.DataKind |= + static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap); Header.OutlinedHashTreeOffset = 0; + Header.StableFunctionMapOffset = 0; // Only write up to the CGDataKind. We need to remember the offset of the // remaining fields to allow back-patching later. @@ -83,6 +93,12 @@ Error CodeGenDataWriter::writeHeader(CGDataOStream &COS) { // Reserve the space for OutlinedHashTreeOffset field. COS.write(0); + // Save the location of Header.StableFunctionMapOffset field in \c COS. + StableFunctionMapOffset = COS.tell(); + + // Reserve the space for StableFunctionMapOffset field. + COS.write(0); + return Error::success(); } @@ -93,10 +109,14 @@ Error CodeGenDataWriter::writeImpl(CGDataOStream &COS) { uint64_t OutlinedHashTreeFieldStart = COS.tell(); if (hasOutlinedHashTree()) HashTreeRecord.serialize(COS.OS); + uint64_t StableFunctionMapFieldStart = COS.tell(); + if (hasStableFunctionMap()) + FunctionMapRecord.serialize(COS.OS); // Back patch the offsets. CGDataPatchItem PatchItems[] = { - {OutlinedHashTreeOffset, &OutlinedHashTreeFieldStart, 1}}; + {OutlinedHashTreeOffset, &OutlinedHashTreeFieldStart, 1}, + {StableFunctionMapOffset, &StableFunctionMapFieldStart, 1}}; COS.patch(PatchItems); return Error::success(); @@ -106,6 +126,9 @@ Error CodeGenDataWriter::writeHeaderText(raw_fd_ostream &OS) { if (hasOutlinedHashTree()) OS << "# Outlined stable hash tree\n:outlined_hash_tree\n"; + if (hasStableFunctionMap()) + OS << "# Stable function map\n:stable_function_map\n"; + // TODO: Add more data types in this header return Error::success(); @@ -119,6 +142,9 @@ Error CodeGenDataWriter::writeText(raw_fd_ostream &OS) { if (hasOutlinedHashTree()) HashTreeRecord.serializeYAML(YOS); + if (hasStableFunctionMap()) + FunctionMapRecord.serializeYAML(YOS); + // TODO: Write more yaml cgdata in order return Error::success(); |
