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
;
124 midgard_load_store_word load_store
;
125 midgard_vector_alu alu
;
126 midgard_texture_word texture
;
127 midgard_branch_extended branch_extended
;
130 /* General branch, rather than packed br_compact. Higher level
131 * than the other components */
132 midgard_branch branch
;
134 } midgard_instruction
;
136 typedef struct midgard_block
{
137 /* Link to next block. Must be first for mir_get_block */
138 struct list_head link
;
140 /* List of midgard_instructions emitted for the current block */
141 struct list_head instructions
;
145 /* List of midgard_bundles emitted (after the scheduler has run) */
146 struct util_dynarray bundles
;
148 /* Number of quadwords _actually_ emitted, as determined after scheduling */
149 unsigned quadword_count
;
151 /* Successors: always one forward (the block after us), maybe
152 * one backwards (for a backward branch). No need for a second
153 * forward, since graph traversal would get there eventually
155 struct midgard_block
*successors
[2];
156 unsigned nr_successors
;
158 /* The successors pointer form a graph, and in the case of
159 * complex control flow, this graph has a cycles. To aid
160 * traversal during liveness analysis, we have a visited?
161 * boolean for passes to use as they see fit, provided they
166 typedef struct midgard_bundle
{
167 /* Tag for the overall bundle */
170 /* Instructions contained by the bundle */
171 int instruction_count
;
172 midgard_instruction instructions
[5];
174 /* Bundle-wide ALU configuration */
177 bool has_embedded_constants
;
179 bool has_blend_constant
;
181 uint16_t register_words
[8];
182 int register_words_count
;
184 uint64_t body_words
[8];
186 int body_words_count
;
189 typedef struct compiler_context
{
191 gl_shader_stage stage
;
193 /* Is internally a blend shader? Depends on stage == FRAGMENT */
196 /* Tracking for blend constant patching */
197 int blend_constant_offset
;
199 /* Current NIR function */
202 /* Unordered list of midgard_blocks */
204 struct list_head blocks
;
206 midgard_block
*initial_block
;
207 midgard_block
*previous_source_block
;
208 midgard_block
*final_block
;
210 /* List of midgard_instructions emitted for the current block */
211 midgard_block
*current_block
;
213 /* The current "depth" of the loop, for disambiguating breaks/continues
214 * when using nested loops */
215 int current_loop_depth
;
217 /* Constants which have been loaded, for later inlining */
218 struct hash_table_u64
*ssa_constants
;
220 /* SSA indices to be outputted to corresponding varying offset */
221 struct hash_table_u64
*ssa_varyings
;
223 /* SSA values / registers which have been aliased. Naively, these
224 * demand a fmov output; instead, we alias them in a later pass to
225 * avoid the wasted op.
227 * A note on encoding: to avoid dynamic memory management here, rather
228 * than ampping to a pointer, we map to the source index; the key
229 * itself is just the destination index. */
231 struct hash_table_u64
*ssa_to_alias
;
232 struct set
*leftover_ssa_to_alias
;
234 /* Actual SSA-to-register for RA */
235 struct hash_table_u64
*ssa_to_register
;
237 /* Mapping of hashes computed from NIR indices to the sequential temp indices ultimately used in MIR */
238 struct hash_table_u64
*hash_to_temp
;
242 /* Just the count of the max register used. Higher count => higher
243 * register pressure */
246 /* Used for cont/last hinting. Increase when a tex op is added.
247 * Decrease when a tex op is removed. */
248 int texture_op_count
;
250 /* Mapping of texture register -> SSA index for unaliasing */
251 int texture_index
[2];
253 /* If any path hits a discard instruction */
256 /* The number of uniforms allowable for the fast path */
259 /* Count of instructions emitted from NIR overall, across all blocks */
260 int instruction_count
;
262 /* Alpha ref value passed in */
265 /* The index corresponding to the fragment output */
266 unsigned fragment_output
;
268 /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */
269 unsigned sysvals
[MAX_SYSVAL_COUNT
];
270 unsigned sysval_count
;
271 struct hash_table_u64
*sysval_to_id
;
274 /* Helpers for manipulating the above structures (forming the driver IR) */
276 /* Append instruction to end of current block */
278 static inline midgard_instruction
*
279 mir_upload_ins(struct midgard_instruction ins
)
281 midgard_instruction
*heap
= malloc(sizeof(ins
));
282 memcpy(heap
, &ins
, sizeof(ins
));
287 emit_mir_instruction(struct compiler_context
*ctx
, struct midgard_instruction ins
)
289 list_addtail(&(mir_upload_ins(ins
))->link
, &ctx
->current_block
->instructions
);
293 mir_insert_instruction_before(struct midgard_instruction
*tag
, struct midgard_instruction ins
)
295 list_addtail(&(mir_upload_ins(ins
))->link
, &tag
->link
);
299 mir_remove_instruction(struct midgard_instruction
*ins
)
301 list_del(&ins
->link
);
304 static inline midgard_instruction
*
305 mir_prev_op(struct midgard_instruction
*ins
)
307 return list_last_entry(&(ins
->link
), midgard_instruction
, link
);
310 static inline midgard_instruction
*
311 mir_next_op(struct midgard_instruction
*ins
)
313 return list_first_entry(&(ins
->link
), midgard_instruction
, link
);
316 #define mir_foreach_block(ctx, v) \
317 list_for_each_entry(struct midgard_block, v, &ctx->blocks, link)
319 #define mir_foreach_block_from(ctx, from, v) \
320 list_for_each_entry_from(struct midgard_block, v, from, &ctx->blocks, link)
322 /* The following routines are for use before the scheduler has run */
324 #define mir_foreach_instr(ctx, v) \
325 list_for_each_entry(struct midgard_instruction, v, &ctx->current_block->instructions, link)
327 #define mir_foreach_instr_safe(ctx, v) \
328 list_for_each_entry_safe(struct midgard_instruction, v, &ctx->current_block->instructions, link)
330 #define mir_foreach_instr_in_block(block, v) \
331 list_for_each_entry(struct midgard_instruction, v, &block->instructions, link)
333 #define mir_foreach_instr_in_block_safe(block, v) \
334 list_for_each_entry_safe(struct midgard_instruction, v, &block->instructions, link)
336 #define mir_foreach_instr_in_block_safe_rev(block, v) \
337 list_for_each_entry_safe_rev(struct midgard_instruction, v, &block->instructions, link)
339 #define mir_foreach_instr_in_block_from(block, v, from) \
340 list_for_each_entry_from(struct midgard_instruction, v, from, &block->instructions, link)
342 #define mir_foreach_instr_in_block_from_rev(block, v, from) \
343 list_for_each_entry_from_rev(struct midgard_instruction, v, from, &block->instructions, link)
345 #define mir_foreach_bundle_in_block(block, v) \
346 util_dynarray_foreach(&block->bundles, midgard_bundle, v)
348 static inline midgard_instruction
*
349 mir_last_in_block(struct midgard_block
*block
)
351 return list_last_entry(&block
->instructions
, struct midgard_instruction
, link
);
354 static inline midgard_block
*
355 mir_get_block(compiler_context
*ctx
, int idx
)
357 struct list_head
*lst
= &ctx
->blocks
;
362 return (struct midgard_block
*) lst
;
367 void mir_print_instruction(midgard_instruction
*ins
);
368 void mir_print_block(midgard_block
*block
);
369 void mir_print_shader(compiler_context
*ctx
);
373 static const midgard_vector_alu_src blank_alu_src
= {
374 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_Y
, COMPONENT_Z
, COMPONENT_W
),
377 static const midgard_vector_alu_src blank_alu_src_xxxx
= {
378 .swizzle
= SWIZZLE(COMPONENT_X
, COMPONENT_X
, COMPONENT_X
, COMPONENT_X
),
381 static const midgard_scalar_alu_src blank_scalar_alu_src
= {
385 /* Used for encoding the unused source of 1-op instructions */
386 static const midgard_vector_alu_src zero_alu_src
= { 0 };
388 /* 'Intrinsic' move for aliasing */
390 static inline midgard_instruction
391 v_fmov(unsigned src
, midgard_vector_alu_src mod
, unsigned dest
)
393 midgard_instruction ins
= {
396 .src0
= SSA_UNUSED_1
,
401 .op
= midgard_alu_op_fmov
,
402 .reg_mode
= midgard_reg_mode_32
,
403 .dest_override
= midgard_dest_override_none
,
405 .src1
= vector_alu_srco_unsigned(zero_alu_src
),
406 .src2
= vector_alu_srco_unsigned(mod
)
415 void schedule_program(compiler_context
*ctx
);
417 /* Register allocation */
421 struct ra_graph
* allocate_registers(compiler_context
*ctx
);
422 void install_registers(compiler_context
*ctx
, struct ra_graph
*g
);
423 bool mir_is_live_after(compiler_context
*ctx
, midgard_block
*block
, midgard_instruction
*start
, int src
);