Path: blob/21.2-virgl/src/gallium/auxiliary/gallivm/lp_bld_nir.h
4565 views
/**************************************************************************1*2* Copyright 2019 Red Hat.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR 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*23**************************************************************************/2425#ifndef LP_BLD_NIR_H26#define LP_BLD_NIR_H2728#include "gallivm/lp_bld.h"29#include "gallivm/lp_bld_limits.h"30#include "lp_bld_type.h"3132#include "gallivm/lp_bld_tgsi.h"33#include "nir.h"3435struct nir_shader;3637void lp_build_nir_soa(struct gallivm_state *gallivm,38struct nir_shader *shader,39const struct lp_build_tgsi_params *params,40LLVMValueRef (*outputs)[4]);4142struct lp_build_nir_context43{44struct lp_build_context base;45struct lp_build_context uint_bld;46struct lp_build_context int_bld;47struct lp_build_context uint8_bld;48struct lp_build_context int8_bld;49struct lp_build_context uint16_bld;50struct lp_build_context int16_bld;51struct lp_build_context dbl_bld;52struct lp_build_context uint64_bld;53struct lp_build_context int64_bld;5455LLVMValueRef *ssa_defs;56struct hash_table *regs;57struct hash_table *vars;5859/** Value range analysis hash table used in code generation. */60struct hash_table *range_ht;6162nir_shader *shader;6364void (*load_ubo)(struct lp_build_nir_context *bld_base,65unsigned nc,66unsigned bit_size,67bool offset_is_uniform,68LLVMValueRef index, LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);6970void (*load_kernel_arg)(struct lp_build_nir_context *bld_base,71unsigned nc,72unsigned bit_size,73unsigned offset_bit_size,74bool offset_is_uniform,75LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);7677void (*load_global)(struct lp_build_nir_context *bld_base,78unsigned nc, unsigned bit_size,79unsigned offset_bit_size,80LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);8182void (*store_global)(struct lp_build_nir_context *bld_base,83unsigned writemask,84unsigned nc, unsigned bit_size,85unsigned addr_bit_size,86LLVMValueRef addr, LLVMValueRef dst);8788void (*atomic_global)(struct lp_build_nir_context *bld_base,89nir_intrinsic_op op,90unsigned addr_bit_size,91unsigned val_bit_size,92LLVMValueRef addr,93LLVMValueRef val, LLVMValueRef val2,94LLVMValueRef *result);9596/* for SSBO and shared memory */97void (*load_mem)(struct lp_build_nir_context *bld_base,98unsigned nc, unsigned bit_size,99LLVMValueRef index, LLVMValueRef offset, LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);100void (*store_mem)(struct lp_build_nir_context *bld_base,101unsigned writemask, unsigned nc, unsigned bit_size,102LLVMValueRef index, LLVMValueRef offset, LLVMValueRef dst);103104void (*atomic_mem)(struct lp_build_nir_context *bld_base,105nir_intrinsic_op op,106unsigned bit_size,107LLVMValueRef index, LLVMValueRef offset,108LLVMValueRef val, LLVMValueRef val2,109LLVMValueRef *result);110111void (*barrier)(struct lp_build_nir_context *bld_base);112113void (*image_op)(struct lp_build_nir_context *bld_base,114struct lp_img_params *params);115void (*image_size)(struct lp_build_nir_context *bld_base,116struct lp_sampler_size_query_params *params);117LLVMValueRef (*get_ssbo_size)(struct lp_build_nir_context *bld_base,118LLVMValueRef index);119120void (*load_var)(struct lp_build_nir_context *bld_base,121nir_variable_mode deref_mode,122unsigned num_components,123unsigned bit_size,124nir_variable *var,125unsigned vertex_index,126LLVMValueRef indir_vertex_index,127unsigned const_index,128LLVMValueRef indir_index,129LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);130void (*store_var)(struct lp_build_nir_context *bld_base,131nir_variable_mode deref_mode,132unsigned num_components,133unsigned bit_size,134nir_variable *var,135unsigned writemask,136LLVMValueRef indir_vertex_index,137unsigned const_index,138LLVMValueRef indir_index,139LLVMValueRef dst);140141LLVMValueRef (*load_reg)(struct lp_build_nir_context *bld_base,142struct lp_build_context *reg_bld,143const nir_reg_src *reg,144LLVMValueRef indir_src,145LLVMValueRef reg_storage);146void (*store_reg)(struct lp_build_nir_context *bld_base,147struct lp_build_context *reg_bld,148const nir_reg_dest *reg,149unsigned writemask,150LLVMValueRef indir_src,151LLVMValueRef reg_storage,152LLVMValueRef dst[NIR_MAX_VEC_COMPONENTS]);153154void (*load_scratch)(struct lp_build_nir_context *bld_base,155unsigned nc, unsigned bit_size,156LLVMValueRef offset,157LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);158void (*store_scratch)(struct lp_build_nir_context *bld_base,159unsigned writemask, unsigned nc,160unsigned bit_size, LLVMValueRef offset,161LLVMValueRef val);162163void (*emit_var_decl)(struct lp_build_nir_context *bld_base,164nir_variable *var);165166void (*tex)(struct lp_build_nir_context *bld_base,167struct lp_sampler_params *params);168169void (*tex_size)(struct lp_build_nir_context *bld_base,170struct lp_sampler_size_query_params *params);171172void (*sysval_intrin)(struct lp_build_nir_context *bld_base,173nir_intrinsic_instr *instr,174LLVMValueRef result[NIR_MAX_VEC_COMPONENTS]);175void (*discard)(struct lp_build_nir_context *bld_base,176LLVMValueRef cond);177178void (*bgnloop)(struct lp_build_nir_context *bld_base);179void (*endloop)(struct lp_build_nir_context *bld_base);180void (*if_cond)(struct lp_build_nir_context *bld_base, LLVMValueRef cond);181void (*else_stmt)(struct lp_build_nir_context *bld_base);182void (*endif_stmt)(struct lp_build_nir_context *bld_base);183void (*break_stmt)(struct lp_build_nir_context *bld_base);184void (*continue_stmt)(struct lp_build_nir_context *bld_base);185186void (*emit_vertex)(struct lp_build_nir_context *bld_base, uint32_t stream_id);187void (*end_primitive)(struct lp_build_nir_context *bld_base, uint32_t stream_id);188189void (*vote)(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef dst[4]);190void (*elect)(struct lp_build_nir_context *bld_base, LLVMValueRef dst[4]);191void (*reduce)(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef dst[4]);192void (*ballot)(struct lp_build_nir_context *bld_base, LLVMValueRef src, nir_intrinsic_instr *instr, LLVMValueRef dst[4]);193void (*read_invocation)(struct lp_build_nir_context *bld_base,194LLVMValueRef src, unsigned bit_size, LLVMValueRef invoc,195LLVMValueRef dst[4]);196void (*helper_invocation)(struct lp_build_nir_context *bld_base, LLVMValueRef *dst);197198void (*interp_at)(struct lp_build_nir_context *bld_base,199unsigned num_components,200nir_variable *var,201bool centroid, bool sample,202unsigned const_index,203LLVMValueRef indir_index,204LLVMValueRef offsets[2], LLVMValueRef dst[4]);205// LLVMValueRef main_function206};207208struct lp_build_nir_soa_context209{210struct lp_build_nir_context bld_base;211212/* Builder for scalar elements of shader's data type (float) */213struct lp_build_context elem_bld;214struct lp_build_context uint_elem_bld;215216LLVMValueRef consts_ptr;217LLVMValueRef const_sizes_ptr;218LLVMValueRef consts[LP_MAX_TGSI_CONST_BUFFERS];219LLVMValueRef consts_sizes[LP_MAX_TGSI_CONST_BUFFERS];220const LLVMValueRef (*inputs)[TGSI_NUM_CHANNELS];221LLVMValueRef (*outputs)[TGSI_NUM_CHANNELS];222LLVMValueRef context_ptr;223LLVMValueRef thread_data_ptr;224225LLVMValueRef ssbo_ptr;226LLVMValueRef ssbo_sizes_ptr;227LLVMValueRef ssbos[LP_MAX_TGSI_SHADER_BUFFERS];228LLVMValueRef ssbo_sizes[LP_MAX_TGSI_SHADER_BUFFERS];229230LLVMValueRef shared_ptr;231LLVMValueRef scratch_ptr;232unsigned scratch_size;233234const struct lp_build_coro_suspend_info *coro;235236const struct lp_build_sampler_soa *sampler;237const struct lp_build_image_soa *image;238239const struct lp_build_gs_iface *gs_iface;240const struct lp_build_tcs_iface *tcs_iface;241const struct lp_build_tes_iface *tes_iface;242const struct lp_build_fs_iface *fs_iface;243LLVMValueRef emitted_prims_vec_ptr[PIPE_MAX_VERTEX_STREAMS];244LLVMValueRef total_emitted_vertices_vec_ptr[PIPE_MAX_VERTEX_STREAMS];245LLVMValueRef emitted_vertices_vec_ptr[PIPE_MAX_VERTEX_STREAMS];246LLVMValueRef max_output_vertices_vec;247struct lp_bld_tgsi_system_values system_values;248249nir_variable_mode indirects;250struct lp_build_mask_context *mask;251struct lp_exec_mask exec_mask;252253/* We allocate/use this array of inputs if (indirects & nir_var_shader_in) is254* set. The inputs[] array above is unused then.255*/256LLVMValueRef inputs_array;257258LLVMValueRef kernel_args_ptr;259unsigned gs_vertex_streams;260};261262bool263lp_build_nir_llvm(struct lp_build_nir_context *bld_base,264struct nir_shader *nir);265266void lp_build_opt_nir(struct nir_shader *nir);267268static inline LLVMValueRef269lp_nir_array_build_gather_values(LLVMBuilderRef builder,270LLVMValueRef * values,271unsigned value_count)272{273LLVMTypeRef arr_type = LLVMArrayType(LLVMTypeOf(values[0]), value_count);274LLVMValueRef arr = LLVMGetUndef(arr_type);275unsigned i;276277for (i = 0; i < value_count; i++) {278arr = LLVMBuildInsertValue(builder, arr, values[i], i, "");279}280return arr;281}282283static inline struct lp_build_context *get_flt_bld(struct lp_build_nir_context *bld_base,284unsigned op_bit_size)285{286switch (op_bit_size) {287case 64:288return &bld_base->dbl_bld;289default:290case 32:291return &bld_base->base;292}293}294295static inline struct lp_build_context *get_int_bld(struct lp_build_nir_context *bld_base,296bool is_unsigned,297unsigned op_bit_size)298{299if (is_unsigned) {300switch (op_bit_size) {301case 64:302return &bld_base->uint64_bld;303case 32:304default:305return &bld_base->uint_bld;306case 16:307return &bld_base->uint16_bld;308case 8:309return &bld_base->uint8_bld;310}311} else {312switch (op_bit_size) {313case 64:314return &bld_base->int64_bld;315default:316case 32:317return &bld_base->int_bld;318case 16:319return &bld_base->int16_bld;320case 8:321return &bld_base->int8_bld;322}323}324}325326#endif327328329