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_debug.h"
40 #include "util/u_dynarray.h"
41 #include "util/list.h"
42 #include "main/mtypes.h"
45 #include "midgard_nir.h"
46 #include "midgard_compile.h"
47 #include "midgard_ops.h"
51 #include "disassemble.h"
53 static const struct debug_named_value debug_options
[] = {
54 {"msgs", MIDGARD_DBG_MSGS
, "Print debug messages"},
55 {"shaders", MIDGARD_DBG_SHADERS
, "Dump shaders in NIR and MIR"},
59 DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug
, "MIDGARD_MESA_DEBUG", debug_options
, 0)
61 int midgard_debug
= 0;
63 #define DBG(fmt, ...) \
64 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
65 fprintf(stderr, "%s:%d: "fmt, \
66 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
69 midgard_is_branch_unit(unsigned unit
)
71 return (unit
== ALU_ENAB_BRANCH
) || (unit
== ALU_ENAB_BR_COMPACT
);
75 midgard_block_add_successor(midgard_block
*block
, midgard_block
*successor
)
77 block
->successors
[block
->nr_successors
++] = successor
;
78 assert(block
->nr_successors
<= ARRAY_SIZE(block
->successors
));
81 /* Helpers to generate midgard_instruction's using macro magic, since every
82 * driver seems to do it that way */
84 #define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
85 #define SWIZZLE_XXXX SWIZZLE(COMPONENT_X, COMPONENT_X, COMPONENT_X, COMPONENT_X)
86 #define SWIZZLE_XYXX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_X)
87 #define SWIZZLE_XYZX SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_X)
88 #define SWIZZLE_XYZW SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W)
89 #define SWIZZLE_WWWW SWIZZLE(COMPONENT_W, COMPONENT_W, COMPONENT_W, COMPONENT_W)
91 static inline unsigned
92 swizzle_of(unsigned comp
)
104 unreachable("Invalid component count");
108 static inline unsigned
109 mask_of(unsigned nr_comp
)
111 return (1 << nr_comp
) - 1;
114 #define M_LOAD_STORE(name, rname, uname) \
115 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
116 midgard_instruction i = { \
117 .type = TAG_LOAD_STORE_4, \
124 .op = midgard_op_##name, \
126 .swizzle = SWIZZLE_XYZW, \
134 #define M_LOAD(name) M_LOAD_STORE(name, dest, src0)
135 #define M_STORE(name) M_LOAD_STORE(name, src0, dest)
137 /* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
138 * the corresponding Midgard source */
140 static midgard_vector_alu_src
141 vector_alu_modifiers(nir_alu_src
*src
, bool is_int
)
143 if (!src
) return blank_alu_src
;
145 midgard_vector_alu_src alu_src
= {
148 .half
= 0, /* TODO */
149 .swizzle
= SWIZZLE_FROM_ARRAY(src
->swizzle
)
153 /* TODO: sign-extend/zero-extend */
154 alu_src
.mod
= midgard_int_normal
;
156 /* These should have been lowered away */
157 assert(!(src
->abs
|| src
->negate
));
159 alu_src
.mod
= (src
->abs
<< 0) | (src
->negate
<< 1);
165 /* load/store instructions have both 32-bit and 16-bit variants, depending on
166 * whether we are using vectors composed of highp or mediump. At the moment, we
167 * don't support half-floats -- this requires changes in other parts of the
168 * compiler -- therefore the 16-bit versions are commented out. */
170 //M_LOAD(ld_attr_16);
172 //M_LOAD(ld_vary_16);
174 //M_LOAD(ld_uniform_16);
175 M_LOAD(ld_uniform_32
);
176 M_LOAD(ld_color_buffer_8
);
177 //M_STORE(st_vary_16);
179 M_STORE(st_cubemap_coords
);
181 static midgard_instruction
182 v_alu_br_compact_cond(midgard_jmp_writeout_op op
, unsigned tag
, signed offset
, unsigned cond
)
184 midgard_branch_cond branch
= {
192 memcpy(&compact
, &branch
, sizeof(branch
));
194 midgard_instruction ins
= {
196 .unit
= ALU_ENAB_BR_COMPACT
,
197 .prepacked_branch
= true,
198 .compact_branch
= true,
199 .br_compact
= compact
202 if (op
== midgard_jmp_writeout_op_writeout
)
208 static midgard_instruction
209 v_branch(bool conditional
, bool invert
)
211 midgard_instruction ins
= {
213 .unit
= ALU_ENAB_BRANCH
,
214 .compact_branch
= true,
216 .conditional
= conditional
,
217 .invert_conditional
= invert
224 static midgard_branch_extended
225 midgard_create_branch_extended( midgard_condition cond
,
226 midgard_jmp_writeout_op op
,
228 signed quadword_offset
)
230 /* For unclear reasons, the condition code is repeated 8 times */
231 uint16_t duplicated_cond
=
241 midgard_branch_extended branch
= {
243 .dest_tag
= dest_tag
,
244 .offset
= quadword_offset
,
245 .cond
= duplicated_cond
252 attach_constants(compiler_context
*ctx
, midgard_instruction
*ins
, void *constants
, int name
)
254 ins
->has_constants
= true;
255 memcpy(&ins
->constants
, constants
, 16);
259 glsl_type_size(const struct glsl_type
*type
, bool bindless
)
261 return glsl_count_attribute_slots(type
, false);
264 /* Lower fdot2 to a vector multiplication followed by channel addition */
266 midgard_nir_lower_fdot2_body(nir_builder
*b
, nir_alu_instr
*alu
)
268 if (alu
->op
!= nir_op_fdot2
)
271 b
->cursor
= nir_before_instr(&alu
->instr
);
273 nir_ssa_def
*src0
= nir_ssa_for_alu_src(b
, alu
, 0);
274 nir_ssa_def
*src1
= nir_ssa_for_alu_src(b
, alu
, 1);
276 nir_ssa_def
*product
= nir_fmul(b
, src0
, src1
);
278 nir_ssa_def
*sum
= nir_fadd(b
,
279 nir_channel(b
, product
, 0),
280 nir_channel(b
, product
, 1));
282 /* Replace the fdot2 with this sum */
283 nir_ssa_def_rewrite_uses(&alu
->dest
.dest
.ssa
, nir_src_for_ssa(sum
));
287 midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr
*instr
)
289 switch (instr
->intrinsic
) {
290 case nir_intrinsic_load_viewport_scale
:
291 return PAN_SYSVAL_VIEWPORT_SCALE
;
292 case nir_intrinsic_load_viewport_offset
:
293 return PAN_SYSVAL_VIEWPORT_OFFSET
;
300 midgard_nir_assign_sysval_body(compiler_context
*ctx
, nir_instr
*instr
)
304 if (instr
->type
== nir_instr_type_intrinsic
) {
305 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
306 sysval
= midgard_nir_sysval_for_intrinsic(intr
);
312 /* We have a sysval load; check if it's already been assigned */
314 if (_mesa_hash_table_u64_search(ctx
->sysval_to_id
, sysval
))
317 /* It hasn't -- so assign it now! */
319 unsigned id
= ctx
->sysval_count
++;
320 _mesa_hash_table_u64_insert(ctx
->sysval_to_id
, sysval
, (void *) ((uintptr_t) id
+ 1));
321 ctx
->sysvals
[id
] = sysval
;
325 midgard_nir_assign_sysvals(compiler_context
*ctx
, nir_shader
*shader
)
327 ctx
->sysval_count
= 0;
329 nir_foreach_function(function
, shader
) {
330 if (!function
->impl
) continue;
332 nir_foreach_block(block
, function
->impl
) {
333 nir_foreach_instr_safe(instr
, block
) {
334 midgard_nir_assign_sysval_body(ctx
, instr
);
341 midgard_nir_lower_fdot2(nir_shader
*shader
)
343 bool progress
= false;
345 nir_foreach_function(function
, shader
) {
346 if (!function
->impl
) continue;
349 nir_builder
*b
= &_b
;
350 nir_builder_init(b
, function
->impl
);
352 nir_foreach_block(block
, function
->impl
) {
353 nir_foreach_instr_safe(instr
, block
) {
354 if (instr
->type
!= nir_instr_type_alu
) continue;
356 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
357 midgard_nir_lower_fdot2_body(b
, alu
);
363 nir_metadata_preserve(function
->impl
, nir_metadata_block_index
| nir_metadata_dominance
);
371 optimise_nir(nir_shader
*nir
)
374 unsigned lower_flrp
=
375 (nir
->options
->lower_flrp16
? 16 : 0) |
376 (nir
->options
->lower_flrp32
? 32 : 0) |
377 (nir
->options
->lower_flrp64
? 64 : 0);
379 NIR_PASS(progress
, nir
, nir_lower_regs_to_ssa
);
380 NIR_PASS(progress
, nir
, midgard_nir_lower_fdot2
);
381 NIR_PASS(progress
, nir
, nir_lower_idiv
);
383 nir_lower_tex_options lower_tex_options
= {
388 NIR_PASS(progress
, nir
, nir_lower_tex
, &lower_tex_options
);
393 NIR_PASS(progress
, nir
, nir_lower_var_copies
);
394 NIR_PASS(progress
, nir
, nir_lower_vars_to_ssa
);
396 NIR_PASS(progress
, nir
, nir_copy_prop
);
397 NIR_PASS(progress
, nir
, nir_opt_dce
);
398 NIR_PASS(progress
, nir
, nir_opt_dead_cf
);
399 NIR_PASS(progress
, nir
, nir_opt_cse
);
400 NIR_PASS(progress
, nir
, nir_opt_peephole_select
, 64, false, true);
401 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
402 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
404 if (lower_flrp
!= 0) {
405 bool lower_flrp_progress
= false;
406 NIR_PASS(lower_flrp_progress
,
410 false /* always_precise */,
411 nir
->options
->lower_ffma
);
412 if (lower_flrp_progress
) {
413 NIR_PASS(progress
, nir
,
414 nir_opt_constant_folding
);
418 /* Nothing should rematerialize any flrps, so we only
419 * need to do this lowering once.
424 NIR_PASS(progress
, nir
, nir_opt_undef
);
425 NIR_PASS(progress
, nir
, nir_opt_loop_unroll
,
428 nir_var_function_temp
);
430 /* TODO: Enable vectorize when merged upstream */
431 // NIR_PASS(progress, nir, nir_opt_vectorize);
434 /* Must be run at the end to prevent creation of fsin/fcos ops */
435 NIR_PASS(progress
, nir
, midgard_nir_scale_trig
);
440 NIR_PASS(progress
, nir
, nir_opt_dce
);
441 NIR_PASS(progress
, nir
, nir_opt_algebraic
);
442 NIR_PASS(progress
, nir
, nir_opt_constant_folding
);
443 NIR_PASS(progress
, nir
, nir_copy_prop
);
446 NIR_PASS(progress
, nir
, nir_opt_algebraic_late
);
448 /* We implement booleans as 32-bit 0/~0 */
449 NIR_PASS(progress
, nir
, nir_lower_bool_to_int32
);
451 /* Now that booleans are lowered, we can run out late opts */
452 NIR_PASS(progress
, nir
, midgard_nir_lower_algebraic_late
);
454 /* Lower mods for float ops only. Integer ops don't support modifiers
455 * (saturate doesn't make sense on integers, neg/abs require dedicated
458 NIR_PASS(progress
, nir
, nir_lower_to_source_mods
, nir_lower_float_source_mods
);
459 NIR_PASS(progress
, nir
, nir_copy_prop
);
460 NIR_PASS(progress
, nir
, nir_opt_dce
);
462 /* Take us out of SSA */
463 NIR_PASS(progress
, nir
, nir_lower_locals_to_regs
);
464 NIR_PASS(progress
, nir
, nir_convert_from_ssa
, true);
466 /* We are a vector architecture; write combine where possible */
467 NIR_PASS(progress
, nir
, nir_move_vec_src_uses_to_dest
);
468 NIR_PASS(progress
, nir
, nir_lower_vec_to_movs
);
470 NIR_PASS(progress
, nir
, nir_opt_dce
);
473 /* Front-half of aliasing the SSA slots, merely by inserting the flag in the
474 * appropriate hash table. Intentional off-by-one to avoid confusing NULL with
475 * r0. See the comments in compiler_context */
478 alias_ssa(compiler_context
*ctx
, int dest
, int src
)
480 _mesa_hash_table_u64_insert(ctx
->ssa_to_alias
, dest
+ 1, (void *) ((uintptr_t) src
+ 1));
481 _mesa_set_add(ctx
->leftover_ssa_to_alias
, (void *) (uintptr_t) (dest
+ 1));
484 /* ...or undo it, after which the original index will be used (dummy move should be emitted alongside this) */
487 unalias_ssa(compiler_context
*ctx
, int dest
)
489 _mesa_hash_table_u64_remove(ctx
->ssa_to_alias
, dest
+ 1);
490 /* TODO: Remove from leftover or no? */
493 /* Do not actually emit a load; instead, cache the constant for inlining */
496 emit_load_const(compiler_context
*ctx
, nir_load_const_instr
*instr
)
498 nir_ssa_def def
= instr
->def
;
500 float *v
= rzalloc_array(NULL
, float, 4);
501 nir_const_load_to_arr(v
, instr
, f32
);
502 _mesa_hash_table_u64_insert(ctx
->ssa_constants
, def
.index
+ 1, v
);
506 nir_src_index(compiler_context
*ctx
, nir_src
*src
)
509 return src
->ssa
->index
;
511 assert(!src
->reg
.indirect
);
512 return ctx
->func
->impl
->ssa_alloc
+ src
->reg
.reg
->index
;
517 nir_dest_index(compiler_context
*ctx
, nir_dest
*dst
)
520 return dst
->ssa
.index
;
522 assert(!dst
->reg
.indirect
);
523 return ctx
->func
->impl
->ssa_alloc
+ dst
->reg
.reg
->index
;
528 nir_alu_src_index(compiler_context
*ctx
, nir_alu_src
*src
)
530 return nir_src_index(ctx
, &src
->src
);
534 nir_is_non_scalar_swizzle(nir_alu_src
*src
, unsigned nr_components
)
536 unsigned comp
= src
->swizzle
[0];
538 for (unsigned c
= 1; c
< nr_components
; ++c
) {
539 if (src
->swizzle
[c
] != comp
)
546 /* Midgard puts scalar conditionals in r31.w; move an arbitrary source (the
547 * output of a conditional test) into that register */
550 emit_condition(compiler_context
*ctx
, nir_src
*src
, bool for_branch
, unsigned component
)
552 int condition
= nir_src_index(ctx
, src
);
554 /* Source to swizzle the desired component into w */
556 const midgard_vector_alu_src alu_src
= {
557 .swizzle
= SWIZZLE(component
, component
, component
, component
),
560 /* There is no boolean move instruction. Instead, we simulate a move by
561 * ANDing the condition with itself to get it into r31.w */
563 midgard_instruction ins
= {
566 /* We need to set the conditional as close as possible */
567 .precede_break
= true,
568 .unit
= for_branch
? UNIT_SMUL
: UNIT_SADD
,
573 .dest
= SSA_FIXED_REGISTER(31),
577 .op
= midgard_alu_op_iand
,
578 .outmod
= midgard_outmod_int_wrap
,
579 .reg_mode
= midgard_reg_mode_32
,
580 .dest_override
= midgard_dest_override_none
,
581 .mask
= (0x3 << 6), /* w */
582 .src1
= vector_alu_srco_unsigned(alu_src
),
583 .src2
= vector_alu_srco_unsigned(alu_src
)
587 emit_mir_instruction(ctx
, ins
);
590 /* Or, for mixed conditions (with csel_v), here's a vector version using all of
594 emit_condition_mixed(compiler_context
*ctx
, nir_alu_src
*src
, unsigned nr_comp
)
596 int condition
= nir_src_index(ctx
, &src
->src
);
598 /* Source to swizzle the desired component into w */
600 const midgard_vector_alu_src alu_src
= {
601 .swizzle
= SWIZZLE_FROM_ARRAY(src
->swizzle
),
604 /* There is no boolean move instruction. Instead, we simulate a move by
605 * ANDing the condition with itself to get it into r31.w */
607 midgard_instruction ins
= {
609 .precede_break
= true,
613 .dest
= SSA_FIXED_REGISTER(31),
616 .op
= midgard_alu_op_iand
,
617 .outmod
= midgard_outmod_int_wrap
,
618 .reg_mode
= midgard_reg_mode_32
,
619 .dest_override
= midgard_dest_override_none
,
620 .mask
= expand_writemask(mask_of(nr_comp
)),
621 .src1
= vector_alu_srco_unsigned(alu_src
),
622 .src2
= vector_alu_srco_unsigned(alu_src
)
626 emit_mir_instruction(ctx
, ins
);
631 /* Likewise, indirect offsets are put in r27.w. TODO: Allow componentwise
632 * pinning to eliminate this move in all known cases */
635 emit_indirect_offset(compiler_context
*ctx
, nir_src
*src
)
637 int offset
= nir_src_index(ctx
, src
);
639 midgard_instruction ins
= {
642 .src0
= SSA_UNUSED_1
,
644 .dest
= SSA_FIXED_REGISTER(REGISTER_OFFSET
),
647 .op
= midgard_alu_op_imov
,
648 .outmod
= midgard_outmod_int_wrap
,
649 .reg_mode
= midgard_reg_mode_32
,
650 .dest_override
= midgard_dest_override_none
,
651 .mask
= (0x3 << 6), /* w */
652 .src1
= vector_alu_srco_unsigned(zero_alu_src
),
653 .src2
= vector_alu_srco_unsigned(blank_alu_src_xxxx
)
657 emit_mir_instruction(ctx
, ins
);
660 #define ALU_CASE(nir, _op) \
662 op = midgard_alu_op_##_op; \
665 nir_is_fzero_constant(nir_src src
)
667 if (!nir_src_is_const(src
))
670 for (unsigned c
= 0; c
< nir_src_num_components(src
); ++c
) {
671 if (nir_src_comp_as_float(src
, c
) != 0.0)
679 emit_alu(compiler_context
*ctx
, nir_alu_instr
*instr
)
681 bool is_ssa
= instr
->dest
.dest
.is_ssa
;
683 unsigned dest
= nir_dest_index(ctx
, &instr
->dest
.dest
);
684 unsigned nr_components
= is_ssa
? instr
->dest
.dest
.ssa
.num_components
: instr
->dest
.dest
.reg
.reg
->num_components
;
685 unsigned nr_inputs
= nir_op_infos
[instr
->op
].num_inputs
;
687 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
688 * supported. A few do not and are commented for now. Also, there are a
689 * number of NIR ops which Midgard does not support and need to be
690 * lowered, also TODO. This switch block emits the opcode and calling
691 * convention of the Midgard instruction; actual packing is done in
697 ALU_CASE(fadd
, fadd
);
698 ALU_CASE(fmul
, fmul
);
699 ALU_CASE(fmin
, fmin
);
700 ALU_CASE(fmax
, fmax
);
701 ALU_CASE(imin
, imin
);
702 ALU_CASE(imax
, imax
);
703 ALU_CASE(umin
, umin
);
704 ALU_CASE(umax
, umax
);
705 ALU_CASE(ffloor
, ffloor
);
706 ALU_CASE(fround_even
, froundeven
);
707 ALU_CASE(ftrunc
, ftrunc
);
708 ALU_CASE(fceil
, fceil
);
709 ALU_CASE(fdot3
, fdot3
);
710 ALU_CASE(fdot4
, fdot4
);
711 ALU_CASE(iadd
, iadd
);
712 ALU_CASE(isub
, isub
);
713 ALU_CASE(imul
, imul
);
715 /* Zero shoved as second-arg */
716 ALU_CASE(iabs
, iabsdiff
);
720 ALU_CASE(feq32
, feq
);
721 ALU_CASE(fne32
, fne
);
722 ALU_CASE(flt32
, flt
);
723 ALU_CASE(ieq32
, ieq
);
724 ALU_CASE(ine32
, ine
);
725 ALU_CASE(ilt32
, ilt
);
726 ALU_CASE(ult32
, ult
);
728 /* We don't have a native b2f32 instruction. Instead, like many
729 * GPUs, we exploit booleans as 0/~0 for false/true, and
730 * correspondingly AND
731 * by 1.0 to do the type conversion. For the moment, prime us
734 * iand [whatever], #0
736 * At the end of emit_alu (as MIR), we'll fix-up the constant
739 ALU_CASE(b2f32
, iand
);
740 ALU_CASE(b2i32
, iand
);
742 /* Likewise, we don't have a dedicated f2b32 instruction, but
743 * we can do a "not equal to 0.0" test. */
745 ALU_CASE(f2b32
, fne
);
746 ALU_CASE(i2b32
, ine
);
748 ALU_CASE(frcp
, frcp
);
749 ALU_CASE(frsq
, frsqrt
);
750 ALU_CASE(fsqrt
, fsqrt
);
751 ALU_CASE(fexp2
, fexp2
);
752 ALU_CASE(flog2
, flog2
);
754 ALU_CASE(f2i32
, f2i
);
755 ALU_CASE(f2u32
, f2u
);
756 ALU_CASE(i2f32
, i2f
);
757 ALU_CASE(u2f32
, u2f
);
759 ALU_CASE(fsin
, fsin
);
760 ALU_CASE(fcos
, fcos
);
762 /* Second op implicit #0 */
763 ALU_CASE(inot
, inor
);
764 ALU_CASE(iand
, iand
);
766 ALU_CASE(ixor
, ixor
);
767 ALU_CASE(ishl
, ishl
);
768 ALU_CASE(ishr
, iasr
);
769 ALU_CASE(ushr
, ilsr
);
771 ALU_CASE(b32all_fequal2
, fball_eq
);
772 ALU_CASE(b32all_fequal3
, fball_eq
);
773 ALU_CASE(b32all_fequal4
, fball_eq
);
775 ALU_CASE(b32any_fnequal2
, fbany_neq
);
776 ALU_CASE(b32any_fnequal3
, fbany_neq
);
777 ALU_CASE(b32any_fnequal4
, fbany_neq
);
779 ALU_CASE(b32all_iequal2
, iball_eq
);
780 ALU_CASE(b32all_iequal3
, iball_eq
);
781 ALU_CASE(b32all_iequal4
, iball_eq
);
783 ALU_CASE(b32any_inequal2
, ibany_neq
);
784 ALU_CASE(b32any_inequal3
, ibany_neq
);
785 ALU_CASE(b32any_inequal4
, ibany_neq
);
787 /* Source mods will be shoved in later */
788 ALU_CASE(fabs
, fmov
);
789 ALU_CASE(fneg
, fmov
);
790 ALU_CASE(fsat
, fmov
);
792 /* For greater-or-equal, we lower to less-or-equal and flip the
800 instr
->op
== nir_op_fge
? midgard_alu_op_fle
:
801 instr
->op
== nir_op_fge32
? midgard_alu_op_fle
:
802 instr
->op
== nir_op_ige32
? midgard_alu_op_ile
:
803 instr
->op
== nir_op_uge32
? midgard_alu_op_ule
:
806 /* Swap via temporary */
807 nir_alu_src temp
= instr
->src
[1];
808 instr
->src
[1] = instr
->src
[0];
809 instr
->src
[0] = temp
;
814 case nir_op_b32csel
: {
815 /* Midgard features both fcsel and icsel, depending on
816 * the type of the arguments/output. However, as long
817 * as we're careful we can _always_ use icsel and
818 * _never_ need fcsel, since the latter does additional
819 * floating-point-specific processing whereas the
820 * former just moves bits on the wire. It's not obvious
821 * why these are separate opcodes, save for the ability
822 * to do things like sat/pos/abs/neg for free */
824 bool mixed
= nir_is_non_scalar_swizzle(&instr
->src
[0], nr_components
);
825 op
= mixed
? midgard_alu_op_icsel_v
: midgard_alu_op_icsel
;
827 /* csel works as a two-arg in Midgard, since the condition is hardcoded in r31.w */
830 /* Emit the condition into r31 */
833 emit_condition_mixed(ctx
, &instr
->src
[0], nr_components
);
835 emit_condition(ctx
, &instr
->src
[0].src
, false, instr
->src
[0].swizzle
[0]);
837 /* The condition is the first argument; move the other
838 * arguments up one to be a binary instruction for
841 memmove(instr
->src
, instr
->src
+ 1, 2 * sizeof(nir_alu_src
));
846 DBG("Unhandled ALU op %s\n", nir_op_infos
[instr
->op
].name
);
851 /* Midgard can perform certain modifiers on output of an ALU op */
854 if (midgard_is_integer_out_op(op
)) {
855 outmod
= midgard_outmod_int_wrap
;
857 bool sat
= instr
->dest
.saturate
|| instr
->op
== nir_op_fsat
;
858 outmod
= sat
? midgard_outmod_sat
: midgard_outmod_none
;
861 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
863 if (instr
->op
== nir_op_fmax
) {
864 if (nir_is_fzero_constant(instr
->src
[0].src
)) {
865 op
= midgard_alu_op_fmov
;
867 outmod
= midgard_outmod_pos
;
868 instr
->src
[0] = instr
->src
[1];
869 } else if (nir_is_fzero_constant(instr
->src
[1].src
)) {
870 op
= midgard_alu_op_fmov
;
872 outmod
= midgard_outmod_pos
;
876 /* Fetch unit, quirks, etc information */
877 unsigned opcode_props
= alu_opcode_props
[op
].props
;
878 bool quirk_flipped_r24
= opcode_props
& QUIRK_FLIPPED_R24
;
880 /* src0 will always exist afaik, but src1 will not for 1-argument
881 * instructions. The latter can only be fetched if the instruction
882 * needs it, or else we may segfault. */
884 unsigned src0
= nir_alu_src_index(ctx
, &instr
->src
[0]);
885 unsigned src1
= nr_inputs
== 2 ? nir_alu_src_index(ctx
, &instr
->src
[1]) : SSA_UNUSED_0
;
887 /* Rather than use the instruction generation helpers, we do it
888 * ourselves here to avoid the mess */
890 midgard_instruction ins
= {
893 .src0
= quirk_flipped_r24
? SSA_UNUSED_1
: src0
,
894 .src1
= quirk_flipped_r24
? src0
: src1
,
899 nir_alu_src
*nirmods
[2] = { NULL
};
901 if (nr_inputs
== 2) {
902 nirmods
[0] = &instr
->src
[0];
903 nirmods
[1] = &instr
->src
[1];
904 } else if (nr_inputs
== 1) {
905 nirmods
[quirk_flipped_r24
] = &instr
->src
[0];
910 /* These were lowered to a move, so apply the corresponding mod */
912 if (instr
->op
== nir_op_fneg
|| instr
->op
== nir_op_fabs
) {
913 nir_alu_src
*s
= nirmods
[quirk_flipped_r24
];
915 if (instr
->op
== nir_op_fneg
)
916 s
->negate
= !s
->negate
;
918 if (instr
->op
== nir_op_fabs
)
922 bool is_int
= midgard_is_integer_op(op
);
924 midgard_vector_alu alu
= {
926 .reg_mode
= midgard_reg_mode_32
,
927 .dest_override
= midgard_dest_override_none
,
930 /* Writemask only valid for non-SSA NIR */
931 .mask
= expand_writemask(mask_of(nr_components
)),
933 .src1
= vector_alu_srco_unsigned(vector_alu_modifiers(nirmods
[0], is_int
)),
934 .src2
= vector_alu_srco_unsigned(vector_alu_modifiers(nirmods
[1], is_int
)),
937 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
940 alu
.mask
&= expand_writemask(instr
->dest
.write_mask
);
944 /* Late fixup for emulated instructions */
946 if (instr
->op
== nir_op_b2f32
|| instr
->op
== nir_op_b2i32
) {
947 /* Presently, our second argument is an inline #0 constant.
948 * Switch over to an embedded 1.0 constant (that can't fit
949 * inline, since we're 32-bit, not 16-bit like the inline
952 ins
.ssa_args
.inline_constant
= false;
953 ins
.ssa_args
.src1
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
954 ins
.has_constants
= true;
956 if (instr
->op
== nir_op_b2f32
) {
957 ins
.constants
[0] = 1.0f
;
959 /* Type pun it into place */
961 memcpy(&ins
.constants
[0], &one
, sizeof(uint32_t));
964 ins
.alu
.src2
= vector_alu_srco_unsigned(blank_alu_src_xxxx
);
965 } else if (nr_inputs
== 1 && !quirk_flipped_r24
) {
966 /* Lots of instructions need a 0 plonked in */
967 ins
.ssa_args
.inline_constant
= false;
968 ins
.ssa_args
.src1
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
969 ins
.has_constants
= true;
970 ins
.constants
[0] = 0.0f
;
971 ins
.alu
.src2
= vector_alu_srco_unsigned(blank_alu_src_xxxx
);
972 } else if (instr
->op
== nir_op_inot
) {
973 /* ~b = ~(b & b), so duplicate the source */
974 ins
.ssa_args
.src1
= ins
.ssa_args
.src0
;
975 ins
.alu
.src2
= ins
.alu
.src1
;
978 if ((opcode_props
& UNITS_ALL
) == UNIT_VLUT
) {
979 /* To avoid duplicating the lookup tables (probably), true LUT
980 * instructions can only operate as if they were scalars. Lower
981 * them here by changing the component. */
983 uint8_t original_swizzle
[4];
984 memcpy(original_swizzle
, nirmods
[0]->swizzle
, sizeof(nirmods
[0]->swizzle
));
986 for (int i
= 0; i
< nr_components
; ++i
) {
987 ins
.alu
.mask
= (0x3) << (2 * i
); /* Mask the associated component */
989 for (int j
= 0; j
< 4; ++j
)
990 nirmods
[0]->swizzle
[j
] = original_swizzle
[i
]; /* Pull from the correct component */
992 ins
.alu
.src1
= vector_alu_srco_unsigned(vector_alu_modifiers(nirmods
[0], is_int
));
993 emit_mir_instruction(ctx
, ins
);
996 emit_mir_instruction(ctx
, ins
);
1003 emit_uniform_read(compiler_context
*ctx
, unsigned dest
, unsigned offset
, nir_src
*indirect_offset
)
1005 /* TODO: half-floats */
1007 if (!indirect_offset
&& offset
< ctx
->uniform_cutoff
) {
1008 /* Fast path: For the first 16 uniforms, direct accesses are
1009 * 0-cycle, since they're just a register fetch in the usual
1010 * case. So, we alias the registers while we're still in
1013 int reg_slot
= 23 - offset
;
1014 alias_ssa(ctx
, dest
, SSA_FIXED_REGISTER(reg_slot
));
1016 /* Otherwise, read from the 'special' UBO to access
1017 * higher-indexed uniforms, at a performance cost. More
1018 * generally, we're emitting a UBO read instruction. */
1020 midgard_instruction ins
= m_ld_uniform_32(dest
, offset
);
1022 /* TODO: Don't split */
1023 ins
.load_store
.varying_parameters
= (offset
& 7) << 7;
1024 ins
.load_store
.address
= offset
>> 3;
1026 if (indirect_offset
) {
1027 emit_indirect_offset(ctx
, indirect_offset
);
1028 ins
.load_store
.unknown
= 0x8700; /* xxx: what is this? */
1030 ins
.load_store
.unknown
= 0x1E00; /* xxx: what is this? */
1033 emit_mir_instruction(ctx
, ins
);
1039 compiler_context
*ctx
,
1040 unsigned dest
, unsigned offset
,
1041 unsigned nr_comp
, unsigned component
,
1042 nir_src
*indirect_offset
)
1044 /* XXX: Half-floats? */
1045 /* TODO: swizzle, mask */
1047 midgard_instruction ins
= m_ld_vary_32(dest
, offset
);
1048 ins
.load_store
.mask
= mask_of(nr_comp
);
1049 ins
.load_store
.swizzle
= SWIZZLE_XYZW
>> (2 * component
);
1051 midgard_varying_parameter p
= {
1053 .interpolation
= midgard_interp_default
,
1054 .flat
= /*var->data.interpolation == INTERP_MODE_FLAT*/ 0
1058 memcpy(&u
, &p
, sizeof(p
));
1059 ins
.load_store
.varying_parameters
= u
;
1061 if (indirect_offset
) {
1062 /* We need to add in the dynamic index, moved to r27.w */
1063 emit_indirect_offset(ctx
, indirect_offset
);
1064 ins
.load_store
.unknown
= 0x79e; /* xxx: what is this? */
1066 /* Just a direct load */
1067 ins
.load_store
.unknown
= 0x1e9e; /* xxx: what is this? */
1070 emit_mir_instruction(ctx
, ins
);
1074 emit_sysval_read(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1076 /* First, pull out the destination */
1077 unsigned dest
= nir_dest_index(ctx
, &instr
->dest
);
1079 /* Now, figure out which uniform this is */
1080 int sysval
= midgard_nir_sysval_for_intrinsic(instr
);
1081 void *val
= _mesa_hash_table_u64_search(ctx
->sysval_to_id
, sysval
);
1083 /* Sysvals are prefix uniforms */
1084 unsigned uniform
= ((uintptr_t) val
) - 1;
1086 /* Emit the read itself -- this is never indirect */
1087 emit_uniform_read(ctx
, dest
, uniform
, NULL
);
1090 /* Reads RGBA8888 value from the tilebuffer and converts to a RGBA32F register,
1091 * using scalar ops functional on earlier Midgard generations. Newer Midgard
1092 * generations have faster vectorized reads. This operation is for blend
1093 * shaders in particular; reading the tilebuffer from the fragment shader
1094 * remains an open problem. */
1097 emit_fb_read_blend_scalar(compiler_context
*ctx
, unsigned reg
)
1099 midgard_instruction ins
= m_ld_color_buffer_8(reg
, 0);
1100 ins
.load_store
.swizzle
= 0; /* xxxx */
1102 /* Read each component sequentially */
1104 for (unsigned c
= 0; c
< 4; ++c
) {
1105 ins
.load_store
.mask
= (1 << c
);
1106 ins
.load_store
.unknown
= c
;
1107 emit_mir_instruction(ctx
, ins
);
1110 /* vadd.u2f hr2, zext(hr2), #0 */
1112 midgard_vector_alu_src alu_src
= blank_alu_src
;
1113 alu_src
.mod
= midgard_int_zero_extend
;
1114 alu_src
.half
= true;
1116 midgard_instruction u2f
= {
1120 .src1
= SSA_UNUSED_0
,
1122 .inline_constant
= true
1125 .op
= midgard_alu_op_u2f
,
1126 .reg_mode
= midgard_reg_mode_16
,
1127 .dest_override
= midgard_dest_override_none
,
1129 .src1
= vector_alu_srco_unsigned(alu_src
),
1130 .src2
= vector_alu_srco_unsigned(blank_alu_src
),
1134 emit_mir_instruction(ctx
, u2f
);
1136 /* vmul.fmul.sat r1, hr2, #0.00392151 */
1140 midgard_instruction fmul
= {
1142 .inline_constant
= _mesa_float_to_half(1.0 / 255.0),
1146 .src1
= SSA_UNUSED_0
,
1147 .inline_constant
= true
1150 .op
= midgard_alu_op_fmul
,
1151 .reg_mode
= midgard_reg_mode_32
,
1152 .dest_override
= midgard_dest_override_none
,
1153 .outmod
= midgard_outmod_sat
,
1155 .src1
= vector_alu_srco_unsigned(alu_src
),
1156 .src2
= vector_alu_srco_unsigned(blank_alu_src
),
1160 emit_mir_instruction(ctx
, fmul
);
1164 emit_intrinsic(compiler_context
*ctx
, nir_intrinsic_instr
*instr
)
1166 unsigned offset
, reg
;
1168 switch (instr
->intrinsic
) {
1169 case nir_intrinsic_discard_if
:
1170 emit_condition(ctx
, &instr
->src
[0], true, COMPONENT_X
);
1174 case nir_intrinsic_discard
: {
1175 bool conditional
= instr
->intrinsic
== nir_intrinsic_discard_if
;
1176 struct midgard_instruction discard
= v_branch(conditional
, false);
1177 discard
.branch
.target_type
= TARGET_DISCARD
;
1178 emit_mir_instruction(ctx
, discard
);
1180 ctx
->can_discard
= true;
1184 case nir_intrinsic_load_uniform
:
1185 case nir_intrinsic_load_input
:
1186 offset
= nir_intrinsic_base(instr
);
1188 unsigned nr_comp
= nir_intrinsic_dest_components(instr
);
1189 bool direct
= nir_src_is_const(instr
->src
[0]);
1192 offset
+= nir_src_as_uint(instr
->src
[0]);
1195 /* We may need to apply a fractional offset */
1196 int component
= instr
->intrinsic
== nir_intrinsic_load_input
?
1197 nir_intrinsic_component(instr
) : 0;
1198 reg
= nir_dest_index(ctx
, &instr
->dest
);
1200 if (instr
->intrinsic
== nir_intrinsic_load_uniform
&& !ctx
->is_blend
) {
1201 emit_uniform_read(ctx
, reg
, ctx
->sysval_count
+ offset
, !direct
? &instr
->src
[0] : NULL
);
1202 } else if (ctx
->stage
== MESA_SHADER_FRAGMENT
&& !ctx
->is_blend
) {
1203 emit_varying_read(ctx
, reg
, offset
, nr_comp
, component
, !direct
? &instr
->src
[0] : NULL
);
1204 } else if (ctx
->is_blend
) {
1205 /* For blend shaders, load the input color, which is
1206 * preloaded to r0 */
1208 midgard_instruction move
= v_fmov(reg
, blank_alu_src
, SSA_FIXED_REGISTER(0));
1209 emit_mir_instruction(ctx
, move
);
1210 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1211 midgard_instruction ins
= m_ld_attr_32(reg
, offset
);
1212 ins
.load_store
.unknown
= 0x1E1E; /* XXX: What is this? */
1213 ins
.load_store
.mask
= mask_of(nr_comp
);
1214 emit_mir_instruction(ctx
, ins
);
1216 DBG("Unknown load\n");
1222 case nir_intrinsic_load_output
:
1223 assert(nir_src_is_const(instr
->src
[0]));
1224 reg
= nir_dest_index(ctx
, &instr
->dest
);
1226 if (ctx
->is_blend
) {
1228 emit_fb_read_blend_scalar(ctx
, reg
);
1230 DBG("Unknown output load\n");
1236 case nir_intrinsic_load_blend_const_color_rgba
: {
1237 assert(ctx
->is_blend
);
1238 reg
= nir_dest_index(ctx
, &instr
->dest
);
1240 /* Blend constants are embedded directly in the shader and
1241 * patched in, so we use some magic routing */
1243 midgard_instruction ins
= v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), blank_alu_src
, reg
);
1244 ins
.has_constants
= true;
1245 ins
.has_blend_constant
= true;
1246 emit_mir_instruction(ctx
, ins
);
1250 case nir_intrinsic_store_output
:
1251 assert(nir_src_is_const(instr
->src
[1]) && "no indirect outputs");
1253 offset
= nir_intrinsic_base(instr
) + nir_src_as_uint(instr
->src
[1]);
1255 reg
= nir_src_index(ctx
, &instr
->src
[0]);
1257 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
1258 /* gl_FragColor is not emitted with load/store
1259 * instructions. Instead, it gets plonked into
1260 * r0 at the end of the shader and we do the
1261 * framebuffer writeout dance. TODO: Defer
1264 midgard_instruction move
= v_fmov(reg
, blank_alu_src
, SSA_FIXED_REGISTER(0));
1265 emit_mir_instruction(ctx
, move
);
1267 /* Save the index we're writing to for later reference
1268 * in the epilogue */
1270 ctx
->fragment_output
= reg
;
1271 } else if (ctx
->stage
== MESA_SHADER_VERTEX
) {
1272 /* Varyings are written into one of two special
1273 * varying register, r26 or r27. The register itself is
1274 * selected as the register in the st_vary instruction,
1275 * minus the base of 26. E.g. write into r27 and then
1276 * call st_vary(1) */
1278 midgard_instruction ins
= v_fmov(reg
, blank_alu_src
, SSA_FIXED_REGISTER(26));
1279 emit_mir_instruction(ctx
, ins
);
1281 /* We should have been vectorized. That also lets us
1282 * ignore the mask. because the mask component on
1283 * st_vary is (as far as I can tell) ignored [the blob
1284 * sets it to zero] */
1285 assert(nir_intrinsic_component(instr
) == 0);
1287 midgard_instruction st
= m_st_vary_32(SSA_FIXED_REGISTER(0), offset
);
1288 st
.load_store
.unknown
= 0x1E9E; /* XXX: What is this? */
1289 emit_mir_instruction(ctx
, st
);
1291 DBG("Unknown store\n");
1297 case nir_intrinsic_load_alpha_ref_float
:
1298 assert(instr
->dest
.is_ssa
);
1300 float ref_value
= ctx
->alpha_ref
;
1302 float *v
= ralloc_array(NULL
, float, 4);
1303 memcpy(v
, &ref_value
, sizeof(float));
1304 _mesa_hash_table_u64_insert(ctx
->ssa_constants
, instr
->dest
.ssa
.index
+ 1, v
);
1307 case nir_intrinsic_load_viewport_scale
:
1308 case nir_intrinsic_load_viewport_offset
:
1309 emit_sysval_read(ctx
, instr
);
1313 printf ("Unhandled intrinsic\n");
1320 midgard_tex_format(enum glsl_sampler_dim dim
)
1323 case GLSL_SAMPLER_DIM_2D
:
1324 case GLSL_SAMPLER_DIM_EXTERNAL
:
1327 case GLSL_SAMPLER_DIM_3D
:
1330 case GLSL_SAMPLER_DIM_CUBE
:
1331 return TEXTURE_CUBE
;
1334 DBG("Unknown sampler dim type\n");
1341 midgard_tex_op(nir_texop op
)
1346 return TEXTURE_OP_NORMAL
;
1348 return TEXTURE_OP_LOD
;
1350 unreachable("Unhanlded texture op");
1355 emit_tex(compiler_context
*ctx
, nir_tex_instr
*instr
)
1358 //assert (!instr->sampler);
1359 //assert (!instr->texture_array_size);
1361 /* Allocate registers via a round robin scheme to alternate between the two registers */
1362 int reg
= ctx
->texture_op_count
& 1;
1363 int in_reg
= reg
, out_reg
= reg
;
1365 /* Make room for the reg */
1367 if (ctx
->texture_index
[reg
] > -1)
1368 unalias_ssa(ctx
, ctx
->texture_index
[reg
]);
1370 int texture_index
= instr
->texture_index
;
1371 int sampler_index
= texture_index
;
1373 unsigned position_swizzle
= 0;
1375 for (unsigned i
= 0; i
< instr
->num_srcs
; ++i
) {
1376 int reg
= SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE
+ in_reg
);
1377 int index
= nir_src_index(ctx
, &instr
->src
[i
].src
);
1378 int nr_comp
= nir_src_num_components(instr
->src
[i
].src
);
1379 midgard_vector_alu_src alu_src
= blank_alu_src
;
1381 switch (instr
->src
[i
].src_type
) {
1382 case nir_tex_src_coord
: {
1383 if (instr
->sampler_dim
== GLSL_SAMPLER_DIM_CUBE
) {
1384 /* For cubemaps, we need to load coords into
1385 * special r27, and then use a special ld/st op
1386 * to select the face and copy the xy into the
1387 * texture register */
1389 alu_src
.swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_X
);
1391 midgard_instruction move
= v_fmov(index
, alu_src
, SSA_FIXED_REGISTER(27));
1392 emit_mir_instruction(ctx
, move
);
1394 midgard_instruction st
= m_st_cubemap_coords(reg
, 0);
1395 st
.load_store
.unknown
= 0x24; /* XXX: What is this? */
1396 st
.load_store
.mask
= 0x3; /* xy */
1397 st
.load_store
.swizzle
= alu_src
.swizzle
;
1398 emit_mir_instruction(ctx
, st
);
1400 position_swizzle
= swizzle_of(2);
1402 position_swizzle
= alu_src
.swizzle
= swizzle_of(nr_comp
);
1404 midgard_instruction ins
= v_fmov(index
, alu_src
, reg
);
1405 ins
.alu
.mask
= expand_writemask(mask_of(nr_comp
));
1406 emit_mir_instruction(ctx
, ins
);
1413 case nir_tex_src_bias
:
1414 case nir_tex_src_lod
: {
1415 /* To keep RA simple, we put the bias/LOD into the w
1416 * component of the input source, which is otherwise in xy */
1418 alu_src
.swizzle
= SWIZZLE_XXXX
;
1420 midgard_instruction ins
= v_fmov(index
, alu_src
, reg
);
1421 ins
.alu
.mask
= expand_writemask(1 << COMPONENT_W
);
1422 emit_mir_instruction(ctx
, ins
);
1427 unreachable("Unknown texture source type\n");
1431 /* No helper to build texture words -- we do it all here */
1432 midgard_instruction ins
= {
1433 .type
= TAG_TEXTURE_4
,
1435 .op
= midgard_tex_op(instr
->op
),
1436 .format
= midgard_tex_format(instr
->sampler_dim
),
1437 .texture_handle
= texture_index
,
1438 .sampler_handle
= sampler_index
,
1440 /* TODO: Regalloc it in */
1441 .swizzle
= SWIZZLE_XYZW
,
1446 .in_reg_swizzle
= position_swizzle
,
1454 /* Set registers to read and write from the same place */
1455 ins
.texture
.in_reg_select
= in_reg
;
1456 ins
.texture
.out_reg_select
= out_reg
;
1458 /* Setup bias/LOD if necessary. Only register mode support right now.
1459 * TODO: Immediate mode for performance gains */
1461 if (instr
->op
== nir_texop_txb
|| instr
->op
== nir_texop_txl
) {
1462 ins
.texture
.lod_register
= true;
1464 midgard_tex_register_select sel
= {
1474 memcpy(&packed
, &sel
, sizeof(packed
));
1475 ins
.texture
.bias
= packed
;
1478 emit_mir_instruction(ctx
, ins
);
1480 /* Simultaneously alias the destination and emit a move for it. The move will be eliminated if possible */
1482 int o_reg
= REGISTER_TEXTURE_BASE
+ out_reg
, o_index
= nir_dest_index(ctx
, &instr
->dest
);
1483 alias_ssa(ctx
, o_index
, SSA_FIXED_REGISTER(o_reg
));
1484 ctx
->texture_index
[reg
] = o_index
;
1486 midgard_instruction ins2
= v_fmov(SSA_FIXED_REGISTER(o_reg
), blank_alu_src
, o_index
);
1487 emit_mir_instruction(ctx
, ins2
);
1489 /* Used for .cont and .last hinting */
1490 ctx
->texture_op_count
++;
1494 emit_jump(compiler_context
*ctx
, nir_jump_instr
*instr
)
1496 switch (instr
->type
) {
1497 case nir_jump_break
: {
1498 /* Emit a branch out of the loop */
1499 struct midgard_instruction br
= v_branch(false, false);
1500 br
.branch
.target_type
= TARGET_BREAK
;
1501 br
.branch
.target_break
= ctx
->current_loop_depth
;
1502 emit_mir_instruction(ctx
, br
);
1509 DBG("Unknown jump type %d\n", instr
->type
);
1515 emit_instr(compiler_context
*ctx
, struct nir_instr
*instr
)
1517 switch (instr
->type
) {
1518 case nir_instr_type_load_const
:
1519 emit_load_const(ctx
, nir_instr_as_load_const(instr
));
1522 case nir_instr_type_intrinsic
:
1523 emit_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1526 case nir_instr_type_alu
:
1527 emit_alu(ctx
, nir_instr_as_alu(instr
));
1530 case nir_instr_type_tex
:
1531 emit_tex(ctx
, nir_instr_as_tex(instr
));
1534 case nir_instr_type_jump
:
1535 emit_jump(ctx
, nir_instr_as_jump(instr
));
1538 case nir_instr_type_ssa_undef
:
1543 DBG("Unhandled instruction type\n");
1549 /* ALU instructions can inline or embed constants, which decreases register
1550 * pressure and saves space. */
1552 #define CONDITIONAL_ATTACH(src) { \
1553 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->ssa_args.src + 1); \
1556 attach_constants(ctx, alu, entry, alu->ssa_args.src + 1); \
1557 alu->ssa_args.src = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
1562 inline_alu_constants(compiler_context
*ctx
)
1564 mir_foreach_instr(ctx
, alu
) {
1565 /* Other instructions cannot inline constants */
1566 if (alu
->type
!= TAG_ALU_4
) continue;
1568 /* If there is already a constant here, we can do nothing */
1569 if (alu
->has_constants
) continue;
1571 /* It makes no sense to inline constants on a branch */
1572 if (alu
->compact_branch
|| alu
->prepacked_branch
) continue;
1574 CONDITIONAL_ATTACH(src0
);
1576 if (!alu
->has_constants
) {
1577 CONDITIONAL_ATTACH(src1
)
1578 } else if (!alu
->inline_constant
) {
1579 /* Corner case: _two_ vec4 constants, for instance with a
1580 * csel. For this case, we can only use a constant
1581 * register for one, we'll have to emit a move for the
1582 * other. Note, if both arguments are constants, then
1583 * necessarily neither argument depends on the value of
1584 * any particular register. As the destination register
1585 * will be wiped, that means we can spill the constant
1586 * to the destination register.
1589 void *entry
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, alu
->ssa_args
.src1
+ 1);
1590 unsigned scratch
= alu
->ssa_args
.dest
;
1593 midgard_instruction ins
= v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), blank_alu_src
, scratch
);
1594 attach_constants(ctx
, &ins
, entry
, alu
->ssa_args
.src1
+ 1);
1596 /* Force a break XXX Defer r31 writes */
1597 ins
.unit
= UNIT_VLUT
;
1599 /* Set the source */
1600 alu
->ssa_args
.src1
= scratch
;
1602 /* Inject us -before- the last instruction which set r31 */
1603 mir_insert_instruction_before(mir_prev_op(alu
), ins
);
1609 /* Midgard supports two types of constants, embedded constants (128-bit) and
1610 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
1611 * constants can be demoted to inline constants, for space savings and
1612 * sometimes a performance boost */
1615 embedded_to_inline_constant(compiler_context
*ctx
)
1617 mir_foreach_instr(ctx
, ins
) {
1618 if (!ins
->has_constants
) continue;
1620 if (ins
->ssa_args
.inline_constant
) continue;
1622 /* Blend constants must not be inlined by definition */
1623 if (ins
->has_blend_constant
) continue;
1625 /* src1 cannot be an inline constant due to encoding
1626 * restrictions. So, if possible we try to flip the arguments
1629 int op
= ins
->alu
.op
;
1631 if (ins
->ssa_args
.src0
== SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
1633 /* These ops require an operational change to flip
1634 * their arguments TODO */
1635 case midgard_alu_op_flt
:
1636 case midgard_alu_op_fle
:
1637 case midgard_alu_op_ilt
:
1638 case midgard_alu_op_ile
:
1639 case midgard_alu_op_fcsel
:
1640 case midgard_alu_op_icsel
:
1641 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props
[op
].name
);
1646 if (alu_opcode_props
[op
].props
& OP_COMMUTES
) {
1647 /* Flip the SSA numbers */
1648 ins
->ssa_args
.src0
= ins
->ssa_args
.src1
;
1649 ins
->ssa_args
.src1
= SSA_FIXED_REGISTER(REGISTER_CONSTANT
);
1651 /* And flip the modifiers */
1655 src_temp
= ins
->alu
.src2
;
1656 ins
->alu
.src2
= ins
->alu
.src1
;
1657 ins
->alu
.src1
= src_temp
;
1661 if (ins
->ssa_args
.src1
== SSA_FIXED_REGISTER(REGISTER_CONSTANT
)) {
1662 /* Extract the source information */
1664 midgard_vector_alu_src
*src
;
1665 int q
= ins
->alu
.src2
;
1666 midgard_vector_alu_src
*m
= (midgard_vector_alu_src
*) &q
;
1669 /* Component is from the swizzle, e.g. r26.w -> w component. TODO: What if x is masked out? */
1670 int component
= src
->swizzle
& 3;
1672 /* Scale constant appropriately, if we can legally */
1673 uint16_t scaled_constant
= 0;
1675 if (midgard_is_integer_op(op
)) {
1676 unsigned int *iconstants
= (unsigned int *) ins
->constants
;
1677 scaled_constant
= (uint16_t) iconstants
[component
];
1679 /* Constant overflow after resize */
1680 if (scaled_constant
!= iconstants
[component
])
1683 float original
= (float) ins
->constants
[component
];
1684 scaled_constant
= _mesa_float_to_half(original
);
1686 /* Check for loss of precision. If this is
1687 * mediump, we don't care, but for a highp
1688 * shader, we need to pay attention. NIR
1689 * doesn't yet tell us which mode we're in!
1690 * Practically this prevents most constants
1691 * from being inlined, sadly. */
1693 float fp32
= _mesa_half_to_float(scaled_constant
);
1695 if (fp32
!= original
)
1699 /* We don't know how to handle these with a constant */
1701 if (src
->mod
|| src
->half
|| src
->rep_low
|| src
->rep_high
) {
1702 DBG("Bailing inline constant...\n");
1706 /* Make sure that the constant is not itself a
1707 * vector by checking if all accessed values
1708 * (by the swizzle) are the same. */
1710 uint32_t *cons
= (uint32_t *) ins
->constants
;
1711 uint32_t value
= cons
[component
];
1713 bool is_vector
= false;
1714 unsigned mask
= effective_writemask(&ins
->alu
);
1716 for (int c
= 1; c
< 4; ++c
) {
1717 /* We only care if this component is actually used */
1718 if (!(mask
& (1 << c
)))
1721 uint32_t test
= cons
[(src
->swizzle
>> (2 * c
)) & 3];
1723 if (test
!= value
) {
1732 /* Get rid of the embedded constant */
1733 ins
->has_constants
= false;
1734 ins
->ssa_args
.src1
= SSA_UNUSED_0
;
1735 ins
->ssa_args
.inline_constant
= true;
1736 ins
->inline_constant
= scaled_constant
;
1741 /* Map normal SSA sources to other SSA sources / fixed registers (like
1745 map_ssa_to_alias(compiler_context
*ctx
, int *ref
)
1747 /* Sign is used quite deliberately for unused */
1751 unsigned int alias
= (uintptr_t) _mesa_hash_table_u64_search(ctx
->ssa_to_alias
, *ref
+ 1);
1754 /* Remove entry in leftovers to avoid a redunant fmov */
1756 struct set_entry
*leftover
= _mesa_set_search(ctx
->leftover_ssa_to_alias
, ((void *) (uintptr_t) (*ref
+ 1)));
1759 _mesa_set_remove(ctx
->leftover_ssa_to_alias
, leftover
);
1761 /* Assign the alias map */
1767 /* Basic dead code elimination on the MIR itself, which cleans up e.g. the
1768 * texture pipeline */
1771 midgard_opt_dead_code_eliminate(compiler_context
*ctx
, midgard_block
*block
)
1773 bool progress
= false;
1775 mir_foreach_instr_in_block_safe(block
, ins
) {
1776 if (ins
->type
!= TAG_ALU_4
) continue;
1777 if (ins
->compact_branch
) continue;
1779 if (ins
->ssa_args
.dest
>= SSA_FIXED_MINIMUM
) continue;
1780 if (mir_is_live_after(ctx
, block
, ins
, ins
->ssa_args
.dest
)) continue;
1782 mir_remove_instruction(ins
);
1789 /* Dead code elimination for branches at the end of a block - only one branch
1790 * per block is legal semantically */
1793 midgard_opt_cull_dead_branch(compiler_context
*ctx
, midgard_block
*block
)
1795 bool branched
= false;
1797 mir_foreach_instr_in_block_safe(block
, ins
) {
1798 if (!midgard_is_branch_unit(ins
->unit
)) continue;
1800 /* We ignore prepacked branches since the fragment epilogue is
1801 * just generally special */
1802 if (ins
->prepacked_branch
) continue;
1804 /* Discards are similarly special and may not correspond to the
1807 if (ins
->branch
.target_type
== TARGET_DISCARD
) continue;
1810 /* We already branched, so this is dead */
1811 mir_remove_instruction(ins
);
1819 mir_nontrivial_mod(midgard_vector_alu_src src
, bool is_int
, unsigned mask
)
1822 if (!is_int
&& src
.mod
) return true;
1825 for (unsigned c
= 0; c
< 4; ++c
) {
1826 if (!(mask
& (1 << c
))) continue;
1827 if (((src
.swizzle
>> (2*c
)) & 3) != c
) return true;
1834 mir_nontrivial_source2_mod(midgard_instruction
*ins
)
1836 unsigned mask
= squeeze_writemask(ins
->alu
.mask
);
1837 bool is_int
= midgard_is_integer_op(ins
->alu
.op
);
1839 midgard_vector_alu_src src2
=
1840 vector_alu_from_unsigned(ins
->alu
.src2
);
1842 return mir_nontrivial_mod(src2
, is_int
, mask
);
1846 mir_nontrivial_outmod(midgard_instruction
*ins
)
1848 bool is_int
= midgard_is_integer_op(ins
->alu
.op
);
1849 unsigned mod
= ins
->alu
.outmod
;
1852 return mod
!= midgard_outmod_int_wrap
;
1854 return mod
!= midgard_outmod_none
;
1858 midgard_opt_copy_prop(compiler_context
*ctx
, midgard_block
*block
)
1860 bool progress
= false;
1862 mir_foreach_instr_in_block_safe(block
, ins
) {
1863 if (ins
->type
!= TAG_ALU_4
) continue;
1864 if (!OP_IS_MOVE(ins
->alu
.op
)) continue;
1866 unsigned from
= ins
->ssa_args
.src1
;
1867 unsigned to
= ins
->ssa_args
.dest
;
1869 /* We only work on pure SSA */
1871 if (to
>= SSA_FIXED_MINIMUM
) continue;
1872 if (from
>= SSA_FIXED_MINIMUM
) continue;
1873 if (to
>= ctx
->func
->impl
->ssa_alloc
) continue;
1874 if (from
>= ctx
->func
->impl
->ssa_alloc
) continue;
1876 /* Constant propagation is not handled here, either */
1877 if (ins
->ssa_args
.inline_constant
) continue;
1878 if (ins
->has_constants
) continue;
1880 if (mir_nontrivial_source2_mod(ins
)) continue;
1881 if (mir_nontrivial_outmod(ins
)) continue;
1883 /* We're clear -- rewrite */
1884 mir_rewrite_index_src(ctx
, to
, from
);
1885 mir_remove_instruction(ins
);
1892 /* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
1893 * the move can be propagated away entirely */
1896 mir_compose_float_outmod(midgard_outmod_float
*outmod
, midgard_outmod_float comp
)
1899 if (comp
== midgard_outmod_none
)
1902 if (*outmod
== midgard_outmod_none
) {
1907 /* TODO: Compose rules */
1912 midgard_opt_pos_propagate(compiler_context
*ctx
, midgard_block
*block
)
1914 bool progress
= false;
1916 mir_foreach_instr_in_block_safe(block
, ins
) {
1917 if (ins
->type
!= TAG_ALU_4
) continue;
1918 if (ins
->alu
.op
!= midgard_alu_op_fmov
) continue;
1919 if (ins
->alu
.outmod
!= midgard_outmod_pos
) continue;
1921 /* TODO: Registers? */
1922 unsigned src
= ins
->ssa_args
.src1
;
1923 if (src
>= ctx
->func
->impl
->ssa_alloc
) continue;
1924 assert(!mir_has_multiple_writes(ctx
, src
));
1926 /* There might be a source modifier, too */
1927 if (mir_nontrivial_source2_mod(ins
)) continue;
1929 /* Backpropagate the modifier */
1930 mir_foreach_instr_in_block_from_rev(block
, v
, mir_prev_op(ins
)) {
1931 if (v
->type
!= TAG_ALU_4
) continue;
1932 if (v
->ssa_args
.dest
!= src
) continue;
1934 /* Can we even take a float outmod? */
1935 if (midgard_is_integer_out_op(v
->alu
.op
)) continue;
1937 midgard_outmod_float temp
= v
->alu
.outmod
;
1938 progress
|= mir_compose_float_outmod(&temp
, ins
->alu
.outmod
);
1940 /* Throw in the towel.. */
1941 if (!progress
) break;
1943 /* Otherwise, transfer the modifier */
1944 v
->alu
.outmod
= temp
;
1945 ins
->alu
.outmod
= midgard_outmod_none
;
1955 midgard_opt_copy_prop_tex(compiler_context
*ctx
, midgard_block
*block
)
1957 bool progress
= false;
1959 mir_foreach_instr_in_block_safe(block
, ins
) {
1960 if (ins
->type
!= TAG_ALU_4
) continue;
1961 if (!OP_IS_MOVE(ins
->alu
.op
)) continue;
1963 unsigned from
= ins
->ssa_args
.src1
;
1964 unsigned to
= ins
->ssa_args
.dest
;
1966 /* Make sure it's simple enough for us to handle */
1968 if (from
>= SSA_FIXED_MINIMUM
) continue;
1969 if (from
>= ctx
->func
->impl
->ssa_alloc
) continue;
1970 if (to
< SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE
)) continue;
1971 if (to
> SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE
+ 1)) continue;
1973 bool eliminated
= false;
1975 mir_foreach_instr_in_block_from_rev(block
, v
, mir_prev_op(ins
)) {
1976 /* The texture registers are not SSA so be careful.
1977 * Conservatively, just stop if we hit a texture op
1978 * (even if it may not write) to where we are */
1980 if (v
->type
!= TAG_ALU_4
)
1983 if (v
->ssa_args
.dest
== from
) {
1984 /* We don't want to track partial writes ... */
1985 if (v
->alu
.mask
== 0xF) {
1986 v
->ssa_args
.dest
= to
;
1995 mir_remove_instruction(ins
);
1997 progress
|= eliminated
;
2003 /* The following passes reorder MIR instructions to enable better scheduling */
2006 midgard_pair_load_store(compiler_context
*ctx
, midgard_block
*block
)
2008 mir_foreach_instr_in_block_safe(block
, ins
) {
2009 if (ins
->type
!= TAG_LOAD_STORE_4
) continue;
2011 /* We've found a load/store op. Check if next is also load/store. */
2012 midgard_instruction
*next_op
= mir_next_op(ins
);
2013 if (&next_op
->link
!= &block
->instructions
) {
2014 if (next_op
->type
== TAG_LOAD_STORE_4
) {
2015 /* If so, we're done since we're a pair */
2016 ins
= mir_next_op(ins
);
2020 /* Maximum search distance to pair, to avoid register pressure disasters */
2021 int search_distance
= 8;
2023 /* Otherwise, we have an orphaned load/store -- search for another load */
2024 mir_foreach_instr_in_block_from(block
, c
, mir_next_op(ins
)) {
2025 /* Terminate search if necessary */
2026 if (!(search_distance
--)) break;
2028 if (c
->type
!= TAG_LOAD_STORE_4
) continue;
2030 /* Stores cannot be reordered, since they have
2031 * dependencies. For the same reason, indirect
2032 * loads cannot be reordered as their index is
2033 * loaded in r27.w */
2035 if (OP_IS_STORE(c
->load_store
.op
)) continue;
2037 /* It appears the 0x800 bit is set whenever a
2038 * load is direct, unset when it is indirect.
2039 * Skip indirect loads. */
2041 if (!(c
->load_store
.unknown
& 0x800)) continue;
2043 /* We found one! Move it up to pair and remove it from the old location */
2045 mir_insert_instruction_before(ins
, *c
);
2046 mir_remove_instruction(c
);
2054 /* If there are leftovers after the below pass, emit actual fmov
2055 * instructions for the slow-but-correct path */
2058 emit_leftover_move(compiler_context
*ctx
)
2060 set_foreach(ctx
->leftover_ssa_to_alias
, leftover
) {
2061 int base
= ((uintptr_t) leftover
->key
) - 1;
2064 map_ssa_to_alias(ctx
, &mapped
);
2065 EMIT(fmov
, mapped
, blank_alu_src
, base
);
2070 actualise_ssa_to_alias(compiler_context
*ctx
)
2072 mir_foreach_instr(ctx
, ins
) {
2073 map_ssa_to_alias(ctx
, &ins
->ssa_args
.src0
);
2074 map_ssa_to_alias(ctx
, &ins
->ssa_args
.src1
);
2077 emit_leftover_move(ctx
);
2081 emit_fragment_epilogue(compiler_context
*ctx
)
2083 /* Special case: writing out constants requires us to include the move
2084 * explicitly now, so shove it into r0 */
2086 void *constant_value
= _mesa_hash_table_u64_search(ctx
->ssa_constants
, ctx
->fragment_output
+ 1);
2088 if (constant_value
) {
2089 midgard_instruction ins
= v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT
), blank_alu_src
, SSA_FIXED_REGISTER(0));
2090 attach_constants(ctx
, &ins
, constant_value
, ctx
->fragment_output
+ 1);
2091 emit_mir_instruction(ctx
, ins
);
2094 /* Perform the actual fragment writeout. We have two writeout/branch
2095 * instructions, forming a loop until writeout is successful as per the
2096 * docs. TODO: gl_FragDepth */
2098 EMIT(alu_br_compact_cond
, midgard_jmp_writeout_op_writeout
, TAG_ALU_4
, 0, midgard_condition_always
);
2099 EMIT(alu_br_compact_cond
, midgard_jmp_writeout_op_writeout
, TAG_ALU_4
, -1, midgard_condition_always
);
2102 /* For the blend epilogue, we need to convert the blended fragment vec4 (stored
2103 * in r0) to a RGBA8888 value by scaling and type converting. We then output it
2104 * with the int8 analogue to the fragment epilogue */
2107 emit_blend_epilogue(compiler_context
*ctx
)
2109 /* vmul.fmul.none.fulllow hr48, r0, #255 */
2111 midgard_instruction scale
= {
2114 .inline_constant
= _mesa_float_to_half(255.0),
2116 .src0
= SSA_FIXED_REGISTER(0),
2117 .src1
= SSA_UNUSED_0
,
2118 .dest
= SSA_FIXED_REGISTER(24),
2119 .inline_constant
= true
2122 .op
= midgard_alu_op_fmul
,
2123 .reg_mode
= midgard_reg_mode_32
,
2124 .dest_override
= midgard_dest_override_lower
,
2126 .src1
= vector_alu_srco_unsigned(blank_alu_src
),
2127 .src2
= vector_alu_srco_unsigned(blank_alu_src
),
2131 emit_mir_instruction(ctx
, scale
);
2133 /* vadd.f2u8.pos.low hr0, hr48, #0 */
2135 midgard_vector_alu_src alu_src
= blank_alu_src
;
2136 alu_src
.half
= true;
2138 midgard_instruction f2u8
= {
2141 .src0
= SSA_FIXED_REGISTER(24),
2142 .src1
= SSA_UNUSED_0
,
2143 .dest
= SSA_FIXED_REGISTER(0),
2144 .inline_constant
= true
2147 .op
= midgard_alu_op_f2u8
,
2148 .reg_mode
= midgard_reg_mode_16
,
2149 .dest_override
= midgard_dest_override_lower
,
2150 .outmod
= midgard_outmod_pos
,
2152 .src1
= vector_alu_srco_unsigned(alu_src
),
2153 .src2
= vector_alu_srco_unsigned(blank_alu_src
),
2157 emit_mir_instruction(ctx
, f2u8
);
2159 /* vmul.imov.quarter r0, r0, r0 */
2161 midgard_instruction imov_8
= {
2164 .src0
= SSA_UNUSED_1
,
2165 .src1
= SSA_FIXED_REGISTER(0),
2166 .dest
= SSA_FIXED_REGISTER(0),
2169 .op
= midgard_alu_op_imov
,
2170 .reg_mode
= midgard_reg_mode_8
,
2171 .dest_override
= midgard_dest_override_none
,
2172 .outmod
= midgard_outmod_int_wrap
,
2174 .src1
= vector_alu_srco_unsigned(blank_alu_src
),
2175 .src2
= vector_alu_srco_unsigned(blank_alu_src
),
2179 /* Emit branch epilogue with the 8-bit move as the source */
2181 emit_mir_instruction(ctx
, imov_8
);
2182 EMIT(alu_br_compact_cond
, midgard_jmp_writeout_op_writeout
, TAG_ALU_4
, 0, midgard_condition_always
);
2184 emit_mir_instruction(ctx
, imov_8
);
2185 EMIT(alu_br_compact_cond
, midgard_jmp_writeout_op_writeout
, TAG_ALU_4
, -1, midgard_condition_always
);
2188 static midgard_block
*
2189 emit_block(compiler_context
*ctx
, nir_block
*block
)
2191 midgard_block
*this_block
= calloc(sizeof(midgard_block
), 1);
2192 list_addtail(&this_block
->link
, &ctx
->blocks
);
2194 this_block
->is_scheduled
= false;
2197 ctx
->texture_index
[0] = -1;
2198 ctx
->texture_index
[1] = -1;
2200 /* Add us as a successor to the block we are following */
2201 if (ctx
->current_block
)
2202 midgard_block_add_successor(ctx
->current_block
, this_block
);
2204 /* Set up current block */
2205 list_inithead(&this_block
->instructions
);
2206 ctx
->current_block
= this_block
;
2208 nir_foreach_instr(instr
, block
) {
2209 emit_instr(ctx
, instr
);
2210 ++ctx
->instruction_count
;
2213 inline_alu_constants(ctx
);
2214 embedded_to_inline_constant(ctx
);
2216 /* Perform heavylifting for aliasing */
2217 actualise_ssa_to_alias(ctx
);
2219 midgard_pair_load_store(ctx
, this_block
);
2221 /* Append fragment shader epilogue (value writeout) */
2222 if (ctx
->stage
== MESA_SHADER_FRAGMENT
) {
2223 if (block
== nir_impl_last_block(ctx
->func
->impl
)) {
2225 emit_blend_epilogue(ctx
);
2227 emit_fragment_epilogue(ctx
);
2231 if (block
== nir_start_block(ctx
->func
->impl
))
2232 ctx
->initial_block
= this_block
;
2234 if (block
== nir_impl_last_block(ctx
->func
->impl
))
2235 ctx
->final_block
= this_block
;
2237 /* Allow the next control flow to access us retroactively, for
2239 ctx
->current_block
= this_block
;
2241 /* Document the fallthrough chain */
2242 ctx
->previous_source_block
= this_block
;
2247 static midgard_block
*emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
);
2250 emit_if(struct compiler_context
*ctx
, nir_if
*nif
)
2252 /* Conditional branches expect the condition in r31.w; emit a move for
2253 * that in the _previous_ block (which is the current block). */
2254 emit_condition(ctx
, &nif
->condition
, true, COMPONENT_X
);
2256 /* Speculatively emit the branch, but we can't fill it in until later */
2257 EMIT(branch
, true, true);
2258 midgard_instruction
*then_branch
= mir_last_in_block(ctx
->current_block
);
2260 /* Emit the two subblocks */
2261 midgard_block
*then_block
= emit_cf_list(ctx
, &nif
->then_list
);
2263 /* Emit a jump from the end of the then block to the end of the else */
2264 EMIT(branch
, false, false);
2265 midgard_instruction
*then_exit
= mir_last_in_block(ctx
->current_block
);
2267 /* Emit second block, and check if it's empty */
2269 int else_idx
= ctx
->block_count
;
2270 int count_in
= ctx
->instruction_count
;
2271 midgard_block
*else_block
= emit_cf_list(ctx
, &nif
->else_list
);
2272 int after_else_idx
= ctx
->block_count
;
2274 /* Now that we have the subblocks emitted, fix up the branches */
2279 if (ctx
->instruction_count
== count_in
) {
2280 /* The else block is empty, so don't emit an exit jump */
2281 mir_remove_instruction(then_exit
);
2282 then_branch
->branch
.target_block
= after_else_idx
;
2284 then_branch
->branch
.target_block
= else_idx
;
2285 then_exit
->branch
.target_block
= after_else_idx
;
2290 emit_loop(struct compiler_context
*ctx
, nir_loop
*nloop
)
2292 /* Remember where we are */
2293 midgard_block
*start_block
= ctx
->current_block
;
2295 /* Allocate a loop number, growing the current inner loop depth */
2296 int loop_idx
= ++ctx
->current_loop_depth
;
2298 /* Get index from before the body so we can loop back later */
2299 int start_idx
= ctx
->block_count
;
2301 /* Emit the body itself */
2302 emit_cf_list(ctx
, &nloop
->body
);
2304 /* Branch back to loop back */
2305 struct midgard_instruction br_back
= v_branch(false, false);
2306 br_back
.branch
.target_block
= start_idx
;
2307 emit_mir_instruction(ctx
, br_back
);
2309 /* Mark down that branch in the graph. Note that we're really branching
2310 * to the block *after* we started in. TODO: Why doesn't the branch
2311 * itself have an off-by-one then...? */
2312 midgard_block_add_successor(ctx
->current_block
, start_block
->successors
[0]);
2314 /* Find the index of the block about to follow us (note: we don't add
2315 * one; blocks are 0-indexed so we get a fencepost problem) */
2316 int break_block_idx
= ctx
->block_count
;
2318 /* Fix up the break statements we emitted to point to the right place,
2319 * now that we can allocate a block number for them */
2321 list_for_each_entry_from(struct midgard_block
, block
, start_block
, &ctx
->blocks
, link
) {
2322 mir_foreach_instr_in_block(block
, ins
) {
2323 if (ins
->type
!= TAG_ALU_4
) continue;
2324 if (!ins
->compact_branch
) continue;
2325 if (ins
->prepacked_branch
) continue;
2327 /* We found a branch -- check the type to see if we need to do anything */
2328 if (ins
->branch
.target_type
!= TARGET_BREAK
) continue;
2330 /* It's a break! Check if it's our break */
2331 if (ins
->branch
.target_break
!= loop_idx
) continue;
2333 /* Okay, cool, we're breaking out of this loop.
2334 * Rewrite from a break to a goto */
2336 ins
->branch
.target_type
= TARGET_GOTO
;
2337 ins
->branch
.target_block
= break_block_idx
;
2341 /* Now that we've finished emitting the loop, free up the depth again
2342 * so we play nice with recursion amid nested loops */
2343 --ctx
->current_loop_depth
;
2346 static midgard_block
*
2347 emit_cf_list(struct compiler_context
*ctx
, struct exec_list
*list
)
2349 midgard_block
*start_block
= NULL
;
2351 foreach_list_typed(nir_cf_node
, node
, node
, list
) {
2352 switch (node
->type
) {
2353 case nir_cf_node_block
: {
2354 midgard_block
*block
= emit_block(ctx
, nir_cf_node_as_block(node
));
2357 start_block
= block
;
2362 case nir_cf_node_if
:
2363 emit_if(ctx
, nir_cf_node_as_if(node
));
2366 case nir_cf_node_loop
:
2367 emit_loop(ctx
, nir_cf_node_as_loop(node
));
2370 case nir_cf_node_function
:
2379 /* Due to lookahead, we need to report the first tag executed in the command
2380 * stream and in branch targets. An initial block might be empty, so iterate
2381 * until we find one that 'works' */
2384 midgard_get_first_tag_from_block(compiler_context
*ctx
, unsigned block_idx
)
2386 midgard_block
*initial_block
= mir_get_block(ctx
, block_idx
);
2388 unsigned first_tag
= 0;
2391 midgard_bundle
*initial_bundle
= util_dynarray_element(&initial_block
->bundles
, midgard_bundle
, 0);
2393 if (initial_bundle
) {
2394 first_tag
= initial_bundle
->tag
;
2398 /* Initial block is empty, try the next block */
2399 initial_block
= list_first_entry(&(initial_block
->link
), midgard_block
, link
);
2400 } while(initial_block
!= NULL
);
2407 midgard_compile_shader_nir(nir_shader
*nir
, midgard_program
*program
, bool is_blend
)
2409 struct util_dynarray
*compiled
= &program
->compiled
;
2411 midgard_debug
= debug_get_option_midgard_debug();
2413 compiler_context ictx
= {
2415 .stage
= nir
->info
.stage
,
2417 .is_blend
= is_blend
,
2418 .blend_constant_offset
= -1,
2420 .alpha_ref
= program
->alpha_ref
2423 compiler_context
*ctx
= &ictx
;
2425 /* TODO: Decide this at runtime */
2426 ctx
->uniform_cutoff
= 8;
2428 /* Initialize at a global (not block) level hash tables */
2430 ctx
->ssa_constants
= _mesa_hash_table_u64_create(NULL
);
2431 ctx
->ssa_to_alias
= _mesa_hash_table_u64_create(NULL
);
2432 ctx
->hash_to_temp
= _mesa_hash_table_u64_create(NULL
);
2433 ctx
->sysval_to_id
= _mesa_hash_table_u64_create(NULL
);
2434 ctx
->leftover_ssa_to_alias
= _mesa_set_create(NULL
, _mesa_hash_pointer
, _mesa_key_pointer_equal
);
2436 /* Record the varying mapping for the command stream's bookkeeping */
2438 struct exec_list
*varyings
=
2439 ctx
->stage
== MESA_SHADER_VERTEX
? &nir
->outputs
: &nir
->inputs
;
2441 unsigned max_varying
= 0;
2442 nir_foreach_variable(var
, varyings
) {
2443 unsigned loc
= var
->data
.driver_location
;
2444 unsigned sz
= glsl_type_size(var
->type
, FALSE
);
2446 for (int c
= 0; c
< sz
; ++c
) {
2447 program
->varyings
[loc
+ c
] = var
->data
.location
+ c
;
2448 max_varying
= MAX2(max_varying
, loc
+ c
);
2452 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2453 * (so we don't accidentally duplicate the epilogue since mesa/st has
2454 * messed with our I/O quite a bit already) */
2456 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2458 if (ctx
->stage
== MESA_SHADER_VERTEX
)
2459 NIR_PASS_V(nir
, nir_lower_viewport_transform
);
2461 NIR_PASS_V(nir
, nir_lower_var_copies
);
2462 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2463 NIR_PASS_V(nir
, nir_split_var_copies
);
2464 NIR_PASS_V(nir
, nir_lower_var_copies
);
2465 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
2466 NIR_PASS_V(nir
, nir_lower_var_copies
);
2467 NIR_PASS_V(nir
, nir_lower_vars_to_ssa
);
2469 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, glsl_type_size
, 0);
2471 /* Optimisation passes */
2475 if (midgard_debug
& MIDGARD_DBG_SHADERS
) {
2476 nir_print_shader(nir
, stdout
);
2479 /* Assign sysvals and counts, now that we're sure
2480 * (post-optimisation) */
2482 midgard_nir_assign_sysvals(ctx
, nir
);
2484 program
->uniform_count
= nir
->num_uniforms
;
2485 program
->sysval_count
= ctx
->sysval_count
;
2486 memcpy(program
->sysvals
, ctx
->sysvals
, sizeof(ctx
->sysvals
[0]) * ctx
->sysval_count
);
2488 program
->attribute_count
= (ctx
->stage
== MESA_SHADER_VERTEX
) ? nir
->num_inputs
: 0;
2489 program
->varying_count
= max_varying
+ 1; /* Fencepost off-by-one */
2491 nir_foreach_function(func
, nir
) {
2495 list_inithead(&ctx
->blocks
);
2496 ctx
->block_count
= 0;
2499 emit_cf_list(ctx
, &func
->impl
->body
);
2500 emit_block(ctx
, func
->impl
->end_block
);
2502 break; /* TODO: Multi-function shaders */
2505 util_dynarray_init(compiled
, NULL
);
2507 /* MIR-level optimizations */
2509 bool progress
= false;
2514 mir_foreach_block(ctx
, block
) {
2515 progress
|= midgard_opt_pos_propagate(ctx
, block
);
2516 progress
|= midgard_opt_copy_prop(ctx
, block
);
2517 progress
|= midgard_opt_copy_prop_tex(ctx
, block
);
2518 progress
|= midgard_opt_dead_code_eliminate(ctx
, block
);
2522 /* Nested control-flow can result in dead branches at the end of the
2523 * block. This messes with our analysis and is just dead code, so cull
2525 mir_foreach_block(ctx
, block
) {
2526 midgard_opt_cull_dead_branch(ctx
, block
);
2530 schedule_program(ctx
);
2532 /* Now that all the bundles are scheduled and we can calculate block
2533 * sizes, emit actual branch instructions rather than placeholders */
2535 int br_block_idx
= 0;
2537 mir_foreach_block(ctx
, block
) {
2538 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2539 for (int c
= 0; c
< bundle
->instruction_count
; ++c
) {
2540 midgard_instruction
*ins
= bundle
->instructions
[c
];
2542 if (!midgard_is_branch_unit(ins
->unit
)) continue;
2544 if (ins
->prepacked_branch
) continue;
2546 /* Parse some basic branch info */
2547 bool is_compact
= ins
->unit
== ALU_ENAB_BR_COMPACT
;
2548 bool is_conditional
= ins
->branch
.conditional
;
2549 bool is_inverted
= ins
->branch
.invert_conditional
;
2550 bool is_discard
= ins
->branch
.target_type
== TARGET_DISCARD
;
2552 /* Determine the block we're jumping to */
2553 int target_number
= ins
->branch
.target_block
;
2555 /* Report the destination tag */
2556 int dest_tag
= is_discard
? 0 : midgard_get_first_tag_from_block(ctx
, target_number
);
2558 /* Count up the number of quadwords we're
2559 * jumping over = number of quadwords until
2560 * (br_block_idx, target_number) */
2562 int quadword_offset
= 0;
2565 /* Jump to the end of the shader. We
2566 * need to include not only the
2567 * following blocks, but also the
2568 * contents of our current block (since
2569 * discard can come in the middle of
2572 midgard_block
*blk
= mir_get_block(ctx
, br_block_idx
+ 1);
2574 for (midgard_bundle
*bun
= bundle
+ 1; bun
< (midgard_bundle
*)((char*) block
->bundles
.data
+ block
->bundles
.size
); ++bun
) {
2575 quadword_offset
+= quadword_size(bun
->tag
);
2578 mir_foreach_block_from(ctx
, blk
, b
) {
2579 quadword_offset
+= b
->quadword_count
;
2582 } else if (target_number
> br_block_idx
) {
2585 for (int idx
= br_block_idx
+ 1; idx
< target_number
; ++idx
) {
2586 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2589 quadword_offset
+= blk
->quadword_count
;
2592 /* Jump backwards */
2594 for (int idx
= br_block_idx
; idx
>= target_number
; --idx
) {
2595 midgard_block
*blk
= mir_get_block(ctx
, idx
);
2598 quadword_offset
-= blk
->quadword_count
;
2602 /* Unconditional extended branches (far jumps)
2603 * have issues, so we always use a conditional
2604 * branch, setting the condition to always for
2605 * unconditional. For compact unconditional
2606 * branches, cond isn't used so it doesn't
2607 * matter what we pick. */
2609 midgard_condition cond
=
2610 !is_conditional
? midgard_condition_always
:
2611 is_inverted
? midgard_condition_false
:
2612 midgard_condition_true
;
2614 midgard_jmp_writeout_op op
=
2615 is_discard
? midgard_jmp_writeout_op_discard
:
2616 (is_compact
&& !is_conditional
) ? midgard_jmp_writeout_op_branch_uncond
:
2617 midgard_jmp_writeout_op_branch_cond
;
2620 midgard_branch_extended branch
=
2621 midgard_create_branch_extended(
2626 memcpy(&ins
->branch_extended
, &branch
, sizeof(branch
));
2627 } else if (is_conditional
|| is_discard
) {
2628 midgard_branch_cond branch
= {
2630 .dest_tag
= dest_tag
,
2631 .offset
= quadword_offset
,
2635 assert(branch
.offset
== quadword_offset
);
2637 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2639 assert(op
== midgard_jmp_writeout_op_branch_uncond
);
2641 midgard_branch_uncond branch
= {
2643 .dest_tag
= dest_tag
,
2644 .offset
= quadword_offset
,
2648 assert(branch
.offset
== quadword_offset
);
2650 memcpy(&ins
->br_compact
, &branch
, sizeof(branch
));
2658 /* Emit flat binary from the instruction arrays. Iterate each block in
2659 * sequence. Save instruction boundaries such that lookahead tags can
2660 * be assigned easily */
2662 /* Cache _all_ bundles in source order for lookahead across failed branches */
2664 int bundle_count
= 0;
2665 mir_foreach_block(ctx
, block
) {
2666 bundle_count
+= block
->bundles
.size
/ sizeof(midgard_bundle
);
2668 midgard_bundle
**source_order_bundles
= malloc(sizeof(midgard_bundle
*) * bundle_count
);
2670 mir_foreach_block(ctx
, block
) {
2671 util_dynarray_foreach(&block
->bundles
, midgard_bundle
, bundle
) {
2672 source_order_bundles
[bundle_idx
++] = bundle
;
2676 int current_bundle
= 0;
2678 /* Midgard prefetches instruction types, so during emission we
2679 * need to lookahead. Unless this is the last instruction, in
2680 * which we return 1. Or if this is the second to last and the
2681 * last is an ALU, then it's also 1... */
2683 mir_foreach_block(ctx
, block
) {
2684 mir_foreach_bundle_in_block(block
, bundle
) {
2687 if (current_bundle
+ 1 < bundle_count
) {
2688 uint8_t next
= source_order_bundles
[current_bundle
+ 1]->tag
;
2690 if (!(current_bundle
+ 2 < bundle_count
) && IS_ALU(next
)) {
2697 emit_binary_bundle(ctx
, bundle
, compiled
, lookahead
);
2701 /* TODO: Free deeper */
2702 //util_dynarray_fini(&block->instructions);
2705 free(source_order_bundles
);
2707 /* Report the very first tag executed */
2708 program
->first_tag
= midgard_get_first_tag_from_block(ctx
, 0);
2710 /* Deal with off-by-one related to the fencepost problem */
2711 program
->work_register_count
= ctx
->work_registers
+ 1;
2713 program
->can_discard
= ctx
->can_discard
;
2714 program
->uniform_cutoff
= ctx
->uniform_cutoff
;
2716 program
->blend_patch_offset
= ctx
->blend_constant_offset
;
2718 if (midgard_debug
& MIDGARD_DBG_SHADERS
)
2719 disassemble_midgard(program
->compiled
.data
, program
->compiled
.size
);