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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
/*
* Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
*
* 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
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS 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:
* Vadim Girlin
*/
#include "sb_bc.h"
#include "sb_shader.h"
#include "sb_pass.h"
namespace r600_sb {
using std::cerr;
shader::shader(sb_context &sctx, shader_target t, unsigned id, bool dump)
: ctx(sctx), next_temp_value_index(temp_regid_offset),
prep_regs_count(), pred_sels(),
regions(), inputs(), undef(), val_pool(sizeof(value)),
pool(), all_nodes(), src_stats(), opt_stats(), errors(), enable_dump(dump),
optimized(), id(id),
coal(*this), bbs(),
target(t), vt(ex), ex(*this), root(),
compute_interferences(),
has_alu_predication(), uses_gradients(), ngpr(), nstack() {}
bool shader::assign_slot(alu_node* n, alu_node *slots[5]) {
unsigned slot_flags = ctx.alu_slots(n->bc.op);
unsigned slot = n->bc.dst_chan;
if (!ctx.is_cayman() && (!(slot_flags & AF_V) || slots[slot]) &&
(slot_flags & AF_S))
slot = SLOT_TRANS;
if (slots[slot])
return false;
n->bc.slot = slot;
slots[slot] = n;
return true;
}
void shader::add_gpr_values(vvec& vec, unsigned gpr, unsigned comp_mask,
bool src) {
unsigned chan = 0;
while (comp_mask) {
if (comp_mask & 1) {
value *v = get_gpr_value(src, gpr, chan, false);
v->flags |= (VLF_PIN_REG | VLF_PIN_CHAN);
if (!v->is_rel()) {
v->gpr = v->pin_gpr = v->select;
v->fix();
}
vec.push_back(v);
}
comp_mask >>= 1;
++chan;
}
}
cf_node* shader::create_clause(node_subtype nst) {
cf_node *n = create_cf();
n->subtype = nst;
switch (nst) {
case NST_ALU_CLAUSE: n->bc.set_op(CF_OP_ALU); break;
case NST_TEX_CLAUSE: n->bc.set_op(CF_OP_TEX); break;
case NST_VTX_CLAUSE: n->bc.set_op(CF_OP_VTX); break;
default: assert(!"invalid clause type"); break;
}
n->bc.barrier = 1;
return n;
}
void shader::create_bbs() {
create_bbs(root, bbs);
}
void shader::expand_bbs() {
expand_bbs(bbs);
}
alu_node* shader::create_mov(value* dst, value* src) {
alu_node *n = create_alu();
n->bc.set_op(ALU_OP1_MOV);
n->dst.push_back(dst);
n->src.push_back(src);
dst->def = n;
return n;
}
alu_node* shader::create_copy_mov(value* dst, value* src, unsigned affcost) {
alu_node *n = create_mov(dst, src);
dst->assign_source(src);
n->flags |= NF_COPY_MOV | NF_DONT_HOIST;
if (affcost && dst->is_sgpr() && src->is_sgpr())
coal.add_edge(src, dst, affcost);
return n;
}
value* shader::get_value(value_kind kind, sel_chan id,
unsigned version) {
if (version == 0 && kind == VLK_REG && id.sel() < prep_regs_count)
return val_pool[id - 1];
unsigned key = (kind << 28) | (version << 16) | id;
value_map::iterator i = reg_values.find(key);
if (i != reg_values.end()) {
return i->second;
}
value *v = create_value(kind, id, version);
reg_values.insert(std::make_pair(key, v));
return v;
}
value* shader::get_special_value(unsigned sv_id, unsigned version) {
sel_chan id(sv_id, 0);
return get_value(VLK_SPECIAL_REG, id, version);
}
void shader::fill_array_values(gpr_array *a, vvec &vv) {
unsigned sz = a->array_size;
vv.resize(sz);
for (unsigned i = 0; i < a->array_size; ++i) {
vv[i] = get_gpr_value(true, a->base_gpr.sel() + i, a->base_gpr.chan(),
false);
}
}
value* shader::get_gpr_value(bool src, unsigned reg, unsigned chan, bool rel,
unsigned version) {
sel_chan id(reg, chan);
value *v;
gpr_array *a = get_gpr_array(reg, chan);
if (rel) {
assert(a);
v = create_value(VLK_REL_REG, id, 0);
v->rel = get_special_value(SV_AR_INDEX);
fill_array_values(a, v->muse);
if (!src)
fill_array_values(a, v->mdef);
} else {
if (version == 0 && reg < prep_regs_count)
return (val_pool[id - 1]);
v = get_value(VLK_REG, id, version);
}
v->array = a;
v->pin_gpr = v->select;
return v;
}
value* shader::create_temp_value() {
sel_chan id(++next_temp_value_index, 0);
return get_value(VLK_TEMP, id, 0);
}
value* shader::get_kcache_value(unsigned bank, unsigned index, unsigned chan) {
return get_ro_value(kcache_values, VLK_KCACHE,
sel_chan((bank << 12) | index, chan));
}
void shader::add_input(unsigned gpr, bool preloaded, unsigned comp_mask) {
if (inputs.size() <= gpr)
inputs.resize(gpr+1);
shader_input &i = inputs[gpr];
i.preloaded = preloaded;
i.comp_mask = comp_mask;
if (preloaded) {
add_gpr_values(root->dst, gpr, comp_mask, true);
}
}
void shader::init() {
assert(!root);
root = create_container();
}
void shader::init_call_fs(cf_node* cf) {
unsigned gpr = 0;
assert(target == TARGET_VS);
for(inputs_vec::const_iterator I = inputs.begin(),
E = inputs.end(); I != E; ++I, ++gpr) {
if (!I->preloaded)
add_gpr_values(cf->dst, gpr, I->comp_mask, false);
else
add_gpr_values(cf->src, gpr, I->comp_mask, true);
}
}
void shader::set_undef(val_set& s) {
value *undefined = get_undef_value();
if (!undefined->gvn_source)
vt.add_value(undefined);
val_set &vs = s;
for (val_set::iterator I = vs.begin(*this), E = vs.end(*this); I != E; ++I) {
value *v = *I;
assert(!v->is_readonly() && !v->is_rel());
v->gvn_source = undefined->gvn_source;
}
}
value* shader::create_value(value_kind k, sel_chan regid, unsigned ver) {
value *v = val_pool.create(k, regid, ver);
return v;
}
value* shader::get_undef_value() {
if (!undef)
undef = create_value(VLK_UNDEF, 0, 0);
return undef;
}
node* shader::create_node(node_type nt, node_subtype nst, node_flags flags) {
node *n = new (pool.allocate(sizeof(node))) node(nt, nst, flags);
all_nodes.push_back(n);
return n;
}
alu_node* shader::create_alu() {
alu_node* n = new (pool.allocate(sizeof(alu_node))) alu_node();
all_nodes.push_back(n);
return n;
}
alu_group_node* shader::create_alu_group() {
alu_group_node* n =
new (pool.allocate(sizeof(alu_group_node))) alu_group_node();
all_nodes.push_back(n);
return n;
}
alu_packed_node* shader::create_alu_packed() {
alu_packed_node* n =
new (pool.allocate(sizeof(alu_packed_node))) alu_packed_node();
all_nodes.push_back(n);
return n;
}
cf_node* shader::create_cf() {
cf_node* n = new (pool.allocate(sizeof(cf_node))) cf_node();
n->bc.barrier = 1;
all_nodes.push_back(n);
return n;
}
fetch_node* shader::create_fetch() {
fetch_node* n = new (pool.allocate(sizeof(fetch_node))) fetch_node();
all_nodes.push_back(n);
return n;
}
region_node* shader::create_region() {
region_node *n = new (pool.allocate(sizeof(region_node)))
region_node(regions.size());
regions.push_back(n);
all_nodes.push_back(n);
return n;
}
depart_node* shader::create_depart(region_node* target) {
depart_node* n = new (pool.allocate(sizeof(depart_node)))
depart_node(target, target->departs.size());
target->departs.push_back(n);
all_nodes.push_back(n);
return n;
}
repeat_node* shader::create_repeat(region_node* target) {
repeat_node* n = new (pool.allocate(sizeof(repeat_node)))
repeat_node(target, target->repeats.size() + 1);
target->repeats.push_back(n);
all_nodes.push_back(n);
return n;
}
container_node* shader::create_container(node_type nt, node_subtype nst,
node_flags flags) {
container_node *n = new (pool.allocate(sizeof(container_node)))
container_node(nt, nst, flags);
all_nodes.push_back(n);
return n;
}
if_node* shader::create_if() {
if_node* n = new (pool.allocate(sizeof(if_node))) if_node();
all_nodes.push_back(n);
return n;
}
bb_node* shader::create_bb(unsigned id, unsigned loop_level) {
bb_node* n = new (pool.allocate(sizeof(bb_node))) bb_node(id, loop_level);
all_nodes.push_back(n);
return n;
}
value* shader::get_special_ro_value(unsigned sel) {
return get_ro_value(special_ro_values, VLK_PARAM, sel);
}
value* shader::get_const_value(const literal &v) {
value *val = get_ro_value(const_values, VLK_CONST, v);
val->literal_value = v;
return val;
}
shader::~shader() {
for (node_vec::iterator I = all_nodes.begin(), E = all_nodes.end();
I != E; ++I)
(*I)->~node();
}
void shader::dump_ir() {
if (ctx.dump_pass)
dump(*this).run();
}
value* shader::get_value_version(value* v, unsigned ver) {
assert(!v->is_readonly() && !v->is_rel());
value *vv = get_value(v->kind, v->select, ver);
assert(vv);
if (v->array) {
vv->array = v->array;
}
return vv;
}
gpr_array* shader::get_gpr_array(unsigned reg, unsigned chan) {
for (regarray_vec::iterator I = gpr_arrays.begin(),
E = gpr_arrays.end(); I != E; ++I) {
gpr_array* a = *I;
unsigned achan = a->base_gpr.chan();
unsigned areg = a->base_gpr.sel();
if (achan == chan && (reg >= areg && reg < areg+a->array_size))
return a;
}
return NULL;
}
void shader::add_gpr_array(unsigned gpr_start, unsigned gpr_count,
unsigned comp_mask) {
unsigned chan = 0;
while (comp_mask) {
if (comp_mask & 1) {
gpr_array *a = new gpr_array(
sel_chan(gpr_start, chan), gpr_count);
SB_DUMP_PASS( cerr << "add_gpr_array: @" << a->base_gpr
<< " [" << a->array_size << "]\n";
);
gpr_arrays.push_back(a);
}
comp_mask >>= 1;
++chan;
}
}
value* shader::get_pred_sel(int sel) {
assert(sel == 0 || sel == 1);
if (!pred_sels[sel])
pred_sels[sel] = get_const_value(sel);
return pred_sels[sel];
}
cf_node* shader::create_cf(unsigned op) {
cf_node *c = create_cf();
c->bc.set_op(op);
c->bc.barrier = 1;
return c;
}
std::string shader::get_full_target_name() {
std::string s = get_shader_target_name();
s += "/";
s += get_hw_chip_name();
s += "/";
s += get_hw_class_name();
return s;
}
const char* shader::get_hw_class_name() {
switch (ctx.hw_class) {
#define TRANSLATE_HW_CLASS(c) case HW_CLASS_##c: return #c
TRANSLATE_HW_CLASS(R600);
TRANSLATE_HW_CLASS(R700);
TRANSLATE_HW_CLASS(EVERGREEN);
TRANSLATE_HW_CLASS(CAYMAN);
#undef TRANSLATE_HW_CLASS
default:
return "INVALID_CHIP_CLASS";
}
}
const char* shader::get_hw_chip_name() {
switch (ctx.hw_chip) {
#define TRANSLATE_CHIP(c) case HW_CHIP_##c: return #c
TRANSLATE_CHIP(R600);
TRANSLATE_CHIP(RV610);
TRANSLATE_CHIP(RV630);
TRANSLATE_CHIP(RV670);
TRANSLATE_CHIP(RV620);
TRANSLATE_CHIP(RV635);
TRANSLATE_CHIP(RS780);
TRANSLATE_CHIP(RS880);
TRANSLATE_CHIP(RV770);
TRANSLATE_CHIP(RV730);
TRANSLATE_CHIP(RV710);
TRANSLATE_CHIP(RV740);
TRANSLATE_CHIP(CEDAR);
TRANSLATE_CHIP(REDWOOD);
TRANSLATE_CHIP(JUNIPER);
TRANSLATE_CHIP(CYPRESS);
TRANSLATE_CHIP(HEMLOCK);
TRANSLATE_CHIP(PALM);
TRANSLATE_CHIP(SUMO);
TRANSLATE_CHIP(SUMO2);
TRANSLATE_CHIP(BARTS);
TRANSLATE_CHIP(TURKS);
TRANSLATE_CHIP(CAICOS);
TRANSLATE_CHIP(CAYMAN);
#undef TRANSLATE_CHIP
default:
assert(!"unknown chip");
return "INVALID_CHIP";
}
}
const char* shader::get_shader_target_name() {
switch (target) {
case TARGET_VS: return "VS";
case TARGET_PS: return "PS";
case TARGET_GS: return "GS";
case TARGET_COMPUTE: return "COMPUTE";
case TARGET_FETCH: return "FETCH";
default:
return "INVALID_TARGET";
}
}
void shader::simplify_dep_rep(node* dr) {
container_node *p = dr->parent;
if (p->is_repeat()) {
repeat_node *r = static_cast<repeat_node*>(p);
r->target->expand_repeat(r);
} else if (p->is_depart()) {
depart_node *d = static_cast<depart_node*>(p);
d->target->expand_depart(d);
}
if (dr->next)
dr->parent->cut(dr->next, NULL);
}
// FIXME this is used in some places as the max non-temp gpr,
// (MAX_GPR - 2 * ctx.alu_temp_gprs) should be used for that instead.
unsigned shader::first_temp_gpr() {
return MAX_GPR - ctx.alu_temp_gprs;
}
unsigned shader::num_nontemp_gpr() {
return MAX_GPR - 2 * ctx.alu_temp_gprs;
}
void shader::set_uses_kill() {
if (root->src.empty())
root->src.resize(1);
if (!root->src[0])
root->src[0] = get_special_value(SV_VALID_MASK);
}
alu_node* shader::clone(alu_node* n) {
alu_node *c = create_alu();
// FIXME: this may be wrong with indirect operands
c->src = n->src;
c->dst = n->dst;
c->bc = n->bc;
c->pred = n->pred;
return c;
}
void shader::collect_stats(bool opt) {
if (!sb_context::dump_stat)
return;
shader_stats &s = opt ? opt_stats : src_stats;
s.shaders = 1;
s.ngpr = ngpr;
s.nstack = nstack;
s.collect(root);
if (opt)
ctx.opt_stats.accumulate(s);
else
ctx.src_stats.accumulate(s);
}
value* shader::get_ro_value(value_map& vm, value_kind vk, unsigned key) {
value_map::iterator I = vm.find(key);
if (I != vm.end())
return I->second;
value *v = create_value(vk, key, 0);
v->flags = VLF_READONLY;
vm.insert(std::make_pair(key, v));
return v;
}
void shader::create_bbs(container_node* n, bbs_vec &bbs, int loop_level) {
bool inside_bb = false;
bool last_inside_bb = true;
node_iterator bb_start(n->begin()), I(bb_start), E(n->end());
for (; I != E; ++I) {
node *k = *I;
inside_bb = k->type == NT_OP;
if (inside_bb && !last_inside_bb)
bb_start = I;
else if (!inside_bb) {
if (last_inside_bb
&& I->type != NT_REPEAT
&& I->type != NT_DEPART
&& I->type != NT_IF) {
bb_node *bb = create_bb(bbs.size(), loop_level);
bbs.push_back(bb);
n->insert_node_before(*bb_start, bb);
if (bb_start != I)
bb->move(bb_start, I);
}
if (k->is_container()) {
bool loop = false;
if (k->type == NT_REGION) {
loop = static_cast<region_node*>(k)->is_loop();
}
create_bbs(static_cast<container_node*>(k), bbs,
loop_level + loop);
}
}
if (k->type == NT_DEPART)
return;
last_inside_bb = inside_bb;
}
if (last_inside_bb) {
bb_node *bb = create_bb(bbs.size(), loop_level);
bbs.push_back(bb);
if (n->empty())
n->push_back(bb);
else {
n->insert_node_before(*bb_start, bb);
if (bb_start != n->end())
bb->move(bb_start, n->end());
}
} else {
if (n->last && n->last->type == NT_IF) {
bb_node *bb = create_bb(bbs.size(), loop_level);
bbs.push_back(bb);
n->push_back(bb);
}
}
}
void shader::expand_bbs(bbs_vec &bbs) {
for (bbs_vec::iterator I = bbs.begin(), E = bbs.end(); I != E; ++I) {
bb_node *b = *I;
b->expand();
}
}
sched_queue_id shader::get_queue_id(node* n) {
switch (n->subtype) {
case NST_ALU_INST:
case NST_ALU_PACKED_INST:
case NST_COPY:
case NST_PSI:
return SQ_ALU;
case NST_FETCH_INST: {
fetch_node *f = static_cast<fetch_node*>(n);
if (ctx.is_r600() && (f->bc.op_ptr->flags & FF_VTX))
return SQ_VTX;
return SQ_TEX;
}
case NST_CF_INST:
return SQ_CF;
default:
assert(0);
return SQ_NUM;
}
}
void shader_stats::collect(node *n) {
if (n->is_alu_inst())
++alu;
else if (n->is_fetch_inst())
++fetch;
else if (n->is_container()) {
container_node *c = static_cast<container_node*>(n);
if (n->is_alu_group())
++alu_groups;
else if (n->is_alu_clause())
++alu_clauses;
else if (n->is_fetch_clause())
++fetch_clauses;
else if (n->is_cf_inst())
++cf;
if (!c->empty()) {
for (node_iterator I = c->begin(), E = c->end(); I != E; ++I) {
collect(*I);
}
}
}
}
void shader_stats::accumulate(shader_stats& s) {
++shaders;
ndw += s.ndw;
ngpr += s.ngpr;
nstack += s.nstack;
alu += s.alu;
alu_groups += s.alu_groups;
alu_clauses += s.alu_clauses;
fetch += s.fetch;
fetch_clauses += s.fetch_clauses;
cf += s.cf;
}
void shader_stats::dump(std::ostream& o) {
o << "dw:" << ndw << ", gpr:" << ngpr << ", stk:" << nstack
<< ", alu groups:" << alu_groups << ", alu clauses: " << alu_clauses
<< ", alu:" << alu << ", fetch:" << fetch
<< ", fetch clauses:" << fetch_clauses
<< ", cf:" << cf;
if (shaders > 1)
o << ", shaders:" << shaders;
o << "\n";
}
static void print_diff(std::ostream &o, unsigned d1, unsigned d2) {
if (d1)
o << ((int)d2 - (int)d1) * 100 / (int)d1 << "%";
else if (d2)
o << "N/A";
else
o << "0%";
}
void shader_stats::dump_diff(std::ostream& o, shader_stats& s) {
o << "dw:"; print_diff(o, ndw, s.ndw);
o << ", gpr:" ; print_diff(o, ngpr, s.ngpr);
o << ", stk:" ; print_diff(o, nstack, s.nstack);
o << ", alu groups:" ; print_diff(o, alu_groups, s.alu_groups);
o << ", alu clauses: " ; print_diff(o, alu_clauses, s.alu_clauses);
o << ", alu:" ; print_diff(o, alu, s.alu);
o << ", fetch:" ; print_diff(o, fetch, s.fetch);
o << ", fetch clauses:" ; print_diff(o, fetch_clauses, s.fetch_clauses);
o << ", cf:" ; print_diff(o, cf, s.cf);
o << "\n";
}
} // namespace r600_sb
|