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 "main/imports.h"
37 #include "compiler/nir/nir_builder.h"
38 #include "util/half_float.h"
39 #include "util/u_math.h"
40 #include "util/u_debug.h"
41 #include "util/u_dynarray.h"
42 #include "util/list.h"
43 #include "main/mtypes.h"
46 #include "midgard_nir.h"
47 #include "midgard_compile.h"
48 #include "midgard_ops.h"
51 #include "midgard_quirks.h"
53 #include "disassemble.h"
55 static const struct debug_named_value debug_options
[] = {
56 {"msgs", MIDGARD_DBG_MSGS
, "Print debug messages"},
57 {"shaders", MIDGARD_DBG_SHADERS
, "Dump shaders in NIR and MIR"},
58 {"shaderdb", MIDGARD_DBG_SHADERDB
, "Prints shader-db statistics"},
62 DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug
, "MIDGARD_MESA_DEBUG", debug_options
, 0)
64 unsigned SHADER_DB_COUNT
= 0;
66 int midgard_debug
= 0;
68 #define DBG(fmt, ...) \
69 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
70 fprintf(stderr, "%s:%d: "fmt, \
71 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
72 static midgard_block
*
73 create_empty_block(compiler_context
*ctx
)
75 midgard_block
*blk
= rzalloc(ctx
, midgard_block
);
77 blk
->predecessors
= _mesa_set_create(blk
,
79 _mesa_key_pointer_equal
);
81 blk
->source_id
= ctx
->block_source_count
++;
87 midgard_block_add_successor(midgard_block
*block
, midgard_block
*successor
)
93 for (unsigned i
= 0; i
< block
->nr_successors
; ++i
) {
94 if (block
->successors
[i
] == successor
)
98 block
->successors
[block
->nr_successors
++] = successor
;
99 assert(block
->nr_successors
<= ARRAY_SIZE(block
->successors
));
101 /* Note the predecessor in the other direction */
102 _mesa_set_add(successor
->predecessors
, block
);
106 schedule_barrier(compiler_context
*ctx
)
108 midgard_block
*temp
= ctx
->after_block
;
109 ctx
->after_block
= create_empty_block(ctx
);
111 list_addtail(&ctx
->after_block
->link
, &ctx
->blocks
);
112 list_inithead(&ctx
->after_block
->instructions
);
113 midgard_block_add_successor(ctx
->current_block
, ctx
->after_block
);
114 ctx
->current_block
= ctx
->after_block
;
115 ctx
->after_block
= temp
;
118 /* Helpers to generate midgard_instruction's using macro magic, since every
119 * driver seems to do it that way */
121 #define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
123 #define M_LOAD_STORE(name, store) \
124 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
125 midgard_instruction i = { \
126 .type = TAG_LOAD_STORE_4, \
129 .src = { ~0, ~0, ~0, ~0 }, \
130 .swizzle = SWIZZLE_IDENTITY_4, \
132 .op = midgard_op_##name, \
145 #define M_LOAD(name) M_LOAD_STORE(name, false)
146 #define M_STORE(name) M_LOAD_STORE(name, true)
148 /* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
149 * the corresponding Midgard source */
151 static midgard_vector_alu_src
152 vector_alu_modifiers(nir_alu_src
*src
, bool is_int
, unsigned broadcast_count
,
153 bool half
, bool sext
)
155 /* Figure out how many components there are so we can adjust.
156 * Specifically we want to broadcast the last channel so things like
160 if (broadcast_count
&& src
) {
161 uint8_t last_component
= src
->swizzle
[broadcast_count
- 1];
163 for (unsigned c
= broadcast_count
; c
< NIR_MAX_VEC_COMPONENTS
; ++c
) {
164 src
->swizzle
[c
] = last_component
;
168 midgard_vector_alu_src alu_src
= {
175 alu_src
.mod
= midgard_int_normal
;
177 /* Sign/zero-extend if needed */
181 midgard_int_sign_extend
182 : midgard_int_zero_extend
;
185 /* These should have been lowered away */
187 assert(!(src
->abs
|| src
->negate
));
190 alu_src
.mod
= (src
->abs
<< 0) | (src
->negate
<< 1);
196 /* load/store instructions have both 32-bit and 16-bit variants, depending on
197 * whether we are using vectors composed of highp or mediump. At the moment, we
198 * don't support half-floats -- this requires changes in other parts of the
199 * compiler -- therefore the 16-bit versions are commented out. */
201 //M_LOAD(ld_attr_16);
203 //M_LOAD(ld_vary_16);
208 M_LOAD(ld_color_buffer_32u
);
209 //M_STORE(st_vary_16);
211 M_LOAD(ld_cubemap_coords
);
212 M_LOAD(ld_compute_id
);
214 static midgard_instruction
215 v_branch(bool conditional
, bool invert
)
217 midgard_instruction ins
= {
219 .unit
= ALU_ENAB_BRANCH
,
220 .compact_branch
= true,
222 .conditional
= conditional
,
223 .invert_conditional
= invert
226 .src
= { ~0, ~0, ~0, ~0 },
232 static midgard_branch_extended
233 midgard_create_branch_extended( midgard_condition cond
,
234 midgard_jmp_writeout_op op
,
236 signed quadword_offset
)
238 /* The condition code is actually a LUT describing a function to
239 * combine multiple condition codes. However, we only support a single
240 * condition code at the moment, so we just duplicate over a bunch of
243 uint16_t duplicated_cond
=
253 midgard_branch_extended branch
= {
255 .dest_tag
= dest_tag
,
256 .offset
= quadword_offset
,
257 .cond
= duplicated_cond
264 attach_constants(compiler_context
*ctx
, midgard_instruction
*ins
, void *constants
, int name
)
266 ins
->has_constants
= true;
267 memcpy(&ins
->constants
, constants
, 16);
271 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
273 return glsl_count_attribute_slots(type
, false);
276 /* Lower fdot2 to a vector multiplication followed by channel addition */
278 midgard_nir_lower_fdot2_body(nir_builder
*b
, nir_alu_instr
*alu
)
280 if (alu
->op
!= nir_op_fdot2
)
283 b
->cursor
= nir_before_instr(&alu
->instr
);
285 nir_ssa_def
*src0
= nir_ssa_for_alu_src(b
, alu
, 0);
286 nir_ssa_def
*src1
= nir_ssa_for_alu_src(b
, alu
, 1);
288 nir_ssa_def
*product
= nir_fmul(b
, src0
, src1
);
290 nir_ssa_def
*sum
= nir_fadd(b
,
291 nir_channel(b
, product
, 0),
292 nir_channel(b
, product
, 1));
294 /* Replace the fdot2 with this sum */
295 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
, nir_src_for_ssa(sum
));
299 midgard_sysval_for_ssbo(nir_intrinsic_instr
*instr
)
301 /* This is way too meta */
302 bool is_store
= instr
->intrinsic
== nir_intrinsic_store_ssbo
;
303 unsigned idx_idx
= is_store
? 1 : 0;
305 nir_src index
= instr
->src
[idx_idx
];
306 assert(nir_src_is_const(index
));
307 uint32_t uindex
= nir_src_as_uint(index
);
309 return PAN_SYSVAL(SSBO
, uindex
);
313 midgard_sysval_for_sampler(nir_intrinsic_instr
*instr
)
315 /* TODO: indirect samplers !!! */
316 nir_src index
= instr
->src
[0];
317 assert(nir_src_is_const(index
));
318 uint32_t uindex
= nir_src_as_uint(index
);
320 return PAN_SYSVAL(SAMPLER
, uindex
);
324 midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr
*instr
)
326 switch (instr
->intrinsic
) {
327 case nir_intrinsic_load_viewport_scale
:
328 return PAN_SYSVAL_VIEWPORT_SCALE
;
329 case nir_intrinsic_load_viewport_offset
:
330 return PAN_SYSVAL_VIEWPORT_OFFSET
;
331 case nir_intrinsic_load_num_work_groups
:
332 return PAN_SYSVAL_NUM_WORK_GROUPS
;
333 case nir_intrinsic_load_ssbo
:
334 case nir_intrinsic_store_ssbo
:
335 return midgard_sysval_for_ssbo(instr
);
336 case nir_intrinsic_load_sampler_lod_parameters_pan
:
337 return midgard_sysval_for_sampler(instr
);
343 static int sysval_for_instr(compiler_context
*ctx
, nir_instr
*instr
,
346 nir_intrinsic_instr
*intr
;
347 nir_dest
*dst
= NULL
;
351 bool is_store
= false;
353 switch (instr
->type
) {
354 case nir_instr_type_intrinsic
:
355 intr
= nir_instr_as_intrinsic(instr
);
356 sysval
= midgard_nir_sysval_for_intrinsic(intr
);
358 is_store
|= intr
->intrinsic
== nir_intrinsic_store_ssbo
;
360 case nir_instr_type_tex
:
361 tex
= nir_instr_as_tex(instr
);
362 if (tex
->op
!= nir_texop_txs
)
365 sysval
= PAN_SYSVAL(TEXTURE_SIZE
,
366 PAN_TXS_SYSVAL_ID(tex
->texture_index
,
367 nir_tex_instr_dest_size(tex
) -
368 (tex
->is_array
? 1 : 0),
376 if (dest
&& dst
&& !is_store
)
377 *dest
= nir_dest_index(ctx
, dst
);
383 midgard_nir_assign_sysval_body(compiler_context
*ctx
, nir_instr
*instr
)
387 sysval
= sysval_for_instr(ctx
, instr
, NULL
);
391 /* We have a sysval load; check if it's already been assigned */
393 if (_mesa_hash_table_u64_search(ctx
->sysval_to_id
, sysval
))
396 /* It hasn't -- so assign it now! */
398 unsigned id
= ctx
->sysval_count
++;
399 _mesa_hash_table_u64_insert(ctx
->sysval_to_id
, sysval
, (void *) ((uintptr_t) id
+ 1));
400 ctx
->sysvals
[id
] = sysval
;
404 midgard_nir_assign_sysvals(compiler_context
*ctx
, nir_shader
*shader
)
406 ctx
->sysval_count
= 0;
408 nir_foreach_function(function
, shader
) {
409 if (!function
->impl
) continue;
411 nir_foreach_block(block
, function
->impl
) {
412 nir_foreach_instr_safe(instr
, block
) {
413 midgard_nir_assign_sysval_body(ctx
, instr
);
420 midgard_nir_lower_fdot2(nir_shader
*shader
)
422 bool progress
= false;
424 nir_foreach_function(function
, shader
) {
425 if (!function
->impl
) continue;
428 nir_builder
*b
= &_b
;
429 nir_builder_init(b
, function
->impl
);
431 nir_foreach_block(block
, function
->impl
) {
432 nir_foreach_instr_safe(instr
, block
) {
433 if (instr
->type
!= nir_instr_type_alu
) continue;
435 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
436 midgard_nir_lower_fdot2_body(b
, alu
);
442 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);
449 /* Flushes undefined values to zero */
452 optimise_nir(nir_shader
*nir
, unsigned quirks
)
455 unsigned lower_flrp
=
456 (nir
->options
->lower_flrp16
? 16 : 0) |
457 (nir
->options
->lower_flrp32
? 32 : 0) |
458 (nir
->options
->lower_flrp64
? 64 : 0);
460 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
461 NIR_PASS(progress
, nir
, nir_lower_idiv
, nir_lower_idiv_fast
);
463 nir_lower_tex_options lower_tex_options
= {
464 .lower_txs_lod
= true,
466 .lower_tex_without_implicit_lod
=
467 (quirks
& MIDGARD_EXPLICIT_LOD
),
469 /* TODO: we have native gradient.. */
473 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
475 /* Must lower fdot2 after tex is lowered */
476 NIR_PASS(progress
, nir
, midgard_nir_lower_fdot2
);
478 /* T720 is broken. */
480 if (quirks
& MIDGARD_BROKEN_LOD
)
481 NIR_PASS_V(nir
, midgard_nir_lod_errata
);
486 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
487 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
489 NIR_PASS(progress
, nir
, nir_copy_prop
);
490 NIR_PASS(progress
, nir
, nir_opt_dce
);
491 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
492 NIR_PASS(progress
, nir
, nir_opt_cse
);
493 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
494 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
495 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
497 if (lower_flrp
!= 0) {
498 bool lower_flrp_progress
= false;
499 NIR_PASS(lower_flrp_progress
,
503 false /* always_precise */,
504 nir
->options
->lower_ffma
);
505 if (lower_flrp_progress
) {
506 NIR_PASS(progress
, nir
,
507 nir_opt_constant_folding
);
511 /* Nothing should rematerialize any flrps, so we only
512 * need to do this lowering once.
517 NIR_PASS(progress
, nir
, nir_opt_undef
);
518 NIR_PASS(progress
, nir
, nir_undef_to_zero
);
520 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
523 nir_var_function_temp
);
525 NIR_PASS(progress
, nir
, nir_opt_vectorize
);
528 /* Must be run at the end to prevent creation of fsin/fcos ops */
529 NIR_PASS(progress
, nir
, midgard_nir_scale_trig
);
534 NIR_PASS(progress
, nir
, nir_opt_dce
);
535 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
536 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
537 NIR_PASS(progress
, nir
, nir_copy_prop
);
540 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
542 /* We implement booleans as 32-bit 0/~0 */
543 NIR_PASS(progress
, nir
, nir_lower_bool_to_int32
);
545 /* Now that booleans are lowered, we can run out late opts */
546 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_late
);
548 /* Lower mods for float ops only. Integer ops don't support modifiers
549 * (saturate doesn't make sense on integers, neg/abs require dedicated
552 NIR_PASS(progress
, nir
, nir_lower_to_source_mods
, nir_lower_float_source_mods
);
553 NIR_PASS(progress
, nir
, nir_copy_prop
);
554 NIR_PASS(progress
, nir
, nir_opt_dce
);
556 /* Take us out of SSA */
557 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
558 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
560 /* We are a vector architecture; write combine where possible */
561 NIR_PASS(progress
, nir
, nir_move_vec_src_uses_to_dest
);
562 NIR_PASS(progress
, nir
, nir_lower_vec_to_movs
);
564 NIR_PASS(progress
, nir
, nir_opt_dce
);
567 /* Do not actually emit a load; instead, cache the constant for inlining */
570 emit_load_const(compiler_context
*ctx
, nir_load_const_instr
*instr
)
572 nir_ssa_def def
= instr
->def
;
574 float *v
= rzalloc_array(NULL
, float, 4);
575 nir_const_value_to_array(v
, instr
->value
, instr
->def
.num_components
, f32
);
577 /* Shifted for SSA, +1 for off-by-one */
578 _mesa_hash_table_u64_insert(ctx
->ssa_constants
, (def
.index
<< 1) + 1, v
);
581 /* Normally constants are embedded implicitly, but for I/O and such we have to
582 * explicitly emit a move with the constant source */
585 emit_explicit_constant(compiler_context
*ctx
, unsigned node
, unsigned to
)
587 void *constant_value
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, node
+ 1);
589 if (constant_value
) {
590 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), to
);
591 attach_constants(ctx
, &ins
, constant_value
, node
+ 1);
592 emit_mir_instruction(ctx
, ins
);
597 nir_is_non_scalar_swizzle(nir_alu_src
*src
, unsigned nr_components
)
599 unsigned comp
= src
->swizzle
[0];
601 for (unsigned c
= 1; c
< nr_components
; ++c
) {
602 if (src
->swizzle
[c
] != comp
)
609 #define ALU_CASE(nir, _op) \
611 op = midgard_alu_op_##_op; \
612 assert(src_bitsize == dst_bitsize); \
615 #define ALU_CASE_BCAST(nir, _op, count) \
617 op = midgard_alu_op_##_op; \
618 broadcast_swizzle = count; \
619 assert(src_bitsize == dst_bitsize); \
622 nir_is_fzero_constant(nir_src src
)
624 if (!nir_src_is_const(src
))
627 for (unsigned c
= 0; c
< nir_src_num_components(src
); ++c
) {
628 if (nir_src_comp_as_float(src
, c
) != 0.0)
635 /* Analyze the sizes of the inputs to determine which reg mode. Ops needed
636 * special treatment override this anyway. */
638 static midgard_reg_mode
639 reg_mode_for_nir(nir_alu_instr
*instr
)
641 unsigned src_bitsize
= nir_src_bit_size(instr
->src
[0].src
);
643 switch (src_bitsize
) {
645 return midgard_reg_mode_8
;
647 return midgard_reg_mode_16
;
649 return midgard_reg_mode_32
;
651 return midgard_reg_mode_64
;
653 unreachable("Invalid bit size");
658 emit_alu(compiler_context
*ctx
, nir_alu_instr
*instr
)
660 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
661 * is handled elsewhere */
663 if (instr
->op
== nir_op_fddx
|| instr
->op
== nir_op_fddy
) {
664 midgard_emit_derivatives(ctx
, instr
);
668 bool is_ssa
= instr
->dest
.dest
.is_ssa
;
670 unsigned dest
= nir_dest_index(ctx
, &instr
->dest
.dest
);
671 unsigned nr_components
= nir_dest_num_components(instr
->dest
.dest
);
672 unsigned nr_inputs
= nir_op_infos
[instr
->op
].num_inputs
;
674 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
675 * supported. A few do not and are commented for now. Also, there are a
676 * number of NIR ops which Midgard does not support and need to be
677 * lowered, also TODO. This switch block emits the opcode and calling
678 * convention of the Midgard instruction; actual packing is done in
683 /* Number of components valid to check for the instruction (the rest
684 * will be forced to the last), or 0 to use as-is. Relevant as
685 * ball-type instructions have a channel count in NIR but are all vec4
688 unsigned broadcast_swizzle
= 0;
690 /* What register mode should we operate in? */
691 midgard_reg_mode reg_mode
=
692 reg_mode_for_nir(instr
);
694 /* Do we need a destination override? Used for inline
697 midgard_dest_override dest_override
=
698 midgard_dest_override_none
;
700 /* Should we use a smaller respective source and sign-extend? */
702 bool half_1
= false, sext_1
= false;
703 bool half_2
= false, sext_2
= false;
705 unsigned src_bitsize
= nir_src_bit_size(instr
->src
[0].src
);
706 unsigned dst_bitsize
= nir_dest_bit_size(instr
->dest
.dest
);
709 ALU_CASE(fadd
, fadd
);
710 ALU_CASE(fmul
, fmul
);
711 ALU_CASE(fmin
, fmin
);
712 ALU_CASE(fmax
, fmax
);
713 ALU_CASE(imin
, imin
);
714 ALU_CASE(imax
, imax
);
715 ALU_CASE(umin
, umin
);
716 ALU_CASE(umax
, umax
);
717 ALU_CASE(ffloor
, ffloor
);
718 ALU_CASE(fround_even
, froundeven
);
719 ALU_CASE(ftrunc
, ftrunc
);
720 ALU_CASE(fceil
, fceil
);
721 ALU_CASE(fdot3
, fdot3
);
722 ALU_CASE(fdot4
, fdot4
);
723 ALU_CASE(iadd
, iadd
);
724 ALU_CASE(isub
, isub
);
725 ALU_CASE(imul
, imul
);
727 /* Zero shoved as second-arg */
728 ALU_CASE(iabs
, iabsdiff
);
732 ALU_CASE(feq32
, feq
);
733 ALU_CASE(fne32
, fne
);
734 ALU_CASE(flt32
, flt
);
735 ALU_CASE(ieq32
, ieq
);
736 ALU_CASE(ine32
, ine
);
737 ALU_CASE(ilt32
, ilt
);
738 ALU_CASE(ult32
, ult
);
740 /* We don't have a native b2f32 instruction. Instead, like many
741 * GPUs, we exploit booleans as 0/~0 for false/true, and
742 * correspondingly AND
743 * by 1.0 to do the type conversion. For the moment, prime us
746 * iand [whatever], #0
748 * At the end of emit_alu (as MIR), we'll fix-up the constant
751 ALU_CASE(b2f32
, iand
);
752 ALU_CASE(b2i32
, iand
);
754 /* Likewise, we don't have a dedicated f2b32 instruction, but
755 * we can do a "not equal to 0.0" test. */
757 ALU_CASE(f2b32
, fne
);
758 ALU_CASE(i2b32
, ine
);
760 ALU_CASE(frcp
, frcp
);
761 ALU_CASE(frsq
, frsqrt
);
762 ALU_CASE(fsqrt
, fsqrt
);
763 ALU_CASE(fexp2
, fexp2
);
764 ALU_CASE(flog2
, flog2
);
766 ALU_CASE(f2i32
, f2i_rtz
);
767 ALU_CASE(f2u32
, f2u_rtz
);
768 ALU_CASE(i2f32
, i2f_rtz
);
769 ALU_CASE(u2f32
, u2f_rtz
);
771 ALU_CASE(f2i16
, f2i_rtz
);
772 ALU_CASE(f2u16
, f2u_rtz
);
773 ALU_CASE(i2f16
, i2f_rtz
);
774 ALU_CASE(u2f16
, u2f_rtz
);
776 ALU_CASE(fsin
, fsin
);
777 ALU_CASE(fcos
, fcos
);
779 /* We'll set invert */
780 ALU_CASE(inot
, imov
);
781 ALU_CASE(iand
, iand
);
783 ALU_CASE(ixor
, ixor
);
784 ALU_CASE(ishl
, ishl
);
785 ALU_CASE(ishr
, iasr
);
786 ALU_CASE(ushr
, ilsr
);
788 ALU_CASE_BCAST(b32all_fequal2
, fball_eq
, 2);
789 ALU_CASE_BCAST(b32all_fequal3
, fball_eq
, 3);
790 ALU_CASE(b32all_fequal4
, fball_eq
);
792 ALU_CASE_BCAST(b32any_fnequal2
, fbany_neq
, 2);
793 ALU_CASE_BCAST(b32any_fnequal3
, fbany_neq
, 3);
794 ALU_CASE(b32any_fnequal4
, fbany_neq
);
796 ALU_CASE_BCAST(b32all_iequal2
, iball_eq
, 2);
797 ALU_CASE_BCAST(b32all_iequal3
, iball_eq
, 3);
798 ALU_CASE(b32all_iequal4
, iball_eq
);
800 ALU_CASE_BCAST(b32any_inequal2
, ibany_neq
, 2);
801 ALU_CASE_BCAST(b32any_inequal3
, ibany_neq
, 3);
802 ALU_CASE(b32any_inequal4
, ibany_neq
);
804 /* Source mods will be shoved in later */
805 ALU_CASE(fabs
, fmov
);
806 ALU_CASE(fneg
, fmov
);
807 ALU_CASE(fsat
, fmov
);
809 /* For size conversion, we use a move. Ideally though we would squash
810 * these ops together; maybe that has to happen after in NIR as part of
811 * propagation...? An earlier algebraic pass ensured we step down by
812 * only / exactly one size. If stepping down, we use a dest override to
813 * reduce the size; if stepping up, we use a larger-sized move with a
814 * half source and a sign/zero-extension modifier */
820 /* If we end up upscale, we'll need a sign-extend on the
821 * operand (the second argument) */
829 op
= midgard_alu_op_imov
;
831 if (dst_bitsize
== (src_bitsize
* 2)) {
835 /* Use a greater register mode */
837 } else if (src_bitsize
== (dst_bitsize
* 2)) {
838 /* Converting down */
839 dest_override
= midgard_dest_override_lower
;
846 assert(src_bitsize
== 32);
848 op
= midgard_alu_op_fmov
;
849 dest_override
= midgard_dest_override_lower
;
854 assert(src_bitsize
== 16);
856 op
= midgard_alu_op_fmov
;
863 /* For greater-or-equal, we lower to less-or-equal and flip the
871 instr
->op
== nir_op_fge
? midgard_alu_op_fle
:
872 instr
->op
== nir_op_fge32
? midgard_alu_op_fle
:
873 instr
->op
== nir_op_ige32
? midgard_alu_op_ile
:
874 instr
->op
== nir_op_uge32
? midgard_alu_op_ule
:
877 /* Swap via temporary */
878 nir_alu_src temp
= instr
->src
[1];
879 instr
->src
[1] = instr
->src
[0];
880 instr
->src
[0] = temp
;
885 case nir_op_b32csel
: {
886 /* Midgard features both fcsel and icsel, depending on
887 * the type of the arguments/output. However, as long
888 * as we're careful we can _always_ use icsel and
889 * _never_ need fcsel, since the latter does additional
890 * floating-point-specific processing whereas the
891 * former just moves bits on the wire. It's not obvious
892 * why these are separate opcodes, save for the ability
893 * to do things like sat/pos/abs/neg for free */
895 bool mixed
= nir_is_non_scalar_swizzle(&instr
->src
[0], nr_components
);
896 op
= mixed
? midgard_alu_op_icsel_v
: midgard_alu_op_icsel
;
898 /* The condition is the first argument; move the other
899 * arguments up one to be a binary instruction for
900 * Midgard with the condition last */
902 nir_alu_src temp
= instr
->src
[2];
904 instr
->src
[2] = instr
->src
[0];
905 instr
->src
[0] = instr
->src
[1];
906 instr
->src
[1] = temp
;
912 DBG("Unhandled ALU op %s\n", nir_op_infos
[instr
->op
].name
);
917 /* Midgard can perform certain modifiers on output of an ALU op */
920 if (midgard_is_integer_out_op(op
)) {
921 outmod
= midgard_outmod_int_wrap
;
923 bool sat
= instr
->dest
.saturate
|| instr
->op
== nir_op_fsat
;
924 outmod
= sat
? midgard_outmod_sat
: midgard_outmod_none
;
927 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
929 if (instr
->op
== nir_op_fmax
) {
930 if (nir_is_fzero_constant(instr
->src
[0].src
)) {
931 op
= midgard_alu_op_fmov
;
933 outmod
= midgard_outmod_pos
;
934 instr
->src
[0] = instr
->src
[1];
935 } else if (nir_is_fzero_constant(instr
->src
[1].src
)) {
936 op
= midgard_alu_op_fmov
;
938 outmod
= midgard_outmod_pos
;
942 /* Fetch unit, quirks, etc information */
943 unsigned opcode_props
= alu_opcode_props
[op
].props
;
944 bool quirk_flipped_r24
= opcode_props
& QUIRK_FLIPPED_R24
;
946 /* src0 will always exist afaik, but src1 will not for 1-argument
947 * instructions. The latter can only be fetched if the instruction
948 * needs it, or else we may segfault. */
950 unsigned src0
= nir_alu_src_index(ctx
, &instr
->src
[0]);
951 unsigned src1
= nr_inputs
>= 2 ? nir_alu_src_index(ctx
, &instr
->src
[1]) : ~0;
952 unsigned src2
= nr_inputs
== 3 ? nir_alu_src_index(ctx
, &instr
->src
[2]) : ~0;
953 assert(nr_inputs
<= 3);
955 /* Rather than use the instruction generation helpers, we do it
956 * ourselves here to avoid the mess */
958 midgard_instruction ins
= {
961 quirk_flipped_r24
? ~0 : src0
,
962 quirk_flipped_r24
? src0
: src1
,
969 nir_alu_src
*nirmods
[3] = { NULL
};
971 if (nr_inputs
>= 2) {
972 nirmods
[0] = &instr
->src
[0];
973 nirmods
[1] = &instr
->src
[1];
974 } else if (nr_inputs
== 1) {
975 nirmods
[quirk_flipped_r24
] = &instr
->src
[0];
981 nirmods
[2] = &instr
->src
[2];
983 /* These were lowered to a move, so apply the corresponding mod */
985 if (instr
->op
== nir_op_fneg
|| instr
->op
== nir_op_fabs
) {
986 nir_alu_src
*s
= nirmods
[quirk_flipped_r24
];
988 if (instr
->op
== nir_op_fneg
)
989 s
->negate
= !s
->negate
;
991 if (instr
->op
== nir_op_fabs
)
995 bool is_int
= midgard_is_integer_op(op
);
997 ins
.mask
= mask_of(nr_components
);
999 midgard_vector_alu alu
= {
1001 .reg_mode
= reg_mode
,
1002 .dest_override
= dest_override
,
1005 .src1
= vector_alu_srco_unsigned(vector_alu_modifiers(nirmods
[0], is_int
, broadcast_swizzle
, half_1
, sext_1
)),
1006 .src2
= vector_alu_srco_unsigned(vector_alu_modifiers(nirmods
[1], is_int
, broadcast_swizzle
, half_2
, sext_2
)),
1009 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
1012 ins
.mask
&= instr
->dest
.write_mask
;
1014 for (unsigned m
= 0; m
< 3; ++m
) {
1018 for (unsigned c
= 0; c
< NIR_MAX_VEC_COMPONENTS
; ++c
)
1019 ins
.swizzle
[m
][c
] = nirmods
[m
]->swizzle
[c
];
1021 /* Replicate. TODO: remove when vec16 lands */
1022 for (unsigned c
= NIR_MAX_VEC_COMPONENTS
; c
< MIR_VEC_COMPONENTS
; ++c
)
1023 ins
.swizzle
[m
][c
] = nirmods
[m
]->swizzle
[NIR_MAX_VEC_COMPONENTS
- 1];
1026 if (nr_inputs
== 3) {
1027 /* Conditions can't have mods */
1028 assert(!nirmods
[2]->abs
);
1029 assert(!nirmods
[2]->negate
);
1034 /* Late fixup for emulated instructions */
1036 if (instr
->op
== nir_op_b2f32
|| instr
->op
== nir_op_b2i32
) {
1037 /* Presently, our second argument is an inline #0 constant.
1038 * Switch over to an embedded 1.0 constant (that can't fit
1039 * inline, since we're 32-bit, not 16-bit like the inline
1042 ins
.has_inline_constant
= false;
1043 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1044 ins
.has_constants
= true;
1046 if (instr
->op
== nir_op_b2f32
) {
1048 memcpy(&ins
.constants
, &f
, sizeof(float));
1050 ins
.constants
[0] = 1;
1054 for (unsigned c
= 0; c
< 16; ++c
)
1055 ins
.swizzle
[1][c
] = 0;
1056 } else if (nr_inputs
== 1 && !quirk_flipped_r24
) {
1057 /* Lots of instructions need a 0 plonked in */
1058 ins
.has_inline_constant
= false;
1059 ins
.src
[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1060 ins
.has_constants
= true;
1061 ins
.constants
[0] = 0;
1063 for (unsigned c
= 0; c
< 16; ++c
)
1064 ins
.swizzle
[1][c
] = 0;
1065 } else if (instr
->op
== nir_op_inot
) {
1069 if ((opcode_props
& UNITS_ALL
) == UNIT_VLUT
) {
1070 /* To avoid duplicating the lookup tables (probably), true LUT
1071 * instructions can only operate as if they were scalars. Lower
1072 * them here by changing the component. */
1074 unsigned orig_mask
= ins
.mask
;
1076 for (int i
= 0; i
< nr_components
; ++i
) {
1077 /* Mask the associated component, dropping the
1078 * instruction if needed */
1081 ins
.mask
&= orig_mask
;
1086 for (unsigned j
= 0; j
< MIR_VEC_COMPONENTS
; ++j
)
1087 ins
.swizzle
[0][j
] = nirmods
[0]->swizzle
[i
]; /* Pull from the correct component */
1089 emit_mir_instruction(ctx
, ins
);
1092 emit_mir_instruction(ctx
, ins
);
1099 mir_set_intr_mask(nir_instr
*instr
, midgard_instruction
*ins
, bool is_read
)
1101 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
1102 unsigned nir_mask
= 0;
1106 nir_mask
= mask_of(nir_intrinsic_dest_components(intr
));
1107 dsize
= nir_dest_bit_size(intr
->dest
);
1109 nir_mask
= nir_intrinsic_write_mask(intr
);
1113 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1114 unsigned bytemask
= mir_to_bytemask(mir_mode_for_destsize(dsize
), nir_mask
);
1115 mir_set_bytemask(ins
, bytemask
);
1118 ins
->load_64
= true;
1121 /* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1122 * optimized) versions of UBO #0 */
1124 static midgard_instruction
*
1126 compiler_context
*ctx
,
1130 nir_src
*indirect_offset
,
1131 unsigned indirect_shift
,
1134 /* TODO: half-floats */
1136 midgard_instruction ins
= m_ld_ubo_int4(dest
, 0);
1137 ins
.constants
[0] = offset
;
1139 if (instr
->type
== nir_instr_type_intrinsic
)
1140 mir_set_intr_mask(instr
, &ins
, true);
1142 if (indirect_offset
) {
1143 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1144 ins
.load_store
.arg_2
= (indirect_shift
<< 5);
1146 ins
.load_store
.arg_2
= 0x1E;
1149 ins
.load_store
.arg_1
= index
;
1151 return emit_mir_instruction(ctx
, ins
);
1154 /* SSBO reads are like UBO reads if you squint */
1158 compiler_context
*ctx
,
1163 nir_src
*indirect_offset
,
1168 midgard_instruction ins
;
1171 ins
= m_ld_int4(srcdest
, offset
);
1173 ins
= m_st_int4(srcdest
, offset
);
1175 /* SSBO reads use a generic memory read interface, so we need the
1176 * address of the SSBO as the first argument. This is a sysval. */
1178 unsigned addr
= make_compiler_temp(ctx
);
1179 emit_sysval_read(ctx
, instr
, addr
, 2);
1181 /* The source array:
1183 * src[0] = store ? value : unused
1187 * We would like arg_1 = the address and
1188 * arg_2 = the offset.
1193 /* TODO: What is this? It looks superficially like a shift << 5, but
1194 * arg_1 doesn't take a shift Should it be E0 or A0? We also need the
1195 * indirect offset. */
1197 if (indirect_offset
) {
1198 ins
.load_store
.arg_1
|= 0xE0;
1199 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1201 ins
.load_store
.arg_2
= 0x7E;
1204 /* TODO: Bounds check */
1206 /* Finally, we emit the direct offset */
1208 ins
.load_store
.varying_parameters
= (offset
& 0x1FF) << 1;
1209 ins
.load_store
.address
= (offset
>> 9);
1210 mir_set_intr_mask(instr
, &ins
, is_read
);
1212 emit_mir_instruction(ctx
, ins
);
1217 compiler_context
*ctx
,
1218 unsigned dest
, unsigned offset
,
1219 unsigned nr_comp
, unsigned component
,
1220 nir_src
*indirect_offset
, nir_alu_type type
, bool flat
)
1222 /* XXX: Half-floats? */
1223 /* TODO: swizzle, mask */
1225 midgard_instruction ins
= m_ld_vary_32(dest
, offset
);
1226 ins
.mask
= mask_of(nr_comp
);
1228 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[0]); ++i
)
1229 ins
.swizzle
[0][i
] = MIN2(i
+ component
, COMPONENT_W
);
1231 midgard_varying_parameter p
= {
1233 .interpolation
= midgard_interp_default
,
1238 memcpy(&u
, &p
, sizeof(p
));
1239 ins
.load_store
.varying_parameters
= u
;
1241 if (indirect_offset
)
1242 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1244 ins
.load_store
.arg_2
= 0x1E;
1246 ins
.load_store
.arg_1
= 0x9E;
1248 /* Use the type appropriate load */
1252 ins
.load_store
.op
= midgard_op_ld_vary_32u
;
1255 ins
.load_store
.op
= midgard_op_ld_vary_32i
;
1257 case nir_type_float
:
1258 ins
.load_store
.op
= midgard_op_ld_vary_32
;
1261 unreachable("Attempted to load unknown type");
1265 emit_mir_instruction(ctx
, ins
);
1270 compiler_context
*ctx
,
1271 unsigned dest
, unsigned offset
,
1272 unsigned nr_comp
, nir_alu_type t
)
1274 midgard_instruction ins
= m_ld_attr_32(dest
, offset
);
1275 ins
.load_store
.arg_1
= 0x1E;
1276 ins
.load_store
.arg_2
= 0x1E;
1277 ins
.mask
= mask_of(nr_comp
);
1279 /* Use the type appropriate load */
1283 ins
.load_store
.op
= midgard_op_ld_attr_32u
;
1286 ins
.load_store
.op
= midgard_op_ld_attr_32i
;
1288 case nir_type_float
:
1289 ins
.load_store
.op
= midgard_op_ld_attr_32
;
1292 unreachable("Attempted to load unknown type");
1296 emit_mir_instruction(ctx
, ins
);
1300 emit_sysval_read(compiler_context
*ctx
, nir_instr
*instr
, signed dest_override
,
1301 unsigned nr_components
)
1305 /* Figure out which uniform this is */
1306 int sysval
= sysval_for_instr(ctx
, instr
, &dest
);
1307 void *val
= _mesa_hash_table_u64_search(ctx
->sysval_to_id
, sysval
);
1309 if (dest_override
>= 0)
1310 dest
= dest_override
;
1312 /* Sysvals are prefix uniforms */
1313 unsigned uniform
= ((uintptr_t) val
) - 1;
1315 /* Emit the read itself -- this is never indirect */
1316 midgard_instruction
*ins
=
1317 emit_ubo_read(ctx
, instr
, dest
, uniform
* 16, NULL
, 0, 0);
1319 ins
->mask
= mask_of(nr_components
);
1323 compute_builtin_arg(nir_op op
)
1326 case nir_intrinsic_load_work_group_id
:
1328 case nir_intrinsic_load_local_invocation_id
:
1331 unreachable("Invalid compute paramater loaded");
1336 emit_fragment_store(compiler_context
*ctx
, unsigned src
, unsigned rt
)
1338 emit_explicit_constant(ctx
, src
, src
);
1340 struct midgard_instruction ins
=
1341 v_branch(false, false);
1343 ins
.writeout
= true;
1345 /* Add dependencies */
1347 ins
.constants
[0] = rt
* 0x100;
1349 /* Emit the branch */
1350 midgard_instruction
*br
= emit_mir_instruction(ctx
, ins
);
1351 schedule_barrier(ctx
);
1353 assert(rt
< ARRAY_SIZE(ctx
->writeout_branch
));
1354 assert(!ctx
->writeout_branch
[rt
]);
1355 ctx
->writeout_branch
[rt
] = br
;
1357 /* Push our current location = current block count - 1 = where we'll
1358 * jump to. Maybe a bit too clever for my own good */
1360 br
->branch
.target_block
= ctx
->block_count
- 1;
1364 emit_compute_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1366 unsigned reg
= nir_dest_index(ctx
, &instr
->dest
);
1367 midgard_instruction ins
= m_ld_compute_id(reg
, 0);
1368 ins
.mask
= mask_of(3);
1369 ins
.load_store
.arg_1
= compute_builtin_arg(instr
->intrinsic
);
1370 emit_mir_instruction(ctx
, ins
);
1374 vertex_builtin_arg(nir_op op
)
1377 case nir_intrinsic_load_vertex_id
:
1378 return PAN_VERTEX_ID
;
1379 case nir_intrinsic_load_instance_id
:
1380 return PAN_INSTANCE_ID
;
1382 unreachable("Invalid vertex builtin");
1387 emit_vertex_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1389 unsigned reg
= nir_dest_index(ctx
, &instr
->dest
);
1390 emit_attr_read(ctx
, reg
, vertex_builtin_arg(instr
->intrinsic
), 1, nir_type_int
);
1394 emit_intrinsic(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1396 unsigned offset
= 0, reg
;
1398 switch (instr
->intrinsic
) {
1399 case nir_intrinsic_discard_if
:
1400 case nir_intrinsic_discard
: {
1401 bool conditional
= instr
->intrinsic
== nir_intrinsic_discard_if
;
1402 struct midgard_instruction discard
= v_branch(conditional
, false);
1403 discard
.branch
.target_type
= TARGET_DISCARD
;
1406 discard
.src
[0] = nir_src_index(ctx
, &instr
->src
[0]);
1408 emit_mir_instruction(ctx
, discard
);
1409 schedule_barrier(ctx
);
1414 case nir_intrinsic_load_uniform
:
1415 case nir_intrinsic_load_ubo
:
1416 case nir_intrinsic_load_ssbo
:
1417 case nir_intrinsic_load_input
:
1418 case nir_intrinsic_load_interpolated_input
: {
1419 bool is_uniform
= instr
->intrinsic
== nir_intrinsic_load_uniform
;
1420 bool is_ubo
= instr
->intrinsic
== nir_intrinsic_load_ubo
;
1421 bool is_ssbo
= instr
->intrinsic
== nir_intrinsic_load_ssbo
;
1422 bool is_flat
= instr
->intrinsic
== nir_intrinsic_load_input
;
1423 bool is_interp
= instr
->intrinsic
== nir_intrinsic_load_interpolated_input
;
1425 /* Get the base type of the intrinsic */
1426 /* TODO: Infer type? Does it matter? */
1428 (is_ubo
|| is_ssbo
) ? nir_type_uint
:
1429 (is_interp
) ? nir_type_float
:
1430 nir_intrinsic_type(instr
);
1432 t
= nir_alu_type_get_base_type(t
);
1434 if (!(is_ubo
|| is_ssbo
)) {
1435 offset
= nir_intrinsic_base(instr
);
1438 unsigned nr_comp
= nir_intrinsic_dest_components(instr
);
1440 nir_src
*src_offset
= nir_get_io_offset_src(instr
);
1442 bool direct
= nir_src_is_const(*src_offset
);
1443 nir_src
*indirect_offset
= direct
? NULL
: src_offset
;
1446 offset
+= nir_src_as_uint(*src_offset
);
1448 /* We may need to apply a fractional offset */
1449 int component
= (is_flat
|| is_interp
) ?
1450 nir_intrinsic_component(instr
) : 0;
1451 reg
= nir_dest_index(ctx
, &instr
->dest
);
1453 if (is_uniform
&& !ctx
->is_blend
) {
1454 emit_ubo_read(ctx
, &instr
->instr
, reg
, (ctx
->sysval_count
+ offset
) * 16, indirect_offset
, 4, 0);
1455 } else if (is_ubo
) {
1456 nir_src index
= instr
->src
[0];
1458 /* TODO: Is indirect block number possible? */
1459 assert(nir_src_is_const(index
));
1461 uint32_t uindex
= nir_src_as_uint(index
) + 1;
1462 emit_ubo_read(ctx
, &instr
->instr
, reg
, offset
, indirect_offset
, 0, uindex
);
1463 } else if (is_ssbo
) {
1464 nir_src index
= instr
->src
[0];
1465 assert(nir_src_is_const(index
));
1466 uint32_t uindex
= nir_src_as_uint(index
);
1468 emit_ssbo_access(ctx
, &instr
->instr
, true, reg
, offset
, indirect_offset
, uindex
);
1469 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& !ctx
->is_blend
) {
1470 emit_varying_read(ctx
, reg
, offset
, nr_comp
, component
, indirect_offset
, t
, is_flat
);
1471 } else if (ctx
->is_blend
) {
1472 /* For blend shaders, load the input color, which is
1473 * preloaded to r0 */
1475 midgard_instruction move
= v_mov(SSA_FIXED_REGISTER(0), reg
);
1476 emit_mir_instruction(ctx
, move
);
1477 schedule_barrier(ctx
);
1478 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1479 emit_attr_read(ctx
, reg
, offset
, nr_comp
, t
);
1481 DBG("Unknown load\n");
1488 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1489 case nir_intrinsic_load_barycentric_pixel
:
1492 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1494 case nir_intrinsic_load_raw_output_pan
:
1495 case nir_intrinsic_load_output_u8_as_fp16_pan
:
1496 reg
= nir_dest_index(ctx
, &instr
->dest
);
1497 assert(ctx
->is_blend
);
1499 /* T720 and below use different blend opcodes with slightly
1500 * different semantics than T760 and up */
1502 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1503 bool old_blend
= ctx
->quirks
& MIDGARD_OLD_BLEND
;
1505 if (instr
->intrinsic
== nir_intrinsic_load_output_u8_as_fp16_pan
) {
1506 ld
.load_store
.op
= old_blend
?
1507 midgard_op_ld_color_buffer_u8_as_fp16_old
:
1508 midgard_op_ld_color_buffer_u8_as_fp16
;
1511 ld
.load_store
.address
= 1;
1512 ld
.load_store
.arg_2
= 0x1E;
1515 for (unsigned c
= 2; c
< 16; ++c
)
1516 ld
.swizzle
[0][c
] = 0;
1519 emit_mir_instruction(ctx
, ld
);
1522 case nir_intrinsic_load_blend_const_color_rgba
: {
1523 assert(ctx
->is_blend
);
1524 reg
= nir_dest_index(ctx
, &instr
->dest
);
1526 /* Blend constants are embedded directly in the shader and
1527 * patched in, so we use some magic routing */
1529 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), reg
);
1530 ins
.has_constants
= true;
1531 ins
.has_blend_constant
= true;
1532 emit_mir_instruction(ctx
, ins
);
1536 case nir_intrinsic_store_output
:
1537 assert(nir_src_is_const(instr
->src
[1]) && "no indirect outputs");
1539 offset
= nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[1]);
1541 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1543 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
1544 emit_fragment_store(ctx
, reg
, offset
);
1545 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1546 /* We should have been vectorized, though we don't
1547 * currently check that st_vary is emitted only once
1548 * per slot (this is relevant, since there's not a mask
1549 * parameter available on the store [set to 0 by the
1550 * blob]). We do respect the component by adjusting the
1551 * swizzle. If this is a constant source, we'll need to
1552 * emit that explicitly. */
1554 emit_explicit_constant(ctx
, reg
, reg
);
1556 unsigned component
= nir_intrinsic_component(instr
);
1557 unsigned nr_comp
= nir_src_num_components(instr
->src
[0]);
1559 midgard_instruction st
= m_st_vary_32(reg
, offset
);
1560 st
.load_store
.arg_1
= 0x9E;
1561 st
.load_store
.arg_2
= 0x1E;
1563 switch (nir_alu_type_get_base_type(nir_intrinsic_type(instr
))) {
1566 st
.load_store
.op
= midgard_op_st_vary_32u
;
1569 st
.load_store
.op
= midgard_op_st_vary_32i
;
1571 case nir_type_float
:
1572 st
.load_store
.op
= midgard_op_st_vary_32
;
1575 unreachable("Attempted to store unknown type");
1579 for (unsigned i
= 0; i
< ARRAY_SIZE(st
.swizzle
[0]); ++i
)
1580 st
.swizzle
[0][i
] = MIN2(i
+ component
, nr_comp
);
1582 emit_mir_instruction(ctx
, st
);
1584 DBG("Unknown store\n");
1590 /* Special case of store_output for lowered blend shaders */
1591 case nir_intrinsic_store_raw_output_pan
:
1592 assert (ctx
->stage
== MESA_SHADER_FRAGMENT
);
1593 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1595 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1596 /* Suppose reg = qr0.xyzw. That means 4 8-bit ---> 1 32-bit. So
1597 * reg = r0.x. We want to splatter. So we can do a 32-bit move
1600 * imov r0.xyzw, r0.xxxx
1603 unsigned expanded
= make_compiler_temp(ctx
);
1605 midgard_instruction splatter
= v_mov(reg
, expanded
);
1607 for (unsigned c
= 0; c
< 16; ++c
)
1608 splatter
.swizzle
[1][c
] = 0;
1610 emit_mir_instruction(ctx
, splatter
);
1611 emit_fragment_store(ctx
, expanded
, ctx
->blend_rt
);
1613 emit_fragment_store(ctx
, reg
, ctx
->blend_rt
);
1617 case nir_intrinsic_store_ssbo
:
1618 assert(nir_src_is_const(instr
->src
[1]));
1620 bool direct_offset
= nir_src_is_const(instr
->src
[2]);
1621 offset
= direct_offset
? nir_src_as_uint(instr
->src
[2]) : 0;
1622 nir_src
*indirect_offset
= direct_offset
? NULL
: &instr
->src
[2];
1623 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1625 uint32_t uindex
= nir_src_as_uint(instr
->src
[1]);
1627 emit_explicit_constant(ctx
, reg
, reg
);
1628 emit_ssbo_access(ctx
, &instr
->instr
, false, reg
, offset
, indirect_offset
, uindex
);
1631 case nir_intrinsic_load_viewport_scale
:
1632 case nir_intrinsic_load_viewport_offset
:
1633 case nir_intrinsic_load_num_work_groups
:
1634 case nir_intrinsic_load_sampler_lod_parameters_pan
:
1635 emit_sysval_read(ctx
, &instr
->instr
, ~0, 3);
1638 case nir_intrinsic_load_work_group_id
:
1639 case nir_intrinsic_load_local_invocation_id
:
1640 emit_compute_builtin(ctx
, instr
);
1643 case nir_intrinsic_load_vertex_id
:
1644 case nir_intrinsic_load_instance_id
:
1645 emit_vertex_builtin(ctx
, instr
);
1649 printf ("Unhandled intrinsic\n");
1656 midgard_tex_format(enum glsl_sampler_dim dim
)
1659 case GLSL_SAMPLER_DIM_1D
:
1660 case GLSL_SAMPLER_DIM_BUF
:
1663 case GLSL_SAMPLER_DIM_2D
:
1664 case GLSL_SAMPLER_DIM_EXTERNAL
:
1665 case GLSL_SAMPLER_DIM_RECT
:
1668 case GLSL_SAMPLER_DIM_3D
:
1671 case GLSL_SAMPLER_DIM_CUBE
:
1672 return MALI_TEX_CUBE
;
1675 DBG("Unknown sampler dim type\n");
1681 /* Tries to attach an explicit LOD / bias as a constant. Returns whether this
1685 pan_attach_constant_bias(
1686 compiler_context
*ctx
,
1688 midgard_texture_word
*word
)
1690 /* To attach as constant, it has to *be* constant */
1692 if (!nir_src_is_const(lod
))
1695 float f
= nir_src_as_float(lod
);
1697 /* Break into fixed-point */
1699 float lod_frac
= f
- lod_int
;
1701 /* Carry over negative fractions */
1702 if (lod_frac
< 0.0) {
1708 word
->bias
= float_to_ubyte(lod_frac
);
1709 word
->bias_int
= lod_int
;
1714 static enum mali_sampler_type
1715 midgard_sampler_type(nir_alu_type t
) {
1716 switch (nir_alu_type_get_base_type(t
))
1718 case nir_type_float
:
1719 return MALI_SAMPLER_FLOAT
;
1721 return MALI_SAMPLER_SIGNED
;
1723 return MALI_SAMPLER_UNSIGNED
;
1725 unreachable("Unknown sampler type");
1730 emit_texop_native(compiler_context
*ctx
, nir_tex_instr
*instr
,
1731 unsigned midgard_texop
)
1734 //assert (!instr->sampler);
1735 //assert (!instr->texture_array_size);
1737 int texture_index
= instr
->texture_index
;
1738 int sampler_index
= texture_index
;
1740 /* No helper to build texture words -- we do it all here */
1741 midgard_instruction ins
= {
1742 .type
= TAG_TEXTURE_4
,
1744 .dest
= nir_dest_index(ctx
, &instr
->dest
),
1745 .src
= { ~0, ~0, ~0, ~0 },
1746 .swizzle
= SWIZZLE_IDENTITY_4
,
1748 .op
= midgard_texop
,
1749 .format
= midgard_tex_format(instr
->sampler_dim
),
1750 .texture_handle
= texture_index
,
1751 .sampler_handle
= sampler_index
,
1757 .sampler_type
= midgard_sampler_type(instr
->dest_type
),
1758 .shadow
= instr
->is_shadow
,
1762 /* We may need a temporary for the coordinate */
1764 bool needs_temp_coord
=
1765 (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) ||
1766 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) ||
1769 unsigned coords
= needs_temp_coord
? make_compiler_temp_reg(ctx
) : 0;
1771 for (unsigned i
= 0; i
< instr
->num_srcs
; ++i
) {
1772 int index
= nir_src_index(ctx
, &instr
->src
[i
].src
);
1773 unsigned nr_components
= nir_src_num_components(instr
->src
[i
].src
);
1775 switch (instr
->src
[i
].src_type
) {
1776 case nir_tex_src_coord
: {
1777 emit_explicit_constant(ctx
, index
, index
);
1779 unsigned coord_mask
= mask_of(instr
->coord_components
);
1781 bool flip_zw
= (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) && (coord_mask
& (1 << COMPONENT_Z
));
1784 coord_mask
^= ((1 << COMPONENT_Z
) | (1 << COMPONENT_W
));
1786 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1787 /* texelFetch is undefined on samplerCube */
1788 assert(midgard_texop
!= TEXTURE_OP_TEXEL_FETCH
);
1790 /* For cubemaps, we use a special ld/st op to
1791 * select the face and copy the xy into the
1792 * texture register */
1794 midgard_instruction ld
= m_ld_cubemap_coords(coords
, 0);
1796 ld
.mask
= 0x3; /* xy */
1797 ld
.load_store
.arg_1
= 0x20;
1798 ld
.swizzle
[1][3] = COMPONENT_X
;
1799 emit_mir_instruction(ctx
, ld
);
1802 ins
.swizzle
[1][2] = instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
1803 ins
.swizzle
[1][3] = COMPONENT_X
;
1804 } else if (needs_temp_coord
) {
1805 /* mov coord_temp, coords */
1806 midgard_instruction mov
= v_mov(index
, coords
);
1807 mov
.mask
= coord_mask
;
1810 mov
.swizzle
[1][COMPONENT_W
] = COMPONENT_Z
;
1812 emit_mir_instruction(ctx
, mov
);
1817 ins
.src
[1] = coords
;
1819 /* Texelfetch coordinates uses all four elements
1820 * (xyz/index) regardless of texture dimensionality,
1821 * which means it's necessary to zero the unused
1822 * components to keep everything happy */
1824 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
1825 /* mov index.zw, #0, or generalized */
1826 midgard_instruction mov
=
1827 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), coords
);
1828 mov
.has_constants
= true;
1829 mov
.mask
= coord_mask
^ 0xF;
1830 emit_mir_instruction(ctx
, mov
);
1833 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) {
1834 /* Array component in w but NIR wants it in z,
1835 * but if we have a temp coord we already fixed
1838 if (nr_components
== 3) {
1839 ins
.swizzle
[1][2] = COMPONENT_Z
;
1840 ins
.swizzle
[1][3] = needs_temp_coord
? COMPONENT_W
: COMPONENT_Z
;
1841 } else if (nr_components
== 2) {
1843 instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
1844 ins
.swizzle
[1][3] = COMPONENT_X
;
1846 unreachable("Invalid texture 2D components");
1849 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
1851 ins
.swizzle
[1][2] = COMPONENT_Z
;
1852 ins
.swizzle
[1][3] = COMPONENT_W
;
1858 case nir_tex_src_bias
:
1859 case nir_tex_src_lod
: {
1860 /* Try as a constant if we can */
1862 bool is_txf
= midgard_texop
== TEXTURE_OP_TEXEL_FETCH
;
1863 if (!is_txf
&& pan_attach_constant_bias(ctx
, instr
->src
[i
].src
, &ins
.texture
))
1866 ins
.texture
.lod_register
= true;
1869 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
1870 ins
.swizzle
[2][c
] = COMPONENT_X
;
1872 emit_explicit_constant(ctx
, index
, index
);
1877 case nir_tex_src_offset
: {
1878 ins
.texture
.offset_register
= true;
1881 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
1882 ins
.swizzle
[3][c
] = (c
> COMPONENT_Z
) ? 0 : c
;
1884 emit_explicit_constant(ctx
, index
, index
);
1888 case nir_tex_src_comparator
: {
1889 unsigned comp
= COMPONENT_Z
;
1891 /* mov coord_temp.foo, coords */
1892 midgard_instruction mov
= v_mov(index
, coords
);
1893 mov
.mask
= 1 << comp
;
1895 for (unsigned i
= 0; i
< MIR_VEC_COMPONENTS
; ++i
)
1896 mov
.swizzle
[1][i
] = COMPONENT_X
;
1898 emit_mir_instruction(ctx
, mov
);
1903 unreachable("Unknown texture source type\n");
1907 emit_mir_instruction(ctx
, ins
);
1909 /* Used for .cont and .last hinting */
1910 ctx
->texture_op_count
++;
1914 emit_tex(compiler_context
*ctx
, nir_tex_instr
*instr
)
1916 switch (instr
->op
) {
1919 emit_texop_native(ctx
, instr
, TEXTURE_OP_NORMAL
);
1922 emit_texop_native(ctx
, instr
, TEXTURE_OP_LOD
);
1925 emit_texop_native(ctx
, instr
, TEXTURE_OP_TEXEL_FETCH
);
1928 emit_sysval_read(ctx
, &instr
->instr
, ~0, 4);
1931 unreachable("Unhanlded texture op");
1936 emit_jump(compiler_context
*ctx
, nir_jump_instr
*instr
)
1938 switch (instr
->type
) {
1939 case nir_jump_break
: {
1940 /* Emit a branch out of the loop */
1941 struct midgard_instruction br
= v_branch(false, false);
1942 br
.branch
.target_type
= TARGET_BREAK
;
1943 br
.branch
.target_break
= ctx
->current_loop_depth
;
1944 emit_mir_instruction(ctx
, br
);
1949 DBG("Unknown jump type %d\n", instr
->type
);
1955 emit_instr(compiler_context
*ctx
, struct nir_instr
*instr
)
1957 switch (instr
->type
) {
1958 case nir_instr_type_load_const
:
1959 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1962 case nir_instr_type_intrinsic
:
1963 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1966 case nir_instr_type_alu
:
1967 emit_alu(ctx
, nir_instr_as_alu(instr
));
1970 case nir_instr_type_tex
:
1971 emit_tex(ctx
, nir_instr_as_tex(instr
));
1974 case nir_instr_type_jump
:
1975 emit_jump(ctx
, nir_instr_as_jump(instr
));
1978 case nir_instr_type_ssa_undef
:
1983 DBG("Unhandled instruction type\n");
1989 /* ALU instructions can inline or embed constants, which decreases register
1990 * pressure and saves space. */
1992 #define CONDITIONAL_ATTACH(idx) { \
1993 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
1996 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
1997 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
2002 inline_alu_constants(compiler_context
*ctx
, midgard_block
*block
)
2004 mir_foreach_instr_in_block(block
, alu
) {
2005 /* Other instructions cannot inline constants */
2006 if (alu
->type
!= TAG_ALU_4
) continue;
2007 if (alu
->compact_branch
) continue;
2009 /* If there is already a constant here, we can do nothing */
2010 if (alu
->has_constants
) continue;
2012 CONDITIONAL_ATTACH(0);
2014 if (!alu
->has_constants
) {
2015 CONDITIONAL_ATTACH(1)
2016 } else if (!alu
->inline_constant
) {
2017 /* Corner case: _two_ vec4 constants, for instance with a
2018 * csel. For this case, we can only use a constant
2019 * register for one, we'll have to emit a move for the
2020 * other. Note, if both arguments are constants, then
2021 * necessarily neither argument depends on the value of
2022 * any particular register. As the destination register
2023 * will be wiped, that means we can spill the constant
2024 * to the destination register.
2027 void *entry
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, alu
->src
[1] + 1);
2028 unsigned scratch
= alu
->dest
;
2031 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), scratch
);
2032 attach_constants(ctx
, &ins
, entry
, alu
->src
[1] + 1);
2034 /* Set the source */
2035 alu
->src
[1] = scratch
;
2037 /* Inject us -before- the last instruction which set r31 */
2038 mir_insert_instruction_before(ctx
, mir_prev_op(alu
), ins
);
2044 /* Being a little silly with the names, but returns the op that is the bitwise
2045 * inverse of the op with the argument switched. I.e. (f and g are
2048 * f(a, b) = ~g(b, a)
2050 * Corollary: if g is the contrapositve of f, f is the contrapositive of g:
2052 * f(a, b) = ~g(b, a)
2053 * ~f(a, b) = g(b, a)
2054 * ~f(a, b) = ~h(a, b) where h is the contrapositive of g
2057 * Thus we define this function in pairs.
2060 static inline midgard_alu_op
2061 mir_contrapositive(midgard_alu_op op
)
2064 case midgard_alu_op_flt
:
2065 return midgard_alu_op_fle
;
2066 case midgard_alu_op_fle
:
2067 return midgard_alu_op_flt
;
2069 case midgard_alu_op_ilt
:
2070 return midgard_alu_op_ile
;
2071 case midgard_alu_op_ile
:
2072 return midgard_alu_op_ilt
;
2075 unreachable("No known contrapositive");
2079 /* Midgard supports two types of constants, embedded constants (128-bit) and
2080 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2081 * constants can be demoted to inline constants, for space savings and
2082 * sometimes a performance boost */
2085 embedded_to_inline_constant(compiler_context
*ctx
, midgard_block
*block
)
2087 mir_foreach_instr_in_block(block
, ins
) {
2088 if (!ins
->has_constants
) continue;
2089 if (ins
->has_inline_constant
) continue;
2091 /* Blend constants must not be inlined by definition */
2092 if (ins
->has_blend_constant
) continue;
2094 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2095 bool is_16
= ins
->alu
.reg_mode
== midgard_reg_mode_16
;
2096 bool is_32
= ins
->alu
.reg_mode
== midgard_reg_mode_32
;
2098 if (!(is_16
|| is_32
))
2101 /* src1 cannot be an inline constant due to encoding
2102 * restrictions. So, if possible we try to flip the arguments
2105 int op
= ins
->alu
.op
;
2107 if (ins
->src
[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2108 bool flip
= alu_opcode_props
[op
].props
& OP_COMMUTES
;
2111 /* Conditionals can be inverted */
2112 case midgard_alu_op_flt
:
2113 case midgard_alu_op_ilt
:
2114 case midgard_alu_op_fle
:
2115 case midgard_alu_op_ile
:
2116 ins
->alu
.op
= mir_contrapositive(ins
->alu
.op
);
2121 case midgard_alu_op_fcsel
:
2122 case midgard_alu_op_icsel
:
2123 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props
[op
].name
);
2132 if (ins
->src
[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2133 /* Extract the source information */
2135 midgard_vector_alu_src
*src
;
2136 int q
= ins
->alu
.src2
;
2137 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
2140 /* Component is from the swizzle. Take a nonzero component */
2142 unsigned first_comp
= ffs(ins
->mask
) - 1;
2143 unsigned component
= ins
->swizzle
[1][first_comp
];
2145 /* Scale constant appropriately, if we can legally */
2146 uint16_t scaled_constant
= 0;
2148 if (midgard_is_integer_op(op
) || is_16
) {
2149 unsigned int *iconstants
= (unsigned int *) ins
->constants
;
2150 scaled_constant
= (uint16_t) iconstants
[component
];
2152 /* Constant overflow after resize */
2153 if (scaled_constant
!= iconstants
[component
])
2156 float *f
= (float *) ins
->constants
;
2157 float original
= f
[component
];
2158 scaled_constant
= _mesa_float_to_half(original
);
2160 /* Check for loss of precision. If this is
2161 * mediump, we don't care, but for a highp
2162 * shader, we need to pay attention. NIR
2163 * doesn't yet tell us which mode we're in!
2164 * Practically this prevents most constants
2165 * from being inlined, sadly. */
2167 float fp32
= _mesa_half_to_float(scaled_constant
);
2169 if (fp32
!= original
)
2173 /* We don't know how to handle these with a constant */
2175 if (mir_nontrivial_source2_mod_simple(ins
) || src
->rep_low
|| src
->rep_high
) {
2176 DBG("Bailing inline constant...\n");
2180 /* Make sure that the constant is not itself a vector
2181 * by checking if all accessed values are the same. */
2183 uint32_t *cons
= ins
->constants
;
2184 uint32_t value
= cons
[component
];
2186 bool is_vector
= false;
2187 unsigned mask
= effective_writemask(&ins
->alu
, ins
->mask
);
2189 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
2190 /* We only care if this component is actually used */
2191 if (!(mask
& (1 << c
)))
2194 uint32_t test
= cons
[ins
->swizzle
[1][c
]];
2196 if (test
!= value
) {
2205 /* Get rid of the embedded constant */
2206 ins
->has_constants
= false;
2208 ins
->has_inline_constant
= true;
2209 ins
->inline_constant
= scaled_constant
;
2214 /* Dead code elimination for branches at the end of a block - only one branch
2215 * per block is legal semantically */
2218 midgard_opt_cull_dead_branch(compiler_context
*ctx
, midgard_block
*block
)
2220 bool branched
= false;
2222 mir_foreach_instr_in_block_safe(block
, ins
) {
2223 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2226 mir_remove_instruction(ins
);
2232 /* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
2233 * the move can be propagated away entirely */
2236 mir_compose_float_outmod(midgard_outmod_float
*outmod
, midgard_outmod_float comp
)
2239 if (comp
== midgard_outmod_none
)
2242 if (*outmod
== midgard_outmod_none
) {
2247 /* TODO: Compose rules */
2252 midgard_opt_pos_propagate(compiler_context
*ctx
, midgard_block
*block
)
2254 bool progress
= false;
2256 mir_foreach_instr_in_block_safe(block
, ins
) {
2257 if (ins
->type
!= TAG_ALU_4
) continue;
2258 if (ins
->alu
.op
!= midgard_alu_op_fmov
) continue;
2259 if (ins
->alu
.outmod
!= midgard_outmod_pos
) continue;
2261 /* TODO: Registers? */
2262 unsigned src
= ins
->src
[1];
2263 if (src
& IS_REG
) continue;
2265 /* There might be a source modifier, too */
2266 if (mir_nontrivial_source2_mod(ins
)) continue;
2268 /* Backpropagate the modifier */
2269 mir_foreach_instr_in_block_from_rev(block
, v
, mir_prev_op(ins
)) {
2270 if (v
->type
!= TAG_ALU_4
) continue;
2271 if (v
->dest
!= src
) continue;
2273 /* Can we even take a float outmod? */
2274 if (midgard_is_integer_out_op(v
->alu
.op
)) continue;
2276 midgard_outmod_float temp
= v
->alu
.outmod
;
2277 progress
|= mir_compose_float_outmod(&temp
, ins
->alu
.outmod
);
2279 /* Throw in the towel.. */
2280 if (!progress
) break;
2282 /* Otherwise, transfer the modifier */
2283 v
->alu
.outmod
= temp
;
2284 ins
->alu
.outmod
= midgard_outmod_none
;
2294 emit_fragment_epilogue(compiler_context
*ctx
, unsigned rt
)
2296 /* Loop to ourselves */
2298 struct midgard_instruction ins
= v_branch(false, false);
2299 ins
.writeout
= true;
2300 ins
.branch
.target_block
= ctx
->block_count
- 1;
2301 ins
.constants
[0] = rt
* 0x100;
2302 emit_mir_instruction(ctx
, ins
);
2304 ctx
->current_block
->epilogue
= true;
2305 schedule_barrier(ctx
);
2306 return ins
.branch
.target_block
;
2309 static midgard_block
*
2310 emit_block(compiler_context
*ctx
, nir_block
*block
)
2312 midgard_block
*this_block
= ctx
->after_block
;
2313 ctx
->after_block
= NULL
;
2316 this_block
= create_empty_block(ctx
);
2318 list_addtail(&this_block
->link
, &ctx
->blocks
);
2320 this_block
->is_scheduled
= false;
2323 /* Set up current block */
2324 list_inithead(&this_block
->instructions
);
2325 ctx
->current_block
= this_block
;
2327 nir_foreach_instr(instr
, block
) {
2328 emit_instr(ctx
, instr
);
2329 ++ctx
->instruction_count
;
2335 static midgard_block
*emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
);
2338 emit_if(struct compiler_context
*ctx
, nir_if
*nif
)
2340 midgard_block
*before_block
= ctx
->current_block
;
2342 /* Speculatively emit the branch, but we can't fill it in until later */
2343 EMIT(branch
, true, true);
2344 midgard_instruction
*then_branch
= mir_last_in_block(ctx
->current_block
);
2345 then_branch
->src
[0] = nir_src_index(ctx
, &nif
->condition
);
2347 /* Emit the two subblocks. */
2348 midgard_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
2349 midgard_block
*end_then_block
= ctx
->current_block
;
2351 /* Emit a jump from the end of the then block to the end of the else */
2352 EMIT(branch
, false, false);
2353 midgard_instruction
*then_exit
= mir_last_in_block(ctx
->current_block
);
2355 /* Emit second block, and check if it's empty */
2357 int else_idx
= ctx
->block_count
;
2358 int count_in
= ctx
->instruction_count
;
2359 midgard_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
2360 midgard_block
*end_else_block
= ctx
->current_block
;
2361 int after_else_idx
= ctx
->block_count
;
2363 /* Now that we have the subblocks emitted, fix up the branches */
2368 if (ctx
->instruction_count
== count_in
) {
2369 /* The else block is empty, so don't emit an exit jump */
2370 mir_remove_instruction(then_exit
);
2371 then_branch
->branch
.target_block
= after_else_idx
;
2373 then_branch
->branch
.target_block
= else_idx
;
2374 then_exit
->branch
.target_block
= after_else_idx
;
2377 /* Wire up the successors */
2379 ctx
->after_block
= create_empty_block(ctx
);
2381 midgard_block_add_successor(before_block
, then_block
);
2382 midgard_block_add_successor(before_block
, else_block
);
2384 midgard_block_add_successor(end_then_block
, ctx
->after_block
);
2385 midgard_block_add_successor(end_else_block
, ctx
->after_block
);
2389 emit_loop(struct compiler_context
*ctx
, nir_loop
*nloop
)
2391 /* Remember where we are */
2392 midgard_block
*start_block
= ctx
->current_block
;
2394 /* Allocate a loop number, growing the current inner loop depth */
2395 int loop_idx
= ++ctx
->current_loop_depth
;
2397 /* Get index from before the body so we can loop back later */
2398 int start_idx
= ctx
->block_count
;
2400 /* Emit the body itself */
2401 midgard_block
*loop_block
= emit_cf_list(ctx
, &nloop
->body
);
2403 /* Branch back to loop back */
2404 struct midgard_instruction br_back
= v_branch(false, false);
2405 br_back
.branch
.target_block
= start_idx
;
2406 emit_mir_instruction(ctx
, br_back
);
2408 /* Mark down that branch in the graph. */
2409 midgard_block_add_successor(start_block
, loop_block
);
2410 midgard_block_add_successor(ctx
->current_block
, loop_block
);
2412 /* Find the index of the block about to follow us (note: we don't add
2413 * one; blocks are 0-indexed so we get a fencepost problem) */
2414 int break_block_idx
= ctx
->block_count
;
2416 /* Fix up the break statements we emitted to point to the right place,
2417 * now that we can allocate a block number for them */
2418 ctx
->after_block
= create_empty_block(ctx
);
2420 list_for_each_entry_from(struct midgard_block
, block
, start_block
, &ctx
->blocks
, link
) {
2421 mir_foreach_instr_in_block(block
, ins
) {
2422 if (ins
->type
!= TAG_ALU_4
) continue;
2423 if (!ins
->compact_branch
) continue;
2425 /* We found a branch -- check the type to see if we need to do anything */
2426 if (ins
->branch
.target_type
!= TARGET_BREAK
) continue;
2428 /* It's a break! Check if it's our break */
2429 if (ins
->branch
.target_break
!= loop_idx
) continue;
2431 /* Okay, cool, we're breaking out of this loop.
2432 * Rewrite from a break to a goto */
2434 ins
->branch
.target_type
= TARGET_GOTO
;
2435 ins
->branch
.target_block
= break_block_idx
;
2437 midgard_block_add_successor(block
, ctx
->after_block
);
2441 /* Now that we've finished emitting the loop, free up the depth again
2442 * so we play nice with recursion amid nested loops */
2443 --ctx
->current_loop_depth
;
2445 /* Dump loop stats */
2449 static midgard_block
*
2450 emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
)
2452 midgard_block
*start_block
= NULL
;
2454 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2455 switch (node
->type
) {
2456 case nir_cf_node_block
: {
2457 midgard_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
2460 start_block
= block
;
2465 case nir_cf_node_if
:
2466 emit_if(ctx
, nir_cf_node_as_if(node
));
2469 case nir_cf_node_loop
:
2470 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2473 case nir_cf_node_function
:
2482 /* Due to lookahead, we need to report the first tag executed in the command
2483 * stream and in branch targets. An initial block might be empty, so iterate
2484 * until we find one that 'works' */
2487 midgard_get_first_tag_from_block(compiler_context
*ctx
, unsigned block_idx
)
2489 midgard_block
*initial_block
= mir_get_block(ctx
, block_idx
);
2491 unsigned first_tag
= 0;
2493 mir_foreach_block_from(ctx
, initial_block
, v
) {
2494 if (v
->quadword_count
) {
2495 midgard_bundle
*initial_bundle
=
2496 util_dynarray_element(&v
->bundles
, midgard_bundle
, 0);
2498 first_tag
= initial_bundle
->tag
;
2507 pan_format_from_nir_base(nir_alu_type base
)
2511 return MALI_FORMAT_SINT
;
2514 return MALI_FORMAT_UINT
;
2515 case nir_type_float
:
2516 return MALI_CHANNEL_FLOAT
;
2518 unreachable("Invalid base");
2523 pan_format_from_nir_size(nir_alu_type base
, unsigned size
)
2525 if (base
== nir_type_float
) {
2527 case 16: return MALI_FORMAT_SINT
;
2528 case 32: return MALI_FORMAT_UNORM
;
2530 unreachable("Invalid float size for format");
2535 case 8: return MALI_CHANNEL_8
;
2536 case 16: return MALI_CHANNEL_16
;
2537 case 32: return MALI_CHANNEL_32
;
2539 unreachable("Invalid int size for format");
2544 static enum mali_format
2545 pan_format_from_glsl(const struct glsl_type
*type
)
2547 enum glsl_base_type glsl_base
= glsl_get_base_type(glsl_without_array(type
));
2548 nir_alu_type t
= nir_get_nir_type_for_glsl_base_type(glsl_base
);
2550 unsigned base
= nir_alu_type_get_base_type(t
);
2551 unsigned size
= nir_alu_type_get_type_size(t
);
2553 return pan_format_from_nir_base(base
) |
2554 pan_format_from_nir_size(base
, size
) |
2555 MALI_NR_CHANNELS(4);
2558 /* For each fragment writeout instruction, generate a writeout loop to
2559 * associate with it */
2562 mir_add_writeout_loops(compiler_context
*ctx
)
2564 for (unsigned rt
= 0; rt
< ARRAY_SIZE(ctx
->writeout_branch
); ++rt
) {
2565 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2568 unsigned popped
= br
->branch
.target_block
;
2569 midgard_block_add_successor(mir_get_block(ctx
, popped
- 1), ctx
->current_block
);
2570 br
->branch
.target_block
= emit_fragment_epilogue(ctx
, rt
);
2572 /* If we have more RTs, we'll need to restore back after our
2573 * loop terminates */
2575 if ((rt
+ 1) < ARRAY_SIZE(ctx
->writeout_branch
) && ctx
->writeout_branch
[rt
+ 1]) {
2576 midgard_instruction uncond
= v_branch(false, false);
2577 uncond
.branch
.target_block
= popped
;
2578 emit_mir_instruction(ctx
, uncond
);
2579 midgard_block_add_successor(ctx
->current_block
, mir_get_block(ctx
, popped
));
2580 schedule_barrier(ctx
);
2582 /* We're last, so we can terminate here */
2583 br
->last_writeout
= true;
2589 midgard_compile_shader_nir(nir_shader
*nir
, midgard_program
*program
, bool is_blend
, unsigned blend_rt
, unsigned gpu_id
, bool shaderdb
)
2591 struct util_dynarray
*compiled
= &program
->compiled
;
2593 midgard_debug
= debug_get_option_midgard_debug();
2595 /* TODO: Bound against what? */
2596 compiler_context
*ctx
= rzalloc(NULL
, compiler_context
);
2599 ctx
->stage
= nir
->info
.stage
;
2600 ctx
->is_blend
= is_blend
;
2601 ctx
->alpha_ref
= program
->alpha_ref
;
2602 ctx
->blend_rt
= blend_rt
;
2603 ctx
->quirks
= midgard_get_quirks(gpu_id
);
2605 /* Start off with a safe cutoff, allowing usage of all 16 work
2606 * registers. Later, we'll promote uniform reads to uniform registers
2607 * if we determine it is beneficial to do so */
2608 ctx
->uniform_cutoff
= 8;
2610 /* Initialize at a global (not block) level hash tables */
2612 ctx
->ssa_constants
= _mesa_hash_table_u64_create(NULL
);
2613 ctx
->hash_to_temp
= _mesa_hash_table_u64_create(NULL
);
2614 ctx
->sysval_to_id
= _mesa_hash_table_u64_create(NULL
);
2616 /* Record the varying mapping for the command stream's bookkeeping */
2618 struct exec_list
*varyings
=
2619 ctx
->stage
== MESA_SHADER_VERTEX
? &nir
->outputs
: &nir
->inputs
;
2621 unsigned max_varying
= 0;
2622 nir_foreach_variable(var
, varyings
) {
2623 unsigned loc
= var
->data
.driver_location
;
2624 unsigned sz
= glsl_type_size(var
->type
, FALSE
);
2626 for (int c
= 0; c
< sz
; ++c
) {
2627 program
->varyings
[loc
+ c
] = var
->data
.location
+ c
;
2628 program
->varying_type
[loc
+ c
] = pan_format_from_glsl(var
->type
);
2629 max_varying
= MAX2(max_varying
, loc
+ c
);
2633 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2634 * (so we don't accidentally duplicate the epilogue since mesa/st has
2635 * messed with our I/O quite a bit already) */
2637 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2639 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
2640 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
2641 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
2644 NIR_PASS_V(nir
, nir_lower_var_copies
);
2645 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2646 NIR_PASS_V(nir
, nir_split_var_copies
);
2647 NIR_PASS_V(nir
, nir_lower_var_copies
);
2648 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
2649 NIR_PASS_V(nir
, nir_lower_var_copies
);
2650 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2652 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, 0);
2654 /* Optimisation passes */
2656 optimise_nir(nir
, ctx
->quirks
);
2658 if (midgard_debug
& MIDGARD_DBG_SHADERS
) {
2659 nir_print_shader(nir
, stdout
);
2662 /* Assign sysvals and counts, now that we're sure
2663 * (post-optimisation) */
2665 midgard_nir_assign_sysvals(ctx
, nir
);
2667 program
->uniform_count
= nir
->num_uniforms
;
2668 program
->sysval_count
= ctx
->sysval_count
;
2669 memcpy(program
->sysvals
, ctx
->sysvals
, sizeof(ctx
->sysvals
[0]) * ctx
->sysval_count
);
2671 nir_foreach_function(func
, nir
) {
2675 list_inithead(&ctx
->blocks
);
2676 ctx
->block_count
= 0;
2679 emit_cf_list(ctx
, &func
->impl
->body
);
2680 break; /* TODO: Multi-function shaders */
2683 util_dynarray_init(compiled
, NULL
);
2685 /* Per-block lowering before opts */
2687 mir_foreach_block(ctx
, block
) {
2688 inline_alu_constants(ctx
, block
);
2689 midgard_opt_promote_fmov(ctx
, block
);
2690 embedded_to_inline_constant(ctx
, block
);
2692 /* MIR-level optimizations */
2694 bool progress
= false;
2699 mir_foreach_block(ctx
, block
) {
2700 progress
|= midgard_opt_pos_propagate(ctx
, block
);
2701 progress
|= midgard_opt_copy_prop(ctx
, block
);
2702 progress
|= midgard_opt_dead_code_eliminate(ctx
, block
);
2703 progress
|= midgard_opt_combine_projection(ctx
, block
);
2704 progress
|= midgard_opt_varying_projection(ctx
, block
);
2705 progress
|= midgard_opt_not_propagate(ctx
, block
);
2706 progress
|= midgard_opt_fuse_src_invert(ctx
, block
);
2707 progress
|= midgard_opt_fuse_dest_invert(ctx
, block
);
2708 progress
|= midgard_opt_csel_invert(ctx
, block
);
2709 progress
|= midgard_opt_drop_cmp_invert(ctx
, block
);
2710 progress
|= midgard_opt_invert_branch(ctx
, block
);
2714 mir_foreach_block(ctx
, block
) {
2715 midgard_lower_invert(ctx
, block
);
2716 midgard_lower_derivatives(ctx
, block
);
2719 /* Nested control-flow can result in dead branches at the end of the
2720 * block. This messes with our analysis and is just dead code, so cull
2722 mir_foreach_block(ctx
, block
) {
2723 midgard_opt_cull_dead_branch(ctx
, block
);
2726 /* Ensure we were lowered */
2727 mir_foreach_instr_global(ctx
, ins
) {
2728 assert(!ins
->invert
);
2731 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2732 mir_add_writeout_loops(ctx
);
2735 schedule_program(ctx
);
2738 /* Now that all the bundles are scheduled and we can calculate block
2739 * sizes, emit actual branch instructions rather than placeholders */
2741 int br_block_idx
= 0;
2743 mir_foreach_block(ctx
, block
) {
2744 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2745 for (int c
= 0; c
< bundle
->instruction_count
; ++c
) {
2746 midgard_instruction
*ins
= bundle
->instructions
[c
];
2748 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2750 /* Parse some basic branch info */
2751 bool is_compact
= ins
->unit
== ALU_ENAB_BR_COMPACT
;
2752 bool is_conditional
= ins
->branch
.conditional
;
2753 bool is_inverted
= ins
->branch
.invert_conditional
;
2754 bool is_discard
= ins
->branch
.target_type
== TARGET_DISCARD
;
2755 bool is_writeout
= ins
->writeout
;
2757 /* Determine the block we're jumping to */
2758 int target_number
= ins
->branch
.target_block
;
2760 /* Report the destination tag */
2761 int dest_tag
= is_discard
? 0 : midgard_get_first_tag_from_block(ctx
, target_number
);
2763 /* Count up the number of quadwords we're
2764 * jumping over = number of quadwords until
2765 * (br_block_idx, target_number) */
2767 int quadword_offset
= 0;
2771 } else if (target_number
> br_block_idx
) {
2774 for (int idx
= br_block_idx
+ 1; idx
< target_number
; ++idx
) {
2775 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2778 quadword_offset
+= blk
->quadword_count
;
2781 /* Jump backwards */
2783 for (int idx
= br_block_idx
; idx
>= target_number
; --idx
) {
2784 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2787 quadword_offset
-= blk
->quadword_count
;
2791 /* Unconditional extended branches (far jumps)
2792 * have issues, so we always use a conditional
2793 * branch, setting the condition to always for
2794 * unconditional. For compact unconditional
2795 * branches, cond isn't used so it doesn't
2796 * matter what we pick. */
2798 midgard_condition cond
=
2799 !is_conditional
? midgard_condition_always
:
2800 is_inverted
? midgard_condition_false
:
2801 midgard_condition_true
;
2803 midgard_jmp_writeout_op op
=
2804 is_discard
? midgard_jmp_writeout_op_discard
:
2805 is_writeout
? midgard_jmp_writeout_op_writeout
:
2806 (is_compact
&& !is_conditional
) ? midgard_jmp_writeout_op_branch_uncond
:
2807 midgard_jmp_writeout_op_branch_cond
;
2810 midgard_branch_extended branch
=
2811 midgard_create_branch_extended(
2816 memcpy(&ins
->branch_extended
, &branch
, sizeof(branch
));
2817 } else if (is_conditional
|| is_discard
) {
2818 midgard_branch_cond branch
= {
2820 .dest_tag
= dest_tag
,
2821 .offset
= quadword_offset
,
2825 assert(branch
.offset
== quadword_offset
);
2827 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2829 assert(op
== midgard_jmp_writeout_op_branch_uncond
);
2831 midgard_branch_uncond branch
= {
2833 .dest_tag
= dest_tag
,
2834 .offset
= quadword_offset
,
2838 assert(branch
.offset
== quadword_offset
);
2840 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2848 /* Emit flat binary from the instruction arrays. Iterate each block in
2849 * sequence. Save instruction boundaries such that lookahead tags can
2850 * be assigned easily */
2852 /* Cache _all_ bundles in source order for lookahead across failed branches */
2854 int bundle_count
= 0;
2855 mir_foreach_block(ctx
, block
) {
2856 bundle_count
+= block
->bundles
.size
/ sizeof(midgard_bundle
);
2858 midgard_bundle
**source_order_bundles
= malloc(sizeof(midgard_bundle
*) * bundle_count
);
2860 mir_foreach_block(ctx
, block
) {
2861 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2862 source_order_bundles
[bundle_idx
++] = bundle
;
2866 int current_bundle
= 0;
2868 /* Midgard prefetches instruction types, so during emission we
2869 * need to lookahead. Unless this is the last instruction, in
2870 * which we return 1. */
2872 mir_foreach_block(ctx
, block
) {
2873 mir_foreach_bundle_in_block(block
, bundle
) {
2876 if (!bundle
->last_writeout
&& (current_bundle
+ 1 < bundle_count
))
2877 lookahead
= source_order_bundles
[current_bundle
+ 1]->tag
;
2879 emit_binary_bundle(ctx
, bundle
, compiled
, lookahead
);
2883 /* TODO: Free deeper */
2884 //util_dynarray_fini(&block->instructions);
2887 free(source_order_bundles
);
2889 /* Report the very first tag executed */
2890 program
->first_tag
= midgard_get_first_tag_from_block(ctx
, 0);
2892 /* Deal with off-by-one related to the fencepost problem */
2893 program
->work_register_count
= ctx
->work_registers
+ 1;
2894 program
->uniform_cutoff
= ctx
->uniform_cutoff
;
2896 program
->blend_patch_offset
= ctx
->blend_constant_offset
;
2897 program
->tls_size
= ctx
->tls_size
;
2899 if (midgard_debug
& MIDGARD_DBG_SHADERS
)
2900 disassemble_midgard(program
->compiled
.data
, program
->compiled
.size
, gpu_id
, ctx
->stage
);
2902 if (midgard_debug
& MIDGARD_DBG_SHADERDB
|| shaderdb
) {
2903 unsigned nr_bundles
= 0, nr_ins
= 0;
2905 /* Count instructions and bundles */
2907 mir_foreach_block(ctx
, block
) {
2908 nr_bundles
+= util_dynarray_num_elements(
2909 &block
->bundles
, midgard_bundle
);
2911 mir_foreach_bundle_in_block(block
, bun
)
2912 nr_ins
+= bun
->instruction_count
;
2915 /* Calculate thread count. There are certain cutoffs by
2916 * register count for thread count */
2918 unsigned nr_registers
= program
->work_register_count
;
2920 unsigned nr_threads
=
2921 (nr_registers
<= 4) ? 4 :
2922 (nr_registers
<= 8) ? 2 :
2927 fprintf(stderr
, "shader%d - %s shader: "
2928 "%u inst, %u bundles, %u quadwords, "
2929 "%u registers, %u threads, %u loops, "
2930 "%u:%u spills:fills\n",
2932 gl_shader_stage_name(ctx
->stage
),
2933 nr_ins
, nr_bundles
, ctx
->quadword_count
,
2934 nr_registers
, nr_threads
,
2936 ctx
->spills
, ctx
->fills
);