2 * Copyright (C) 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 #ifndef _MDG_COMPILER_H
25 #define _MDG_COMPILER_H
29 #include "midgard_compile.h"
31 #include "util/hash_table.h"
32 #include "util/u_dynarray.h"
34 #include "util/list.h"
36 #include "main/mtypes.h"
37 #include "compiler/nir_types.h"
38 #include "compiler/nir/nir.h"
43 /* Target types. Defaults to TARGET_GOTO (the type corresponding directly to
44 * the hardware), hence why that must be zero. TARGET_DISCARD signals this
45 * instruction is actually a discard op. */
48 #define TARGET_BREAK 1
49 #define TARGET_CONTINUE 2
50 #define TARGET_DISCARD 3
52 typedef struct midgard_branch
{
53 /* If conditional, the condition is specified in r31.w */
56 /* For conditionals, if this is true, we branch on FALSE. If false, we branch on TRUE. */
57 bool invert_conditional
;
59 /* Branch targets: the start of a block, the start of a loop (continue), the end of a loop (break). Value is one of TARGET_ */
62 /* The actual target */
70 /* Generic in-memory data type repesenting a single logical instruction, rather
71 * than a single instruction group. This is the preferred form for code gen.
72 * Multiple midgard_insturctions will later be combined during scheduling,
73 * though this is not represented in this structure. Its format bridges
74 * the low-level binary representation with the higher level semantic meaning.
76 * Notably, it allows registers to be specified as block local SSA, for code
77 * emitted before the register allocation pass.
80 typedef struct midgard_instruction
{
81 /* Must be first for casting */
82 struct list_head link
;
84 unsigned type
; /* ALU, load/store, texture */
86 /* Instruction arguments represented as block-local SSA
87 * indices, rather than registers. ~0 means unused. */
91 /* Special fields for an ALU instruction */
92 midgard_reg_info registers
;
94 /* I.e. (1 << alu_bit) */
97 /* When emitting bundle, should this instruction have a break forced
98 * before it? Used for r31 writes which are valid only within a single
99 * bundle and *need* to happen as early as possible... this is a hack,
100 * TODO remove when we have a scheduler */
104 uint32_t constants
[4];
105 uint16_t inline_constant
;
106 bool has_blend_constant
;
107 bool has_inline_constant
;
111 bool prepacked_branch
;
113 /* Kind of a hack, but hint against aggressive DCE */
116 /* Masks in a saneish format. One bit per channel, not packed fancy.
117 * Use this instead of the op specific ones, and switch over at emit
122 /* For ALU ops only: set to true to invert (bitwise NOT) the
123 * destination of an integer-out op. Not imeplemented in hardware but
124 * allows more optimizations */
128 /* Hint for the register allocator not to spill the destination written
129 * from this instruction (because it is a spill/unspill node itself) */
133 /* Generic hint for intra-pass use */
137 midgard_load_store_word load_store
;
138 midgard_vector_alu alu
;
139 midgard_texture_word texture
;
140 midgard_branch_extended branch_extended
;
143 /* General branch, rather than packed br_compact. Higher level
144 * than the other components */
145 midgard_branch branch
;
147 } midgard_instruction
;
149 typedef struct midgard_block
{
150 /* Link to next block. Must be first for mir_get_block */
151 struct list_head link
;
153 /* List of midgard_instructions emitted for the current block */
154 struct list_head instructions
;
156 /* Index of the block in source order */
161 /* List of midgard_bundles emitted (after the scheduler has run) */
162 struct util_dynarray bundles
;
164 /* Number of quadwords _actually_ emitted, as determined after scheduling */
165 unsigned quadword_count
;
167 /* Succeeding blocks. The compiler should not necessarily rely on
168 * source-order traversal */
169 struct midgard_block
*successors
[2];
170 unsigned nr_successors
;
172 struct set
*predecessors
;
174 /* The successors pointer form a graph, and in the case of
175 * complex control flow, this graph has a cycles. To aid
176 * traversal during liveness analysis, we have a visited?
177 * boolean for passes to use as they see fit, provided they
181 /* In liveness analysis, these are live masks (per-component) for
182 * indices for the block. Scalar compilers have the luxury of using
183 * simple bit fields, but for us, liveness is a vector idea. We use
184 * 8-bit to allow finegrained tracking up to vec8. If you're
185 * implementing vec16 on Panfrost... I'm sorry. */
190 typedef struct midgard_bundle
{
191 /* Tag for the overall bundle */
194 /* Instructions contained by the bundle */
195 int instruction_count
;
196 midgard_instruction
*instructions
[5];
198 /* Bundle-wide ALU configuration */
201 bool has_embedded_constants
;
203 bool has_blend_constant
;
206 typedef struct compiler_context
{
208 gl_shader_stage stage
;
210 /* The screen we correspond to */
211 struct midgard_screen
*screen
;
213 /* Is internally a blend shader? Depends on stage == FRAGMENT */
216 /* Tracking for blend constant patching */
217 int blend_constant_offset
;
219 /* Number of bytes used for Thread Local Storage */
222 /* Count of spills and fills for shaderdb */
226 /* Current NIR function */
229 /* Allocated compiler temporary counter */
232 /* Unordered list of midgard_blocks */
234 struct list_head blocks
;
236 /* TODO merge with block_count? */
237 unsigned block_source_count
;
239 /* List of midgard_instructions emitted for the current block */
240 midgard_block
*current_block
;
242 /* If there is a preset after block, use this, otherwise emit_block will create one if NULL */
243 midgard_block
*after_block
;
245 /* The current "depth" of the loop, for disambiguating breaks/continues
246 * when using nested loops */
247 int current_loop_depth
;
249 /* Total number of loops for shader-db */
252 /* Constants which have been loaded, for later inlining */
253 struct hash_table_u64
*ssa_constants
;
255 /* Mapping of hashes computed from NIR indices to the sequential temp indices ultimately used in MIR */
256 struct hash_table_u64
*hash_to_temp
;
260 /* Just the count of the max register used. Higher count => higher
261 * register pressure */
264 /* Used for cont/last hinting. Increase when a tex op is added.
265 * Decrease when a tex op is removed. */
266 int texture_op_count
;
268 /* Mapping of texture register -> SSA index for unaliasing */
269 int texture_index
[2];
271 /* The number of uniforms allowable for the fast path */
274 /* Count of instructions emitted from NIR overall, across all blocks */
275 int instruction_count
;
277 /* Alpha ref value passed in */
280 /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */
281 unsigned sysvals
[MAX_SYSVAL_COUNT
];
282 unsigned sysval_count
;
283 struct hash_table_u64
*sysval_to_id
;
286 /* Helpers for manipulating the above structures (forming the driver IR) */
288 /* Append instruction to end of current block */
290 static inline midgard_instruction
*
291 mir_upload_ins(struct midgard_instruction ins
)
293 midgard_instruction
*heap
= malloc(sizeof(ins
));
294 memcpy(heap
, &ins
, sizeof(ins
));
298 static inline midgard_instruction
*
299 emit_mir_instruction(struct compiler_context
*ctx
, struct midgard_instruction ins
)
301 midgard_instruction
*u
= mir_upload_ins(ins
);
302 list_addtail(&u
->link
, &ctx
->current_block
->instructions
);
306 static inline struct midgard_instruction
*
307 mir_insert_instruction_before(struct midgard_instruction
*tag
, struct midgard_instruction ins
)
309 struct midgard_instruction
*u
= mir_upload_ins(ins
);
310 list_addtail(&u
->link
, &tag
->link
);
315 mir_remove_instruction(struct midgard_instruction
*ins
)
317 list_del(&ins
->link
);
321 static inline midgard_instruction
*
322 mir_prev_op(struct midgard_instruction
*ins
)
324 return list_last_entry(&(ins
->link
), midgard_instruction
, link
);
327 static inline midgard_instruction
*
328 mir_next_op(struct midgard_instruction
*ins
)
330 return list_first_entry(&(ins
->link
), midgard_instruction
, link
);
333 #define mir_foreach_block(ctx, v) \
334 list_for_each_entry(struct midgard_block, v, &ctx->blocks, link)
336 #define mir_foreach_block_from(ctx, from, v) \
337 list_for_each_entry_from(struct midgard_block, v, from, &ctx->blocks, link)
339 #define mir_foreach_instr(ctx, v) \
340 list_for_each_entry(struct midgard_instruction, v, &ctx->current_block->instructions, link)
342 #define mir_foreach_instr_safe(ctx, v) \
343 list_for_each_entry_safe(struct midgard_instruction, v, &ctx->current_block->instructions, link)
345 #define mir_foreach_instr_in_block(block, v) \
346 list_for_each_entry(struct midgard_instruction, v, &block->instructions, link)
347 #define mir_foreach_instr_in_block_rev(block, v) \
348 list_for_each_entry_rev(struct midgard_instruction, v, &block->instructions, link)
350 #define mir_foreach_instr_in_block_safe(block, v) \
351 list_for_each_entry_safe(struct midgard_instruction, v, &block->instructions, link)
353 #define mir_foreach_instr_in_block_safe_rev(block, v) \
354 list_for_each_entry_safe_rev(struct midgard_instruction, v, &block->instructions, link)
356 #define mir_foreach_instr_in_block_from(block, v, from) \
357 list_for_each_entry_from(struct midgard_instruction, v, from, &block->instructions, link)
359 #define mir_foreach_instr_in_block_from_rev(block, v, from) \
360 list_for_each_entry_from_rev(struct midgard_instruction, v, from, &block->instructions, link)
362 #define mir_foreach_bundle_in_block(block, v) \
363 util_dynarray_foreach(&block->bundles, midgard_bundle, v)
365 #define mir_foreach_instr_global(ctx, v) \
366 mir_foreach_block(ctx, v_block) \
367 mir_foreach_instr_in_block(v_block, v)
369 #define mir_foreach_instr_global_safe(ctx, v) \
370 mir_foreach_block(ctx, v_block) \
371 mir_foreach_instr_in_block_safe(v_block, v)
373 #define mir_foreach_successor(blk, v) \
374 struct midgard_block *v; \
375 struct midgard_block **_v; \
376 for (_v = &blk->successors[0], \
378 v != NULL && _v < &blk->successors[2]; \
381 /* Based on set_foreach, expanded with automatic type casts */
383 #define mir_foreach_predecessor(blk, v) \
384 struct set_entry *_entry_##v; \
385 struct midgard_block *v; \
386 for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \
387 v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL); \
388 _entry_##v != NULL; \
389 _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \
390 v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL))
392 #define mir_foreach_src(ins, v) \
393 for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v)
395 static inline midgard_instruction
*
396 mir_last_in_block(struct midgard_block
*block
)
398 return list_last_entry(&block
->instructions
, struct midgard_instruction
, link
);
401 static inline midgard_block
*
402 mir_get_block(compiler_context
*ctx
, int idx
)
404 struct list_head
*lst
= &ctx
->blocks
;
409 return (struct midgard_block
*) lst
;
412 static inline midgard_block
*
413 mir_exit_block(struct compiler_context
*ctx
)
415 midgard_block
*last
= list_last_entry(&ctx
->blocks
,
416 struct midgard_block
, link
);
418 /* The last block must be empty (the exit block) */
419 assert(list_empty(&last
->instructions
));
420 assert(last
->nr_successors
== 0);
426 mir_is_alu_bundle(midgard_bundle
*bundle
)
428 return IS_ALU(bundle
->tag
);
431 /* Registers/SSA are distinguish in the backend by the bottom-most bit */
435 static inline unsigned
436 make_compiler_temp(compiler_context
*ctx
)
438 return (ctx
->func
->impl
->ssa_alloc
+ ctx
->temp_alloc
++) << 1;
441 static inline unsigned
442 make_compiler_temp_reg(compiler_context
*ctx
)
444 return ((ctx
->func
->impl
->reg_alloc
+ ctx
->temp_alloc
++) << 1) | IS_REG
;
447 static inline unsigned
448 nir_src_index(compiler_context
*ctx
, nir_src
*src
)
451 return (src
->ssa
->index
<< 1) | 0;
453 assert(!src
->reg
.indirect
);
454 return (src
->reg
.reg
->index
<< 1) | IS_REG
;
458 static inline unsigned
459 nir_alu_src_index(compiler_context
*ctx
, nir_alu_src
*src
)
461 return nir_src_index(ctx
, &src
->src
);
464 static inline unsigned
465 nir_dest_index(compiler_context
*ctx
, nir_dest
*dst
)
468 return (dst
->ssa
.index
<< 1) | 0;
470 assert(!dst
->reg
.indirect
);
471 return (dst
->reg
.reg
->index
<< 1) | IS_REG
;
477 /* MIR manipulation */
479 void mir_rewrite_index(compiler_context
*ctx
, unsigned old
, unsigned new);
480 void mir_rewrite_index_src(compiler_context
*ctx
, unsigned old
, unsigned new);
481 void mir_rewrite_index_dst(compiler_context
*ctx
, unsigned old
, unsigned new);
482 void mir_rewrite_index_dst_tag(compiler_context
*ctx
, unsigned old
, unsigned new, unsigned tag
);
483 void mir_rewrite_index_dst_single(midgard_instruction
*ins
, unsigned old
, unsigned new);
484 void mir_rewrite_index_src_single(midgard_instruction
*ins
, unsigned old
, unsigned new);
485 void mir_rewrite_index_src_tag(compiler_context
*ctx
, unsigned old
, unsigned new, unsigned tag
);
486 void mir_rewrite_index_src_swizzle(compiler_context
*ctx
, unsigned old
, unsigned new, unsigned swizzle
);
487 bool mir_single_use(compiler_context
*ctx
, unsigned value
);
488 bool mir_special_index(compiler_context
*ctx
, unsigned idx
);
489 unsigned mir_use_count(compiler_context
*ctx
, unsigned value
);
490 bool mir_is_written_before(compiler_context
*ctx
, midgard_instruction
*ins
, unsigned node
);
491 unsigned mir_mask_of_read_components(midgard_instruction
*ins
, unsigned node
);
492 unsigned mir_ubo_shift(midgard_load_store_op op
);
496 void mir_print_instruction(midgard_instruction
*ins
);
497 void mir_print_bundle(midgard_bundle
*ctx
);
498 void mir_print_block(midgard_block
*block
);
499 void mir_print_shader(compiler_context
*ctx
);
500 bool mir_nontrivial_source2_mod(midgard_instruction
*ins
);
501 bool mir_nontrivial_source2_mod_simple(midgard_instruction
*ins
);
502 bool mir_nontrivial_mod(midgard_vector_alu_src src
, bool is_int
, unsigned mask
);
503 bool mir_nontrivial_outmod(midgard_instruction
*ins
);
507 static const midgard_vector_alu_src blank_alu_src
= {
508 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
511 static const midgard_vector_alu_src blank_alu_src_xxxx
= {
512 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_X
, COMPONENT_X
, COMPONENT_X
),
515 static const midgard_scalar_alu_src blank_scalar_alu_src
= {
519 /* Used for encoding the unused source of 1-op instructions */
520 static const midgard_vector_alu_src zero_alu_src
= { 0 };
522 /* 'Intrinsic' move for aliasing */
524 static inline midgard_instruction
525 v_mov(unsigned src
, midgard_vector_alu_src mod
, unsigned dest
)
527 midgard_instruction ins
= {
530 .src
= { SSA_UNUSED
, src
, SSA_UNUSED
},
533 .op
= midgard_alu_op_imov
,
534 .reg_mode
= midgard_reg_mode_32
,
535 .dest_override
= midgard_dest_override_none
,
536 .outmod
= midgard_outmod_int_wrap
,
537 .src1
= vector_alu_srco_unsigned(zero_alu_src
),
538 .src2
= vector_alu_srco_unsigned(mod
)
546 mir_has_arg(midgard_instruction
*ins
, unsigned arg
)
548 for (unsigned i
= 0; i
< ARRAY_SIZE(ins
->src
); ++i
) {
549 if (ins
->src
[i
] == arg
)
558 void schedule_program(compiler_context
*ctx
);
560 /* Register allocation */
564 /* Broad types of register classes so we can handle special
567 #define NR_REG_CLASSES 5
569 #define REG_CLASS_WORK 0
570 #define REG_CLASS_LDST 1
571 #define REG_CLASS_LDST27 2
572 #define REG_CLASS_TEXR 3
573 #define REG_CLASS_TEXW 4
575 void mir_lower_special_reads(compiler_context
*ctx
);
576 struct ra_graph
* allocate_registers(compiler_context
*ctx
, bool *spilled
);
577 void install_registers(compiler_context
*ctx
, struct ra_graph
*g
);
578 bool mir_is_live_after(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*start
, int src
);
579 bool mir_has_multiple_writes(compiler_context
*ctx
, int src
);
581 void mir_create_pipeline_registers(compiler_context
*ctx
);
584 midgard_promote_uniforms(compiler_context
*ctx
, unsigned promoted_count
);
586 midgard_instruction
*
588 compiler_context
*ctx
,
592 nir_src
*indirect_offset
,
596 emit_sysval_read(compiler_context
*ctx
, nir_instr
*instr
, signed dest_override
, unsigned nr_components
);
599 midgard_emit_derivatives(compiler_context
*ctx
, nir_alu_instr
*instr
);
602 midgard_lower_derivatives(compiler_context
*ctx
, midgard_block
*block
);
604 bool mir_op_computes_derivatives(unsigned op
);
608 void emit_binary_bundle(
609 compiler_context
*ctx
,
610 midgard_bundle
*bundle
,
611 struct util_dynarray
*emission
,
614 /* NIR stuff. TODO: Move? Share? Something? */
617 nir_undef_to_zero(nir_shader
*shader
);
620 nir_clamp_psiz(nir_shader
*shader
, float min_size
, float max_size
);
624 bool midgard_opt_copy_prop(compiler_context
*ctx
, midgard_block
*block
);
625 bool midgard_opt_combine_projection(compiler_context
*ctx
, midgard_block
*block
);
626 bool midgard_opt_varying_projection(compiler_context
*ctx
, midgard_block
*block
);
627 bool midgard_opt_dead_code_eliminate(compiler_context
*ctx
, midgard_block
*block
);
628 bool midgard_opt_dead_move_eliminate(compiler_context
*ctx
, midgard_block
*block
);
629 void midgard_opt_post_move_eliminate(compiler_context
*ctx
, midgard_block
*block
, struct ra_graph
*g
);
631 void midgard_lower_invert(compiler_context
*ctx
, midgard_block
*block
);
632 bool midgard_opt_not_propagate(compiler_context
*ctx
, midgard_block
*block
);
633 bool midgard_opt_fuse_src_invert(compiler_context
*ctx
, midgard_block
*block
);
634 bool midgard_opt_fuse_dest_invert(compiler_context
*ctx
, midgard_block
*block
);
635 bool midgard_opt_promote_fmov(compiler_context
*ctx
, midgard_block
*block
);