Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- CodeGenDataWriter.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 writing codegen data.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CGDATA_CODEGENDATAWRITER_H
0014 #define LLVM_CGDATA_CODEGENDATAWRITER_H
0015 
0016 #include "llvm/CGData/CodeGenData.h"
0017 #include "llvm/CGData/OutlinedHashTreeRecord.h"
0018 #include "llvm/CGData/StableFunctionMapRecord.h"
0019 #include "llvm/Support/EndianStream.h"
0020 #include "llvm/Support/Error.h"
0021 
0022 namespace llvm {
0023 
0024 /// A struct to define how the data stream should be patched.
0025 struct CGDataPatchItem {
0026   uint64_t Pos; // Where to patch.
0027   uint64_t *D;  // Pointer to an array of source data.
0028   int N;        // Number of elements in \c D array.
0029 };
0030 
0031 /// A wrapper class to abstract writer stream with support of bytes
0032 /// back patching.
0033 class CGDataOStream {
0034 public:
0035   CGDataOStream(raw_fd_ostream &FD)
0036       : IsFDOStream(true), OS(FD), LE(FD, llvm::endianness::little) {}
0037   CGDataOStream(raw_string_ostream &STR)
0038       : IsFDOStream(false), OS(STR), LE(STR, llvm::endianness::little) {}
0039 
0040   uint64_t tell() { return OS.tell(); }
0041   void write(uint64_t V) { LE.write<uint64_t>(V); }
0042   void write32(uint32_t V) { LE.write<uint32_t>(V); }
0043   void write8(uint8_t V) { LE.write<uint8_t>(V); }
0044 
0045   // \c patch can only be called when all data is written and flushed.
0046   // For raw_string_ostream, the patch is done on the target string
0047   // directly and it won't be reflected in the stream's internal buffer.
0048   void patch(ArrayRef<CGDataPatchItem> P);
0049 
0050   // If \c OS is an instance of \c raw_fd_ostream, this field will be
0051   // true. Otherwise, \c OS will be an raw_string_ostream.
0052   bool IsFDOStream;
0053   raw_ostream &OS;
0054   support::endian::Writer LE;
0055 };
0056 
0057 class CodeGenDataWriter {
0058   /// The outlined hash tree to be written.
0059   OutlinedHashTreeRecord HashTreeRecord;
0060 
0061   /// The stable function map to be written.
0062   StableFunctionMapRecord FunctionMapRecord;
0063 
0064   /// A bit mask describing the kind of the codegen data.
0065   CGDataKind DataKind = CGDataKind::Unknown;
0066 
0067 public:
0068   CodeGenDataWriter() = default;
0069   ~CodeGenDataWriter() = default;
0070 
0071   /// Add the outlined hash tree record. The input hash tree is released.
0072   void addRecord(OutlinedHashTreeRecord &Record);
0073 
0074   /// Add the stable function map record. The input function map is released.
0075   void addRecord(StableFunctionMapRecord &Record);
0076 
0077   /// Write the codegen data to \c OS
0078   Error write(raw_fd_ostream &OS);
0079 
0080   /// Write the codegen data in text format to \c OS
0081   Error writeText(raw_fd_ostream &OS);
0082 
0083   /// Return the attributes of the current CGData.
0084   CGDataKind getCGDataKind() const { return DataKind; }
0085 
0086   /// Return true if the header indicates the data has an outlined hash tree.
0087   bool hasOutlinedHashTree() const {
0088     return static_cast<uint32_t>(DataKind) &
0089            static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree);
0090   }
0091   /// Return true if the header indicates the data has a stable function map.
0092   bool hasStableFunctionMap() const {
0093     return static_cast<uint32_t>(DataKind) &
0094            static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap);
0095   }
0096 
0097 private:
0098   /// The offset of the outlined hash tree in the file.
0099   uint64_t OutlinedHashTreeOffset;
0100 
0101   /// The offset of the stable function map in the file.
0102   uint64_t StableFunctionMapOffset;
0103 
0104   /// Write the codegen data header to \c COS
0105   Error writeHeader(CGDataOStream &COS);
0106 
0107   /// Write the codegen data header in text to \c OS
0108   Error writeHeaderText(raw_fd_ostream &OS);
0109 
0110   Error writeImpl(CGDataOStream &COS);
0111 };
0112 
0113 } // end namespace llvm
0114 
0115 #endif // LLVM_CGDATA_CODEGENDATAWRITER_H