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
|
/*
* Copyright (C) 2020 Collabora, Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* 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 OR COPYRIGHT HOLDERS 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.
*/
#include "compiler.h"
/* This file contains the final passes of the compiler. Running after
* scheduling and RA, the IR is now finalized, so we need to emit it to actual
* bits on the wire (as well as fixup branches) */
static uint64_t
bi_pack_header(bi_clause *clause, bi_clause *next)
{
struct bifrost_header header = {
/* stub */
.no_end_of_shader = (next != NULL),
};
uint64_t u = 0;
memcpy(&u, &header, sizeof(header));
return u;
}
/* Represents the assignment of ports for a given bundle */
struct bi_registers {
/* Register to assign to each port */
unsigned port[4];
/* Read ports can be disabled */
bool enabled[2];
/* Should we write FMA? what about ADD? If only a single port is
* enabled it is in port 2, else ADD/FMA is 2/3 respectively */
bool write_fma, write_add;
/* Should we read with port 3? */
bool read_port3;
/* Packed uniform/constant */
unsigned uniform_constant;
/* Whether writes are actually for the last instruction */
bool first_instruction;
};
/* Assigns a port for reading, before anything is written */
static void
bi_assign_port_read(struct bi_registers *regs, unsigned src)
{
/* We only assign for registers */
if (!(src & BIR_INDEX_REGISTER))
return;
unsigned reg = src & ~BIR_INDEX_REGISTER;
/* Check if we already assigned the port */
for (unsigned i = 0; i <= 1; ++i) {
if (regs->port[i] == reg && regs->enabled[i])
return;
}
if (regs->port[3] == reg && regs->read_port3)
return;
/* Assign it now */
for (unsigned i = 0; i <= 1; ++i) {
if (!regs->enabled[i]) {
regs->port[i] = reg;
regs->enabled[i] = true;
return;
}
}
if (!regs->read_port3) {
regs->port[3] = reg;
regs->read_port3 = true;
}
}
static struct bi_registers
bi_assign_ports(bi_bundle now, bi_bundle prev)
{
struct bi_registers regs = { 0 };
/* First, assign reads */
if (now.fma)
bi_foreach_src(now.fma, src)
bi_assign_port_read(®s, now.fma->src[src]);
if (now.add)
bi_foreach_src(now.add, src)
bi_assign_port_read(®s, now.add->src[src]);
/* Next, assign writes */
if (prev.fma && prev.fma->dest & BIR_INDEX_REGISTER) {
regs.port[2] = prev.fma->dest & ~BIR_INDEX_REGISTER;
regs.write_fma = true;
}
if (prev.add && prev.add->dest & BIR_INDEX_REGISTER) {
unsigned r = prev.add->dest & ~BIR_INDEX_REGISTER;
if (regs.write_fma) {
/* Scheduler constraint: cannot read 3 and write 2 */
assert(!regs.read_port3);
regs.port[3] = r;
} else {
regs.port[2] = r;
}
regs.write_add = true;
}
/* Finally, ensure port 1 > port 0 for the 63-x trick to function */
if (regs.enabled[0] && regs.enabled[1] && regs.port[1] < regs.port[0]) {
unsigned temp = regs.port[0];
regs.port[0] = regs.port[1];
regs.port[1] = temp;
}
return regs;
}
/* Determines the register control field, ignoring the first? flag */
static enum bifrost_reg_control
bi_pack_register_ctrl_lo(struct bi_registers r)
{
if (r.write_fma) {
if (r.write_add) {
assert(!r.read_port3);
return BIFROST_WRITE_ADD_P2_FMA_P3;
} else {
if (r.read_port3)
return BIFROST_WRITE_FMA_P2_READ_P3;
else
return BIFROST_WRITE_FMA_P2;
}
} else if (r.write_add) {
if (r.read_port3)
return BIFROST_WRITE_ADD_P2_READ_P3;
else
return BIFROST_WRITE_ADD_P2;
} else if (r.read_port3)
return BIFROST_READ_P3;
else
return BIFROST_REG_NONE;
}
/* Ditto but account for the first? flag this time */
static enum bifrost_reg_control
bi_pack_register_ctrl(struct bi_registers r)
{
enum bifrost_reg_control ctrl = bi_pack_register_ctrl_lo(r);
if (r.first_instruction) {
if (ctrl == BIFROST_REG_NONE)
ctrl = BIFROST_FIRST_NONE;
else
ctrl |= BIFROST_FIRST_NONE;
}
return ctrl;
}
static uint64_t
bi_pack_registers(struct bi_registers regs)
{
enum bifrost_reg_control ctrl = bi_pack_register_ctrl(regs);
struct bifrost_regs s;
uint64_t packed = 0;
if (regs.enabled[1]) {
/* Gotta save that bit!~ Required by the 63-x trick */
assert(regs.port[1] > regs.port[0]);
assert(regs.enabled[0]);
/* Do the 63-x trick, see docs/disasm */
if (regs.port[0] > 31) {
regs.port[0] = 63 - regs.port[0];
regs.port[1] = 63 - regs.port[1];
}
assert(regs.port[0] <= 31);
assert(regs.port[1] <= 63);
s.ctrl = ctrl;
s.reg1 = regs.port[1];
s.reg0 = regs.port[0];
} else {
/* Port 1 disabled, so set to zero and use port 1 for ctrl */
s.reg1 = ctrl << 2;
if (regs.enabled[0]) {
/* Bit 0 upper bit of port 0 */
s.reg1 |= (regs.port[0] >> 5);
/* Rest of port 0 in usual spot */
s.reg0 = (regs.port[0] & 0b11111);
} else {
/* Bit 1 set if port 0 also disabled */
s.reg1 |= (1 << 1);
}
}
s.reg3 = regs.port[3];
s.reg2 = regs.port[2];
s.uniform_const = regs.uniform_constant;
memcpy(&packed, &s, sizeof(s));
return packed;
}
static unsigned
bi_pack_fma(bi_clause *clause, bi_bundle bundle)
{
/* TODO */
return BIFROST_FMA_NOP;
}
static unsigned
bi_pack_add(bi_clause *clause, bi_bundle bundle)
{
/* TODO */
return BIFROST_ADD_NOP;
}
struct bi_packed_bundle {
uint64_t lo;
uint64_t hi;
};
static struct bi_packed_bundle
bi_pack_bundle(bi_clause *clause, bi_bundle bundle, bi_bundle prev, bool first_bundle)
{
struct bi_registers regs = bi_assign_ports(bundle, prev);
regs.first_instruction = first_bundle;
uint64_t reg = bi_pack_registers(regs);
uint64_t fma = bi_pack_fma(clause, bundle);
uint64_t add = bi_pack_add(clause, bundle);
struct bi_packed_bundle packed = {
.lo = reg | (fma << 35) | ((add & 0b111111) << 58),
.hi = add >> 6
};
return packed;
}
static void
bi_pack_clause(bi_context *ctx, bi_clause *clause, bi_clause *next,
struct util_dynarray *emission)
{
struct bi_packed_bundle ins_1 = bi_pack_bundle(clause, clause->bundles[0], clause->bundles[0], true);
assert(clause->bundle_count == 1);
struct bifrost_fmt1 quad_1 = {
.tag = BIFROST_FMT1_FINAL,
.header = bi_pack_header(clause, next),
.ins_1 = ins_1.lo,
.ins_2 = ins_1.hi & ((1 << 11) - 1),
.ins_0 = (ins_1.hi >> 11) & 0b111,
};
util_dynarray_append(emission, struct bifrost_fmt1, quad_1);
}
static bi_clause *
bi_next_clause(bi_context *ctx, pan_block *block, bi_clause *clause)
{
/* Try the next clause in this block */
if (clause->link.next != &((bi_block *) block)->clauses)
return list_first_entry(&(clause->link), bi_clause, link);
/* Try the next block, or the one after that if it's empty, etc .*/
pan_block *next_block = pan_next_block(block);
bi_foreach_block_from(ctx, next_block, block) {
bi_block *blk = (bi_block *) block;
if (!list_is_empty(&blk->clauses))
return list_first_entry(&(blk->clauses), bi_clause, link);
}
return NULL;
}
void
bi_pack(bi_context *ctx, struct util_dynarray *emission)
{
util_dynarray_init(emission, NULL);
bi_foreach_block(ctx, _block) {
bi_block *block = (bi_block *) _block;
bi_foreach_clause_in_block(block, clause) {
bi_clause *next = bi_next_clause(ctx, _block, clause);
bi_pack_clause(ctx, clause, next, emission);
}
}
}
|