1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
//===- BitstreamRemarkSerializer.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides the implementation of the LLVM bitstream remark serializer
// using LLVM's bitstream writer.
//
//===----------------------------------------------------------------------===//
#include "llvm/Remarks/BitstreamRemarkSerializer.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/Remarks/Remark.h"
#include <cassert>
#include <optional>
using namespace llvm;
using namespace llvm::remarks;
BitstreamRemarkSerializerHelper::BitstreamRemarkSerializerHelper(
BitstreamRemarkContainerType ContainerType, raw_ostream &OS)
: Bitstream(OS), ContainerType(ContainerType) {}
static void setRecordName(unsigned RecordID, BitstreamWriter &Bitstream,
SmallVectorImpl<uint64_t> &R, StringRef Str) {
R.clear();
R.push_back(RecordID);
append_range(R, Str);
Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_SETRECORDNAME, R);
}
static void initBlock(unsigned BlockID, BitstreamWriter &Bitstream,
SmallVectorImpl<uint64_t> &R, StringRef Str) {
R.clear();
R.push_back(BlockID);
Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_SETBID, R);
R.clear();
append_range(R, Str);
Bitstream.EmitRecord(bitc::BLOCKINFO_CODE_BLOCKNAME, R);
}
void BitstreamRemarkSerializerHelper::setupMetaBlockInfo() {
// Setup the metadata block.
initBlock(META_BLOCK_ID, Bitstream, R, MetaBlockName);
// The container information.
setRecordName(RECORD_META_CONTAINER_INFO, Bitstream, R,
MetaContainerInfoName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_META_CONTAINER_INFO));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Type.
RecordMetaContainerInfoAbbrevID =
Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
}
void BitstreamRemarkSerializerHelper::setupMetaRemarkVersion() {
setRecordName(RECORD_META_REMARK_VERSION, Bitstream, R,
MetaRemarkVersionName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_META_REMARK_VERSION));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Version.
RecordMetaRemarkVersionAbbrevID =
Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
}
void BitstreamRemarkSerializerHelper::emitMetaRemarkVersion(
uint64_t RemarkVersion) {
// The remark version is emitted only if we emit remarks.
R.clear();
R.push_back(RECORD_META_REMARK_VERSION);
R.push_back(RemarkVersion);
Bitstream.EmitRecordWithAbbrev(RecordMetaRemarkVersionAbbrevID, R);
}
void BitstreamRemarkSerializerHelper::setupMetaStrTab() {
setRecordName(RECORD_META_STRTAB, Bitstream, R, MetaStrTabName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_META_STRTAB));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Raw table.
RecordMetaStrTabAbbrevID =
Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
}
void BitstreamRemarkSerializerHelper::emitMetaStrTab(
const StringTable &StrTab) {
// The string table is not emitted if we emit remarks separately.
R.clear();
R.push_back(RECORD_META_STRTAB);
// Serialize to a blob.
std::string Buf;
raw_string_ostream OS(Buf);
StrTab.serialize(OS);
StringRef Blob = OS.str();
Bitstream.EmitRecordWithBlob(RecordMetaStrTabAbbrevID, R, Blob);
}
void BitstreamRemarkSerializerHelper::setupMetaExternalFile() {
setRecordName(RECORD_META_EXTERNAL_FILE, Bitstream, R, MetaExternalFileName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_META_EXTERNAL_FILE));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Filename.
RecordMetaExternalFileAbbrevID =
Bitstream.EmitBlockInfoAbbrev(META_BLOCK_ID, Abbrev);
}
void BitstreamRemarkSerializerHelper::emitMetaExternalFile(StringRef Filename) {
// The external file is emitted only if we emit the separate metadata.
R.clear();
R.push_back(RECORD_META_EXTERNAL_FILE);
Bitstream.EmitRecordWithBlob(RecordMetaExternalFileAbbrevID, R, Filename);
}
void BitstreamRemarkSerializerHelper::setupRemarkBlockInfo() {
// Setup the remark block.
initBlock(REMARK_BLOCK_ID, Bitstream, R, RemarkBlockName);
// The header of a remark.
{
setRecordName(RECORD_REMARK_HEADER, Bitstream, R, RemarkHeaderName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_HEADER));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Type
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Remark Name
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Pass name
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Function name
RecordRemarkHeaderAbbrevID =
Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
}
// The location of a remark.
{
setRecordName(RECORD_REMARK_DEBUG_LOC, Bitstream, R, RemarkDebugLocName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_DEBUG_LOC));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
RecordRemarkDebugLocAbbrevID =
Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
}
// The hotness of a remark.
{
setRecordName(RECORD_REMARK_HOTNESS, Bitstream, R, RemarkHotnessName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_HOTNESS));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Hotness
RecordRemarkHotnessAbbrevID =
Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
}
// An argument entry with a debug location attached.
{
setRecordName(RECORD_REMARK_ARG_WITH_DEBUGLOC, Bitstream, R,
RemarkArgWithDebugLocName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_ARG_WITH_DEBUGLOC));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // File
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Line
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // Column
RecordRemarkArgWithDebugLocAbbrevID =
Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
}
// An argument entry with no debug location attached.
{
setRecordName(RECORD_REMARK_ARG_WITHOUT_DEBUGLOC, Bitstream, R,
RemarkArgWithoutDebugLocName);
auto Abbrev = std::make_shared<BitCodeAbbrev>();
Abbrev->Add(BitCodeAbbrevOp(RECORD_REMARK_ARG_WITHOUT_DEBUGLOC));
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Key
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 7)); // Value
RecordRemarkArgWithoutDebugLocAbbrevID =
Bitstream.EmitBlockInfoAbbrev(REMARK_BLOCK_ID, Abbrev);
}
}
void BitstreamRemarkSerializerHelper::setupBlockInfo() {
// Emit magic number.
for (const char C : ContainerMagic)
Bitstream.Emit(static_cast<unsigned>(C), 8);
Bitstream.EnterBlockInfoBlock();
auto ExitBlock = make_scope_exit([&] { Bitstream.ExitBlock(); });
// Setup the main metadata. Depending on the container type, we'll setup the
// required records next.
setupMetaBlockInfo();
switch (ContainerType) {
case BitstreamRemarkContainerType::RemarksFileExternal:
// Needs to know where the external remarks file is.
setupMetaExternalFile();
return;
case BitstreamRemarkContainerType::RemarksFile:
// Contains remarks: emit the version.
setupMetaRemarkVersion();
// Needs a string table.
setupMetaStrTab();
// Contains remarks: emit the remark abbrevs.
setupRemarkBlockInfo();
return;
}
llvm_unreachable("Unexpected BitstreamRemarkContainerType");
}
void BitstreamRemarkSerializerHelper::emitMetaBlock(
std::optional<StringRef> Filename) {
// Emit the meta block
Bitstream.EnterSubblock(META_BLOCK_ID, 3);
auto ExitBlock = make_scope_exit([&] { Bitstream.ExitBlock(); });
// The container version and type.
R.clear();
R.push_back(RECORD_META_CONTAINER_INFO);
R.push_back(CurrentContainerVersion);
R.push_back(static_cast<uint64_t>(ContainerType));
Bitstream.EmitRecordWithAbbrev(RecordMetaContainerInfoAbbrevID, R);
switch (ContainerType) {
case BitstreamRemarkContainerType::RemarksFileExternal:
assert(Filename != std::nullopt);
emitMetaExternalFile(*Filename);
return;
case BitstreamRemarkContainerType::RemarksFile:
emitMetaRemarkVersion(CurrentRemarkVersion);
return;
}
llvm_unreachable("Unexpected BitstreamRemarkContainerType");
}
void BitstreamRemarkSerializerHelper::emitLateMetaBlock(
const StringTable &StrTab) {
// Emit the late meta block (after all remarks are serialized)
Bitstream.EnterSubblock(META_BLOCK_ID, 3);
emitMetaStrTab(StrTab);
Bitstream.ExitBlock();
}
void BitstreamRemarkSerializerHelper::emitRemark(const Remark &Remark,
StringTable &StrTab) {
Bitstream.EnterSubblock(REMARK_BLOCK_ID, 4);
R.clear();
R.push_back(RECORD_REMARK_HEADER);
R.push_back(static_cast<uint64_t>(Remark.RemarkType));
R.push_back(StrTab.add(Remark.RemarkName).first);
R.push_back(StrTab.add(Remark.PassName).first);
R.push_back(StrTab.add(Remark.FunctionName).first);
Bitstream.EmitRecordWithAbbrev(RecordRemarkHeaderAbbrevID, R);
if (const std::optional<RemarkLocation> &Loc = Remark.Loc) {
R.clear();
R.push_back(RECORD_REMARK_DEBUG_LOC);
R.push_back(StrTab.add(Loc->SourceFilePath).first);
R.push_back(Loc->SourceLine);
R.push_back(Loc->SourceColumn);
Bitstream.EmitRecordWithAbbrev(RecordRemarkDebugLocAbbrevID, R);
}
if (std::optional<uint64_t> Hotness = Remark.Hotness) {
R.clear();
R.push_back(RECORD_REMARK_HOTNESS);
R.push_back(*Hotness);
Bitstream.EmitRecordWithAbbrev(RecordRemarkHotnessAbbrevID, R);
}
for (const Argument &Arg : Remark.Args) {
R.clear();
unsigned Key = StrTab.add(Arg.Key).first;
unsigned Val = StrTab.add(Arg.Val).first;
bool HasDebugLoc = Arg.Loc != std::nullopt;
R.push_back(HasDebugLoc ? RECORD_REMARK_ARG_WITH_DEBUGLOC
: RECORD_REMARK_ARG_WITHOUT_DEBUGLOC);
R.push_back(Key);
R.push_back(Val);
if (HasDebugLoc) {
R.push_back(StrTab.add(Arg.Loc->SourceFilePath).first);
R.push_back(Arg.Loc->SourceLine);
R.push_back(Arg.Loc->SourceColumn);
}
Bitstream.EmitRecordWithAbbrev(HasDebugLoc
? RecordRemarkArgWithDebugLocAbbrevID
: RecordRemarkArgWithoutDebugLocAbbrevID,
R);
}
Bitstream.ExitBlock();
}
BitstreamRemarkSerializer::BitstreamRemarkSerializer(raw_ostream &OS)
: RemarkSerializer(Format::Bitstream, OS) {
StrTab.emplace();
}
BitstreamRemarkSerializer::BitstreamRemarkSerializer(raw_ostream &OS,
StringTable StrTabIn)
: RemarkSerializer(Format::Bitstream, OS) {
StrTab = std::move(StrTabIn);
}
BitstreamRemarkSerializer::~BitstreamRemarkSerializer() { finalize(); }
void BitstreamRemarkSerializer::setup() {
if (Helper)
return;
Helper.emplace(BitstreamRemarkContainerType::RemarksFile, OS);
Helper->setupBlockInfo();
Helper->emitMetaBlock();
}
void BitstreamRemarkSerializer::finalize() {
if (!Helper)
return;
Helper->emitLateMetaBlock(*StrTab);
Helper = std::nullopt;
}
void BitstreamRemarkSerializer::emit(const Remark &Remark) {
setup();
Helper->emitRemark(Remark, *StrTab);
}
std::unique_ptr<MetaSerializer>
BitstreamRemarkSerializer::metaSerializer(raw_ostream &OS,
StringRef ExternalFilename) {
return std::make_unique<BitstreamMetaSerializer>(
OS, BitstreamRemarkContainerType::RemarksFileExternal, ExternalFilename);
}
void BitstreamMetaSerializer::emit() {
assert(Helper && "BitstreamMetaSerializer emitted multiple times");
Helper->setupBlockInfo();
Helper->emitMetaBlock(ExternalFilename);
Helper = std::nullopt;
}
|