/*
* Copyright 2016 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
#define SI_SHADER_PRIVATE_H
#include "si_shader.h"
-#include "gallivm/lp_bld_init.h"
-#include "gallivm/lp_bld_tgsi.h"
-#include "tgsi/tgsi_parse.h"
-#include "ac_llvm_util.h"
-#include "ac_llvm_build.h"
-
-#include <llvm-c/Core.h>
-#include <llvm-c/TargetMachine.h>
+#include "ac_shader_abi.h"
struct pipe_debug_callback;
-struct ac_shader_binary;
-#define RADEON_LLVM_MAX_INPUT_SLOTS 32
#define RADEON_LLVM_MAX_INPUTS 32 * 4
-#define RADEON_LLVM_MAX_OUTPUTS 32 * 4
-
-#define RADEON_LLVM_INITIAL_CF_DEPTH 4
-#define RADEON_LLVM_MAX_SYSTEM_VALUES 11
-#define RADEON_LLVM_MAX_ADDRS 16
+/* Ideally pass the sample mask input to the PS epilog as v14, which
+ * is its usual location, so that the shader doesn't have to add v_mov.
+ */
+#define PS_EPILOG_SAMPLEMASK_MIN_LOC 14
-struct si_llvm_flow;
+struct si_shader_output_values {
+ LLVMValueRef values[4];
+ unsigned semantic_name;
+ unsigned semantic_index;
+ ubyte vertex_stream[4];
+};
struct si_shader_context {
- struct lp_build_tgsi_context bld_base;
- struct gallivm_state gallivm;
struct ac_llvm_context ac;
struct si_shader *shader;
struct si_screen *screen;
unsigned num_images;
unsigned num_samplers;
- /* Whether the prolog will be compiled separately. */
- bool separate_prolog;
-
- /** This function is responsible for initilizing the inputs array and will be
- * called once for each input declared in the TGSI shader.
- */
- void (*load_input)(struct si_shader_context *,
- unsigned input_index,
- const struct tgsi_full_declaration *decl,
- LLVMValueRef out[4]);
-
- void (*load_system_value)(struct si_shader_context *,
- unsigned index,
- const struct tgsi_full_declaration *decl);
-
- void (*declare_memory_region)(struct si_shader_context *,
- const struct tgsi_full_declaration *decl);
-
- /** This array contains the input values for the shader. Typically these
- * values will be in the form of a target intrinsic that will inform the
- * backend how to load the actual inputs to the shader.
- */
- struct tgsi_full_declaration input_decls[RADEON_LLVM_MAX_INPUT_SLOTS];
- LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS];
- LLVMValueRef outputs[RADEON_LLVM_MAX_OUTPUTS][TGSI_NUM_CHANNELS];
- LLVMValueRef addrs[RADEON_LLVM_MAX_ADDRS][TGSI_NUM_CHANNELS];
-
- /** This pointer is used to contain the temporary values.
- * The amount of temporary used in tgsi can't be bound to a max value and
- * thus we must allocate this array at runtime.
- */
- LLVMValueRef *temps;
- unsigned temps_count;
- LLVMValueRef system_values[RADEON_LLVM_MAX_SYSTEM_VALUES];
-
- LLVMValueRef *imms;
- unsigned imms_num;
-
- struct si_llvm_flow *flow;
- unsigned flow_depth;
- unsigned flow_depth_max;
+ struct ac_shader_args args;
+ struct ac_shader_abi abi;
- struct tgsi_array_info *temp_arrays;
- LLVMValueRef *temp_array_allocas;
+ LLVMValueRef inputs[RADEON_LLVM_MAX_INPUTS];
- LLVMValueRef undef_alloca;
+ LLVMBasicBlockRef merged_wrap_if_entry_block;
+ int merged_wrap_if_label;
LLVMValueRef main_fn;
LLVMTypeRef return_type;
- /* Parameter indices for LLVMGetParam. */
- int param_rw_buffers;
- int param_const_and_shader_buffers;
- int param_samplers_and_images;
+ struct ac_arg const_and_shader_buffers;
+ struct ac_arg samplers_and_images;
+
+ /* For merged shaders, the per-stage descriptors for the stage other
+ * than the one we're processing, used to pass them through from the
+ * first stage to the second.
+ */
+ struct ac_arg other_const_and_shader_buffers;
+ struct ac_arg other_samplers_and_images;
+
+ struct ac_arg rw_buffers;
+ struct ac_arg bindless_samplers_and_images;
/* Common inputs for merged shaders. */
- int param_merged_wave_info;
- int param_merged_scratch_offset;
+ struct ac_arg merged_wave_info;
+ struct ac_arg merged_scratch_offset;
/* API VS */
- int param_vertex_buffers;
- int param_base_vertex;
- int param_start_instance;
- int param_draw_id;
- int param_vertex_id;
- int param_rel_auto_id;
- int param_vs_prim_id;
- int param_instance_id;
- int param_vertex_index0;
+ struct ac_arg vertex_buffers;
+ struct ac_arg vb_descriptors[5];
+ struct ac_arg rel_auto_id;
+ struct ac_arg vs_prim_id;
+ struct ac_arg vertex_index0;
/* VS states and layout of LS outputs / TCS inputs at the end
* [0] = clamp vertex color
* [1] = indexed
- * [8:20] = stride between patches in DW = num_inputs * num_vertices * 4
- * max = 32*32*4 + 32*4
+ * [2:3] = NGG: output primitive type
+ * [4:5] = NGG: provoking vertex index
+ * [6] = NGG: streamout queries enabled
+ * [11:23] = stride between patches in DW = num_inputs * num_vertices * 4
+ * max = 32*32*4 + 32*4
* [24:31] = stride between vertices in DW = num_inputs * 4
* max = 32*4
*/
- int param_vs_state_bits;
+ struct ac_arg vs_state_bits;
+ struct ac_arg vs_blit_inputs;
/* HW VS */
- int param_streamout_config;
- int param_streamout_write_index;
- int param_streamout_offset[4];
+ struct ac_arg streamout_config;
+ struct ac_arg streamout_write_index;
+ struct ac_arg streamout_offset[4];
/* API TCS & TES */
/* Layout of TCS outputs in the offchip buffer
* [12:31] = the offset of per patch attributes in the buffer in bytes.
* max = NUM_PATCHES*32*32*16
*/
- int param_tcs_offchip_layout;
+ struct ac_arg tcs_offchip_layout;
/* API TCS */
/* Offsets where TCS outputs and TCS patch outputs live in LDS:
* [16:31] = TCS output patch0 offset for per-patch / 16
* max = (NUM_PATCHES + 1) * 32*32
*/
- int param_tcs_out_lds_offsets;
+ struct ac_arg tcs_out_lds_offsets;
/* Layout of TCS outputs / TES inputs:
* [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
* max = 32*32*4 + 32*4
- * [13:20] = stride between output vertices in DW = num_inputs * 4
- * max = 32*4
- * [26:31] = gl_PatchVerticesIn, max = 32
+ * [13:18] = gl_PatchVerticesIn, max = 32
+ * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
*/
- int param_tcs_out_lds_layout;
- int param_tcs_offchip_addr_base64k;
- int param_tcs_factor_addr_base64k;
- int param_tcs_offchip_offset;
- int param_tcs_factor_offset;
- int param_tcs_patch_id;
- int param_tcs_rel_ids;
+ struct ac_arg tcs_out_lds_layout;
+ struct ac_arg tcs_offchip_offset;
+ struct ac_arg tcs_factor_offset;
/* API TES */
- int param_tes_u;
- int param_tes_v;
- int param_tes_rel_patch_id;
- int param_tes_patch_id;
+ struct ac_arg tes_offchip_addr;
+ struct ac_arg tes_u;
+ struct ac_arg tes_v;
+ struct ac_arg tes_rel_patch_id;
/* HW ES */
- int param_es2gs_offset;
+ struct ac_arg es2gs_offset;
+ /* HW GS */
+ /* On gfx10:
+ * - bits 0..11: ordered_wave_id
+ * - bits 12..20: number of vertices in group
+ * - bits 22..30: number of primitives in group
+ */
+ struct ac_arg gs_tg_info;
/* API GS */
- int param_gs2vs_offset;
- int param_gs_wave_id; /* GFX6 */
- int param_gs_vtx0_offset; /* in dwords (GFX6) */
- int param_gs_vtx1_offset; /* in dwords (GFX6) */
- int param_gs_prim_id;
- int param_gs_vtx2_offset; /* in dwords (GFX6) */
- int param_gs_vtx3_offset; /* in dwords (GFX6) */
- int param_gs_vtx4_offset; /* in dwords (GFX6) */
- int param_gs_vtx5_offset; /* in dwords (GFX6) */
- int param_gs_instance_id;
- int param_gs_vtx01_offset; /* in dwords (GFX9) */
- int param_gs_vtx23_offset; /* in dwords (GFX9) */
- int param_gs_vtx45_offset; /* in dwords (GFX9) */
+ struct ac_arg gs2vs_offset;
+ struct ac_arg gs_wave_id; /* GFX6 */
+ struct ac_arg gs_vtx_offset[6]; /* in dwords (GFX6) */
+ struct ac_arg gs_vtx01_offset; /* in dwords (GFX9) */
+ struct ac_arg gs_vtx23_offset; /* in dwords (GFX9) */
+ struct ac_arg gs_vtx45_offset; /* in dwords (GFX9) */
+ /* PS */
+ struct ac_arg pos_fixed_pt;
/* CS */
- int param_grid_size;
- int param_block_size;
- int param_block_id[3];
- int param_thread_id;
+ struct ac_arg block_size;
+ struct ac_arg cs_user_data;
- LLVMTargetMachineRef tm;
-
- unsigned range_md_kind;
- unsigned fpmath_md_kind;
- LLVMValueRef fpmath_md_2p5_ulp;
+ struct ac_llvm_compiler *compiler;
/* Preloaded descriptors. */
LLVMValueRef esgs_ring;
LLVMValueRef gsvs_ring[4];
+ LLVMValueRef tess_offchip_ring;
- LLVMValueRef lds;
+ LLVMValueRef invoc0_tess_factors[6]; /* outer[4], inner[2] */
LLVMValueRef gs_next_vertex[4];
+ LLVMValueRef gs_curprim_verts[4];
+ LLVMValueRef gs_generated_prims[4];
+ LLVMValueRef gs_ngg_emit;
+ LLVMValueRef gs_ngg_scratch;
LLVMValueRef postponed_kill;
LLVMValueRef return_value;
LLVMValueRef i32_0;
LLVMValueRef i32_1;
-
- LLVMValueRef shared_memory;
+ LLVMValueRef i1false;
+ LLVMValueRef i1true;
};
static inline struct si_shader_context *
-si_shader_context(struct lp_build_tgsi_context *bld_base)
+si_shader_context_from_abi(struct ac_shader_abi *abi)
{
- return (struct si_shader_context*)bld_base;
+ struct si_shader_context *ctx = NULL;
+ return container_of(abi, ctx, abi);
}
-void si_llvm_add_attribute(LLVMValueRef F, const char *name, int value);
-
-unsigned si_llvm_compile(LLVMModuleRef M, struct ac_shader_binary *binary,
- LLVMTargetMachineRef tm,
- struct pipe_debug_callback *debug);
-
-LLVMTypeRef tgsi2llvmtype(struct lp_build_tgsi_context *bld_base,
- enum tgsi_opcode_type type);
-
-LLVMValueRef bitcast(struct lp_build_tgsi_context *bld_base,
- enum tgsi_opcode_type type, LLVMValueRef value);
-
-LLVMValueRef si_llvm_bound_index(struct si_shader_context *ctx,
- LLVMValueRef index,
- unsigned num);
-
void si_llvm_context_init(struct si_shader_context *ctx,
struct si_screen *sscreen,
- LLVMTargetMachineRef tm);
-void si_llvm_context_set_tgsi(struct si_shader_context *ctx,
- struct si_shader *shader);
+ struct ac_llvm_compiler *compiler,
+ unsigned wave_size);
+void si_llvm_context_set_ir(struct si_shader_context *ctx,
+ struct si_shader *shader);
-void si_llvm_create_func(struct si_shader_context *ctx,
- const char *name,
+void si_llvm_create_func(struct si_shader_context *ctx, const char *name,
LLVMTypeRef *return_types, unsigned num_return_elems,
- LLVMTypeRef *ParamTypes, unsigned ParamCount);
+ unsigned max_workgroup_size);
void si_llvm_dispose(struct si_shader_context *ctx);
void si_llvm_optimize_module(struct si_shader_context *ctx);
-LLVMValueRef si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context *bld_base,
- enum tgsi_opcode_type type,
- LLVMValueRef ptr,
- LLVMValueRef ptr2);
-
-LLVMValueRef si_llvm_emit_fetch(struct lp_build_tgsi_context *bld_base,
- const struct tgsi_full_src_register *reg,
- enum tgsi_opcode_type type,
- unsigned swizzle);
-
-void si_llvm_emit_store(struct lp_build_tgsi_context *bld_base,
- const struct tgsi_full_instruction *inst,
- const struct tgsi_opcode_info *info,
- LLVMValueRef dst[4]);
-
-/* Combine these with & instead of |. */
-#define NOOP_WAITCNT 0xf7f
-#define LGKM_CNT 0x07f
-#define VM_CNT 0xf70
-
-void si_emit_waitcnt(struct si_shader_context *ctx, unsigned simm16);
-
-LLVMValueRef si_get_bounded_indirect_index(struct si_shader_context *ctx,
- const struct tgsi_ind_register *ind,
- int rel_index, unsigned num);
-
-LLVMTypeRef si_const_array(LLVMTypeRef elem_type, int num_elements);
-
-void si_shader_context_init_alu(struct lp_build_tgsi_context *bld_base);
-void si_shader_context_init_mem(struct si_shader_context *ctx);
+LLVMValueRef si_nir_load_input_tes(struct ac_shader_abi *abi,
+ LLVMTypeRef type,
+ LLVMValueRef vertex_index,
+ LLVMValueRef param_index,
+ unsigned const_index,
+ unsigned location,
+ unsigned driver_location,
+ unsigned component,
+ unsigned num_components,
+ bool is_patch,
+ bool is_compact,
+ bool load_input);
+bool si_is_merged_shader(struct si_shader_context *ctx);
+LLVMValueRef si_get_sample_id(struct si_shader_context *ctx);
+LLVMValueRef si_buffer_load_const(struct si_shader_context *ctx,
+ LLVMValueRef resource, LLVMValueRef offset);
+void si_llvm_build_ret(struct si_shader_context *ctx, LLVMValueRef ret);
+LLVMValueRef si_prolog_get_rw_buffers(struct si_shader_context *ctx);
+LLVMValueRef si_build_gather_64bit(struct si_shader_context *ctx,
+ LLVMTypeRef type, LLVMValueRef val1,
+ LLVMValueRef val2);
+void si_llvm_emit_barrier(struct si_shader_context *ctx);
+void si_llvm_declare_esgs_ring(struct si_shader_context *ctx);
+void si_declare_compute_memory(struct si_shader_context *ctx);
+LLVMValueRef si_get_primitive_id(struct si_shader_context *ctx,
+ unsigned swizzle);
+void si_llvm_export_vs(struct si_shader_context *ctx,
+ struct si_shader_output_values *outputs,
+ unsigned noutput);
+void si_emit_streamout_output(struct si_shader_context *ctx,
+ LLVMValueRef const *so_buffers,
+ LLVMValueRef const *so_write_offsets,
+ struct pipe_stream_output *stream_out,
+ struct si_shader_output_values *shader_out);
+void si_add_arg_checked(struct ac_shader_args *args,
+ enum ac_arg_regfile file,
+ unsigned registers, enum ac_arg_type type,
+ struct ac_arg *arg,
+ unsigned idx);
+
+void si_llvm_load_input_vs(
+ struct si_shader_context *ctx,
+ unsigned input_index,
+ LLVMValueRef out[4]);
+
+bool si_nir_build_llvm(struct si_shader_context *ctx, struct nir_shader *nir);
+
+LLVMValueRef si_unpack_param(struct si_shader_context *ctx,
+ struct ac_arg param, unsigned rshift,
+ unsigned bitwidth);
+void si_build_wrapper_function(struct si_shader_context *ctx, LLVMValueRef *parts,
+ unsigned num_parts, unsigned main_part,
+ unsigned next_shader_first_part);
+bool si_need_ps_prolog(const union si_shader_part_key *key);
+void si_get_ps_prolog_key(struct si_shader *shader,
+ union si_shader_part_key *key,
+ bool separate_prolog);
+void si_get_ps_epilog_key(struct si_shader *shader,
+ union si_shader_part_key *key);
+LLVMValueRef si_insert_input_ret(struct si_shader_context *ctx, LLVMValueRef ret,
+ struct ac_arg param, unsigned return_index);
+LLVMValueRef si_insert_input_ret_float(struct si_shader_context *ctx, LLVMValueRef ret,
+ struct ac_arg param, unsigned return_index);
+LLVMValueRef si_insert_input_ptr(struct si_shader_context *ctx, LLVMValueRef ret,
+ struct ac_arg param, unsigned return_index);
+int si_compile_llvm(struct si_screen *sscreen,
+ struct si_shader_binary *binary,
+ struct ac_shader_config *conf,
+ struct ac_llvm_compiler *compiler,
+ struct ac_llvm_context *ac,
+ struct pipe_debug_callback *debug,
+ enum pipe_shader_type shader_type,
+ const char *name,
+ bool less_optimized);
+void si_fix_resource_usage(struct si_screen *sscreen, struct si_shader *shader);
+void si_llvm_emit_streamout(struct si_shader_context *ctx,
+ struct si_shader_output_values *outputs,
+ unsigned noutput, unsigned stream);
+void si_create_function(struct si_shader_context *ctx);
+
+void gfx10_ngg_build_sendmsg_gs_alloc_req(struct si_shader_context *ctx);
+void gfx10_emit_ngg_epilogue(struct ac_shader_abi *abi,
+ unsigned max_outputs,
+ LLVMValueRef *addrs);
+void gfx10_ngg_gs_emit_vertex(struct si_shader_context *ctx,
+ unsigned stream,
+ LLVMValueRef *addrs);
+void gfx10_ngg_gs_emit_prologue(struct si_shader_context *ctx);
+void gfx10_ngg_gs_emit_epilogue(struct si_shader_context *ctx);
+void gfx10_ngg_calculate_subgroup_info(struct si_shader *shader);
+
+/* si_shader_llvm_gs.c */
+LLVMValueRef si_is_es_thread(struct si_shader_context *ctx);
+LLVMValueRef si_is_gs_thread(struct si_shader_context *ctx);
+void si_llvm_emit_es_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
+ LLVMValueRef *addrs);
+void si_preload_esgs_ring(struct si_shader_context *ctx);
+void si_preload_gs_rings(struct si_shader_context *ctx);
+void si_llvm_build_gs_prolog(struct si_shader_context *ctx,
+ union si_shader_part_key *key);
+void si_llvm_init_gs_callbacks(struct si_shader_context *ctx);
+
+/* si_shader_llvm_tess.c */
+void si_llvm_preload_tes_rings(struct si_shader_context *ctx);
+void si_llvm_emit_ls_epilogue(struct ac_shader_abi *abi, unsigned max_outputs,
+ LLVMValueRef *addrs);
+void si_llvm_build_tcs_epilog(struct si_shader_context *ctx,
+ union si_shader_part_key *key);
+void si_llvm_init_tcs_callbacks(struct si_shader_context *ctx);
+void si_llvm_init_tes_callbacks(struct si_shader_context *ctx);
+
+/* si_shader_llvm_ps.c */
+void si_llvm_build_ps_prolog(struct si_shader_context *ctx,
+ union si_shader_part_key *key);
+void si_llvm_build_ps_epilog(struct si_shader_context *ctx,
+ union si_shader_part_key *key);
+void si_llvm_build_monolithic_ps(struct si_shader_context *ctx,
+ struct si_shader *shader);
+void si_llvm_init_ps_callbacks(struct si_shader_context *ctx);
+
+/* si_shader_llvm_resources.c */
+void si_llvm_init_resource_callbacks(struct si_shader_context *ctx);
#endif