File indexing completed on 2026-05-10 08:43:23
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_CGDATA_CODEGENDATAREADER_H
0014 #define LLVM_CGDATA_CODEGENDATAREADER_H
0015
0016 #include "llvm/CGData/CodeGenData.h"
0017 #include "llvm/CGData/OutlinedHashTreeRecord.h"
0018 #include "llvm/CGData/StableFunctionMapRecord.h"
0019 #include "llvm/Support/LineIterator.h"
0020 #include "llvm/Support/VirtualFileSystem.h"
0021
0022 namespace llvm {
0023
0024 class CodeGenDataReader {
0025 cgdata_error LastError = cgdata_error::success;
0026 std::string LastErrorMsg;
0027
0028 public:
0029 CodeGenDataReader() = default;
0030 virtual ~CodeGenDataReader() = default;
0031
0032
0033 virtual Error read() = 0;
0034
0035 virtual uint32_t getVersion() const = 0;
0036
0037 virtual CGDataKind getDataKind() const = 0;
0038
0039 virtual bool hasOutlinedHashTree() const = 0;
0040
0041 virtual bool hasStableFunctionMap() const = 0;
0042
0043 std::unique_ptr<OutlinedHashTree> releaseOutlinedHashTree() {
0044 return std::move(HashTreeRecord.HashTree);
0045 }
0046 std::unique_ptr<StableFunctionMap> releaseStableFunctionMap() {
0047 return std::move(FunctionMapRecord.FunctionMap);
0048 }
0049
0050
0051
0052 static Expected<std::unique_ptr<CodeGenDataReader>>
0053 create(const Twine &Path, vfs::FileSystem &FS);
0054
0055
0056
0057 static Expected<std::unique_ptr<CodeGenDataReader>>
0058 create(std::unique_ptr<MemoryBuffer> Buffer);
0059
0060
0061
0062
0063
0064
0065 static Error
0066 mergeFromObjectFile(const object::ObjectFile *Obj,
0067 OutlinedHashTreeRecord &GlobalOutlineRecord,
0068 StableFunctionMapRecord &GlobalFunctionMapRecord,
0069 stable_hash *CombinedHash = nullptr);
0070
0071 protected:
0072
0073
0074 OutlinedHashTreeRecord HashTreeRecord;
0075
0076
0077
0078 StableFunctionMapRecord FunctionMapRecord;
0079
0080
0081 Error error(cgdata_error Err, const std::string &ErrMsg = "") {
0082 LastError = Err;
0083 LastErrorMsg = ErrMsg;
0084 if (Err == cgdata_error::success)
0085 return Error::success();
0086 return make_error<CGDataError>(Err, ErrMsg);
0087 }
0088
0089 Error error(Error &&E) {
0090 handleAllErrors(std::move(E), [&](const CGDataError &IPE) {
0091 LastError = IPE.get();
0092 LastErrorMsg = IPE.getMessage();
0093 });
0094 return make_error<CGDataError>(LastError, LastErrorMsg);
0095 }
0096
0097
0098 Error success() { return error(cgdata_error::success); }
0099 };
0100
0101 class IndexedCodeGenDataReader : public CodeGenDataReader {
0102
0103 std::unique_ptr<MemoryBuffer> DataBuffer;
0104
0105 IndexedCGData::Header Header;
0106
0107 public:
0108 IndexedCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer)
0109 : DataBuffer(std::move(DataBuffer)) {}
0110 IndexedCodeGenDataReader(const IndexedCodeGenDataReader &) = delete;
0111 IndexedCodeGenDataReader &
0112 operator=(const IndexedCodeGenDataReader &) = delete;
0113
0114
0115 static bool hasFormat(const MemoryBuffer &Buffer);
0116
0117 Error read() override;
0118
0119 uint32_t getVersion() const override { return Header.Version; }
0120
0121 CGDataKind getDataKind() const override {
0122 return static_cast<CGDataKind>(Header.DataKind);
0123 }
0124
0125
0126 bool hasOutlinedHashTree() const override {
0127 return Header.DataKind &
0128 static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
0129 }
0130
0131 bool hasStableFunctionMap() const override {
0132 return Header.DataKind &
0133 static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
0134 }
0135 };
0136
0137
0138
0139
0140
0141
0142 class TextCodeGenDataReader : public CodeGenDataReader {
0143
0144 std::unique_ptr<MemoryBuffer> DataBuffer;
0145
0146 line_iterator Line;
0147
0148 CGDataKind DataKind = CGDataKind::Unknown;
0149
0150 public:
0151 TextCodeGenDataReader(std::unique_ptr<MemoryBuffer> DataBuffer_)
0152 : DataBuffer(std::move(DataBuffer_)), Line(*DataBuffer, true, '#') {}
0153 TextCodeGenDataReader(const TextCodeGenDataReader &) = delete;
0154 TextCodeGenDataReader &operator=(const TextCodeGenDataReader &) = delete;
0155
0156
0157 static bool hasFormat(const MemoryBuffer &Buffer);
0158
0159 Error read() override;
0160
0161 uint32_t getVersion() const override { return 0; }
0162
0163 CGDataKind getDataKind() const override { return DataKind; }
0164
0165
0166 bool hasOutlinedHashTree() const override {
0167 return static_cast<uint32_t>(DataKind) &
0168 static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
0169 }
0170
0171
0172 bool hasStableFunctionMap() const override {
0173 return static_cast<uint32_t>(DataKind) &
0174 static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
0175 }
0176 };
0177
0178 }
0179
0180 #endif