Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:23

0001 //===- CodeGenDataReader.h --------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file contains support for reading codegen data.
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   /// Read the header.  Required before reading first record.
0033   virtual Error read() = 0;
0034   /// Return the codegen data version.
0035   virtual uint32_t getVersion() const = 0;
0036   /// Return the codegen data kind.
0037   virtual CGDataKind getDataKind() const = 0;
0038   /// Return true if the data has an outlined hash tree.
0039   virtual bool hasOutlinedHashTree() const = 0;
0040   /// Return true if the data has a stable function map.
0041   virtual bool hasStableFunctionMap() const = 0;
0042   /// Return the outlined hash tree that is released from the reader.
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   /// Factory method to create an appropriately typed reader for the given
0051   /// codegen data file path and file system.
0052   static Expected<std::unique_ptr<CodeGenDataReader>>
0053   create(const Twine &Path, vfs::FileSystem &FS);
0054 
0055   /// Factory method to create an appropriately typed reader for the given
0056   /// memory buffer.
0057   static Expected<std::unique_ptr<CodeGenDataReader>>
0058   create(std::unique_ptr<MemoryBuffer> Buffer);
0059 
0060   /// Extract the cgdata embedded in sections from the given object file and
0061   /// merge them into the GlobalOutlineRecord. This is a static helper that
0062   /// is used by `llvm-cgdata --merge` or ThinLTO's two-codegen rounds.
0063   /// Optionally, \p CombinedHash can be used to compuate the combined hash of
0064   /// the merged data.
0065   static Error
0066   mergeFromObjectFile(const object::ObjectFile *Obj,
0067                       OutlinedHashTreeRecord &GlobalOutlineRecord,
0068                       StableFunctionMapRecord &GlobalFunctionMapRecord,
0069                       stable_hash *CombinedHash = nullptr);
0070 
0071 protected:
0072   /// The outlined hash tree that has been read. When it's released by
0073   /// releaseOutlinedHashTree(), it's no longer valid.
0074   OutlinedHashTreeRecord HashTreeRecord;
0075 
0076   /// The stable function map that has been read. When it's released by
0077   // releaseStableFunctionMap(), it's no longer valid.
0078   StableFunctionMapRecord FunctionMapRecord;
0079 
0080   /// Set the current error and return same.
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   /// Clear the current error and return a successful one.
0098   Error success() { return error(cgdata_error::success); }
0099 };
0100 
0101 class IndexedCodeGenDataReader : public CodeGenDataReader {
0102   /// The codegen data file contents.
0103   std::unique_ptr<MemoryBuffer> DataBuffer;
0104   /// The header
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   /// Return true if the given buffer is in binary codegen data format.
0115   static bool hasFormat(const MemoryBuffer &Buffer);
0116   /// Read the contents including the header.
0117   Error read() override;
0118   /// Return the codegen data version.
0119   uint32_t getVersion() const override { return Header.Version; }
0120   /// Return the codegen data kind.
0121   CGDataKind getDataKind() const override {
0122     return static_cast<CGDataKind>(Header.DataKind);
0123   }
0124   /// Return true if the header indicates the data has an outlined hash tree.
0125   /// This does not mean that the data is still available.
0126   bool hasOutlinedHashTree() const override {
0127     return Header.DataKind &
0128            static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
0129   }
0130   /// Return true if the header indicates the data has a stable function map.
0131   bool hasStableFunctionMap() const override {
0132     return Header.DataKind &
0133            static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
0134   }
0135 };
0136 
0137 /// This format is a simple text format that's suitable for test data.
0138 /// The header is a custom format starting with `:` per line to indicate which
0139 /// codegen data is recorded. `#` is used to indicate a comment.
0140 /// The subsequent data is a YAML format per each codegen data in order.
0141 /// Currently, it only has a function outlined hash tree.
0142 class TextCodeGenDataReader : public CodeGenDataReader {
0143   /// The codegen data file contents.
0144   std::unique_ptr<MemoryBuffer> DataBuffer;
0145   /// Iterator over the profile data.
0146   line_iterator Line;
0147   /// Describe the kind of the codegen data.
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   /// Return true if the given buffer is in text codegen data format.
0157   static bool hasFormat(const MemoryBuffer &Buffer);
0158   /// Read the contents including the header.
0159   Error read() override;
0160   /// Text format does not have version, so return 0.
0161   uint32_t getVersion() const override { return 0; }
0162   /// Return the codegen data kind.
0163   CGDataKind getDataKind() const override { return DataKind; }
0164   /// Return true if the header indicates the data has an outlined hash tree.
0165   /// This does not mean that the data is still available.
0166   bool hasOutlinedHashTree() const override {
0167     return static_cast<uint32_t>(DataKind) &
0168            static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
0169   }
0170   /// Return true if the header indicates the data has a stable function map.
0171   /// This does not mean that the data is still available.
0172   bool hasStableFunctionMap() const override {
0173     return static_cast<uint32_t>(DataKind) &
0174            static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
0175   }
0176 };
0177 
0178 } // end namespace llvm
0179 
0180 #endif // LLVM_CGDATA_CODEGENDATAREADER_H