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/ralloc.h"
28 #include "util/bitscan.h"
29 #include "compiler/nir/nir.h"
30 #include "pipe/p_state.h"
35 static void *ppir_node_create_ssa(ppir_block
*block
, ppir_op op
, nir_ssa_def
*ssa
)
37 ppir_node
*node
= ppir_node_create(block
, op
, ssa
->index
, 0);
41 ppir_dest
*dest
= ppir_node_get_dest(node
);
42 dest
->type
= ppir_target_ssa
;
43 dest
->ssa
.num_components
= ssa
->num_components
;
44 dest
->ssa
.live_in
= INT_MAX
;
45 dest
->ssa
.live_out
= 0;
46 dest
->write_mask
= u_bit_consecutive(0, ssa
->num_components
);
48 if (node
->type
== ppir_node_type_load
||
49 node
->type
== ppir_node_type_store
)
50 dest
->ssa
.is_head
= true;
55 static void *ppir_node_create_reg(ppir_block
*block
, ppir_op op
,
56 nir_reg_dest
*reg
, unsigned mask
)
58 ppir_node
*node
= ppir_node_create(block
, op
, reg
->reg
->index
, mask
);
62 ppir_dest
*dest
= ppir_node_get_dest(node
);
64 list_for_each_entry(ppir_reg
, r
, &block
->comp
->reg_list
, list
) {
65 if (r
->index
== reg
->reg
->index
) {
71 dest
->type
= ppir_target_register
;
72 dest
->write_mask
= mask
;
74 if (node
->type
== ppir_node_type_load
||
75 node
->type
== ppir_node_type_store
)
76 dest
->reg
->is_head
= true;
81 static void *ppir_node_create_dest(ppir_block
*block
, ppir_op op
,
82 nir_dest
*dest
, unsigned mask
)
88 return ppir_node_create_ssa(block
, op
, &dest
->ssa
);
90 return ppir_node_create_reg(block
, op
, &dest
->reg
, mask
);
93 return ppir_node_create(block
, op
, index
, 0);
96 static void ppir_node_add_src(ppir_compiler
*comp
, ppir_node
*node
,
97 ppir_src
*ps
, nir_src
*ns
, unsigned mask
)
99 ppir_node
*child
= NULL
;
102 child
= comp
->var_nodes
[ns
->ssa
->index
];
103 /* Clone consts for each successor */
106 child
= ppir_node_clone(node
->block
, child
);
108 /* Clone uniforms and load textures for each block */
109 case ppir_op_load_texture
:
110 case ppir_op_load_uniform
:
111 case ppir_op_load_varying
:
112 if (child
->block
!= node
->block
) {
113 child
= ppir_node_clone(node
->block
, child
);
114 comp
->var_nodes
[ns
->ssa
->index
] = child
;
121 ppir_node_add_dep(node
, child
);
124 nir_register
*reg
= ns
->reg
.reg
;
126 int swizzle
= ps
->swizzle
[u_bit_scan(&mask
)];
127 child
= comp
->var_nodes
[(reg
->index
<< 2) + comp
->reg_base
+ swizzle
];
128 ppir_node_add_dep(node
, child
);
132 ppir_node_target_assign(ps
, child
);
135 static int nir_to_ppir_opcodes
[nir_num_opcodes
] = {
137 [0 ... nir_last_opcode
] = -1,
139 [nir_op_mov
] = ppir_op_mov
,
140 [nir_op_fmul
] = ppir_op_mul
,
141 [nir_op_fabs
] = ppir_op_abs
,
142 [nir_op_fneg
] = ppir_op_neg
,
143 [nir_op_fadd
] = ppir_op_add
,
144 [nir_op_fsum3
] = ppir_op_sum3
,
145 [nir_op_fsum4
] = ppir_op_sum4
,
146 [nir_op_frsq
] = ppir_op_rsqrt
,
147 [nir_op_flog2
] = ppir_op_log2
,
148 [nir_op_fexp2
] = ppir_op_exp2
,
149 [nir_op_fsqrt
] = ppir_op_sqrt
,
150 [nir_op_fsin
] = ppir_op_sin
,
151 [nir_op_fcos
] = ppir_op_cos
,
152 [nir_op_fmax
] = ppir_op_max
,
153 [nir_op_fmin
] = ppir_op_min
,
154 [nir_op_frcp
] = ppir_op_rcp
,
155 [nir_op_ffloor
] = ppir_op_floor
,
156 [nir_op_fceil
] = ppir_op_ceil
,
157 [nir_op_ffract
] = ppir_op_fract
,
158 [nir_op_sge
] = ppir_op_ge
,
159 [nir_op_fge
] = ppir_op_ge
,
160 [nir_op_slt
] = ppir_op_lt
,
161 [nir_op_flt
] = ppir_op_lt
,
162 [nir_op_seq
] = ppir_op_eq
,
163 [nir_op_feq
] = ppir_op_eq
,
164 [nir_op_sne
] = ppir_op_ne
,
165 [nir_op_fne
] = ppir_op_ne
,
166 [nir_op_fcsel
] = ppir_op_select
,
167 [nir_op_inot
] = ppir_op_not
,
168 [nir_op_ftrunc
] = ppir_op_trunc
,
169 [nir_op_fsat
] = ppir_op_sat
,
170 [nir_op_fddx
] = ppir_op_ddx
,
171 [nir_op_fddy
] = ppir_op_ddy
,
174 static ppir_node
*ppir_emit_alu(ppir_block
*block
, nir_instr
*ni
)
176 nir_alu_instr
*instr
= nir_instr_as_alu(ni
);
177 int op
= nir_to_ppir_opcodes
[instr
->op
];
180 ppir_error("unsupported nir_op: %s\n", nir_op_infos
[instr
->op
].name
);
184 ppir_alu_node
*node
= ppir_node_create_dest(block
, op
, &instr
->dest
.dest
,
185 instr
->dest
.write_mask
);
189 ppir_dest
*pd
= &node
->dest
;
190 nir_alu_dest
*nd
= &instr
->dest
;
192 pd
->modifier
= ppir_outmod_clamp_fraction
;
203 src_mask
= pd
->write_mask
;
207 unsigned num_child
= nir_op_infos
[instr
->op
].num_inputs
;
208 node
->num_src
= num_child
;
210 for (int i
= 0; i
< num_child
; i
++) {
211 nir_alu_src
*ns
= instr
->src
+ i
;
212 ppir_src
*ps
= node
->src
+ i
;
213 memcpy(ps
->swizzle
, ns
->swizzle
, sizeof(ps
->swizzle
));
214 ppir_node_add_src(block
->comp
, &node
->node
, ps
, &ns
->src
, src_mask
);
216 ps
->absolute
= ns
->abs
;
217 ps
->negate
= ns
->negate
;
223 static ppir_block
*ppir_block_create(ppir_compiler
*comp
);
225 static bool ppir_emit_discard_block(ppir_compiler
*comp
)
227 ppir_block
*block
= ppir_block_create(comp
);
228 ppir_discard_node
*discard
;
232 comp
->discard_block
= block
;
235 discard
= ppir_node_create(block
, ppir_op_discard
, -1, 0);
237 list_addtail(&discard
->node
.list
, &block
->node_list
);
244 static ppir_node
*ppir_emit_discard_if(ppir_block
*block
, nir_instr
*ni
)
246 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(ni
);
248 ppir_compiler
*comp
= block
->comp
;
249 ppir_branch_node
*branch
;
251 if (!comp
->discard_block
&& !ppir_emit_discard_block(comp
))
254 node
= ppir_node_create(block
, ppir_op_branch
, -1, 0);
257 branch
= ppir_node_to_branch(node
);
259 /* second src and condition will be updated during lowering */
260 ppir_node_add_src(block
->comp
, node
, &branch
->src
[0],
261 &instr
->src
[0], u_bit_consecutive(0, instr
->num_components
));
263 branch
->target
= comp
->discard_block
;
268 static ppir_node
*ppir_emit_discard(ppir_block
*block
, nir_instr
*ni
)
270 ppir_node
*node
= ppir_node_create(block
, ppir_op_discard
, -1, 0);
275 static ppir_node
*ppir_emit_intrinsic(ppir_block
*block
, nir_instr
*ni
)
277 nir_intrinsic_instr
*instr
= nir_instr_as_intrinsic(ni
);
279 ppir_load_node
*lnode
;
280 ppir_store_node
*snode
;
282 switch (instr
->intrinsic
) {
283 case nir_intrinsic_load_input
:
284 if (!instr
->dest
.is_ssa
)
285 mask
= u_bit_consecutive(0, instr
->num_components
);
287 lnode
= ppir_node_create_dest(block
, ppir_op_load_varying
, &instr
->dest
, mask
);
291 lnode
->num_components
= instr
->num_components
;
292 lnode
->index
= nir_intrinsic_base(instr
) * 4 + nir_intrinsic_component(instr
);
295 case nir_intrinsic_load_frag_coord
:
296 case nir_intrinsic_load_point_coord
:
297 case nir_intrinsic_load_front_face
:
298 if (!instr
->dest
.is_ssa
)
299 mask
= u_bit_consecutive(0, instr
->num_components
);
302 switch (instr
->intrinsic
) {
303 case nir_intrinsic_load_frag_coord
:
304 op
= ppir_op_load_fragcoord
;
306 case nir_intrinsic_load_point_coord
:
307 op
= ppir_op_load_pointcoord
;
309 case nir_intrinsic_load_front_face
:
310 op
= ppir_op_load_frontface
;
317 lnode
= ppir_node_create_dest(block
, op
, &instr
->dest
, mask
);
321 lnode
->num_components
= instr
->num_components
;
324 case nir_intrinsic_load_uniform
:
325 if (!instr
->dest
.is_ssa
)
326 mask
= u_bit_consecutive(0, instr
->num_components
);
328 lnode
= ppir_node_create_dest(block
, ppir_op_load_uniform
, &instr
->dest
, mask
);
332 lnode
->num_components
= instr
->num_components
;
333 lnode
->index
= nir_intrinsic_base(instr
);
334 lnode
->index
+= (uint32_t)nir_src_as_float(instr
->src
[0]);
338 case nir_intrinsic_store_output
:
339 snode
= ppir_node_create_dest(block
, ppir_op_store_color
, NULL
, 0);
343 snode
->index
= nir_intrinsic_base(instr
);
345 for (int i
= 0; i
< instr
->num_components
; i
++)
346 snode
->src
.swizzle
[i
] = i
;
348 ppir_node_add_src(block
->comp
, &snode
->node
, &snode
->src
, instr
->src
,
349 u_bit_consecutive(0, instr
->num_components
));
353 case nir_intrinsic_discard
:
354 return ppir_emit_discard(block
, ni
);
356 case nir_intrinsic_discard_if
:
357 return ppir_emit_discard_if(block
, ni
);
360 ppir_error("unsupported nir_intrinsic_instr %s\n",
361 nir_intrinsic_infos
[instr
->intrinsic
].name
);
366 static ppir_node
*ppir_emit_load_const(ppir_block
*block
, nir_instr
*ni
)
368 nir_load_const_instr
*instr
= nir_instr_as_load_const(ni
);
369 ppir_const_node
*node
= ppir_node_create_ssa(block
, ppir_op_const
, &instr
->def
);
373 assert(instr
->def
.bit_size
== 32);
375 for (int i
= 0; i
< instr
->def
.num_components
; i
++)
376 node
->constant
.value
[i
].i
= instr
->value
[i
].i32
;
377 node
->constant
.num
= instr
->def
.num_components
;
382 static ppir_node
*ppir_emit_ssa_undef(ppir_block
*block
, nir_instr
*ni
)
384 ppir_error("nir_ssa_undef_instr not support\n");
388 static ppir_node
*ppir_emit_tex(ppir_block
*block
, nir_instr
*ni
)
390 nir_tex_instr
*instr
= nir_instr_as_tex(ni
);
391 ppir_load_texture_node
*node
;
393 if (instr
->op
!= nir_texop_tex
) {
394 ppir_error("unsupported texop %d\n", instr
->op
);
399 if (!instr
->dest
.is_ssa
)
400 mask
= u_bit_consecutive(0, nir_tex_instr_dest_size(instr
));
402 node
= ppir_node_create_dest(block
, ppir_op_load_texture
, &instr
->dest
, mask
);
406 node
->sampler
= instr
->texture_index
;
408 switch (instr
->sampler_dim
) {
409 case GLSL_SAMPLER_DIM_2D
:
410 case GLSL_SAMPLER_DIM_RECT
:
411 case GLSL_SAMPLER_DIM_EXTERNAL
:
414 ppir_error("unsupported sampler dim: %d\n", instr
->sampler_dim
);
418 node
->sampler_dim
= instr
->sampler_dim
;
420 for (int i
= 0; i
< instr
->coord_components
; i
++)
421 node
->src_coords
.swizzle
[i
] = i
;
423 for (int i
= 0; i
< instr
->num_srcs
; i
++) {
424 switch (instr
->src
[i
].src_type
) {
425 case nir_tex_src_coord
:
426 ppir_node_add_src(block
->comp
, &node
->node
, &node
->src_coords
, &instr
->src
[i
].src
,
427 u_bit_consecutive(0, instr
->coord_components
));
430 ppir_error("unsupported texture source type\n");
439 static ppir_node
*ppir_emit_jump(ppir_block
*block
, nir_instr
*ni
)
441 ppir_error("nir_jump_instr not support\n");
445 static ppir_node
*(*ppir_emit_instr
[nir_instr_type_phi
])(ppir_block
*, nir_instr
*) = {
446 [nir_instr_type_alu
] = ppir_emit_alu
,
447 [nir_instr_type_intrinsic
] = ppir_emit_intrinsic
,
448 [nir_instr_type_load_const
] = ppir_emit_load_const
,
449 [nir_instr_type_ssa_undef
] = ppir_emit_ssa_undef
,
450 [nir_instr_type_tex
] = ppir_emit_tex
,
451 [nir_instr_type_jump
] = ppir_emit_jump
,
454 static ppir_block
*ppir_block_create(ppir_compiler
*comp
)
456 ppir_block
*block
= rzalloc(comp
, ppir_block
);
460 list_inithead(&block
->node_list
);
461 list_inithead(&block
->instr_list
);
466 static bool ppir_emit_block(ppir_compiler
*comp
, nir_block
*nblock
)
468 ppir_block
*block
= ppir_block_create(comp
);
472 list_addtail(&block
->list
, &comp
->block_list
);
475 nir_foreach_instr(instr
, nblock
) {
476 assert(instr
->type
< nir_instr_type_phi
);
477 ppir_node
*node
= ppir_emit_instr
[instr
->type
](block
, instr
);
481 list_addtail(&node
->list
, &block
->node_list
);
487 static bool ppir_emit_if(ppir_compiler
*comp
, nir_if
*nif
)
489 ppir_error("if nir_cf_node not support\n");
493 static bool ppir_emit_loop(ppir_compiler
*comp
, nir_loop
*nloop
)
495 ppir_error("loop nir_cf_node not support\n");
499 static bool ppir_emit_function(ppir_compiler
*comp
, nir_function_impl
*nfunc
)
501 ppir_error("function nir_cf_node not support\n");
505 static bool ppir_emit_cf_list(ppir_compiler
*comp
, struct exec_list
*list
)
507 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
510 switch (node
->type
) {
511 case nir_cf_node_block
:
512 ret
= ppir_emit_block(comp
, nir_cf_node_as_block(node
));
515 ret
= ppir_emit_if(comp
, nir_cf_node_as_if(node
));
517 case nir_cf_node_loop
:
518 ret
= ppir_emit_loop(comp
, nir_cf_node_as_loop(node
));
520 case nir_cf_node_function
:
521 ret
= ppir_emit_function(comp
, nir_cf_node_as_function(node
));
524 ppir_error("unknown NIR node type %d\n", node
->type
);
535 static ppir_compiler
*ppir_compiler_create(void *prog
, unsigned num_reg
, unsigned num_ssa
)
537 ppir_compiler
*comp
= rzalloc_size(
538 prog
, sizeof(*comp
) + ((num_reg
<< 2) + num_ssa
) * sizeof(ppir_node
*));
542 list_inithead(&comp
->block_list
);
543 list_inithead(&comp
->reg_list
);
545 comp
->var_nodes
= (ppir_node
**)(comp
+ 1);
546 comp
->reg_base
= num_ssa
;
551 static void ppir_add_ordering_deps(ppir_compiler
*comp
)
553 /* Some intrinsics do not have explicit dependencies and thus depend
554 * on instructions order. Consider discard_if and store_ouput as
555 * example. If we don't add fake dependency of discard_if to store_output
556 * scheduler may put store_output first and since store_output terminates
557 * shader on Utgard PP, rest of it will never be executed.
558 * Add fake dependencies for discard/branch/store to preserve
561 * TODO: scheduler should schedule discard_if as early as possible otherwise
562 * we may end up with suboptimal code for cases like this:
569 * In this case store depends on discard_if and s4, but since dependencies can
570 * be scheduled in any order it can result in code like this:
572 * instr1: s3 = s1 < s3
573 * instr2: s4 = s1 + s2
574 * instr3: discard_if s3
577 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
578 ppir_node
*prev_node
= NULL
;
579 list_for_each_entry_rev(ppir_node
, node
, &block
->node_list
, list
) {
580 if (prev_node
&& ppir_node_is_root(node
) && node
->op
!= ppir_op_const
) {
581 ppir_node_add_dep(prev_node
, node
);
583 if (node
->op
== ppir_op_discard
||
584 node
->op
== ppir_op_store_color
||
585 node
->op
== ppir_op_store_temp
||
586 node
->op
== ppir_op_branch
) {
593 static void ppir_print_shader_db(struct nir_shader
*nir
, ppir_compiler
*comp
,
594 struct pipe_debug_callback
*debug
)
596 const struct shader_info
*info
= &nir
->info
;
598 int ret
= asprintf(&shaderdb
,
599 "%s shader: %d inst, %d loops, %d:%d spills:fills\n",
600 gl_shader_stage_name(info
->stage
),
601 comp
->cur_instr_index
,
607 if (lima_debug
& LIMA_DEBUG_SHADERDB
)
608 fprintf(stderr
, "SHADER-DB: %s\n", shaderdb
);
610 pipe_debug_message(debug
, SHADER_INFO
, "%s", shaderdb
);
614 static void ppir_add_write_after_read_deps(ppir_compiler
*comp
)
616 list_for_each_entry(ppir_block
, block
, &comp
->block_list
, list
) {
617 list_for_each_entry(ppir_reg
, reg
, &comp
->reg_list
, list
) {
618 ppir_node
*write
= NULL
;
619 list_for_each_entry_rev(ppir_node
, node
, &block
->node_list
, list
) {
620 for (int i
= 0; i
< ppir_node_get_src_num(node
); i
++) {
621 ppir_src
*src
= ppir_node_get_src(node
, i
);
622 if (src
&& src
->type
== ppir_target_register
&&
625 ppir_node_add_dep(write
, node
);
627 ppir_dest
*dest
= ppir_node_get_dest(node
);
628 if (dest
&& dest
->type
== ppir_target_register
&&
636 bool ppir_compile_nir(struct lima_fs_shader_state
*prog
, struct nir_shader
*nir
,
638 struct pipe_debug_callback
*debug
)
640 nir_function_impl
*func
= nir_shader_get_entrypoint(nir
);
641 ppir_compiler
*comp
= ppir_compiler_create(prog
, func
->reg_alloc
, func
->ssa_alloc
);
647 foreach_list_typed(nir_register
, reg
, node
, &func
->registers
) {
648 ppir_reg
*r
= rzalloc(comp
, ppir_reg
);
652 r
->index
= reg
->index
;
653 r
->num_components
= reg
->num_components
;
654 r
->live_in
= INT_MAX
;
657 list_addtail(&r
->list
, &comp
->reg_list
);
660 if (!ppir_emit_cf_list(comp
, &func
->body
))
663 /* If we have discard block add it to the very end */
664 if (comp
->discard_block
)
665 list_addtail(&comp
->discard_block
->list
, &comp
->block_list
);
667 ppir_node_print_prog(comp
);
669 if (!ppir_lower_prog(comp
))
672 ppir_add_ordering_deps(comp
);
673 ppir_add_write_after_read_deps(comp
);
675 ppir_node_print_prog(comp
);
677 if (!ppir_node_to_instr(comp
))
680 if (!ppir_schedule_prog(comp
))
683 if (!ppir_regalloc_prog(comp
))
686 if (!ppir_codegen_prog(comp
))
689 ppir_print_shader_db(nir
, comp
, debug
);