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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
|
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
/*
* Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
*
* 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.
*
* Authors:
* Rob Clark <robclark@freedesktop.org>
*/
#include "pipe/p_shader_tokens.h"
#include "util/u_math.h"
#include "ir3.h"
#include "ir3_visitor.h"
/*
* Register Assignment:
*
* NOTE: currently only works on a single basic block.. need to think
* about how multiple basic blocks are going to get scheduled. But
* I think I want to re-arrange how blocks work, ie. get rid of the
* block nesting thing..
*
* NOTE: we could do register coalescing (eliminate moves) as part of
* the RA step.. OTOH I think we need to do scheduling before register
* assignment. And if we remove a mov that effects scheduling (unless
* we leave a placeholder nop, which seems lame), so I'm not really
* sure how practical this is to do both in a single stage. But OTOH
* I'm not really sure a sane way for the CP stage to realize when it
* cannot remove a mov due to multi-register constraints..
*
*/
struct ir3_ra_ctx {
struct ir3_block *block;
enum shader_t type;
int cnt;
bool error;
};
struct ir3_ra_assignment {
int8_t off; /* offset of instruction dst within range */
uint8_t num; /* number of components for the range */
};
static void ra_assign(struct ir3_ra_ctx *ctx,
struct ir3_instruction *assigner, int num);
static struct ir3_ra_assignment ra_calc(struct ir3_instruction *instr);
/*
* Register Allocation:
*/
#define REG(n, wm) (struct ir3_register){ \
/*.flags = ((so)->half_precision) ? IR3_REG_HALF : 0,*/ \
.num = (n), \
.wrmask = TGSI_WRITEMASK_ ## wm, \
}
/* check that the register exists, is a GPR and is not special (a0/p0) */
static struct ir3_register * reg_check(struct ir3_instruction *instr, unsigned n)
{
if ((n < instr->regs_count) && reg_gpr(instr->regs[n]))
return instr->regs[n];
return NULL;
}
static int output_base(struct ir3_ra_ctx *ctx)
{
/* ugg, for fragment shader we need to have input at r0.x
* (or at least if there is a way to configure it, I can't
* see how because the blob driver always uses r0.x (ie.
* all zeros)
*/
if (ctx->type == SHADER_FRAGMENT)
return 2;
return 0;
}
/* live means read before written */
static void compute_liveregs(struct ir3_ra_ctx *ctx,
struct ir3_instruction *instr, regmask_t *liveregs)
{
struct ir3_block *block = instr->block;
regmask_t written;
unsigned i, j;
regmask_init(liveregs);
regmask_init(&written);
for (instr = instr->next; instr; instr = instr->next) {
struct ir3_register *r;
if (is_meta(instr))
continue;
/* check first src's read: */
for (j = 1; j < instr->regs_count; j++) {
r = reg_check(instr, j);
if (r)
regmask_set_if_not(liveregs, r, &written);
}
/* then dst written (if assigned already): */
if (instr->flags & IR3_INSTR_MARK) {
r = reg_check(instr, 0);
if (r)
regmask_set(&written, r);
}
}
/* be sure to account for output registers too: */
for (i = 0; i < block->noutputs; i++) {
struct ir3_register reg = REG(output_base(ctx) + i, X);
regmask_set_if_not(liveregs, ®, &written);
}
}
/* calculate registers that are clobbered before last use of 'assigner'.
* This needs to be done backwards, although it could possibly be
* combined into compute_liveregs(). (Ie. compute_liveregs() could
* reverse the list, then do this part backwards reversing the list
* again back to original order.) Otoh, probably I should try to
* construct a proper interference graph instead.
*
* XXX this need to follow the same recursion path that is used for
* to rename/assign registers (ie. ra_assign_src()).. this is a bit
* ugly right now, maybe refactor into node iterator sort of things
* that iterates nodes in the correct order?
*/
static bool compute_clobbers(struct ir3_ra_ctx *ctx,
struct ir3_instruction *instr, struct ir3_instruction *assigner,
regmask_t *liveregs)
{
unsigned i;
bool live = false, was_live = false;
if (instr == NULL) {
struct ir3_block *block = ctx->block;
/* if at the end, check outputs: */
for (i = 0; i < block->noutputs; i++)
if (block->outputs[i] == assigner)
return true;
return false;
}
for (i = 1; i < instr->regs_count; i++) {
struct ir3_register *reg = instr->regs[i];
if ((reg->flags & IR3_REG_SSA) && (reg->instr == assigner)) {
if (is_meta(instr)) {
switch (instr->opc) {
case OPC_META_INPUT:
// TODO
assert(0);
break;
case OPC_META_FO:
case OPC_META_FI:
was_live |= compute_clobbers(ctx, instr->next,
instr, liveregs);
break;
default:
break;
}
}
live = true;
break;
}
}
was_live |= compute_clobbers(ctx, instr->next, assigner, liveregs);
if (was_live && (instr->regs_count > 0) &&
(instr->flags & IR3_INSTR_MARK) &&
!is_meta(instr))
regmask_set(liveregs, instr->regs[0]);
return live || was_live;
}
static int find_available(regmask_t *liveregs, int size)
{
unsigned i;
for (i = 0; i < MAX_REG - size; i++) {
if (!regmask_get(liveregs, ®(i, X))) {
unsigned start = i++;
for (; (i < MAX_REG) && ((i - start) < size); i++)
if (regmask_get(liveregs, ®(i, X)))
break;
if ((i - start) >= size)
return start;
}
}
assert(0);
return -1;
}
static int alloc_block(struct ir3_ra_ctx *ctx,
struct ir3_instruction *instr, int size)
{
if (!instr) {
/* special case, allocating shader outputs. At this
* point, nothing is allocated, just start the shader
* outputs at r0.x and let compute_liveregs() take
* care of the rest from here:
*/
return 0;
} else {
regmask_t liveregs;
compute_liveregs(ctx, instr, &liveregs);
// XXX XXX XXX XXX XXX XXX XXX XXX XXX
// XXX hack.. maybe ra_calc should give us a list of
// instrs to compute_clobbers() on?
if (is_meta(instr) && (instr->opc == OPC_META_INPUT) &&
(instr->regs_count == 1)) {
unsigned i, base = instr->regs[0]->num & ~0x3;
for (i = 0; i < 4; i++) {
struct ir3_instruction *in = ctx->block->inputs[base + i];
if (in)
compute_clobbers(ctx, in->next, in, &liveregs);
}
} else
// XXX XXX XXX XXX XXX XXX XXX XXX XXX
compute_clobbers(ctx, instr->next, instr, &liveregs);
return find_available(&liveregs, size);
}
}
/*
* Constraint Calculation:
*/
struct ra_calc_visitor {
struct ir3_visitor base;
struct ir3_ra_assignment a;
};
static inline struct ra_calc_visitor *ra_calc_visitor(struct ir3_visitor *v)
{
return (struct ra_calc_visitor *)v;
}
/* calculate register assignment for the instruction. If the register
* written by this instruction is required to be part of a range, to
* handle other (input/output/sam/bary.f/etc) contiguous register range
* constraints, that is calculated handled here.
*/
static void ra_calc_dst(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_calc_visitor *c = ra_calc_visitor(v);
if (is_tex(instr)) {
c->a.off = 0;
c->a.num = 4;
} else {
c->a.off = 0;
c->a.num = 1;
}
}
static void
ra_calc_dst_shader_input(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_calc_visitor *c = ra_calc_visitor(v);
struct ir3_block *block = instr->block;
struct ir3_register *dst = instr->regs[0];
unsigned base = dst->num & ~0x3;
unsigned i, num = 0;
assert(!(dst->flags & IR3_REG_IA));
/* check what input components we need: */
for (i = 0; i < 4; i++) {
unsigned idx = base + i;
if ((idx < block->ninputs) && block->inputs[idx])
num = i + 1;
}
c->a.off = dst->num - base;
c->a.num = num;
}
static void ra_calc_src_fanin(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_calc_visitor *c = ra_calc_visitor(v);
unsigned srcn = ir3_instr_regno(instr, reg) - 1;
c->a.off -= srcn;
c->a.num += srcn;
c->a.num = MAX2(c->a.num, instr->regs_count - 1);
}
static const struct ir3_visitor_funcs calc_visitor_funcs = {
.instr = ir3_visit_instr,
.dst_shader_input = ra_calc_dst_shader_input,
.dst_fanout = ra_calc_dst,
.dst_fanin = ra_calc_dst,
.dst = ra_calc_dst,
.src_fanout = ir3_visit_reg,
.src_fanin = ra_calc_src_fanin,
.src = ir3_visit_reg,
};
static struct ir3_ra_assignment ra_calc(struct ir3_instruction *assigner)
{
struct ra_calc_visitor v = {
.base.funcs = &calc_visitor_funcs,
};
ir3_visit_instr(&v.base, assigner);
return v.a;
}
/*
* Register Assignment:
*/
struct ra_assign_visitor {
struct ir3_visitor base;
struct ir3_ra_ctx *ctx;
int num;
};
static inline struct ra_assign_visitor *ra_assign_visitor(struct ir3_visitor *v)
{
return (struct ra_assign_visitor *)v;
}
static void ra_assign_reg(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_assign_visitor *a = ra_assign_visitor(v);
reg->flags &= ~IR3_REG_SSA;
reg->num = a->num;
}
static void ra_assign_dst_shader_input(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_assign_visitor *a = ra_assign_visitor(v);
unsigned i, base = reg->num & ~0x3;
int off = base - reg->num;
ra_assign_reg(v, instr, reg);
reg->flags |= IR3_REG_IA;
/* trigger assignment of all our companion input components: */
for (i = 0; i < 4; i++) {
struct ir3_instruction *in = instr->block->inputs[i+base];
if (in && is_meta(in) && (in->opc == OPC_META_INPUT))
ra_assign(a->ctx, in, a->num + off + i);
}
}
static void ra_assign_dst_fanout(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_assign_visitor *a = ra_assign_visitor(v);
struct ir3_register *src = instr->regs[1];
ra_assign_reg(v, instr, reg);
if (src->flags & IR3_REG_SSA)
ra_assign(a->ctx, src->instr, a->num - instr->fo.off);
}
static void ra_assign_src_fanout(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_assign_visitor *a = ra_assign_visitor(v);
ra_assign_reg(v, instr, reg);
ra_assign(a->ctx, instr, a->num + instr->fo.off);
}
static void ra_assign_src_fanin(struct ir3_visitor *v,
struct ir3_instruction *instr, struct ir3_register *reg)
{
struct ra_assign_visitor *a = ra_assign_visitor(v);
unsigned j, srcn = ir3_instr_regno(instr, reg) - 1;
ra_assign_reg(v, instr, reg);
ra_assign(a->ctx, instr, a->num - srcn);
for (j = 1; j < instr->regs_count; j++) {
struct ir3_register *reg = instr->regs[j];
if (reg->flags & IR3_REG_SSA) /* could be renamed already */
ra_assign(a->ctx, reg->instr, a->num - srcn + j - 1);
}
}
static const struct ir3_visitor_funcs assign_visitor_funcs = {
.instr = ir3_visit_instr,
.dst_shader_input = ra_assign_dst_shader_input,
.dst_fanout = ra_assign_dst_fanout,
.dst_fanin = ra_assign_reg,
.dst = ra_assign_reg,
.src_fanout = ra_assign_src_fanout,
.src_fanin = ra_assign_src_fanin,
.src = ra_assign_reg,
};
static void ra_assign(struct ir3_ra_ctx *ctx,
struct ir3_instruction *assigner, int num)
{
struct ra_assign_visitor v = {
.base.funcs = &assign_visitor_funcs,
.ctx = ctx,
.num = num,
};
/* if we've already visited this instruction, bail now: */
if (ir3_instr_check_mark(assigner)) {
debug_assert(assigner->regs[0]->num == num);
if (assigner->regs[0]->num != num) {
/* impossible situation, should have been resolved
* at an earlier stage by inserting extra mov's:
*/
ctx->error = true;
}
return;
}
ir3_visit_instr(&v.base, assigner);
}
/*
*
*/
static void ir3_instr_ra(struct ir3_ra_ctx *ctx,
struct ir3_instruction *instr)
{
struct ir3_ra_assignment a;
unsigned num;
/* skip over nop's */
if (instr->regs_count == 0)
return;
/* if we've already visited this instruction, bail now: */
if (instr->flags & IR3_INSTR_MARK)
return;
/* allocate register(s): */
a = ra_calc(instr);
num = alloc_block(ctx, instr, a.num) + a.off;
ra_assign(ctx, instr, num);
}
/* flatten into shader: */
// XXX this should probably be somewhere else:
static void legalize(struct ir3_ra_ctx *ctx, struct ir3_block *block)
{
struct ir3_instruction *n;
struct ir3_shader *shader = block->shader;
struct ir3_instruction *end =
ir3_instr_create(block, 0, OPC_END);
struct ir3_instruction *last_input = NULL;
regmask_t needs_ss;
regmask_t needs_sy;
regmask_init(&needs_ss);
regmask_init(&needs_sy);
shader->instrs_count = 0;
for (n = block->head; n; n = n->next) {
unsigned i;
if (is_meta(n))
continue;
for (i = 1; i < n->regs_count; i++) {
struct ir3_register *reg = n->regs[i];
if (is_gpr(reg)) {
/* TODO: we probably only need (ss) for alu
* instr consuming sfu result.. need to make
* some tests for both this and (sy)..
*/
if (regmask_get(&needs_ss, reg)) {
n->flags |= IR3_INSTR_SS;
regmask_init(&needs_ss);
}
if (regmask_get(&needs_sy, reg)) {
n->flags |= IR3_INSTR_SY;
regmask_init(&needs_sy);
}
}
}
shader->instrs[shader->instrs_count++] = n;
if (is_sfu(n))
regmask_set(&needs_ss, n->regs[0]);
if (is_tex(n))
regmask_set(&needs_sy, n->regs[0]);
if (is_input(n))
last_input = n;
}
if (last_input)
last_input->regs[0]->flags |= IR3_REG_EI;
shader->instrs[shader->instrs_count++] = end;
shader->instrs[0]->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
}
static int block_ra(struct ir3_ra_ctx *ctx, struct ir3_block *block)
{
struct ir3_instruction *n;
if (!block->parent) {
unsigned i;
int base, off = output_base(ctx);
base = alloc_block(ctx, NULL, block->noutputs + off);
for (i = 0; i < block->noutputs; i++)
if (block->outputs[i])
ra_assign(ctx, block->outputs[i], base + i + off);
if (ctx->type == SHADER_FRAGMENT) {
for (i = 0; i < block->ninputs; i++)
if (block->inputs[i])
ra_assign(ctx, block->inputs[i], base + i);
} else {
for (i = 0; i < block->ninputs; i++)
if (block->inputs[i])
ir3_instr_ra(ctx, block->inputs[i]);
}
}
/* then loop over instruction list and assign registers:
*/
n = block->head;
while (n) {
ir3_instr_ra(ctx, n);
if (ctx->error)
return -1;
n = n->next;
}
legalize(ctx, block);
return 0;
}
int ir3_block_ra(struct ir3_block *block, enum shader_t type)
{
struct ir3_ra_ctx ctx = {
.block = block,
.type = type,
};
ir3_shader_clear_mark(block->shader);
return block_ra(&ctx, block);
}
|