r600g/sb: initial commit of the optimizing shader backend
[mesa.git] / src / gallium / drivers / r600 / sb / sb_gcm.cpp
1 /*
2 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Vadim Girlin
25 */
26
27 #define GCM_DEBUG 0
28
29 #if GCM_DEBUG
30 #define GCM_DUMP(a) do { a } while(0);
31 #else
32 #define GCM_DUMP(a)
33 #endif
34
35 #include <iostream>
36 #include <map>
37
38 #include "sb_bc.h"
39 #include "sb_shader.h"
40
41 #include "sb_pass.h"
42
43 namespace r600_sb {
44
45 using std::cerr;
46
47 int gcm::run() {
48
49 GCM_DUMP( cerr << "==== GCM ==== \n"; sh.dump_ir(); );
50
51 collect_instructions(sh.root, true);
52
53 init_def_count(uses, pending);
54
55 for (node_iterator N, I = pending.begin(), E = pending.end();
56 I != E; I = N) {
57 N = I;
58 ++N;
59 node *o = *I;
60
61 GCM_DUMP(
62 cerr << "pending : ";
63 dump::dump_op(o);
64 cerr << "\n";
65 );
66
67 if (td_is_ready(o)) {
68
69 GCM_DUMP(
70 cerr << " ready: ";
71 dump::dump_op(o);
72 cerr << "\n";
73 );
74 pending.remove_node(o);
75 ready.push_back(o);
76 } else {
77 }
78 }
79
80 sched_early(sh.root);
81
82 if (!pending.empty()) {
83 cerr << "##### gcm_sched_early_pass: unscheduled ops:\n";
84 dump::dump_op(pending.front());
85 }
86
87 assert(pending.empty());
88
89 GCM_DUMP( sh.dump_ir(); );
90
91 GCM_DUMP( cerr << "\n\n ############## gcm late\n\n"; );
92
93 collect_instructions(sh.root, false);
94
95 init_use_count(uses, pending);
96
97 sched_late(sh.root);
98 if (!pending.empty()) {
99 cerr << "##### gcm_sched_late_pass: unscheduled ops:\n";
100 dump::dump_op(pending.front());
101 }
102
103 assert(ucs_level == 0);
104 assert(pending.empty());
105
106 return 0;
107 }
108
109
110 void gcm::collect_instructions(container_node *c, bool early_pass) {
111 if (c->is_bb()) {
112
113 if (early_pass) {
114 for (node_iterator I = c->begin(), E = c->end(); I != E; ++I) {
115 node *n = *I;
116 if (n->flags & NF_DONT_MOVE) {
117 op_info &o = op_map[n];
118 o.top_bb = o.bottom_bb = static_cast<bb_node*>(c);
119 }
120 }
121 }
122
123 pending.append_from(c);
124 return;
125 }
126
127 for (node_iterator I = c->begin(), E = c->end(); I != E; ++I) {
128 if (I->is_container()) {
129 collect_instructions(static_cast<container_node*>(*I), early_pass);
130 }
131 }
132 }
133
134 void gcm::sched_early(container_node *n) {
135
136 region_node *r =
137 (n->type == NT_REGION) ? static_cast<region_node*>(n) : NULL;
138
139 if (r && r->loop_phi) {
140 sched_early(r->loop_phi);
141 }
142
143 for (node_iterator I = n->begin(), E = n->end(); I != E; ++I) {
144 if (I->type == NT_OP) {
145 node *op = *I;
146 if (op->subtype == NST_PHI) {
147 td_release_uses(op->dst);
148 }
149 } else if (I->is_container()) {
150 if (I->subtype == NST_BB) {
151 bb_node* bb = static_cast<bb_node*>(*I);
152 td_sched_bb(bb);
153 } else {
154 sched_early(static_cast<container_node*>(*I));
155 }
156 }
157 }
158
159 if (r && r->phi) {
160 sched_early(r->phi);
161 }
162 }
163
164 void gcm::td_schedule(bb_node *bb, node *n) {
165 GCM_DUMP(
166 cerr << "scheduling : ";
167 dump::dump_op(n);
168 cerr << "\n";
169 );
170 td_release_uses(n->dst);
171
172 bb->push_back(n);
173
174 op_map[n].top_bb = bb;
175
176 }
177
178 void gcm::td_sched_bb(bb_node* bb) {
179 GCM_DUMP(
180 cerr << "td scheduling BB_" << bb->id << "\n";
181 );
182
183 while (!ready.empty()) {
184 for (sq_iterator N, I = ready.begin(), E = ready.end(); I != E;
185 I = N) {
186 N = I; ++N;
187 td_schedule(bb, *I);
188 ready.erase(I);
189 }
190 }
191 }
192
193 bool gcm::td_is_ready(node* n) {
194 return uses[n] == 0;
195 }
196
197 void gcm::td_release_val(value *v) {
198
199 GCM_DUMP(
200 cerr << "td checking uses: ";
201 dump::dump_val(v);
202 cerr << "\n";
203 );
204
205 use_info *u = v->uses;
206 while (u) {
207 if (u->op->parent != &pending) {
208 u = u->next;
209 continue;
210 }
211
212 GCM_DUMP(
213 cerr << "td used in ";
214 dump::dump_op(u->op);
215 cerr << "\n";
216 );
217
218 if (--uses[u->op] == 0) {
219 GCM_DUMP(
220 cerr << "td released : ";
221 dump::dump_op(u->op);
222 cerr << "\n";
223 );
224
225 pending.remove_node(u->op);
226 ready.push_back(u->op);
227 }
228 u = u->next;
229 }
230
231 }
232
233 void gcm::td_release_uses(vvec& v) {
234 for (vvec::iterator I = v.begin(), E = v.end(); I != E; ++I) {
235 value *v = *I;
236 if (!v)
237 continue;
238
239 if (v->is_rel())
240 td_release_uses(v->mdef);
241 else
242 td_release_val(v);
243 }
244 }
245
246 void gcm::sched_late(container_node *n) {
247
248 bool stack_pushed = false;
249
250 if (n->is_depart()) {
251 depart_node *d = static_cast<depart_node*>(n);
252 push_uc_stack();
253 stack_pushed = true;
254 bu_release_phi_defs(d->target->phi, d->dep_id);
255 } else if (n->is_repeat()) {
256 repeat_node *r = static_cast<repeat_node*>(n);
257 assert(r->target->loop_phi);
258 push_uc_stack();
259 stack_pushed = true;
260 bu_release_phi_defs(r->target->loop_phi, r->rep_id);
261 }
262
263 for (node_riterator I = n->rbegin(), E = n->rend(); I != E; ++I) {
264 if (I->is_container()) {
265 if (I->subtype == NST_BB) {
266 bb_node* bb = static_cast<bb_node*>(*I);
267 bu_sched_bb(bb);
268 } else {
269 sched_late(static_cast<container_node*>(*I));
270 }
271 }
272 }
273
274 if (n->type == NT_IF) {
275 if_node *f = static_cast<if_node*>(n);
276 if (f->cond)
277 pending_defs.push_back(f->cond);
278 } else if (n->type == NT_REGION) {
279 region_node *r = static_cast<region_node*>(n);
280 if (r->loop_phi)
281 bu_release_phi_defs(r->loop_phi, 0);
282 }
283
284 if (stack_pushed)
285 pop_uc_stack();
286
287 }
288
289 void gcm::bu_sched_bb(bb_node* bb) {
290 GCM_DUMP(
291 cerr << "bu scheduling BB_" << bb->id << "\n";
292 );
293
294 bu_bb = bb;
295
296 if (!pending_nodes.empty()) {
297 GCM_DUMP(
298 cerr << "pending nodes:\n";
299 );
300
301 // TODO consider sorting the exports by array_base,
302 // possibly it can improve performance
303
304 for (node_list::iterator I = pending_nodes.begin(),
305 E = pending_nodes.end(); I != E; ++I) {
306 bu_release_op(*I);
307 }
308 pending_nodes.clear();
309 GCM_DUMP(
310 cerr << "pending nodes processed...\n";
311 );
312 }
313
314
315 if (!pending_defs.empty()) {
316 for (vvec::iterator I = pending_defs.begin(), E = pending_defs.end();
317 I != E; ++I) {
318 bu_release_val(*I);
319 }
320 pending_defs.clear();
321 }
322
323 for (sched_queue::iterator N, I = ready_above.begin(), E = ready_above.end();
324 I != E; I = N) {
325 N = I;
326 ++N;
327 node *n = *I;
328 if (op_map[n].bottom_bb == bb) {
329 add_ready(*I);
330 ready_above.erase(I);
331 }
332 }
333
334 unsigned cnt_ready[SQ_NUM];
335
336 container_node *clause = NULL;
337 unsigned last_inst_type = ~0;
338 unsigned last_count = 0;
339
340 bool s = true;
341 while (s) {
342 node *n;
343
344 s = false;
345
346 unsigned ready_mask = 0;
347
348 for (unsigned sq = SQ_CF; sq < SQ_NUM; ++sq) {
349 if (!bu_ready[sq].empty() || !bu_ready_next[sq].empty())
350 ready_mask |= (1 << sq);
351 }
352
353 if (!ready_mask) {
354 for (unsigned sq = SQ_CF; sq < SQ_NUM; ++sq) {
355 if (!bu_ready_early[sq].empty()) {
356 node *n = bu_ready_early[sq].front();
357 bu_ready_early[sq].pop_front();
358 bu_ready[sq].push_back(n);
359 break;
360 }
361 }
362 }
363
364 for (unsigned sq = SQ_CF; sq < SQ_NUM; ++sq) {
365
366 if (!bu_ready_next[sq].empty())
367 bu_ready[sq].splice(bu_ready[sq].end(), bu_ready_next[sq]);
368
369 cnt_ready[sq] = bu_ready[sq].size();
370
371 if ((sq == SQ_TEX || sq == SQ_VTX) &&
372 cnt_ready[sq] < ctx.max_fetch/2 &&
373 !bu_ready_next[SQ_ALU].empty()) {
374 sq = SQ_ALU;
375 --sq;
376 continue;
377 }
378
379 while (!bu_ready[sq].empty()) {
380
381 if (last_inst_type != sq) {
382 clause = NULL;
383 last_count = 0;
384 last_inst_type = sq;
385 }
386
387 n = bu_ready[sq].front();
388
389 // real count (e.g. SAMPLE_G will be expanded to 3 instructions,
390 // 2 SET_GRAD_ + 1 SAMPLE_G
391 unsigned ncnt = 1;
392 if (n->is_fetch_inst() && n->src.size() == 12) {
393 ncnt = 3;
394 }
395
396 if ((sq == SQ_TEX || sq == SQ_VTX) &&
397 ((last_count >= ctx.max_fetch/2 &&
398 check_alu_ready_count(24)) ||
399 last_count + ncnt > ctx.max_fetch))
400 break;
401 else if (sq == SQ_CF && last_count > 4 &&
402 check_alu_ready_count(24))
403 break;
404
405 bu_ready[sq].pop_front();
406
407 if (sq != SQ_CF) {
408 if (!clause) {
409 clause = sh.create_clause(sq == SQ_ALU ?
410 NST_ALU_CLAUSE :
411 sq == SQ_TEX ? NST_TEX_CLAUSE :
412 NST_VTX_CLAUSE);
413 bb->push_front(clause);
414 }
415 } else {
416 clause = bb;
417 }
418
419 bu_schedule(clause, n);
420 s = true;
421 last_count += ncnt;
422 }
423 }
424 }
425
426 bu_bb = NULL;
427
428 GCM_DUMP(
429 cerr << "bu finished scheduling BB_" << bb->id << "\n";
430 );
431 }
432
433 void gcm::bu_release_defs(vvec& v, bool src) {
434 for (vvec::reverse_iterator I = v.rbegin(), E = v.rend(); I != E; ++I) {
435 value *v = *I;
436 if (!v || v->is_readonly())
437 continue;
438
439 if (v->is_rel()) {
440 if (!v->rel->is_readonly())
441 bu_release_val(v->rel);
442 bu_release_defs(v->muse, true);
443 } else if (src)
444 bu_release_val(v);
445 }
446 }
447
448 void gcm::push_uc_stack() {
449 GCM_DUMP(
450 cerr << "pushing use count stack prev_level " << ucs_level
451 << " new level " << (ucs_level + 1) << "\n";
452 );
453 ++ucs_level;
454 if (ucs_level == nuc_stk.size()) {
455 nuc_stk.resize(ucs_level + 1);
456 }
457 else {
458 nuc_stk[ucs_level].clear();
459 }
460 }
461
462 bool gcm::bu_is_ready(node* n) {
463 nuc_map &cm = nuc_stk[ucs_level];
464 nuc_map::iterator F = cm.find(n);
465 unsigned uc = (F == cm.end() ? 0 : F->second);
466 return uc == uses[n];
467 }
468
469 void gcm::bu_schedule(container_node* c, node* n) {
470 GCM_DUMP(
471 cerr << "bu scheduling : ";
472 dump::dump_op(n);
473 cerr << "\n";
474 );
475
476 assert(op_map[n].bottom_bb == bu_bb);
477
478 bu_release_defs(n->src, true);
479 bu_release_defs(n->dst, false);
480
481 c->push_front(n);
482 }
483
484 void gcm::dump_uc_stack() {
485 cerr << "##### uc_stk start ####\n";
486 for (unsigned l = 0; l <= ucs_level; ++l) {
487 nuc_map &m = nuc_stk[l];
488
489 cerr << "nuc_stk[" << l << "] : @" << &m << "\n";
490
491 for (nuc_map::iterator I = m.begin(), E = m.end(); I != E; ++I) {
492 cerr << " uc " << I->second << " for ";
493 dump::dump_op(I->first);
494 cerr << "\n";
495 }
496 }
497 cerr << "##### uc_stk end ####\n";
498 }
499
500 void gcm::pop_uc_stack() {
501 nuc_map &pm = nuc_stk[ucs_level];
502 --ucs_level;
503 nuc_map &cm = nuc_stk[ucs_level];
504
505 GCM_DUMP(
506 cerr << "merging use stack from level " << (ucs_level+1)
507 << " to " << ucs_level << "\n";
508 );
509
510 for (nuc_map::iterator N, I = pm.begin(), E = pm.end(); I != E; ++I) {
511 node *n = I->first;
512
513 GCM_DUMP(
514 cerr << " " << cm[n] << " += " << I->second << " for ";
515 dump::dump_op(n);
516 cerr << "\n";
517 );
518
519 unsigned uc = cm[n] += I->second;
520
521 if (n->parent == &pending && uc == uses[n]) {
522 cm.erase(n);
523 pending_nodes.push_back(n);
524 GCM_DUMP(
525 cerr << "pushed pending_node due to stack pop ";
526 dump::dump_op(n);
527 cerr << "\n";
528 );
529 }
530 }
531 }
532
533 void gcm::bu_find_best_bb(node *n, op_info &oi) {
534
535 GCM_DUMP(
536 cerr << " find best bb : ";
537 dump::dump_op(n);
538 cerr << "\n";
539 );
540
541 if (oi.bottom_bb)
542 return;
543
544 // don't hoist generated copies
545 if (n->flags & NF_DONT_HOIST) {
546 oi.bottom_bb = bu_bb;
547 return;
548 }
549
550 bb_node* best_bb = bu_bb;
551 bb_node* top_bb = oi.top_bb;
552 assert(oi.top_bb && !oi.bottom_bb);
553
554 node *c = best_bb;
555
556 // FIXME top_bb may be located inside the loop so we'll never enter it
557 // in the loop below, and the instruction will be incorrectly placed at the
558 // beginning of the shader.
559 // For now just check if top_bb's loop_level is higher than of
560 // current bb and abort the search for better bb in such case,
561 // but this problem may require more complete (and more expensive) fix
562 if (top_bb->loop_level <= best_bb->loop_level) {
563 while (c && c != top_bb) {
564
565 if (c->prev) {
566 c = c->prev;
567 } else {
568 c = c->parent;
569 if (!c)
570 break;
571 continue;
572 }
573
574 if (c->subtype == NST_BB) {
575 bb_node *bb = static_cast<bb_node*>(c);
576 if (bb->loop_level < best_bb->loop_level)
577 best_bb = bb;
578 }
579 }
580 }
581
582 oi.bottom_bb = best_bb;
583 }
584
585 void gcm::add_ready(node *n) {
586 sched_queue_id sq = sh.get_queue_id(n);
587 if (n->flags & NF_SCHEDULE_EARLY)
588 bu_ready_early[sq].push_back(n);
589 else
590 bu_ready_next[sq].push_back(n);
591 }
592
593 void gcm::bu_release_op(node * n) {
594 op_info &oi = op_map[n];
595
596 GCM_DUMP(
597 cerr << " bu release op ";
598 dump::dump_op(n);
599 );
600
601 nuc_stk[ucs_level].erase(n);
602 pending.remove_node(n);
603
604 bu_find_best_bb(n, oi);
605
606 if (oi.bottom_bb == bu_bb) {
607 GCM_DUMP( cerr << " ready\n";);
608 add_ready(n);
609 } else {
610 GCM_DUMP( cerr << " ready_above\n";);
611 ready_above.push_back(n);
612 }
613 }
614
615 void gcm::bu_release_phi_defs(container_node* p, unsigned op)
616 {
617 for (node_riterator I = p->rbegin(), E = p->rend(); I != E; ++I) {
618 node *o = *I;
619 value *v = o->src[op];
620 if (v && !v->is_readonly())
621 pending_defs.push_back(o->src[op]);
622
623 }
624 }
625
626 unsigned gcm::get_uc_vec(vvec &vv) {
627 unsigned c = 0;
628 for (vvec::iterator I = vv.begin(), E = vv.end(); I != E; ++I) {
629 value *v = *I;
630 if (!v)
631 continue;
632
633 if (v->is_rel())
634 c += get_uc_vec(v->mdef);
635 else
636 c += v->use_count();
637 }
638 return c;
639 }
640
641 void gcm::init_use_count(nuc_map& m, container_node &s) {
642 m.clear();
643 for (node_iterator I = s.begin(), E = s.end(); I != E; ++I) {
644 node *n = *I;
645 unsigned uc = get_uc_vec(n->dst);
646 GCM_DUMP(
647 cerr << "uc " << uc << " ";
648 dump::dump_op(n);
649 cerr << "\n";
650 );
651 if (!uc) {
652 pending_nodes.push_back(n);
653 GCM_DUMP(
654 cerr << "pushed pending_node in init ";
655 dump::dump_op(n);
656 cerr << "\n";
657 );
658
659 } else
660 m[n] = uc;
661 }
662 }
663
664 void gcm::bu_release_val(value* v) {
665 node *n = v->any_def();
666
667 if (n && n->parent == &pending) {
668 unsigned uc = ++nuc_stk[ucs_level][n];
669 unsigned uc2 = uses[n];
670
671 GCM_DUMP(
672 cerr << "release val ";
673 dump::dump_val(v);
674 cerr << " for node ";
675 dump::dump_op(n);
676 cerr << " new uc=" << uc << ", total " << uc2 << "\n";
677 );
678
679 if (uc == uc2)
680 bu_release_op(n);
681 }
682
683 }
684
685 void gcm::init_def_count(nuc_map& m, container_node& s) {
686 m.clear();
687 for (node_iterator I = s.begin(), E = s.end(); I != E; ++I) {
688 node *n = *I;
689 unsigned dc = get_dc_vec(n->src, true) + get_dc_vec(n->dst, false);
690 m[n] = dc;
691
692 GCM_DUMP(
693 cerr << "dc " << dc << " ";
694 dump::dump_op(n);
695 cerr << "\n";
696 );
697 }
698 }
699
700 unsigned gcm::get_dc_vec(vvec& vv, bool src) {
701 unsigned c = 0;
702 for (vvec::iterator I = vv.begin(), E = vv.end(); I != E; ++I) {
703 value *v = *I;
704 if (!v || v->is_readonly())
705 continue;
706
707 if (v->is_rel()) {
708 c += v->rel->def != NULL;
709 c += get_dc_vec(v->muse, true);
710 }
711 else if (src) {
712 c += v->def != NULL;
713 c += v->adef != NULL;
714 }
715 }
716 return c;
717 }
718
719 unsigned gcm::real_alu_count(sched_queue& q, unsigned max) {
720 sq_iterator I(q.begin()), E(q.end());
721 unsigned c = 0;
722
723 while (I != E && c < max) {
724 node *n = *I;
725 if (n->is_alu_inst()) {
726 if (!n->is_copy_mov() || !n->src[0]->is_any_gpr())
727 ++c;
728 } else if (n->is_alu_packed()) {
729 c += static_cast<container_node*>(n)->count();
730 }
731 ++I;
732 }
733
734 return c;
735 }
736
737 bool gcm::check_alu_ready_count(unsigned threshold) {
738 unsigned r = real_alu_count(bu_ready[SQ_ALU], threshold);
739 if (r >= threshold)
740 return true;
741 r += real_alu_count(bu_ready_next[SQ_ALU], threshold - r);
742 return r >= threshold;
743 }
744
745 } // namespace r600_sb