LLVM 20.0.0git
CodeGenDataWriter.cpp
Go to the documentation of this file.
1//===- CodeGenDataWriter.cpp ----------------------------------------------===//
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
14
15#define DEBUG_TYPE "cg-data-writer"
16
17using namespace llvm;
18
20 using namespace support;
21
22 if (IsFDOStream) {
23 raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
24 const uint64_t LastPos = FDOStream.tell();
25 for (const auto &K : P) {
26 FDOStream.seek(K.Pos);
27 for (int I = 0; I < K.N; I++)
28 write(K.D[I]);
29 }
30 // Reset the stream to the last position after patching so that users
31 // don't accidentally overwrite data. This makes it consistent with
32 // the string stream below which replaces the data directly.
33 FDOStream.seek(LastPos);
34 } else {
35 raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
36 std::string &Data = SOStream.str(); // with flush
37 for (const auto &K : P) {
38 for (int I = 0; I < K.N; I++) {
39 uint64_t Bytes =
40 endian::byte_swap<uint64_t, llvm::endianness::little>(K.D[I]);
41 Data.replace(K.Pos + I * sizeof(uint64_t), sizeof(uint64_t),
42 reinterpret_cast<const char *>(&Bytes), sizeof(uint64_t));
43 }
44 }
45 }
46}
47
49 assert(Record.HashTree && "empty hash tree in the record");
50 HashTreeRecord.HashTree = std::move(Record.HashTree);
51
53}
54
56 CGDataOStream COS(OS);
57 return writeImpl(COS);
58}
59
60Error CodeGenDataWriter::writeHeader(CGDataOStream &COS) {
61 using namespace support;
63 Header.Magic = IndexedCGData::Magic;
64 Header.Version = IndexedCGData::Version;
65
66 // Set the CGDataKind depending on the kind.
67 Header.DataKind = 0;
68 if (static_cast<bool>(DataKind & CGDataKind::FunctionOutlinedHashTree))
69 Header.DataKind |=
71
72 Header.OutlinedHashTreeOffset = 0;
73
74 // Only write up to the CGDataKind. We need to remember the offset of the
75 // remaining fields to allow back-patching later.
76 COS.write(Header.Magic);
77 COS.write32(Header.Version);
78 COS.write32(Header.DataKind);
79
80 // Save the location of Header.OutlinedHashTreeOffset field in \c COS.
81 OutlinedHashTreeOffset = COS.tell();
82
83 // Reserve the space for OutlinedHashTreeOffset field.
84 COS.write(0);
85
86 return Error::success();
87}
88
89Error CodeGenDataWriter::writeImpl(CGDataOStream &COS) {
90 if (Error E = writeHeader(COS))
91 return E;
92
93 uint64_t OutlinedHashTreeFieldStart = COS.tell();
95 HashTreeRecord.serialize(COS.OS);
96
97 // Back patch the offsets.
98 CGDataPatchItem PatchItems[] = {
99 {OutlinedHashTreeOffset, &OutlinedHashTreeFieldStart, 1}};
100 COS.patch(PatchItems);
101
102 return Error::success();
103}
104
105Error CodeGenDataWriter::writeHeaderText(raw_fd_ostream &OS) {
107 OS << "# Outlined stable hash tree\n:outlined_hash_tree\n";
108
109 // TODO: Add more data types in this header
110
111 return Error::success();
112}
113
115 if (Error E = writeHeaderText(OS))
116 return E;
117
118 yaml::Output YOS(OS);
120 HashTreeRecord.serializeYAML(YOS);
121
122 // TODO: Write more yaml cgdata in order
123
124 return Error::success();
125}
#define I(x, y, z)
Definition: MD5.cpp:58
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
A wrapper class to abstract writer stream with support of bytes back patching.
void patch(ArrayRef< CGDataPatchItem > P)
void write32(uint32_t V)
void write(uint64_t V)
Error writeText(raw_fd_ostream &OS)
Write the codegen data in text format to OS.
Error write(raw_fd_ostream &OS)
Write the codegen data to OS.
bool hasOutlinedHashTree() const
Return true if the header indicates the data has an outlined hash tree.
void addRecord(OutlinedHashTreeRecord &Record)
Add the outlined hash tree record. The input Record is released.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:460
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:147
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:679
const uint64_t Version
Definition: CodeGenData.h:184
const uint64_t Magic
Definition: CodeGenData.h:176
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A struct to define how the data stream should be patched.
void serializeYAML(yaml::Output &YOS) const
Serialize the outlined hash tree to a YAML stream.
void serialize(raw_ostream &OS) const
Serialize the outlined hash tree to a raw_ostream.
std::unique_ptr< OutlinedHashTree > HashTree