diff options
| author | Kyungwoo Lee <kyulee@meta.com> | 2024-09-09 19:38:05 -0700 |
|---|---|---|
| committer | Kyungwoo Lee <kyulee@meta.com> | 2024-10-27 00:13:00 -0700 |
| commit | c7913f9fff736da4cc6a78a17e41dc539bc75e8a (patch) | |
| tree | b23e15800d3e8119b4385dfe95896fccba57623e /llvm/lib/CGData/CodeGenDataReader.cpp | |
| parent | 8e10ed3b27b0f0098782171bb38387e86536be5f (diff) | |
| download | llvm-upstream/users/kyulee-com/cgdata.tar.gz llvm-upstream/users/kyulee-com/cgdata.tar.bz2 llvm-upstream/users/kyulee-com/cgdata.zip | |
[CGData][llvm-cgdata] Support for stable function mapupstream/users/kyulee-com/cgdata
This introduces a new cgdata format for stable function maps.
The raw data is embedded in the __llvm_merge section during compile time.
This data can be read and merged using the llvm-cgdata tool, into an indexed cgdata file. Consequently, the tool is now capable of handling either outlined hash trees, stable function maps, or both, as they are orthogonal.
Diffstat (limited to 'llvm/lib/CGData/CodeGenDataReader.cpp')
| -rw-r--r-- | llvm/lib/CGData/CodeGenDataReader.cpp | 63 |
1 files changed, 43 insertions, 20 deletions
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(); } |
