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
);
44 emit_jump(bi_context
*ctx
, nir_jump_instr
*instr
)
46 bi_instruction
*branch
= bi_emit_branch(ctx
);
48 switch (instr
->type
) {
50 branch
->branch
.target
= ctx
->break_block
;
52 case nir_jump_continue
:
53 branch
->branch
.target
= ctx
->continue_block
;
56 unreachable("Unhandled jump type");
59 bi_block_add_successor(ctx
->current_block
, branch
->branch
.target
);
63 emit_instr(bi_context
*ctx
, struct nir_instr
*instr
)
65 switch (instr
->type
) {
67 case nir_instr_type_load_const
:
68 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
71 case nir_instr_type_intrinsic
:
72 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
75 case nir_instr_type_alu
:
76 emit_alu(ctx
, nir_instr_as_alu(instr
));
79 case nir_instr_type_tex
:
80 emit_tex(ctx
, nir_instr_as_tex(instr
));
84 case nir_instr_type_jump
:
85 emit_jump(ctx
, nir_instr_as_jump(instr
));
88 case nir_instr_type_ssa_undef
:
93 //unreachable("Unhandled instruction type");
101 create_empty_block(bi_context
*ctx
)
103 bi_block
*blk
= rzalloc(ctx
, bi_block
);
105 blk
->predecessors
= _mesa_set_create(blk
,
107 _mesa_key_pointer_equal
);
109 blk
->name
= ctx
->block_name_count
++;
115 bi_block_add_successor(bi_block
*block
, bi_block
*successor
)
120 for (unsigned i
= 0; i
< ARRAY_SIZE(block
->successors
); ++i
) {
121 if (block
->successors
[i
]) {
122 if (block
->successors
[i
] == successor
)
128 block
->successors
[i
] = successor
;
129 _mesa_set_add(successor
->predecessors
, block
);
133 unreachable("Too many successors");
137 emit_block(bi_context
*ctx
, nir_block
*block
)
139 if (ctx
->after_block
) {
140 ctx
->current_block
= ctx
->after_block
;
141 ctx
->after_block
= NULL
;
143 ctx
->current_block
= create_empty_block(ctx
);
146 list_addtail(&ctx
->current_block
->link
, &ctx
->blocks
);
147 list_inithead(&ctx
->current_block
->instructions
);
149 nir_foreach_instr(instr
, block
) {
150 emit_instr(ctx
, instr
);
151 ++ctx
->instruction_count
;
154 return ctx
->current_block
;
157 /* Emits an unconditional branch to the end of the current block, returning a
158 * pointer so the user can fill in details */
160 static bi_instruction
*
161 bi_emit_branch(bi_context
*ctx
)
163 bi_instruction branch
= {
166 .cond
= BI_COND_ALWAYS
170 return bi_emit(ctx
, branch
);
173 /* Sets a condition for a branch by examing the NIR condition. If we're
174 * familiar with the condition, we unwrap it to fold it into the branch
175 * instruction. Otherwise, we consume the condition directly. We
176 * generally use 1-bit booleans which allows us to use small types for
181 bi_set_branch_cond(bi_instruction
*branch
, nir_src
*cond
, bool invert
)
183 /* TODO: Try to unwrap instead of always bailing */
184 branch
->src
[0] = bir_src_index(cond
);
185 branch
->src
[1] = BIR_INDEX_ZERO
;
186 branch
->src_types
[0] = branch
->src_types
[1] = nir_type_uint16
;
187 branch
->branch
.cond
= invert
? BI_COND_EQ
: BI_COND_NE
;
191 emit_if(bi_context
*ctx
, nir_if
*nif
)
193 bi_block
*before_block
= ctx
->current_block
;
195 /* Speculatively emit the branch, but we can't fill it in until later */
196 bi_instruction
*then_branch
= bi_emit_branch(ctx
);
197 bi_set_branch_cond(then_branch
, &nif
->condition
, true);
199 /* Emit the two subblocks. */
200 bi_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
201 bi_block
*end_then_block
= ctx
->current_block
;
203 /* Emit a jump from the end of the then block to the end of the else */
204 bi_instruction
*then_exit
= bi_emit_branch(ctx
);
206 /* Emit second block, and check if it's empty */
208 int count_in
= ctx
->instruction_count
;
209 bi_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
210 bi_block
*end_else_block
= ctx
->current_block
;
211 ctx
->after_block
= create_empty_block(ctx
);
213 /* Now that we have the subblocks emitted, fix up the branches */
218 if (ctx
->instruction_count
== count_in
) {
219 /* The else block is empty, so don't emit an exit jump */
220 bi_remove_instruction(then_exit
);
221 then_branch
->branch
.target
= ctx
->after_block
;
223 then_branch
->branch
.target
= else_block
;
224 then_exit
->branch
.target
= ctx
->after_block
;
225 bi_block_add_successor(end_then_block
, then_exit
->branch
.target
);
228 /* Wire up the successors */
230 bi_block_add_successor(before_block
, then_branch
->branch
.target
); /* then_branch */
232 bi_block_add_successor(before_block
, then_block
); /* fallthrough */
233 bi_block_add_successor(end_else_block
, ctx
->after_block
); /* fallthrough */
237 emit_loop(bi_context
*ctx
, nir_loop
*nloop
)
239 /* Remember where we are */
240 bi_block
*start_block
= ctx
->current_block
;
242 bi_block
*saved_break
= ctx
->break_block
;
243 bi_block
*saved_continue
= ctx
->continue_block
;
245 ctx
->continue_block
= create_empty_block(ctx
);
246 ctx
->break_block
= create_empty_block(ctx
);
247 ctx
->after_block
= ctx
->continue_block
;
249 /* Emit the body itself */
250 emit_cf_list(ctx
, &nloop
->body
);
252 /* Branch back to loop back */
253 bi_instruction
*br_back
= bi_emit_branch(ctx
);
254 br_back
->branch
.target
= ctx
->continue_block
;
255 bi_block_add_successor(start_block
, ctx
->continue_block
);
256 bi_block_add_successor(ctx
->current_block
, ctx
->continue_block
);
258 ctx
->after_block
= ctx
->break_block
;
261 ctx
->break_block
= saved_break
;
262 ctx
->continue_block
= saved_continue
;
267 emit_cf_list(bi_context
*ctx
, struct exec_list
*list
)
269 bi_block
*start_block
= NULL
;
271 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
272 switch (node
->type
) {
273 case nir_cf_node_block
: {
274 bi_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
283 emit_if(ctx
, nir_cf_node_as_if(node
));
286 case nir_cf_node_loop
:
287 emit_loop(ctx
, nir_cf_node_as_loop(node
));
291 unreachable("Unknown control flow");
299 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
301 return glsl_count_attribute_slots(type
, false);
305 bi_optimize_nir(nir_shader
*nir
)
308 unsigned lower_flrp
= 16 | 32 | 64;
310 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
311 NIR_PASS(progress
, nir
, nir_lower_idiv
, nir_lower_idiv_fast
);
313 nir_lower_tex_options lower_tex_options
= {
314 .lower_txs_lod
= true,
316 .lower_tex_without_implicit_lod
= true,
320 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
325 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
326 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
328 NIR_PASS(progress
, nir
, nir_copy_prop
);
329 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
330 NIR_PASS(progress
, nir
, nir_opt_dce
);
331 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
332 NIR_PASS(progress
, nir
, nir_opt_cse
);
333 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
334 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
335 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
337 if (lower_flrp
!= 0) {
338 bool lower_flrp_progress
= false;
339 NIR_PASS(lower_flrp_progress
,
343 false /* always_precise */,
344 nir
->options
->lower_ffma
);
345 if (lower_flrp_progress
) {
346 NIR_PASS(progress
, nir
,
347 nir_opt_constant_folding
);
351 /* Nothing should rematerialize any flrps, so we only
352 * need to do this lowering once.
357 NIR_PASS(progress
, nir
, nir_opt_undef
);
358 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
361 nir_var_function_temp
);
364 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
366 /* Take us out of SSA */
367 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
368 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
372 bifrost_compile_shader_nir(nir_shader
*nir
, bifrost_program
*program
, unsigned product_id
)
374 bi_context
*ctx
= rzalloc(NULL
, bi_context
);
376 ctx
->stage
= nir
->info
.stage
;
377 ctx
->quirks
= bifrost_get_quirks(product_id
);
378 list_inithead(&ctx
->blocks
);
380 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
381 * (so we don't accidentally duplicate the epilogue since mesa/st has
382 * messed with our I/O quite a bit already) */
384 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
386 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
387 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
388 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
391 NIR_PASS_V(nir
, nir_split_var_copies
);
392 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
393 NIR_PASS_V(nir
, nir_lower_var_copies
);
394 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
395 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, 0);
396 NIR_PASS_V(nir
, nir_lower_ssbo
);
398 /* We have to lower ALU to scalar ourselves since viewport
399 * transformations produce vector ops */
400 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
, NULL
, NULL
);
402 bi_optimize_nir(nir
);
403 nir_print_shader(nir
, stdout
);
405 nir_foreach_function(func
, nir
) {
409 emit_cf_list(ctx
, &func
->impl
->body
);
410 break; /* TODO: Multi-function shaders */
413 bi_print_shader(ctx
, stdout
);