2 * Copyright (c) 2017 Lima Project
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 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
27 #include "util/hash_table.h"
28 #include "util/ralloc.h"
29 #include "util/bitscan.h"
30 #include "compiler/nir/nir.h"
31 #include "pipe/p_state.h"
36 static void *ppir_node_create_ssa(ppir_block
*block
, ppir_op op
, nir_ssa_def
*ssa
)
38 ppir_node
*node
= ppir_node_create(block
, op
, ssa
->index
, 0);
42 ppir_dest
*dest
= ppir_node_get_dest(node
);
43 dest
->type
= ppir_target_ssa
;
44 dest
->ssa
.num_components
= ssa
->num_components
;
45 dest
->write_mask
= u_bit_consecutive(0, ssa
->num_components
);
47 if (node
->type
== ppir_node_type_load
||
48 node
->type
== ppir_node_type_store
)
49 dest
->ssa
.is_head
= true;
54 static void *ppir_node_create_reg(ppir_block
*block
, ppir_op op
,
55 nir_register
*reg
, unsigned mask
)
57 ppir_node
*node
= ppir_node_create(block
, op
, reg
->index
, mask
);
61 ppir_dest
*dest
= ppir_node_get_dest(node
);
63 list_for_each_entry(ppir_reg
, r
, &block
->comp
->reg_list
, list
) {
64 if (r
->index
== reg
->index
) {
70 dest
->type
= ppir_target_register
;
71 dest
->write_mask
= mask
;
73 if (node
->type
== ppir_node_type_load
||
74 node
->type
== ppir_node_type_store
)
75 dest
->reg
->is_head
= true;
80 static void *ppir_node_create_dest(ppir_block
*block
, ppir_op op
,
81 nir_dest
*dest
, unsigned mask
)
87 return ppir_node_create_ssa(block
, op
, &dest
->ssa
);
89 return ppir_node_create_reg(block
, op
, dest
->reg
.reg
, mask
);
92 return ppir_node_create(block
, op
, index
, 0);
95 static void ppir_node_add_src(ppir_compiler
*comp
, ppir_node
*node
,
96 ppir_src
*ps
, nir_src
*ns
, unsigned mask
)
98 ppir_node
*child
= NULL
;
101 child
= comp
->var_nodes
[ns
->ssa
->index
];
102 /* Clone consts for each successor */
105 child
= ppir_node_clone(node
->block
, child
);
107 case ppir_op_load_varying
: {
108 bool is_load_coords
= false;
109 if (node
->op
== ppir_op_load_texture
) {
110 nir_tex_src
*nts
= (nir_tex_src
*)ns
;
111 if (nts
->src_type
== nir_tex_src_coord
)
112 is_load_coords
= true;
115 if (!is_load_coords
) {
116 /* Clone varying loads for each block */
117 if (child
->block
!= node
->block
) {
118 ppir_node
*new = ppir_node_clone(node
->block
, child
);
119 /* If we clone it for every block and there is no user of
120 * the original load left, delete the original one. */
121 ppir_delete_if_orphan(node
->block
, child
);
123 comp
->var_nodes
[ns
->ssa
->index
] = child
;
127 /* At least one successor is load_texture, promote it to load_coords
128 * to ensure that is has exactly one successor */
129 child
->op
= ppir_op_load_coords
;
132 case ppir_op_load_uniform
:
133 case ppir_op_load_coords
:
134 case ppir_op_load_coords_reg
:
135 /* Clone uniform and texture coord loads for each block.
136 * Also ensure that each load has a single successor.
137 * Let's do a fetch each time and hope for a cache hit instead
138 * of increasing reg pressure.
140 if (child
->block
!= node
->block
|| !ppir_node_is_root(child
)) {
141 child
= ppir_node_clone(node
->block
, child
);
142 comp
->var_nodes
[ns
->ssa
->index
] = child
;
149 if (child
->op
!= ppir_op_undef
)
150 ppir_node_add_dep(node
, child
, ppir_dep_src
);
153 nir_register
*reg
= ns
->reg
.reg
;
155 int swizzle
= ps
->swizzle
[u_bit_scan(&mask
)];
156 child
= comp
->var_nodes
[(reg
->index
<< 2) + comp
->reg_base
+ swizzle
];
157 /* Reg is read before it was written, create a dummy node for it */
159 child
= ppir_node_create_reg(node
->block
, ppir_op_undef
, reg
,
160 u_bit_consecutive(0, 4));
161 comp
->var_nodes
[(reg
->index
<< 2) + comp
->reg_base
+ swizzle
] = child
;
163 /* Don't add dummies or recursive deps for ops like r1 = r1 + ssa1 */
164 if (child
&& node
!= child
&& child
->op
!= ppir_op_undef
)
165 ppir_node_add_dep(node
, child
, ppir_dep_src
);
169 ppir_node_target_assign(ps
, child
);
172 static int nir_to_ppir_opcodes
[nir_num_opcodes
] = {
174 [0 ... nir_last_opcode
] = -1,
176 [nir_op_mov
] = ppir_op_mov
,
177 [nir_op_fmul
] = ppir_op_mul
,
178 [nir_op_fabs
] = ppir_op_abs
,
179 [nir_op_fneg
] = ppir_op_neg
,
180 [nir_op_fadd
] = ppir_op_add
,
181 [nir_op_fsum3
] = ppir_op_sum3
,
182 [nir_op_fsum4
] = ppir_op_sum4
,
183 [nir_op_frsq
] = ppir_op_rsqrt
,
184 [nir_op_flog2
] = ppir_op_log2
,
185 [nir_op_fexp2
] = ppir_op_exp2
,
186 [nir_op_fsqrt
] = ppir_op_sqrt
,
187 [nir_op_fsin
] = ppir_op_sin
,
188 [nir_op_fcos
] = ppir_op_cos
,
189 [nir_op_fmax
] = ppir_op_max
,
190 [nir_op_fmin
] = ppir_op_min
,
191 [nir_op_frcp
] = ppir_op_rcp
,
192 [nir_op_ffloor
] = ppir_op_floor
,
193 [nir_op_fceil
] = ppir_op_ceil
,
194 [nir_op_ffract
] = ppir_op_fract
,
195 [nir_op_sge
] = ppir_op_ge
,
196 [nir_op_slt
] = ppir_op_lt
,
197 [nir_op_seq
] = ppir_op_eq
,
198 [nir_op_sne
] = ppir_op_ne
,
199 [nir_op_fcsel
] = ppir_op_select
,
200 [nir_op_inot
] = ppir_op_not
,
201 [nir_op_ftrunc
] = ppir_op_trunc
,
202 [nir_op_fsat
] = ppir_op_sat
,
203 [nir_op_fddx
] = ppir_op_ddx
,
204 [nir_op_fddy
] = ppir_op_ddy
,
207 static ppir_node
*ppir_emit_alu(ppir_block
*block
, nir_instr
*ni
)
209 nir_alu_instr
*instr
= nir_instr_as_alu(ni
);
210 int op
= nir_to_ppir_opcodes
[instr
->op
];
213 ppir_error("unsupported nir_op: %s\n", nir_op_infos
[instr
->op
].name
);
217 ppir_alu_node
*node
= ppir_node_create_dest(block
, op
, &instr
->dest
.dest
,
218 instr
->dest
.write_mask
);
222 ppir_dest
*pd
= &node
->dest
;
223 nir_alu_dest
*nd
= &instr
->dest
;
225 pd
->modifier
= ppir_outmod_clamp_fraction
;
236 src_mask
= pd
->write_mask
;
240 unsigned num_child
= nir_op_infos
[instr
->op
].num_inputs
;
241 node
->num_src
= num_child
;
243 for (int i
= 0; i
< num_child
; i
++) {
244 nir_alu_src
*ns
= instr
->src
+ i
;
245 ppir_src
*ps
= node
->src
+ i
;
246 memcpy(ps
->swizzle
, ns
->swizzle
, sizeof(ps
->swizzle
));
247 ppir_node_add_src(block
->comp
, &node
->node
, ps
, &ns
->src
, src_mask
);
249 ps
->absolute
= ns
->abs
;
250 ps
->negate
= ns
->negate
;
256 static ppir_block
*ppir_block_create(ppir_compiler
*comp
);
258 static bool ppir_emit_discard_block(ppir_compiler
*comp
)
260 ppir_block
*block
= ppir_block_create(comp
);
261 ppir_discard_node
*discard
;
265 comp
->discard_block
= block
;
268 discard
= ppir_node_create(block
, ppir_op_discard
, -1, 0);
270 list_addtail(&discard
->node
.list
, &block
->node_list
);
277 static ppir_node
*ppir_emit_discard_if(ppir_block
*block
, nir_instr
*ni
)
279 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(ni
);
281 ppir_compiler
*comp
= block
->comp
;
282 ppir_branch_node
*branch
;
284 if (!comp
->discard_block
&& !ppir_emit_discard_block(comp
))
287 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
290 branch
= ppir_node_to_branch(node
);
292 /* second src and condition will be updated during lowering */
293 ppir_node_add_src(block
->comp
, node
, &branch
->src
[0],
294 &instr
->src
[0], u_bit_consecutive(0, instr
->num_components
));
296 branch
->target
= comp
->discard_block
;
301 static ppir_node
*ppir_emit_discard(ppir_block
*block
, nir_instr
*ni
)
303 ppir_node
*node
= ppir_node_create(block
, ppir_op_discard
, -1, 0);
308 static ppir_node
*ppir_emit_intrinsic(ppir_block
*block
, nir_instr
*ni
)
310 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(ni
);
312 ppir_load_node
*lnode
;
313 ppir_alu_node
*alu_node
;
315 switch (instr
->intrinsic
) {
316 case nir_intrinsic_load_input
:
317 if (!instr
->dest
.is_ssa
)
318 mask
= u_bit_consecutive(0, instr
->num_components
);
320 lnode
= ppir_node_create_dest(block
, ppir_op_load_varying
, &instr
->dest
, mask
);
324 lnode
->num_components
= instr
->num_components
;
325 lnode
->index
= nir_intrinsic_base(instr
) * 4 + nir_intrinsic_component(instr
);
326 if (nir_src_is_const(instr
->src
[0]))
327 lnode
->index
+= (uint32_t)(nir_src_as_float(instr
->src
[0]) * 4);
330 ppir_node_add_src(block
->comp
, &lnode
->node
, &lnode
->src
, instr
->src
, 1);
334 case nir_intrinsic_load_frag_coord
:
335 case nir_intrinsic_load_point_coord
:
336 case nir_intrinsic_load_front_face
:
337 if (!instr
->dest
.is_ssa
)
338 mask
= u_bit_consecutive(0, instr
->num_components
);
341 switch (instr
->intrinsic
) {
342 case nir_intrinsic_load_frag_coord
:
343 op
= ppir_op_load_fragcoord
;
345 case nir_intrinsic_load_point_coord
:
346 op
= ppir_op_load_pointcoord
;
348 case nir_intrinsic_load_front_face
:
349 op
= ppir_op_load_frontface
;
356 lnode
= ppir_node_create_dest(block
, op
, &instr
->dest
, mask
);
360 lnode
->num_components
= instr
->num_components
;
363 case nir_intrinsic_load_uniform
:
364 if (!instr
->dest
.is_ssa
)
365 mask
= u_bit_consecutive(0, instr
->num_components
);
367 lnode
= ppir_node_create_dest(block
, ppir_op_load_uniform
, &instr
->dest
, mask
);
371 lnode
->num_components
= instr
->num_components
;
372 lnode
->index
= nir_intrinsic_base(instr
);
373 if (nir_src_is_const(instr
->src
[0]))
374 lnode
->index
+= (uint32_t)nir_src_as_float(instr
->src
[0]);
377 ppir_node_add_src(block
->comp
, &lnode
->node
, &lnode
->src
, instr
->src
, 1);
382 case nir_intrinsic_store_output
: {
383 alu_node
= ppir_node_create_dest(block
, ppir_op_store_color
, NULL
, 0);
387 ppir_dest
*dest
= ppir_node_get_dest(&alu_node
->node
);
388 dest
->type
= ppir_target_ssa
;
389 dest
->ssa
.num_components
= instr
->num_components
;
391 dest
->write_mask
= u_bit_consecutive(0, instr
->num_components
);
393 alu_node
->num_src
= 1;
395 for (int i
= 0; i
< instr
->num_components
; i
++)
396 alu_node
->src
[0].swizzle
[i
] = i
;
398 ppir_node_add_src(block
->comp
, &alu_node
->node
, alu_node
->src
, instr
->src
,
399 u_bit_consecutive(0, instr
->num_components
));
401 return &alu_node
->node
;
404 case nir_intrinsic_discard
:
405 return ppir_emit_discard(block
, ni
);
407 case nir_intrinsic_discard_if
:
408 return ppir_emit_discard_if(block
, ni
);
411 ppir_error("unsupported nir_intrinsic_instr %s\n",
412 nir_intrinsic_infos
[instr
->intrinsic
].name
);
417 static ppir_node
*ppir_emit_load_const(ppir_block
*block
, nir_instr
*ni
)
419 nir_load_const_instr
*instr
= nir_instr_as_load_const(ni
);
420 ppir_const_node
*node
= ppir_node_create_ssa(block
, ppir_op_const
, &instr
->def
);
424 assert(instr
->def
.bit_size
== 32);
426 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
427 node
->constant
.value
[i
].i
= instr
->value
[i
].i32
;
428 node
->constant
.num
= instr
->def
.num_components
;
433 static ppir_node
*ppir_emit_ssa_undef(ppir_block
*block
, nir_instr
*ni
)
435 nir_ssa_undef_instr
*undef
= nir_instr_as_ssa_undef(ni
);
436 ppir_node
*node
= ppir_node_create_ssa(block
, ppir_op_undef
, &undef
->def
);
439 ppir_alu_node
*alu
= ppir_node_to_alu(node
);
441 ppir_dest
*dest
= &alu
->dest
;
442 dest
->ssa
.undef
= true;
443 ppir_reg
*ssa
= &dest
->ssa
;
445 list_add(&ssa
->list
, &block
->comp
->reg_list
);
450 static ppir_node
*ppir_emit_tex(ppir_block
*block
, nir_instr
*ni
)
452 nir_tex_instr
*instr
= nir_instr_as_tex(ni
);
453 ppir_load_texture_node
*node
;
461 ppir_error("unsupported texop %d\n", instr
->op
);
466 if (!instr
->dest
.is_ssa
)
467 mask
= u_bit_consecutive(0, nir_tex_instr_dest_size(instr
));
469 node
= ppir_node_create_dest(block
, ppir_op_load_texture
, &instr
->dest
, mask
);
473 node
->sampler
= instr
->texture_index
;
475 switch (instr
->sampler_dim
) {
476 case GLSL_SAMPLER_DIM_2D
:
477 case GLSL_SAMPLER_DIM_CUBE
:
478 case GLSL_SAMPLER_DIM_RECT
:
479 case GLSL_SAMPLER_DIM_EXTERNAL
:
482 ppir_error("unsupported sampler dim: %d\n", instr
->sampler_dim
);
486 node
->sampler_dim
= instr
->sampler_dim
;
488 for (int i
= 0; i
< instr
->coord_components
; i
++)
489 node
->src
[0].swizzle
[i
] = i
;
491 for (int i
= 0; i
< instr
->num_srcs
; i
++) {
492 switch (instr
->src
[i
].src_type
) {
493 case nir_tex_src_coord
:
494 ppir_node_add_src(block
->comp
, &node
->node
, &node
->src
[0], &instr
->src
[i
].src
,
495 u_bit_consecutive(0, instr
->coord_components
));
498 case nir_tex_src_bias
:
499 case nir_tex_src_lod
:
500 node
->lod_bias_en
= true;
501 node
->explicit_lod
= (instr
->src
[i
].src_type
== nir_tex_src_lod
);
502 ppir_node_add_src(block
->comp
, &node
->node
, &node
->src
[1], &instr
->src
[i
].src
, 1);
506 ppir_error("unsupported texture source type\n");
514 static ppir_block
*ppir_get_block(ppir_compiler
*comp
, nir_block
*nblock
)
516 ppir_block
*block
= _mesa_hash_table_u64_search(comp
->blocks
, (uint64_t)nblock
);
521 static ppir_node
*ppir_emit_jump(ppir_block
*block
, nir_instr
*ni
)
524 ppir_compiler
*comp
= block
->comp
;
525 ppir_branch_node
*branch
;
526 ppir_block
*jump_block
;
527 nir_jump_instr
*jump
= nir_instr_as_jump(ni
);
529 switch (jump
->type
) {
530 case nir_jump_break
: {
531 assert(comp
->current_block
->successors
[0]);
532 assert(!comp
->current_block
->successors
[1]);
533 jump_block
= comp
->current_block
->successors
[0];
536 case nir_jump_continue
:
537 jump_block
= comp
->loop_cont_block
;
540 ppir_error("nir_jump_instr not support\n");
544 assert(jump_block
!= NULL
);
546 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
549 branch
= ppir_node_to_branch(node
);
553 branch
->target
= jump_block
;
558 static ppir_node
*(*ppir_emit_instr
[nir_instr_type_phi
])(ppir_block
*, nir_instr
*) = {
559 [nir_instr_type_alu
] = ppir_emit_alu
,
560 [nir_instr_type_intrinsic
] = ppir_emit_intrinsic
,
561 [nir_instr_type_load_const
] = ppir_emit_load_const
,
562 [nir_instr_type_ssa_undef
] = ppir_emit_ssa_undef
,
563 [nir_instr_type_tex
] = ppir_emit_tex
,
564 [nir_instr_type_jump
] = ppir_emit_jump
,
567 static ppir_block
*ppir_block_create(ppir_compiler
*comp
)
569 ppir_block
*block
= rzalloc(comp
, ppir_block
);
573 list_inithead(&block
->node_list
);
574 list_inithead(&block
->instr_list
);
581 static bool ppir_emit_block(ppir_compiler
*comp
, nir_block
*nblock
)
583 ppir_block
*block
= ppir_get_block(comp
, nblock
);
585 comp
->current_block
= block
;
587 list_addtail(&block
->list
, &comp
->block_list
);
589 nir_foreach_instr(instr
, nblock
) {
590 assert(instr
->type
< nir_instr_type_phi
);
591 ppir_node
*node
= ppir_emit_instr
[instr
->type
](block
, instr
);
595 list_addtail(&node
->list
, &block
->node_list
);
601 static bool ppir_emit_cf_list(ppir_compiler
*comp
, struct exec_list
*list
);
603 static bool ppir_emit_if(ppir_compiler
*comp
, nir_if
*if_stmt
)
606 ppir_branch_node
*else_branch
, *after_branch
;
607 nir_block
*nir_else_block
= nir_if_first_else_block(if_stmt
);
608 bool empty_else_block
=
609 (nir_else_block
== nir_if_last_else_block(if_stmt
) &&
610 exec_list_is_empty(&nir_else_block
->instr_list
));
611 ppir_block
*block
= comp
->current_block
;
613 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
616 else_branch
= ppir_node_to_branch(node
);
617 ppir_node_add_src(block
->comp
, node
, &else_branch
->src
[0],
618 &if_stmt
->condition
, 1);
619 else_branch
->num_src
= 1;
620 /* Negate condition to minimize branching. We're generating following:
621 * current_block: { ...; if (!statement) branch else_block; }
622 * then_block: { ...; branch after_block; }
623 * else_block: { ... }
624 * after_block: { ... }
626 * or if else list is empty:
627 * block: { if (!statement) branch else_block; }
628 * then_block: { ... }
629 * else_block: after_block: { ... }
631 else_branch
->negate
= true;
632 list_addtail(&else_branch
->node
.list
, &block
->node_list
);
634 ppir_emit_cf_list(comp
, &if_stmt
->then_list
);
635 if (empty_else_block
) {
636 nir_block
*nblock
= nir_if_last_else_block(if_stmt
);
637 assert(nblock
->successors
[0]);
638 assert(!nblock
->successors
[1]);
639 else_branch
->target
= ppir_get_block(comp
, nblock
->successors
[0]);
640 /* Add empty else block to the list */
641 list_addtail(&block
->successors
[1]->list
, &comp
->block_list
);
645 else_branch
->target
= ppir_get_block(comp
, nir_if_first_else_block(if_stmt
));
647 nir_block
*last_then_block
= nir_if_last_then_block(if_stmt
);
648 assert(last_then_block
->successors
[0]);
649 assert(!last_then_block
->successors
[1]);
650 block
= ppir_get_block(comp
, last_then_block
);
651 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
654 after_branch
= ppir_node_to_branch(node
);
656 after_branch
->num_src
= 0;
657 after_branch
->target
= ppir_get_block(comp
, last_then_block
->successors
[0]);
658 /* Target should be after_block, will fixup later */
659 list_addtail(&after_branch
->node
.list
, &block
->node_list
);
661 ppir_emit_cf_list(comp
, &if_stmt
->else_list
);
666 static bool ppir_emit_loop(ppir_compiler
*comp
, nir_loop
*nloop
)
668 ppir_block
*save_loop_cont_block
= comp
->loop_cont_block
;
670 ppir_branch_node
*loop_branch
;
671 nir_block
*loop_last_block
;
674 comp
->loop_cont_block
= ppir_get_block(comp
, nir_loop_first_block(nloop
));
676 ppir_emit_cf_list(comp
, &nloop
->body
);
678 loop_last_block
= nir_loop_last_block(nloop
);
679 block
= ppir_get_block(comp
, loop_last_block
);
680 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
683 loop_branch
= ppir_node_to_branch(node
);
685 loop_branch
->num_src
= 0;
686 loop_branch
->target
= comp
->loop_cont_block
;
687 list_addtail(&loop_branch
->node
.list
, &block
->node_list
);
689 comp
->loop_cont_block
= save_loop_cont_block
;
696 static bool ppir_emit_function(ppir_compiler
*comp
, nir_function_impl
*nfunc
)
698 ppir_error("function nir_cf_node not support\n");
702 static bool ppir_emit_cf_list(ppir_compiler
*comp
, struct exec_list
*list
)
704 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
707 switch (node
->type
) {
708 case nir_cf_node_block
:
709 ret
= ppir_emit_block(comp
, nir_cf_node_as_block(node
));
712 ret
= ppir_emit_if(comp
, nir_cf_node_as_if(node
));
714 case nir_cf_node_loop
:
715 ret
= ppir_emit_loop(comp
, nir_cf_node_as_loop(node
));
717 case nir_cf_node_function
:
718 ret
= ppir_emit_function(comp
, nir_cf_node_as_function(node
));
721 ppir_error("unknown NIR node type %d\n", node
->type
);
732 static ppir_compiler
*ppir_compiler_create(void *prog
, unsigned num_reg
, unsigned num_ssa
)
734 ppir_compiler
*comp
= rzalloc_size(
735 prog
, sizeof(*comp
) + ((num_reg
<< 2) + num_ssa
) * sizeof(ppir_node
*));
739 list_inithead(&comp
->block_list
);
740 list_inithead(&comp
->reg_list
);
741 comp
->blocks
= _mesa_hash_table_u64_create(prog
);
743 comp
->var_nodes
= (ppir_node
**)(comp
+ 1);
744 comp
->reg_base
= num_ssa
;
749 static void ppir_add_ordering_deps(ppir_compiler
*comp
)
751 /* Some intrinsics do not have explicit dependencies and thus depend
752 * on instructions order. Consider discard_if and store_ouput as
753 * example. If we don't add fake dependency of discard_if to store_output
754 * scheduler may put store_output first and since store_output terminates
755 * shader on Utgard PP, rest of it will never be executed.
756 * Add fake dependencies for discard/branch/store to preserve
759 * TODO: scheduler should schedule discard_if as early as possible otherwise
760 * we may end up with suboptimal code for cases like this:
767 * In this case store depends on discard_if and s4, but since dependencies can
768 * be scheduled in any order it can result in code like this:
770 * instr1: s3 = s1 < s3
771 * instr2: s4 = s1 + s2
772 * instr3: discard_if s3
775 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
776 ppir_node
*prev_node
= NULL
;
777 list_for_each_entry_rev(ppir_node
, node
, &block
->node_list
, list
) {
778 if (prev_node
&& ppir_node_is_root(node
) && node
->op
!= ppir_op_const
) {
779 ppir_node_add_dep(prev_node
, node
, ppir_dep_sequence
);
781 if (node
->op
== ppir_op_discard
||
782 node
->op
== ppir_op_store_color
||
783 node
->op
== ppir_op_store_temp
||
784 node
->op
== ppir_op_branch
) {
791 static void ppir_print_shader_db(struct nir_shader
*nir
, ppir_compiler
*comp
,
792 struct pipe_debug_callback
*debug
)
794 const struct shader_info
*info
= &nir
->info
;
796 int ret
= asprintf(&shaderdb
,
797 "%s shader: %d inst, %d loops, %d:%d spills:fills\n",
798 gl_shader_stage_name(info
->stage
),
799 comp
->cur_instr_index
,
805 if (lima_debug
& LIMA_DEBUG_SHADERDB
)
806 fprintf(stderr
, "SHADER-DB: %s\n", shaderdb
);
808 pipe_debug_message(debug
, SHADER_INFO
, "%s", shaderdb
);
812 static void ppir_add_write_after_read_deps(ppir_compiler
*comp
)
814 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
815 list_for_each_entry(ppir_reg
, reg
, &comp
->reg_list
, list
) {
816 ppir_node
*write
= NULL
;
817 list_for_each_entry_rev(ppir_node
, node
, &block
->node_list
, list
) {
818 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
819 ppir_src
*src
= ppir_node_get_src(node
, i
);
820 if (src
&& src
->type
== ppir_target_register
&&
823 ppir_debug("Adding dep %d for write %d\n", node
->index
, write
->index
);
824 ppir_node_add_dep(write
, node
, ppir_dep_write_after_read
);
827 ppir_dest
*dest
= ppir_node_get_dest(node
);
828 if (dest
&& dest
->type
== ppir_target_register
&&
836 bool ppir_compile_nir(struct lima_fs_shader_state
*prog
, struct nir_shader
*nir
,
838 struct pipe_debug_callback
*debug
)
840 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
841 ppir_compiler
*comp
= ppir_compiler_create(prog
, func
->reg_alloc
, func
->ssa_alloc
);
847 /* 1st pass: create ppir blocks */
848 nir_foreach_function(function
, nir
) {
852 nir_foreach_block(nblock
, function
->impl
) {
853 ppir_block
*block
= ppir_block_create(comp
);
856 block
->index
= nblock
->index
;
857 _mesa_hash_table_u64_insert(comp
->blocks
, (uint64_t)nblock
, block
);
861 /* 2nd pass: populate successors */
862 nir_foreach_function(function
, nir
) {
866 nir_foreach_block(nblock
, function
->impl
) {
867 ppir_block
*block
= ppir_get_block(comp
, nblock
);
870 for (int i
= 0; i
< 2; i
++) {
871 if (nblock
->successors
[i
])
872 block
->successors
[i
] = ppir_get_block(comp
, nblock
->successors
[i
]);
877 /* Validate outputs, we support only gl_FragColor */
878 nir_foreach_variable(var
, &nir
->outputs
) {
879 switch (var
->data
.location
) {
880 case FRAG_RESULT_COLOR
:
881 case FRAG_RESULT_DATA0
:
884 ppir_error("unsupported output type\n");
890 foreach_list_typed(nir_register
, reg
, node
, &func
->registers
) {
891 ppir_reg
*r
= rzalloc(comp
, ppir_reg
);
895 r
->index
= reg
->index
;
896 r
->num_components
= reg
->num_components
;
898 list_addtail(&r
->list
, &comp
->reg_list
);
901 if (!ppir_emit_cf_list(comp
, &func
->body
))
904 /* If we have discard block add it to the very end */
905 if (comp
->discard_block
)
906 list_addtail(&comp
->discard_block
->list
, &comp
->block_list
);
908 ppir_node_print_prog(comp
);
910 if (!ppir_lower_prog(comp
))
913 ppir_add_ordering_deps(comp
);
914 ppir_add_write_after_read_deps(comp
);
916 ppir_node_print_prog(comp
);
918 if (!ppir_node_to_instr(comp
))
921 if (!ppir_schedule_prog(comp
))
924 if (!ppir_regalloc_prog(comp
))
927 if (!ppir_codegen_prog(comp
))
930 ppir_print_shader_db(nir
, comp
, debug
);
932 _mesa_hash_table_u64_destroy(comp
->blocks
, NULL
);
937 _mesa_hash_table_u64_destroy(comp
->blocks
, NULL
);