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 /* Instruction arguments represented as block-local SSA indices, rather than
71 * registers. Negative values mean unused. */
78 /* src1 is -not- SSA but instead a 16-bit inline constant to be smudged
79 * in. Only valid for ALU ops. */
83 /* Generic in-memory data type repesenting a single logical instruction, rather
84 * than a single instruction group. This is the preferred form for code gen.
85 * Multiple midgard_insturctions will later be combined during scheduling,
86 * though this is not represented in this structure. Its format bridges
87 * the low-level binary representation with the higher level semantic meaning.
89 * Notably, it allows registers to be specified as block local SSA, for code
90 * emitted before the register allocation pass.
93 typedef struct midgard_instruction
{
94 /* Must be first for casting */
95 struct list_head link
;
97 unsigned type
; /* ALU, load/store, texture */
99 /* If the register allocator has not run yet... */
102 /* Special fields for an ALU instruction */
103 midgard_reg_info registers
;
105 /* I.e. (1 << alu_bit) */
108 /* When emitting bundle, should this instruction have a break forced
109 * before it? Used for r31 writes which are valid only within a single
110 * bundle and *need* to happen as early as possible... this is a hack,
111 * TODO remove when we have a scheduler */
116 uint16_t inline_constant
;
117 bool has_blend_constant
;
121 bool prepacked_branch
;
123 /* Masks in a saneish format. One bit per channel, not packed fancy.
124 * Use this instead of the op specific ones, and switch over at emit
129 midgard_load_store_word load_store
;
130 midgard_vector_alu alu
;
131 midgard_texture_word texture
;
132 midgard_branch_extended branch_extended
;
135 /* General branch, rather than packed br_compact. Higher level
136 * than the other components */
137 midgard_branch branch
;
139 } midgard_instruction
;
141 typedef struct midgard_block
{
142 /* Link to next block. Must be first for mir_get_block */
143 struct list_head link
;
145 /* List of midgard_instructions emitted for the current block */
146 struct list_head instructions
;
150 /* List of midgard_bundles emitted (after the scheduler has run) */
151 struct util_dynarray bundles
;
153 /* Number of quadwords _actually_ emitted, as determined after scheduling */
154 unsigned quadword_count
;
156 /* Successors: always one forward (the block after us), maybe
157 * one backwards (for a backward branch). No need for a second
158 * forward, since graph traversal would get there eventually
160 struct midgard_block
*successors
[2];
161 unsigned nr_successors
;
163 /* The successors pointer form a graph, and in the case of
164 * complex control flow, this graph has a cycles. To aid
165 * traversal during liveness analysis, we have a visited?
166 * boolean for passes to use as they see fit, provided they
171 typedef struct midgard_bundle
{
172 /* Tag for the overall bundle */
175 /* Instructions contained by the bundle */
176 int instruction_count
;
177 midgard_instruction
*instructions
[5];
179 /* Bundle-wide ALU configuration */
182 bool has_embedded_constants
;
184 bool has_blend_constant
;
187 typedef struct compiler_context
{
189 gl_shader_stage stage
;
191 /* The screen we correspond to */
192 struct midgard_screen
*screen
;
194 /* Is internally a blend shader? Depends on stage == FRAGMENT */
197 /* Tracking for blend constant patching */
198 int blend_constant_offset
;
200 /* Number of bytes used for Thread Local Storage */
203 /* Count of spills and fills for shaderdb */
207 /* Current NIR function */
210 /* Unordered list of midgard_blocks */
212 struct list_head blocks
;
214 midgard_block
*initial_block
;
215 midgard_block
*previous_source_block
;
216 midgard_block
*final_block
;
218 /* List of midgard_instructions emitted for the current block */
219 midgard_block
*current_block
;
221 /* The current "depth" of the loop, for disambiguating breaks/continues
222 * when using nested loops */
223 int current_loop_depth
;
225 /* Total number of loops for shader-db */
228 /* Constants which have been loaded, for later inlining */
229 struct hash_table_u64
*ssa_constants
;
231 /* Mapping of hashes computed from NIR indices to the sequential temp indices ultimately used in MIR */
232 struct hash_table_u64
*hash_to_temp
;
236 /* Just the count of the max register used. Higher count => higher
237 * register pressure */
240 /* Used for cont/last hinting. Increase when a tex op is added.
241 * Decrease when a tex op is removed. */
242 int texture_op_count
;
244 /* Mapping of texture register -> SSA index for unaliasing */
245 int texture_index
[2];
247 /* The number of uniforms allowable for the fast path */
250 /* Count of instructions emitted from NIR overall, across all blocks */
251 int instruction_count
;
253 /* Alpha ref value passed in */
256 /* The index corresponding to the fragment output */
257 unsigned fragment_output
;
259 /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */
260 unsigned sysvals
[MAX_SYSVAL_COUNT
];
261 unsigned sysval_count
;
262 struct hash_table_u64
*sysval_to_id
;
265 /* Helpers for manipulating the above structures (forming the driver IR) */
267 /* Append instruction to end of current block */
269 static inline midgard_instruction
*
270 mir_upload_ins(struct midgard_instruction ins
)
272 midgard_instruction
*heap
= malloc(sizeof(ins
));
273 memcpy(heap
, &ins
, sizeof(ins
));
278 emit_mir_instruction(struct compiler_context
*ctx
, struct midgard_instruction ins
)
280 list_addtail(&(mir_upload_ins(ins
))->link
, &ctx
->current_block
->instructions
);
284 mir_insert_instruction_before(struct midgard_instruction
*tag
, struct midgard_instruction ins
)
286 list_addtail(&(mir_upload_ins(ins
))->link
, &tag
->link
);
290 mir_remove_instruction(struct midgard_instruction
*ins
)
292 list_del(&ins
->link
);
295 static inline midgard_instruction
*
296 mir_prev_op(struct midgard_instruction
*ins
)
298 return list_last_entry(&(ins
->link
), midgard_instruction
, link
);
301 static inline midgard_instruction
*
302 mir_next_op(struct midgard_instruction
*ins
)
304 return list_first_entry(&(ins
->link
), midgard_instruction
, link
);
307 #define mir_foreach_block(ctx, v) \
308 list_for_each_entry(struct midgard_block, v, &ctx->blocks, link)
310 #define mir_foreach_block_from(ctx, from, v) \
311 list_for_each_entry_from(struct midgard_block, v, from, &ctx->blocks, link)
313 #define mir_foreach_instr(ctx, v) \
314 list_for_each_entry(struct midgard_instruction, v, &ctx->current_block->instructions, link)
316 #define mir_foreach_instr_safe(ctx, v) \
317 list_for_each_entry_safe(struct midgard_instruction, v, &ctx->current_block->instructions, link)
319 #define mir_foreach_instr_in_block(block, v) \
320 list_for_each_entry(struct midgard_instruction, v, &block->instructions, link)
322 #define mir_foreach_instr_in_block_safe(block, v) \
323 list_for_each_entry_safe(struct midgard_instruction, v, &block->instructions, link)
325 #define mir_foreach_instr_in_block_safe_rev(block, v) \
326 list_for_each_entry_safe_rev(struct midgard_instruction, v, &block->instructions, link)
328 #define mir_foreach_instr_in_block_from(block, v, from) \
329 list_for_each_entry_from(struct midgard_instruction, v, from, &block->instructions, link)
331 #define mir_foreach_instr_in_block_from_rev(block, v, from) \
332 list_for_each_entry_from_rev(struct midgard_instruction, v, from, &block->instructions, link)
334 #define mir_foreach_bundle_in_block(block, v) \
335 util_dynarray_foreach(&block->bundles, midgard_bundle, v)
337 #define mir_foreach_instr_global(ctx, v) \
338 mir_foreach_block(ctx, v_block) \
339 mir_foreach_instr_in_block(v_block, v)
341 #define mir_foreach_instr_global_safe(ctx, v) \
342 mir_foreach_block(ctx, v_block) \
343 mir_foreach_instr_in_block_safe(v_block, v)
347 static inline midgard_instruction
*
348 mir_last_in_block(struct midgard_block
*block
)
350 return list_last_entry(&block
->instructions
, struct midgard_instruction
, link
);
353 static inline midgard_block
*
354 mir_get_block(compiler_context
*ctx
, int idx
)
356 struct list_head
*lst
= &ctx
->blocks
;
361 return (struct midgard_block
*) lst
;
365 mir_is_alu_bundle(midgard_bundle
*bundle
)
367 return IS_ALU(bundle
->tag
);
370 /* MIR manipulation */
372 void mir_rewrite_index(compiler_context
*ctx
, unsigned old
, unsigned new);
373 void mir_rewrite_index_src(compiler_context
*ctx
, unsigned old
, unsigned new);
374 void mir_rewrite_index_dst(compiler_context
*ctx
, unsigned old
, unsigned new);
375 void mir_rewrite_index_src_single(midgard_instruction
*ins
, unsigned old
, unsigned new);
376 void mir_rewrite_index_src_tag(compiler_context
*ctx
, unsigned old
, unsigned new, unsigned tag
);
377 bool mir_single_use(compiler_context
*ctx
, unsigned value
);
378 bool mir_special_index(compiler_context
*ctx
, unsigned idx
);
382 void mir_print_instruction(midgard_instruction
*ins
);
383 void mir_print_bundle(midgard_bundle
*ctx
);
384 void mir_print_block(midgard_block
*block
);
385 void mir_print_shader(compiler_context
*ctx
);
386 bool mir_nontrivial_source2_mod(midgard_instruction
*ins
);
387 bool mir_nontrivial_mod(midgard_vector_alu_src src
, bool is_int
, unsigned mask
);
391 static const midgard_vector_alu_src blank_alu_src
= {
392 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
395 static const midgard_vector_alu_src blank_alu_src_xxxx
= {
396 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_X
, COMPONENT_X
, COMPONENT_X
),
399 static const midgard_scalar_alu_src blank_scalar_alu_src
= {
403 /* Used for encoding the unused source of 1-op instructions */
404 static const midgard_vector_alu_src zero_alu_src
= { 0 };
406 /* 'Intrinsic' move for aliasing */
408 static inline midgard_instruction
409 v_mov(unsigned src
, midgard_vector_alu_src mod
, unsigned dest
)
411 midgard_instruction ins
= {
415 .src0
= SSA_UNUSED_1
,
420 .op
= midgard_alu_op_imov
,
421 .reg_mode
= midgard_reg_mode_32
,
422 .dest_override
= midgard_dest_override_none
,
423 .outmod
= midgard_outmod_int_wrap
,
424 .src1
= vector_alu_srco_unsigned(zero_alu_src
),
425 .src2
= vector_alu_srco_unsigned(mod
)
433 mir_has_arg(midgard_instruction
*ins
, unsigned arg
)
435 if (ins
->ssa_args
.src0
== arg
)
438 if (ins
->ssa_args
.src1
== arg
&& !ins
->ssa_args
.inline_constant
)
446 void schedule_program(compiler_context
*ctx
);
448 /* Register allocation */
452 /* Broad types of register classes so we can handle special
455 #define NR_REG_CLASSES 3
457 #define REG_CLASS_WORK 0
458 #define REG_CLASS_LDST 1
459 #define REG_CLASS_LDST27 2
460 #define REG_CLASS_TEX 3
462 void mir_lower_special_reads(compiler_context
*ctx
);
463 struct ra_graph
* allocate_registers(compiler_context
*ctx
, bool *spilled
);
464 void install_registers(compiler_context
*ctx
, struct ra_graph
*g
);
465 bool mir_is_live_after(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*start
, int src
);
466 bool mir_has_multiple_writes(compiler_context
*ctx
, int src
);
468 void mir_create_pipeline_registers(compiler_context
*ctx
);
471 midgard_promote_uniforms(compiler_context
*ctx
, unsigned pressure
);
475 compiler_context
*ctx
,
478 nir_src
*indirect_offset
,
484 void emit_binary_bundle(
485 compiler_context
*ctx
,
486 midgard_bundle
*bundle
,
487 struct util_dynarray
*emission
,
490 /* NIR stuff. TODO: Move? Share? Something? */
493 nir_undef_to_zero(nir_shader
*shader
);
496 nir_clamp_psiz(nir_shader
*shader
, float min_size
, float max_size
);
500 bool midgard_opt_copy_prop(compiler_context
*ctx
, midgard_block
*block
);