forked from AmpereComputing/cbp-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace_reader.hpp
More file actions
285 lines (248 loc) · 9.44 KB
/
Copy pathtrace_reader.hpp
File metadata and controls
285 lines (248 loc) · 9.44 KB
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
//
// Portions copyright (c) 2025 Ampere Computing
//
// This file handles reading a subset of the format used for CBP2025 at
// https://github.com/ramisheikh/cbp2025/blob/main/lib/trace_reader.h. The
// original copyright/license message from that file is reproduced below:
// CBP Trace Reader
// Author: Arthur Perais (arthur.perais@gmail.com) for CVP
// Saransh Jain/Rami Sheikh updated for CBP
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org>
*/
#pragma once
#include <cassert>
#include <cstdint>
#include <zlib.h>
#include <string>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <optional>
enum class INST_CLASS: uint8_t
{
ALU = 0,
LOAD = 1,
STORE = 2,
BR_COND = 3,
BR_UNCOND_DIRECT = 4,
BR_UNCOND_INDIRECT = 5,
FP = 6,
ALU_SLOW = 7,
UNDEF = 8,
BR_CALL_DIRECT = 9,
BR_CALL_INDIRECT = 10,
BR_RETURN = 11
};
struct instruction {
uint64_t pc = 0xdeadc0dedeadc0de;
uint64_t next_pc = 0xdeadc0dedeadc0de;
INST_CLASS inst_class = INST_CLASS::ALU;
bool branch = false;
bool taken_branch = false;
};
struct out_of_instructions: std::runtime_error {
using std::runtime_error::runtime_error;
};
class trace_reader
{
private:
std::string trace_name;
gzFile gz_fp;
// branch_only stores whether there is information in this trace about
// non-branch instructions.
bool branch_only;
std::optional<struct instruction> putback_instruction;
uint64_t num_instructions_read = 0;
public:
trace_reader(std::string path, std::string name): trace_name{name} {
gz_fp = gzopen(path.c_str(), "rb");
if (!gz_fp) {
throw std::runtime_error("Failed to open gzipped file: " + path);
}
// Try to read a magic number as the first 8 bytes of this trace to see
// which version it is.
const char magic[9] = "CBPNGAmp";
branch_only = true;
for (unsigned i = 0; i < sizeof(magic) - 1; i++) {
char c;
read(c);
if (c != magic[i]) {
branch_only = false;
gzseek(gz_fp, 0, SEEK_SET);
break;
}
}
}
std::string name() { return trace_name; }
~trace_reader() {
if (gz_fp) {
gzclose(gz_fp);
gz_fp = nullptr;
}
}
private:
template <typename T>
void read(T& obj) {
long unsigned num_read = gzread(gz_fp, static_cast<void*>(&obj), sizeof(T));
if (num_read < sizeof(T)) {
if (num_read == 0 && gzeof(gz_fp)) {
throw out_of_instructions("Encountered EOF when attempting to read instruction");
}
throw std::runtime_error("Failed to read all bytes from trace");
}
}
void ignore_read(size_t bytes) {
auto ret = gzseek(gz_fp, bytes, SEEK_CUR);
if (ret < 0)
throw std::runtime_error("Failed to ignore bytes from trace");
}
struct instruction read_next_instruction() {
// Trace Format :
// Inst PC - 8 bytes
// Inst Type - 1 byte
// If load/store
// Effective Address - 8 bytes
// Access Size (total) - 1 byte
// Involves Base Update - 1 byte
// If Store:
// Involves Reg Offset - 1 byte
// If branch
// Taken - 1 byte
// If Taken:
// Target - 8 bytes
// Num Input Regs - 1 byte
// Input Reg Names - 1 byte each
// Num Output Regs - 1 byte
// Output Reg Names - 1 byte each
// Output Reg Values
// If INT - 8 bytes each
// If SIMD - 16 bytes each
//
// Int registers are encoded 0-30(GPRs), 31(Stack Pointer Register), 64(Flag Register), 65(Zero Register)
// SIMD registers are encoded 32-63
struct instruction instr;
read(instr.pc);
instr.next_pc = instr.pc + 4;
read(instr.inst_class);
assert(instr.inst_class != INST_CLASS::UNDEF);
//EffAddr is the base address
bool has_base_update = false;
if (instr.inst_class == INST_CLASS::LOAD || instr.inst_class == INST_CLASS::STORE) {
// Effective Address - 8 bytes
// Access Size (total) - 1 byte
ignore_read(8 + 1);
// Involves Base Update - 1 byte
read(has_base_update);
// If Store:
// Involves Reg Offset - 1 byte
if (instr.inst_class == INST_CLASS::STORE)
ignore_read(1);
}
const bool is_branch =
instr.inst_class == INST_CLASS::BR_COND ||
instr.inst_class == INST_CLASS::BR_UNCOND_DIRECT ||
instr.inst_class == INST_CLASS::BR_UNCOND_INDIRECT ||
instr.inst_class == INST_CLASS::BR_CALL_DIRECT ||
instr.inst_class == INST_CLASS::BR_CALL_INDIRECT ||
instr.inst_class == INST_CLASS::BR_RETURN;
if (is_branch) {
instr.branch = true;
read(instr.taken_branch);
if(instr.inst_class != INST_CLASS::BR_COND)
assert(instr.taken_branch);
if(instr.taken_branch)
read(instr.next_pc);
}
uint8_t num_in_regs, num_out_regs;
// note: will only hold integer registers in these vectors!
std::vector<uint8_t> in_regs, out_regs;
read(num_in_regs);
for (int i = 0; i < num_in_regs; i++) {
uint8_t reg_no;
read(reg_no);
if (reg_no < 32)
in_regs.push_back(reg_no);
}
read(num_out_regs);
for (int i = 0; i < num_out_regs; i++) {
uint8_t reg_no;
read(reg_no);
out_regs.push_back(reg_no);
}
// Determine if this instruction has a "base update register" so that
// we can correctly read the trace later depending on the answer
uint8_t base_update_reg = 0;
if (has_base_update) {
if (instr.inst_class == INST_CLASS::STORE) {
if (num_out_regs != 1) {
has_base_update = false;
} else {
assert(out_regs.size() == 1);
base_update_reg = out_regs[0];
}
} else { // loads
if (num_out_regs <= 1) {
has_base_update = false;
} else {
std::vector<uint8_t> overlap;
std::vector<uint8_t> int_out_regs;
std::copy_if(out_regs.begin(), out_regs.end(), std::back_inserter(int_out_regs), [](int reg) { return reg < 32; });
std::set_intersection(in_regs.begin(), in_regs.end(), int_out_regs.begin(), int_out_regs.end(), std::back_inserter(overlap));
if (overlap.size() == 1) {
base_update_reg = overlap[0];
} else {
has_base_update = false;
}
}
}
}
// ignore reads for all register values
for (auto i = 0; i < num_out_regs; i++)
{
ignore_read(8);
// Base update registers are not written to the trace, so we
// shouldn't ignore them
const bool matching_base_upd = has_base_update && base_update_reg == out_regs[i];
// < 32 are integer registers; 32-63 are FP/SIMD; 64 and 65 are condition code and zero register, respectively
const bool integer_register = (out_regs[i] < 32) || (out_regs[i] == 64) || (out_regs[i] == 65);
if (!matching_base_upd && !integer_register)
ignore_read(8);
}
num_instructions_read++;
return instr;
}
public:
void put_back(struct instruction inst) {
assert(!putback_instruction.has_value());
putback_instruction = inst;
}
struct instruction next_instruction() {
if (putback_instruction.has_value()) {
struct instruction tmp = putback_instruction.value();
putback_instruction.reset();
return tmp;
} else {
return read_next_instruction();
}
}
};