2 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
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:
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
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.
30 #define FBC_DUMP(q) do { q } while (0)
36 #include "sb_shader.h"
41 int bc_finalizer::run() {
45 regions_vec
&rv
= sh
.get_regions();
46 for (regions_vec::reverse_iterator I
= rv
.rbegin(), E
= rv
.rend(); I
!= E
;
52 bool loop
= r
->is_loop();
64 // workaround for some problems on r6xx/7xx
65 // add ALU NOP to each vertex shader
66 if (!ctx
.is_egcm() && (sh
.target
== TARGET_VS
|| sh
.target
== TARGET_ES
)) {
67 cf_node
*c
= sh
.create_clause(NST_ALU_CLAUSE
);
69 alu_group_node
*g
= sh
.create_alu_group();
71 alu_node
*a
= sh
.create_alu();
72 a
->bc
.set_op(ALU_OP0_NOP
);
78 sh
.root
->push_back(c
);
80 c
= sh
.create_cf(CF_OP_NOP
);
81 sh
.root
->push_back(c
);
86 if (last_cf
->bc
.op_ptr
->flags
& CF_ALU
) {
87 last_cf
= sh
.create_cf(CF_OP_NOP
);
88 sh
.root
->push_back(last_cf
);
92 last_cf
->insert_after(sh
.create_cf(CF_OP_CF_END
));
94 last_cf
->bc
.end_of_program
= 1;
96 for (unsigned t
= EXP_PIXEL
; t
< EXP_TYPE_COUNT
; ++t
) {
97 cf_node
*le
= last_export
[t
];
99 le
->bc
.set_op(CF_OP_EXPORT_DONE
);
107 void bc_finalizer::finalize_loop(region_node
* r
) {
109 cf_node
*loop_start
= sh
.create_cf(CF_OP_LOOP_START_DX10
);
110 cf_node
*loop_end
= sh
.create_cf(CF_OP_LOOP_END
);
112 loop_start
->jump_after(loop_end
);
113 loop_end
->jump_after(loop_start
);
115 for (depart_vec::iterator I
= r
->departs
.begin(), E
= r
->departs
.end();
117 depart_node
*dep
= *I
;
118 cf_node
*loop_break
= sh
.create_cf(CF_OP_LOOP_BREAK
);
119 loop_break
->jump(loop_end
);
120 dep
->push_back(loop_break
);
124 // FIXME produces unnecessary LOOP_CONTINUE
125 for (repeat_vec::iterator I
= r
->repeats
.begin(), E
= r
->repeats
.end();
127 repeat_node
*rep
= *I
;
128 if (!(rep
->parent
== r
&& rep
->prev
== NULL
)) {
129 cf_node
*loop_cont
= sh
.create_cf(CF_OP_LOOP_CONTINUE
);
130 loop_cont
->jump(loop_end
);
131 rep
->push_back(loop_cont
);
136 r
->push_front(loop_start
);
137 r
->push_back(loop_end
);
140 void bc_finalizer::finalize_if(region_node
* r
) {
144 // expecting the following control flow structure here:
147 // - depart/repeat 1 (it may be depart/repeat for some outer region)
151 // - depart/repeat 2 (possibly for outer region)
153 // - some optional code
156 // - optional <else> code> ...
160 container_node
*repdep1
= static_cast<container_node
*>(r
->first
);
161 assert(repdep1
->is_depart() || repdep1
->is_repeat());
163 if_node
*n_if
= static_cast<if_node
*>(repdep1
->first
);
168 assert(n_if
->is_if());
170 container_node
*repdep2
= static_cast<container_node
*>(n_if
->first
);
171 assert(repdep2
->is_depart() || repdep2
->is_repeat());
173 cf_node
*if_jump
= sh
.create_cf(CF_OP_JUMP
);
174 cf_node
*if_pop
= sh
.create_cf(CF_OP_POP
);
176 if_pop
->bc
.pop_count
= 1;
177 if_pop
->jump_after(if_pop
);
179 r
->push_front(if_jump
);
180 r
->push_back(if_pop
);
182 bool has_else
= n_if
->next
;
185 cf_node
*nelse
= sh
.create_cf(CF_OP_ELSE
);
186 n_if
->insert_after(nelse
);
187 if_jump
->jump(nelse
);
188 nelse
->jump_after(if_pop
);
189 nelse
->bc
.pop_count
= 1;
192 if_jump
->jump_after(if_pop
);
193 if_jump
->bc
.pop_count
= 1;
199 for (depart_vec::iterator I
= r
->departs
.begin(), E
= r
->departs
.end();
204 assert(r
->repeats
.empty());
207 void bc_finalizer::run_on(container_node
* c
) {
209 for (node_iterator I
= c
->begin(), E
= c
->end(); I
!= E
; ++I
) {
212 if (n
->is_alu_group()) {
213 finalize_alu_group(static_cast<alu_group_node
*>(n
));
215 if (n
->is_alu_clause()) {
216 cf_node
*c
= static_cast<cf_node
*>(n
);
218 if (c
->bc
.op
== CF_OP_ALU_PUSH_BEFORE
&& ctx
.is_egcm()) {
219 if (ctx
.stack_workaround_8xx
) {
220 region_node
*r
= c
->get_parent_region();
223 unsigned elems
= get_stack_depth(r
, loops
, ifs
);
224 unsigned dmod1
= elems
% ctx
.stack_entry_size
;
225 unsigned dmod2
= (elems
+ 1) % ctx
.stack_entry_size
;
227 if (elems
&& (!dmod1
|| !dmod2
))
228 c
->flags
|= NF_ALU_STACK_WORKAROUND
;
230 } else if (ctx
.stack_workaround_9xx
) {
231 region_node
*r
= c
->get_parent_region();
234 get_stack_depth(r
, loops
, ifs
);
236 c
->flags
|= NF_ALU_STACK_WORKAROUND
;
240 } else if (n
->is_fetch_inst()) {
241 finalize_fetch(static_cast<fetch_node
*>(n
));
242 } else if (n
->is_cf_inst()) {
243 finalize_cf(static_cast<cf_node
*>(n
));
245 if (n
->is_container())
246 run_on(static_cast<container_node
*>(n
));
251 void bc_finalizer::finalize_alu_group(alu_group_node
* g
) {
253 alu_node
*last
= NULL
;
255 for (node_iterator I
= g
->begin(), E
= g
->end(); I
!= E
; ++I
) {
256 alu_node
*n
= static_cast<alu_node
*>(*I
);
257 unsigned slot
= n
->bc
.slot
;
259 value
*d
= n
->dst
.empty() ? NULL
: n
->dst
[0];
261 if (d
&& d
->is_special_reg()) {
262 assert(n
->bc
.op_ptr
->flags
& AF_MOVA
);
266 sel_chan fdst
= d
? d
->get_final_gpr() : sel_chan(0, 0);
269 assert(fdst
.chan() == slot
|| slot
== SLOT_TRANS
);
272 n
->bc
.dst_gpr
= fdst
.sel();
273 n
->bc
.dst_chan
= d
? fdst
.chan() : slot
< SLOT_TRANS
? slot
: 0;
276 if (d
&& d
->is_rel() && d
->rel
&& !d
->rel
->is_const()) {
278 update_ngpr(d
->array
->gpr
.sel() + d
->array
->array_size
-1);
283 n
->bc
.write_mask
= d
!= NULL
;
286 if (n
->bc
.op_ptr
->flags
& AF_PRED
) {
287 n
->bc
.update_pred
= (n
->dst
[1] != NULL
);
288 n
->bc
.update_exec_mask
= (n
->dst
[2] != NULL
);
291 // FIXME handle predication here
292 n
->bc
.pred_sel
= PRED_SEL_OFF
;
294 update_ngpr(n
->bc
.dst_gpr
);
296 finalize_alu_src(g
, n
);
304 void bc_finalizer::finalize_alu_src(alu_group_node
* g
, alu_node
* a
) {
308 sblog
<< "finalize_alu_src: ";
315 for (vvec::iterator I
= sv
.begin(), E
= sv
.end(); I
!= E
; ++I
, ++si
) {
319 bc_alu_src
&src
= a
->bc
.src
[si
];
327 sc
= v
->get_final_gpr();
329 src
.chan
= sc
.chan();
330 if (!v
->rel
->is_const()) {
332 update_ngpr(v
->array
->gpr
.sel() + v
->array
->array_size
-1);
338 gpr
= v
->get_final_gpr();
340 src
.chan
= gpr
.chan();
341 update_ngpr(src
.sel
);
344 src
.sel
= v
->gpr
.sel();
345 src
.chan
= v
->gpr
.chan();
346 update_ngpr(src
.sel
);
350 literal lv
= v
->literal_value
;
353 if (lv
== literal(0))
355 else if (lv
== literal(0.5f
))
356 src
.sel
= ALU_SRC_0_5
;
357 else if (lv
== literal(1.0f
))
359 else if (lv
== literal(1))
360 src
.sel
= ALU_SRC_1_INT
;
361 else if (lv
== literal(-1))
362 src
.sel
= ALU_SRC_M_1_INT
;
364 src
.sel
= ALU_SRC_LITERAL
;
365 src
.chan
= g
->literal_chan(lv
);
371 cf_node
*clause
= static_cast<cf_node
*>(g
->parent
);
372 assert(clause
->is_alu_clause());
373 sel_chan k
= translate_kcache(clause
, v
);
375 assert(k
&& "kcache translation failed");
382 case VLK_SPECIAL_CONST
:
383 src
.sel
= v
->select
.sel();
384 src
.chan
= v
->select
.chan();
387 assert(!"unknown value kind");
393 a
->bc
.src
[si
++].sel
= 0;
397 void bc_finalizer::emit_set_grad(fetch_node
* f
) {
399 assert(f
->src
.size() == 12);
400 unsigned ops
[2] = { FETCH_OP_SET_GRADIENTS_V
, FETCH_OP_SET_GRADIENTS_H
};
402 unsigned arg_start
= 0;
404 for (unsigned op
= 0; op
< 2; ++op
) {
405 fetch_node
*n
= sh
.create_fetch();
406 n
->bc
.set_op(ops
[op
]);
408 // FIXME extract this loop into a separate method and reuse it
414 for (unsigned chan
= 0; chan
< 4; ++chan
) {
416 n
->bc
.dst_sel
[chan
] = SEL_MASK
;
418 unsigned sel
= SEL_MASK
;
420 value
*v
= f
->src
[arg_start
+ chan
];
422 if (!v
|| v
->is_undef()) {
424 } else if (v
->is_const()) {
425 literal l
= v
->literal_value
;
428 else if (l
== literal(1.0f
))
431 sblog
<< "invalid fetch constant operand " << chan
<< " ";
437 } else if (v
->is_any_gpr()) {
438 unsigned vreg
= v
->gpr
.sel();
439 unsigned vchan
= v
->gpr
.chan();
443 else if ((unsigned)reg
!= vreg
) {
444 sblog
<< "invalid fetch source operand " << chan
<< " ";
453 sblog
<< "invalid fetch source operand " << chan
<< " ";
459 n
->bc
.src_sel
[chan
] = sel
;
465 n
->bc
.src_gpr
= reg
>= 0 ? reg
: 0;
472 void bc_finalizer::finalize_fetch(fetch_node
* f
) {
478 unsigned src_count
= 4;
480 unsigned flags
= f
->bc
.op_ptr
->flags
;
482 if (flags
& FF_VTX
) {
484 } else if (flags
& FF_USEGRAD
) {
488 for (unsigned chan
= 0; chan
< src_count
; ++chan
) {
490 unsigned sel
= f
->bc
.src_sel
[chan
];
495 value
*v
= f
->src
[chan
];
499 } else if (v
->is_const()) {
500 literal l
= v
->literal_value
;
503 else if (l
== literal(1.0f
))
506 sblog
<< "invalid fetch constant operand " << chan
<< " ";
512 } else if (v
->is_any_gpr()) {
513 unsigned vreg
= v
->gpr
.sel();
514 unsigned vchan
= v
->gpr
.chan();
518 else if ((unsigned)reg
!= vreg
) {
519 sblog
<< "invalid fetch source operand " << chan
<< " ";
528 sblog
<< "invalid fetch source operand " << chan
<< " ";
534 f
->bc
.src_sel
[chan
] = sel
;
540 f
->bc
.src_gpr
= reg
>= 0 ? reg
: 0;
546 unsigned dst_swz
[4] = {SEL_MASK
, SEL_MASK
, SEL_MASK
, SEL_MASK
};
548 for (unsigned chan
= 0; chan
< 4; ++chan
) {
550 unsigned sel
= f
->bc
.dst_sel
[chan
];
555 value
*v
= f
->dst
[chan
];
559 if (v
->is_any_gpr()) {
560 unsigned vreg
= v
->gpr
.sel();
561 unsigned vchan
= v
->gpr
.chan();
565 else if ((unsigned)reg
!= vreg
) {
566 sblog
<< "invalid fetch dst operand " << chan
<< " ";
572 dst_swz
[vchan
] = sel
;
575 sblog
<< "invalid fetch dst operand " << chan
<< " ";
583 for (unsigned i
= 0; i
< 4; ++i
)
584 f
->bc
.dst_sel
[i
] = dst_swz
[i
];
591 f
->bc
.dst_gpr
= reg
>= 0 ? reg
: 0;
594 void bc_finalizer::finalize_cf(cf_node
* c
) {
596 unsigned flags
= c
->bc
.op_ptr
->flags
;
598 c
->bc
.end_of_program
= 0;
601 if (flags
& CF_EXP
) {
602 c
->bc
.set_op(CF_OP_EXPORT
);
603 last_export
[c
->bc
.type
] = c
;
607 for (unsigned chan
= 0; chan
< 4; ++chan
) {
609 unsigned sel
= c
->bc
.sel
[chan
];
614 value
*v
= c
->src
[chan
];
618 } else if (v
->is_const()) {
619 literal l
= v
->literal_value
;
622 else if (l
== literal(1.0f
))
625 sblog
<< "invalid export constant operand " << chan
<< " ";
631 } else if (v
->is_any_gpr()) {
632 unsigned vreg
= v
->gpr
.sel();
633 unsigned vchan
= v
->gpr
.chan();
637 else if ((unsigned)reg
!= vreg
) {
638 sblog
<< "invalid export source operand " << chan
<< " ";
647 sblog
<< "invalid export source operand " << chan
<< " ";
653 c
->bc
.sel
[chan
] = sel
;
659 c
->bc
.rw_gpr
= reg
>= 0 ? reg
: 0;
661 } else if (flags
& CF_MEM
) {
666 for (unsigned chan
= 0; chan
< 4; ++chan
) {
667 value
*v
= c
->src
[chan
];
668 if (!v
|| v
->is_undef())
671 if (!v
->is_any_gpr() || v
->gpr
.chan() != chan
) {
672 sblog
<< "invalid source operand " << chan
<< " ";
677 unsigned vreg
= v
->gpr
.sel();
680 else if ((unsigned)reg
!= vreg
) {
681 sblog
<< "invalid source operand " << chan
<< " ";
690 assert(reg
>= 0 && mask
);
695 c
->bc
.rw_gpr
= reg
>= 0 ? reg
: 0;
696 c
->bc
.comp_mask
= mask
;
698 if ((flags
& CF_RAT
) && (c
->bc
.type
& 1)) {
702 for (unsigned chan
= 0; chan
< 4; ++chan
) {
703 value
*v
= c
->src
[4 + chan
];
704 if (!v
|| v
->is_undef())
707 if (!v
->is_any_gpr() || v
->gpr
.chan() != chan
) {
708 sblog
<< "invalid source operand " << chan
<< " ";
713 unsigned vreg
= v
->gpr
.sel();
716 else if ((unsigned)reg
!= vreg
) {
717 sblog
<< "invalid source operand " << chan
<< " ";
729 c
->bc
.index_gpr
= reg
>= 0 ? reg
: 0;
731 } else if (flags
& CF_CALL
) {
732 update_nstack(c
->get_parent_region(), ctx
.wavefront_size
== 16 ? 2 : 1);
736 sel_chan
bc_finalizer::translate_kcache(cf_node
* alu
, value
* v
) {
737 unsigned sel
= v
->select
.sel();
738 unsigned bank
= sel
>> 12;
739 unsigned chan
= v
->select
.chan();
740 static const unsigned kc_base
[] = {128, 160, 256, 288};
744 unsigned line
= sel
>> 4;
746 for (unsigned k
= 0; k
< 4; ++k
) {
747 bc_kcache
&kc
= alu
->bc
.kc
[k
];
749 if (kc
.mode
== KC_LOCK_NONE
)
752 if (kc
.bank
== bank
&& (kc
.addr
== line
||
753 (kc
.mode
== KC_LOCK_2
&& kc
.addr
+ 1 == line
))) {
755 sel
= kc_base
[k
] + (sel
- (kc
.addr
<< 4));
757 return sel_chan(sel
, chan
);
761 assert(!"kcache translation error");
765 void bc_finalizer::update_ngpr(unsigned gpr
) {
766 if (gpr
< MAX_GPR
- ctx
.alu_temp_gprs
&& gpr
>= ngpr
)
770 unsigned bc_finalizer::get_stack_depth(node
*n
, unsigned &loops
,
771 unsigned &ifs
, unsigned add
) {
772 unsigned stack_elements
= add
;
773 bool has_non_wqm_push
= (add
!= 0);
774 region_node
*r
= n
->is_region() ?
775 static_cast<region_node
*>(n
) : n
->get_parent_region();
785 has_non_wqm_push
= true;
787 r
= r
->get_parent_region();
789 stack_elements
+= (loops
* ctx
.stack_entry_size
) + ifs
;
791 // reserve additional elements in some cases
792 switch (ctx
.hw_class
) {
795 // If any non-WQM push is invoked, 2 elements should be reserved.
796 if (has_non_wqm_push
)
799 case HW_CLASS_CAYMAN
:
800 // If any stack operation is invoked, 2 elements should be reserved
804 case HW_CLASS_EVERGREEN
:
805 // According to the docs we need to reserve 1 element for each of the
807 // 1) non-WQM push is used with WQM/LOOP frames on stack
808 // 2) ALU_ELSE_AFTER is used at the point of max stack usage
810 // It was found that the conditions above are not sufficient, there are
811 // other cases where we also need to reserve stack space, that's why
812 // we always reserve 1 stack element if we have non-WQM push on stack.
813 // Condition 2 is ignored for now because we don't use this instruction.
814 if (has_non_wqm_push
)
818 return stack_elements
;
821 void bc_finalizer::update_nstack(region_node
* r
, unsigned add
) {
824 unsigned elems
= r
? get_stack_depth(r
, loops
, ifs
, add
) : add
;
826 // XXX all chips expect this value to be computed using 4 as entry size,
827 // not the real entry size
828 unsigned stack_entries
= (elems
+ 3) >> 2;
830 if (nstack
< stack_entries
)
831 nstack
= stack_entries
;
834 void bc_finalizer::cf_peephole() {
835 if (ctx
.stack_workaround_8xx
|| ctx
.stack_workaround_9xx
) {
836 for (node_iterator N
, I
= sh
.root
->begin(), E
= sh
.root
->end(); I
!= E
;
839 cf_node
*c
= static_cast<cf_node
*>(*I
);
841 if (c
->bc
.op
== CF_OP_ALU_PUSH_BEFORE
&&
842 (c
->flags
& NF_ALU_STACK_WORKAROUND
)) {
843 cf_node
*push
= sh
.create_cf(CF_OP_PUSH
);
844 c
->insert_before(push
);
846 c
->bc
.set_op(CF_OP_ALU
);
851 for (node_iterator N
, I
= sh
.root
->begin(), E
= sh
.root
->end(); I
!= E
;
855 cf_node
*c
= static_cast<cf_node
*>(*I
);
857 if (c
->jump_after_target
) {
858 c
->jump_target
= static_cast<cf_node
*>(c
->jump_target
->next
);
859 c
->jump_after_target
= false;
862 if (c
->is_cf_op(CF_OP_POP
)) {
864 if (p
->is_alu_clause()) {
865 cf_node
*a
= static_cast<cf_node
*>(p
);
867 if (a
->bc
.op
== CF_OP_ALU
) {
868 a
->bc
.set_op(CF_OP_ALU_POP_AFTER
);
872 } else if (c
->is_cf_op(CF_OP_JUMP
) && c
->jump_target
== c
->next
) {
873 // if JUMP is immediately followed by its jump target,
874 // then JUMP is useless and we can eliminate it
880 } // namespace r600_sb