pan/midgard: Remove util/ra support
[mesa.git] / src / panfrost / midgard / compiler.h
1 /*
2 * Copyright (C) 2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 */
23
24 #ifndef _MDG_COMPILER_H
25 #define _MDG_COMPILER_H
26
27 #include "midgard.h"
28 #include "helpers.h"
29 #include "midgard_compile.h"
30 #include "lcra.h"
31
32 #include "util/hash_table.h"
33 #include "util/u_dynarray.h"
34 #include "util/set.h"
35 #include "util/list.h"
36
37 #include "main/mtypes.h"
38 #include "compiler/nir_types.h"
39 #include "compiler/nir/nir.h"
40
41 /* Forward declare */
42 struct midgard_block;
43
44 /* Target types. Defaults to TARGET_GOTO (the type corresponding directly to
45 * the hardware), hence why that must be zero. TARGET_DISCARD signals this
46 * instruction is actually a discard op. */
47
48 #define TARGET_GOTO 0
49 #define TARGET_BREAK 1
50 #define TARGET_CONTINUE 2
51 #define TARGET_DISCARD 3
52
53 typedef struct midgard_branch {
54 /* If conditional, the condition is specified in r31.w */
55 bool conditional;
56
57 /* For conditionals, if this is true, we branch on FALSE. If false, we branch on TRUE. */
58 bool invert_conditional;
59
60 /* Branch targets: the start of a block, the start of a loop (continue), the end of a loop (break). Value is one of TARGET_ */
61 unsigned target_type;
62
63 /* The actual target */
64 union {
65 int target_block;
66 int target_break;
67 int target_continue;
68 };
69 } midgard_branch;
70
71 /* Generic in-memory data type repesenting a single logical instruction, rather
72 * than a single instruction group. This is the preferred form for code gen.
73 * Multiple midgard_insturctions will later be combined during scheduling,
74 * though this is not represented in this structure. Its format bridges
75 * the low-level binary representation with the higher level semantic meaning.
76 *
77 * Notably, it allows registers to be specified as block local SSA, for code
78 * emitted before the register allocation pass.
79 */
80
81 #define MIR_SRC_COUNT 3
82 #define MIR_VEC_COMPONENTS 16
83
84 typedef struct midgard_instruction {
85 /* Must be first for casting */
86 struct list_head link;
87
88 unsigned type; /* ALU, load/store, texture */
89
90 /* Instruction arguments represented as block-local SSA
91 * indices, rather than registers. ~0 means unused. */
92 unsigned src[3];
93 unsigned dest;
94
95 /* vec16 swizzle, unpacked, per source */
96 unsigned swizzle[MIR_SRC_COUNT][MIR_VEC_COMPONENTS];
97
98 /* Special fields for an ALU instruction */
99 midgard_reg_info registers;
100
101 /* I.e. (1 << alu_bit) */
102 int unit;
103
104 bool has_constants;
105 uint32_t constants[4];
106 uint16_t inline_constant;
107 bool has_blend_constant;
108 bool has_inline_constant;
109
110 bool compact_branch;
111 bool writeout;
112 bool prepacked_branch;
113
114 /* Kind of a hack, but hint against aggressive DCE */
115 bool dont_eliminate;
116
117 /* Masks in a saneish format. One bit per channel, not packed fancy.
118 * Use this instead of the op specific ones, and switch over at emit
119 * time */
120
121 uint16_t mask;
122
123 /* For ALU ops only: set to true to invert (bitwise NOT) the
124 * destination of an integer-out op. Not imeplemented in hardware but
125 * allows more optimizations */
126
127 bool invert;
128
129 /* Hint for the register allocator not to spill the destination written
130 * from this instruction (because it is a spill/unspill node itself) */
131
132 bool no_spill;
133
134 /* Generic hint for intra-pass use */
135 bool hint;
136
137 /* During scheduling, the backwards dependency graph
138 * (DAG). nr_dependencies is the number of unscheduled
139 * instructions that must still be scheduled after
140 * (before) this instruction. dependents are which
141 * instructions need to be scheduled before (after) this
142 * instruction. */
143
144 unsigned nr_dependencies;
145 BITSET_WORD *dependents;
146
147 union {
148 midgard_load_store_word load_store;
149 midgard_vector_alu alu;
150 midgard_texture_word texture;
151 midgard_branch_extended branch_extended;
152 uint16_t br_compact;
153
154 /* General branch, rather than packed br_compact. Higher level
155 * than the other components */
156 midgard_branch branch;
157 };
158 } midgard_instruction;
159
160 typedef struct midgard_block {
161 /* Link to next block. Must be first for mir_get_block */
162 struct list_head link;
163
164 /* List of midgard_instructions emitted for the current block */
165 struct list_head instructions;
166
167 /* Index of the block in source order */
168 unsigned source_id;
169
170 bool is_scheduled;
171
172 /* List of midgard_bundles emitted (after the scheduler has run) */
173 struct util_dynarray bundles;
174
175 /* Number of quadwords _actually_ emitted, as determined after scheduling */
176 unsigned quadword_count;
177
178 /* Succeeding blocks. The compiler should not necessarily rely on
179 * source-order traversal */
180 struct midgard_block *successors[2];
181 unsigned nr_successors;
182
183 struct set *predecessors;
184
185 /* The successors pointer form a graph, and in the case of
186 * complex control flow, this graph has a cycles. To aid
187 * traversal during liveness analysis, we have a visited?
188 * boolean for passes to use as they see fit, provided they
189 * clean up later */
190 bool visited;
191
192 /* In liveness analysis, these are live masks (per-component) for
193 * indices for the block. Scalar compilers have the luxury of using
194 * simple bit fields, but for us, liveness is a vector idea. */
195 uint16_t *live_in;
196 uint16_t *live_out;
197 } midgard_block;
198
199 typedef struct midgard_bundle {
200 /* Tag for the overall bundle */
201 int tag;
202
203 /* Instructions contained by the bundle. instruction_count <= 6 (vmul,
204 * sadd, vadd, smul, vlut, branch) */
205 int instruction_count;
206 midgard_instruction *instructions[6];
207
208 /* Bundle-wide ALU configuration */
209 int padding;
210 int control;
211 bool has_embedded_constants;
212 float constants[4];
213 bool has_blend_constant;
214 } midgard_bundle;
215
216 typedef struct compiler_context {
217 nir_shader *nir;
218 gl_shader_stage stage;
219
220 /* Is internally a blend shader? Depends on stage == FRAGMENT */
221 bool is_blend;
222
223 /* Tracking for blend constant patching */
224 int blend_constant_offset;
225
226 /* Number of bytes used for Thread Local Storage */
227 unsigned tls_size;
228
229 /* Count of spills and fills for shaderdb */
230 unsigned spills;
231 unsigned fills;
232
233 /* Current NIR function */
234 nir_function *func;
235
236 /* Allocated compiler temporary counter */
237 unsigned temp_alloc;
238
239 /* Unordered list of midgard_blocks */
240 int block_count;
241 struct list_head blocks;
242
243 /* TODO merge with block_count? */
244 unsigned block_source_count;
245
246 /* List of midgard_instructions emitted for the current block */
247 midgard_block *current_block;
248
249 /* If there is a preset after block, use this, otherwise emit_block will create one if NULL */
250 midgard_block *after_block;
251
252 /* The current "depth" of the loop, for disambiguating breaks/continues
253 * when using nested loops */
254 int current_loop_depth;
255
256 /* Total number of loops for shader-db */
257 unsigned loop_count;
258
259 /* Constants which have been loaded, for later inlining */
260 struct hash_table_u64 *ssa_constants;
261
262 /* Mapping of hashes computed from NIR indices to the sequential temp indices ultimately used in MIR */
263 struct hash_table_u64 *hash_to_temp;
264 int temp_count;
265 int max_hash;
266
267 /* Just the count of the max register used. Higher count => higher
268 * register pressure */
269 int work_registers;
270
271 /* Used for cont/last hinting. Increase when a tex op is added.
272 * Decrease when a tex op is removed. */
273 int texture_op_count;
274
275 /* The number of uniforms allowable for the fast path */
276 int uniform_cutoff;
277
278 /* Count of instructions emitted from NIR overall, across all blocks */
279 int instruction_count;
280
281 /* Alpha ref value passed in */
282 float alpha_ref;
283
284 unsigned quadword_count;
285
286 /* The mapping of sysvals to uniforms, the count, and the off-by-one inverse */
287 unsigned sysvals[MAX_SYSVAL_COUNT];
288 unsigned sysval_count;
289 struct hash_table_u64 *sysval_to_id;
290
291 /* Bitmask of valid metadata */
292 unsigned metadata;
293
294 unsigned gpu_id;
295 } compiler_context;
296
297 /* Per-block live_in/live_out */
298 #define MIDGARD_METADATA_LIVENESS (1 << 0)
299
300 /* Helpers for manipulating the above structures (forming the driver IR) */
301
302 /* Append instruction to end of current block */
303
304 static inline midgard_instruction *
305 mir_upload_ins(struct compiler_context *ctx, struct midgard_instruction ins)
306 {
307 midgard_instruction *heap = ralloc(ctx, struct midgard_instruction);
308 memcpy(heap, &ins, sizeof(ins));
309 return heap;
310 }
311
312 static inline midgard_instruction *
313 emit_mir_instruction(struct compiler_context *ctx, struct midgard_instruction ins)
314 {
315 midgard_instruction *u = mir_upload_ins(ctx, ins);
316 list_addtail(&u->link, &ctx->current_block->instructions);
317 return u;
318 }
319
320 static inline struct midgard_instruction *
321 mir_insert_instruction_before(struct compiler_context *ctx,
322 struct midgard_instruction *tag,
323 struct midgard_instruction ins)
324 {
325 struct midgard_instruction *u = mir_upload_ins(ctx, ins);
326 list_addtail(&u->link, &tag->link);
327 return u;
328 }
329
330 static inline void
331 mir_remove_instruction(struct midgard_instruction *ins)
332 {
333 list_del(&ins->link);
334 }
335
336 static inline midgard_instruction*
337 mir_prev_op(struct midgard_instruction *ins)
338 {
339 return list_last_entry(&(ins->link), midgard_instruction, link);
340 }
341
342 static inline midgard_instruction*
343 mir_next_op(struct midgard_instruction *ins)
344 {
345 return list_first_entry(&(ins->link), midgard_instruction, link);
346 }
347
348 #define mir_foreach_block(ctx, v) \
349 list_for_each_entry(struct midgard_block, v, &ctx->blocks, link)
350
351 #define mir_foreach_block_from(ctx, from, v) \
352 list_for_each_entry_from(struct midgard_block, v, from, &ctx->blocks, link)
353
354 #define mir_foreach_instr(ctx, v) \
355 list_for_each_entry(struct midgard_instruction, v, &ctx->current_block->instructions, link)
356
357 #define mir_foreach_instr_safe(ctx, v) \
358 list_for_each_entry_safe(struct midgard_instruction, v, &ctx->current_block->instructions, link)
359
360 #define mir_foreach_instr_in_block(block, v) \
361 list_for_each_entry(struct midgard_instruction, v, &block->instructions, link)
362 #define mir_foreach_instr_in_block_rev(block, v) \
363 list_for_each_entry_rev(struct midgard_instruction, v, &block->instructions, link)
364
365 #define mir_foreach_instr_in_block_safe(block, v) \
366 list_for_each_entry_safe(struct midgard_instruction, v, &block->instructions, link)
367
368 #define mir_foreach_instr_in_block_safe_rev(block, v) \
369 list_for_each_entry_safe_rev(struct midgard_instruction, v, &block->instructions, link)
370
371 #define mir_foreach_instr_in_block_from(block, v, from) \
372 list_for_each_entry_from(struct midgard_instruction, v, from, &block->instructions, link)
373
374 #define mir_foreach_instr_in_block_from_rev(block, v, from) \
375 list_for_each_entry_from_rev(struct midgard_instruction, v, from, &block->instructions, link)
376
377 #define mir_foreach_bundle_in_block(block, v) \
378 util_dynarray_foreach(&block->bundles, midgard_bundle, v)
379
380 #define mir_foreach_bundle_in_block_rev(block, v) \
381 util_dynarray_foreach_reverse(&block->bundles, midgard_bundle, v)
382
383 #define mir_foreach_instr_in_block_scheduled_rev(block, v) \
384 midgard_instruction* v; \
385 signed i = 0; \
386 mir_foreach_bundle_in_block_rev(block, _bundle) \
387 for (i = (_bundle->instruction_count - 1), v = _bundle->instructions[i]; \
388 i >= 0; \
389 --i, v = _bundle->instructions[i]) \
390
391 #define mir_foreach_instr_global(ctx, v) \
392 mir_foreach_block(ctx, v_block) \
393 mir_foreach_instr_in_block(v_block, v)
394
395 #define mir_foreach_instr_global_safe(ctx, v) \
396 mir_foreach_block(ctx, v_block) \
397 mir_foreach_instr_in_block_safe(v_block, v)
398
399 #define mir_foreach_successor(blk, v) \
400 struct midgard_block *v; \
401 struct midgard_block **_v; \
402 for (_v = &blk->successors[0], \
403 v = *_v; \
404 v != NULL && _v < &blk->successors[2]; \
405 _v++, v = *_v) \
406
407 /* Based on set_foreach, expanded with automatic type casts */
408
409 #define mir_foreach_predecessor(blk, v) \
410 struct set_entry *_entry_##v; \
411 struct midgard_block *v; \
412 for (_entry_##v = _mesa_set_next_entry(blk->predecessors, NULL), \
413 v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL); \
414 _entry_##v != NULL; \
415 _entry_##v = _mesa_set_next_entry(blk->predecessors, _entry_##v), \
416 v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL))
417
418 #define mir_foreach_src(ins, v) \
419 for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v)
420
421 static inline midgard_instruction *
422 mir_last_in_block(struct midgard_block *block)
423 {
424 return list_last_entry(&block->instructions, struct midgard_instruction, link);
425 }
426
427 static inline midgard_block *
428 mir_get_block(compiler_context *ctx, int idx)
429 {
430 struct list_head *lst = &ctx->blocks;
431
432 while ((idx--) + 1)
433 lst = lst->next;
434
435 return (struct midgard_block *) lst;
436 }
437
438 static inline midgard_block *
439 mir_exit_block(struct compiler_context *ctx)
440 {
441 midgard_block *last = list_last_entry(&ctx->blocks,
442 struct midgard_block, link);
443
444 /* The last block must be empty logically but contains branch writeout
445 * for fragment shaders */
446
447 assert(last->nr_successors == 0);
448
449 return last;
450 }
451
452 static inline bool
453 mir_is_alu_bundle(midgard_bundle *bundle)
454 {
455 return IS_ALU(bundle->tag);
456 }
457
458 /* Registers/SSA are distinguish in the backend by the bottom-most bit */
459
460 #define IS_REG (1)
461
462 static inline unsigned
463 make_compiler_temp(compiler_context *ctx)
464 {
465 return (ctx->func->impl->ssa_alloc + ctx->temp_alloc++) << 1;
466 }
467
468 static inline unsigned
469 make_compiler_temp_reg(compiler_context *ctx)
470 {
471 return ((ctx->func->impl->reg_alloc + ctx->temp_alloc++) << 1) | IS_REG;
472 }
473
474 static inline unsigned
475 nir_src_index(compiler_context *ctx, nir_src *src)
476 {
477 if (src->is_ssa)
478 return (src->ssa->index << 1) | 0;
479 else {
480 assert(!src->reg.indirect);
481 return (src->reg.reg->index << 1) | IS_REG;
482 }
483 }
484
485 static inline unsigned
486 nir_alu_src_index(compiler_context *ctx, nir_alu_src *src)
487 {
488 return nir_src_index(ctx, &src->src);
489 }
490
491 static inline unsigned
492 nir_dest_index(compiler_context *ctx, nir_dest *dst)
493 {
494 if (dst->is_ssa)
495 return (dst->ssa.index << 1) | 0;
496 else {
497 assert(!dst->reg.indirect);
498 return (dst->reg.reg->index << 1) | IS_REG;
499 }
500 }
501
502
503
504 /* MIR manipulation */
505
506 void mir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new);
507 void mir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new);
508 void mir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new);
509 void mir_rewrite_index_dst_single(midgard_instruction *ins, unsigned old, unsigned new);
510 void mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old, unsigned new);
511 void mir_rewrite_index_src_swizzle(compiler_context *ctx, unsigned old, unsigned new, unsigned *swizzle);
512 bool mir_single_use(compiler_context *ctx, unsigned value);
513 bool mir_special_index(compiler_context *ctx, unsigned idx);
514 unsigned mir_use_count(compiler_context *ctx, unsigned value);
515 bool mir_is_written_before(compiler_context *ctx, midgard_instruction *ins, unsigned node);
516 uint16_t mir_bytemask_of_read_components(midgard_instruction *ins, unsigned node);
517 unsigned mir_ubo_shift(midgard_load_store_op op);
518 midgard_reg_mode mir_typesize(midgard_instruction *ins);
519 midgard_reg_mode mir_srcsize(midgard_instruction *ins, unsigned i);
520 unsigned mir_bytes_for_mode(midgard_reg_mode mode);
521 uint16_t mir_from_bytemask(uint16_t bytemask, midgard_reg_mode mode);
522 uint16_t mir_bytemask(midgard_instruction *ins);
523 uint16_t mir_round_bytemask_down(uint16_t mask, midgard_reg_mode mode);
524 void mir_set_bytemask(midgard_instruction *ins, uint16_t bytemask);
525
526 /* MIR printing */
527
528 void mir_print_instruction(midgard_instruction *ins);
529 void mir_print_bundle(midgard_bundle *ctx);
530 void mir_print_block(midgard_block *block);
531 void mir_print_shader(compiler_context *ctx);
532 bool mir_nontrivial_source2_mod(midgard_instruction *ins);
533 bool mir_nontrivial_source2_mod_simple(midgard_instruction *ins);
534 bool mir_nontrivial_outmod(midgard_instruction *ins);
535
536 void mir_insert_instruction_before_scheduled(compiler_context *ctx, midgard_block *block, midgard_instruction *tag, midgard_instruction ins);
537 void mir_insert_instruction_after_scheduled(compiler_context *ctx, midgard_block *block, midgard_instruction *tag, midgard_instruction ins);
538 void mir_flip(midgard_instruction *ins);
539 void mir_compute_temp_count(compiler_context *ctx);
540
541 /* 'Intrinsic' move for aliasing */
542
543 static inline midgard_instruction
544 v_mov(unsigned src, unsigned dest)
545 {
546 midgard_instruction ins = {
547 .type = TAG_ALU_4,
548 .mask = 0xF,
549 .src = { SSA_UNUSED, src, SSA_UNUSED },
550 .swizzle = SWIZZLE_IDENTITY,
551 .dest = dest,
552 .alu = {
553 .op = midgard_alu_op_imov,
554 .reg_mode = midgard_reg_mode_32,
555 .dest_override = midgard_dest_override_none,
556 .outmod = midgard_outmod_int_wrap
557 },
558 };
559
560 return ins;
561 }
562
563 static inline bool
564 mir_has_arg(midgard_instruction *ins, unsigned arg)
565 {
566 if (!ins)
567 return false;
568
569 for (unsigned i = 0; i < ARRAY_SIZE(ins->src); ++i) {
570 if (ins->src[i] == arg)
571 return true;
572 }
573
574 return false;
575 }
576
577 /* Scheduling */
578
579 void schedule_program(compiler_context *ctx);
580
581 /* Broad types of register classes so we can handle special
582 * registers */
583
584 #define REG_CLASS_WORK 0
585 #define REG_CLASS_LDST 1
586 #define REG_CLASS_TEXR 3
587 #define REG_CLASS_TEXW 4
588
589 void mir_lower_special_reads(compiler_context *ctx);
590 struct lcra_state* allocate_registers(compiler_context *ctx, bool *spilled);
591 void install_registers(compiler_context *ctx, struct lcra_state *g);
592 void mir_liveness_ins_update(uint16_t *live, midgard_instruction *ins, unsigned max);
593 void mir_compute_liveness(compiler_context *ctx);
594 void mir_invalidate_liveness(compiler_context *ctx);
595 bool mir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src);
596
597 void mir_create_pipeline_registers(compiler_context *ctx);
598
599 void
600 midgard_promote_uniforms(compiler_context *ctx, unsigned promoted_count);
601
602 midgard_instruction *
603 emit_ubo_read(
604 compiler_context *ctx,
605 nir_instr *instr,
606 unsigned dest,
607 unsigned offset,
608 nir_src *indirect_offset,
609 unsigned index);
610
611 void
612 emit_sysval_read(compiler_context *ctx, nir_instr *instr, signed dest_override, unsigned nr_components);
613
614 void
615 midgard_emit_derivatives(compiler_context *ctx, nir_alu_instr *instr);
616
617 void
618 midgard_lower_derivatives(compiler_context *ctx, midgard_block *block);
619
620 bool mir_op_computes_derivatives(unsigned op);
621
622 /* Final emission */
623
624 void emit_binary_bundle(
625 compiler_context *ctx,
626 midgard_bundle *bundle,
627 struct util_dynarray *emission,
628 int next_tag);
629
630 bool
631 nir_undef_to_zero(nir_shader *shader);
632
633 /* Optimizations */
634
635 bool midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block);
636 bool midgard_opt_combine_projection(compiler_context *ctx, midgard_block *block);
637 bool midgard_opt_varying_projection(compiler_context *ctx, midgard_block *block);
638 bool midgard_opt_dead_code_eliminate(compiler_context *ctx, midgard_block *block);
639 bool midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block);
640
641 void midgard_lower_invert(compiler_context *ctx, midgard_block *block);
642 bool midgard_opt_not_propagate(compiler_context *ctx, midgard_block *block);
643 bool midgard_opt_fuse_src_invert(compiler_context *ctx, midgard_block *block);
644 bool midgard_opt_fuse_dest_invert(compiler_context *ctx, midgard_block *block);
645 bool midgard_opt_csel_invert(compiler_context *ctx, midgard_block *block);
646 bool midgard_opt_promote_fmov(compiler_context *ctx, midgard_block *block);
647
648 #endif