2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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
24 #include <sys/types.h>
33 #include "main/mtypes.h"
34 #include "compiler/glsl/glsl_to_nir.h"
35 #include "compiler/nir_types.h"
36 #include "compiler/nir/nir_builder.h"
37 #include "util/half_float.h"
38 #include "util/u_math.h"
39 #include "util/u_debug.h"
40 #include "util/u_dynarray.h"
41 #include "util/list.h"
42 #include "main/mtypes.h"
45 #include "midgard_nir.h"
46 #include "midgard_compile.h"
47 #include "midgard_ops.h"
50 #include "midgard_quirks.h"
51 #include "panfrost-quirks.h"
52 #include "panfrost/util/pan_lower_framebuffer.h"
54 #include "disassemble.h"
56 static const struct debug_named_value debug_options
[] = {
57 {"msgs", MIDGARD_DBG_MSGS
, "Print debug messages"},
58 {"shaders", MIDGARD_DBG_SHADERS
, "Dump shaders in NIR and MIR"},
59 {"shaderdb", MIDGARD_DBG_SHADERDB
, "Prints shader-db statistics"},
63 DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug
, "MIDGARD_MESA_DEBUG", debug_options
, 0)
65 unsigned SHADER_DB_COUNT
= 0;
67 int midgard_debug
= 0;
69 #define DBG(fmt, ...) \
70 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
71 fprintf(stderr, "%s:%d: "fmt, \
72 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
73 static midgard_block
*
74 create_empty_block(compiler_context
*ctx
)
76 midgard_block
*blk
= rzalloc(ctx
, midgard_block
);
78 blk
->base
.predecessors
= _mesa_set_create(blk
,
80 _mesa_key_pointer_equal
);
82 blk
->base
.name
= ctx
->block_source_count
++;
88 schedule_barrier(compiler_context
*ctx
)
90 midgard_block
*temp
= ctx
->after_block
;
91 ctx
->after_block
= create_empty_block(ctx
);
93 list_addtail(&ctx
->after_block
->base
.link
, &ctx
->blocks
);
94 list_inithead(&ctx
->after_block
->base
.instructions
);
95 pan_block_add_successor(&ctx
->current_block
->base
, &ctx
->after_block
->base
);
96 ctx
->current_block
= ctx
->after_block
;
97 ctx
->after_block
= temp
;
100 /* Helpers to generate midgard_instruction's using macro magic, since every
101 * driver seems to do it that way */
103 #define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
105 #define M_LOAD_STORE(name, store, T) \
106 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
107 midgard_instruction i = { \
108 .type = TAG_LOAD_STORE_4, \
111 .src = { ~0, ~0, ~0, ~0 }, \
112 .swizzle = SWIZZLE_IDENTITY_4, \
113 .op = midgard_op_##name, \
121 i.src_types[0] = T; \
130 #define M_LOAD(name, T) M_LOAD_STORE(name, false, T)
131 #define M_STORE(name, T) M_LOAD_STORE(name, true, T)
133 M_LOAD(ld_attr_32
, nir_type_uint32
);
134 M_LOAD(ld_vary_32
, nir_type_uint32
);
135 M_LOAD(ld_ubo_int4
, nir_type_uint32
);
136 M_LOAD(ld_int4
, nir_type_uint32
);
137 M_STORE(st_int4
, nir_type_uint32
);
138 M_LOAD(ld_color_buffer_32u
, nir_type_uint32
);
139 M_LOAD(ld_color_buffer_as_fp16
, nir_type_float16
);
140 M_LOAD(ld_color_buffer_as_fp32
, nir_type_float32
);
141 M_STORE(st_vary_32
, nir_type_uint32
);
142 M_LOAD(ld_cubemap_coords
, nir_type_uint32
);
143 M_LOAD(ld_compute_id
, nir_type_uint32
);
145 static midgard_instruction
146 v_branch(bool conditional
, bool invert
)
148 midgard_instruction ins
= {
150 .unit
= ALU_ENAB_BRANCH
,
151 .compact_branch
= true,
153 .conditional
= conditional
,
154 .invert_conditional
= invert
157 .src
= { ~0, ~0, ~0, ~0 },
164 attach_constants(compiler_context
*ctx
, midgard_instruction
*ins
, void *constants
, int name
)
166 ins
->has_constants
= true;
167 memcpy(&ins
->constants
, constants
, 16);
171 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
173 return glsl_count_attribute_slots(type
, false);
176 /* Lower fdot2 to a vector multiplication followed by channel addition */
178 midgard_nir_lower_fdot2_body(nir_builder
*b
, nir_alu_instr
*alu
)
180 if (alu
->op
!= nir_op_fdot2
)
183 b
->cursor
= nir_before_instr(&alu
->instr
);
185 nir_ssa_def
*src0
= nir_ssa_for_alu_src(b
, alu
, 0);
186 nir_ssa_def
*src1
= nir_ssa_for_alu_src(b
, alu
, 1);
188 nir_ssa_def
*product
= nir_fmul(b
, src0
, src1
);
190 nir_ssa_def
*sum
= nir_fadd(b
,
191 nir_channel(b
, product
, 0),
192 nir_channel(b
, product
, 1));
194 /* Replace the fdot2 with this sum */
195 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
, nir_src_for_ssa(sum
));
199 midgard_nir_lower_fdot2(nir_shader
*shader
)
201 bool progress
= false;
203 nir_foreach_function(function
, shader
) {
204 if (!function
->impl
) continue;
207 nir_builder
*b
= &_b
;
208 nir_builder_init(b
, function
->impl
);
210 nir_foreach_block(block
, function
->impl
) {
211 nir_foreach_instr_safe(instr
, block
) {
212 if (instr
->type
!= nir_instr_type_alu
) continue;
214 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
215 midgard_nir_lower_fdot2_body(b
, alu
);
221 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);
228 static const nir_variable
*
229 search_var(nir_shader
*nir
, nir_variable_mode mode
, unsigned driver_loc
)
231 nir_foreach_variable_with_modes(var
, nir
, mode
) {
232 if (var
->data
.driver_location
== driver_loc
)
239 /* Midgard can write all of color, depth and stencil in a single writeout
240 * operation, so we merge depth/stencil stores with color stores.
241 * If there are no color stores, we add a write to the "depth RT".
244 midgard_nir_lower_zs_store(nir_shader
*nir
)
246 if (nir
->info
.stage
!= MESA_SHADER_FRAGMENT
)
249 nir_variable
*z_var
= NULL
, *s_var
= NULL
;
251 nir_foreach_shader_out_variable(var
, nir
) {
252 if (var
->data
.location
== FRAG_RESULT_DEPTH
)
254 else if (var
->data
.location
== FRAG_RESULT_STENCIL
)
258 if (!z_var
&& !s_var
)
261 bool progress
= false;
263 nir_foreach_function(function
, nir
) {
264 if (!function
->impl
) continue;
266 nir_intrinsic_instr
*z_store
= NULL
, *s_store
= NULL
;
268 nir_foreach_block(block
, function
->impl
) {
269 nir_foreach_instr_safe(instr
, block
) {
270 if (instr
->type
!= nir_instr_type_intrinsic
)
273 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
274 if (intr
->intrinsic
!= nir_intrinsic_store_output
)
277 if (z_var
&& nir_intrinsic_base(intr
) == z_var
->data
.driver_location
) {
282 if (s_var
&& nir_intrinsic_base(intr
) == s_var
->data
.driver_location
) {
289 if (!z_store
&& !s_store
) continue;
291 bool replaced
= false;
293 nir_foreach_block(block
, function
->impl
) {
294 nir_foreach_instr_safe(instr
, block
) {
295 if (instr
->type
!= nir_instr_type_intrinsic
)
298 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
299 if (intr
->intrinsic
!= nir_intrinsic_store_output
)
302 const nir_variable
*var
= search_var(nir
, nir_var_shader_out
, nir_intrinsic_base(intr
));
305 if (var
->data
.location
!= FRAG_RESULT_COLOR
&&
306 var
->data
.location
< FRAG_RESULT_DATA0
)
312 assert(nir_src_is_const(intr
->src
[1]) && "no indirect outputs");
315 nir_builder_init(&b
, function
->impl
);
317 assert(!z_store
|| z_store
->instr
.block
== instr
->block
);
318 assert(!s_store
|| s_store
->instr
.block
== instr
->block
);
319 b
.cursor
= nir_after_block_before_jump(instr
->block
);
321 nir_intrinsic_instr
*combined_store
;
322 combined_store
= nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_store_combined_output_pan
);
324 combined_store
->num_components
= intr
->src
[0].ssa
->num_components
;
326 nir_intrinsic_set_base(combined_store
, nir_intrinsic_base(intr
));
328 unsigned writeout
= PAN_WRITEOUT_C
;
330 writeout
|= PAN_WRITEOUT_Z
;
332 writeout
|= PAN_WRITEOUT_S
;
334 nir_intrinsic_set_component(combined_store
, writeout
);
336 struct nir_ssa_def
*zero
= nir_imm_int(&b
, 0);
338 struct nir_ssa_def
*src
[4] = {
341 z_store
? z_store
->src
[0].ssa
: zero
,
342 s_store
? s_store
->src
[0].ssa
: zero
,
345 for (int i
= 0; i
< 4; ++i
)
346 combined_store
->src
[i
] = nir_src_for_ssa(src
[i
]);
348 nir_builder_instr_insert(&b
, &combined_store
->instr
);
350 nir_instr_remove(instr
);
356 /* Insert a store to the depth RT (0xff) if needed */
359 nir_builder_init(&b
, function
->impl
);
361 nir_block
*block
= NULL
;
362 if (z_store
&& s_store
)
363 assert(z_store
->instr
.block
== s_store
->instr
.block
);
366 block
= z_store
->instr
.block
;
368 block
= s_store
->instr
.block
;
370 b
.cursor
= nir_after_block_before_jump(block
);
372 nir_intrinsic_instr
*combined_store
;
373 combined_store
= nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_store_combined_output_pan
);
375 combined_store
->num_components
= 4;
379 base
= nir_intrinsic_base(z_store
);
381 base
= nir_intrinsic_base(s_store
);
382 nir_intrinsic_set_base(combined_store
, base
);
384 unsigned writeout
= 0;
386 writeout
|= PAN_WRITEOUT_Z
;
388 writeout
|= PAN_WRITEOUT_S
;
390 nir_intrinsic_set_component(combined_store
, writeout
);
392 struct nir_ssa_def
*zero
= nir_imm_int(&b
, 0);
394 struct nir_ssa_def
*src
[4] = {
395 nir_imm_vec4(&b
, 0, 0, 0, 0),
397 z_store
? z_store
->src
[0].ssa
: zero
,
398 s_store
? s_store
->src
[0].ssa
: zero
,
401 for (int i
= 0; i
< 4; ++i
)
402 combined_store
->src
[i
] = nir_src_for_ssa(src
[i
]);
404 nir_builder_instr_insert(&b
, &combined_store
->instr
);
408 nir_instr_remove(&z_store
->instr
);
411 nir_instr_remove(&s_store
->instr
);
413 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);
420 /* Real writeout stores, which break execution, need to be moved to after
421 * dual-source stores, which are just standard register writes. */
423 midgard_nir_reorder_writeout(nir_shader
*nir
)
425 bool progress
= false;
427 nir_foreach_function(function
, nir
) {
428 if (!function
->impl
) continue;
430 nir_foreach_block(block
, function
->impl
) {
431 nir_instr
*last_writeout
= NULL
;
433 nir_foreach_instr_reverse_safe(instr
, block
) {
434 if (instr
->type
!= nir_instr_type_intrinsic
)
437 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
438 if (intr
->intrinsic
!= nir_intrinsic_store_output
)
441 const nir_variable
*var
= search_var(nir
, nir_var_shader_out
, nir_intrinsic_base(intr
));
443 if (var
->data
.index
) {
445 last_writeout
= instr
;
452 /* This is a real store, so move it to after dual-source stores */
453 exec_node_remove(&instr
->node
);
454 exec_node_insert_after(&last_writeout
->node
, &instr
->node
);
464 /* Flushes undefined values to zero */
467 optimise_nir(nir_shader
*nir
, unsigned quirks
, bool is_blend
)
470 unsigned lower_flrp
=
471 (nir
->options
->lower_flrp16
? 16 : 0) |
472 (nir
->options
->lower_flrp32
? 32 : 0) |
473 (nir
->options
->lower_flrp64
? 64 : 0);
475 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
476 NIR_PASS(progress
, nir
, nir_lower_idiv
, nir_lower_idiv_fast
);
478 nir_lower_tex_options lower_tex_options
= {
479 .lower_txs_lod
= true,
481 .lower_tex_without_implicit_lod
=
482 (quirks
& MIDGARD_EXPLICIT_LOD
),
484 /* TODO: we have native gradient.. */
488 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
490 /* Must lower fdot2 after tex is lowered */
491 NIR_PASS(progress
, nir
, midgard_nir_lower_fdot2
);
493 /* T720 is broken. */
495 if (quirks
& MIDGARD_BROKEN_LOD
)
496 NIR_PASS_V(nir
, midgard_nir_lod_errata
);
498 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_early
);
503 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
504 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
506 NIR_PASS(progress
, nir
, nir_copy_prop
);
507 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
508 NIR_PASS(progress
, nir
, nir_opt_dce
);
509 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
510 NIR_PASS(progress
, nir
, nir_opt_cse
);
511 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
512 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
513 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
515 if (lower_flrp
!= 0) {
516 bool lower_flrp_progress
= false;
517 NIR_PASS(lower_flrp_progress
,
521 false /* always_precise */,
522 nir
->options
->lower_ffma
);
523 if (lower_flrp_progress
) {
524 NIR_PASS(progress
, nir
,
525 nir_opt_constant_folding
);
529 /* Nothing should rematerialize any flrps, so we only
530 * need to do this lowering once.
535 NIR_PASS(progress
, nir
, nir_opt_undef
);
536 NIR_PASS(progress
, nir
, nir_undef_to_zero
);
538 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
541 nir_var_function_temp
);
543 NIR_PASS(progress
, nir
, nir_opt_vectorize
);
546 /* Run after opts so it can hit more */
548 NIR_PASS(progress
, nir
, nir_fuse_io_16
);
550 /* Must be run at the end to prevent creation of fsin/fcos ops */
551 NIR_PASS(progress
, nir
, midgard_nir_scale_trig
);
556 NIR_PASS(progress
, nir
, nir_opt_dce
);
557 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
558 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
559 NIR_PASS(progress
, nir
, nir_copy_prop
);
562 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
563 NIR_PASS(progress
, nir
, nir_opt_algebraic_distribute_src_mods
);
565 /* We implement booleans as 32-bit 0/~0 */
566 NIR_PASS(progress
, nir
, nir_lower_bool_to_int32
);
568 /* Now that booleans are lowered, we can run out late opts */
569 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_late
);
570 NIR_PASS(progress
, nir
, midgard_nir_cancel_inot
);
572 NIR_PASS(progress
, nir
, nir_copy_prop
);
573 NIR_PASS(progress
, nir
, nir_opt_dce
);
575 /* Take us out of SSA */
576 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
577 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
579 /* We are a vector architecture; write combine where possible */
580 NIR_PASS(progress
, nir
, nir_move_vec_src_uses_to_dest
);
581 NIR_PASS(progress
, nir
, nir_lower_vec_to_movs
);
583 NIR_PASS(progress
, nir
, nir_opt_dce
);
586 /* Do not actually emit a load; instead, cache the constant for inlining */
589 emit_load_const(compiler_context
*ctx
, nir_load_const_instr
*instr
)
591 nir_ssa_def def
= instr
->def
;
593 midgard_constants
*consts
= rzalloc(NULL
, midgard_constants
);
595 assert(instr
->def
.num_components
* instr
->def
.bit_size
<= sizeof(*consts
) * 8);
597 #define RAW_CONST_COPY(bits) \
598 nir_const_value_to_array(consts->u##bits, instr->value, \
599 instr->def.num_components, u##bits)
601 switch (instr
->def
.bit_size
) {
615 unreachable("Invalid bit_size for load_const instruction\n");
618 /* Shifted for SSA, +1 for off-by-one */
619 _mesa_hash_table_u64_insert(ctx
->ssa_constants
, (def
.index
<< 1) + 1, consts
);
622 /* Normally constants are embedded implicitly, but for I/O and such we have to
623 * explicitly emit a move with the constant source */
626 emit_explicit_constant(compiler_context
*ctx
, unsigned node
, unsigned to
)
628 void *constant_value
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, node
+ 1);
630 if (constant_value
) {
631 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), to
);
632 attach_constants(ctx
, &ins
, constant_value
, node
+ 1);
633 emit_mir_instruction(ctx
, ins
);
638 nir_is_non_scalar_swizzle(nir_alu_src
*src
, unsigned nr_components
)
640 unsigned comp
= src
->swizzle
[0];
642 for (unsigned c
= 1; c
< nr_components
; ++c
) {
643 if (src
->swizzle
[c
] != comp
)
650 #define ALU_CASE(nir, _op) \
652 op = midgard_alu_op_##_op; \
653 assert(src_bitsize == dst_bitsize); \
656 #define ALU_CASE_RTZ(nir, _op) \
658 op = midgard_alu_op_##_op; \
659 roundmode = MIDGARD_RTZ; \
662 #define ALU_CHECK_CMP(sext) \
663 assert(src_bitsize == 16 || src_bitsize == 32); \
664 assert(dst_bitsize == 16 || dst_bitsize == 32); \
666 #define ALU_CASE_BCAST(nir, _op, count) \
668 op = midgard_alu_op_##_op; \
669 broadcast_swizzle = count; \
670 ALU_CHECK_CMP(true); \
673 #define ALU_CASE_CMP(nir, _op, sext) \
675 op = midgard_alu_op_##_op; \
676 ALU_CHECK_CMP(sext); \
679 /* Compare mir_lower_invert */
681 nir_accepts_inot(nir_op op
, unsigned src
)
685 case nir_op_iand
: /* TODO: b2f16 */
689 /* Only the condition */
697 mir_accept_dest_mod(compiler_context
*ctx
, nir_dest
**dest
, nir_op op
)
699 if (pan_has_dest_mod(dest
, op
)) {
700 assert((*dest
)->is_ssa
);
701 BITSET_SET(ctx
->already_emitted
, (*dest
)->ssa
.index
);
708 /* Look for floating point mods. We have the mods fsat, fsat_signed,
709 * and fpos. We also have the relations (note 3 * 2 = 6 cases):
711 * fsat_signed(fpos(x)) = fsat(x)
712 * fsat_signed(fsat(x)) = fsat(x)
713 * fpos(fsat_signed(x)) = fsat(x)
714 * fpos(fsat(x)) = fsat(x)
715 * fsat(fsat_signed(x)) = fsat(x)
716 * fsat(fpos(x)) = fsat(x)
718 * So by cases any composition of output modifiers is equivalent to
722 mir_determine_float_outmod(compiler_context
*ctx
, nir_dest
**dest
, unsigned prior_outmod
)
724 bool fpos
= mir_accept_dest_mod(ctx
, dest
, nir_op_fclamp_pos
);
725 bool fsat
= mir_accept_dest_mod(ctx
, dest
, nir_op_fsat
);
726 bool ssat
= mir_accept_dest_mod(ctx
, dest
, nir_op_fsat_signed
);
727 bool prior
= (prior_outmod
!= midgard_outmod_none
);
728 int count
= (int) prior
+ (int) fpos
+ (int) ssat
+ (int) fsat
;
730 return ((count
> 1) || fsat
) ? midgard_outmod_sat
:
731 fpos
? midgard_outmod_pos
:
732 ssat
? midgard_outmod_sat_signed
:
737 mir_copy_src(midgard_instruction
*ins
, nir_alu_instr
*instr
, unsigned i
, unsigned to
, bool *abs
, bool *neg
, bool *not, enum midgard_roundmode
*roundmode
, bool is_int
, unsigned bcast_count
)
739 nir_alu_src src
= instr
->src
[i
];
742 if (pan_has_source_mod(&src
, nir_op_fneg
))
745 if (pan_has_source_mod(&src
, nir_op_fabs
))
749 if (nir_accepts_inot(instr
->op
, i
) && pan_has_source_mod(&src
, nir_op_inot
))
753 if (pan_has_source_mod(&src
, nir_op_fround_even
))
754 *roundmode
= MIDGARD_RTE
;
756 if (pan_has_source_mod(&src
, nir_op_ftrunc
))
757 *roundmode
= MIDGARD_RTZ
;
759 if (pan_has_source_mod(&src
, nir_op_ffloor
))
760 *roundmode
= MIDGARD_RTN
;
762 if (pan_has_source_mod(&src
, nir_op_fceil
))
763 *roundmode
= MIDGARD_RTP
;
766 unsigned bits
= nir_src_bit_size(src
.src
);
768 ins
->src
[to
] = nir_src_index(NULL
, &src
.src
);
769 ins
->src_types
[to
] = nir_op_infos
[instr
->op
].input_types
[i
] | bits
;
771 for (unsigned c
= 0; c
< NIR_MAX_VEC_COMPONENTS
; ++c
) {
772 ins
->swizzle
[to
][c
] = src
.swizzle
[
773 (!bcast_count
|| c
< bcast_count
) ? c
:
778 /* Midgard features both fcsel and icsel, depending on whether you want int or
779 * float modifiers. NIR's csel is typeless, so we want a heuristic to guess if
780 * we should emit an int or float csel depending on what modifiers could be
781 * placed. In the absense of modifiers, this is probably arbitrary. */
784 mir_is_bcsel_float(nir_alu_instr
*instr
)
787 nir_op_i2i8
, nir_op_i2i16
,
788 nir_op_i2i32
, nir_op_i2i64
791 nir_op floatmods
[] = {
792 nir_op_fabs
, nir_op_fneg
,
793 nir_op_f2f16
, nir_op_f2f32
,
797 nir_op floatdestmods
[] = {
798 nir_op_fsat
, nir_op_fsat_signed
, nir_op_fclamp_pos
,
799 nir_op_f2f16
, nir_op_f2f32
804 for (unsigned i
= 1; i
< 3; ++i
) {
805 nir_alu_src s
= instr
->src
[i
];
806 for (unsigned q
= 0; q
< ARRAY_SIZE(intmods
); ++q
) {
807 if (pan_has_source_mod(&s
, intmods
[q
]))
812 for (unsigned i
= 1; i
< 3; ++i
) {
813 nir_alu_src s
= instr
->src
[i
];
814 for (unsigned q
= 0; q
< ARRAY_SIZE(floatmods
); ++q
) {
815 if (pan_has_source_mod(&s
, floatmods
[q
]))
820 for (unsigned q
= 0; q
< ARRAY_SIZE(floatdestmods
); ++q
) {
821 nir_dest
*dest
= &instr
->dest
.dest
;
822 if (pan_has_dest_mod(&dest
, floatdestmods
[q
]))
830 emit_alu(compiler_context
*ctx
, nir_alu_instr
*instr
)
832 nir_dest
*dest
= &instr
->dest
.dest
;
834 if (dest
->is_ssa
&& BITSET_TEST(ctx
->already_emitted
, dest
->ssa
.index
))
837 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
838 * is handled elsewhere */
840 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddy
) {
841 midgard_emit_derivatives(ctx
, instr
);
845 bool is_ssa
= dest
->is_ssa
;
847 unsigned nr_components
= nir_dest_num_components(*dest
);
848 unsigned nr_inputs
= nir_op_infos
[instr
->op
].num_inputs
;
851 /* Number of components valid to check for the instruction (the rest
852 * will be forced to the last), or 0 to use as-is. Relevant as
853 * ball-type instructions have a channel count in NIR but are all vec4
856 unsigned broadcast_swizzle
= 0;
858 /* Should we swap arguments? */
859 bool flip_src12
= false;
861 unsigned src_bitsize
= nir_src_bit_size(instr
->src
[0].src
);
862 unsigned dst_bitsize
= nir_dest_bit_size(*dest
);
864 enum midgard_roundmode roundmode
= MIDGARD_RTE
;
867 ALU_CASE(fadd
, fadd
);
868 ALU_CASE(fmul
, fmul
);
869 ALU_CASE(fmin
, fmin
);
870 ALU_CASE(fmax
, fmax
);
871 ALU_CASE(imin
, imin
);
872 ALU_CASE(imax
, imax
);
873 ALU_CASE(umin
, umin
);
874 ALU_CASE(umax
, umax
);
875 ALU_CASE(ffloor
, ffloor
);
876 ALU_CASE(fround_even
, froundeven
);
877 ALU_CASE(ftrunc
, ftrunc
);
878 ALU_CASE(fceil
, fceil
);
879 ALU_CASE(fdot3
, fdot3
);
880 ALU_CASE(fdot4
, fdot4
);
881 ALU_CASE(iadd
, iadd
);
882 ALU_CASE(isub
, isub
);
883 ALU_CASE(imul
, imul
);
885 /* Zero shoved as second-arg */
886 ALU_CASE(iabs
, iabsdiff
);
890 ALU_CASE_CMP(feq32
, feq
, false);
891 ALU_CASE_CMP(fne32
, fne
, false);
892 ALU_CASE_CMP(flt32
, flt
, false);
893 ALU_CASE_CMP(ieq32
, ieq
, true);
894 ALU_CASE_CMP(ine32
, ine
, true);
895 ALU_CASE_CMP(ilt32
, ilt
, true);
896 ALU_CASE_CMP(ult32
, ult
, false);
898 /* We don't have a native b2f32 instruction. Instead, like many
899 * GPUs, we exploit booleans as 0/~0 for false/true, and
900 * correspondingly AND
901 * by 1.0 to do the type conversion. For the moment, prime us
904 * iand [whatever], #0
906 * At the end of emit_alu (as MIR), we'll fix-up the constant
909 ALU_CASE_CMP(b2f32
, iand
, true);
910 ALU_CASE_CMP(b2f16
, iand
, true);
911 ALU_CASE_CMP(b2i32
, iand
, true);
913 /* Likewise, we don't have a dedicated f2b32 instruction, but
914 * we can do a "not equal to 0.0" test. */
916 ALU_CASE_CMP(f2b32
, fne
, false);
917 ALU_CASE_CMP(i2b32
, ine
, true);
919 ALU_CASE(frcp
, frcp
);
920 ALU_CASE(frsq
, frsqrt
);
921 ALU_CASE(fsqrt
, fsqrt
);
922 ALU_CASE(fexp2
, fexp2
);
923 ALU_CASE(flog2
, flog2
);
925 ALU_CASE_RTZ(f2i64
, f2i_rte
);
926 ALU_CASE_RTZ(f2u64
, f2u_rte
);
927 ALU_CASE_RTZ(i2f64
, i2f_rte
);
928 ALU_CASE_RTZ(u2f64
, u2f_rte
);
930 ALU_CASE_RTZ(f2i32
, f2i_rte
);
931 ALU_CASE_RTZ(f2u32
, f2u_rte
);
932 ALU_CASE_RTZ(i2f32
, i2f_rte
);
933 ALU_CASE_RTZ(u2f32
, u2f_rte
);
935 ALU_CASE_RTZ(f2i8
, f2i_rte
);
936 ALU_CASE_RTZ(f2u8
, f2u_rte
);
938 ALU_CASE_RTZ(f2i16
, f2i_rte
);
939 ALU_CASE_RTZ(f2u16
, f2u_rte
);
940 ALU_CASE_RTZ(i2f16
, i2f_rte
);
941 ALU_CASE_RTZ(u2f16
, u2f_rte
);
943 ALU_CASE(fsin
, fsin
);
944 ALU_CASE(fcos
, fcos
);
946 /* We'll get 0 in the second arg, so:
947 * ~a = ~(a | 0) = nor(a, 0) */
948 ALU_CASE(inot
, inor
);
949 ALU_CASE(iand
, iand
);
951 ALU_CASE(ixor
, ixor
);
952 ALU_CASE(ishl
, ishl
);
953 ALU_CASE(ishr
, iasr
);
954 ALU_CASE(ushr
, ilsr
);
956 ALU_CASE_BCAST(b32all_fequal2
, fball_eq
, 2);
957 ALU_CASE_BCAST(b32all_fequal3
, fball_eq
, 3);
958 ALU_CASE_CMP(b32all_fequal4
, fball_eq
, true);
960 ALU_CASE_BCAST(b32any_fnequal2
, fbany_neq
, 2);
961 ALU_CASE_BCAST(b32any_fnequal3
, fbany_neq
, 3);
962 ALU_CASE_CMP(b32any_fnequal4
, fbany_neq
, true);
964 ALU_CASE_BCAST(b32all_iequal2
, iball_eq
, 2);
965 ALU_CASE_BCAST(b32all_iequal3
, iball_eq
, 3);
966 ALU_CASE_CMP(b32all_iequal4
, iball_eq
, true);
968 ALU_CASE_BCAST(b32any_inequal2
, ibany_neq
, 2);
969 ALU_CASE_BCAST(b32any_inequal3
, ibany_neq
, 3);
970 ALU_CASE_CMP(b32any_inequal4
, ibany_neq
, true);
972 /* Source mods will be shoved in later */
973 ALU_CASE(fabs
, fmov
);
974 ALU_CASE(fneg
, fmov
);
975 ALU_CASE(fsat
, fmov
);
976 ALU_CASE(fsat_signed
, fmov
);
977 ALU_CASE(fclamp_pos
, fmov
);
979 /* For size conversion, we use a move. Ideally though we would squash
980 * these ops together; maybe that has to happen after in NIR as part of
981 * propagation...? An earlier algebraic pass ensured we step down by
982 * only / exactly one size. If stepping down, we use a dest override to
983 * reduce the size; if stepping up, we use a larger-sized move with a
984 * half source and a sign/zero-extension modifier */
997 if (instr
->op
== nir_op_f2f16
|| instr
->op
== nir_op_f2f32
||
998 instr
->op
== nir_op_f2f64
)
999 op
= midgard_alu_op_fmov
;
1001 op
= midgard_alu_op_imov
;
1006 /* For greater-or-equal, we lower to less-or-equal and flip the
1012 case nir_op_uge32
: {
1014 instr
->op
== nir_op_fge
? midgard_alu_op_fle
:
1015 instr
->op
== nir_op_fge32
? midgard_alu_op_fle
:
1016 instr
->op
== nir_op_ige32
? midgard_alu_op_ile
:
1017 instr
->op
== nir_op_uge32
? midgard_alu_op_ule
:
1021 ALU_CHECK_CMP(false);
1025 case nir_op_b32csel
: {
1026 bool mixed
= nir_is_non_scalar_swizzle(&instr
->src
[0], nr_components
);
1027 bool is_float
= mir_is_bcsel_float(instr
);
1029 (mixed
? midgard_alu_op_fcsel_v
: midgard_alu_op_fcsel
) :
1030 (mixed
? midgard_alu_op_icsel_v
: midgard_alu_op_icsel
);
1035 case nir_op_unpack_32_2x16
:
1036 case nir_op_unpack_32_4x8
:
1037 case nir_op_pack_32_2x16
:
1038 case nir_op_pack_32_4x8
: {
1039 op
= midgard_alu_op_imov
;
1044 DBG("Unhandled ALU op %s\n", nir_op_infos
[instr
->op
].name
);
1049 /* Promote imov to fmov if it might help inline a constant */
1050 if (op
== midgard_alu_op_imov
&& nir_src_is_const(instr
->src
[0].src
)
1051 && nir_src_bit_size(instr
->src
[0].src
) == 32
1052 && nir_is_same_comp_swizzle(instr
->src
[0].swizzle
,
1053 nir_src_num_components(instr
->src
[0].src
))) {
1054 op
= midgard_alu_op_fmov
;
1057 /* Midgard can perform certain modifiers on output of an ALU op */
1059 unsigned outmod
= 0;
1060 bool is_int
= midgard_is_integer_op(op
);
1062 if (midgard_is_integer_out_op(op
)) {
1063 outmod
= midgard_outmod_int_wrap
;
1064 } else if (instr
->op
== nir_op_fsat
) {
1065 outmod
= midgard_outmod_sat
;
1066 } else if (instr
->op
== nir_op_fsat_signed
) {
1067 outmod
= midgard_outmod_sat_signed
;
1068 } else if (instr
->op
== nir_op_fclamp_pos
) {
1069 outmod
= midgard_outmod_pos
;
1072 /* Fetch unit, quirks, etc information */
1073 unsigned opcode_props
= alu_opcode_props
[op
].props
;
1074 bool quirk_flipped_r24
= opcode_props
& QUIRK_FLIPPED_R24
;
1076 if (!midgard_is_integer_out_op(op
)) {
1077 outmod
= mir_determine_float_outmod(ctx
, &dest
, outmod
);
1080 midgard_instruction ins
= {
1082 .dest
= nir_dest_index(dest
),
1083 .dest_type
= nir_op_infos
[instr
->op
].output_type
1084 | nir_dest_bit_size(*dest
),
1085 .roundmode
= roundmode
,
1088 enum midgard_roundmode
*roundptr
= (opcode_props
& MIDGARD_ROUNDS
) ?
1089 &ins
.roundmode
: NULL
;
1091 for (unsigned i
= nr_inputs
; i
< ARRAY_SIZE(ins
.src
); ++i
)
1094 if (quirk_flipped_r24
) {
1096 mir_copy_src(&ins
, instr
, 0, 1, &ins
.src_abs
[1], &ins
.src_neg
[1], &ins
.src_invert
[1], roundptr
, is_int
, broadcast_swizzle
);
1098 for (unsigned i
= 0; i
< nr_inputs
; ++i
) {
1101 if (instr
->op
== nir_op_b32csel
) {
1102 /* The condition is the first argument; move
1103 * the other arguments up one to be a binary
1104 * instruction for Midgard with the condition
1109 else if (flip_src12
)
1113 } else if (flip_src12
) {
1117 mir_copy_src(&ins
, instr
, i
, to
, &ins
.src_abs
[to
], &ins
.src_neg
[to
], &ins
.src_invert
[to
], roundptr
, is_int
, broadcast_swizzle
);
1119 /* (!c) ? a : b = c ? b : a */
1120 if (instr
->op
== nir_op_b32csel
&& ins
.src_invert
[2]) {
1121 ins
.src_invert
[2] = false;
1127 if (instr
->op
== nir_op_fneg
|| instr
->op
== nir_op_fabs
) {
1128 /* Lowered to move */
1129 if (instr
->op
== nir_op_fneg
)
1130 ins
.src_neg
[1] ^= true;
1132 if (instr
->op
== nir_op_fabs
)
1133 ins
.src_abs
[1] = true;
1136 ins
.mask
= mask_of(nr_components
);
1138 /* Apply writemask if non-SSA, keeping in mind that we can't write to
1139 * components that don't exist. Note modifier => SSA => !reg => no
1140 * writemask, so we don't have to worry about writemasks here.*/
1143 ins
.mask
&= instr
->dest
.write_mask
;
1146 ins
.outmod
= outmod
;
1148 /* Late fixup for emulated instructions */
1150 if (instr
->op
== nir_op_b2f32
|| instr
->op
== nir_op_b2i32
) {
1151 /* Presently, our second argument is an inline #0 constant.
1152 * Switch over to an embedded 1.0 constant (that can't fit
1153 * inline, since we're 32-bit, not 16-bit like the inline
1156 ins
.has_inline_constant
= false;
1157 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1158 ins
.src_types
[1] = nir_type_float32
;
1159 ins
.has_constants
= true;
1161 if (instr
->op
== nir_op_b2f32
)
1162 ins
.constants
.f32
[0] = 1.0f
;
1164 ins
.constants
.i32
[0] = 1;
1166 for (unsigned c
= 0; c
< 16; ++c
)
1167 ins
.swizzle
[1][c
] = 0;
1168 } else if (instr
->op
== nir_op_b2f16
) {
1169 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1170 ins
.src_types
[1] = nir_type_float16
;
1171 ins
.has_constants
= true;
1172 ins
.constants
.i16
[0] = _mesa_float_to_half(1.0);
1174 for (unsigned c
= 0; c
< 16; ++c
)
1175 ins
.swizzle
[1][c
] = 0;
1176 } else if (nr_inputs
== 1 && !quirk_flipped_r24
) {
1177 /* Lots of instructions need a 0 plonked in */
1178 ins
.has_inline_constant
= false;
1179 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1180 ins
.src_types
[1] = ins
.src_types
[0];
1181 ins
.has_constants
= true;
1182 ins
.constants
.u32
[0] = 0;
1184 for (unsigned c
= 0; c
< 16; ++c
)
1185 ins
.swizzle
[1][c
] = 0;
1186 } else if (instr
->op
== nir_op_pack_32_2x16
) {
1187 ins
.dest_type
= nir_type_uint16
;
1188 ins
.mask
= mask_of(nr_components
* 2);
1190 } else if (instr
->op
== nir_op_pack_32_4x8
) {
1191 ins
.dest_type
= nir_type_uint8
;
1192 ins
.mask
= mask_of(nr_components
* 4);
1194 } else if (instr
->op
== nir_op_unpack_32_2x16
) {
1195 ins
.dest_type
= nir_type_uint32
;
1196 ins
.mask
= mask_of(nr_components
>> 1);
1198 } else if (instr
->op
== nir_op_unpack_32_4x8
) {
1199 ins
.dest_type
= nir_type_uint32
;
1200 ins
.mask
= mask_of(nr_components
>> 2);
1204 if ((opcode_props
& UNITS_ALL
) == UNIT_VLUT
) {
1205 /* To avoid duplicating the lookup tables (probably), true LUT
1206 * instructions can only operate as if they were scalars. Lower
1207 * them here by changing the component. */
1209 unsigned orig_mask
= ins
.mask
;
1211 unsigned swizzle_back
[MIR_VEC_COMPONENTS
];
1212 memcpy(&swizzle_back
, ins
.swizzle
[0], sizeof(swizzle_back
));
1214 midgard_instruction ins_split
[MIR_VEC_COMPONENTS
];
1215 unsigned ins_count
= 0;
1217 for (int i
= 0; i
< nr_components
; ++i
) {
1218 /* Mask the associated component, dropping the
1219 * instruction if needed */
1222 ins
.mask
&= orig_mask
;
1224 for (unsigned j
= 0; j
< ins_count
; ++j
) {
1225 if (swizzle_back
[i
] == ins_split
[j
].swizzle
[0][0]) {
1226 ins_split
[j
].mask
|= ins
.mask
;
1235 for (unsigned j
= 0; j
< MIR_VEC_COMPONENTS
; ++j
)
1236 ins
.swizzle
[0][j
] = swizzle_back
[i
]; /* Pull from the correct component */
1238 ins_split
[ins_count
] = ins
;
1243 for (unsigned i
= 0; i
< ins_count
; ++i
) {
1244 emit_mir_instruction(ctx
, ins_split
[i
]);
1247 emit_mir_instruction(ctx
, ins
);
1254 mir_set_intr_mask(nir_instr
*instr
, midgard_instruction
*ins
, bool is_read
)
1256 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
1257 unsigned nir_mask
= 0;
1261 nir_mask
= mask_of(nir_intrinsic_dest_components(intr
));
1262 dsize
= nir_dest_bit_size(intr
->dest
);
1264 nir_mask
= nir_intrinsic_write_mask(intr
);
1268 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1269 unsigned bytemask
= pan_to_bytemask(dsize
, nir_mask
);
1270 mir_set_bytemask(ins
, bytemask
);
1271 ins
->dest_type
= nir_type_uint
| dsize
;
1274 /* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1275 * optimized) versions of UBO #0 */
1277 static midgard_instruction
*
1279 compiler_context
*ctx
,
1283 nir_src
*indirect_offset
,
1284 unsigned indirect_shift
,
1287 /* TODO: half-floats */
1289 midgard_instruction ins
= m_ld_ubo_int4(dest
, 0);
1290 ins
.constants
.u32
[0] = offset
;
1292 if (instr
->type
== nir_instr_type_intrinsic
)
1293 mir_set_intr_mask(instr
, &ins
, true);
1295 if (indirect_offset
) {
1296 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1297 ins
.src_types
[2] = nir_type_uint32
;
1298 ins
.load_store
.arg_2
= (indirect_shift
<< 5);
1300 /* X component for the whole swizzle to prevent register
1301 * pressure from ballooning from the extra components */
1302 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[2]); ++i
)
1303 ins
.swizzle
[2][i
] = 0;
1305 ins
.load_store
.arg_2
= 0x1E;
1308 ins
.load_store
.arg_1
= index
;
1310 return emit_mir_instruction(ctx
, ins
);
1313 /* Globals are like UBOs if you squint. And shared memory is like globals if
1314 * you squint even harder */
1318 compiler_context
*ctx
,
1327 midgard_instruction ins
;
1330 ins
= m_ld_int4(srcdest
, 0);
1332 ins
= m_st_int4(srcdest
, 0);
1334 mir_set_offset(ctx
, &ins
, offset
, is_shared
);
1335 mir_set_intr_mask(instr
, &ins
, is_read
);
1337 emit_mir_instruction(ctx
, ins
);
1342 compiler_context
*ctx
,
1343 unsigned dest
, unsigned offset
,
1344 unsigned nr_comp
, unsigned component
,
1345 nir_src
*indirect_offset
, nir_alu_type type
, bool flat
)
1347 /* XXX: Half-floats? */
1348 /* TODO: swizzle, mask */
1350 midgard_instruction ins
= m_ld_vary_32(dest
, offset
);
1351 ins
.mask
= mask_of(nr_comp
);
1352 ins
.dest_type
= type
;
1354 if (type
== nir_type_float16
) {
1355 /* Ensure we are aligned so we can pack it later */
1356 ins
.mask
= mask_of(ALIGN_POT(nr_comp
, 2));
1359 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[0]); ++i
)
1360 ins
.swizzle
[0][i
] = MIN2(i
+ component
, COMPONENT_W
);
1362 midgard_varying_parameter p
= {
1364 .interpolation
= midgard_interp_default
,
1369 memcpy(&u
, &p
, sizeof(p
));
1370 ins
.load_store
.varying_parameters
= u
;
1372 if (indirect_offset
) {
1373 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1374 ins
.src_types
[2] = nir_type_uint32
;
1376 ins
.load_store
.arg_2
= 0x1E;
1378 ins
.load_store
.arg_1
= 0x9E;
1380 /* Use the type appropriate load */
1382 case nir_type_uint32
:
1383 case nir_type_bool32
:
1384 ins
.op
= midgard_op_ld_vary_32u
;
1386 case nir_type_int32
:
1387 ins
.op
= midgard_op_ld_vary_32i
;
1389 case nir_type_float32
:
1390 ins
.op
= midgard_op_ld_vary_32
;
1392 case nir_type_float16
:
1393 ins
.op
= midgard_op_ld_vary_16
;
1396 unreachable("Attempted to load unknown type");
1400 emit_mir_instruction(ctx
, ins
);
1405 compiler_context
*ctx
,
1406 unsigned dest
, unsigned offset
,
1407 unsigned nr_comp
, nir_alu_type t
)
1409 midgard_instruction ins
= m_ld_attr_32(dest
, offset
);
1410 ins
.load_store
.arg_1
= 0x1E;
1411 ins
.load_store
.arg_2
= 0x1E;
1412 ins
.mask
= mask_of(nr_comp
);
1414 /* Use the type appropriate load */
1418 ins
.op
= midgard_op_ld_attr_32u
;
1421 ins
.op
= midgard_op_ld_attr_32i
;
1423 case nir_type_float
:
1424 ins
.op
= midgard_op_ld_attr_32
;
1427 unreachable("Attempted to load unknown type");
1431 emit_mir_instruction(ctx
, ins
);
1435 emit_sysval_read(compiler_context
*ctx
, nir_instr
*instr
,
1436 unsigned nr_components
, unsigned offset
)
1440 /* Figure out which uniform this is */
1441 int sysval
= panfrost_sysval_for_instr(instr
, &nir_dest
);
1442 void *val
= _mesa_hash_table_u64_search(ctx
->sysvals
.sysval_to_id
, sysval
);
1444 unsigned dest
= nir_dest_index(&nir_dest
);
1446 /* Sysvals are prefix uniforms */
1447 unsigned uniform
= ((uintptr_t) val
) - 1;
1449 /* Emit the read itself -- this is never indirect */
1450 midgard_instruction
*ins
=
1451 emit_ubo_read(ctx
, instr
, dest
, (uniform
* 16) + offset
, NULL
, 0, 0);
1453 ins
->mask
= mask_of(nr_components
);
1457 compute_builtin_arg(nir_op op
)
1460 case nir_intrinsic_load_work_group_id
:
1462 case nir_intrinsic_load_local_invocation_id
:
1465 unreachable("Invalid compute paramater loaded");
1470 emit_fragment_store(compiler_context
*ctx
, unsigned src
, unsigned src_z
, unsigned src_s
, enum midgard_rt_id rt
)
1472 assert(rt
< ARRAY_SIZE(ctx
->writeout_branch
));
1474 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
1478 emit_explicit_constant(ctx
, src
, src
);
1480 struct midgard_instruction ins
=
1481 v_branch(false, false);
1483 bool depth_only
= (rt
== MIDGARD_ZS_RT
);
1485 ins
.writeout
= depth_only
? 0 : PAN_WRITEOUT_C
;
1487 /* Add dependencies */
1489 ins
.src_types
[0] = nir_type_uint32
;
1490 ins
.constants
.u32
[0] = depth_only
? 0xFF : (rt
- MIDGARD_COLOR_RT0
) * 0x100;
1491 for (int i
= 0; i
< 4; ++i
)
1492 ins
.swizzle
[0][i
] = i
;
1495 emit_explicit_constant(ctx
, src_z
, src_z
);
1497 ins
.src_types
[2] = nir_type_uint32
;
1498 ins
.writeout
|= PAN_WRITEOUT_Z
;
1501 emit_explicit_constant(ctx
, src_s
, src_s
);
1503 ins
.src_types
[3] = nir_type_uint32
;
1504 ins
.writeout
|= PAN_WRITEOUT_S
;
1507 /* Emit the branch */
1508 br
= emit_mir_instruction(ctx
, ins
);
1509 schedule_barrier(ctx
);
1510 ctx
->writeout_branch
[rt
] = br
;
1512 /* Push our current location = current block count - 1 = where we'll
1513 * jump to. Maybe a bit too clever for my own good */
1515 br
->branch
.target_block
= ctx
->block_count
- 1;
1519 emit_compute_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1521 unsigned reg
= nir_dest_index(&instr
->dest
);
1522 midgard_instruction ins
= m_ld_compute_id(reg
, 0);
1523 ins
.mask
= mask_of(3);
1524 ins
.swizzle
[0][3] = COMPONENT_X
; /* xyzx */
1525 ins
.load_store
.arg_1
= compute_builtin_arg(instr
->intrinsic
);
1526 emit_mir_instruction(ctx
, ins
);
1530 vertex_builtin_arg(nir_op op
)
1533 case nir_intrinsic_load_vertex_id
:
1534 return PAN_VERTEX_ID
;
1535 case nir_intrinsic_load_instance_id
:
1536 return PAN_INSTANCE_ID
;
1538 unreachable("Invalid vertex builtin");
1543 emit_vertex_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1545 unsigned reg
= nir_dest_index(&instr
->dest
);
1546 emit_attr_read(ctx
, reg
, vertex_builtin_arg(instr
->intrinsic
), 1, nir_type_int
);
1550 emit_msaa_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1552 unsigned reg
= nir_dest_index(&instr
->dest
);
1554 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1555 ld
.op
= midgard_op_ld_color_buffer_32u_old
;
1556 ld
.load_store
.address
= 97;
1557 ld
.load_store
.arg_2
= 0x1E;
1559 for (int i
= 0; i
< 4; ++i
)
1560 ld
.swizzle
[0][i
] = COMPONENT_X
;
1562 emit_mir_instruction(ctx
, ld
);
1566 emit_control_barrier(compiler_context
*ctx
)
1568 midgard_instruction ins
= {
1569 .type
= TAG_TEXTURE_4
,
1571 .src
= { ~0, ~0, ~0, ~0 },
1572 .op
= TEXTURE_OP_BARRIER
,
1575 emit_mir_instruction(ctx
, ins
);
1579 mir_get_branch_cond(nir_src
*src
, bool *invert
)
1581 /* Wrap it. No swizzle since it's a scalar */
1587 *invert
= pan_has_source_mod(&alu
, nir_op_inot
);
1588 return nir_src_index(NULL
, &alu
.src
);
1592 output_load_rt_addr(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1595 return ctx
->blend_rt
;
1597 const nir_variable
*var
;
1598 var
= search_var(ctx
->nir
, nir_var_shader_out
, nir_intrinsic_base(instr
));
1601 unsigned loc
= var
->data
.location
;
1603 if (loc
== FRAG_RESULT_COLOR
)
1604 loc
= FRAG_RESULT_DATA0
;
1606 if (loc
>= FRAG_RESULT_DATA0
)
1607 return loc
- FRAG_RESULT_DATA0
;
1609 if (loc
== FRAG_RESULT_DEPTH
)
1611 if (loc
== FRAG_RESULT_STENCIL
)
1614 unreachable("Invalid RT to load from");
1618 emit_intrinsic(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1620 unsigned offset
= 0, reg
;
1622 switch (instr
->intrinsic
) {
1623 case nir_intrinsic_discard_if
:
1624 case nir_intrinsic_discard
: {
1625 bool conditional
= instr
->intrinsic
== nir_intrinsic_discard_if
;
1626 struct midgard_instruction discard
= v_branch(conditional
, false);
1627 discard
.branch
.target_type
= TARGET_DISCARD
;
1630 discard
.src
[0] = mir_get_branch_cond(&instr
->src
[0],
1631 &discard
.branch
.invert_conditional
);
1632 discard
.src_types
[0] = nir_type_uint32
;
1635 emit_mir_instruction(ctx
, discard
);
1636 schedule_barrier(ctx
);
1641 case nir_intrinsic_load_uniform
:
1642 case nir_intrinsic_load_ubo
:
1643 case nir_intrinsic_load_global
:
1644 case nir_intrinsic_load_shared
:
1645 case nir_intrinsic_load_input
:
1646 case nir_intrinsic_load_interpolated_input
: {
1647 bool is_uniform
= instr
->intrinsic
== nir_intrinsic_load_uniform
;
1648 bool is_ubo
= instr
->intrinsic
== nir_intrinsic_load_ubo
;
1649 bool is_global
= instr
->intrinsic
== nir_intrinsic_load_global
;
1650 bool is_shared
= instr
->intrinsic
== nir_intrinsic_load_shared
;
1651 bool is_flat
= instr
->intrinsic
== nir_intrinsic_load_input
;
1652 bool is_interp
= instr
->intrinsic
== nir_intrinsic_load_interpolated_input
;
1654 /* Get the base type of the intrinsic */
1655 /* TODO: Infer type? Does it matter? */
1657 (is_ubo
|| is_global
|| is_shared
) ? nir_type_uint
:
1658 (is_interp
) ? nir_type_float
:
1659 nir_intrinsic_type(instr
);
1661 t
= nir_alu_type_get_base_type(t
);
1663 if (!(is_ubo
|| is_global
)) {
1664 offset
= nir_intrinsic_base(instr
);
1667 unsigned nr_comp
= nir_intrinsic_dest_components(instr
);
1669 nir_src
*src_offset
= nir_get_io_offset_src(instr
);
1671 bool direct
= nir_src_is_const(*src_offset
);
1672 nir_src
*indirect_offset
= direct
? NULL
: src_offset
;
1675 offset
+= nir_src_as_uint(*src_offset
);
1677 /* We may need to apply a fractional offset */
1678 int component
= (is_flat
|| is_interp
) ?
1679 nir_intrinsic_component(instr
) : 0;
1680 reg
= nir_dest_index(&instr
->dest
);
1682 if (is_uniform
&& !ctx
->is_blend
) {
1683 emit_ubo_read(ctx
, &instr
->instr
, reg
, (ctx
->sysvals
.sysval_count
+ offset
) * 16, indirect_offset
, 4, 0);
1684 } else if (is_ubo
) {
1685 nir_src index
= instr
->src
[0];
1687 /* TODO: Is indirect block number possible? */
1688 assert(nir_src_is_const(index
));
1690 uint32_t uindex
= nir_src_as_uint(index
) + 1;
1691 emit_ubo_read(ctx
, &instr
->instr
, reg
, offset
, indirect_offset
, 0, uindex
);
1692 } else if (is_global
|| is_shared
) {
1693 emit_global(ctx
, &instr
->instr
, true, reg
, src_offset
, is_shared
);
1694 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& !ctx
->is_blend
) {
1695 emit_varying_read(ctx
, reg
, offset
, nr_comp
, component
, indirect_offset
, t
| nir_dest_bit_size(instr
->dest
), is_flat
);
1696 } else if (ctx
->is_blend
) {
1697 /* ctx->blend_input will be precoloured to r0/r2, where
1698 * the input is preloaded */
1700 unsigned *input
= offset
? &ctx
->blend_src1
: &ctx
->blend_input
;
1705 emit_mir_instruction(ctx
, v_mov(*input
, reg
));
1706 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1707 emit_attr_read(ctx
, reg
, offset
, nr_comp
, t
);
1709 DBG("Unknown load\n");
1716 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1717 case nir_intrinsic_load_barycentric_pixel
:
1718 case nir_intrinsic_load_barycentric_centroid
:
1721 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1723 case nir_intrinsic_load_raw_output_pan
: {
1724 reg
= nir_dest_index(&instr
->dest
);
1726 /* T720 and below use different blend opcodes with slightly
1727 * different semantics than T760 and up */
1729 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1731 ld
.load_store
.arg_2
= output_load_rt_addr(ctx
, instr
);
1733 if (nir_src_is_const(instr
->src
[0])) {
1734 ld
.load_store
.arg_1
= nir_src_as_uint(instr
->src
[0]);
1736 ld
.load_store
.varying_parameters
= 2;
1737 ld
.src
[1] = nir_src_index(ctx
, &instr
->src
[0]);
1738 ld
.src_types
[1] = nir_type_int32
;
1741 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1742 ld
.op
= midgard_op_ld_color_buffer_32u_old
;
1743 ld
.load_store
.address
= 16;
1744 ld
.load_store
.arg_2
= 0x1E;
1747 emit_mir_instruction(ctx
, ld
);
1751 case nir_intrinsic_load_output
: {
1752 reg
= nir_dest_index(&instr
->dest
);
1754 unsigned bits
= nir_dest_bit_size(instr
->dest
);
1756 midgard_instruction ld
;
1758 ld
= m_ld_color_buffer_as_fp16(reg
, 0);
1760 ld
= m_ld_color_buffer_as_fp32(reg
, 0);
1762 ld
.load_store
.arg_2
= output_load_rt_addr(ctx
, instr
);
1764 for (unsigned c
= 4; c
< 16; ++c
)
1765 ld
.swizzle
[0][c
] = 0;
1767 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1769 ld
.op
= midgard_op_ld_color_buffer_as_fp16_old
;
1771 ld
.op
= midgard_op_ld_color_buffer_as_fp32_old
;
1772 ld
.load_store
.address
= 1;
1773 ld
.load_store
.arg_2
= 0x1E;
1776 emit_mir_instruction(ctx
, ld
);
1780 case nir_intrinsic_load_blend_const_color_rgba
: {
1781 assert(ctx
->is_blend
);
1782 reg
= nir_dest_index(&instr
->dest
);
1784 /* Blend constants are embedded directly in the shader and
1785 * patched in, so we use some magic routing */
1787 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), reg
);
1788 ins
.has_constants
= true;
1789 ins
.has_blend_constant
= true;
1790 emit_mir_instruction(ctx
, ins
);
1794 case nir_intrinsic_store_output
:
1795 case nir_intrinsic_store_combined_output_pan
:
1796 assert(nir_src_is_const(instr
->src
[1]) && "no indirect outputs");
1798 offset
= nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[1]);
1800 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1802 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
1803 bool combined
= instr
->intrinsic
==
1804 nir_intrinsic_store_combined_output_pan
;
1806 const nir_variable
*var
;
1807 var
= search_var(ctx
->nir
, nir_var_shader_out
,
1808 nir_intrinsic_base(instr
));
1811 /* Dual-source blend writeout is done by leaving the
1812 * value in r2 for the blend shader to use. */
1813 if (var
->data
.index
) {
1814 if (instr
->src
[0].is_ssa
) {
1815 emit_explicit_constant(ctx
, reg
, reg
);
1817 unsigned out
= make_compiler_temp(ctx
);
1819 midgard_instruction ins
= v_mov(reg
, out
);
1820 emit_mir_instruction(ctx
, ins
);
1822 ctx
->blend_src1
= out
;
1824 ctx
->blend_src1
= reg
;
1830 enum midgard_rt_id rt
;
1831 if (var
->data
.location
== FRAG_RESULT_COLOR
)
1832 rt
= MIDGARD_COLOR_RT0
;
1833 else if (var
->data
.location
>= FRAG_RESULT_DATA0
)
1834 rt
= MIDGARD_COLOR_RT0
+ var
->data
.location
-
1841 unsigned reg_z
= ~0, reg_s
= ~0;
1843 unsigned writeout
= nir_intrinsic_component(instr
);
1844 if (writeout
& PAN_WRITEOUT_Z
)
1845 reg_z
= nir_src_index(ctx
, &instr
->src
[2]);
1846 if (writeout
& PAN_WRITEOUT_S
)
1847 reg_s
= nir_src_index(ctx
, &instr
->src
[3]);
1850 emit_fragment_store(ctx
, reg
, reg_z
, reg_s
, rt
);
1851 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1852 assert(instr
->intrinsic
== nir_intrinsic_store_output
);
1854 /* We should have been vectorized, though we don't
1855 * currently check that st_vary is emitted only once
1856 * per slot (this is relevant, since there's not a mask
1857 * parameter available on the store [set to 0 by the
1858 * blob]). We do respect the component by adjusting the
1859 * swizzle. If this is a constant source, we'll need to
1860 * emit that explicitly. */
1862 emit_explicit_constant(ctx
, reg
, reg
);
1864 unsigned dst_component
= nir_intrinsic_component(instr
);
1865 unsigned nr_comp
= nir_src_num_components(instr
->src
[0]);
1867 midgard_instruction st
= m_st_vary_32(reg
, offset
);
1868 st
.load_store
.arg_1
= 0x9E;
1869 st
.load_store
.arg_2
= 0x1E;
1871 switch (nir_alu_type_get_base_type(nir_intrinsic_type(instr
))) {
1874 st
.op
= midgard_op_st_vary_32u
;
1877 st
.op
= midgard_op_st_vary_32i
;
1879 case nir_type_float
:
1880 st
.op
= midgard_op_st_vary_32
;
1883 unreachable("Attempted to store unknown type");
1887 /* nir_intrinsic_component(store_intr) encodes the
1888 * destination component start. Source component offset
1889 * adjustment is taken care of in
1890 * install_registers_instr(), when offset_swizzle() is
1893 unsigned src_component
= COMPONENT_X
;
1895 assert(nr_comp
> 0);
1896 for (unsigned i
= 0; i
< ARRAY_SIZE(st
.swizzle
); ++i
) {
1897 st
.swizzle
[0][i
] = src_component
;
1898 if (i
>= dst_component
&& i
< dst_component
+ nr_comp
- 1)
1902 emit_mir_instruction(ctx
, st
);
1904 DBG("Unknown store\n");
1910 /* Special case of store_output for lowered blend shaders */
1911 case nir_intrinsic_store_raw_output_pan
:
1912 assert (ctx
->stage
== MESA_SHADER_FRAGMENT
);
1913 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1914 emit_fragment_store(ctx
, reg
, ~0, ~0, ctx
->blend_rt
);
1917 case nir_intrinsic_store_global
:
1918 case nir_intrinsic_store_shared
:
1919 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1920 emit_explicit_constant(ctx
, reg
, reg
);
1922 emit_global(ctx
, &instr
->instr
, false, reg
, &instr
->src
[1], instr
->intrinsic
== nir_intrinsic_store_shared
);
1925 case nir_intrinsic_load_ssbo_address
:
1926 emit_sysval_read(ctx
, &instr
->instr
, 1, 0);
1929 case nir_intrinsic_get_buffer_size
:
1930 emit_sysval_read(ctx
, &instr
->instr
, 1, 8);
1933 case nir_intrinsic_load_viewport_scale
:
1934 case nir_intrinsic_load_viewport_offset
:
1935 case nir_intrinsic_load_num_work_groups
:
1936 case nir_intrinsic_load_sampler_lod_parameters_pan
:
1937 emit_sysval_read(ctx
, &instr
->instr
, 3, 0);
1940 case nir_intrinsic_load_work_group_id
:
1941 case nir_intrinsic_load_local_invocation_id
:
1942 emit_compute_builtin(ctx
, instr
);
1945 case nir_intrinsic_load_vertex_id
:
1946 case nir_intrinsic_load_instance_id
:
1947 emit_vertex_builtin(ctx
, instr
);
1950 case nir_intrinsic_load_sample_id
:
1951 emit_msaa_builtin(ctx
, instr
);
1954 case nir_intrinsic_memory_barrier_buffer
:
1955 case nir_intrinsic_memory_barrier_shared
:
1958 case nir_intrinsic_control_barrier
:
1959 schedule_barrier(ctx
);
1960 emit_control_barrier(ctx
);
1961 schedule_barrier(ctx
);
1965 fprintf(stderr
, "Unhandled intrinsic %s\n", nir_intrinsic_infos
[instr
->intrinsic
].name
);
1971 /* Returns dimension with 0 special casing cubemaps */
1973 midgard_tex_format(enum glsl_sampler_dim dim
)
1976 case GLSL_SAMPLER_DIM_1D
:
1977 case GLSL_SAMPLER_DIM_BUF
:
1980 case GLSL_SAMPLER_DIM_2D
:
1981 case GLSL_SAMPLER_DIM_MS
:
1982 case GLSL_SAMPLER_DIM_EXTERNAL
:
1983 case GLSL_SAMPLER_DIM_RECT
:
1986 case GLSL_SAMPLER_DIM_3D
:
1989 case GLSL_SAMPLER_DIM_CUBE
:
1993 DBG("Unknown sampler dim type\n");
1999 /* Tries to attach an explicit LOD or bias as a constant. Returns whether this
2003 pan_attach_constant_bias(
2004 compiler_context
*ctx
,
2006 midgard_texture_word
*word
)
2008 /* To attach as constant, it has to *be* constant */
2010 if (!nir_src_is_const(lod
))
2013 float f
= nir_src_as_float(lod
);
2015 /* Break into fixed-point */
2017 float lod_frac
= f
- lod_int
;
2019 /* Carry over negative fractions */
2020 if (lod_frac
< 0.0) {
2026 word
->bias
= float_to_ubyte(lod_frac
);
2027 word
->bias_int
= lod_int
;
2033 emit_texop_native(compiler_context
*ctx
, nir_tex_instr
*instr
,
2034 unsigned midgard_texop
)
2037 //assert (!instr->sampler);
2039 nir_dest
*dest
= &instr
->dest
;
2041 int texture_index
= instr
->texture_index
;
2042 int sampler_index
= texture_index
;
2044 nir_alu_type dest_base
= nir_alu_type_get_base_type(instr
->dest_type
);
2045 nir_alu_type dest_type
= dest_base
| nir_dest_bit_size(*dest
);
2047 /* texture instructions support float outmods */
2048 unsigned outmod
= midgard_outmod_none
;
2049 if (dest_base
== nir_type_float
) {
2050 outmod
= mir_determine_float_outmod(ctx
, &dest
, 0);
2053 midgard_instruction ins
= {
2054 .type
= TAG_TEXTURE_4
,
2056 .dest
= nir_dest_index(dest
),
2057 .src
= { ~0, ~0, ~0, ~0 },
2058 .dest_type
= dest_type
,
2059 .swizzle
= SWIZZLE_IDENTITY_4
,
2061 .op
= midgard_texop
,
2063 .format
= midgard_tex_format(instr
->sampler_dim
),
2064 .texture_handle
= texture_index
,
2065 .sampler_handle
= sampler_index
,
2066 .shadow
= instr
->is_shadow
,
2070 if (instr
->is_shadow
&& !instr
->is_new_style_shadow
)
2071 for (int i
= 0; i
< 4; ++i
)
2072 ins
.swizzle
[0][i
] = COMPONENT_X
;
2074 /* We may need a temporary for the coordinate */
2076 bool needs_temp_coord
=
2077 (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) ||
2078 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) ||
2081 unsigned coords
= needs_temp_coord
? make_compiler_temp_reg(ctx
) : 0;
2083 for (unsigned i
= 0; i
< instr
->num_srcs
; ++i
) {
2084 int index
= nir_src_index(ctx
, &instr
->src
[i
].src
);
2085 unsigned nr_components
= nir_src_num_components(instr
->src
[i
].src
);
2086 unsigned sz
= nir_src_bit_size(instr
->src
[i
].src
);
2087 nir_alu_type T
= nir_tex_instr_src_type(instr
, i
) | sz
;
2089 switch (instr
->src
[i
].src_type
) {
2090 case nir_tex_src_coord
: {
2091 emit_explicit_constant(ctx
, index
, index
);
2093 unsigned coord_mask
= mask_of(instr
->coord_components
);
2095 bool flip_zw
= (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) && (coord_mask
& (1 << COMPONENT_Z
));
2098 coord_mask
^= ((1 << COMPONENT_Z
) | (1 << COMPONENT_W
));
2100 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2101 /* texelFetch is undefined on samplerCube */
2102 assert(midgard_texop
!= TEXTURE_OP_TEXEL_FETCH
);
2104 /* For cubemaps, we use a special ld/st op to
2105 * select the face and copy the xy into the
2106 * texture register */
2108 midgard_instruction ld
= m_ld_cubemap_coords(coords
, 0);
2110 ld
.src_types
[1] = T
;
2111 ld
.mask
= 0x3; /* xy */
2112 ld
.load_store
.arg_1
= 0x20;
2113 ld
.swizzle
[1][3] = COMPONENT_X
;
2114 emit_mir_instruction(ctx
, ld
);
2117 ins
.swizzle
[1][2] = instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
2118 ins
.swizzle
[1][3] = COMPONENT_X
;
2119 } else if (needs_temp_coord
) {
2120 /* mov coord_temp, coords */
2121 midgard_instruction mov
= v_mov(index
, coords
);
2122 mov
.mask
= coord_mask
;
2125 mov
.swizzle
[1][COMPONENT_W
] = COMPONENT_Z
;
2127 emit_mir_instruction(ctx
, mov
);
2132 ins
.src
[1] = coords
;
2133 ins
.src_types
[1] = T
;
2135 /* Texelfetch coordinates uses all four elements
2136 * (xyz/index) regardless of texture dimensionality,
2137 * which means it's necessary to zero the unused
2138 * components to keep everything happy */
2140 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
2141 /* mov index.zw, #0, or generalized */
2142 midgard_instruction mov
=
2143 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), coords
);
2144 mov
.has_constants
= true;
2145 mov
.mask
= coord_mask
^ 0xF;
2146 emit_mir_instruction(ctx
, mov
);
2149 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) {
2150 /* Array component in w but NIR wants it in z,
2151 * but if we have a temp coord we already fixed
2154 if (nr_components
== 3) {
2155 ins
.swizzle
[1][2] = COMPONENT_Z
;
2156 ins
.swizzle
[1][3] = needs_temp_coord
? COMPONENT_W
: COMPONENT_Z
;
2157 } else if (nr_components
== 2) {
2159 instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
2160 ins
.swizzle
[1][3] = COMPONENT_X
;
2162 unreachable("Invalid texture 2D components");
2165 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
2167 ins
.swizzle
[1][2] = COMPONENT_Z
;
2168 ins
.swizzle
[1][3] = COMPONENT_W
;
2174 case nir_tex_src_bias
:
2175 case nir_tex_src_lod
: {
2176 /* Try as a constant if we can */
2178 bool is_txf
= midgard_texop
== TEXTURE_OP_TEXEL_FETCH
;
2179 if (!is_txf
&& pan_attach_constant_bias(ctx
, instr
->src
[i
].src
, &ins
.texture
))
2182 ins
.texture
.lod_register
= true;
2184 ins
.src_types
[2] = T
;
2186 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
2187 ins
.swizzle
[2][c
] = COMPONENT_X
;
2189 emit_explicit_constant(ctx
, index
, index
);
2194 case nir_tex_src_offset
: {
2195 ins
.texture
.offset_register
= true;
2197 ins
.src_types
[3] = T
;
2199 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
2200 ins
.swizzle
[3][c
] = (c
> COMPONENT_Z
) ? 0 : c
;
2202 emit_explicit_constant(ctx
, index
, index
);
2206 case nir_tex_src_comparator
:
2207 case nir_tex_src_ms_index
: {
2208 unsigned comp
= COMPONENT_Z
;
2210 /* mov coord_temp.foo, coords */
2211 midgard_instruction mov
= v_mov(index
, coords
);
2212 mov
.mask
= 1 << comp
;
2214 for (unsigned i
= 0; i
< MIR_VEC_COMPONENTS
; ++i
)
2215 mov
.swizzle
[1][i
] = COMPONENT_X
;
2217 emit_mir_instruction(ctx
, mov
);
2222 fprintf(stderr
, "Unknown texture source type: %d\n", instr
->src
[i
].src_type
);
2228 emit_mir_instruction(ctx
, ins
);
2232 emit_tex(compiler_context
*ctx
, nir_tex_instr
*instr
)
2234 switch (instr
->op
) {
2237 emit_texop_native(ctx
, instr
, TEXTURE_OP_NORMAL
);
2240 emit_texop_native(ctx
, instr
, TEXTURE_OP_LOD
);
2243 case nir_texop_txf_ms
:
2244 emit_texop_native(ctx
, instr
, TEXTURE_OP_TEXEL_FETCH
);
2247 emit_sysval_read(ctx
, &instr
->instr
, 4, 0);
2250 fprintf(stderr
, "Unhandled texture op: %d\n", instr
->op
);
2257 emit_jump(compiler_context
*ctx
, nir_jump_instr
*instr
)
2259 switch (instr
->type
) {
2260 case nir_jump_break
: {
2261 /* Emit a branch out of the loop */
2262 struct midgard_instruction br
= v_branch(false, false);
2263 br
.branch
.target_type
= TARGET_BREAK
;
2264 br
.branch
.target_break
= ctx
->current_loop_depth
;
2265 emit_mir_instruction(ctx
, br
);
2270 DBG("Unknown jump type %d\n", instr
->type
);
2276 emit_instr(compiler_context
*ctx
, struct nir_instr
*instr
)
2278 switch (instr
->type
) {
2279 case nir_instr_type_load_const
:
2280 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2283 case nir_instr_type_intrinsic
:
2284 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2287 case nir_instr_type_alu
:
2288 emit_alu(ctx
, nir_instr_as_alu(instr
));
2291 case nir_instr_type_tex
:
2292 emit_tex(ctx
, nir_instr_as_tex(instr
));
2295 case nir_instr_type_jump
:
2296 emit_jump(ctx
, nir_instr_as_jump(instr
));
2299 case nir_instr_type_ssa_undef
:
2304 DBG("Unhandled instruction type\n");
2310 /* ALU instructions can inline or embed constants, which decreases register
2311 * pressure and saves space. */
2313 #define CONDITIONAL_ATTACH(idx) { \
2314 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
2317 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
2318 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
2323 inline_alu_constants(compiler_context
*ctx
, midgard_block
*block
)
2325 mir_foreach_instr_in_block(block
, alu
) {
2326 /* Other instructions cannot inline constants */
2327 if (alu
->type
!= TAG_ALU_4
) continue;
2328 if (alu
->compact_branch
) continue;
2330 /* If there is already a constant here, we can do nothing */
2331 if (alu
->has_constants
) continue;
2333 CONDITIONAL_ATTACH(0);
2335 if (!alu
->has_constants
) {
2336 CONDITIONAL_ATTACH(1)
2337 } else if (!alu
->inline_constant
) {
2338 /* Corner case: _two_ vec4 constants, for instance with a
2339 * csel. For this case, we can only use a constant
2340 * register for one, we'll have to emit a move for the
2343 void *entry
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, alu
->src
[1] + 1);
2344 unsigned scratch
= make_compiler_temp(ctx
);
2347 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), scratch
);
2348 attach_constants(ctx
, &ins
, entry
, alu
->src
[1] + 1);
2350 /* Set the source */
2351 alu
->src
[1] = scratch
;
2353 /* Inject us -before- the last instruction which set r31 */
2354 mir_insert_instruction_before(ctx
, mir_prev_op(alu
), ins
);
2361 max_bitsize_for_alu(midgard_instruction
*ins
)
2363 unsigned max_bitsize
= 0;
2364 for (int i
= 0; i
< MIR_SRC_COUNT
; i
++) {
2365 if (ins
->src
[i
] == ~0) continue;
2366 unsigned src_bitsize
= nir_alu_type_get_type_size(ins
->src_types
[i
]);
2367 max_bitsize
= MAX2(src_bitsize
, max_bitsize
);
2369 unsigned dst_bitsize
= nir_alu_type_get_type_size(ins
->dest_type
);
2370 max_bitsize
= MAX2(dst_bitsize
, max_bitsize
);
2372 /* We don't have fp16 LUTs, so we'll want to emit code like:
2374 * vlut.fsinr hr0, hr0
2376 * where both input and output are 16-bit but the operation is carried
2381 case midgard_alu_op_fsqrt
:
2382 case midgard_alu_op_frcp
:
2383 case midgard_alu_op_frsqrt
:
2384 case midgard_alu_op_fsin
:
2385 case midgard_alu_op_fcos
:
2386 case midgard_alu_op_fexp2
:
2387 case midgard_alu_op_flog2
:
2388 max_bitsize
= MAX2(max_bitsize
, 32);
2399 reg_mode_for_bitsize(unsigned bitsize
)
2402 /* use 16 pipe for 8 since we don't support vec16 yet */
2405 return midgard_reg_mode_16
;
2407 return midgard_reg_mode_32
;
2409 return midgard_reg_mode_64
;
2411 unreachable("invalid bit size");
2415 /* Midgard supports two types of constants, embedded constants (128-bit) and
2416 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2417 * constants can be demoted to inline constants, for space savings and
2418 * sometimes a performance boost */
2421 embedded_to_inline_constant(compiler_context
*ctx
, midgard_block
*block
)
2423 mir_foreach_instr_in_block(block
, ins
) {
2424 if (!ins
->has_constants
) continue;
2425 if (ins
->has_inline_constant
) continue;
2427 /* Blend constants must not be inlined by definition */
2428 if (ins
->has_blend_constant
) continue;
2430 unsigned max_bitsize
= max_bitsize_for_alu(ins
);
2432 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2433 bool is_16
= max_bitsize
== 16;
2434 bool is_32
= max_bitsize
== 32;
2436 if (!(is_16
|| is_32
))
2439 /* src1 cannot be an inline constant due to encoding
2440 * restrictions. So, if possible we try to flip the arguments
2445 if (ins
->src
[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
) &&
2446 alu_opcode_props
[op
].props
& OP_COMMUTES
) {
2450 if (ins
->src
[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2451 /* Component is from the swizzle. Take a nonzero component */
2453 unsigned first_comp
= ffs(ins
->mask
) - 1;
2454 unsigned component
= ins
->swizzle
[1][first_comp
];
2456 /* Scale constant appropriately, if we can legally */
2457 int16_t scaled_constant
= 0;
2460 scaled_constant
= ins
->constants
.u16
[component
];
2461 } else if (midgard_is_integer_op(op
)) {
2462 scaled_constant
= ins
->constants
.u32
[component
];
2464 /* Constant overflow after resize */
2465 if (scaled_constant
!= ins
->constants
.u32
[component
])
2468 float original
= ins
->constants
.f32
[component
];
2469 scaled_constant
= _mesa_float_to_half(original
);
2471 /* Check for loss of precision. If this is
2472 * mediump, we don't care, but for a highp
2473 * shader, we need to pay attention. NIR
2474 * doesn't yet tell us which mode we're in!
2475 * Practically this prevents most constants
2476 * from being inlined, sadly. */
2478 float fp32
= _mesa_half_to_float(scaled_constant
);
2480 if (fp32
!= original
)
2484 /* Should've been const folded */
2485 if (ins
->src_abs
[1] || ins
->src_neg
[1])
2488 /* Make sure that the constant is not itself a vector
2489 * by checking if all accessed values are the same. */
2491 const midgard_constants
*cons
= &ins
->constants
;
2492 uint32_t value
= is_16
? cons
->u16
[component
] : cons
->u32
[component
];
2494 bool is_vector
= false;
2495 unsigned mask
= effective_writemask(ins
->op
, ins
->mask
);
2497 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
2498 /* We only care if this component is actually used */
2499 if (!(mask
& (1 << c
)))
2502 uint32_t test
= is_16
?
2503 cons
->u16
[ins
->swizzle
[1][c
]] :
2504 cons
->u32
[ins
->swizzle
[1][c
]];
2506 if (test
!= value
) {
2515 /* Get rid of the embedded constant */
2516 ins
->has_constants
= false;
2518 ins
->has_inline_constant
= true;
2519 ins
->inline_constant
= scaled_constant
;
2524 /* Dead code elimination for branches at the end of a block - only one branch
2525 * per block is legal semantically */
2528 midgard_cull_dead_branch(compiler_context
*ctx
, midgard_block
*block
)
2530 bool branched
= false;
2532 mir_foreach_instr_in_block_safe(block
, ins
) {
2533 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2536 mir_remove_instruction(ins
);
2542 /* We want to force the invert on AND/OR to the second slot to legalize into
2543 * iandnot/iornot. The relevant patterns are for AND (and OR respectively)
2545 * ~a & #b = ~a & ~(#~b)
2550 midgard_legalize_invert(compiler_context
*ctx
, midgard_block
*block
)
2552 mir_foreach_instr_in_block(block
, ins
) {
2553 if (ins
->type
!= TAG_ALU_4
) continue;
2555 if (ins
->op
!= midgard_alu_op_iand
&&
2556 ins
->op
!= midgard_alu_op_ior
) continue;
2558 if (ins
->src_invert
[1] || !ins
->src_invert
[0]) continue;
2560 if (ins
->has_inline_constant
) {
2561 /* ~(#~a) = ~(~#a) = a, so valid, and forces both
2563 ins
->inline_constant
= ~ins
->inline_constant
;
2564 ins
->src_invert
[1] = true;
2566 /* Flip to the right invert order. Note
2567 * has_inline_constant false by assumption on the
2568 * branch, so flipping makes sense. */
2575 emit_fragment_epilogue(compiler_context
*ctx
, unsigned rt
)
2577 /* Loop to ourselves */
2578 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2579 struct midgard_instruction ins
= v_branch(false, false);
2580 ins
.writeout
= br
->writeout
;
2581 ins
.branch
.target_block
= ctx
->block_count
- 1;
2582 ins
.constants
.u32
[0] = br
->constants
.u32
[0];
2583 memcpy(&ins
.src_types
, &br
->src_types
, sizeof(ins
.src_types
));
2584 emit_mir_instruction(ctx
, ins
);
2586 ctx
->current_block
->epilogue
= true;
2587 schedule_barrier(ctx
);
2588 return ins
.branch
.target_block
;
2591 static midgard_block
*
2592 emit_block_init(compiler_context
*ctx
)
2594 midgard_block
*this_block
= ctx
->after_block
;
2595 ctx
->after_block
= NULL
;
2598 this_block
= create_empty_block(ctx
);
2600 list_addtail(&this_block
->base
.link
, &ctx
->blocks
);
2602 this_block
->scheduled
= false;
2605 /* Set up current block */
2606 list_inithead(&this_block
->base
.instructions
);
2607 ctx
->current_block
= this_block
;
2612 static midgard_block
*
2613 emit_block(compiler_context
*ctx
, nir_block
*block
)
2615 midgard_block
*this_block
= emit_block_init(ctx
);
2617 nir_foreach_instr(instr
, block
) {
2618 emit_instr(ctx
, instr
);
2619 ++ctx
->instruction_count
;
2625 static midgard_block
*emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
);
2628 emit_if(struct compiler_context
*ctx
, nir_if
*nif
)
2630 midgard_block
*before_block
= ctx
->current_block
;
2632 /* Speculatively emit the branch, but we can't fill it in until later */
2634 EMIT(branch
, true, true);
2635 midgard_instruction
*then_branch
= mir_last_in_block(ctx
->current_block
);
2636 then_branch
->src
[0] = mir_get_branch_cond(&nif
->condition
, &inv
);
2637 then_branch
->src_types
[0] = nir_type_uint32
;
2638 then_branch
->branch
.invert_conditional
= !inv
;
2640 /* Emit the two subblocks. */
2641 midgard_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
2642 midgard_block
*end_then_block
= ctx
->current_block
;
2644 /* Emit a jump from the end of the then block to the end of the else */
2645 EMIT(branch
, false, false);
2646 midgard_instruction
*then_exit
= mir_last_in_block(ctx
->current_block
);
2648 /* Emit second block, and check if it's empty */
2650 int else_idx
= ctx
->block_count
;
2651 int count_in
= ctx
->instruction_count
;
2652 midgard_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
2653 midgard_block
*end_else_block
= ctx
->current_block
;
2654 int after_else_idx
= ctx
->block_count
;
2656 /* Now that we have the subblocks emitted, fix up the branches */
2661 if (ctx
->instruction_count
== count_in
) {
2662 /* The else block is empty, so don't emit an exit jump */
2663 mir_remove_instruction(then_exit
);
2664 then_branch
->branch
.target_block
= after_else_idx
;
2666 then_branch
->branch
.target_block
= else_idx
;
2667 then_exit
->branch
.target_block
= after_else_idx
;
2670 /* Wire up the successors */
2672 ctx
->after_block
= create_empty_block(ctx
);
2674 pan_block_add_successor(&before_block
->base
, &then_block
->base
);
2675 pan_block_add_successor(&before_block
->base
, &else_block
->base
);
2677 pan_block_add_successor(&end_then_block
->base
, &ctx
->after_block
->base
);
2678 pan_block_add_successor(&end_else_block
->base
, &ctx
->after_block
->base
);
2682 emit_loop(struct compiler_context
*ctx
, nir_loop
*nloop
)
2684 /* Remember where we are */
2685 midgard_block
*start_block
= ctx
->current_block
;
2687 /* Allocate a loop number, growing the current inner loop depth */
2688 int loop_idx
= ++ctx
->current_loop_depth
;
2690 /* Get index from before the body so we can loop back later */
2691 int start_idx
= ctx
->block_count
;
2693 /* Emit the body itself */
2694 midgard_block
*loop_block
= emit_cf_list(ctx
, &nloop
->body
);
2696 /* Branch back to loop back */
2697 struct midgard_instruction br_back
= v_branch(false, false);
2698 br_back
.branch
.target_block
= start_idx
;
2699 emit_mir_instruction(ctx
, br_back
);
2701 /* Mark down that branch in the graph. */
2702 pan_block_add_successor(&start_block
->base
, &loop_block
->base
);
2703 pan_block_add_successor(&ctx
->current_block
->base
, &loop_block
->base
);
2705 /* Find the index of the block about to follow us (note: we don't add
2706 * one; blocks are 0-indexed so we get a fencepost problem) */
2707 int break_block_idx
= ctx
->block_count
;
2709 /* Fix up the break statements we emitted to point to the right place,
2710 * now that we can allocate a block number for them */
2711 ctx
->after_block
= create_empty_block(ctx
);
2713 mir_foreach_block_from(ctx
, start_block
, _block
) {
2714 mir_foreach_instr_in_block(((midgard_block
*) _block
), ins
) {
2715 if (ins
->type
!= TAG_ALU_4
) continue;
2716 if (!ins
->compact_branch
) continue;
2718 /* We found a branch -- check the type to see if we need to do anything */
2719 if (ins
->branch
.target_type
!= TARGET_BREAK
) continue;
2721 /* It's a break! Check if it's our break */
2722 if (ins
->branch
.target_break
!= loop_idx
) continue;
2724 /* Okay, cool, we're breaking out of this loop.
2725 * Rewrite from a break to a goto */
2727 ins
->branch
.target_type
= TARGET_GOTO
;
2728 ins
->branch
.target_block
= break_block_idx
;
2730 pan_block_add_successor(_block
, &ctx
->after_block
->base
);
2734 /* Now that we've finished emitting the loop, free up the depth again
2735 * so we play nice with recursion amid nested loops */
2736 --ctx
->current_loop_depth
;
2738 /* Dump loop stats */
2742 static midgard_block
*
2743 emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
)
2745 midgard_block
*start_block
= NULL
;
2747 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2748 switch (node
->type
) {
2749 case nir_cf_node_block
: {
2750 midgard_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
2753 start_block
= block
;
2758 case nir_cf_node_if
:
2759 emit_if(ctx
, nir_cf_node_as_if(node
));
2762 case nir_cf_node_loop
:
2763 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2766 case nir_cf_node_function
:
2775 /* Due to lookahead, we need to report the first tag executed in the command
2776 * stream and in branch targets. An initial block might be empty, so iterate
2777 * until we find one that 'works' */
2780 midgard_get_first_tag_from_block(compiler_context
*ctx
, unsigned block_idx
)
2782 midgard_block
*initial_block
= mir_get_block(ctx
, block_idx
);
2784 mir_foreach_block_from(ctx
, initial_block
, _v
) {
2785 midgard_block
*v
= (midgard_block
*) _v
;
2786 if (v
->quadword_count
) {
2787 midgard_bundle
*initial_bundle
=
2788 util_dynarray_element(&v
->bundles
, midgard_bundle
, 0);
2790 return initial_bundle
->tag
;
2794 /* Default to a tag 1 which will break from the shader, in case we jump
2795 * to the exit block (i.e. `return` in a compute shader) */
2800 /* For each fragment writeout instruction, generate a writeout loop to
2801 * associate with it */
2804 mir_add_writeout_loops(compiler_context
*ctx
)
2806 for (unsigned rt
= 0; rt
< ARRAY_SIZE(ctx
->writeout_branch
); ++rt
) {
2807 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2810 unsigned popped
= br
->branch
.target_block
;
2811 pan_block_add_successor(&(mir_get_block(ctx
, popped
- 1)->base
), &ctx
->current_block
->base
);
2812 br
->branch
.target_block
= emit_fragment_epilogue(ctx
, rt
);
2813 br
->branch
.target_type
= TARGET_GOTO
;
2815 /* If we have more RTs, we'll need to restore back after our
2816 * loop terminates */
2818 if ((rt
+ 1) < ARRAY_SIZE(ctx
->writeout_branch
) && ctx
->writeout_branch
[rt
+ 1]) {
2819 midgard_instruction uncond
= v_branch(false, false);
2820 uncond
.branch
.target_block
= popped
;
2821 uncond
.branch
.target_type
= TARGET_GOTO
;
2822 emit_mir_instruction(ctx
, uncond
);
2823 pan_block_add_successor(&ctx
->current_block
->base
, &(mir_get_block(ctx
, popped
)->base
));
2824 schedule_barrier(ctx
);
2826 /* We're last, so we can terminate here */
2827 br
->last_writeout
= true;
2833 midgard_compile_shader_nir(nir_shader
*nir
, panfrost_program
*program
, bool is_blend
, unsigned blend_rt
, unsigned gpu_id
, bool shaderdb
, bool silent
)
2835 struct util_dynarray
*compiled
= &program
->compiled
;
2837 midgard_debug
= debug_get_option_midgard_debug();
2839 /* TODO: Bound against what? */
2840 compiler_context
*ctx
= rzalloc(NULL
, compiler_context
);
2843 ctx
->stage
= nir
->info
.stage
;
2844 ctx
->is_blend
= is_blend
;
2845 ctx
->blend_rt
= MIDGARD_COLOR_RT0
+ blend_rt
;
2846 ctx
->blend_input
= ~0;
2847 ctx
->blend_src1
= ~0;
2848 ctx
->quirks
= midgard_get_quirks(gpu_id
);
2850 /* Start off with a safe cutoff, allowing usage of all 16 work
2851 * registers. Later, we'll promote uniform reads to uniform registers
2852 * if we determine it is beneficial to do so */
2853 ctx
->uniform_cutoff
= 8;
2855 /* Initialize at a global (not block) level hash tables */
2857 ctx
->ssa_constants
= _mesa_hash_table_u64_create(NULL
);
2859 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2860 * (so we don't accidentally duplicate the epilogue since mesa/st has
2861 * messed with our I/O quite a bit already) */
2863 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2865 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
2866 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
2867 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
2870 NIR_PASS_V(nir
, nir_lower_var_copies
);
2871 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2872 NIR_PASS_V(nir
, nir_split_var_copies
);
2873 NIR_PASS_V(nir
, nir_lower_var_copies
);
2874 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
2875 NIR_PASS_V(nir
, nir_lower_var_copies
);
2876 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2878 unsigned pan_quirks
= panfrost_get_quirks(gpu_id
);
2879 NIR_PASS_V(nir
, pan_lower_framebuffer
,
2880 program
->rt_formats
, is_blend
, pan_quirks
);
2882 NIR_PASS_V(nir
, nir_lower_io
, nir_var_shader_in
| nir_var_shader_out
,
2884 NIR_PASS_V(nir
, nir_lower_ssbo
);
2885 NIR_PASS_V(nir
, midgard_nir_lower_zs_store
);
2887 /* Optimisation passes */
2889 optimise_nir(nir
, ctx
->quirks
, is_blend
);
2891 NIR_PASS_V(nir
, midgard_nir_reorder_writeout
);
2893 if ((midgard_debug
& MIDGARD_DBG_SHADERS
) && !silent
) {
2894 nir_print_shader(nir
, stdout
);
2897 /* Assign sysvals and counts, now that we're sure
2898 * (post-optimisation) */
2900 panfrost_nir_assign_sysvals(&ctx
->sysvals
, ctx
, nir
);
2901 program
->sysval_count
= ctx
->sysvals
.sysval_count
;
2902 memcpy(program
->sysvals
, ctx
->sysvals
.sysvals
, sizeof(ctx
->sysvals
.sysvals
[0]) * ctx
->sysvals
.sysval_count
);
2904 nir_foreach_function(func
, nir
) {
2908 list_inithead(&ctx
->blocks
);
2909 ctx
->block_count
= 0;
2911 ctx
->already_emitted
= calloc(BITSET_WORDS(func
->impl
->ssa_alloc
), sizeof(BITSET_WORD
));
2913 if (nir
->info
.outputs_read
&& !is_blend
) {
2914 emit_block_init(ctx
);
2916 struct midgard_instruction wait
= v_branch(false, false);
2917 wait
.branch
.target_type
= TARGET_TILEBUF_WAIT
;
2919 emit_mir_instruction(ctx
, wait
);
2921 ++ctx
->instruction_count
;
2924 emit_cf_list(ctx
, &func
->impl
->body
);
2925 free(ctx
->already_emitted
);
2926 break; /* TODO: Multi-function shaders */
2929 util_dynarray_init(compiled
, NULL
);
2931 /* Per-block lowering before opts */
2933 mir_foreach_block(ctx
, _block
) {
2934 midgard_block
*block
= (midgard_block
*) _block
;
2935 inline_alu_constants(ctx
, block
);
2936 embedded_to_inline_constant(ctx
, block
);
2938 /* MIR-level optimizations */
2940 bool progress
= false;
2944 progress
|= midgard_opt_dead_code_eliminate(ctx
);
2946 mir_foreach_block(ctx
, _block
) {
2947 midgard_block
*block
= (midgard_block
*) _block
;
2948 progress
|= midgard_opt_copy_prop(ctx
, block
);
2949 progress
|= midgard_opt_combine_projection(ctx
, block
);
2950 progress
|= midgard_opt_varying_projection(ctx
, block
);
2954 mir_foreach_block(ctx
, _block
) {
2955 midgard_block
*block
= (midgard_block
*) _block
;
2956 midgard_lower_derivatives(ctx
, block
);
2957 midgard_legalize_invert(ctx
, block
);
2958 midgard_cull_dead_branch(ctx
, block
);
2961 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2962 mir_add_writeout_loops(ctx
);
2964 /* Analyze now that the code is known but before scheduling creates
2965 * pipeline registers which are harder to track */
2966 mir_analyze_helper_terminate(ctx
);
2967 mir_analyze_helper_requirements(ctx
);
2970 midgard_schedule_program(ctx
);
2973 /* Emit flat binary from the instruction arrays. Iterate each block in
2974 * sequence. Save instruction boundaries such that lookahead tags can
2975 * be assigned easily */
2977 /* Cache _all_ bundles in source order for lookahead across failed branches */
2979 int bundle_count
= 0;
2980 mir_foreach_block(ctx
, _block
) {
2981 midgard_block
*block
= (midgard_block
*) _block
;
2982 bundle_count
+= block
->bundles
.size
/ sizeof(midgard_bundle
);
2984 midgard_bundle
**source_order_bundles
= malloc(sizeof(midgard_bundle
*) * bundle_count
);
2986 mir_foreach_block(ctx
, _block
) {
2987 midgard_block
*block
= (midgard_block
*) _block
;
2988 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2989 source_order_bundles
[bundle_idx
++] = bundle
;
2993 int current_bundle
= 0;
2995 /* Midgard prefetches instruction types, so during emission we
2996 * need to lookahead. Unless this is the last instruction, in
2997 * which we return 1. */
2999 mir_foreach_block(ctx
, _block
) {
3000 midgard_block
*block
= (midgard_block
*) _block
;
3001 mir_foreach_bundle_in_block(block
, bundle
) {
3004 if (!bundle
->last_writeout
&& (current_bundle
+ 1 < bundle_count
))
3005 lookahead
= source_order_bundles
[current_bundle
+ 1]->tag
;
3007 emit_binary_bundle(ctx
, block
, bundle
, compiled
, lookahead
);
3011 /* TODO: Free deeper */
3012 //util_dynarray_fini(&block->instructions);
3015 free(source_order_bundles
);
3017 /* Report the very first tag executed */
3018 program
->first_tag
= midgard_get_first_tag_from_block(ctx
, 0);
3020 /* Deal with off-by-one related to the fencepost problem */
3021 program
->work_register_count
= ctx
->work_registers
+ 1;
3022 program
->uniform_cutoff
= ctx
->uniform_cutoff
;
3024 program
->blend_patch_offset
= ctx
->blend_constant_offset
;
3025 program
->tls_size
= ctx
->tls_size
;
3027 if ((midgard_debug
& MIDGARD_DBG_SHADERS
) && !silent
)
3028 disassemble_midgard(stdout
, program
->compiled
.data
, program
->compiled
.size
, gpu_id
, ctx
->stage
);
3030 if ((midgard_debug
& MIDGARD_DBG_SHADERDB
|| shaderdb
) && !silent
) {
3031 unsigned nr_bundles
= 0, nr_ins
= 0;
3033 /* Count instructions and bundles */
3035 mir_foreach_block(ctx
, _block
) {
3036 midgard_block
*block
= (midgard_block
*) _block
;
3037 nr_bundles
+= util_dynarray_num_elements(
3038 &block
->bundles
, midgard_bundle
);
3040 mir_foreach_bundle_in_block(block
, bun
)
3041 nr_ins
+= bun
->instruction_count
;
3044 /* Calculate thread count. There are certain cutoffs by
3045 * register count for thread count */
3047 unsigned nr_registers
= program
->work_register_count
;
3049 unsigned nr_threads
=
3050 (nr_registers
<= 4) ? 4 :
3051 (nr_registers
<= 8) ? 2 :
3056 fprintf(stderr
, "shader%d - %s shader: "
3057 "%u inst, %u bundles, %u quadwords, "
3058 "%u registers, %u threads, %u loops, "
3059 "%u:%u spills:fills\n",
3061 ctx
->is_blend
? "PAN_SHADER_BLEND" :
3062 gl_shader_stage_name(ctx
->stage
),
3063 nr_ins
, nr_bundles
, ctx
->quadword_count
,
3064 nr_registers
, nr_threads
,
3066 ctx
->spills
, ctx
->fills
);