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
);
465 mdg_is_64(const nir_instr
*instr
, const void *_unused
)
467 const nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
469 if (nir_dest_bit_size(alu
->dest
.dest
) == 64)
473 case nir_op_umul_high
:
474 case nir_op_imul_high
:
481 /* Flushes undefined values to zero */
484 optimise_nir(nir_shader
*nir
, unsigned quirks
, bool is_blend
)
487 unsigned lower_flrp
=
488 (nir
->options
->lower_flrp16
? 16 : 0) |
489 (nir
->options
->lower_flrp32
? 32 : 0) |
490 (nir
->options
->lower_flrp64
? 64 : 0);
492 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
493 NIR_PASS(progress
, nir
, nir_lower_idiv
, nir_lower_idiv_fast
);
495 nir_lower_tex_options lower_tex_options
= {
496 .lower_txs_lod
= true,
498 .lower_tex_without_implicit_lod
=
499 (quirks
& MIDGARD_EXPLICIT_LOD
),
501 /* TODO: we have native gradient.. */
505 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
507 /* Must lower fdot2 after tex is lowered */
508 NIR_PASS(progress
, nir
, midgard_nir_lower_fdot2
);
510 /* T720 is broken. */
512 if (quirks
& MIDGARD_BROKEN_LOD
)
513 NIR_PASS_V(nir
, midgard_nir_lod_errata
);
515 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_early
);
520 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
521 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
523 NIR_PASS(progress
, nir
, nir_copy_prop
);
524 NIR_PASS(progress
, nir
, nir_opt_remove_phis
);
525 NIR_PASS(progress
, nir
, nir_opt_dce
);
526 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
527 NIR_PASS(progress
, nir
, nir_opt_cse
);
528 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
529 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
530 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
532 if (lower_flrp
!= 0) {
533 bool lower_flrp_progress
= false;
534 NIR_PASS(lower_flrp_progress
,
538 false /* always_precise */,
539 nir
->options
->lower_ffma
);
540 if (lower_flrp_progress
) {
541 NIR_PASS(progress
, nir
,
542 nir_opt_constant_folding
);
546 /* Nothing should rematerialize any flrps, so we only
547 * need to do this lowering once.
552 NIR_PASS(progress
, nir
, nir_opt_undef
);
553 NIR_PASS(progress
, nir
, nir_undef_to_zero
);
555 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
558 nir_var_function_temp
);
560 NIR_PASS(progress
, nir
, nir_opt_vectorize
);
563 NIR_PASS_V(nir
, nir_lower_alu_to_scalar
, mdg_is_64
, NULL
);
565 /* Run after opts so it can hit more */
567 NIR_PASS(progress
, nir
, nir_fuse_io_16
);
569 /* Must be run at the end to prevent creation of fsin/fcos ops */
570 NIR_PASS(progress
, nir
, midgard_nir_scale_trig
);
575 NIR_PASS(progress
, nir
, nir_opt_dce
);
576 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
577 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
578 NIR_PASS(progress
, nir
, nir_copy_prop
);
581 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
582 NIR_PASS(progress
, nir
, nir_opt_algebraic_distribute_src_mods
);
584 /* We implement booleans as 32-bit 0/~0 */
585 NIR_PASS(progress
, nir
, nir_lower_bool_to_int32
);
587 /* Now that booleans are lowered, we can run out late opts */
588 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_late
);
589 NIR_PASS(progress
, nir
, midgard_nir_cancel_inot
);
591 NIR_PASS(progress
, nir
, nir_copy_prop
);
592 NIR_PASS(progress
, nir
, nir_opt_dce
);
594 /* Take us out of SSA */
595 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
596 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
598 /* We are a vector architecture; write combine where possible */
599 NIR_PASS(progress
, nir
, nir_move_vec_src_uses_to_dest
);
600 NIR_PASS(progress
, nir
, nir_lower_vec_to_movs
);
602 NIR_PASS(progress
, nir
, nir_opt_dce
);
605 /* Do not actually emit a load; instead, cache the constant for inlining */
608 emit_load_const(compiler_context
*ctx
, nir_load_const_instr
*instr
)
610 nir_ssa_def def
= instr
->def
;
612 midgard_constants
*consts
= rzalloc(NULL
, midgard_constants
);
614 assert(instr
->def
.num_components
* instr
->def
.bit_size
<= sizeof(*consts
) * 8);
616 #define RAW_CONST_COPY(bits) \
617 nir_const_value_to_array(consts->u##bits, instr->value, \
618 instr->def.num_components, u##bits)
620 switch (instr
->def
.bit_size
) {
634 unreachable("Invalid bit_size for load_const instruction\n");
637 /* Shifted for SSA, +1 for off-by-one */
638 _mesa_hash_table_u64_insert(ctx
->ssa_constants
, (def
.index
<< 1) + 1, consts
);
641 /* Normally constants are embedded implicitly, but for I/O and such we have to
642 * explicitly emit a move with the constant source */
645 emit_explicit_constant(compiler_context
*ctx
, unsigned node
, unsigned to
)
647 void *constant_value
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, node
+ 1);
649 if (constant_value
) {
650 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), to
);
651 attach_constants(ctx
, &ins
, constant_value
, node
+ 1);
652 emit_mir_instruction(ctx
, ins
);
657 nir_is_non_scalar_swizzle(nir_alu_src
*src
, unsigned nr_components
)
659 unsigned comp
= src
->swizzle
[0];
661 for (unsigned c
= 1; c
< nr_components
; ++c
) {
662 if (src
->swizzle
[c
] != comp
)
669 #define ALU_CASE(nir, _op) \
671 op = midgard_alu_op_##_op; \
672 assert(src_bitsize == dst_bitsize); \
675 #define ALU_CASE_RTZ(nir, _op) \
677 op = midgard_alu_op_##_op; \
678 roundmode = MIDGARD_RTZ; \
681 #define ALU_CHECK_CMP(sext) \
682 assert(src_bitsize == 16 || src_bitsize == 32); \
683 assert(dst_bitsize == 16 || dst_bitsize == 32); \
685 #define ALU_CASE_BCAST(nir, _op, count) \
687 op = midgard_alu_op_##_op; \
688 broadcast_swizzle = count; \
689 ALU_CHECK_CMP(true); \
692 #define ALU_CASE_CMP(nir, _op, sext) \
694 op = midgard_alu_op_##_op; \
695 ALU_CHECK_CMP(sext); \
698 /* Compare mir_lower_invert */
700 nir_accepts_inot(nir_op op
, unsigned src
)
704 case nir_op_iand
: /* TODO: b2f16 */
708 /* Only the condition */
716 mir_accept_dest_mod(compiler_context
*ctx
, nir_dest
**dest
, nir_op op
)
718 if (pan_has_dest_mod(dest
, op
)) {
719 assert((*dest
)->is_ssa
);
720 BITSET_SET(ctx
->already_emitted
, (*dest
)->ssa
.index
);
727 /* Look for floating point mods. We have the mods fsat, fsat_signed,
728 * and fpos. We also have the relations (note 3 * 2 = 6 cases):
730 * fsat_signed(fpos(x)) = fsat(x)
731 * fsat_signed(fsat(x)) = fsat(x)
732 * fpos(fsat_signed(x)) = fsat(x)
733 * fpos(fsat(x)) = fsat(x)
734 * fsat(fsat_signed(x)) = fsat(x)
735 * fsat(fpos(x)) = fsat(x)
737 * So by cases any composition of output modifiers is equivalent to
741 mir_determine_float_outmod(compiler_context
*ctx
, nir_dest
**dest
, unsigned prior_outmod
)
743 bool fpos
= mir_accept_dest_mod(ctx
, dest
, nir_op_fclamp_pos
);
744 bool fsat
= mir_accept_dest_mod(ctx
, dest
, nir_op_fsat
);
745 bool ssat
= mir_accept_dest_mod(ctx
, dest
, nir_op_fsat_signed
);
746 bool prior
= (prior_outmod
!= midgard_outmod_none
);
747 int count
= (int) prior
+ (int) fpos
+ (int) ssat
+ (int) fsat
;
749 return ((count
> 1) || fsat
) ? midgard_outmod_sat
:
750 fpos
? midgard_outmod_pos
:
751 ssat
? midgard_outmod_sat_signed
:
756 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
)
758 nir_alu_src src
= instr
->src
[i
];
761 if (pan_has_source_mod(&src
, nir_op_fneg
))
764 if (pan_has_source_mod(&src
, nir_op_fabs
))
768 if (nir_accepts_inot(instr
->op
, i
) && pan_has_source_mod(&src
, nir_op_inot
))
772 if (pan_has_source_mod(&src
, nir_op_fround_even
))
773 *roundmode
= MIDGARD_RTE
;
775 if (pan_has_source_mod(&src
, nir_op_ftrunc
))
776 *roundmode
= MIDGARD_RTZ
;
778 if (pan_has_source_mod(&src
, nir_op_ffloor
))
779 *roundmode
= MIDGARD_RTN
;
781 if (pan_has_source_mod(&src
, nir_op_fceil
))
782 *roundmode
= MIDGARD_RTP
;
785 unsigned bits
= nir_src_bit_size(src
.src
);
787 ins
->src
[to
] = nir_src_index(NULL
, &src
.src
);
788 ins
->src_types
[to
] = nir_op_infos
[instr
->op
].input_types
[i
] | bits
;
790 for (unsigned c
= 0; c
< NIR_MAX_VEC_COMPONENTS
; ++c
) {
791 ins
->swizzle
[to
][c
] = src
.swizzle
[
792 (!bcast_count
|| c
< bcast_count
) ? c
:
797 /* Midgard features both fcsel and icsel, depending on whether you want int or
798 * float modifiers. NIR's csel is typeless, so we want a heuristic to guess if
799 * we should emit an int or float csel depending on what modifiers could be
800 * placed. In the absense of modifiers, this is probably arbitrary. */
803 mir_is_bcsel_float(nir_alu_instr
*instr
)
806 nir_op_i2i8
, nir_op_i2i16
,
807 nir_op_i2i32
, nir_op_i2i64
810 nir_op floatmods
[] = {
811 nir_op_fabs
, nir_op_fneg
,
812 nir_op_f2f16
, nir_op_f2f32
,
816 nir_op floatdestmods
[] = {
817 nir_op_fsat
, nir_op_fsat_signed
, nir_op_fclamp_pos
,
818 nir_op_f2f16
, nir_op_f2f32
823 for (unsigned i
= 1; i
< 3; ++i
) {
824 nir_alu_src s
= instr
->src
[i
];
825 for (unsigned q
= 0; q
< ARRAY_SIZE(intmods
); ++q
) {
826 if (pan_has_source_mod(&s
, intmods
[q
]))
831 for (unsigned i
= 1; i
< 3; ++i
) {
832 nir_alu_src s
= instr
->src
[i
];
833 for (unsigned q
= 0; q
< ARRAY_SIZE(floatmods
); ++q
) {
834 if (pan_has_source_mod(&s
, floatmods
[q
]))
839 for (unsigned q
= 0; q
< ARRAY_SIZE(floatdestmods
); ++q
) {
840 nir_dest
*dest
= &instr
->dest
.dest
;
841 if (pan_has_dest_mod(&dest
, floatdestmods
[q
]))
849 emit_alu(compiler_context
*ctx
, nir_alu_instr
*instr
)
851 nir_dest
*dest
= &instr
->dest
.dest
;
853 if (dest
->is_ssa
&& BITSET_TEST(ctx
->already_emitted
, dest
->ssa
.index
))
856 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
857 * is handled elsewhere */
859 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddy
) {
860 midgard_emit_derivatives(ctx
, instr
);
864 bool is_ssa
= dest
->is_ssa
;
866 unsigned nr_components
= nir_dest_num_components(*dest
);
867 unsigned nr_inputs
= nir_op_infos
[instr
->op
].num_inputs
;
870 /* Number of components valid to check for the instruction (the rest
871 * will be forced to the last), or 0 to use as-is. Relevant as
872 * ball-type instructions have a channel count in NIR but are all vec4
875 unsigned broadcast_swizzle
= 0;
877 /* Should we swap arguments? */
878 bool flip_src12
= false;
880 ASSERTED
unsigned src_bitsize
= nir_src_bit_size(instr
->src
[0].src
);
881 ASSERTED
unsigned dst_bitsize
= nir_dest_bit_size(*dest
);
883 enum midgard_roundmode roundmode
= MIDGARD_RTE
;
886 ALU_CASE(fadd
, fadd
);
887 ALU_CASE(fmul
, fmul
);
888 ALU_CASE(fmin
, fmin
);
889 ALU_CASE(fmax
, fmax
);
890 ALU_CASE(imin
, imin
);
891 ALU_CASE(imax
, imax
);
892 ALU_CASE(umin
, umin
);
893 ALU_CASE(umax
, umax
);
894 ALU_CASE(ffloor
, ffloor
);
895 ALU_CASE(fround_even
, froundeven
);
896 ALU_CASE(ftrunc
, ftrunc
);
897 ALU_CASE(fceil
, fceil
);
898 ALU_CASE(fdot3
, fdot3
);
899 ALU_CASE(fdot4
, fdot4
);
900 ALU_CASE(iadd
, iadd
);
901 ALU_CASE(isub
, isub
);
902 ALU_CASE(imul
, imul
);
903 ALU_CASE(imul_high
, imul
);
904 ALU_CASE(umul_high
, imul
);
906 /* Zero shoved as second-arg */
907 ALU_CASE(iabs
, iabsdiff
);
911 ALU_CASE_CMP(feq32
, feq
, false);
912 ALU_CASE_CMP(fneu32
, fne
, false);
913 ALU_CASE_CMP(flt32
, flt
, false);
914 ALU_CASE_CMP(ieq32
, ieq
, true);
915 ALU_CASE_CMP(ine32
, ine
, true);
916 ALU_CASE_CMP(ilt32
, ilt
, true);
917 ALU_CASE_CMP(ult32
, ult
, false);
919 /* We don't have a native b2f32 instruction. Instead, like many
920 * GPUs, we exploit booleans as 0/~0 for false/true, and
921 * correspondingly AND
922 * by 1.0 to do the type conversion. For the moment, prime us
925 * iand [whatever], #0
927 * At the end of emit_alu (as MIR), we'll fix-up the constant
930 ALU_CASE_CMP(b2f32
, iand
, true);
931 ALU_CASE_CMP(b2f16
, iand
, true);
932 ALU_CASE_CMP(b2i32
, iand
, true);
934 /* Likewise, we don't have a dedicated f2b32 instruction, but
935 * we can do a "not equal to 0.0" test. */
937 ALU_CASE_CMP(f2b32
, fne
, false);
938 ALU_CASE_CMP(i2b32
, ine
, true);
940 ALU_CASE(frcp
, frcp
);
941 ALU_CASE(frsq
, frsqrt
);
942 ALU_CASE(fsqrt
, fsqrt
);
943 ALU_CASE(fexp2
, fexp2
);
944 ALU_CASE(flog2
, flog2
);
946 ALU_CASE_RTZ(f2i64
, f2i_rte
);
947 ALU_CASE_RTZ(f2u64
, f2u_rte
);
948 ALU_CASE_RTZ(i2f64
, i2f_rte
);
949 ALU_CASE_RTZ(u2f64
, u2f_rte
);
951 ALU_CASE_RTZ(f2i32
, f2i_rte
);
952 ALU_CASE_RTZ(f2u32
, f2u_rte
);
953 ALU_CASE_RTZ(i2f32
, i2f_rte
);
954 ALU_CASE_RTZ(u2f32
, u2f_rte
);
956 ALU_CASE_RTZ(f2i8
, f2i_rte
);
957 ALU_CASE_RTZ(f2u8
, f2u_rte
);
959 ALU_CASE_RTZ(f2i16
, f2i_rte
);
960 ALU_CASE_RTZ(f2u16
, f2u_rte
);
961 ALU_CASE_RTZ(i2f16
, i2f_rte
);
962 ALU_CASE_RTZ(u2f16
, u2f_rte
);
964 ALU_CASE(fsin
, fsin
);
965 ALU_CASE(fcos
, fcos
);
967 /* We'll get 0 in the second arg, so:
968 * ~a = ~(a | 0) = nor(a, 0) */
969 ALU_CASE(inot
, inor
);
970 ALU_CASE(iand
, iand
);
972 ALU_CASE(ixor
, ixor
);
973 ALU_CASE(ishl
, ishl
);
974 ALU_CASE(ishr
, iasr
);
975 ALU_CASE(ushr
, ilsr
);
977 ALU_CASE_BCAST(b32all_fequal2
, fball_eq
, 2);
978 ALU_CASE_BCAST(b32all_fequal3
, fball_eq
, 3);
979 ALU_CASE_CMP(b32all_fequal4
, fball_eq
, true);
981 ALU_CASE_BCAST(b32any_fnequal2
, fbany_neq
, 2);
982 ALU_CASE_BCAST(b32any_fnequal3
, fbany_neq
, 3);
983 ALU_CASE_CMP(b32any_fnequal4
, fbany_neq
, true);
985 ALU_CASE_BCAST(b32all_iequal2
, iball_eq
, 2);
986 ALU_CASE_BCAST(b32all_iequal3
, iball_eq
, 3);
987 ALU_CASE_CMP(b32all_iequal4
, iball_eq
, true);
989 ALU_CASE_BCAST(b32any_inequal2
, ibany_neq
, 2);
990 ALU_CASE_BCAST(b32any_inequal3
, ibany_neq
, 3);
991 ALU_CASE_CMP(b32any_inequal4
, ibany_neq
, true);
993 /* Source mods will be shoved in later */
994 ALU_CASE(fabs
, fmov
);
995 ALU_CASE(fneg
, fmov
);
996 ALU_CASE(fsat
, fmov
);
997 ALU_CASE(fsat_signed
, fmov
);
998 ALU_CASE(fclamp_pos
, fmov
);
1000 /* For size conversion, we use a move. Ideally though we would squash
1001 * these ops together; maybe that has to happen after in NIR as part of
1002 * propagation...? An earlier algebraic pass ensured we step down by
1003 * only / exactly one size. If stepping down, we use a dest override to
1004 * reduce the size; if stepping up, we use a larger-sized move with a
1005 * half source and a sign/zero-extension modifier */
1017 case nir_op_f2f64
: {
1018 if (instr
->op
== nir_op_f2f16
|| instr
->op
== nir_op_f2f32
||
1019 instr
->op
== nir_op_f2f64
)
1020 op
= midgard_alu_op_fmov
;
1022 op
= midgard_alu_op_imov
;
1027 /* For greater-or-equal, we lower to less-or-equal and flip the
1033 case nir_op_uge32
: {
1035 instr
->op
== nir_op_fge
? midgard_alu_op_fle
:
1036 instr
->op
== nir_op_fge32
? midgard_alu_op_fle
:
1037 instr
->op
== nir_op_ige32
? midgard_alu_op_ile
:
1038 instr
->op
== nir_op_uge32
? midgard_alu_op_ule
:
1042 ALU_CHECK_CMP(false);
1046 case nir_op_b32csel
: {
1047 bool mixed
= nir_is_non_scalar_swizzle(&instr
->src
[0], nr_components
);
1048 bool is_float
= mir_is_bcsel_float(instr
);
1050 (mixed
? midgard_alu_op_fcsel_v
: midgard_alu_op_fcsel
) :
1051 (mixed
? midgard_alu_op_icsel_v
: midgard_alu_op_icsel
);
1056 case nir_op_unpack_32_2x16
:
1057 case nir_op_unpack_32_4x8
:
1058 case nir_op_pack_32_2x16
:
1059 case nir_op_pack_32_4x8
: {
1060 op
= midgard_alu_op_imov
;
1065 DBG("Unhandled ALU op %s\n", nir_op_infos
[instr
->op
].name
);
1070 /* Promote imov to fmov if it might help inline a constant */
1071 if (op
== midgard_alu_op_imov
&& nir_src_is_const(instr
->src
[0].src
)
1072 && nir_src_bit_size(instr
->src
[0].src
) == 32
1073 && nir_is_same_comp_swizzle(instr
->src
[0].swizzle
,
1074 nir_src_num_components(instr
->src
[0].src
))) {
1075 op
= midgard_alu_op_fmov
;
1078 /* Midgard can perform certain modifiers on output of an ALU op */
1080 unsigned outmod
= 0;
1081 bool is_int
= midgard_is_integer_op(op
);
1083 if (instr
->op
== nir_op_umul_high
|| instr
->op
== nir_op_imul_high
) {
1084 outmod
= midgard_outmod_int_high
;
1085 } else if (midgard_is_integer_out_op(op
)) {
1086 outmod
= midgard_outmod_int_wrap
;
1087 } else if (instr
->op
== nir_op_fsat
) {
1088 outmod
= midgard_outmod_sat
;
1089 } else if (instr
->op
== nir_op_fsat_signed
) {
1090 outmod
= midgard_outmod_sat_signed
;
1091 } else if (instr
->op
== nir_op_fclamp_pos
) {
1092 outmod
= midgard_outmod_pos
;
1095 /* Fetch unit, quirks, etc information */
1096 unsigned opcode_props
= alu_opcode_props
[op
].props
;
1097 bool quirk_flipped_r24
= opcode_props
& QUIRK_FLIPPED_R24
;
1099 if (!midgard_is_integer_out_op(op
)) {
1100 outmod
= mir_determine_float_outmod(ctx
, &dest
, outmod
);
1103 midgard_instruction ins
= {
1105 .dest
= nir_dest_index(dest
),
1106 .dest_type
= nir_op_infos
[instr
->op
].output_type
1107 | nir_dest_bit_size(*dest
),
1108 .roundmode
= roundmode
,
1111 enum midgard_roundmode
*roundptr
= (opcode_props
& MIDGARD_ROUNDS
) ?
1112 &ins
.roundmode
: NULL
;
1114 for (unsigned i
= nr_inputs
; i
< ARRAY_SIZE(ins
.src
); ++i
)
1117 if (quirk_flipped_r24
) {
1119 mir_copy_src(&ins
, instr
, 0, 1, &ins
.src_abs
[1], &ins
.src_neg
[1], &ins
.src_invert
[1], roundptr
, is_int
, broadcast_swizzle
);
1121 for (unsigned i
= 0; i
< nr_inputs
; ++i
) {
1124 if (instr
->op
== nir_op_b32csel
) {
1125 /* The condition is the first argument; move
1126 * the other arguments up one to be a binary
1127 * instruction for Midgard with the condition
1132 else if (flip_src12
)
1136 } else if (flip_src12
) {
1140 mir_copy_src(&ins
, instr
, i
, to
, &ins
.src_abs
[to
], &ins
.src_neg
[to
], &ins
.src_invert
[to
], roundptr
, is_int
, broadcast_swizzle
);
1142 /* (!c) ? a : b = c ? b : a */
1143 if (instr
->op
== nir_op_b32csel
&& ins
.src_invert
[2]) {
1144 ins
.src_invert
[2] = false;
1150 if (instr
->op
== nir_op_fneg
|| instr
->op
== nir_op_fabs
) {
1151 /* Lowered to move */
1152 if (instr
->op
== nir_op_fneg
)
1153 ins
.src_neg
[1] ^= true;
1155 if (instr
->op
== nir_op_fabs
)
1156 ins
.src_abs
[1] = true;
1159 ins
.mask
= mask_of(nr_components
);
1161 /* Apply writemask if non-SSA, keeping in mind that we can't write to
1162 * components that don't exist. Note modifier => SSA => !reg => no
1163 * writemask, so we don't have to worry about writemasks here.*/
1166 ins
.mask
&= instr
->dest
.write_mask
;
1169 ins
.outmod
= outmod
;
1171 /* Late fixup for emulated instructions */
1173 if (instr
->op
== nir_op_b2f32
|| instr
->op
== nir_op_b2i32
) {
1174 /* Presently, our second argument is an inline #0 constant.
1175 * Switch over to an embedded 1.0 constant (that can't fit
1176 * inline, since we're 32-bit, not 16-bit like the inline
1179 ins
.has_inline_constant
= false;
1180 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1181 ins
.src_types
[1] = nir_type_float32
;
1182 ins
.has_constants
= true;
1184 if (instr
->op
== nir_op_b2f32
)
1185 ins
.constants
.f32
[0] = 1.0f
;
1187 ins
.constants
.i32
[0] = 1;
1189 for (unsigned c
= 0; c
< 16; ++c
)
1190 ins
.swizzle
[1][c
] = 0;
1191 } else if (instr
->op
== nir_op_b2f16
) {
1192 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1193 ins
.src_types
[1] = nir_type_float16
;
1194 ins
.has_constants
= true;
1195 ins
.constants
.i16
[0] = _mesa_float_to_half(1.0);
1197 for (unsigned c
= 0; c
< 16; ++c
)
1198 ins
.swizzle
[1][c
] = 0;
1199 } else if (nr_inputs
== 1 && !quirk_flipped_r24
) {
1200 /* Lots of instructions need a 0 plonked in */
1201 ins
.has_inline_constant
= false;
1202 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1203 ins
.src_types
[1] = ins
.src_types
[0];
1204 ins
.has_constants
= true;
1205 ins
.constants
.u32
[0] = 0;
1207 for (unsigned c
= 0; c
< 16; ++c
)
1208 ins
.swizzle
[1][c
] = 0;
1209 } else if (instr
->op
== nir_op_pack_32_2x16
) {
1210 ins
.dest_type
= nir_type_uint16
;
1211 ins
.mask
= mask_of(nr_components
* 2);
1213 } else if (instr
->op
== nir_op_pack_32_4x8
) {
1214 ins
.dest_type
= nir_type_uint8
;
1215 ins
.mask
= mask_of(nr_components
* 4);
1217 } else if (instr
->op
== nir_op_unpack_32_2x16
) {
1218 ins
.dest_type
= nir_type_uint32
;
1219 ins
.mask
= mask_of(nr_components
>> 1);
1221 } else if (instr
->op
== nir_op_unpack_32_4x8
) {
1222 ins
.dest_type
= nir_type_uint32
;
1223 ins
.mask
= mask_of(nr_components
>> 2);
1227 if ((opcode_props
& UNITS_ALL
) == UNIT_VLUT
) {
1228 /* To avoid duplicating the lookup tables (probably), true LUT
1229 * instructions can only operate as if they were scalars. Lower
1230 * them here by changing the component. */
1232 unsigned orig_mask
= ins
.mask
;
1234 unsigned swizzle_back
[MIR_VEC_COMPONENTS
];
1235 memcpy(&swizzle_back
, ins
.swizzle
[0], sizeof(swizzle_back
));
1237 midgard_instruction ins_split
[MIR_VEC_COMPONENTS
];
1238 unsigned ins_count
= 0;
1240 for (int i
= 0; i
< nr_components
; ++i
) {
1241 /* Mask the associated component, dropping the
1242 * instruction if needed */
1245 ins
.mask
&= orig_mask
;
1247 for (unsigned j
= 0; j
< ins_count
; ++j
) {
1248 if (swizzle_back
[i
] == ins_split
[j
].swizzle
[0][0]) {
1249 ins_split
[j
].mask
|= ins
.mask
;
1258 for (unsigned j
= 0; j
< MIR_VEC_COMPONENTS
; ++j
)
1259 ins
.swizzle
[0][j
] = swizzle_back
[i
]; /* Pull from the correct component */
1261 ins_split
[ins_count
] = ins
;
1266 for (unsigned i
= 0; i
< ins_count
; ++i
) {
1267 emit_mir_instruction(ctx
, ins_split
[i
]);
1270 emit_mir_instruction(ctx
, ins
);
1277 mir_set_intr_mask(nir_instr
*instr
, midgard_instruction
*ins
, bool is_read
)
1279 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
1280 unsigned nir_mask
= 0;
1284 nir_mask
= mask_of(nir_intrinsic_dest_components(intr
));
1285 dsize
= nir_dest_bit_size(intr
->dest
);
1287 nir_mask
= nir_intrinsic_write_mask(intr
);
1291 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1292 unsigned bytemask
= pan_to_bytemask(dsize
, nir_mask
);
1293 mir_set_bytemask(ins
, bytemask
);
1294 ins
->dest_type
= nir_type_uint
| dsize
;
1297 /* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1298 * optimized) versions of UBO #0 */
1300 static midgard_instruction
*
1302 compiler_context
*ctx
,
1306 nir_src
*indirect_offset
,
1307 unsigned indirect_shift
,
1310 /* TODO: half-floats */
1312 midgard_instruction ins
= m_ld_ubo_int4(dest
, 0);
1313 ins
.constants
.u32
[0] = offset
;
1315 if (instr
->type
== nir_instr_type_intrinsic
)
1316 mir_set_intr_mask(instr
, &ins
, true);
1318 if (indirect_offset
) {
1319 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1320 ins
.src_types
[2] = nir_type_uint32
;
1321 ins
.load_store
.arg_2
= (indirect_shift
<< 5);
1323 /* X component for the whole swizzle to prevent register
1324 * pressure from ballooning from the extra components */
1325 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[2]); ++i
)
1326 ins
.swizzle
[2][i
] = 0;
1328 ins
.load_store
.arg_2
= 0x1E;
1331 ins
.load_store
.arg_1
= index
;
1333 return emit_mir_instruction(ctx
, ins
);
1336 /* Globals are like UBOs if you squint. And shared memory is like globals if
1337 * you squint even harder */
1341 compiler_context
*ctx
,
1350 midgard_instruction ins
;
1353 ins
= m_ld_int4(srcdest
, 0);
1355 ins
= m_st_int4(srcdest
, 0);
1357 mir_set_offset(ctx
, &ins
, offset
, is_shared
);
1358 mir_set_intr_mask(instr
, &ins
, is_read
);
1360 emit_mir_instruction(ctx
, ins
);
1365 compiler_context
*ctx
,
1366 unsigned dest
, unsigned offset
,
1367 unsigned nr_comp
, unsigned component
,
1368 nir_src
*indirect_offset
, nir_alu_type type
, bool flat
)
1370 /* XXX: Half-floats? */
1371 /* TODO: swizzle, mask */
1373 midgard_instruction ins
= m_ld_vary_32(dest
, offset
);
1374 ins
.mask
= mask_of(nr_comp
);
1375 ins
.dest_type
= type
;
1377 if (type
== nir_type_float16
) {
1378 /* Ensure we are aligned so we can pack it later */
1379 ins
.mask
= mask_of(ALIGN_POT(nr_comp
, 2));
1382 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[0]); ++i
)
1383 ins
.swizzle
[0][i
] = MIN2(i
+ component
, COMPONENT_W
);
1385 midgard_varying_parameter p
= {
1387 .interpolation
= midgard_interp_default
,
1392 memcpy(&u
, &p
, sizeof(p
));
1393 ins
.load_store
.varying_parameters
= u
;
1395 if (indirect_offset
) {
1396 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1397 ins
.src_types
[2] = nir_type_uint32
;
1399 ins
.load_store
.arg_2
= 0x1E;
1401 ins
.load_store
.arg_1
= 0x9E;
1403 /* Use the type appropriate load */
1405 case nir_type_uint32
:
1406 case nir_type_bool32
:
1407 ins
.op
= midgard_op_ld_vary_32u
;
1409 case nir_type_int32
:
1410 ins
.op
= midgard_op_ld_vary_32i
;
1412 case nir_type_float32
:
1413 ins
.op
= midgard_op_ld_vary_32
;
1415 case nir_type_float16
:
1416 ins
.op
= midgard_op_ld_vary_16
;
1419 unreachable("Attempted to load unknown type");
1423 emit_mir_instruction(ctx
, ins
);
1428 compiler_context
*ctx
,
1429 unsigned dest
, unsigned offset
,
1430 unsigned nr_comp
, nir_alu_type t
)
1432 midgard_instruction ins
= m_ld_attr_32(dest
, offset
);
1433 ins
.load_store
.arg_1
= 0x1E;
1434 ins
.load_store
.arg_2
= 0x1E;
1435 ins
.mask
= mask_of(nr_comp
);
1437 /* Use the type appropriate load */
1441 ins
.op
= midgard_op_ld_attr_32u
;
1444 ins
.op
= midgard_op_ld_attr_32i
;
1446 case nir_type_float
:
1447 ins
.op
= midgard_op_ld_attr_32
;
1450 unreachable("Attempted to load unknown type");
1454 emit_mir_instruction(ctx
, ins
);
1458 emit_sysval_read(compiler_context
*ctx
, nir_instr
*instr
,
1459 unsigned nr_components
, unsigned offset
)
1463 /* Figure out which uniform this is */
1464 int sysval
= panfrost_sysval_for_instr(instr
, &nir_dest
);
1465 void *val
= _mesa_hash_table_u64_search(ctx
->sysvals
.sysval_to_id
, sysval
);
1467 unsigned dest
= nir_dest_index(&nir_dest
);
1469 /* Sysvals are prefix uniforms */
1470 unsigned uniform
= ((uintptr_t) val
) - 1;
1472 /* Emit the read itself -- this is never indirect */
1473 midgard_instruction
*ins
=
1474 emit_ubo_read(ctx
, instr
, dest
, (uniform
* 16) + offset
, NULL
, 0, 0);
1476 ins
->mask
= mask_of(nr_components
);
1480 compute_builtin_arg(nir_op op
)
1483 case nir_intrinsic_load_work_group_id
:
1485 case nir_intrinsic_load_local_invocation_id
:
1488 unreachable("Invalid compute paramater loaded");
1493 emit_fragment_store(compiler_context
*ctx
, unsigned src
, unsigned src_z
, unsigned src_s
, enum midgard_rt_id rt
)
1495 assert(rt
< ARRAY_SIZE(ctx
->writeout_branch
));
1497 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
1501 emit_explicit_constant(ctx
, src
, src
);
1503 struct midgard_instruction ins
=
1504 v_branch(false, false);
1506 bool depth_only
= (rt
== MIDGARD_ZS_RT
);
1508 ins
.writeout
= depth_only
? 0 : PAN_WRITEOUT_C
;
1510 /* Add dependencies */
1512 ins
.src_types
[0] = nir_type_uint32
;
1513 ins
.constants
.u32
[0] = depth_only
? 0xFF : (rt
- MIDGARD_COLOR_RT0
) * 0x100;
1514 for (int i
= 0; i
< 4; ++i
)
1515 ins
.swizzle
[0][i
] = i
;
1518 emit_explicit_constant(ctx
, src_z
, src_z
);
1520 ins
.src_types
[2] = nir_type_uint32
;
1521 ins
.writeout
|= PAN_WRITEOUT_Z
;
1524 emit_explicit_constant(ctx
, src_s
, src_s
);
1526 ins
.src_types
[3] = nir_type_uint32
;
1527 ins
.writeout
|= PAN_WRITEOUT_S
;
1530 /* Emit the branch */
1531 br
= emit_mir_instruction(ctx
, ins
);
1532 schedule_barrier(ctx
);
1533 ctx
->writeout_branch
[rt
] = br
;
1535 /* Push our current location = current block count - 1 = where we'll
1536 * jump to. Maybe a bit too clever for my own good */
1538 br
->branch
.target_block
= ctx
->block_count
- 1;
1542 emit_compute_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1544 unsigned reg
= nir_dest_index(&instr
->dest
);
1545 midgard_instruction ins
= m_ld_compute_id(reg
, 0);
1546 ins
.mask
= mask_of(3);
1547 ins
.swizzle
[0][3] = COMPONENT_X
; /* xyzx */
1548 ins
.load_store
.arg_1
= compute_builtin_arg(instr
->intrinsic
);
1549 emit_mir_instruction(ctx
, ins
);
1553 vertex_builtin_arg(nir_op op
)
1556 case nir_intrinsic_load_vertex_id
:
1557 return PAN_VERTEX_ID
;
1558 case nir_intrinsic_load_instance_id
:
1559 return PAN_INSTANCE_ID
;
1561 unreachable("Invalid vertex builtin");
1566 emit_vertex_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1568 unsigned reg
= nir_dest_index(&instr
->dest
);
1569 emit_attr_read(ctx
, reg
, vertex_builtin_arg(instr
->intrinsic
), 1, nir_type_int
);
1573 emit_msaa_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1575 unsigned reg
= nir_dest_index(&instr
->dest
);
1577 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1578 ld
.op
= midgard_op_ld_color_buffer_32u_old
;
1579 ld
.load_store
.address
= 97;
1580 ld
.load_store
.arg_2
= 0x1E;
1582 for (int i
= 0; i
< 4; ++i
)
1583 ld
.swizzle
[0][i
] = COMPONENT_X
;
1585 emit_mir_instruction(ctx
, ld
);
1589 emit_control_barrier(compiler_context
*ctx
)
1591 midgard_instruction ins
= {
1592 .type
= TAG_TEXTURE_4
,
1594 .src
= { ~0, ~0, ~0, ~0 },
1595 .op
= TEXTURE_OP_BARRIER
,
1598 emit_mir_instruction(ctx
, ins
);
1602 mir_get_branch_cond(nir_src
*src
, bool *invert
)
1604 /* Wrap it. No swizzle since it's a scalar */
1610 *invert
= pan_has_source_mod(&alu
, nir_op_inot
);
1611 return nir_src_index(NULL
, &alu
.src
);
1615 output_load_rt_addr(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1618 return ctx
->blend_rt
;
1620 const nir_variable
*var
;
1621 var
= search_var(ctx
->nir
, nir_var_shader_out
, nir_intrinsic_base(instr
));
1624 unsigned loc
= var
->data
.location
;
1626 if (loc
== FRAG_RESULT_COLOR
)
1627 loc
= FRAG_RESULT_DATA0
;
1629 if (loc
>= FRAG_RESULT_DATA0
)
1630 return loc
- FRAG_RESULT_DATA0
;
1632 if (loc
== FRAG_RESULT_DEPTH
)
1634 if (loc
== FRAG_RESULT_STENCIL
)
1637 unreachable("Invalid RT to load from");
1641 emit_intrinsic(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1643 unsigned offset
= 0, reg
;
1645 switch (instr
->intrinsic
) {
1646 case nir_intrinsic_discard_if
:
1647 case nir_intrinsic_discard
: {
1648 bool conditional
= instr
->intrinsic
== nir_intrinsic_discard_if
;
1649 struct midgard_instruction discard
= v_branch(conditional
, false);
1650 discard
.branch
.target_type
= TARGET_DISCARD
;
1653 discard
.src
[0] = mir_get_branch_cond(&instr
->src
[0],
1654 &discard
.branch
.invert_conditional
);
1655 discard
.src_types
[0] = nir_type_uint32
;
1658 emit_mir_instruction(ctx
, discard
);
1659 schedule_barrier(ctx
);
1664 case nir_intrinsic_load_uniform
:
1665 case nir_intrinsic_load_ubo
:
1666 case nir_intrinsic_load_global
:
1667 case nir_intrinsic_load_shared
:
1668 case nir_intrinsic_load_input
:
1669 case nir_intrinsic_load_interpolated_input
: {
1670 bool is_uniform
= instr
->intrinsic
== nir_intrinsic_load_uniform
;
1671 bool is_ubo
= instr
->intrinsic
== nir_intrinsic_load_ubo
;
1672 bool is_global
= instr
->intrinsic
== nir_intrinsic_load_global
;
1673 bool is_shared
= instr
->intrinsic
== nir_intrinsic_load_shared
;
1674 bool is_flat
= instr
->intrinsic
== nir_intrinsic_load_input
;
1675 bool is_interp
= instr
->intrinsic
== nir_intrinsic_load_interpolated_input
;
1677 /* Get the base type of the intrinsic */
1678 /* TODO: Infer type? Does it matter? */
1680 (is_ubo
|| is_global
|| is_shared
) ? nir_type_uint
:
1681 (is_interp
) ? nir_type_float
:
1682 nir_intrinsic_type(instr
);
1684 t
= nir_alu_type_get_base_type(t
);
1686 if (!(is_ubo
|| is_global
)) {
1687 offset
= nir_intrinsic_base(instr
);
1690 unsigned nr_comp
= nir_intrinsic_dest_components(instr
);
1692 nir_src
*src_offset
= nir_get_io_offset_src(instr
);
1694 bool direct
= nir_src_is_const(*src_offset
);
1695 nir_src
*indirect_offset
= direct
? NULL
: src_offset
;
1698 offset
+= nir_src_as_uint(*src_offset
);
1700 /* We may need to apply a fractional offset */
1701 int component
= (is_flat
|| is_interp
) ?
1702 nir_intrinsic_component(instr
) : 0;
1703 reg
= nir_dest_index(&instr
->dest
);
1705 if (is_uniform
&& !ctx
->is_blend
) {
1706 emit_ubo_read(ctx
, &instr
->instr
, reg
, (ctx
->sysvals
.sysval_count
+ offset
) * 16, indirect_offset
, 4, 0);
1707 } else if (is_ubo
) {
1708 nir_src index
= instr
->src
[0];
1710 /* TODO: Is indirect block number possible? */
1711 assert(nir_src_is_const(index
));
1713 uint32_t uindex
= nir_src_as_uint(index
) + 1;
1714 emit_ubo_read(ctx
, &instr
->instr
, reg
, offset
, indirect_offset
, 0, uindex
);
1715 } else if (is_global
|| is_shared
) {
1716 emit_global(ctx
, &instr
->instr
, true, reg
, src_offset
, is_shared
);
1717 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& !ctx
->is_blend
) {
1718 emit_varying_read(ctx
, reg
, offset
, nr_comp
, component
, indirect_offset
, t
| nir_dest_bit_size(instr
->dest
), is_flat
);
1719 } else if (ctx
->is_blend
) {
1720 /* ctx->blend_input will be precoloured to r0/r2, where
1721 * the input is preloaded */
1723 unsigned *input
= offset
? &ctx
->blend_src1
: &ctx
->blend_input
;
1728 emit_mir_instruction(ctx
, v_mov(*input
, reg
));
1729 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1730 emit_attr_read(ctx
, reg
, offset
, nr_comp
, t
);
1732 DBG("Unknown load\n");
1739 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1740 case nir_intrinsic_load_barycentric_pixel
:
1741 case nir_intrinsic_load_barycentric_centroid
:
1744 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1746 case nir_intrinsic_load_raw_output_pan
: {
1747 reg
= nir_dest_index(&instr
->dest
);
1749 /* T720 and below use different blend opcodes with slightly
1750 * different semantics than T760 and up */
1752 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1754 ld
.load_store
.arg_2
= output_load_rt_addr(ctx
, instr
);
1756 if (nir_src_is_const(instr
->src
[0])) {
1757 ld
.load_store
.arg_1
= nir_src_as_uint(instr
->src
[0]);
1759 ld
.load_store
.varying_parameters
= 2;
1760 ld
.src
[1] = nir_src_index(ctx
, &instr
->src
[0]);
1761 ld
.src_types
[1] = nir_type_int32
;
1764 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1765 ld
.op
= midgard_op_ld_color_buffer_32u_old
;
1766 ld
.load_store
.address
= 16;
1767 ld
.load_store
.arg_2
= 0x1E;
1770 emit_mir_instruction(ctx
, ld
);
1774 case nir_intrinsic_load_output
: {
1775 reg
= nir_dest_index(&instr
->dest
);
1777 unsigned bits
= nir_dest_bit_size(instr
->dest
);
1779 midgard_instruction ld
;
1781 ld
= m_ld_color_buffer_as_fp16(reg
, 0);
1783 ld
= m_ld_color_buffer_as_fp32(reg
, 0);
1785 ld
.load_store
.arg_2
= output_load_rt_addr(ctx
, instr
);
1787 for (unsigned c
= 4; c
< 16; ++c
)
1788 ld
.swizzle
[0][c
] = 0;
1790 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1792 ld
.op
= midgard_op_ld_color_buffer_as_fp16_old
;
1794 ld
.op
= midgard_op_ld_color_buffer_as_fp32_old
;
1795 ld
.load_store
.address
= 1;
1796 ld
.load_store
.arg_2
= 0x1E;
1799 emit_mir_instruction(ctx
, ld
);
1803 case nir_intrinsic_load_blend_const_color_rgba
: {
1804 assert(ctx
->is_blend
);
1805 reg
= nir_dest_index(&instr
->dest
);
1807 /* Blend constants are embedded directly in the shader and
1808 * patched in, so we use some magic routing */
1810 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), reg
);
1811 ins
.has_constants
= true;
1812 ins
.has_blend_constant
= true;
1813 emit_mir_instruction(ctx
, ins
);
1817 case nir_intrinsic_store_output
:
1818 case nir_intrinsic_store_combined_output_pan
:
1819 assert(nir_src_is_const(instr
->src
[1]) && "no indirect outputs");
1821 offset
= nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[1]);
1823 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1825 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
1826 bool combined
= instr
->intrinsic
==
1827 nir_intrinsic_store_combined_output_pan
;
1829 const nir_variable
*var
;
1830 var
= search_var(ctx
->nir
, nir_var_shader_out
,
1831 nir_intrinsic_base(instr
));
1834 /* Dual-source blend writeout is done by leaving the
1835 * value in r2 for the blend shader to use. */
1836 if (var
->data
.index
) {
1837 if (instr
->src
[0].is_ssa
) {
1838 emit_explicit_constant(ctx
, reg
, reg
);
1840 unsigned out
= make_compiler_temp(ctx
);
1842 midgard_instruction ins
= v_mov(reg
, out
);
1843 emit_mir_instruction(ctx
, ins
);
1845 ctx
->blend_src1
= out
;
1847 ctx
->blend_src1
= reg
;
1853 enum midgard_rt_id rt
;
1854 if (var
->data
.location
== FRAG_RESULT_COLOR
)
1855 rt
= MIDGARD_COLOR_RT0
;
1856 else if (var
->data
.location
>= FRAG_RESULT_DATA0
)
1857 rt
= MIDGARD_COLOR_RT0
+ var
->data
.location
-
1862 unreachable("bad rt");
1864 unsigned reg_z
= ~0, reg_s
= ~0;
1866 unsigned writeout
= nir_intrinsic_component(instr
);
1867 if (writeout
& PAN_WRITEOUT_Z
)
1868 reg_z
= nir_src_index(ctx
, &instr
->src
[2]);
1869 if (writeout
& PAN_WRITEOUT_S
)
1870 reg_s
= nir_src_index(ctx
, &instr
->src
[3]);
1873 emit_fragment_store(ctx
, reg
, reg_z
, reg_s
, rt
);
1874 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1875 assert(instr
->intrinsic
== nir_intrinsic_store_output
);
1877 /* We should have been vectorized, though we don't
1878 * currently check that st_vary is emitted only once
1879 * per slot (this is relevant, since there's not a mask
1880 * parameter available on the store [set to 0 by the
1881 * blob]). We do respect the component by adjusting the
1882 * swizzle. If this is a constant source, we'll need to
1883 * emit that explicitly. */
1885 emit_explicit_constant(ctx
, reg
, reg
);
1887 unsigned dst_component
= nir_intrinsic_component(instr
);
1888 unsigned nr_comp
= nir_src_num_components(instr
->src
[0]);
1890 midgard_instruction st
= m_st_vary_32(reg
, offset
);
1891 st
.load_store
.arg_1
= 0x9E;
1892 st
.load_store
.arg_2
= 0x1E;
1894 switch (nir_alu_type_get_base_type(nir_intrinsic_type(instr
))) {
1897 st
.op
= midgard_op_st_vary_32u
;
1900 st
.op
= midgard_op_st_vary_32i
;
1902 case nir_type_float
:
1903 st
.op
= midgard_op_st_vary_32
;
1906 unreachable("Attempted to store unknown type");
1910 /* nir_intrinsic_component(store_intr) encodes the
1911 * destination component start. Source component offset
1912 * adjustment is taken care of in
1913 * install_registers_instr(), when offset_swizzle() is
1916 unsigned src_component
= COMPONENT_X
;
1918 assert(nr_comp
> 0);
1919 for (unsigned i
= 0; i
< ARRAY_SIZE(st
.swizzle
); ++i
) {
1920 st
.swizzle
[0][i
] = src_component
;
1921 if (i
>= dst_component
&& i
< dst_component
+ nr_comp
- 1)
1925 emit_mir_instruction(ctx
, st
);
1927 DBG("Unknown store\n");
1933 /* Special case of store_output for lowered blend shaders */
1934 case nir_intrinsic_store_raw_output_pan
:
1935 assert (ctx
->stage
== MESA_SHADER_FRAGMENT
);
1936 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1937 emit_fragment_store(ctx
, reg
, ~0, ~0, ctx
->blend_rt
);
1940 case nir_intrinsic_store_global
:
1941 case nir_intrinsic_store_shared
:
1942 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1943 emit_explicit_constant(ctx
, reg
, reg
);
1945 emit_global(ctx
, &instr
->instr
, false, reg
, &instr
->src
[1], instr
->intrinsic
== nir_intrinsic_store_shared
);
1948 case nir_intrinsic_load_ssbo_address
:
1949 emit_sysval_read(ctx
, &instr
->instr
, 1, 0);
1952 case nir_intrinsic_get_buffer_size
:
1953 emit_sysval_read(ctx
, &instr
->instr
, 1, 8);
1956 case nir_intrinsic_load_viewport_scale
:
1957 case nir_intrinsic_load_viewport_offset
:
1958 case nir_intrinsic_load_num_work_groups
:
1959 case nir_intrinsic_load_sampler_lod_parameters_pan
:
1960 emit_sysval_read(ctx
, &instr
->instr
, 3, 0);
1963 case nir_intrinsic_load_work_group_id
:
1964 case nir_intrinsic_load_local_invocation_id
:
1965 emit_compute_builtin(ctx
, instr
);
1968 case nir_intrinsic_load_vertex_id
:
1969 case nir_intrinsic_load_instance_id
:
1970 emit_vertex_builtin(ctx
, instr
);
1973 case nir_intrinsic_load_sample_id
:
1974 emit_msaa_builtin(ctx
, instr
);
1977 case nir_intrinsic_memory_barrier_buffer
:
1978 case nir_intrinsic_memory_barrier_shared
:
1981 case nir_intrinsic_control_barrier
:
1982 schedule_barrier(ctx
);
1983 emit_control_barrier(ctx
);
1984 schedule_barrier(ctx
);
1988 fprintf(stderr
, "Unhandled intrinsic %s\n", nir_intrinsic_infos
[instr
->intrinsic
].name
);
1994 /* Returns dimension with 0 special casing cubemaps */
1996 midgard_tex_format(enum glsl_sampler_dim dim
)
1999 case GLSL_SAMPLER_DIM_1D
:
2000 case GLSL_SAMPLER_DIM_BUF
:
2003 case GLSL_SAMPLER_DIM_2D
:
2004 case GLSL_SAMPLER_DIM_MS
:
2005 case GLSL_SAMPLER_DIM_EXTERNAL
:
2006 case GLSL_SAMPLER_DIM_RECT
:
2009 case GLSL_SAMPLER_DIM_3D
:
2012 case GLSL_SAMPLER_DIM_CUBE
:
2016 DBG("Unknown sampler dim type\n");
2022 /* Tries to attach an explicit LOD or bias as a constant. Returns whether this
2026 pan_attach_constant_bias(
2027 compiler_context
*ctx
,
2029 midgard_texture_word
*word
)
2031 /* To attach as constant, it has to *be* constant */
2033 if (!nir_src_is_const(lod
))
2036 float f
= nir_src_as_float(lod
);
2038 /* Break into fixed-point */
2040 float lod_frac
= f
- lod_int
;
2042 /* Carry over negative fractions */
2043 if (lod_frac
< 0.0) {
2049 word
->bias
= float_to_ubyte(lod_frac
);
2050 word
->bias_int
= lod_int
;
2056 emit_texop_native(compiler_context
*ctx
, nir_tex_instr
*instr
,
2057 unsigned midgard_texop
)
2060 //assert (!instr->sampler);
2062 nir_dest
*dest
= &instr
->dest
;
2064 int texture_index
= instr
->texture_index
;
2065 int sampler_index
= texture_index
;
2067 nir_alu_type dest_base
= nir_alu_type_get_base_type(instr
->dest_type
);
2068 nir_alu_type dest_type
= dest_base
| nir_dest_bit_size(*dest
);
2070 /* texture instructions support float outmods */
2071 unsigned outmod
= midgard_outmod_none
;
2072 if (dest_base
== nir_type_float
) {
2073 outmod
= mir_determine_float_outmod(ctx
, &dest
, 0);
2076 midgard_instruction ins
= {
2077 .type
= TAG_TEXTURE_4
,
2079 .dest
= nir_dest_index(dest
),
2080 .src
= { ~0, ~0, ~0, ~0 },
2081 .dest_type
= dest_type
,
2082 .swizzle
= SWIZZLE_IDENTITY_4
,
2084 .op
= midgard_texop
,
2086 .format
= midgard_tex_format(instr
->sampler_dim
),
2087 .texture_handle
= texture_index
,
2088 .sampler_handle
= sampler_index
,
2089 .shadow
= instr
->is_shadow
,
2093 if (instr
->is_shadow
&& !instr
->is_new_style_shadow
)
2094 for (int i
= 0; i
< 4; ++i
)
2095 ins
.swizzle
[0][i
] = COMPONENT_X
;
2097 /* We may need a temporary for the coordinate */
2099 bool needs_temp_coord
=
2100 (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) ||
2101 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) ||
2104 unsigned coords
= needs_temp_coord
? make_compiler_temp_reg(ctx
) : 0;
2106 for (unsigned i
= 0; i
< instr
->num_srcs
; ++i
) {
2107 int index
= nir_src_index(ctx
, &instr
->src
[i
].src
);
2108 unsigned nr_components
= nir_src_num_components(instr
->src
[i
].src
);
2109 unsigned sz
= nir_src_bit_size(instr
->src
[i
].src
);
2110 nir_alu_type T
= nir_tex_instr_src_type(instr
, i
) | sz
;
2112 switch (instr
->src
[i
].src_type
) {
2113 case nir_tex_src_coord
: {
2114 emit_explicit_constant(ctx
, index
, index
);
2116 unsigned coord_mask
= mask_of(instr
->coord_components
);
2118 bool flip_zw
= (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) && (coord_mask
& (1 << COMPONENT_Z
));
2121 coord_mask
^= ((1 << COMPONENT_Z
) | (1 << COMPONENT_W
));
2123 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
2124 /* texelFetch is undefined on samplerCube */
2125 assert(midgard_texop
!= TEXTURE_OP_TEXEL_FETCH
);
2127 /* For cubemaps, we use a special ld/st op to
2128 * select the face and copy the xy into the
2129 * texture register */
2131 midgard_instruction ld
= m_ld_cubemap_coords(coords
, 0);
2133 ld
.src_types
[1] = T
;
2134 ld
.mask
= 0x3; /* xy */
2135 ld
.load_store
.arg_1
= 0x20;
2136 ld
.swizzle
[1][3] = COMPONENT_X
;
2137 emit_mir_instruction(ctx
, ld
);
2140 ins
.swizzle
[1][2] = instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
2141 ins
.swizzle
[1][3] = COMPONENT_X
;
2142 } else if (needs_temp_coord
) {
2143 /* mov coord_temp, coords */
2144 midgard_instruction mov
= v_mov(index
, coords
);
2145 mov
.mask
= coord_mask
;
2148 mov
.swizzle
[1][COMPONENT_W
] = COMPONENT_Z
;
2150 emit_mir_instruction(ctx
, mov
);
2155 ins
.src
[1] = coords
;
2156 ins
.src_types
[1] = T
;
2158 /* Texelfetch coordinates uses all four elements
2159 * (xyz/index) regardless of texture dimensionality,
2160 * which means it's necessary to zero the unused
2161 * components to keep everything happy */
2163 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
2164 /* mov index.zw, #0, or generalized */
2165 midgard_instruction mov
=
2166 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), coords
);
2167 mov
.has_constants
= true;
2168 mov
.mask
= coord_mask
^ 0xF;
2169 emit_mir_instruction(ctx
, mov
);
2172 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) {
2173 /* Array component in w but NIR wants it in z,
2174 * but if we have a temp coord we already fixed
2177 if (nr_components
== 3) {
2178 ins
.swizzle
[1][2] = COMPONENT_Z
;
2179 ins
.swizzle
[1][3] = needs_temp_coord
? COMPONENT_W
: COMPONENT_Z
;
2180 } else if (nr_components
== 2) {
2182 instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
2183 ins
.swizzle
[1][3] = COMPONENT_X
;
2185 unreachable("Invalid texture 2D components");
2188 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
2190 ins
.swizzle
[1][2] = COMPONENT_Z
;
2191 ins
.swizzle
[1][3] = COMPONENT_W
;
2197 case nir_tex_src_bias
:
2198 case nir_tex_src_lod
: {
2199 /* Try as a constant if we can */
2201 bool is_txf
= midgard_texop
== TEXTURE_OP_TEXEL_FETCH
;
2202 if (!is_txf
&& pan_attach_constant_bias(ctx
, instr
->src
[i
].src
, &ins
.texture
))
2205 ins
.texture
.lod_register
= true;
2207 ins
.src_types
[2] = T
;
2209 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
2210 ins
.swizzle
[2][c
] = COMPONENT_X
;
2212 emit_explicit_constant(ctx
, index
, index
);
2217 case nir_tex_src_offset
: {
2218 ins
.texture
.offset_register
= true;
2220 ins
.src_types
[3] = T
;
2222 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
2223 ins
.swizzle
[3][c
] = (c
> COMPONENT_Z
) ? 0 : c
;
2225 emit_explicit_constant(ctx
, index
, index
);
2229 case nir_tex_src_comparator
:
2230 case nir_tex_src_ms_index
: {
2231 unsigned comp
= COMPONENT_Z
;
2233 /* mov coord_temp.foo, coords */
2234 midgard_instruction mov
= v_mov(index
, coords
);
2235 mov
.mask
= 1 << comp
;
2237 for (unsigned i
= 0; i
< MIR_VEC_COMPONENTS
; ++i
)
2238 mov
.swizzle
[1][i
] = COMPONENT_X
;
2240 emit_mir_instruction(ctx
, mov
);
2245 fprintf(stderr
, "Unknown texture source type: %d\n", instr
->src
[i
].src_type
);
2251 emit_mir_instruction(ctx
, ins
);
2255 emit_tex(compiler_context
*ctx
, nir_tex_instr
*instr
)
2257 switch (instr
->op
) {
2260 emit_texop_native(ctx
, instr
, TEXTURE_OP_NORMAL
);
2263 emit_texop_native(ctx
, instr
, TEXTURE_OP_LOD
);
2266 case nir_texop_txf_ms
:
2267 emit_texop_native(ctx
, instr
, TEXTURE_OP_TEXEL_FETCH
);
2270 emit_sysval_read(ctx
, &instr
->instr
, 4, 0);
2273 fprintf(stderr
, "Unhandled texture op: %d\n", instr
->op
);
2280 emit_jump(compiler_context
*ctx
, nir_jump_instr
*instr
)
2282 switch (instr
->type
) {
2283 case nir_jump_break
: {
2284 /* Emit a branch out of the loop */
2285 struct midgard_instruction br
= v_branch(false, false);
2286 br
.branch
.target_type
= TARGET_BREAK
;
2287 br
.branch
.target_break
= ctx
->current_loop_depth
;
2288 emit_mir_instruction(ctx
, br
);
2293 DBG("Unknown jump type %d\n", instr
->type
);
2299 emit_instr(compiler_context
*ctx
, struct nir_instr
*instr
)
2301 switch (instr
->type
) {
2302 case nir_instr_type_load_const
:
2303 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
2306 case nir_instr_type_intrinsic
:
2307 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
2310 case nir_instr_type_alu
:
2311 emit_alu(ctx
, nir_instr_as_alu(instr
));
2314 case nir_instr_type_tex
:
2315 emit_tex(ctx
, nir_instr_as_tex(instr
));
2318 case nir_instr_type_jump
:
2319 emit_jump(ctx
, nir_instr_as_jump(instr
));
2322 case nir_instr_type_ssa_undef
:
2327 DBG("Unhandled instruction type\n");
2333 /* ALU instructions can inline or embed constants, which decreases register
2334 * pressure and saves space. */
2336 #define CONDITIONAL_ATTACH(idx) { \
2337 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
2340 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
2341 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
2346 inline_alu_constants(compiler_context
*ctx
, midgard_block
*block
)
2348 mir_foreach_instr_in_block(block
, alu
) {
2349 /* Other instructions cannot inline constants */
2350 if (alu
->type
!= TAG_ALU_4
) continue;
2351 if (alu
->compact_branch
) continue;
2353 /* If there is already a constant here, we can do nothing */
2354 if (alu
->has_constants
) continue;
2356 CONDITIONAL_ATTACH(0);
2358 if (!alu
->has_constants
) {
2359 CONDITIONAL_ATTACH(1)
2360 } else if (!alu
->inline_constant
) {
2361 /* Corner case: _two_ vec4 constants, for instance with a
2362 * csel. For this case, we can only use a constant
2363 * register for one, we'll have to emit a move for the
2366 void *entry
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, alu
->src
[1] + 1);
2367 unsigned scratch
= make_compiler_temp(ctx
);
2370 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), scratch
);
2371 attach_constants(ctx
, &ins
, entry
, alu
->src
[1] + 1);
2373 /* Set the source */
2374 alu
->src
[1] = scratch
;
2376 /* Inject us -before- the last instruction which set r31 */
2377 mir_insert_instruction_before(ctx
, mir_prev_op(alu
), ins
);
2384 max_bitsize_for_alu(midgard_instruction
*ins
)
2386 unsigned max_bitsize
= 0;
2387 for (int i
= 0; i
< MIR_SRC_COUNT
; i
++) {
2388 if (ins
->src
[i
] == ~0) continue;
2389 unsigned src_bitsize
= nir_alu_type_get_type_size(ins
->src_types
[i
]);
2390 max_bitsize
= MAX2(src_bitsize
, max_bitsize
);
2392 unsigned dst_bitsize
= nir_alu_type_get_type_size(ins
->dest_type
);
2393 max_bitsize
= MAX2(dst_bitsize
, max_bitsize
);
2395 /* We don't have fp16 LUTs, so we'll want to emit code like:
2397 * vlut.fsinr hr0, hr0
2399 * where both input and output are 16-bit but the operation is carried
2404 case midgard_alu_op_fsqrt
:
2405 case midgard_alu_op_frcp
:
2406 case midgard_alu_op_frsqrt
:
2407 case midgard_alu_op_fsin
:
2408 case midgard_alu_op_fcos
:
2409 case midgard_alu_op_fexp2
:
2410 case midgard_alu_op_flog2
:
2411 max_bitsize
= MAX2(max_bitsize
, 32);
2418 /* High implies computing at a higher bitsize, e.g umul_high of 32-bit
2419 * requires computing at 64-bit */
2420 if (midgard_is_integer_out_op(ins
->op
) && ins
->outmod
== midgard_outmod_int_high
) {
2422 assert(max_bitsize
<= 64);
2429 reg_mode_for_bitsize(unsigned bitsize
)
2432 /* use 16 pipe for 8 since we don't support vec16 yet */
2435 return midgard_reg_mode_16
;
2437 return midgard_reg_mode_32
;
2439 return midgard_reg_mode_64
;
2441 unreachable("invalid bit size");
2445 /* Midgard supports two types of constants, embedded constants (128-bit) and
2446 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2447 * constants can be demoted to inline constants, for space savings and
2448 * sometimes a performance boost */
2451 embedded_to_inline_constant(compiler_context
*ctx
, midgard_block
*block
)
2453 mir_foreach_instr_in_block(block
, ins
) {
2454 if (!ins
->has_constants
) continue;
2455 if (ins
->has_inline_constant
) continue;
2457 /* Blend constants must not be inlined by definition */
2458 if (ins
->has_blend_constant
) continue;
2460 unsigned max_bitsize
= max_bitsize_for_alu(ins
);
2462 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2463 bool is_16
= max_bitsize
== 16;
2464 bool is_32
= max_bitsize
== 32;
2466 if (!(is_16
|| is_32
))
2469 /* src1 cannot be an inline constant due to encoding
2470 * restrictions. So, if possible we try to flip the arguments
2475 if (ins
->src
[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
) &&
2476 alu_opcode_props
[op
].props
& OP_COMMUTES
) {
2480 if (ins
->src
[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2481 /* Component is from the swizzle. Take a nonzero component */
2483 unsigned first_comp
= ffs(ins
->mask
) - 1;
2484 unsigned component
= ins
->swizzle
[1][first_comp
];
2486 /* Scale constant appropriately, if we can legally */
2487 int16_t scaled_constant
= 0;
2490 scaled_constant
= ins
->constants
.u16
[component
];
2491 } else if (midgard_is_integer_op(op
)) {
2492 scaled_constant
= ins
->constants
.u32
[component
];
2494 /* Constant overflow after resize */
2495 if (scaled_constant
!= ins
->constants
.u32
[component
])
2498 float original
= ins
->constants
.f32
[component
];
2499 scaled_constant
= _mesa_float_to_half(original
);
2501 /* Check for loss of precision. If this is
2502 * mediump, we don't care, but for a highp
2503 * shader, we need to pay attention. NIR
2504 * doesn't yet tell us which mode we're in!
2505 * Practically this prevents most constants
2506 * from being inlined, sadly. */
2508 float fp32
= _mesa_half_to_float(scaled_constant
);
2510 if (fp32
!= original
)
2514 /* Should've been const folded */
2515 if (ins
->src_abs
[1] || ins
->src_neg
[1])
2518 /* Make sure that the constant is not itself a vector
2519 * by checking if all accessed values are the same. */
2521 const midgard_constants
*cons
= &ins
->constants
;
2522 uint32_t value
= is_16
? cons
->u16
[component
] : cons
->u32
[component
];
2524 bool is_vector
= false;
2525 unsigned mask
= effective_writemask(ins
->op
, ins
->mask
);
2527 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
2528 /* We only care if this component is actually used */
2529 if (!(mask
& (1 << c
)))
2532 uint32_t test
= is_16
?
2533 cons
->u16
[ins
->swizzle
[1][c
]] :
2534 cons
->u32
[ins
->swizzle
[1][c
]];
2536 if (test
!= value
) {
2545 /* Get rid of the embedded constant */
2546 ins
->has_constants
= false;
2548 ins
->has_inline_constant
= true;
2549 ins
->inline_constant
= scaled_constant
;
2554 /* Dead code elimination for branches at the end of a block - only one branch
2555 * per block is legal semantically */
2558 midgard_cull_dead_branch(compiler_context
*ctx
, midgard_block
*block
)
2560 bool branched
= false;
2562 mir_foreach_instr_in_block_safe(block
, ins
) {
2563 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2566 mir_remove_instruction(ins
);
2572 /* We want to force the invert on AND/OR to the second slot to legalize into
2573 * iandnot/iornot. The relevant patterns are for AND (and OR respectively)
2575 * ~a & #b = ~a & ~(#~b)
2580 midgard_legalize_invert(compiler_context
*ctx
, midgard_block
*block
)
2582 mir_foreach_instr_in_block(block
, ins
) {
2583 if (ins
->type
!= TAG_ALU_4
) continue;
2585 if (ins
->op
!= midgard_alu_op_iand
&&
2586 ins
->op
!= midgard_alu_op_ior
) continue;
2588 if (ins
->src_invert
[1] || !ins
->src_invert
[0]) continue;
2590 if (ins
->has_inline_constant
) {
2591 /* ~(#~a) = ~(~#a) = a, so valid, and forces both
2593 ins
->inline_constant
= ~ins
->inline_constant
;
2594 ins
->src_invert
[1] = true;
2596 /* Flip to the right invert order. Note
2597 * has_inline_constant false by assumption on the
2598 * branch, so flipping makes sense. */
2605 emit_fragment_epilogue(compiler_context
*ctx
, unsigned rt
)
2607 /* Loop to ourselves */
2608 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2609 struct midgard_instruction ins
= v_branch(false, false);
2610 ins
.writeout
= br
->writeout
;
2611 ins
.branch
.target_block
= ctx
->block_count
- 1;
2612 ins
.constants
.u32
[0] = br
->constants
.u32
[0];
2613 memcpy(&ins
.src_types
, &br
->src_types
, sizeof(ins
.src_types
));
2614 emit_mir_instruction(ctx
, ins
);
2616 ctx
->current_block
->epilogue
= true;
2617 schedule_barrier(ctx
);
2618 return ins
.branch
.target_block
;
2621 static midgard_block
*
2622 emit_block_init(compiler_context
*ctx
)
2624 midgard_block
*this_block
= ctx
->after_block
;
2625 ctx
->after_block
= NULL
;
2628 this_block
= create_empty_block(ctx
);
2630 list_addtail(&this_block
->base
.link
, &ctx
->blocks
);
2632 this_block
->scheduled
= false;
2635 /* Set up current block */
2636 list_inithead(&this_block
->base
.instructions
);
2637 ctx
->current_block
= this_block
;
2642 static midgard_block
*
2643 emit_block(compiler_context
*ctx
, nir_block
*block
)
2645 midgard_block
*this_block
= emit_block_init(ctx
);
2647 nir_foreach_instr(instr
, block
) {
2648 emit_instr(ctx
, instr
);
2649 ++ctx
->instruction_count
;
2655 static midgard_block
*emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
);
2658 emit_if(struct compiler_context
*ctx
, nir_if
*nif
)
2660 midgard_block
*before_block
= ctx
->current_block
;
2662 /* Speculatively emit the branch, but we can't fill it in until later */
2664 EMIT(branch
, true, true);
2665 midgard_instruction
*then_branch
= mir_last_in_block(ctx
->current_block
);
2666 then_branch
->src
[0] = mir_get_branch_cond(&nif
->condition
, &inv
);
2667 then_branch
->src_types
[0] = nir_type_uint32
;
2668 then_branch
->branch
.invert_conditional
= !inv
;
2670 /* Emit the two subblocks. */
2671 midgard_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
2672 midgard_block
*end_then_block
= ctx
->current_block
;
2674 /* Emit a jump from the end of the then block to the end of the else */
2675 EMIT(branch
, false, false);
2676 midgard_instruction
*then_exit
= mir_last_in_block(ctx
->current_block
);
2678 /* Emit second block, and check if it's empty */
2680 int else_idx
= ctx
->block_count
;
2681 int count_in
= ctx
->instruction_count
;
2682 midgard_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
2683 midgard_block
*end_else_block
= ctx
->current_block
;
2684 int after_else_idx
= ctx
->block_count
;
2686 /* Now that we have the subblocks emitted, fix up the branches */
2691 if (ctx
->instruction_count
== count_in
) {
2692 /* The else block is empty, so don't emit an exit jump */
2693 mir_remove_instruction(then_exit
);
2694 then_branch
->branch
.target_block
= after_else_idx
;
2696 then_branch
->branch
.target_block
= else_idx
;
2697 then_exit
->branch
.target_block
= after_else_idx
;
2700 /* Wire up the successors */
2702 ctx
->after_block
= create_empty_block(ctx
);
2704 pan_block_add_successor(&before_block
->base
, &then_block
->base
);
2705 pan_block_add_successor(&before_block
->base
, &else_block
->base
);
2707 pan_block_add_successor(&end_then_block
->base
, &ctx
->after_block
->base
);
2708 pan_block_add_successor(&end_else_block
->base
, &ctx
->after_block
->base
);
2712 emit_loop(struct compiler_context
*ctx
, nir_loop
*nloop
)
2714 /* Remember where we are */
2715 midgard_block
*start_block
= ctx
->current_block
;
2717 /* Allocate a loop number, growing the current inner loop depth */
2718 int loop_idx
= ++ctx
->current_loop_depth
;
2720 /* Get index from before the body so we can loop back later */
2721 int start_idx
= ctx
->block_count
;
2723 /* Emit the body itself */
2724 midgard_block
*loop_block
= emit_cf_list(ctx
, &nloop
->body
);
2726 /* Branch back to loop back */
2727 struct midgard_instruction br_back
= v_branch(false, false);
2728 br_back
.branch
.target_block
= start_idx
;
2729 emit_mir_instruction(ctx
, br_back
);
2731 /* Mark down that branch in the graph. */
2732 pan_block_add_successor(&start_block
->base
, &loop_block
->base
);
2733 pan_block_add_successor(&ctx
->current_block
->base
, &loop_block
->base
);
2735 /* Find the index of the block about to follow us (note: we don't add
2736 * one; blocks are 0-indexed so we get a fencepost problem) */
2737 int break_block_idx
= ctx
->block_count
;
2739 /* Fix up the break statements we emitted to point to the right place,
2740 * now that we can allocate a block number for them */
2741 ctx
->after_block
= create_empty_block(ctx
);
2743 mir_foreach_block_from(ctx
, start_block
, _block
) {
2744 mir_foreach_instr_in_block(((midgard_block
*) _block
), ins
) {
2745 if (ins
->type
!= TAG_ALU_4
) continue;
2746 if (!ins
->compact_branch
) continue;
2748 /* We found a branch -- check the type to see if we need to do anything */
2749 if (ins
->branch
.target_type
!= TARGET_BREAK
) continue;
2751 /* It's a break! Check if it's our break */
2752 if (ins
->branch
.target_break
!= loop_idx
) continue;
2754 /* Okay, cool, we're breaking out of this loop.
2755 * Rewrite from a break to a goto */
2757 ins
->branch
.target_type
= TARGET_GOTO
;
2758 ins
->branch
.target_block
= break_block_idx
;
2760 pan_block_add_successor(_block
, &ctx
->after_block
->base
);
2764 /* Now that we've finished emitting the loop, free up the depth again
2765 * so we play nice with recursion amid nested loops */
2766 --ctx
->current_loop_depth
;
2768 /* Dump loop stats */
2772 static midgard_block
*
2773 emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
)
2775 midgard_block
*start_block
= NULL
;
2777 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2778 switch (node
->type
) {
2779 case nir_cf_node_block
: {
2780 midgard_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
2783 start_block
= block
;
2788 case nir_cf_node_if
:
2789 emit_if(ctx
, nir_cf_node_as_if(node
));
2792 case nir_cf_node_loop
:
2793 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2796 case nir_cf_node_function
:
2805 /* Due to lookahead, we need to report the first tag executed in the command
2806 * stream and in branch targets. An initial block might be empty, so iterate
2807 * until we find one that 'works' */
2810 midgard_get_first_tag_from_block(compiler_context
*ctx
, unsigned block_idx
)
2812 midgard_block
*initial_block
= mir_get_block(ctx
, block_idx
);
2814 mir_foreach_block_from(ctx
, initial_block
, _v
) {
2815 midgard_block
*v
= (midgard_block
*) _v
;
2816 if (v
->quadword_count
) {
2817 midgard_bundle
*initial_bundle
=
2818 util_dynarray_element(&v
->bundles
, midgard_bundle
, 0);
2820 return initial_bundle
->tag
;
2824 /* Default to a tag 1 which will break from the shader, in case we jump
2825 * to the exit block (i.e. `return` in a compute shader) */
2830 /* For each fragment writeout instruction, generate a writeout loop to
2831 * associate with it */
2834 mir_add_writeout_loops(compiler_context
*ctx
)
2836 for (unsigned rt
= 0; rt
< ARRAY_SIZE(ctx
->writeout_branch
); ++rt
) {
2837 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2840 unsigned popped
= br
->branch
.target_block
;
2841 pan_block_add_successor(&(mir_get_block(ctx
, popped
- 1)->base
), &ctx
->current_block
->base
);
2842 br
->branch
.target_block
= emit_fragment_epilogue(ctx
, rt
);
2843 br
->branch
.target_type
= TARGET_GOTO
;
2845 /* If we have more RTs, we'll need to restore back after our
2846 * loop terminates */
2848 if ((rt
+ 1) < ARRAY_SIZE(ctx
->writeout_branch
) && ctx
->writeout_branch
[rt
+ 1]) {
2849 midgard_instruction uncond
= v_branch(false, false);
2850 uncond
.branch
.target_block
= popped
;
2851 uncond
.branch
.target_type
= TARGET_GOTO
;
2852 emit_mir_instruction(ctx
, uncond
);
2853 pan_block_add_successor(&ctx
->current_block
->base
, &(mir_get_block(ctx
, popped
)->base
));
2854 schedule_barrier(ctx
);
2856 /* We're last, so we can terminate here */
2857 br
->last_writeout
= true;
2863 midgard_compile_shader_nir(nir_shader
*nir
, panfrost_program
*program
, bool is_blend
, unsigned blend_rt
, unsigned gpu_id
, bool shaderdb
, bool silent
)
2865 struct util_dynarray
*compiled
= &program
->compiled
;
2867 midgard_debug
= debug_get_option_midgard_debug();
2869 /* TODO: Bound against what? */
2870 compiler_context
*ctx
= rzalloc(NULL
, compiler_context
);
2873 ctx
->stage
= nir
->info
.stage
;
2874 ctx
->is_blend
= is_blend
;
2875 ctx
->blend_rt
= MIDGARD_COLOR_RT0
+ blend_rt
;
2876 ctx
->blend_input
= ~0;
2877 ctx
->blend_src1
= ~0;
2878 ctx
->quirks
= midgard_get_quirks(gpu_id
);
2880 /* Start off with a safe cutoff, allowing usage of all 16 work
2881 * registers. Later, we'll promote uniform reads to uniform registers
2882 * if we determine it is beneficial to do so */
2883 ctx
->uniform_cutoff
= 8;
2885 /* Initialize at a global (not block) level hash tables */
2887 ctx
->ssa_constants
= _mesa_hash_table_u64_create(NULL
);
2889 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2890 * (so we don't accidentally duplicate the epilogue since mesa/st has
2891 * messed with our I/O quite a bit already) */
2893 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2895 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
2896 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
2897 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
2900 NIR_PASS_V(nir
, nir_lower_var_copies
);
2901 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2902 NIR_PASS_V(nir
, nir_split_var_copies
);
2903 NIR_PASS_V(nir
, nir_lower_var_copies
);
2904 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
2905 NIR_PASS_V(nir
, nir_lower_var_copies
);
2906 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2908 unsigned pan_quirks
= panfrost_get_quirks(gpu_id
);
2909 NIR_PASS_V(nir
, pan_lower_framebuffer
,
2910 program
->rt_formats
, is_blend
, pan_quirks
);
2912 NIR_PASS_V(nir
, nir_lower_io
, nir_var_shader_in
| nir_var_shader_out
,
2914 NIR_PASS_V(nir
, nir_lower_ssbo
);
2915 NIR_PASS_V(nir
, midgard_nir_lower_zs_store
);
2917 /* Optimisation passes */
2919 optimise_nir(nir
, ctx
->quirks
, is_blend
);
2921 NIR_PASS_V(nir
, midgard_nir_reorder_writeout
);
2923 if ((midgard_debug
& MIDGARD_DBG_SHADERS
) && !silent
) {
2924 nir_print_shader(nir
, stdout
);
2927 /* Assign sysvals and counts, now that we're sure
2928 * (post-optimisation) */
2930 panfrost_nir_assign_sysvals(&ctx
->sysvals
, ctx
, nir
);
2931 program
->sysval_count
= ctx
->sysvals
.sysval_count
;
2932 memcpy(program
->sysvals
, ctx
->sysvals
.sysvals
, sizeof(ctx
->sysvals
.sysvals
[0]) * ctx
->sysvals
.sysval_count
);
2934 nir_foreach_function(func
, nir
) {
2938 list_inithead(&ctx
->blocks
);
2939 ctx
->block_count
= 0;
2941 ctx
->already_emitted
= calloc(BITSET_WORDS(func
->impl
->ssa_alloc
), sizeof(BITSET_WORD
));
2943 if (nir
->info
.outputs_read
&& !is_blend
) {
2944 emit_block_init(ctx
);
2946 struct midgard_instruction wait
= v_branch(false, false);
2947 wait
.branch
.target_type
= TARGET_TILEBUF_WAIT
;
2949 emit_mir_instruction(ctx
, wait
);
2951 ++ctx
->instruction_count
;
2954 emit_cf_list(ctx
, &func
->impl
->body
);
2955 free(ctx
->already_emitted
);
2956 break; /* TODO: Multi-function shaders */
2959 util_dynarray_init(compiled
, NULL
);
2961 /* Per-block lowering before opts */
2963 mir_foreach_block(ctx
, _block
) {
2964 midgard_block
*block
= (midgard_block
*) _block
;
2965 inline_alu_constants(ctx
, block
);
2966 embedded_to_inline_constant(ctx
, block
);
2968 /* MIR-level optimizations */
2970 bool progress
= false;
2974 progress
|= midgard_opt_dead_code_eliminate(ctx
);
2976 mir_foreach_block(ctx
, _block
) {
2977 midgard_block
*block
= (midgard_block
*) _block
;
2978 progress
|= midgard_opt_copy_prop(ctx
, block
);
2979 progress
|= midgard_opt_combine_projection(ctx
, block
);
2980 progress
|= midgard_opt_varying_projection(ctx
, block
);
2984 mir_foreach_block(ctx
, _block
) {
2985 midgard_block
*block
= (midgard_block
*) _block
;
2986 midgard_lower_derivatives(ctx
, block
);
2987 midgard_legalize_invert(ctx
, block
);
2988 midgard_cull_dead_branch(ctx
, block
);
2991 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2992 mir_add_writeout_loops(ctx
);
2994 /* Analyze now that the code is known but before scheduling creates
2995 * pipeline registers which are harder to track */
2996 mir_analyze_helper_terminate(ctx
);
2997 mir_analyze_helper_requirements(ctx
);
3000 midgard_schedule_program(ctx
);
3003 /* Emit flat binary from the instruction arrays. Iterate each block in
3004 * sequence. Save instruction boundaries such that lookahead tags can
3005 * be assigned easily */
3007 /* Cache _all_ bundles in source order for lookahead across failed branches */
3009 int bundle_count
= 0;
3010 mir_foreach_block(ctx
, _block
) {
3011 midgard_block
*block
= (midgard_block
*) _block
;
3012 bundle_count
+= block
->bundles
.size
/ sizeof(midgard_bundle
);
3014 midgard_bundle
**source_order_bundles
= malloc(sizeof(midgard_bundle
*) * bundle_count
);
3016 mir_foreach_block(ctx
, _block
) {
3017 midgard_block
*block
= (midgard_block
*) _block
;
3018 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
3019 source_order_bundles
[bundle_idx
++] = bundle
;
3023 int current_bundle
= 0;
3025 /* Midgard prefetches instruction types, so during emission we
3026 * need to lookahead. Unless this is the last instruction, in
3027 * which we return 1. */
3029 mir_foreach_block(ctx
, _block
) {
3030 midgard_block
*block
= (midgard_block
*) _block
;
3031 mir_foreach_bundle_in_block(block
, bundle
) {
3034 if (!bundle
->last_writeout
&& (current_bundle
+ 1 < bundle_count
))
3035 lookahead
= source_order_bundles
[current_bundle
+ 1]->tag
;
3037 emit_binary_bundle(ctx
, block
, bundle
, compiled
, lookahead
);
3041 /* TODO: Free deeper */
3042 //util_dynarray_fini(&block->instructions);
3045 free(source_order_bundles
);
3047 /* Report the very first tag executed */
3048 program
->first_tag
= midgard_get_first_tag_from_block(ctx
, 0);
3050 /* Deal with off-by-one related to the fencepost problem */
3051 program
->work_register_count
= ctx
->work_registers
+ 1;
3052 program
->uniform_cutoff
= ctx
->uniform_cutoff
;
3054 program
->blend_patch_offset
= ctx
->blend_constant_offset
;
3055 program
->tls_size
= ctx
->tls_size
;
3057 if ((midgard_debug
& MIDGARD_DBG_SHADERS
) && !silent
)
3058 disassemble_midgard(stdout
, program
->compiled
.data
, program
->compiled
.size
, gpu_id
, ctx
->stage
);
3060 if ((midgard_debug
& MIDGARD_DBG_SHADERDB
|| shaderdb
) && !silent
) {
3061 unsigned nr_bundles
= 0, nr_ins
= 0;
3063 /* Count instructions and bundles */
3065 mir_foreach_block(ctx
, _block
) {
3066 midgard_block
*block
= (midgard_block
*) _block
;
3067 nr_bundles
+= util_dynarray_num_elements(
3068 &block
->bundles
, midgard_bundle
);
3070 mir_foreach_bundle_in_block(block
, bun
)
3071 nr_ins
+= bun
->instruction_count
;
3074 /* Calculate thread count. There are certain cutoffs by
3075 * register count for thread count */
3077 unsigned nr_registers
= program
->work_register_count
;
3079 unsigned nr_threads
=
3080 (nr_registers
<= 4) ? 4 :
3081 (nr_registers
<= 8) ? 2 :
3086 fprintf(stderr
, "shader%d - %s shader: "
3087 "%u inst, %u bundles, %u quadwords, "
3088 "%u registers, %u threads, %u loops, "
3089 "%u:%u spills:fills\n",
3091 ctx
->is_blend
? "PAN_SHADER_BLEND" :
3092 gl_shader_stage_name(ctx
->stage
),
3093 nr_ins
, nr_bundles
, ctx
->quadword_count
,
3094 nr_registers
, nr_threads
,
3096 ctx
->spills
, ctx
->fills
);