1 //===- CodeGenDataWriter.h --------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains support for writing codegen data. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CGDATA_CODEGENDATAWRITER_H 14 #define LLVM_CGDATA_CODEGENDATAWRITER_H 15 16 #include "llvm/CGData/CGDataPatchItem.h" 17 #include "llvm/CGData/CodeGenData.h" 18 #include "llvm/CGData/OutlinedHashTreeRecord.h" 19 #include "llvm/CGData/StableFunctionMapRecord.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm/Support/EndianStream.h" 22 #include "llvm/Support/Error.h" 23 24 namespace llvm { 25 26 /// A wrapper class to abstract writer stream with support of bytes 27 /// back patching. 28 class CGDataOStream { 29 enum class OStreamKind { 30 fd, 31 string, 32 svector, 33 }; 34 35 public: CGDataOStream(raw_fd_ostream & FD)36 CGDataOStream(raw_fd_ostream &FD) 37 : Kind(OStreamKind::fd), OS(FD), LE(FD, llvm::endianness::little) {} CGDataOStream(raw_string_ostream & STR)38 CGDataOStream(raw_string_ostream &STR) 39 : Kind(OStreamKind::string), OS(STR), LE(STR, llvm::endianness::little) {} CGDataOStream(raw_svector_ostream & SVEC)40 CGDataOStream(raw_svector_ostream &SVEC) 41 : Kind(OStreamKind::svector), OS(SVEC), 42 LE(SVEC, llvm::endianness::little) {} 43 tell()44 uint64_t tell() { return OS.tell(); } write(uint64_t V)45 void write(uint64_t V) { LE.write<uint64_t>(V); } write32(uint32_t V)46 void write32(uint32_t V) { LE.write<uint32_t>(V); } write8(uint8_t V)47 void write8(uint8_t V) { LE.write<uint8_t>(V); } 48 49 // \c patch can only be called when all data is written and flushed. 50 // For raw_string_ostream, the patch is done on the target string 51 // directly and it won't be reflected in the stream's internal buffer. 52 LLVM_ABI void patch(ArrayRef<CGDataPatchItem> P); 53 54 OStreamKind Kind; 55 raw_ostream &OS; 56 support::endian::Writer LE; 57 }; 58 59 class CodeGenDataWriter { 60 /// The outlined hash tree to be written. 61 OutlinedHashTreeRecord HashTreeRecord; 62 63 /// The stable function map to be written. 64 StableFunctionMapRecord FunctionMapRecord; 65 66 /// A bit mask describing the kind of the codegen data. 67 CGDataKind DataKind = CGDataKind::Unknown; 68 69 public: 70 CodeGenDataWriter() = default; 71 ~CodeGenDataWriter() = default; 72 73 /// Add the outlined hash tree record. The input hash tree is released. 74 LLVM_ABI void addRecord(OutlinedHashTreeRecord &Record); 75 76 /// Add the stable function map record. The input function map is released. 77 LLVM_ABI void addRecord(StableFunctionMapRecord &Record); 78 79 /// Write the codegen data to \c OS 80 LLVM_ABI Error write(raw_fd_ostream &OS); 81 82 /// Write the codegen data in text format to \c OS 83 LLVM_ABI Error writeText(raw_fd_ostream &OS); 84 85 /// Return the attributes of the current CGData. getCGDataKind()86 CGDataKind getCGDataKind() const { return DataKind; } 87 88 /// Return true if the header indicates the data has an outlined hash tree. hasOutlinedHashTree()89 bool hasOutlinedHashTree() const { 90 return static_cast<uint32_t>(DataKind) & 91 static_cast<uint32_t>(CGDataKind::FunctionOutlinedHashTree); 92 } 93 /// Return true if the header indicates the data has a stable function map. hasStableFunctionMap()94 bool hasStableFunctionMap() const { 95 return static_cast<uint32_t>(DataKind) & 96 static_cast<uint32_t>(CGDataKind::StableFunctionMergingMap); 97 } 98 99 private: 100 /// The offset of the outlined hash tree in the file. 101 uint64_t OutlinedHashTreeOffset; 102 103 /// The offset of the stable function map in the file. 104 uint64_t StableFunctionMapOffset; 105 106 /// Write the codegen data header to \c COS 107 Error writeHeader(CGDataOStream &COS); 108 109 /// Write the codegen data header in text to \c OS 110 Error writeHeaderText(raw_fd_ostream &OS); 111 112 Error writeImpl(CGDataOStream &COS); 113 }; 114 115 } // end namespace llvm 116 117 #endif // LLVM_CGDATA_CODEGENDATAWRITER_H 118