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 midgard_instruction
*
1126 compiler_context
*ctx
,
1130 nir_src
*indirect_offset
,
1133 /* TODO: half-floats */
1135 midgard_instruction ins
= m_ld_ubo_int4(dest
, 0);
1136 ins
.constants
[0] = offset
;
1138 if (instr
->type
== nir_instr_type_intrinsic
)
1139 mir_set_intr_mask(instr
, &ins
, true);
1141 if (indirect_offset
) {
1142 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1143 ins
.load_store
.arg_2
= 0x80;
1145 ins
.load_store
.arg_2
= 0x1E;
1148 ins
.load_store
.arg_1
= index
;
1150 return emit_mir_instruction(ctx
, ins
);
1153 /* SSBO reads are like UBO reads if you squint */
1157 compiler_context
*ctx
,
1162 nir_src
*indirect_offset
,
1167 midgard_instruction ins
;
1170 ins
= m_ld_int4(srcdest
, offset
);
1172 ins
= m_st_int4(srcdest
, offset
);
1174 /* SSBO reads use a generic memory read interface, so we need the
1175 * address of the SSBO as the first argument. This is a sysval. */
1177 unsigned addr
= make_compiler_temp(ctx
);
1178 emit_sysval_read(ctx
, instr
, addr
, 2);
1180 /* The source array:
1182 * src[0] = store ? value : unused
1186 * We would like arg_1 = the address and
1187 * arg_2 = the offset.
1192 /* TODO: What is this? It looks superficially like a shift << 5, but
1193 * arg_1 doesn't take a shift Should it be E0 or A0? We also need the
1194 * indirect offset. */
1196 if (indirect_offset
) {
1197 ins
.load_store
.arg_1
|= 0xE0;
1198 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1200 ins
.load_store
.arg_2
= 0x7E;
1203 /* TODO: Bounds check */
1205 /* Finally, we emit the direct offset */
1207 ins
.load_store
.varying_parameters
= (offset
& 0x1FF) << 1;
1208 ins
.load_store
.address
= (offset
>> 9);
1209 mir_set_intr_mask(instr
, &ins
, is_read
);
1211 emit_mir_instruction(ctx
, ins
);
1216 compiler_context
*ctx
,
1217 unsigned dest
, unsigned offset
,
1218 unsigned nr_comp
, unsigned component
,
1219 nir_src
*indirect_offset
, nir_alu_type type
, bool flat
)
1221 /* XXX: Half-floats? */
1222 /* TODO: swizzle, mask */
1224 midgard_instruction ins
= m_ld_vary_32(dest
, offset
);
1225 ins
.mask
= mask_of(nr_comp
);
1227 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
.swizzle
[0]); ++i
)
1228 ins
.swizzle
[0][i
] = MIN2(i
+ component
, COMPONENT_W
);
1230 midgard_varying_parameter p
= {
1232 .interpolation
= midgard_interp_default
,
1237 memcpy(&u
, &p
, sizeof(p
));
1238 ins
.load_store
.varying_parameters
= u
;
1240 if (indirect_offset
)
1241 ins
.src
[2] = nir_src_index(ctx
, indirect_offset
);
1243 ins
.load_store
.arg_2
= 0x1E;
1245 ins
.load_store
.arg_1
= 0x9E;
1247 /* Use the type appropriate load */
1251 ins
.load_store
.op
= midgard_op_ld_vary_32u
;
1254 ins
.load_store
.op
= midgard_op_ld_vary_32i
;
1256 case nir_type_float
:
1257 ins
.load_store
.op
= midgard_op_ld_vary_32
;
1260 unreachable("Attempted to load unknown type");
1264 emit_mir_instruction(ctx
, ins
);
1269 compiler_context
*ctx
,
1270 unsigned dest
, unsigned offset
,
1271 unsigned nr_comp
, nir_alu_type t
)
1273 midgard_instruction ins
= m_ld_attr_32(dest
, offset
);
1274 ins
.load_store
.arg_1
= 0x1E;
1275 ins
.load_store
.arg_2
= 0x1E;
1276 ins
.mask
= mask_of(nr_comp
);
1278 /* Use the type appropriate load */
1282 ins
.load_store
.op
= midgard_op_ld_attr_32u
;
1285 ins
.load_store
.op
= midgard_op_ld_attr_32i
;
1287 case nir_type_float
:
1288 ins
.load_store
.op
= midgard_op_ld_attr_32
;
1291 unreachable("Attempted to load unknown type");
1295 emit_mir_instruction(ctx
, ins
);
1299 emit_sysval_read(compiler_context
*ctx
, nir_instr
*instr
, signed dest_override
,
1300 unsigned nr_components
)
1304 /* Figure out which uniform this is */
1305 int sysval
= sysval_for_instr(ctx
, instr
, &dest
);
1306 void *val
= _mesa_hash_table_u64_search(ctx
->sysval_to_id
, sysval
);
1308 if (dest_override
>= 0)
1309 dest
= dest_override
;
1311 /* Sysvals are prefix uniforms */
1312 unsigned uniform
= ((uintptr_t) val
) - 1;
1314 /* Emit the read itself -- this is never indirect */
1315 midgard_instruction
*ins
=
1316 emit_ubo_read(ctx
, instr
, dest
, uniform
* 16, NULL
, 0);
1318 ins
->mask
= mask_of(nr_components
);
1322 compute_builtin_arg(nir_op op
)
1325 case nir_intrinsic_load_work_group_id
:
1327 case nir_intrinsic_load_local_invocation_id
:
1330 unreachable("Invalid compute paramater loaded");
1335 emit_fragment_store(compiler_context
*ctx
, unsigned src
, unsigned rt
)
1337 emit_explicit_constant(ctx
, src
, src
);
1339 struct midgard_instruction ins
=
1340 v_branch(false, false);
1342 ins
.writeout
= true;
1344 /* Add dependencies */
1346 ins
.constants
[0] = rt
* 0x100;
1348 /* Emit the branch */
1349 midgard_instruction
*br
= emit_mir_instruction(ctx
, ins
);
1350 schedule_barrier(ctx
);
1352 assert(rt
< ARRAY_SIZE(ctx
->writeout_branch
));
1353 assert(!ctx
->writeout_branch
[rt
]);
1354 ctx
->writeout_branch
[rt
] = br
;
1356 /* Push our current location = current block count - 1 = where we'll
1357 * jump to. Maybe a bit too clever for my own good */
1359 br
->branch
.target_block
= ctx
->block_count
- 1;
1363 emit_compute_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1365 unsigned reg
= nir_dest_index(ctx
, &instr
->dest
);
1366 midgard_instruction ins
= m_ld_compute_id(reg
, 0);
1367 ins
.mask
= mask_of(3);
1368 ins
.load_store
.arg_1
= compute_builtin_arg(instr
->intrinsic
);
1369 emit_mir_instruction(ctx
, ins
);
1373 vertex_builtin_arg(nir_op op
)
1376 case nir_intrinsic_load_vertex_id
:
1377 return PAN_VERTEX_ID
;
1378 case nir_intrinsic_load_instance_id
:
1379 return PAN_INSTANCE_ID
;
1381 unreachable("Invalid vertex builtin");
1386 emit_vertex_builtin(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1388 unsigned reg
= nir_dest_index(ctx
, &instr
->dest
);
1389 emit_attr_read(ctx
, reg
, vertex_builtin_arg(instr
->intrinsic
), 1, nir_type_int
);
1393 emit_intrinsic(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1395 unsigned offset
= 0, reg
;
1397 switch (instr
->intrinsic
) {
1398 case nir_intrinsic_discard_if
:
1399 case nir_intrinsic_discard
: {
1400 bool conditional
= instr
->intrinsic
== nir_intrinsic_discard_if
;
1401 struct midgard_instruction discard
= v_branch(conditional
, false);
1402 discard
.branch
.target_type
= TARGET_DISCARD
;
1405 discard
.src
[0] = nir_src_index(ctx
, &instr
->src
[0]);
1407 emit_mir_instruction(ctx
, discard
);
1408 schedule_barrier(ctx
);
1413 case nir_intrinsic_load_uniform
:
1414 case nir_intrinsic_load_ubo
:
1415 case nir_intrinsic_load_ssbo
:
1416 case nir_intrinsic_load_input
:
1417 case nir_intrinsic_load_interpolated_input
: {
1418 bool is_uniform
= instr
->intrinsic
== nir_intrinsic_load_uniform
;
1419 bool is_ubo
= instr
->intrinsic
== nir_intrinsic_load_ubo
;
1420 bool is_ssbo
= instr
->intrinsic
== nir_intrinsic_load_ssbo
;
1421 bool is_flat
= instr
->intrinsic
== nir_intrinsic_load_input
;
1422 bool is_interp
= instr
->intrinsic
== nir_intrinsic_load_interpolated_input
;
1424 /* Get the base type of the intrinsic */
1425 /* TODO: Infer type? Does it matter? */
1427 (is_ubo
|| is_ssbo
) ? nir_type_uint
:
1428 (is_interp
) ? nir_type_float
:
1429 nir_intrinsic_type(instr
);
1431 t
= nir_alu_type_get_base_type(t
);
1433 if (!(is_ubo
|| is_ssbo
)) {
1434 offset
= nir_intrinsic_base(instr
);
1437 unsigned nr_comp
= nir_intrinsic_dest_components(instr
);
1439 nir_src
*src_offset
= nir_get_io_offset_src(instr
);
1441 bool direct
= nir_src_is_const(*src_offset
);
1442 nir_src
*indirect_offset
= direct
? NULL
: src_offset
;
1445 offset
+= nir_src_as_uint(*src_offset
);
1447 /* We may need to apply a fractional offset */
1448 int component
= (is_flat
|| is_interp
) ?
1449 nir_intrinsic_component(instr
) : 0;
1450 reg
= nir_dest_index(ctx
, &instr
->dest
);
1452 if (is_uniform
&& !ctx
->is_blend
) {
1453 emit_ubo_read(ctx
, &instr
->instr
, reg
, (ctx
->sysval_count
+ offset
) * 16, indirect_offset
, 0);
1454 } else if (is_ubo
) {
1455 nir_src index
= instr
->src
[0];
1457 /* We don't yet support indirect UBOs. For indirect
1458 * block numbers (if that's possible), we don't know
1459 * enough about the hardware yet. For indirect sources,
1460 * we know what we need but we need to add some NIR
1461 * support for lowering correctly with respect to
1464 assert(nir_src_is_const(index
));
1465 assert(nir_src_is_const(*src_offset
));
1467 uint32_t uindex
= nir_src_as_uint(index
) + 1;
1468 emit_ubo_read(ctx
, &instr
->instr
, reg
, offset
, NULL
, uindex
);
1469 } else if (is_ssbo
) {
1470 nir_src index
= instr
->src
[0];
1471 assert(nir_src_is_const(index
));
1472 uint32_t uindex
= nir_src_as_uint(index
);
1474 emit_ssbo_access(ctx
, &instr
->instr
, true, reg
, offset
, indirect_offset
, uindex
);
1475 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& !ctx
->is_blend
) {
1476 emit_varying_read(ctx
, reg
, offset
, nr_comp
, component
, indirect_offset
, t
, is_flat
);
1477 } else if (ctx
->is_blend
) {
1478 /* For blend shaders, load the input color, which is
1479 * preloaded to r0 */
1481 midgard_instruction move
= v_mov(SSA_FIXED_REGISTER(0), reg
);
1482 emit_mir_instruction(ctx
, move
);
1483 schedule_barrier(ctx
);
1484 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1485 emit_attr_read(ctx
, reg
, offset
, nr_comp
, t
);
1487 DBG("Unknown load\n");
1494 /* Artefact of load_interpolated_input. TODO: other barycentric modes */
1495 case nir_intrinsic_load_barycentric_pixel
:
1498 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1500 case nir_intrinsic_load_raw_output_pan
:
1501 case nir_intrinsic_load_output_u8_as_fp16_pan
:
1502 reg
= nir_dest_index(ctx
, &instr
->dest
);
1503 assert(ctx
->is_blend
);
1505 /* T720 and below use different blend opcodes with slightly
1506 * different semantics than T760 and up */
1508 midgard_instruction ld
= m_ld_color_buffer_32u(reg
, 0);
1509 bool old_blend
= ctx
->quirks
& MIDGARD_OLD_BLEND
;
1511 if (instr
->intrinsic
== nir_intrinsic_load_output_u8_as_fp16_pan
) {
1512 ld
.load_store
.op
= old_blend
?
1513 midgard_op_ld_color_buffer_u8_as_fp16_old
:
1514 midgard_op_ld_color_buffer_u8_as_fp16
;
1517 ld
.load_store
.address
= 1;
1518 ld
.load_store
.arg_2
= 0x1E;
1521 for (unsigned c
= 2; c
< 16; ++c
)
1522 ld
.swizzle
[0][c
] = 0;
1525 emit_mir_instruction(ctx
, ld
);
1528 case nir_intrinsic_load_blend_const_color_rgba
: {
1529 assert(ctx
->is_blend
);
1530 reg
= nir_dest_index(ctx
, &instr
->dest
);
1532 /* Blend constants are embedded directly in the shader and
1533 * patched in, so we use some magic routing */
1535 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), reg
);
1536 ins
.has_constants
= true;
1537 ins
.has_blend_constant
= true;
1538 emit_mir_instruction(ctx
, ins
);
1542 case nir_intrinsic_store_output
:
1543 assert(nir_src_is_const(instr
->src
[1]) && "no indirect outputs");
1545 offset
= nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[1]);
1547 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1549 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
1550 emit_fragment_store(ctx
, reg
, offset
);
1551 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1552 /* We should have been vectorized, though we don't
1553 * currently check that st_vary is emitted only once
1554 * per slot (this is relevant, since there's not a mask
1555 * parameter available on the store [set to 0 by the
1556 * blob]). We do respect the component by adjusting the
1557 * swizzle. If this is a constant source, we'll need to
1558 * emit that explicitly. */
1560 emit_explicit_constant(ctx
, reg
, reg
);
1562 unsigned component
= nir_intrinsic_component(instr
);
1563 unsigned nr_comp
= nir_src_num_components(instr
->src
[0]);
1565 midgard_instruction st
= m_st_vary_32(reg
, offset
);
1566 st
.load_store
.arg_1
= 0x9E;
1567 st
.load_store
.arg_2
= 0x1E;
1569 switch (nir_alu_type_get_base_type(nir_intrinsic_type(instr
))) {
1572 st
.load_store
.op
= midgard_op_st_vary_32u
;
1575 st
.load_store
.op
= midgard_op_st_vary_32i
;
1577 case nir_type_float
:
1578 st
.load_store
.op
= midgard_op_st_vary_32
;
1581 unreachable("Attempted to store unknown type");
1585 for (unsigned i
= 0; i
< ARRAY_SIZE(st
.swizzle
[0]); ++i
)
1586 st
.swizzle
[0][i
] = MIN2(i
+ component
, nr_comp
);
1588 emit_mir_instruction(ctx
, st
);
1590 DBG("Unknown store\n");
1596 /* Special case of store_output for lowered blend shaders */
1597 case nir_intrinsic_store_raw_output_pan
:
1598 assert (ctx
->stage
== MESA_SHADER_FRAGMENT
);
1599 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1601 if (ctx
->quirks
& MIDGARD_OLD_BLEND
) {
1602 /* Suppose reg = qr0.xyzw. That means 4 8-bit ---> 1 32-bit. So
1603 * reg = r0.x. We want to splatter. So we can do a 32-bit move
1606 * imov r0.xyzw, r0.xxxx
1609 unsigned expanded
= make_compiler_temp(ctx
);
1611 midgard_instruction splatter
= v_mov(reg
, expanded
);
1613 for (unsigned c
= 0; c
< 16; ++c
)
1614 splatter
.swizzle
[1][c
] = 0;
1616 emit_mir_instruction(ctx
, splatter
);
1617 emit_fragment_store(ctx
, expanded
, ctx
->blend_rt
);
1619 emit_fragment_store(ctx
, reg
, ctx
->blend_rt
);
1623 case nir_intrinsic_store_ssbo
:
1624 assert(nir_src_is_const(instr
->src
[1]));
1626 bool direct_offset
= nir_src_is_const(instr
->src
[2]);
1627 offset
= direct_offset
? nir_src_as_uint(instr
->src
[2]) : 0;
1628 nir_src
*indirect_offset
= direct_offset
? NULL
: &instr
->src
[2];
1629 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1631 uint32_t uindex
= nir_src_as_uint(instr
->src
[1]);
1633 emit_explicit_constant(ctx
, reg
, reg
);
1634 emit_ssbo_access(ctx
, &instr
->instr
, false, reg
, offset
, indirect_offset
, uindex
);
1637 case nir_intrinsic_load_viewport_scale
:
1638 case nir_intrinsic_load_viewport_offset
:
1639 case nir_intrinsic_load_num_work_groups
:
1640 case nir_intrinsic_load_sampler_lod_parameters_pan
:
1641 emit_sysval_read(ctx
, &instr
->instr
, ~0, 3);
1644 case nir_intrinsic_load_work_group_id
:
1645 case nir_intrinsic_load_local_invocation_id
:
1646 emit_compute_builtin(ctx
, instr
);
1649 case nir_intrinsic_load_vertex_id
:
1650 case nir_intrinsic_load_instance_id
:
1651 emit_vertex_builtin(ctx
, instr
);
1655 printf ("Unhandled intrinsic\n");
1662 midgard_tex_format(enum glsl_sampler_dim dim
)
1665 case GLSL_SAMPLER_DIM_1D
:
1666 case GLSL_SAMPLER_DIM_BUF
:
1669 case GLSL_SAMPLER_DIM_2D
:
1670 case GLSL_SAMPLER_DIM_EXTERNAL
:
1671 case GLSL_SAMPLER_DIM_RECT
:
1674 case GLSL_SAMPLER_DIM_3D
:
1677 case GLSL_SAMPLER_DIM_CUBE
:
1678 return MALI_TEX_CUBE
;
1681 DBG("Unknown sampler dim type\n");
1687 /* Tries to attach an explicit LOD / bias as a constant. Returns whether this
1691 pan_attach_constant_bias(
1692 compiler_context
*ctx
,
1694 midgard_texture_word
*word
)
1696 /* To attach as constant, it has to *be* constant */
1698 if (!nir_src_is_const(lod
))
1701 float f
= nir_src_as_float(lod
);
1703 /* Break into fixed-point */
1705 float lod_frac
= f
- lod_int
;
1707 /* Carry over negative fractions */
1708 if (lod_frac
< 0.0) {
1714 word
->bias
= float_to_ubyte(lod_frac
);
1715 word
->bias_int
= lod_int
;
1720 static enum mali_sampler_type
1721 midgard_sampler_type(nir_alu_type t
) {
1722 switch (nir_alu_type_get_base_type(t
))
1724 case nir_type_float
:
1725 return MALI_SAMPLER_FLOAT
;
1727 return MALI_SAMPLER_SIGNED
;
1729 return MALI_SAMPLER_UNSIGNED
;
1731 unreachable("Unknown sampler type");
1736 emit_texop_native(compiler_context
*ctx
, nir_tex_instr
*instr
,
1737 unsigned midgard_texop
)
1740 //assert (!instr->sampler);
1741 //assert (!instr->texture_array_size);
1743 int texture_index
= instr
->texture_index
;
1744 int sampler_index
= texture_index
;
1746 /* No helper to build texture words -- we do it all here */
1747 midgard_instruction ins
= {
1748 .type
= TAG_TEXTURE_4
,
1750 .dest
= nir_dest_index(ctx
, &instr
->dest
),
1751 .src
= { ~0, ~0, ~0, ~0 },
1752 .swizzle
= SWIZZLE_IDENTITY_4
,
1754 .op
= midgard_texop
,
1755 .format
= midgard_tex_format(instr
->sampler_dim
),
1756 .texture_handle
= texture_index
,
1757 .sampler_handle
= sampler_index
,
1763 .sampler_type
= midgard_sampler_type(instr
->dest_type
),
1764 .shadow
= instr
->is_shadow
,
1768 /* We may need a temporary for the coordinate */
1770 bool needs_temp_coord
=
1771 (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) ||
1772 (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) ||
1775 unsigned coords
= needs_temp_coord
? make_compiler_temp_reg(ctx
) : 0;
1777 for (unsigned i
= 0; i
< instr
->num_srcs
; ++i
) {
1778 int index
= nir_src_index(ctx
, &instr
->src
[i
].src
);
1779 unsigned nr_components
= nir_src_num_components(instr
->src
[i
].src
);
1781 switch (instr
->src
[i
].src_type
) {
1782 case nir_tex_src_coord
: {
1783 emit_explicit_constant(ctx
, index
, index
);
1785 unsigned coord_mask
= mask_of(instr
->coord_components
);
1787 bool flip_zw
= (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) && (coord_mask
& (1 << COMPONENT_Z
));
1790 coord_mask
^= ((1 << COMPONENT_Z
) | (1 << COMPONENT_W
));
1792 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1793 /* texelFetch is undefined on samplerCube */
1794 assert(midgard_texop
!= TEXTURE_OP_TEXEL_FETCH
);
1796 /* For cubemaps, we use a special ld/st op to
1797 * select the face and copy the xy into the
1798 * texture register */
1800 midgard_instruction ld
= m_ld_cubemap_coords(coords
, 0);
1802 ld
.mask
= 0x3; /* xy */
1803 ld
.load_store
.arg_1
= 0x20;
1804 ld
.swizzle
[1][3] = COMPONENT_X
;
1805 emit_mir_instruction(ctx
, ld
);
1808 ins
.swizzle
[1][2] = instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
1809 ins
.swizzle
[1][3] = COMPONENT_X
;
1810 } else if (needs_temp_coord
) {
1811 /* mov coord_temp, coords */
1812 midgard_instruction mov
= v_mov(index
, coords
);
1813 mov
.mask
= coord_mask
;
1816 mov
.swizzle
[1][COMPONENT_W
] = COMPONENT_Z
;
1818 emit_mir_instruction(ctx
, mov
);
1823 ins
.src
[1] = coords
;
1825 /* Texelfetch coordinates uses all four elements
1826 * (xyz/index) regardless of texture dimensionality,
1827 * which means it's necessary to zero the unused
1828 * components to keep everything happy */
1830 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
1831 /* mov index.zw, #0, or generalized */
1832 midgard_instruction mov
=
1833 v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), coords
);
1834 mov
.has_constants
= true;
1835 mov
.mask
= coord_mask
^ 0xF;
1836 emit_mir_instruction(ctx
, mov
);
1839 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_2D
) {
1840 /* Array component in w but NIR wants it in z,
1841 * but if we have a temp coord we already fixed
1844 if (nr_components
== 3) {
1845 ins
.swizzle
[1][2] = COMPONENT_Z
;
1846 ins
.swizzle
[1][3] = needs_temp_coord
? COMPONENT_W
: COMPONENT_Z
;
1847 } else if (nr_components
== 2) {
1849 instr
->is_shadow
? COMPONENT_Z
: COMPONENT_X
;
1850 ins
.swizzle
[1][3] = COMPONENT_X
;
1852 unreachable("Invalid texture 2D components");
1855 if (midgard_texop
== TEXTURE_OP_TEXEL_FETCH
) {
1857 ins
.swizzle
[1][2] = COMPONENT_Z
;
1858 ins
.swizzle
[1][3] = COMPONENT_W
;
1864 case nir_tex_src_bias
:
1865 case nir_tex_src_lod
: {
1866 /* Try as a constant if we can */
1868 bool is_txf
= midgard_texop
== TEXTURE_OP_TEXEL_FETCH
;
1869 if (!is_txf
&& pan_attach_constant_bias(ctx
, instr
->src
[i
].src
, &ins
.texture
))
1872 ins
.texture
.lod_register
= true;
1875 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
1876 ins
.swizzle
[2][c
] = COMPONENT_X
;
1878 emit_explicit_constant(ctx
, index
, index
);
1883 case nir_tex_src_offset
: {
1884 ins
.texture
.offset_register
= true;
1887 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
)
1888 ins
.swizzle
[3][c
] = (c
> COMPONENT_Z
) ? 0 : c
;
1890 emit_explicit_constant(ctx
, index
, index
);
1894 case nir_tex_src_comparator
: {
1895 unsigned comp
= COMPONENT_Z
;
1897 /* mov coord_temp.foo, coords */
1898 midgard_instruction mov
= v_mov(index
, coords
);
1899 mov
.mask
= 1 << comp
;
1901 for (unsigned i
= 0; i
< MIR_VEC_COMPONENTS
; ++i
)
1902 mov
.swizzle
[1][i
] = COMPONENT_X
;
1904 emit_mir_instruction(ctx
, mov
);
1909 unreachable("Unknown texture source type\n");
1913 emit_mir_instruction(ctx
, ins
);
1915 /* Used for .cont and .last hinting */
1916 ctx
->texture_op_count
++;
1920 emit_tex(compiler_context
*ctx
, nir_tex_instr
*instr
)
1922 switch (instr
->op
) {
1925 emit_texop_native(ctx
, instr
, TEXTURE_OP_NORMAL
);
1928 emit_texop_native(ctx
, instr
, TEXTURE_OP_LOD
);
1931 emit_texop_native(ctx
, instr
, TEXTURE_OP_TEXEL_FETCH
);
1934 emit_sysval_read(ctx
, &instr
->instr
, ~0, 4);
1937 unreachable("Unhanlded texture op");
1942 emit_jump(compiler_context
*ctx
, nir_jump_instr
*instr
)
1944 switch (instr
->type
) {
1945 case nir_jump_break
: {
1946 /* Emit a branch out of the loop */
1947 struct midgard_instruction br
= v_branch(false, false);
1948 br
.branch
.target_type
= TARGET_BREAK
;
1949 br
.branch
.target_break
= ctx
->current_loop_depth
;
1950 emit_mir_instruction(ctx
, br
);
1955 DBG("Unknown jump type %d\n", instr
->type
);
1961 emit_instr(compiler_context
*ctx
, struct nir_instr
*instr
)
1963 switch (instr
->type
) {
1964 case nir_instr_type_load_const
:
1965 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1968 case nir_instr_type_intrinsic
:
1969 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1972 case nir_instr_type_alu
:
1973 emit_alu(ctx
, nir_instr_as_alu(instr
));
1976 case nir_instr_type_tex
:
1977 emit_tex(ctx
, nir_instr_as_tex(instr
));
1980 case nir_instr_type_jump
:
1981 emit_jump(ctx
, nir_instr_as_jump(instr
));
1984 case nir_instr_type_ssa_undef
:
1989 DBG("Unhandled instruction type\n");
1995 /* ALU instructions can inline or embed constants, which decreases register
1996 * pressure and saves space. */
1998 #define CONDITIONAL_ATTACH(idx) { \
1999 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
2002 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
2003 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
2008 inline_alu_constants(compiler_context
*ctx
, midgard_block
*block
)
2010 mir_foreach_instr_in_block(block
, alu
) {
2011 /* Other instructions cannot inline constants */
2012 if (alu
->type
!= TAG_ALU_4
) continue;
2013 if (alu
->compact_branch
) continue;
2015 /* If there is already a constant here, we can do nothing */
2016 if (alu
->has_constants
) continue;
2018 CONDITIONAL_ATTACH(0);
2020 if (!alu
->has_constants
) {
2021 CONDITIONAL_ATTACH(1)
2022 } else if (!alu
->inline_constant
) {
2023 /* Corner case: _two_ vec4 constants, for instance with a
2024 * csel. For this case, we can only use a constant
2025 * register for one, we'll have to emit a move for the
2026 * other. Note, if both arguments are constants, then
2027 * necessarily neither argument depends on the value of
2028 * any particular register. As the destination register
2029 * will be wiped, that means we can spill the constant
2030 * to the destination register.
2033 void *entry
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, alu
->src
[1] + 1);
2034 unsigned scratch
= alu
->dest
;
2037 midgard_instruction ins
= v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), scratch
);
2038 attach_constants(ctx
, &ins
, entry
, alu
->src
[1] + 1);
2040 /* Set the source */
2041 alu
->src
[1] = scratch
;
2043 /* Inject us -before- the last instruction which set r31 */
2044 mir_insert_instruction_before(ctx
, mir_prev_op(alu
), ins
);
2050 /* Being a little silly with the names, but returns the op that is the bitwise
2051 * inverse of the op with the argument switched. I.e. (f and g are
2054 * f(a, b) = ~g(b, a)
2056 * Corollary: if g is the contrapositve of f, f is the contrapositive of g:
2058 * f(a, b) = ~g(b, a)
2059 * ~f(a, b) = g(b, a)
2060 * ~f(a, b) = ~h(a, b) where h is the contrapositive of g
2063 * Thus we define this function in pairs.
2066 static inline midgard_alu_op
2067 mir_contrapositive(midgard_alu_op op
)
2070 case midgard_alu_op_flt
:
2071 return midgard_alu_op_fle
;
2072 case midgard_alu_op_fle
:
2073 return midgard_alu_op_flt
;
2075 case midgard_alu_op_ilt
:
2076 return midgard_alu_op_ile
;
2077 case midgard_alu_op_ile
:
2078 return midgard_alu_op_ilt
;
2081 unreachable("No known contrapositive");
2085 /* Midgard supports two types of constants, embedded constants (128-bit) and
2086 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2087 * constants can be demoted to inline constants, for space savings and
2088 * sometimes a performance boost */
2091 embedded_to_inline_constant(compiler_context
*ctx
, midgard_block
*block
)
2093 mir_foreach_instr_in_block(block
, ins
) {
2094 if (!ins
->has_constants
) continue;
2095 if (ins
->has_inline_constant
) continue;
2097 /* Blend constants must not be inlined by definition */
2098 if (ins
->has_blend_constant
) continue;
2100 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2101 bool is_16
= ins
->alu
.reg_mode
== midgard_reg_mode_16
;
2102 bool is_32
= ins
->alu
.reg_mode
== midgard_reg_mode_32
;
2104 if (!(is_16
|| is_32
))
2107 /* src1 cannot be an inline constant due to encoding
2108 * restrictions. So, if possible we try to flip the arguments
2111 int op
= ins
->alu
.op
;
2113 if (ins
->src
[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2114 bool flip
= alu_opcode_props
[op
].props
& OP_COMMUTES
;
2117 /* Conditionals can be inverted */
2118 case midgard_alu_op_flt
:
2119 case midgard_alu_op_ilt
:
2120 case midgard_alu_op_fle
:
2121 case midgard_alu_op_ile
:
2122 ins
->alu
.op
= mir_contrapositive(ins
->alu
.op
);
2127 case midgard_alu_op_fcsel
:
2128 case midgard_alu_op_icsel
:
2129 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props
[op
].name
);
2138 if (ins
->src
[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
2139 /* Extract the source information */
2141 midgard_vector_alu_src
*src
;
2142 int q
= ins
->alu
.src2
;
2143 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
2146 /* Component is from the swizzle. Take a nonzero component */
2148 unsigned first_comp
= ffs(ins
->mask
) - 1;
2149 unsigned component
= ins
->swizzle
[1][first_comp
];
2151 /* Scale constant appropriately, if we can legally */
2152 uint16_t scaled_constant
= 0;
2154 if (midgard_is_integer_op(op
) || is_16
) {
2155 unsigned int *iconstants
= (unsigned int *) ins
->constants
;
2156 scaled_constant
= (uint16_t) iconstants
[component
];
2158 /* Constant overflow after resize */
2159 if (scaled_constant
!= iconstants
[component
])
2162 float *f
= (float *) ins
->constants
;
2163 float original
= f
[component
];
2164 scaled_constant
= _mesa_float_to_half(original
);
2166 /* Check for loss of precision. If this is
2167 * mediump, we don't care, but for a highp
2168 * shader, we need to pay attention. NIR
2169 * doesn't yet tell us which mode we're in!
2170 * Practically this prevents most constants
2171 * from being inlined, sadly. */
2173 float fp32
= _mesa_half_to_float(scaled_constant
);
2175 if (fp32
!= original
)
2179 /* We don't know how to handle these with a constant */
2181 if (mir_nontrivial_source2_mod_simple(ins
) || src
->rep_low
|| src
->rep_high
) {
2182 DBG("Bailing inline constant...\n");
2186 /* Make sure that the constant is not itself a vector
2187 * by checking if all accessed values are the same. */
2189 uint32_t *cons
= ins
->constants
;
2190 uint32_t value
= cons
[component
];
2192 bool is_vector
= false;
2193 unsigned mask
= effective_writemask(&ins
->alu
, ins
->mask
);
2195 for (unsigned c
= 0; c
< MIR_VEC_COMPONENTS
; ++c
) {
2196 /* We only care if this component is actually used */
2197 if (!(mask
& (1 << c
)))
2200 uint32_t test
= cons
[ins
->swizzle
[1][c
]];
2202 if (test
!= value
) {
2211 /* Get rid of the embedded constant */
2212 ins
->has_constants
= false;
2214 ins
->has_inline_constant
= true;
2215 ins
->inline_constant
= scaled_constant
;
2220 /* Dead code elimination for branches at the end of a block - only one branch
2221 * per block is legal semantically */
2224 midgard_opt_cull_dead_branch(compiler_context
*ctx
, midgard_block
*block
)
2226 bool branched
= false;
2228 mir_foreach_instr_in_block_safe(block
, ins
) {
2229 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2232 mir_remove_instruction(ins
);
2238 /* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
2239 * the move can be propagated away entirely */
2242 mir_compose_float_outmod(midgard_outmod_float
*outmod
, midgard_outmod_float comp
)
2245 if (comp
== midgard_outmod_none
)
2248 if (*outmod
== midgard_outmod_none
) {
2253 /* TODO: Compose rules */
2258 midgard_opt_pos_propagate(compiler_context
*ctx
, midgard_block
*block
)
2260 bool progress
= false;
2262 mir_foreach_instr_in_block_safe(block
, ins
) {
2263 if (ins
->type
!= TAG_ALU_4
) continue;
2264 if (ins
->alu
.op
!= midgard_alu_op_fmov
) continue;
2265 if (ins
->alu
.outmod
!= midgard_outmod_pos
) continue;
2267 /* TODO: Registers? */
2268 unsigned src
= ins
->src
[1];
2269 if (src
& IS_REG
) continue;
2271 /* There might be a source modifier, too */
2272 if (mir_nontrivial_source2_mod(ins
)) continue;
2274 /* Backpropagate the modifier */
2275 mir_foreach_instr_in_block_from_rev(block
, v
, mir_prev_op(ins
)) {
2276 if (v
->type
!= TAG_ALU_4
) continue;
2277 if (v
->dest
!= src
) continue;
2279 /* Can we even take a float outmod? */
2280 if (midgard_is_integer_out_op(v
->alu
.op
)) continue;
2282 midgard_outmod_float temp
= v
->alu
.outmod
;
2283 progress
|= mir_compose_float_outmod(&temp
, ins
->alu
.outmod
);
2285 /* Throw in the towel.. */
2286 if (!progress
) break;
2288 /* Otherwise, transfer the modifier */
2289 v
->alu
.outmod
= temp
;
2290 ins
->alu
.outmod
= midgard_outmod_none
;
2300 emit_fragment_epilogue(compiler_context
*ctx
, unsigned rt
)
2302 /* Loop to ourselves */
2304 struct midgard_instruction ins
= v_branch(false, false);
2305 ins
.writeout
= true;
2306 ins
.branch
.target_block
= ctx
->block_count
- 1;
2307 ins
.constants
[0] = rt
* 0x100;
2308 emit_mir_instruction(ctx
, ins
);
2310 ctx
->current_block
->epilogue
= true;
2311 schedule_barrier(ctx
);
2312 return ins
.branch
.target_block
;
2315 static midgard_block
*
2316 emit_block(compiler_context
*ctx
, nir_block
*block
)
2318 midgard_block
*this_block
= ctx
->after_block
;
2319 ctx
->after_block
= NULL
;
2322 this_block
= create_empty_block(ctx
);
2324 list_addtail(&this_block
->link
, &ctx
->blocks
);
2326 this_block
->is_scheduled
= false;
2329 /* Set up current block */
2330 list_inithead(&this_block
->instructions
);
2331 ctx
->current_block
= this_block
;
2333 nir_foreach_instr(instr
, block
) {
2334 emit_instr(ctx
, instr
);
2335 ++ctx
->instruction_count
;
2341 static midgard_block
*emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
);
2344 emit_if(struct compiler_context
*ctx
, nir_if
*nif
)
2346 midgard_block
*before_block
= ctx
->current_block
;
2348 /* Speculatively emit the branch, but we can't fill it in until later */
2349 EMIT(branch
, true, true);
2350 midgard_instruction
*then_branch
= mir_last_in_block(ctx
->current_block
);
2351 then_branch
->src
[0] = nir_src_index(ctx
, &nif
->condition
);
2353 /* Emit the two subblocks. */
2354 midgard_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
2355 midgard_block
*end_then_block
= ctx
->current_block
;
2357 /* Emit a jump from the end of the then block to the end of the else */
2358 EMIT(branch
, false, false);
2359 midgard_instruction
*then_exit
= mir_last_in_block(ctx
->current_block
);
2361 /* Emit second block, and check if it's empty */
2363 int else_idx
= ctx
->block_count
;
2364 int count_in
= ctx
->instruction_count
;
2365 midgard_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
2366 midgard_block
*end_else_block
= ctx
->current_block
;
2367 int after_else_idx
= ctx
->block_count
;
2369 /* Now that we have the subblocks emitted, fix up the branches */
2374 if (ctx
->instruction_count
== count_in
) {
2375 /* The else block is empty, so don't emit an exit jump */
2376 mir_remove_instruction(then_exit
);
2377 then_branch
->branch
.target_block
= after_else_idx
;
2379 then_branch
->branch
.target_block
= else_idx
;
2380 then_exit
->branch
.target_block
= after_else_idx
;
2383 /* Wire up the successors */
2385 ctx
->after_block
= create_empty_block(ctx
);
2387 midgard_block_add_successor(before_block
, then_block
);
2388 midgard_block_add_successor(before_block
, else_block
);
2390 midgard_block_add_successor(end_then_block
, ctx
->after_block
);
2391 midgard_block_add_successor(end_else_block
, ctx
->after_block
);
2395 emit_loop(struct compiler_context
*ctx
, nir_loop
*nloop
)
2397 /* Remember where we are */
2398 midgard_block
*start_block
= ctx
->current_block
;
2400 /* Allocate a loop number, growing the current inner loop depth */
2401 int loop_idx
= ++ctx
->current_loop_depth
;
2403 /* Get index from before the body so we can loop back later */
2404 int start_idx
= ctx
->block_count
;
2406 /* Emit the body itself */
2407 midgard_block
*loop_block
= emit_cf_list(ctx
, &nloop
->body
);
2409 /* Branch back to loop back */
2410 struct midgard_instruction br_back
= v_branch(false, false);
2411 br_back
.branch
.target_block
= start_idx
;
2412 emit_mir_instruction(ctx
, br_back
);
2414 /* Mark down that branch in the graph. */
2415 midgard_block_add_successor(start_block
, loop_block
);
2416 midgard_block_add_successor(ctx
->current_block
, loop_block
);
2418 /* Find the index of the block about to follow us (note: we don't add
2419 * one; blocks are 0-indexed so we get a fencepost problem) */
2420 int break_block_idx
= ctx
->block_count
;
2422 /* Fix up the break statements we emitted to point to the right place,
2423 * now that we can allocate a block number for them */
2424 ctx
->after_block
= create_empty_block(ctx
);
2426 list_for_each_entry_from(struct midgard_block
, block
, start_block
, &ctx
->blocks
, link
) {
2427 mir_foreach_instr_in_block(block
, ins
) {
2428 if (ins
->type
!= TAG_ALU_4
) continue;
2429 if (!ins
->compact_branch
) continue;
2431 /* We found a branch -- check the type to see if we need to do anything */
2432 if (ins
->branch
.target_type
!= TARGET_BREAK
) continue;
2434 /* It's a break! Check if it's our break */
2435 if (ins
->branch
.target_break
!= loop_idx
) continue;
2437 /* Okay, cool, we're breaking out of this loop.
2438 * Rewrite from a break to a goto */
2440 ins
->branch
.target_type
= TARGET_GOTO
;
2441 ins
->branch
.target_block
= break_block_idx
;
2443 midgard_block_add_successor(block
, ctx
->after_block
);
2447 /* Now that we've finished emitting the loop, free up the depth again
2448 * so we play nice with recursion amid nested loops */
2449 --ctx
->current_loop_depth
;
2451 /* Dump loop stats */
2455 static midgard_block
*
2456 emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
)
2458 midgard_block
*start_block
= NULL
;
2460 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2461 switch (node
->type
) {
2462 case nir_cf_node_block
: {
2463 midgard_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
2466 start_block
= block
;
2471 case nir_cf_node_if
:
2472 emit_if(ctx
, nir_cf_node_as_if(node
));
2475 case nir_cf_node_loop
:
2476 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2479 case nir_cf_node_function
:
2488 /* Due to lookahead, we need to report the first tag executed in the command
2489 * stream and in branch targets. An initial block might be empty, so iterate
2490 * until we find one that 'works' */
2493 midgard_get_first_tag_from_block(compiler_context
*ctx
, unsigned block_idx
)
2495 midgard_block
*initial_block
= mir_get_block(ctx
, block_idx
);
2497 unsigned first_tag
= 0;
2499 mir_foreach_block_from(ctx
, initial_block
, v
) {
2500 if (v
->quadword_count
) {
2501 midgard_bundle
*initial_bundle
=
2502 util_dynarray_element(&v
->bundles
, midgard_bundle
, 0);
2504 first_tag
= initial_bundle
->tag
;
2513 pan_format_from_nir_base(nir_alu_type base
)
2517 return MALI_FORMAT_SINT
;
2520 return MALI_FORMAT_UINT
;
2521 case nir_type_float
:
2522 return MALI_CHANNEL_FLOAT
;
2524 unreachable("Invalid base");
2529 pan_format_from_nir_size(nir_alu_type base
, unsigned size
)
2531 if (base
== nir_type_float
) {
2533 case 16: return MALI_FORMAT_SINT
;
2534 case 32: return MALI_FORMAT_UNORM
;
2536 unreachable("Invalid float size for format");
2541 case 8: return MALI_CHANNEL_8
;
2542 case 16: return MALI_CHANNEL_16
;
2543 case 32: return MALI_CHANNEL_32
;
2545 unreachable("Invalid int size for format");
2550 static enum mali_format
2551 pan_format_from_glsl(const struct glsl_type
*type
)
2553 enum glsl_base_type glsl_base
= glsl_get_base_type(glsl_without_array(type
));
2554 nir_alu_type t
= nir_get_nir_type_for_glsl_base_type(glsl_base
);
2556 unsigned base
= nir_alu_type_get_base_type(t
);
2557 unsigned size
= nir_alu_type_get_type_size(t
);
2559 return pan_format_from_nir_base(base
) |
2560 pan_format_from_nir_size(base
, size
) |
2561 MALI_NR_CHANNELS(4);
2564 /* For each fragment writeout instruction, generate a writeout loop to
2565 * associate with it */
2568 mir_add_writeout_loops(compiler_context
*ctx
)
2570 for (unsigned rt
= 0; rt
< ARRAY_SIZE(ctx
->writeout_branch
); ++rt
) {
2571 midgard_instruction
*br
= ctx
->writeout_branch
[rt
];
2574 unsigned popped
= br
->branch
.target_block
;
2575 midgard_block_add_successor(mir_get_block(ctx
, popped
- 1), ctx
->current_block
);
2576 br
->branch
.target_block
= emit_fragment_epilogue(ctx
, rt
);
2578 /* If we have more RTs, we'll need to restore back after our
2579 * loop terminates */
2581 if ((rt
+ 1) < ARRAY_SIZE(ctx
->writeout_branch
) && ctx
->writeout_branch
[rt
+ 1]) {
2582 midgard_instruction uncond
= v_branch(false, false);
2583 uncond
.branch
.target_block
= popped
;
2584 emit_mir_instruction(ctx
, uncond
);
2585 midgard_block_add_successor(ctx
->current_block
, mir_get_block(ctx
, popped
));
2586 schedule_barrier(ctx
);
2588 /* We're last, so we can terminate here */
2589 br
->last_writeout
= true;
2595 midgard_compile_shader_nir(nir_shader
*nir
, midgard_program
*program
, bool is_blend
, unsigned blend_rt
, unsigned gpu_id
, bool shaderdb
)
2597 struct util_dynarray
*compiled
= &program
->compiled
;
2599 midgard_debug
= debug_get_option_midgard_debug();
2601 /* TODO: Bound against what? */
2602 compiler_context
*ctx
= rzalloc(NULL
, compiler_context
);
2605 ctx
->stage
= nir
->info
.stage
;
2606 ctx
->is_blend
= is_blend
;
2607 ctx
->alpha_ref
= program
->alpha_ref
;
2608 ctx
->blend_rt
= blend_rt
;
2609 ctx
->quirks
= midgard_get_quirks(gpu_id
);
2611 /* Start off with a safe cutoff, allowing usage of all 16 work
2612 * registers. Later, we'll promote uniform reads to uniform registers
2613 * if we determine it is beneficial to do so */
2614 ctx
->uniform_cutoff
= 8;
2616 /* Initialize at a global (not block) level hash tables */
2618 ctx
->ssa_constants
= _mesa_hash_table_u64_create(NULL
);
2619 ctx
->hash_to_temp
= _mesa_hash_table_u64_create(NULL
);
2620 ctx
->sysval_to_id
= _mesa_hash_table_u64_create(NULL
);
2622 /* Record the varying mapping for the command stream's bookkeeping */
2624 struct exec_list
*varyings
=
2625 ctx
->stage
== MESA_SHADER_VERTEX
? &nir
->outputs
: &nir
->inputs
;
2627 unsigned max_varying
= 0;
2628 nir_foreach_variable(var
, varyings
) {
2629 unsigned loc
= var
->data
.driver_location
;
2630 unsigned sz
= glsl_type_size(var
->type
, FALSE
);
2632 for (int c
= 0; c
< sz
; ++c
) {
2633 program
->varyings
[loc
+ c
] = var
->data
.location
+ c
;
2634 program
->varying_type
[loc
+ c
] = pan_format_from_glsl(var
->type
);
2635 max_varying
= MAX2(max_varying
, loc
+ c
);
2639 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2640 * (so we don't accidentally duplicate the epilogue since mesa/st has
2641 * messed with our I/O quite a bit already) */
2643 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2645 if (ctx
->stage
== MESA_SHADER_VERTEX
) {
2646 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
2647 NIR_PASS_V(nir
, nir_lower_point_size
, 1.0, 1024.0);
2650 NIR_PASS_V(nir
, nir_lower_var_copies
);
2651 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2652 NIR_PASS_V(nir
, nir_split_var_copies
);
2653 NIR_PASS_V(nir
, nir_lower_var_copies
);
2654 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
2655 NIR_PASS_V(nir
, nir_lower_var_copies
);
2656 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2658 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, 0);
2660 /* Optimisation passes */
2662 optimise_nir(nir
, ctx
->quirks
);
2664 if (midgard_debug
& MIDGARD_DBG_SHADERS
) {
2665 nir_print_shader(nir
, stdout
);
2668 /* Assign sysvals and counts, now that we're sure
2669 * (post-optimisation) */
2671 midgard_nir_assign_sysvals(ctx
, nir
);
2673 program
->uniform_count
= nir
->num_uniforms
;
2674 program
->sysval_count
= ctx
->sysval_count
;
2675 memcpy(program
->sysvals
, ctx
->sysvals
, sizeof(ctx
->sysvals
[0]) * ctx
->sysval_count
);
2677 nir_foreach_function(func
, nir
) {
2681 list_inithead(&ctx
->blocks
);
2682 ctx
->block_count
= 0;
2685 emit_cf_list(ctx
, &func
->impl
->body
);
2686 break; /* TODO: Multi-function shaders */
2689 util_dynarray_init(compiled
, NULL
);
2691 /* Per-block lowering before opts */
2693 mir_foreach_block(ctx
, block
) {
2694 inline_alu_constants(ctx
, block
);
2695 midgard_opt_promote_fmov(ctx
, block
);
2696 embedded_to_inline_constant(ctx
, block
);
2698 /* MIR-level optimizations */
2700 bool progress
= false;
2705 mir_foreach_block(ctx
, block
) {
2706 progress
|= midgard_opt_pos_propagate(ctx
, block
);
2707 progress
|= midgard_opt_copy_prop(ctx
, block
);
2708 progress
|= midgard_opt_dead_code_eliminate(ctx
, block
);
2709 progress
|= midgard_opt_combine_projection(ctx
, block
);
2710 progress
|= midgard_opt_varying_projection(ctx
, block
);
2711 progress
|= midgard_opt_not_propagate(ctx
, block
);
2712 progress
|= midgard_opt_fuse_src_invert(ctx
, block
);
2713 progress
|= midgard_opt_fuse_dest_invert(ctx
, block
);
2714 progress
|= midgard_opt_csel_invert(ctx
, block
);
2715 progress
|= midgard_opt_drop_cmp_invert(ctx
, block
);
2716 progress
|= midgard_opt_invert_branch(ctx
, block
);
2720 mir_foreach_block(ctx
, block
) {
2721 midgard_lower_invert(ctx
, block
);
2722 midgard_lower_derivatives(ctx
, block
);
2725 /* Nested control-flow can result in dead branches at the end of the
2726 * block. This messes with our analysis and is just dead code, so cull
2728 mir_foreach_block(ctx
, block
) {
2729 midgard_opt_cull_dead_branch(ctx
, block
);
2732 /* Ensure we were lowered */
2733 mir_foreach_instr_global(ctx
, ins
) {
2734 assert(!ins
->invert
);
2737 if (ctx
->stage
== MESA_SHADER_FRAGMENT
)
2738 mir_add_writeout_loops(ctx
);
2741 schedule_program(ctx
);
2744 /* Now that all the bundles are scheduled and we can calculate block
2745 * sizes, emit actual branch instructions rather than placeholders */
2747 int br_block_idx
= 0;
2749 mir_foreach_block(ctx
, block
) {
2750 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2751 for (int c
= 0; c
< bundle
->instruction_count
; ++c
) {
2752 midgard_instruction
*ins
= bundle
->instructions
[c
];
2754 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2756 /* Parse some basic branch info */
2757 bool is_compact
= ins
->unit
== ALU_ENAB_BR_COMPACT
;
2758 bool is_conditional
= ins
->branch
.conditional
;
2759 bool is_inverted
= ins
->branch
.invert_conditional
;
2760 bool is_discard
= ins
->branch
.target_type
== TARGET_DISCARD
;
2761 bool is_writeout
= ins
->writeout
;
2763 /* Determine the block we're jumping to */
2764 int target_number
= ins
->branch
.target_block
;
2766 /* Report the destination tag */
2767 int dest_tag
= is_discard
? 0 : midgard_get_first_tag_from_block(ctx
, target_number
);
2769 /* Count up the number of quadwords we're
2770 * jumping over = number of quadwords until
2771 * (br_block_idx, target_number) */
2773 int quadword_offset
= 0;
2777 } else if (target_number
> br_block_idx
) {
2780 for (int idx
= br_block_idx
+ 1; idx
< target_number
; ++idx
) {
2781 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2784 quadword_offset
+= blk
->quadword_count
;
2787 /* Jump backwards */
2789 for (int idx
= br_block_idx
; idx
>= target_number
; --idx
) {
2790 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2793 quadword_offset
-= blk
->quadword_count
;
2797 /* Unconditional extended branches (far jumps)
2798 * have issues, so we always use a conditional
2799 * branch, setting the condition to always for
2800 * unconditional. For compact unconditional
2801 * branches, cond isn't used so it doesn't
2802 * matter what we pick. */
2804 midgard_condition cond
=
2805 !is_conditional
? midgard_condition_always
:
2806 is_inverted
? midgard_condition_false
:
2807 midgard_condition_true
;
2809 midgard_jmp_writeout_op op
=
2810 is_discard
? midgard_jmp_writeout_op_discard
:
2811 is_writeout
? midgard_jmp_writeout_op_writeout
:
2812 (is_compact
&& !is_conditional
) ? midgard_jmp_writeout_op_branch_uncond
:
2813 midgard_jmp_writeout_op_branch_cond
;
2816 midgard_branch_extended branch
=
2817 midgard_create_branch_extended(
2822 memcpy(&ins
->branch_extended
, &branch
, sizeof(branch
));
2823 } else if (is_conditional
|| is_discard
) {
2824 midgard_branch_cond branch
= {
2826 .dest_tag
= dest_tag
,
2827 .offset
= quadword_offset
,
2831 assert(branch
.offset
== quadword_offset
);
2833 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2835 assert(op
== midgard_jmp_writeout_op_branch_uncond
);
2837 midgard_branch_uncond branch
= {
2839 .dest_tag
= dest_tag
,
2840 .offset
= quadword_offset
,
2844 assert(branch
.offset
== quadword_offset
);
2846 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2854 /* Emit flat binary from the instruction arrays. Iterate each block in
2855 * sequence. Save instruction boundaries such that lookahead tags can
2856 * be assigned easily */
2858 /* Cache _all_ bundles in source order for lookahead across failed branches */
2860 int bundle_count
= 0;
2861 mir_foreach_block(ctx
, block
) {
2862 bundle_count
+= block
->bundles
.size
/ sizeof(midgard_bundle
);
2864 midgard_bundle
**source_order_bundles
= malloc(sizeof(midgard_bundle
*) * bundle_count
);
2866 mir_foreach_block(ctx
, block
) {
2867 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2868 source_order_bundles
[bundle_idx
++] = bundle
;
2872 int current_bundle
= 0;
2874 /* Midgard prefetches instruction types, so during emission we
2875 * need to lookahead. Unless this is the last instruction, in
2876 * which we return 1. */
2878 mir_foreach_block(ctx
, block
) {
2879 mir_foreach_bundle_in_block(block
, bundle
) {
2882 if (!bundle
->last_writeout
&& (current_bundle
+ 1 < bundle_count
))
2883 lookahead
= source_order_bundles
[current_bundle
+ 1]->tag
;
2885 emit_binary_bundle(ctx
, bundle
, compiled
, lookahead
);
2889 /* TODO: Free deeper */
2890 //util_dynarray_fini(&block->instructions);
2893 free(source_order_bundles
);
2895 /* Report the very first tag executed */
2896 program
->first_tag
= midgard_get_first_tag_from_block(ctx
, 0);
2898 /* Deal with off-by-one related to the fencepost problem */
2899 program
->work_register_count
= ctx
->work_registers
+ 1;
2900 program
->uniform_cutoff
= ctx
->uniform_cutoff
;
2902 program
->blend_patch_offset
= ctx
->blend_constant_offset
;
2903 program
->tls_size
= ctx
->tls_size
;
2905 if (midgard_debug
& MIDGARD_DBG_SHADERS
)
2906 disassemble_midgard(program
->compiled
.data
, program
->compiled
.size
, gpu_id
, ctx
->stage
);
2908 if (midgard_debug
& MIDGARD_DBG_SHADERDB
|| shaderdb
) {
2909 unsigned nr_bundles
= 0, nr_ins
= 0;
2911 /* Count instructions and bundles */
2913 mir_foreach_block(ctx
, block
) {
2914 nr_bundles
+= util_dynarray_num_elements(
2915 &block
->bundles
, midgard_bundle
);
2917 mir_foreach_bundle_in_block(block
, bun
)
2918 nr_ins
+= bun
->instruction_count
;
2921 /* Calculate thread count. There are certain cutoffs by
2922 * register count for thread count */
2924 unsigned nr_registers
= program
->work_register_count
;
2926 unsigned nr_threads
=
2927 (nr_registers
<= 4) ? 4 :
2928 (nr_registers
<= 8) ? 2 :
2933 fprintf(stderr
, "shader%d - %s shader: "
2934 "%u inst, %u bundles, %u quadwords, "
2935 "%u registers, %u threads, %u loops, "
2936 "%u:%u spills:fills\n",
2938 gl_shader_stage_name(ctx
->stage
),
2939 nr_ins
, nr_bundles
, ctx
->quadword_count
,
2940 nr_registers
, nr_threads
,
2942 ctx
->spills
, ctx
->fills
);