Path: blob/21.2-virgl/src/panfrost/midgard/compiler.h
4564 views
/*1* Copyright (C) 2019-2020 Collabora, Ltd.2* Copyright (C) 2019 Alyssa Rosenzweig <[email protected]>3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 THE21* SOFTWARE.22*/2324#ifndef _MDG_COMPILER_H25#define _MDG_COMPILER_H2627#include "midgard.h"28#include "helpers.h"29#include "midgard_compile.h"30#include "midgard_ops.h"3132#include "util/hash_table.h"33#include "util/u_dynarray.h"34#include "util/set.h"35#include "util/list.h"3637#include "main/mtypes.h"38#include "compiler/nir_types.h"39#include "compiler/nir/nir.h"40#include "panfrost/util/pan_ir.h"41#include "panfrost/util/lcra.h"4243/* Forward declare */44struct midgard_block;4546/* Target types. Defaults to TARGET_GOTO (the type corresponding directly to47* the hardware), hence why that must be zero. TARGET_DISCARD signals this48* instruction is actually a discard op. */4950#define TARGET_GOTO 051#define TARGET_BREAK 152#define TARGET_CONTINUE 253#define TARGET_DISCARD 354#define TARGET_TILEBUF_WAIT 45556typedef struct midgard_branch {57/* If conditional, the condition is specified in r31.w */58bool conditional;5960/* For conditionals, if this is true, we branch on FALSE. If false, we branch on TRUE. */61bool invert_conditional;6263/* Branch targets: the start of a block, the start of a loop (continue), the end of a loop (break). Value is one of TARGET_ */64unsigned target_type;6566/* The actual target */67union {68int target_block;69int target_break;70int target_continue;71};72} midgard_branch;7374/* Generic in-memory data type repesenting a single logical instruction, rather75* than a single instruction group. This is the preferred form for code gen.76* Multiple midgard_insturctions will later be combined during scheduling,77* though this is not represented in this structure. Its format bridges78* the low-level binary representation with the higher level semantic meaning.79*80* Notably, it allows registers to be specified as block local SSA, for code81* emitted before the register allocation pass.82*/8384#define MIR_SRC_COUNT 485#define MIR_VEC_COMPONENTS 168687typedef struct midgard_instruction {88/* Must be first for casting */89struct list_head link;9091unsigned type; /* ALU, load/store, texture */9293/* Instruction arguments represented as block-local SSA94* indices, rather than registers. ~0 means unused. */95unsigned src[MIR_SRC_COUNT];96unsigned dest;9798/* vec16 swizzle, unpacked, per source */99unsigned swizzle[MIR_SRC_COUNT][MIR_VEC_COMPONENTS];100101/* Types! */102nir_alu_type src_types[MIR_SRC_COUNT];103nir_alu_type dest_type;104105/* Packing ops have non-32-bit dest types even though they functionally106* work at the 32-bit level, use this as a signal to disable copyprop.107* We maybe need synthetic pack ops instead. */108bool is_pack;109110/* Modifiers, depending on type */111union {112struct {113bool src_abs[MIR_SRC_COUNT];114bool src_neg[MIR_SRC_COUNT];115};116117struct {118bool src_shift[MIR_SRC_COUNT];119};120};121122/* Out of the union for csel (could maybe be fixed..) */123bool src_invert[MIR_SRC_COUNT];124125/* If the op supports it */126enum midgard_roundmode roundmode;127128/* For textures: should helpers execute this instruction (instead of129* just helping with derivatives)? Should helpers terminate after? */130bool helper_terminate;131bool helper_execute;132133/* I.e. (1 << alu_bit) */134int unit;135136bool has_constants;137midgard_constants constants;138uint16_t inline_constant;139bool has_inline_constant;140141bool compact_branch;142uint8_t writeout;143bool last_writeout;144145/* Masks in a saneish format. One bit per channel, not packed fancy.146* Use this instead of the op specific ones, and switch over at emit147* time */148149uint16_t mask;150151/* Hint for the register allocator not to spill the destination written152* from this instruction (because it is a spill/unspill node itself).153* Bitmask of spilled classes */154155unsigned no_spill;156157/* Generic hint for intra-pass use */158bool hint;159160/* During scheduling, the backwards dependency graph161* (DAG). nr_dependencies is the number of unscheduled162* instructions that must still be scheduled after163* (before) this instruction. dependents are which164* instructions need to be scheduled before (after) this165* instruction. */166167unsigned nr_dependencies;168BITSET_WORD *dependents;169170/* Use this in conjunction with `type` */171unsigned op;172173/* This refers to midgard_outmod_float or midgard_outmod_int.174* In case of a ALU op, use midgard_is_integer_out_op() to know which175* one is used.176* If it's a texture op, it's always midgard_outmod_float. */177unsigned outmod;178179union {180midgard_load_store_word load_store;181midgard_texture_word texture;182183midgard_branch branch;184};185186unsigned bundle_id;187} midgard_instruction;188189typedef struct midgard_block {190pan_block base;191192bool scheduled;193194/* List of midgard_bundles emitted (after the scheduler has run) */195struct util_dynarray bundles;196197/* Number of quadwords _actually_ emitted, as determined after scheduling */198unsigned quadword_count;199200/* Indicates this is a fixed-function fragment epilogue block */201bool epilogue;202203/* Are helper invocations required by this block? */204bool helpers_in;205} midgard_block;206207typedef struct midgard_bundle {208/* Tag for the overall bundle */209int tag;210211/* Instructions contained by the bundle. instruction_count <= 6 (vmul,212* sadd, vadd, smul, vlut, branch) */213int instruction_count;214midgard_instruction *instructions[6];215216/* Bundle-wide ALU configuration */217int padding;218int control;219bool has_embedded_constants;220midgard_constants constants;221bool last_writeout;222} midgard_bundle;223224enum midgard_rt_id {225MIDGARD_COLOR_RT0 = 0,226MIDGARD_COLOR_RT1,227MIDGARD_COLOR_RT2,228MIDGARD_COLOR_RT3,229MIDGARD_COLOR_RT4,230MIDGARD_COLOR_RT5,231MIDGARD_COLOR_RT6,232MIDGARD_COLOR_RT7,233MIDGARD_ZS_RT,234MIDGARD_NUM_RTS,235};236237#define MIDGARD_MAX_SAMPLE_ITER 16238239typedef struct compiler_context {240const struct panfrost_compile_inputs *inputs;241nir_shader *nir;242struct pan_shader_info *info;243gl_shader_stage stage;244245/* Number of samples for a keyed blend shader. Depends on is_blend */246unsigned blend_sample_iterations;247248/* Index to precolour to r0 for an input blend colour */249unsigned blend_input;250251/* Index to precolour to r2 for a dual-source blend colour */252unsigned blend_src1;253254/* Count of spills and fills for shaderdb */255unsigned spills;256unsigned fills;257258/* Current NIR function */259nir_function *func;260261/* Allocated compiler temporary counter */262unsigned temp_alloc;263264/* Unordered list of midgard_blocks */265int block_count;266struct list_head blocks;267268/* TODO merge with block_count? */269unsigned block_source_count;270271/* List of midgard_instructions emitted for the current block */272midgard_block *current_block;273274/* If there is a preset after block, use this, otherwise emit_block will create one if NULL */275midgard_block *after_block;276277/* The current "depth" of the loop, for disambiguating breaks/continues278* when using nested loops */279int current_loop_depth;280281/* Total number of loops for shader-db */282unsigned loop_count;283284/* Constants which have been loaded, for later inlining */285struct hash_table_u64 *ssa_constants;286287int temp_count;288int max_hash;289290/* Set of NIR indices that were already emitted as outmods */291BITSET_WORD *already_emitted;292293/* Count of instructions emitted from NIR overall, across all blocks */294int instruction_count;295296unsigned quadword_count;297298/* Bitmask of valid metadata */299unsigned metadata;300301/* Model-specific quirk set */302uint32_t quirks;303304/* Writeout instructions for each render target */305midgard_instruction *writeout_branch[MIDGARD_NUM_RTS][MIDGARD_MAX_SAMPLE_ITER];306307struct hash_table_u64 *sysval_to_id;308309/* Mask of UBOs that need to be uploaded */310uint32_t ubo_mask;311} compiler_context;312313/* Per-block live_in/live_out */314#define MIDGARD_METADATA_LIVENESS (1 << 0)315316/* Helpers for manipulating the above structures (forming the driver IR) */317318/* Append instruction to end of current block */319320static inline midgard_instruction *321mir_upload_ins(struct compiler_context *ctx, struct midgard_instruction ins)322{323midgard_instruction *heap = ralloc(ctx, struct midgard_instruction);324memcpy(heap, &ins, sizeof(ins));325return heap;326}327328static inline midgard_instruction *329emit_mir_instruction(struct compiler_context *ctx, struct midgard_instruction ins)330{331midgard_instruction *u = mir_upload_ins(ctx, ins);332list_addtail(&u->link, &ctx->current_block->base.instructions);333return u;334}335336static inline struct midgard_instruction *337mir_insert_instruction_before(struct compiler_context *ctx,338struct midgard_instruction *tag,339struct midgard_instruction ins)340{341struct midgard_instruction *u = mir_upload_ins(ctx, ins);342list_addtail(&u->link, &tag->link);343return u;344}345346static inline void347mir_remove_instruction(struct midgard_instruction *ins)348{349list_del(&ins->link);350}351352static inline midgard_instruction*353mir_prev_op(struct midgard_instruction *ins)354{355return list_last_entry(&(ins->link), midgard_instruction, link);356}357358static inline midgard_instruction*359mir_next_op(struct midgard_instruction *ins)360{361return list_first_entry(&(ins->link), midgard_instruction, link);362}363364#define mir_foreach_block(ctx, v) \365list_for_each_entry(pan_block, v, &ctx->blocks, link)366367#define mir_foreach_block_from(ctx, from, v) \368list_for_each_entry_from(pan_block, v, &from->base, &ctx->blocks, link)369370#define mir_foreach_instr_in_block(block, v) \371list_for_each_entry(struct midgard_instruction, v, &block->base.instructions, link)372#define mir_foreach_instr_in_block_rev(block, v) \373list_for_each_entry_rev(struct midgard_instruction, v, &block->base.instructions, link)374375#define mir_foreach_instr_in_block_safe(block, v) \376list_for_each_entry_safe(struct midgard_instruction, v, &block->base.instructions, link)377378#define mir_foreach_instr_in_block_safe_rev(block, v) \379list_for_each_entry_safe_rev(struct midgard_instruction, v, &block->base.instructions, link)380381#define mir_foreach_instr_in_block_from(block, v, from) \382list_for_each_entry_from(struct midgard_instruction, v, from, &block->base.instructions, link)383384#define mir_foreach_instr_in_block_from_rev(block, v, from) \385list_for_each_entry_from_rev(struct midgard_instruction, v, from, &block->base.instructions, link)386387#define mir_foreach_bundle_in_block(block, v) \388util_dynarray_foreach(&block->bundles, midgard_bundle, v)389390#define mir_foreach_bundle_in_block_rev(block, v) \391util_dynarray_foreach_reverse(&block->bundles, midgard_bundle, v)392393#define mir_foreach_instr_in_block_scheduled_rev(block, v) \394midgard_instruction* v; \395signed i = 0; \396mir_foreach_bundle_in_block_rev(block, _bundle) \397for (i = (_bundle->instruction_count - 1), v = _bundle->instructions[i]; \398i >= 0; \399--i, v = (i >= 0) ? _bundle->instructions[i] : NULL) \400401#define mir_foreach_instr_global(ctx, v) \402mir_foreach_block(ctx, v_block) \403mir_foreach_instr_in_block(((midgard_block *) v_block), v)404405#define mir_foreach_instr_global_safe(ctx, v) \406mir_foreach_block(ctx, v_block) \407mir_foreach_instr_in_block_safe(((midgard_block *) v_block), v)408409/* Based on set_foreach, expanded with automatic type casts */410411#define mir_foreach_predecessor(blk, v) \412struct set_entry *_entry_##v; \413struct midgard_block *v; \414for (_entry_##v = _mesa_set_next_entry(blk->base.predecessors, NULL), \415v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL); \416_entry_##v != NULL; \417_entry_##v = _mesa_set_next_entry(blk->base.predecessors, _entry_##v), \418v = (struct midgard_block *) (_entry_##v ? _entry_##v->key : NULL))419420#define mir_foreach_src(ins, v) \421for (unsigned v = 0; v < ARRAY_SIZE(ins->src); ++v)422423static inline midgard_instruction *424mir_last_in_block(struct midgard_block *block)425{426return list_last_entry(&block->base.instructions, struct midgard_instruction, link);427}428429static inline midgard_block *430mir_get_block(compiler_context *ctx, int idx)431{432struct list_head *lst = &ctx->blocks;433434while ((idx--) + 1)435lst = lst->next;436437return (struct midgard_block *) lst;438}439440static inline bool441mir_is_alu_bundle(midgard_bundle *bundle)442{443return IS_ALU(bundle->tag);444}445446static inline unsigned447make_compiler_temp(compiler_context *ctx)448{449return (ctx->func->impl->ssa_alloc + ctx->temp_alloc++) << 1;450}451452static inline unsigned453make_compiler_temp_reg(compiler_context *ctx)454{455return ((ctx->func->impl->reg_alloc + ctx->temp_alloc++) << 1) | PAN_IS_REG;456}457458static inline unsigned459nir_ssa_index(nir_ssa_def *ssa)460{461return (ssa->index << 1) | 0;462}463464static inline unsigned465nir_src_index(compiler_context *ctx, nir_src *src)466{467if (src->is_ssa)468return nir_ssa_index(src->ssa);469else {470assert(!src->reg.indirect);471return (src->reg.reg->index << 1) | PAN_IS_REG;472}473}474475static inline unsigned476nir_dest_index(nir_dest *dst)477{478if (dst->is_ssa)479return (dst->ssa.index << 1) | 0;480else {481assert(!dst->reg.indirect);482return (dst->reg.reg->index << 1) | PAN_IS_REG;483}484}485486487488/* MIR manipulation */489490void mir_rewrite_index(compiler_context *ctx, unsigned old, unsigned new);491void mir_rewrite_index_src(compiler_context *ctx, unsigned old, unsigned new);492void mir_rewrite_index_dst(compiler_context *ctx, unsigned old, unsigned new);493void mir_rewrite_index_dst_single(midgard_instruction *ins, unsigned old, unsigned new);494void mir_rewrite_index_src_single(midgard_instruction *ins, unsigned old, unsigned new);495void mir_rewrite_index_src_swizzle(compiler_context *ctx, unsigned old, unsigned new, unsigned *swizzle);496bool mir_single_use(compiler_context *ctx, unsigned value);497unsigned mir_use_count(compiler_context *ctx, unsigned value);498uint16_t mir_bytemask_of_read_components(midgard_instruction *ins, unsigned node);499uint16_t mir_bytemask_of_read_components_index(midgard_instruction *ins, unsigned i);500uint16_t mir_from_bytemask(uint16_t bytemask, unsigned bits);501uint16_t mir_bytemask(midgard_instruction *ins);502uint16_t mir_round_bytemask_up(uint16_t mask, unsigned bits);503void mir_set_bytemask(midgard_instruction *ins, uint16_t bytemask);504signed mir_upper_override(midgard_instruction *ins, unsigned inst_size);505unsigned mir_components_for_type(nir_alu_type T);506unsigned max_bitsize_for_alu(midgard_instruction *ins);507midgard_reg_mode reg_mode_for_bitsize(unsigned bitsize);508509/* MIR printing */510511void mir_print_instruction(midgard_instruction *ins);512void mir_print_bundle(midgard_bundle *ctx);513void mir_print_block(midgard_block *block);514void mir_print_shader(compiler_context *ctx);515bool mir_nontrivial_mod(midgard_instruction *ins, unsigned i, bool check_swizzle);516bool mir_nontrivial_outmod(midgard_instruction *ins);517518void mir_insert_instruction_before_scheduled(compiler_context *ctx, midgard_block *block, midgard_instruction *tag, midgard_instruction ins);519void mir_insert_instruction_after_scheduled(compiler_context *ctx, midgard_block *block, midgard_instruction *tag, midgard_instruction ins);520void mir_flip(midgard_instruction *ins);521void mir_compute_temp_count(compiler_context *ctx);522523#define LDST_GLOBAL (REGISTER_LDST_ZERO << 2)524#define LDST_SHARED ((REGISTER_LDST_LOCAL_STORAGE_PTR << 2) | COMPONENT_Z)525#define LDST_SCRATCH ((REGISTER_LDST_PC_SP << 2) | COMPONENT_Z)526527void mir_set_offset(compiler_context *ctx, midgard_instruction *ins, nir_src *offset, unsigned seg);528void mir_set_ubo_offset(midgard_instruction *ins, nir_src *src, unsigned bias);529530/* 'Intrinsic' move for aliasing */531532static inline midgard_instruction533v_mov(unsigned src, unsigned dest)534{535midgard_instruction ins = {536.type = TAG_ALU_4,537.mask = 0xF,538.src = { ~0, src, ~0, ~0 },539.src_types = { 0, nir_type_uint32 },540.swizzle = SWIZZLE_IDENTITY,541.dest = dest,542.dest_type = nir_type_uint32,543.op = midgard_alu_op_imov,544.outmod = midgard_outmod_keeplo545};546547return ins;548}549550/* Broad types of register classes so we can handle special551* registers */552553#define REG_CLASS_WORK 0554#define REG_CLASS_LDST 1555#define REG_CLASS_TEXR 3556#define REG_CLASS_TEXW 4557558/* Like a move, but to thread local storage! */559560static inline midgard_instruction561v_load_store_scratch(562unsigned srcdest,563unsigned index,564bool is_store,565unsigned mask)566{567/* We index by 32-bit vec4s */568unsigned byte = (index * 4 * 4);569570midgard_instruction ins = {571.type = TAG_LOAD_STORE_4,572.mask = mask,573.dest_type = nir_type_uint32,574.dest = ~0,575.src = { ~0, ~0, ~0, ~0 },576.swizzle = SWIZZLE_IDENTITY_4,577.op = is_store ? midgard_op_st_128 : midgard_op_ld_128,578.load_store = {579/* For register spilling - to thread local storage */580.arg_reg = REGISTER_LDST_LOCAL_STORAGE_PTR,581.arg_comp = COMPONENT_X,582.bitsize_toggle = true,583.index_format = midgard_index_address_u32,584.index_reg = REGISTER_LDST_ZERO,585},586587/* If we spill an unspill, RA goes into an infinite loop */588.no_spill = (1 << REG_CLASS_WORK)589};590591ins.constants.u32[0] = byte;592593if (is_store) {594ins.src[0] = srcdest;595ins.src_types[0] = nir_type_uint32;596597/* Ensure we are tightly swizzled so liveness analysis is598* correct */599600for (unsigned i = 0; i < 4; ++i) {601if (!(mask & (1 << i)))602ins.swizzle[0][i] = COMPONENT_X;603}604} else605ins.dest = srcdest;606607return ins;608}609610static inline bool611mir_has_arg(midgard_instruction *ins, unsigned arg)612{613if (!ins)614return false;615616mir_foreach_src(ins, i) {617if (ins->src[i] == arg)618return true;619}620621return false;622}623624/* Scheduling */625626void midgard_schedule_program(compiler_context *ctx);627628void mir_ra(compiler_context *ctx);629void mir_squeeze_index(compiler_context *ctx);630void mir_lower_special_reads(compiler_context *ctx);631void mir_liveness_ins_update(uint16_t *live, midgard_instruction *ins, unsigned max);632void mir_compute_liveness(compiler_context *ctx);633void mir_invalidate_liveness(compiler_context *ctx);634bool mir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src);635636void mir_create_pipeline_registers(compiler_context *ctx);637void midgard_promote_uniforms(compiler_context *ctx);638639void640midgard_emit_derivatives(compiler_context *ctx, nir_alu_instr *instr);641642void643midgard_lower_derivatives(compiler_context *ctx, midgard_block *block);644645bool mir_op_computes_derivatives(gl_shader_stage stage, unsigned op);646647void mir_analyze_helper_terminate(compiler_context *ctx);648void mir_analyze_helper_requirements(compiler_context *ctx);649650/* Final emission */651652void emit_binary_bundle(653compiler_context *ctx,654midgard_block *block,655midgard_bundle *bundle,656struct util_dynarray *emission,657int next_tag);658659bool nir_fuse_io_16(nir_shader *shader);660661bool midgard_nir_lod_errata(nir_shader *shader);662663unsigned midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx);664665/* Optimizations */666667bool midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block);668bool midgard_opt_combine_projection(compiler_context *ctx, midgard_block *block);669bool midgard_opt_varying_projection(compiler_context *ctx, midgard_block *block);670bool midgard_opt_dead_code_eliminate(compiler_context *ctx);671bool midgard_opt_dead_move_eliminate(compiler_context *ctx, midgard_block *block);672673#endif674675676