2 * Copyright (C) 2020 Collabora Ltd.
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, sublicense,
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 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 NONINFRINGEMENT. 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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
27 #include "main/mtypes.h"
28 #include "compiler/glsl/glsl_to_nir.h"
29 #include "compiler/nir_types.h"
30 #include "main/imports.h"
31 #include "compiler/nir/nir_builder.h"
33 #include "disassemble.h"
34 #include "bifrost_compile.h"
36 #include "bi_quirks.h"
39 static bi_block
*emit_cf_list(bi_context
*ctx
, struct exec_list
*list
);
40 static bi_instruction
*bi_emit_branch(bi_context
*ctx
);
41 static void bi_block_add_successor(bi_block
*block
, bi_block
*successor
);
42 static void bi_schedule_barrier(bi_context
*ctx
);
45 emit_jump(bi_context
*ctx
, nir_jump_instr
*instr
)
47 bi_instruction
*branch
= bi_emit_branch(ctx
);
49 switch (instr
->type
) {
51 branch
->branch
.target
= ctx
->break_block
;
53 case nir_jump_continue
:
54 branch
->branch
.target
= ctx
->continue_block
;
57 unreachable("Unhandled jump type");
60 bi_block_add_successor(ctx
->current_block
, branch
->branch
.target
);
63 /* Gets a bytemask for a complete vecN write */
65 bi_mask_for_channels_32(unsigned i
)
67 return (1 << (4 * i
)) - 1;
71 bi_load(enum bi_class T
, nir_intrinsic_instr
*instr
)
73 bi_instruction load
= {
75 .writemask
= bi_mask_for_channels_32(instr
->num_components
),
76 .src
= { BIR_INDEX_CONSTANT
},
77 .constant
= { .u64
= nir_intrinsic_base(instr
) },
80 const nir_intrinsic_info
*info
= &nir_intrinsic_infos
[instr
->intrinsic
];
83 load
.dest
= bir_dest_index(&instr
->dest
);
85 if (info
->has_dest
&& info
->index_map
[NIR_INTRINSIC_TYPE
] > 0)
86 load
.dest_type
= nir_intrinsic_type(instr
);
88 nir_src
*offset
= nir_get_io_offset_src(instr
);
90 if (nir_src_is_const(*offset
))
91 load
.constant
.u64
+= nir_src_as_uint(*offset
);
93 load
.src
[0] = bir_src_index(offset
);
99 bi_emit_ld_vary(bi_context
*ctx
, nir_intrinsic_instr
*instr
)
101 bi_instruction ins
= bi_load(BI_LOAD_VAR
, instr
);
102 ins
.load_vary
.interp_mode
= BIFROST_INTERP_DEFAULT
; /* TODO */
103 ins
.load_vary
.reuse
= false; /* TODO */
104 ins
.load_vary
.flat
= instr
->intrinsic
!= nir_intrinsic_load_interpolated_input
;
105 ins
.dest_type
= nir_type_float
| nir_dest_bit_size(instr
->dest
),
110 bi_emit_frag_out(bi_context
*ctx
, nir_intrinsic_instr
*instr
)
112 if (!ctx
->emitted_atest
) {
113 bi_instruction ins
= {
118 bi_schedule_barrier(ctx
);
119 ctx
->emitted_atest
= true;
122 bi_instruction blend
= {
124 .blend_location
= nir_intrinsic_base(instr
),
126 bir_src_index(&instr
->src
[0])
134 bi_schedule_barrier(ctx
);
138 bi_emit_st_vary(bi_context
*ctx
, nir_intrinsic_instr
*instr
)
140 bi_instruction address
= bi_load(BI_LOAD_VAR_ADDRESS
, instr
);
141 address
.dest
= bi_make_temp(ctx
);
142 address
.dest_type
= nir_type_uint64
;
143 address
.writemask
= (1 << 8) - 1;
145 bi_instruction st
= {
146 .type
= BI_STORE_VAR
,
149 bir_src_index(&instr
->src
[0])
156 bi_emit(ctx
, address
);
161 bi_emit_ld_uniform(bi_context
*ctx
, nir_intrinsic_instr
*instr
)
163 bi_instruction ld
= bi_load(BI_LOAD_UNIFORM
, instr
);
164 ld
.src
[1] = BIR_INDEX_ZERO
; /* TODO: UBO index */
169 emit_intrinsic(bi_context
*ctx
, nir_intrinsic_instr
*instr
)
172 switch (instr
->intrinsic
) {
173 case nir_intrinsic_load_barycentric_pixel
:
176 case nir_intrinsic_load_interpolated_input
:
177 case nir_intrinsic_load_input
:
178 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
179 bi_emit_ld_vary(ctx
, instr
);
180 else if (ctx
->stage
== MESA_SHADER_VERTEX
)
181 bi_emit(ctx
, bi_load(BI_LOAD_ATTR
, instr
));
183 unreachable("Unsupported shader stage");
187 case nir_intrinsic_store_output
:
188 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
189 bi_emit_frag_out(ctx
, instr
);
190 else if (ctx
->stage
== MESA_SHADER_VERTEX
)
191 bi_emit_st_vary(ctx
, instr
);
193 unreachable("Unsupported shader stage");
196 case nir_intrinsic_load_uniform
:
197 bi_emit_ld_uniform(ctx
, instr
);
207 emit_load_const(bi_context
*ctx
, nir_load_const_instr
*instr
)
209 /* Make sure we've been lowered */
210 assert(instr
->def
.num_components
== 1);
212 bi_instruction move
= {
214 .dest
= bir_ssa_index(&instr
->def
),
215 .dest_type
= instr
->def
.bit_size
| nir_type_uint
,
216 .writemask
= (1 << (instr
->def
.bit_size
/ 8)) - 1,
221 .u64
= nir_const_value_as_uint(instr
->value
[0], instr
->def
.bit_size
)
229 emit_instr(bi_context
*ctx
, struct nir_instr
*instr
)
231 switch (instr
->type
) {
232 case nir_instr_type_load_const
:
233 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
236 case nir_instr_type_intrinsic
:
237 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
241 case nir_instr_type_alu
:
242 emit_alu(ctx
, nir_instr_as_alu(instr
));
245 case nir_instr_type_tex
:
246 emit_tex(ctx
, nir_instr_as_tex(instr
));
250 case nir_instr_type_jump
:
251 emit_jump(ctx
, nir_instr_as_jump(instr
));
254 case nir_instr_type_ssa_undef
:
259 //unreachable("Unhandled instruction type");
267 create_empty_block(bi_context
*ctx
)
269 bi_block
*blk
= rzalloc(ctx
, bi_block
);
271 blk
->predecessors
= _mesa_set_create(blk
,
273 _mesa_key_pointer_equal
);
275 blk
->name
= ctx
->block_name_count
++;
281 bi_block_add_successor(bi_block
*block
, bi_block
*successor
)
286 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); ++i
) {
287 if (block
->successors
[i
]) {
288 if (block
->successors
[i
] == successor
)
294 block
->successors
[i
] = successor
;
295 _mesa_set_add(successor
->predecessors
, block
);
299 unreachable("Too many successors");
303 bi_schedule_barrier(bi_context
*ctx
)
305 bi_block
*temp
= ctx
->after_block
;
306 ctx
->after_block
= create_empty_block(ctx
);
307 list_addtail(&ctx
->after_block
->link
, &ctx
->blocks
);
308 list_inithead(&ctx
->after_block
->instructions
);
309 bi_block_add_successor(ctx
->current_block
, ctx
->after_block
);
310 ctx
->current_block
= ctx
->after_block
;
311 ctx
->after_block
= temp
;
315 emit_block(bi_context
*ctx
, nir_block
*block
)
317 if (ctx
->after_block
) {
318 ctx
->current_block
= ctx
->after_block
;
319 ctx
->after_block
= NULL
;
321 ctx
->current_block
= create_empty_block(ctx
);
324 list_addtail(&ctx
->current_block
->link
, &ctx
->blocks
);
325 list_inithead(&ctx
->current_block
->instructions
);
327 nir_foreach_instr(instr
, block
) {
328 emit_instr(ctx
, instr
);
329 ++ctx
->instruction_count
;
332 return ctx
->current_block
;
335 /* Emits an unconditional branch to the end of the current block, returning a
336 * pointer so the user can fill in details */
338 static bi_instruction
*
339 bi_emit_branch(bi_context
*ctx
)
341 bi_instruction branch
= {
344 .cond
= BI_COND_ALWAYS
348 return bi_emit(ctx
, branch
);
351 /* Sets a condition for a branch by examing the NIR condition. If we're
352 * familiar with the condition, we unwrap it to fold it into the branch
353 * instruction. Otherwise, we consume the condition directly. We
354 * generally use 1-bit booleans which allows us to use small types for
359 bi_set_branch_cond(bi_instruction
*branch
, nir_src
*cond
, bool invert
)
361 /* TODO: Try to unwrap instead of always bailing */
362 branch
->src
[0] = bir_src_index(cond
);
363 branch
->src
[1] = BIR_INDEX_ZERO
;
364 branch
->src_types
[0] = branch
->src_types
[1] = nir_type_uint16
;
365 branch
->branch
.cond
= invert
? BI_COND_EQ
: BI_COND_NE
;
369 emit_if(bi_context
*ctx
, nir_if
*nif
)
371 bi_block
*before_block
= ctx
->current_block
;
373 /* Speculatively emit the branch, but we can't fill it in until later */
374 bi_instruction
*then_branch
= bi_emit_branch(ctx
);
375 bi_set_branch_cond(then_branch
, &nif
->condition
, true);
377 /* Emit the two subblocks. */
378 bi_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
379 bi_block
*end_then_block
= ctx
->current_block
;
381 /* Emit a jump from the end of the then block to the end of the else */
382 bi_instruction
*then_exit
= bi_emit_branch(ctx
);
384 /* Emit second block, and check if it's empty */
386 int count_in
= ctx
->instruction_count
;
387 bi_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
388 bi_block
*end_else_block
= ctx
->current_block
;
389 ctx
->after_block
= create_empty_block(ctx
);
391 /* Now that we have the subblocks emitted, fix up the branches */
396 if (ctx
->instruction_count
== count_in
) {
397 /* The else block is empty, so don't emit an exit jump */
398 bi_remove_instruction(then_exit
);
399 then_branch
->branch
.target
= ctx
->after_block
;
401 then_branch
->branch
.target
= else_block
;
402 then_exit
->branch
.target
= ctx
->after_block
;
403 bi_block_add_successor(end_then_block
, then_exit
->branch
.target
);
406 /* Wire up the successors */
408 bi_block_add_successor(before_block
, then_branch
->branch
.target
); /* then_branch */
410 bi_block_add_successor(before_block
, then_block
); /* fallthrough */
411 bi_block_add_successor(end_else_block
, ctx
->after_block
); /* fallthrough */
415 emit_loop(bi_context
*ctx
, nir_loop
*nloop
)
417 /* Remember where we are */
418 bi_block
*start_block
= ctx
->current_block
;
420 bi_block
*saved_break
= ctx
->break_block
;
421 bi_block
*saved_continue
= ctx
->continue_block
;
423 ctx
->continue_block
= create_empty_block(ctx
);
424 ctx
->break_block
= create_empty_block(ctx
);
425 ctx
->after_block
= ctx
->continue_block
;
427 /* Emit the body itself */
428 emit_cf_list(ctx
, &nloop
->body
);
430 /* Branch back to loop back */
431 bi_instruction
*br_back
= bi_emit_branch(ctx
);
432 br_back
->branch
.target
= ctx
->continue_block
;
433 bi_block_add_successor(start_block
, ctx
->continue_block
);
434 bi_block_add_successor(ctx
->current_block
, ctx
->continue_block
);
436 ctx
->after_block
= ctx
->break_block
;
439 ctx
->break_block
= saved_break
;
440 ctx
->continue_block
= saved_continue
;
445 emit_cf_list(bi_context
*ctx
, struct exec_list
*list
)
447 bi_block
*start_block
= NULL
;
449 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
450 switch (node
->type
) {
451 case nir_cf_node_block
: {
452 bi_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
461 emit_if(ctx
, nir_cf_node_as_if(node
));
464 case nir_cf_node_loop
:
465 emit_loop(ctx
, nir_cf_node_as_loop(node
));
469 unreachable("Unknown control flow");
477 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
479 return glsl_count_attribute_slots(type
, false);
483 bi_optimize_nir(nir_shader
*nir
)
486 unsigned lower_flrp
= 16 | 32 | 64;
488 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
489 NIR_PASS(progress
, nir
, nir_lower_idiv
, nir_lower_idiv_fast
);
491 nir_lower_tex_options lower_tex_options
= {
492 .lower_txs_lod
= true,
494 .lower_tex_without_implicit_lod
= true,
498 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
499 NIR_PASS(progress
, nir
, nir_lower_alu_to_scalar
, NULL
, NULL
);
500 NIR_PASS(progress
, nir
, nir_lower_load_const_to_scalar
);
505 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
506 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
508 NIR_PASS(progress
, nir
, nir_copy_prop
);
509 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
510 NIR_PASS(progress
, nir
, nir_opt_dce
);
511 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
512 NIR_PASS(progress
, nir
, nir_opt_cse
);
513 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
514 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
515 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
517 if (lower_flrp
!= 0) {
518 bool lower_flrp_progress
= false;
519 NIR_PASS(lower_flrp_progress
,
523 false /* always_precise */,
524 nir
->options
->lower_ffma
);
525 if (lower_flrp_progress
) {
526 NIR_PASS(progress
, nir
,
527 nir_opt_constant_folding
);
531 /* Nothing should rematerialize any flrps, so we only
532 * need to do this lowering once.
537 NIR_PASS(progress
, nir
, nir_opt_undef
);
538 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
541 nir_var_function_temp
);
544 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
545 NIR_PASS(progress
, nir
, nir_lower_alu_to_scalar
, NULL
, NULL
);
546 NIR_PASS(progress
, nir
, nir_lower_load_const_to_scalar
);
548 /* Take us out of SSA */
549 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
550 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
552 /* We're a primary scalar architecture but there's enough vector that
553 * we use a vector IR so let's not also deal with scalar hacks on top
554 * of the vector hacks */
556 NIR_PASS(progress
, nir
, nir_move_vec_src_uses_to_dest
);
557 NIR_PASS(progress
, nir
, nir_lower_vec_to_movs
);
558 NIR_PASS(progress
, nir
, nir_opt_dce
);
562 bifrost_compile_shader_nir(nir_shader
*nir
, bifrost_program
*program
, unsigned product_id
)
564 bi_context
*ctx
= rzalloc(NULL
, bi_context
);
566 ctx
->stage
= nir
->info
.stage
;
567 ctx
->quirks
= bifrost_get_quirks(product_id
);
568 list_inithead(&ctx
->blocks
);
570 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
571 * (so we don't accidentally duplicate the epilogue since mesa/st has
572 * messed with our I/O quite a bit already) */
574 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
576 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
577 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
578 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
581 NIR_PASS_V(nir
, nir_split_var_copies
);
582 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
583 NIR_PASS_V(nir
, nir_lower_var_copies
);
584 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
585 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, 0);
586 NIR_PASS_V(nir
, nir_lower_ssbo
);
588 bi_optimize_nir(nir
);
589 nir_print_shader(nir
, stdout
);
591 nir_foreach_function(func
, nir
) {
595 ctx
->impl
= func
->impl
;
596 emit_cf_list(ctx
, &func
->impl
->body
);
597 break; /* TODO: Multi-function shaders */
600 bi_print_shader(ctx
, stdout
);