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 void bc_finalizer::insert_rv6xx_load_ar_workaround(alu_group_node
*b4
) {
43 alu_group_node
*g
= sh
.create_alu_group();
44 alu_node
*a
= sh
.create_alu();
46 a
->bc
.set_op(ALU_OP0_NOP
);
53 int bc_finalizer::run() {
57 regions_vec
&rv
= sh
.get_regions();
58 for (regions_vec::reverse_iterator I
= rv
.rbegin(), E
= rv
.rend(); I
!= E
;
64 bool loop
= r
->is_loop();
76 // workaround for some problems on r6xx/7xx
77 // add ALU NOP to each vertex shader
78 if (!ctx
.is_egcm() && (sh
.target
== TARGET_VS
|| sh
.target
== TARGET_ES
)) {
79 cf_node
*c
= sh
.create_clause(NST_ALU_CLAUSE
);
81 alu_group_node
*g
= sh
.create_alu_group();
83 alu_node
*a
= sh
.create_alu();
84 a
->bc
.set_op(ALU_OP0_NOP
);
90 sh
.root
->push_back(c
);
92 c
= sh
.create_cf(CF_OP_NOP
);
93 sh
.root
->push_back(c
);
98 if (!ctx
.is_cayman() && last_cf
->bc
.op_ptr
->flags
& CF_ALU
) {
99 last_cf
= sh
.create_cf(CF_OP_NOP
);
100 sh
.root
->push_back(last_cf
);
103 if (ctx
.is_cayman()) {
105 cf_node
*c
= sh
.create_cf(CF_OP_CF_END
);
106 sh
.root
->push_back(c
);
108 last_cf
->insert_after(sh
.create_cf(CF_OP_CF_END
));
110 last_cf
->bc
.end_of_program
= 1;
112 for (unsigned t
= EXP_PIXEL
; t
< EXP_TYPE_COUNT
; ++t
) {
113 cf_node
*le
= last_export
[t
];
115 le
->bc
.set_op(CF_OP_EXPORT_DONE
);
123 void bc_finalizer::finalize_loop(region_node
* r
) {
127 cf_node
*loop_start
= sh
.create_cf(CF_OP_LOOP_START_DX10
);
128 cf_node
*loop_end
= sh
.create_cf(CF_OP_LOOP_END
);
130 // Update last_cf, but don't overwrite it if it's outside the current loop nest since
131 // it may point to a cf that is later in program order.
132 // The single parent level check is sufficient since finalize_loop() is processed in
133 // reverse order from innermost to outermost loop nest level.
134 if (!last_cf
|| last_cf
->get_parent_region() == r
) {
138 loop_start
->jump_after(loop_end
);
139 loop_end
->jump_after(loop_start
);
141 for (depart_vec::iterator I
= r
->departs
.begin(), E
= r
->departs
.end();
143 depart_node
*dep
= *I
;
144 cf_node
*loop_break
= sh
.create_cf(CF_OP_LOOP_BREAK
);
145 loop_break
->jump(loop_end
);
146 dep
->push_back(loop_break
);
150 // FIXME produces unnecessary LOOP_CONTINUE
151 for (repeat_vec::iterator I
= r
->repeats
.begin(), E
= r
->repeats
.end();
153 repeat_node
*rep
= *I
;
154 if (!(rep
->parent
== r
&& rep
->prev
== NULL
)) {
155 cf_node
*loop_cont
= sh
.create_cf(CF_OP_LOOP_CONTINUE
);
156 loop_cont
->jump(loop_end
);
157 rep
->push_back(loop_cont
);
162 r
->push_front(loop_start
);
163 r
->push_back(loop_end
);
166 void bc_finalizer::finalize_if(region_node
* r
) {
170 // expecting the following control flow structure here:
173 // - depart/repeat 1 (it may be depart/repeat for some outer region)
177 // - depart/repeat 2 (possibly for outer region)
179 // - some optional code
182 // - optional <else> code> ...
186 container_node
*repdep1
= static_cast<container_node
*>(r
->first
);
187 assert(repdep1
->is_depart() || repdep1
->is_repeat());
189 if_node
*n_if
= static_cast<if_node
*>(repdep1
->first
);
194 assert(n_if
->is_if());
196 container_node
*repdep2
= static_cast<container_node
*>(n_if
->first
);
197 assert(repdep2
->is_depart() || repdep2
->is_repeat());
199 cf_node
*if_jump
= sh
.create_cf(CF_OP_JUMP
);
200 cf_node
*if_pop
= sh
.create_cf(CF_OP_POP
);
202 if (!last_cf
|| last_cf
->get_parent_region() == r
) {
205 if_pop
->bc
.pop_count
= 1;
206 if_pop
->jump_after(if_pop
);
208 r
->push_front(if_jump
);
209 r
->push_back(if_pop
);
211 bool has_else
= n_if
->next
;
214 cf_node
*nelse
= sh
.create_cf(CF_OP_ELSE
);
215 n_if
->insert_after(nelse
);
216 if_jump
->jump(nelse
);
217 nelse
->jump_after(if_pop
);
218 nelse
->bc
.pop_count
= 1;
221 if_jump
->jump_after(if_pop
);
222 if_jump
->bc
.pop_count
= 1;
228 for (depart_vec::iterator I
= r
->departs
.begin(), E
= r
->departs
.end();
233 assert(r
->repeats
.empty());
236 void bc_finalizer::run_on(container_node
* c
) {
237 node
*prev_node
= NULL
;
238 for (node_iterator I
= c
->begin(), E
= c
->end(); I
!= E
; ++I
) {
241 if (n
->is_alu_group()) {
242 finalize_alu_group(static_cast<alu_group_node
*>(n
), prev_node
);
244 if (n
->is_alu_clause()) {
245 cf_node
*c
= static_cast<cf_node
*>(n
);
247 if (c
->bc
.op
== CF_OP_ALU_PUSH_BEFORE
&& ctx
.is_egcm()) {
248 if (ctx
.stack_workaround_8xx
) {
249 region_node
*r
= c
->get_parent_region();
252 unsigned elems
= get_stack_depth(r
, loops
, ifs
);
253 unsigned dmod1
= elems
% ctx
.stack_entry_size
;
254 unsigned dmod2
= (elems
+ 1) % ctx
.stack_entry_size
;
256 if (elems
&& (!dmod1
|| !dmod2
))
257 c
->flags
|= NF_ALU_STACK_WORKAROUND
;
259 } else if (ctx
.stack_workaround_9xx
) {
260 region_node
*r
= c
->get_parent_region();
263 get_stack_depth(r
, loops
, ifs
);
265 c
->flags
|= NF_ALU_STACK_WORKAROUND
;
269 } else if (n
->is_fetch_inst()) {
270 finalize_fetch(static_cast<fetch_node
*>(n
));
271 } else if (n
->is_cf_inst()) {
272 finalize_cf(static_cast<cf_node
*>(n
));
274 if (n
->is_container())
275 run_on(static_cast<container_node
*>(n
));
281 void bc_finalizer::finalize_alu_group(alu_group_node
* g
, node
*prev_node
) {
283 alu_node
*last
= NULL
;
284 alu_group_node
*prev_g
= NULL
;
285 bool add_nop
= false;
286 if (prev_node
&& prev_node
->is_alu_group()) {
287 prev_g
= static_cast<alu_group_node
*>(prev_node
);
290 for (node_iterator I
= g
->begin(), E
= g
->end(); I
!= E
; ++I
) {
291 alu_node
*n
= static_cast<alu_node
*>(*I
);
292 unsigned slot
= n
->bc
.slot
;
293 value
*d
= n
->dst
.empty() ? NULL
: n
->dst
[0];
295 if (d
&& d
->is_special_reg()) {
296 assert((n
->bc
.op_ptr
->flags
& AF_MOVA
) || d
->is_geometry_emit());
300 sel_chan fdst
= d
? d
->get_final_gpr() : sel_chan(0, 0);
303 assert(fdst
.chan() == slot
|| slot
== SLOT_TRANS
);
306 if (!(n
->bc
.op_ptr
->flags
& AF_MOVA
&& ctx
.is_cayman()))
307 n
->bc
.dst_gpr
= fdst
.sel();
308 n
->bc
.dst_chan
= d
? fdst
.chan() : slot
< SLOT_TRANS
? slot
: 0;
311 if (d
&& d
->is_rel() && d
->rel
&& !d
->rel
->is_const()) {
313 update_ngpr(d
->array
->gpr
.sel() + d
->array
->array_size
-1);
318 n
->bc
.write_mask
= d
!= NULL
;
321 if (n
->bc
.op_ptr
->flags
& AF_PRED
) {
322 n
->bc
.update_pred
= (n
->dst
[1] != NULL
);
323 n
->bc
.update_exec_mask
= (n
->dst
[2] != NULL
);
326 // FIXME handle predication here
327 n
->bc
.pred_sel
= PRED_SEL_OFF
;
329 update_ngpr(n
->bc
.dst_gpr
);
331 add_nop
|= finalize_alu_src(g
, n
, prev_g
);
337 if (sh
.get_ctx().r6xx_gpr_index_workaround
) {
338 insert_rv6xx_load_ar_workaround(g
);
344 bool bc_finalizer::finalize_alu_src(alu_group_node
* g
, alu_node
* a
, alu_group_node
*prev
) {
346 bool add_nop
= false;
348 sblog
<< "finalize_alu_src: ";
355 for (vvec::iterator I
= sv
.begin(), E
= sv
.end(); I
!= E
; ++I
, ++si
) {
359 bc_alu_src
&src
= a
->bc
.src
[si
];
367 sc
= v
->get_final_gpr();
369 src
.chan
= sc
.chan();
370 if (!v
->rel
->is_const()) {
372 update_ngpr(v
->array
->gpr
.sel() + v
->array
->array_size
-1);
373 if (prev
&& !add_nop
) {
374 for (node_iterator pI
= prev
->begin(), pE
= prev
->end(); pI
!= pE
; ++pI
) {
375 alu_node
*pn
= static_cast<alu_node
*>(*pI
);
376 if (pn
->bc
.dst_gpr
== src
.sel
) {
387 gpr
= v
->get_final_gpr();
389 src
.chan
= gpr
.chan();
390 update_ngpr(src
.sel
);
393 src
.sel
= v
->gpr
.sel();
394 src
.chan
= v
->gpr
.chan();
395 update_ngpr(src
.sel
);
399 literal lv
= v
->literal_value
;
402 if (lv
== literal(0))
404 else if (lv
== literal(0.5f
))
405 src
.sel
= ALU_SRC_0_5
;
406 else if (lv
== literal(1.0f
))
408 else if (lv
== literal(1))
409 src
.sel
= ALU_SRC_1_INT
;
410 else if (lv
== literal(-1))
411 src
.sel
= ALU_SRC_M_1_INT
;
413 src
.sel
= ALU_SRC_LITERAL
;
414 src
.chan
= g
->literal_chan(lv
);
420 cf_node
*clause
= static_cast<cf_node
*>(g
->parent
);
421 assert(clause
->is_alu_clause());
422 sel_chan k
= translate_kcache(clause
, v
);
424 assert(k
&& "kcache translation failed");
431 case VLK_SPECIAL_CONST
:
432 src
.sel
= v
->select
.sel();
433 src
.chan
= v
->select
.chan();
436 assert(!"unknown value kind");
439 if (prev
&& !add_nop
) {
440 for (node_iterator pI
= prev
->begin(), pE
= prev
->end(); pI
!= pE
; ++pI
) {
441 alu_node
*pn
= static_cast<alu_node
*>(*pI
);
442 if (pn
->bc
.dst_rel
) {
443 if (pn
->bc
.dst_gpr
== src
.sel
) {
453 a
->bc
.src
[si
++].sel
= 0;
458 void bc_finalizer::copy_fetch_src(fetch_node
&dst
, fetch_node
&src
, unsigned arg_start
)
462 for (unsigned chan
= 0; chan
< 4; ++chan
) {
464 dst
.bc
.dst_sel
[chan
] = SEL_MASK
;
466 unsigned sel
= SEL_MASK
;
468 value
*v
= src
.src
[arg_start
+ chan
];
470 if (!v
|| v
->is_undef()) {
472 } else if (v
->is_const()) {
473 literal l
= v
->literal_value
;
476 else if (l
== literal(1.0f
))
479 sblog
<< "invalid fetch constant operand " << chan
<< " ";
485 } else if (v
->is_any_gpr()) {
486 unsigned vreg
= v
->gpr
.sel();
487 unsigned vchan
= v
->gpr
.chan();
491 else if ((unsigned)reg
!= vreg
) {
492 sblog
<< "invalid fetch source operand " << chan
<< " ";
501 sblog
<< "invalid fetch source operand " << chan
<< " ";
507 dst
.bc
.src_sel
[chan
] = sel
;
513 dst
.bc
.src_gpr
= reg
>= 0 ? reg
: 0;
516 void bc_finalizer::emit_set_grad(fetch_node
* f
) {
518 assert(f
->src
.size() == 12 || f
->src
.size() == 13);
519 unsigned ops
[2] = { FETCH_OP_SET_GRADIENTS_V
, FETCH_OP_SET_GRADIENTS_H
};
521 unsigned arg_start
= 0;
523 for (unsigned op
= 0; op
< 2; ++op
) {
524 fetch_node
*n
= sh
.create_fetch();
525 n
->bc
.set_op(ops
[op
]);
529 copy_fetch_src(*n
, *f
, arg_start
);
536 void bc_finalizer::emit_set_texture_offsets(fetch_node
&f
) {
537 assert(f
.src
.size() == 8);
539 fetch_node
*n
= sh
.create_fetch();
541 n
->bc
.set_op(FETCH_OP_SET_TEXTURE_OFFSETS
);
543 copy_fetch_src(*n
, f
, 4);
548 void bc_finalizer::finalize_fetch(fetch_node
* f
) {
554 unsigned src_count
= 4;
556 unsigned flags
= f
->bc
.op_ptr
->flags
;
558 if (flags
& FF_VTX
) {
560 } else if (flags
& FF_USEGRAD
) {
562 } else if (flags
& FF_USE_TEXTURE_OFFSETS
) {
563 emit_set_texture_offsets(*f
);
566 for (unsigned chan
= 0; chan
< src_count
; ++chan
) {
568 unsigned sel
= f
->bc
.src_sel
[chan
];
573 value
*v
= f
->src
[chan
];
577 } else if (v
->is_const()) {
578 literal l
= v
->literal_value
;
581 else if (l
== literal(1.0f
))
584 sblog
<< "invalid fetch constant operand " << chan
<< " ";
590 } else if (v
->is_any_gpr()) {
591 unsigned vreg
= v
->gpr
.sel();
592 unsigned vchan
= v
->gpr
.chan();
596 else if ((unsigned)reg
!= vreg
) {
597 sblog
<< "invalid fetch source operand " << chan
<< " ";
606 sblog
<< "invalid fetch source operand " << chan
<< " ";
612 f
->bc
.src_sel
[chan
] = sel
;
618 f
->bc
.src_gpr
= reg
>= 0 ? reg
: 0;
624 unsigned dst_swz
[4] = {SEL_MASK
, SEL_MASK
, SEL_MASK
, SEL_MASK
};
626 for (unsigned chan
= 0; chan
< 4; ++chan
) {
628 unsigned sel
= f
->bc
.dst_sel
[chan
];
633 value
*v
= f
->dst
[chan
];
637 if (v
->is_any_gpr()) {
638 unsigned vreg
= v
->gpr
.sel();
639 unsigned vchan
= v
->gpr
.chan();
643 else if ((unsigned)reg
!= vreg
) {
644 sblog
<< "invalid fetch dst operand " << chan
<< " ";
650 dst_swz
[vchan
] = sel
;
653 sblog
<< "invalid fetch dst operand " << chan
<< " ";
661 for (unsigned i
= 0; i
< 4; ++i
)
662 f
->bc
.dst_sel
[i
] = dst_swz
[i
];
669 f
->bc
.dst_gpr
= reg
>= 0 ? reg
: 0;
672 void bc_finalizer::finalize_cf(cf_node
* c
) {
674 unsigned flags
= c
->bc
.op_ptr
->flags
;
676 c
->bc
.end_of_program
= 0;
679 if (flags
& CF_EXP
) {
680 c
->bc
.set_op(CF_OP_EXPORT
);
681 last_export
[c
->bc
.type
] = c
;
685 for (unsigned chan
= 0; chan
< 4; ++chan
) {
687 unsigned sel
= c
->bc
.sel
[chan
];
692 value
*v
= c
->src
[chan
];
696 } else if (v
->is_const()) {
697 literal l
= v
->literal_value
;
700 else if (l
== literal(1.0f
))
703 sblog
<< "invalid export constant operand " << chan
<< " ";
709 } else if (v
->is_any_gpr()) {
710 unsigned vreg
= v
->gpr
.sel();
711 unsigned vchan
= v
->gpr
.chan();
715 else if ((unsigned)reg
!= vreg
) {
716 sblog
<< "invalid export source operand " << chan
<< " ";
725 sblog
<< "invalid export source operand " << chan
<< " ";
731 c
->bc
.sel
[chan
] = sel
;
737 c
->bc
.rw_gpr
= reg
>= 0 ? reg
: 0;
739 } else if (flags
& CF_MEM
) {
744 for (unsigned chan
= 0; chan
< 4; ++chan
) {
745 value
*v
= c
->src
[chan
];
746 if (!v
|| v
->is_undef())
749 if (!v
->is_any_gpr() || v
->gpr
.chan() != chan
) {
750 sblog
<< "invalid source operand " << chan
<< " ";
755 unsigned vreg
= v
->gpr
.sel();
758 else if ((unsigned)reg
!= vreg
) {
759 sblog
<< "invalid source operand " << chan
<< " ";
771 c
->bc
.rw_gpr
= reg
>= 0 ? reg
: 0;
772 c
->bc
.comp_mask
= mask
;
774 if (((flags
& CF_RAT
) || (!(flags
& CF_STRM
))) && (c
->bc
.type
& 1)) {
778 for (unsigned chan
= 0; chan
< 4; ++chan
) {
779 value
*v
= c
->src
[4 + chan
];
780 if (!v
|| v
->is_undef())
783 if (!v
->is_any_gpr() || v
->gpr
.chan() != chan
) {
784 sblog
<< "invalid source operand " << chan
<< " ";
789 unsigned vreg
= v
->gpr
.sel();
792 else if ((unsigned)reg
!= vreg
) {
793 sblog
<< "invalid source operand " << chan
<< " ";
805 c
->bc
.index_gpr
= reg
>= 0 ? reg
: 0;
807 } else if (flags
& CF_CALL
) {
808 update_nstack(c
->get_parent_region(), ctx
.wavefront_size
== 16 ? 2 : 1);
812 sel_chan
bc_finalizer::translate_kcache(cf_node
* alu
, value
* v
) {
813 unsigned sel
= v
->select
.kcache_sel();
814 unsigned bank
= v
->select
.kcache_bank();
815 unsigned chan
= v
->select
.chan();
816 static const unsigned kc_base
[] = {128, 160, 256, 288};
820 unsigned line
= sel
>> 4;
822 for (unsigned k
= 0; k
< 4; ++k
) {
823 bc_kcache
&kc
= alu
->bc
.kc
[k
];
825 if (kc
.mode
== KC_LOCK_NONE
)
828 if (kc
.bank
== bank
&& (kc
.addr
== line
||
829 (kc
.mode
== KC_LOCK_2
&& kc
.addr
+ 1 == line
))) {
831 sel
= kc_base
[k
] + (sel
- (kc
.addr
<< 4));
833 return sel_chan(sel
, chan
);
837 assert(!"kcache translation error");
841 void bc_finalizer::update_ngpr(unsigned gpr
) {
842 if (gpr
< MAX_GPR
- ctx
.alu_temp_gprs
&& gpr
>= ngpr
)
846 unsigned bc_finalizer::get_stack_depth(node
*n
, unsigned &loops
,
847 unsigned &ifs
, unsigned add
) {
848 unsigned stack_elements
= add
;
849 bool has_non_wqm_push
= (add
!= 0);
850 region_node
*r
= n
->is_region() ?
851 static_cast<region_node
*>(n
) : n
->get_parent_region();
861 has_non_wqm_push
= true;
863 r
= r
->get_parent_region();
865 stack_elements
+= (loops
* ctx
.stack_entry_size
) + ifs
;
867 // reserve additional elements in some cases
868 switch (ctx
.hw_class
) {
871 // If any non-WQM push is invoked, 2 elements should be reserved.
872 if (has_non_wqm_push
)
875 case HW_CLASS_CAYMAN
:
876 // If any stack operation is invoked, 2 elements should be reserved
880 case HW_CLASS_EVERGREEN
:
881 // According to the docs we need to reserve 1 element for each of the
883 // 1) non-WQM push is used with WQM/LOOP frames on stack
884 // 2) ALU_ELSE_AFTER is used at the point of max stack usage
886 // It was found that the conditions above are not sufficient, there are
887 // other cases where we also need to reserve stack space, that's why
888 // we always reserve 1 stack element if we have non-WQM push on stack.
889 // Condition 2 is ignored for now because we don't use this instruction.
890 if (has_non_wqm_push
)
893 case HW_CLASS_UNKNOWN
:
896 return stack_elements
;
899 void bc_finalizer::update_nstack(region_node
* r
, unsigned add
) {
902 unsigned elems
= r
? get_stack_depth(r
, loops
, ifs
, add
) : add
;
904 // XXX all chips expect this value to be computed using 4 as entry size,
905 // not the real entry size
906 unsigned stack_entries
= (elems
+ 3) >> 2;
908 if (nstack
< stack_entries
)
909 nstack
= stack_entries
;
912 void bc_finalizer::cf_peephole() {
913 if (ctx
.stack_workaround_8xx
|| ctx
.stack_workaround_9xx
) {
914 for (node_iterator N
, I
= sh
.root
->begin(), E
= sh
.root
->end(); I
!= E
;
917 cf_node
*c
= static_cast<cf_node
*>(*I
);
919 if (c
->bc
.op
== CF_OP_ALU_PUSH_BEFORE
&&
920 (c
->flags
& NF_ALU_STACK_WORKAROUND
)) {
921 cf_node
*push
= sh
.create_cf(CF_OP_PUSH
);
922 c
->insert_before(push
);
924 c
->bc
.set_op(CF_OP_ALU
);
929 for (node_iterator N
, I
= sh
.root
->begin(), E
= sh
.root
->end(); I
!= E
;
933 cf_node
*c
= static_cast<cf_node
*>(*I
);
935 if (c
->jump_after_target
) {
936 c
->jump_target
= static_cast<cf_node
*>(c
->jump_target
->next
);
937 c
->jump_after_target
= false;
940 if (c
->is_cf_op(CF_OP_POP
)) {
942 if (p
->is_alu_clause()) {
943 cf_node
*a
= static_cast<cf_node
*>(p
);
945 if (a
->bc
.op
== CF_OP_ALU
) {
946 a
->bc
.set_op(CF_OP_ALU_POP_AFTER
);
950 } else if (c
->is_cf_op(CF_OP_JUMP
) && c
->jump_target
== c
->next
) {
951 // if JUMP is immediately followed by its jump target,
952 // then JUMP is useless and we can eliminate it
958 } // namespace r600_sb