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