Path: blob/21.2-virgl/src/gallium/drivers/lima/ir/pp/ppir.h
4574 views
/*1* Copyright (c) 2017 Lima Project2* Copyright (c) 2013 Connor Abbott3*4* Permission is hereby granted, free of charge, to any person obtaining a copy5* of this software and associated documentation files (the "Software"), to deal6* in the Software without restriction, including without limitation the rights7* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell8* copies of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be included in12* all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE17* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN20* THE SOFTWARE.21*22*/2324#ifndef LIMA_IR_PP_PPIR_H25#define LIMA_IR_PP_PPIR_H2627#include "util/u_math.h"28#include "util/list.h"29#include "util/set.h"3031#include "ir/lima_ir.h"3233typedef enum {34ppir_op_mov,35ppir_op_abs,36ppir_op_neg,37ppir_op_sat,38ppir_op_add,3940ppir_op_ddx,41ppir_op_ddy,4243ppir_op_mul,44ppir_op_rcp,4546ppir_op_sin_lut,47ppir_op_cos_lut,4849ppir_op_sum3,50ppir_op_sum4,5152ppir_op_normalize2,53ppir_op_normalize3,54ppir_op_normalize4,5556ppir_op_select,5758ppir_op_sin,59ppir_op_cos,60ppir_op_tan,61ppir_op_asin,62ppir_op_acos,6364ppir_op_atan,65ppir_op_atan2,66ppir_op_atan_pt1,67ppir_op_atan2_pt1,68ppir_op_atan_pt2,6970ppir_op_exp,71ppir_op_log,72ppir_op_exp2,73ppir_op_log2,74ppir_op_sqrt,75ppir_op_rsqrt,7677ppir_op_sign,78ppir_op_floor,79ppir_op_ceil,80ppir_op_fract,81ppir_op_mod,82ppir_op_min,83ppir_op_max,84ppir_op_trunc,8586ppir_op_and,87ppir_op_or,88ppir_op_xor,8990ppir_op_lt,91ppir_op_gt,92ppir_op_le,93ppir_op_ge,94ppir_op_eq,95ppir_op_ne,96ppir_op_not,9798ppir_op_load_uniform,99ppir_op_load_varying,100ppir_op_load_coords,101ppir_op_load_coords_reg,102ppir_op_load_fragcoord,103ppir_op_load_pointcoord,104ppir_op_load_frontface,105ppir_op_load_texture,106ppir_op_load_temp,107108ppir_op_store_temp,109110ppir_op_const,111112ppir_op_discard,113ppir_op_branch,114115ppir_op_undef,116ppir_op_dummy,117118ppir_op_num,119} ppir_op;120121typedef enum {122ppir_node_type_alu,123ppir_node_type_const,124ppir_node_type_load,125ppir_node_type_store,126ppir_node_type_load_texture,127ppir_node_type_discard,128ppir_node_type_branch,129} ppir_node_type;130131typedef struct {132char *name;133ppir_node_type type;134int *slots;135} ppir_op_info;136137extern const ppir_op_info ppir_op_infos[];138139typedef enum {140ppir_dep_src,141ppir_dep_write_after_read,142ppir_dep_sequence,143} ppir_dep_type;144145typedef struct {146void *pred, *succ;147ppir_dep_type type;148struct list_head pred_link;149struct list_head succ_link;150} ppir_dep;151152typedef struct ppir_node {153struct list_head list;154struct list_head sched_list;155ppir_op op;156ppir_node_type type;157int index;158char name[16];159bool printed;160struct ppir_instr *instr;161int instr_pos;162struct ppir_block *block;163bool is_end;164bool succ_different_block;165166/* for scheduler */167struct list_head succ_list;168struct list_head pred_list;169} ppir_node;170171typedef enum {172ppir_pipeline_reg_const0,173ppir_pipeline_reg_const1,174ppir_pipeline_reg_sampler,175ppir_pipeline_reg_uniform,176ppir_pipeline_reg_vmul,177ppir_pipeline_reg_fmul,178ppir_pipeline_reg_discard, /* varying load */179} ppir_pipeline;180181typedef struct ppir_reg {182struct list_head list;183int index;184int regalloc_index;185int num_components;186187/* whether this reg has to start from the x component188* of a full physical reg, this is true for reg used189* in load/store instr which has no swizzle field */190bool is_head;191bool spilled;192bool undef;193} ppir_reg;194195typedef enum {196ppir_target_ssa,197ppir_target_pipeline,198ppir_target_register,199} ppir_target;200201typedef struct ppir_src {202ppir_target type;203ppir_node *node;204205union {206ppir_reg *ssa;207ppir_reg *reg;208ppir_pipeline pipeline;209};210211uint8_t swizzle[4];212bool absolute, negate;213} ppir_src;214215typedef enum {216ppir_outmod_none,217ppir_outmod_clamp_fraction,218ppir_outmod_clamp_positive,219ppir_outmod_round,220} ppir_outmod;221222typedef struct ppir_dest {223ppir_target type;224225union {226ppir_reg ssa;227ppir_reg *reg;228ppir_pipeline pipeline;229};230231ppir_outmod modifier;232unsigned write_mask : 4;233} ppir_dest;234235typedef struct {236ppir_node node;237ppir_dest dest;238ppir_src src[3];239int num_src;240int shift : 3; /* Only used for ppir_op_mul */241} ppir_alu_node;242243typedef struct ppir_const {244union fi value[4];245int num;246} ppir_const;247248typedef struct {249ppir_node node;250ppir_const constant;251ppir_dest dest;252} ppir_const_node;253254typedef struct {255ppir_node node;256int index;257int num_components;258ppir_dest dest;259ppir_src src;260int num_src;261} ppir_load_node;262263typedef struct {264ppir_node node;265int index;266int num_components;267ppir_src src;268} ppir_store_node;269270typedef struct {271ppir_node node;272ppir_dest dest;273ppir_src src[2];274int num_src;275int sampler;276int sampler_dim;277bool lod_bias_en;278bool explicit_lod;279} ppir_load_texture_node;280281typedef struct {282ppir_node node;283} ppir_discard_node;284285enum ppir_instr_slot {286PPIR_INSTR_SLOT_VARYING,287PPIR_INSTR_SLOT_TEXLD,288PPIR_INSTR_SLOT_UNIFORM,289PPIR_INSTR_SLOT_ALU_VEC_MUL,290PPIR_INSTR_SLOT_ALU_SCL_MUL,291PPIR_INSTR_SLOT_ALU_VEC_ADD,292PPIR_INSTR_SLOT_ALU_SCL_ADD,293PPIR_INSTR_SLOT_ALU_COMBINE,294PPIR_INSTR_SLOT_STORE_TEMP,295PPIR_INSTR_SLOT_BRANCH,296PPIR_INSTR_SLOT_NUM,297PPIR_INSTR_SLOT_END,298PPIR_INSTR_SLOT_ALU_START = PPIR_INSTR_SLOT_ALU_VEC_MUL,299PPIR_INSTR_SLOT_ALU_END = PPIR_INSTR_SLOT_ALU_COMBINE,300};301302typedef struct ppir_instr {303struct list_head list;304int index;305bool printed;306int seq; /* command sequence after schedule */307308ppir_node *slots[PPIR_INSTR_SLOT_NUM];309ppir_const constant[2];310bool is_end;311312/* for scheduler */313struct list_head succ_list;314struct list_head pred_list;315float reg_pressure;316int est; /* earliest start time */317int parent_index;318bool scheduled;319int offset;320int encode_size;321322/* for liveness analysis */323BITSET_WORD *live_set;324uint8_t *live_mask; /* mask for non-ssa registers */325/* live_internal is to mark registers only live within an326* instruction, without propagation */327BITSET_WORD *live_internal;328} ppir_instr;329330typedef struct ppir_block {331struct list_head list;332struct list_head node_list;333struct list_head instr_list;334335struct ppir_block *successors[2];336337struct ppir_compiler *comp;338339/* for scheduler */340int sched_instr_index;341int sched_instr_base;342int index;343} ppir_block;344345typedef struct {346ppir_node node;347ppir_src src[2];348int num_src;349bool cond_gt;350bool cond_eq;351bool cond_lt;352bool negate;353ppir_block *target;354} ppir_branch_node;355356struct ra_regs;357struct lima_fs_compiled_shader;358359typedef struct ppir_compiler {360struct list_head block_list;361struct hash_table_u64 *blocks;362int cur_index;363int cur_instr_index;364365struct list_head reg_list;366int reg_num;367368/* array for searching ssa/reg node */369ppir_node **var_nodes;370unsigned reg_base;371372struct ra_regs *ra;373struct lima_fs_compiled_shader *prog;374bool uses_discard;375376/* for scheduler */377int sched_instr_base;378379/* for regalloc spilling debug */380int force_spilling;381382/* shaderdb */383int num_loops;384int num_spills;385int num_fills;386387ppir_block *discard_block;388ppir_block *current_block;389ppir_block *loop_break_block;390ppir_block *loop_cont_block;391} ppir_compiler;392393void *ppir_node_create(ppir_block *block, ppir_op op, int index, unsigned mask);394void ppir_node_add_dep(ppir_node *succ, ppir_node *pred, ppir_dep_type type);395void ppir_node_remove_dep(ppir_dep *dep);396void ppir_node_delete(ppir_node *node);397void ppir_node_print_prog(ppir_compiler *comp);398void ppir_node_replace_child(ppir_node *parent, ppir_node *old_child, ppir_node *new_child);399void ppir_node_replace_all_succ(ppir_node *dst, ppir_node *src);400void ppir_node_replace_pred(ppir_dep *dep, ppir_node *new_pred);401ppir_dep *ppir_dep_for_pred(ppir_node *node, ppir_node *pred);402/* Assumes that node successors are in the same block */403ppir_node *ppir_node_insert_mov(ppir_node *node);404405static inline bool ppir_node_is_root(ppir_node *node)406{407return list_is_empty(&node->succ_list);408}409410static inline bool ppir_node_is_leaf(ppir_node *node)411{412return list_is_empty(&node->pred_list);413}414415static inline bool ppir_node_has_single_succ(ppir_node *node)416{417return list_is_singular(&node->succ_list)418&& !node->succ_different_block;419}420421bool ppir_node_has_single_src_succ(ppir_node *node);422423static inline ppir_node *ppir_node_first_succ(ppir_node *node)424{425return list_first_entry(&node->succ_list, ppir_dep, succ_link)->succ;426}427428static inline bool ppir_node_has_single_pred(ppir_node *node)429{430return list_is_singular(&node->pred_list);431}432433static inline ppir_node *ppir_node_first_pred(ppir_node *node)434{435return list_first_entry(&node->pred_list, ppir_dep, pred_link)->pred;436}437438#define ppir_node_foreach_succ(node, dep) \439list_for_each_entry(ppir_dep, dep, &node->succ_list, succ_link)440#define ppir_node_foreach_succ_safe(node, dep) \441list_for_each_entry_safe(ppir_dep, dep, &node->succ_list, succ_link)442#define ppir_node_foreach_pred(node, dep) \443list_for_each_entry(ppir_dep, dep, &node->pred_list, pred_link)444#define ppir_node_foreach_pred_safe(node, dep) \445list_for_each_entry_safe(ppir_dep, dep, &node->pred_list, pred_link)446447#define ppir_node_to_alu(node) ((ppir_alu_node *)(node))448#define ppir_node_to_const(node) ((ppir_const_node *)(node))449#define ppir_node_to_load(node) ((ppir_load_node *)(node))450#define ppir_node_to_store(node) ((ppir_store_node *)(node))451#define ppir_node_to_load_texture(node) ((ppir_load_texture_node *)(node))452#define ppir_node_to_discard(node) ((ppir_discard_node *)(node))453#define ppir_node_to_branch(node) ((ppir_branch_node *)(node))454455static inline ppir_dest *ppir_node_get_dest(ppir_node *node)456{457switch (node->type) {458case ppir_node_type_alu:459return &ppir_node_to_alu(node)->dest;460case ppir_node_type_load:461return &ppir_node_to_load(node)->dest;462case ppir_node_type_const:463return &ppir_node_to_const(node)->dest;464case ppir_node_type_load_texture:465return &ppir_node_to_load_texture(node)->dest;466default:467return NULL;468}469}470471static inline int ppir_node_get_src_num(ppir_node *node)472{473switch (node->type) {474case ppir_node_type_alu:475return ppir_node_to_alu(node)->num_src;476case ppir_node_type_branch:477return ppir_node_to_branch(node)->num_src;478case ppir_node_type_load:479return ppir_node_to_load(node)->num_src;480case ppir_node_type_load_texture:481return ppir_node_to_load_texture(node)->num_src;482case ppir_node_type_store:483return 1;484default:485return 0;486}487488return 0;489}490491static inline ppir_src *ppir_node_get_src(ppir_node *node, int idx)492{493if (idx < 0 || idx >= ppir_node_get_src_num(node))494return NULL;495496switch (node->type) {497case ppir_node_type_alu:498return &ppir_node_to_alu(node)->src[idx];499case ppir_node_type_branch:500return &ppir_node_to_branch(node)->src[idx];501case ppir_node_type_load_texture:502return &ppir_node_to_load_texture(node)->src[idx];503case ppir_node_type_load:504return &ppir_node_to_load(node)->src;505case ppir_node_type_store:506return &ppir_node_to_store(node)->src;507default:508break;509}510511return NULL;512}513514static inline ppir_reg *ppir_src_get_reg(ppir_src *src)515{516switch (src->type) {517case ppir_target_ssa:518return src->ssa;519case ppir_target_register:520return src->reg;521default:522return NULL;523}524}525526static inline ppir_reg *ppir_dest_get_reg(ppir_dest *dest)527{528switch (dest->type) {529case ppir_target_ssa:530return &dest->ssa;531case ppir_target_register:532return dest->reg;533default:534return NULL;535}536}537538static inline void ppir_node_target_assign(ppir_src *src, ppir_node *node)539{540ppir_dest *dest = ppir_node_get_dest(node);541src->type = dest->type;542switch (src->type) {543case ppir_target_ssa:544src->ssa = &dest->ssa;545src->node = node;546break;547case ppir_target_register:548src->reg = dest->reg;549/* Registers can be assigned from multiple nodes, so don't keep550* pointer to the node here551*/552src->node = NULL;553break;554case ppir_target_pipeline:555src->pipeline = dest->pipeline;556src->node = node;557break;558}559}560561static inline bool ppir_node_target_equal(ppir_src *src, ppir_dest *dest)562{563if (src->type != dest->type ||564(src->type == ppir_target_ssa && src->ssa != &dest->ssa) ||565(src->type == ppir_target_register && src->reg != dest->reg) ||566(src->type == ppir_target_pipeline && src->pipeline != dest->pipeline))567return false;568569return true;570}571572static inline int ppir_target_get_src_reg_index(ppir_src *src)573{574switch (src->type) {575case ppir_target_ssa:576if (src->ssa)577return src->ssa->index;578break;579case ppir_target_register:580if (src->reg)581return src->reg->index;582break;583case ppir_target_pipeline:584if (src->pipeline == ppir_pipeline_reg_discard)585return 15 * 4;586return (src->pipeline + 12) * 4;587}588589return -1;590}591592static inline int ppir_target_get_dest_reg_index(ppir_dest *dest)593{594switch (dest->type) {595case ppir_target_ssa:596return dest->ssa.index;597case ppir_target_register:598return dest->reg->index;599case ppir_target_pipeline:600if (dest->pipeline == ppir_pipeline_reg_discard)601return 15 * 4;602return (dest->pipeline + 12) * 4;603}604605return -1;606}607608static inline int ppir_src_get_mask(ppir_src *src)609{610ppir_reg *reg = ppir_src_get_reg(src);611int mask = 0;612613for (int i = 0; i < reg->num_components; i++)614mask |= (1 << src->swizzle[i]);615616return mask;617}618619static inline bool ppir_target_is_scalar(ppir_dest *dest)620{621switch (dest->type) {622case ppir_target_ssa:623return dest->ssa.num_components == 1;624case ppir_target_register:625/* only one bit in mask is set */626if ((dest->write_mask & 0x3) == 0x3 ||627(dest->write_mask & 0x5) == 0x5 ||628(dest->write_mask & 0x9) == 0x9 ||629(dest->write_mask & 0x6) == 0x6 ||630(dest->write_mask & 0xa) == 0xa ||631(dest->write_mask & 0xc) == 0xc)632return false;633else634return true;635case ppir_target_pipeline:636if (dest->pipeline == ppir_pipeline_reg_fmul)637return true;638else639return false;640default:641return false;642}643}644645static inline bool ppir_node_schedulable_slot(ppir_node *node,646enum ppir_instr_slot slot)647{648int *slots = ppir_op_infos[node->op].slots;649for (int i = 0; slots[i] != PPIR_INSTR_SLOT_END; i++)650if (slots[i] == slot)651return true;652653return false;654}655656ppir_instr *ppir_instr_create(ppir_block *block);657bool ppir_instr_insert_node(ppir_instr *instr, ppir_node *node);658void ppir_instr_add_dep(ppir_instr *succ, ppir_instr *pred);659void ppir_instr_print_list(ppir_compiler *comp);660void ppir_instr_print_dep(ppir_compiler *comp);661void ppir_instr_insert_mul_node(ppir_node *add, ppir_node *mul);662663#define ppir_instr_foreach_succ(instr, dep) \664list_for_each_entry(ppir_dep, dep, &instr->succ_list, succ_link)665#define ppir_instr_foreach_succ_safe(instr, dep) \666list_for_each_entry_safe(ppir_dep, dep, &instr->succ_list, succ_link)667#define ppir_instr_foreach_pred(instr, dep) \668list_for_each_entry(ppir_dep, dep, &instr->pred_list, pred_link)669#define ppir_instr_foreach_pred_safe(instr, dep) \670list_for_each_entry_safe(ppir_dep, dep, &instr->pred_list, pred_link)671672static inline bool ppir_instr_is_root(ppir_instr *instr)673{674return list_is_empty(&instr->succ_list);675}676677static inline bool ppir_instr_is_leaf(ppir_instr *instr)678{679return list_is_empty(&instr->pred_list);680}681682bool ppir_lower_prog(ppir_compiler *comp);683bool ppir_node_to_instr(ppir_compiler *comp);684bool ppir_schedule_prog(ppir_compiler *comp);685bool ppir_regalloc_prog(ppir_compiler *comp);686bool ppir_codegen_prog(ppir_compiler *comp);687void ppir_liveness_analysis(ppir_compiler *comp);688689static inline unsigned int reg_mask_size(unsigned int num_reg)690{691return (num_reg + 1) / 2;692}693694static inline uint8_t get_reg_mask(uint8_t *set, unsigned index)695{696unsigned int i = index / 2;697unsigned int shift = index % 2 ? 4 : 0;698uint8_t mask = 0x0f << shift;699return (set[i] & mask) >> shift;700}701702static inline void set_reg_mask(uint8_t *set, unsigned int index, uint8_t bits)703{704unsigned int i = index / 2;705unsigned int shift = index % 2 ? 4 : 0;706uint8_t mask = 0x0f << shift;707set[i] &= ~mask;708set[i] |= (bits << shift);709}710711#endif712713714