2 * Copyright 2016 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #ifndef SI_SHADER_PRIVATE_H
26 #define SI_SHADER_PRIVATE_H
28 #include "si_shader.h"
29 #include "gallivm/lp_bld_flow.h"
30 #include "gallivm/lp_bld_init.h"
31 #include "gallivm/lp_bld_tgsi.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "ac_shader_abi.h"
35 #include <llvm-c/Core.h>
36 #include <llvm-c/TargetMachine.h>
38 struct pipe_debug_callback
;
40 #define RADEON_LLVM_MAX_INPUT_SLOTS 32
41 #define RADEON_LLVM_MAX_INPUTS 32 * 4
42 #define RADEON_LLVM_MAX_OUTPUTS 32 * 4
44 #define RADEON_LLVM_MAX_SYSTEM_VALUES 11
45 #define RADEON_LLVM_MAX_ADDRS 16
53 * Used to collect types and other info about arguments of the LLVM function
54 * before the function is created.
56 struct si_function_info
{
57 LLVMTypeRef types
[100];
58 LLVMValueRef
*assign
[100];
59 unsigned num_sgpr_params
;
63 struct si_shader_output_values
{
64 LLVMValueRef values
[4];
65 unsigned semantic_name
;
66 unsigned semantic_index
;
67 ubyte vertex_stream
[4];
70 struct si_shader_context
{
71 struct lp_build_tgsi_context bld_base
;
72 struct gallivm_state gallivm
;
73 struct ac_llvm_context ac
;
74 struct si_shader
*shader
;
75 struct si_screen
*screen
;
77 unsigned type
; /* PIPE_SHADER_* specifies the type of shader. */
79 /* For clamping the non-constant index in resource indexing: */
80 unsigned num_const_buffers
;
81 unsigned num_shader_buffers
;
83 unsigned num_samplers
;
85 struct ac_shader_abi abi
;
87 /** This function is responsible for initilizing the inputs array and will be
88 * called once for each input declared in the TGSI shader.
90 void (*load_input
)(struct si_shader_context
*,
92 const struct tgsi_full_declaration
*decl
,
95 /** This array contains the input values for the shader. Typically these
96 * values will be in the form of a target intrinsic that will inform the
97 * backend how to load the actual inputs to the shader.
99 struct tgsi_full_declaration input_decls
[RADEON_LLVM_MAX_INPUT_SLOTS
];
100 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
];
101 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
][TGSI_NUM_CHANNELS
];
102 LLVMValueRef addrs
[RADEON_LLVM_MAX_ADDRS
][TGSI_NUM_CHANNELS
];
104 /** This pointer is used to contain the temporary values.
105 * The amount of temporary used in tgsi can't be bound to a max value and
106 * thus we must allocate this array at runtime.
109 unsigned temps_count
;
110 LLVMValueRef system_values
[RADEON_LLVM_MAX_SYSTEM_VALUES
];
115 LLVMBasicBlockRef merged_wrap_if_entry_block
;
116 int merged_wrap_if_label
;
118 struct tgsi_array_info
*temp_arrays
;
119 LLVMValueRef
*temp_array_allocas
;
121 LLVMValueRef undef_alloca
;
123 LLVMValueRef main_fn
;
124 LLVMTypeRef return_type
;
126 /* Parameter indices for LLVMGetParam. */
127 int param_rw_buffers
;
128 int param_const_and_shader_buffers
;
129 int param_samplers_and_images
;
130 int param_bindless_samplers_and_images
;
131 /* Common inputs for merged shaders. */
132 int param_merged_wave_info
;
133 int param_merged_scratch_offset
;
135 int param_vertex_buffers
;
136 int param_rel_auto_id
;
137 int param_vs_prim_id
;
138 int param_vertex_index0
;
139 /* VS states and layout of LS outputs / TCS inputs at the end
140 * [0] = clamp vertex color
142 * [8:20] = stride between patches in DW = num_inputs * num_vertices * 4
143 * max = 32*32*4 + 32*4
144 * [24:31] = stride between vertices in DW = num_inputs * 4
147 int param_vs_state_bits
;
148 int param_vs_blit_inputs
;
150 int param_streamout_config
;
151 int param_streamout_write_index
;
152 int param_streamout_offset
[4];
155 /* Layout of TCS outputs in the offchip buffer
157 * [0:5] = the number of patches per threadgroup, max = NUM_PATCHES (40)
159 * [6:11] = the number of output vertices per patch, max = 32
161 * [12:31] = the offset of per patch attributes in the buffer in bytes.
162 * max = NUM_PATCHES*32*32*16
164 int param_tcs_offchip_layout
;
167 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
168 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
169 * [16:31] = TCS output patch0 offset for per-patch / 16
170 * max = (NUM_PATCHES + 1) * 32*32
172 int param_tcs_out_lds_offsets
;
173 /* Layout of TCS outputs / TES inputs:
174 * [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
175 * max = 32*32*4 + 32*4
176 * [13:18] = gl_PatchVerticesIn, max = 32
177 * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
179 int param_tcs_out_lds_layout
;
180 int param_tcs_offchip_offset
;
181 int param_tcs_factor_offset
;
184 int param_tes_offchip_addr
;
187 int param_tes_rel_patch_id
;
189 int param_es2gs_offset
;
192 * - bits 0..10: ordered_wave_id
193 * - bits 12..20: number of vertices in group
194 * - bits 22..30: number of primitives in group
196 LLVMValueRef gs_tg_info
;
198 int param_gs2vs_offset
;
199 int param_gs_wave_id
; /* GFX6 */
200 LLVMValueRef gs_vtx_offset
[6]; /* in dwords (GFX6) */
201 int param_gs_vtx01_offset
; /* in dwords (GFX9) */
202 int param_gs_vtx23_offset
; /* in dwords (GFX9) */
203 int param_gs_vtx45_offset
; /* in dwords (GFX9) */
205 int param_block_size
;
206 int param_cs_user_data
;
208 struct ac_llvm_compiler
*compiler
;
210 /* Preloaded descriptors. */
211 LLVMValueRef esgs_ring
;
212 LLVMValueRef gsvs_ring
[4];
213 LLVMValueRef tess_offchip_ring
;
215 LLVMValueRef invoc0_tess_factors
[6]; /* outer[4], inner[2] */
216 LLVMValueRef gs_next_vertex
[4];
217 LLVMValueRef gs_curprim_verts
[4];
218 LLVMValueRef gs_generated_prims
[4];
219 LLVMValueRef gs_ngg_emit
;
220 LLVMValueRef gs_ngg_scratch
;
221 LLVMValueRef postponed_kill
;
222 LLVMValueRef return_value
;
238 LLVMValueRef i1false
;
242 static inline struct si_shader_context
*
243 si_shader_context(struct lp_build_tgsi_context
*bld_base
)
245 return (struct si_shader_context
*)bld_base
;
248 static inline struct si_shader_context
*
249 si_shader_context_from_abi(struct ac_shader_abi
*abi
)
251 struct si_shader_context
*ctx
= NULL
;
252 return container_of(abi
, ctx
, abi
);
255 void si_init_function_info(struct si_function_info
*fninfo
);
256 unsigned add_arg_assign(struct si_function_info
*fninfo
,
257 enum si_arg_regfile regfile
, LLVMTypeRef type
,
258 LLVMValueRef
*assign
);
259 void si_create_function(struct si_shader_context
*ctx
,
261 LLVMTypeRef
*returns
, unsigned num_returns
,
262 struct si_function_info
*fninfo
,
263 unsigned max_workgroup_size
);
264 unsigned si_llvm_compile(LLVMModuleRef M
, struct si_shader_binary
*binary
,
265 struct ac_llvm_compiler
*compiler
,
266 struct pipe_debug_callback
*debug
,
267 bool less_optimized
, unsigned wave_size
);
269 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
270 enum tgsi_opcode_type type
);
272 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
273 enum tgsi_opcode_type type
, LLVMValueRef value
);
275 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
279 void si_llvm_context_init(struct si_shader_context
*ctx
,
280 struct si_screen
*sscreen
,
281 struct ac_llvm_compiler
*compiler
,
283 unsigned ballot_mask_bits
);
284 void si_llvm_context_set_ir(struct si_shader_context
*ctx
,
285 struct si_shader
*shader
);
287 void si_llvm_create_func(struct si_shader_context
*ctx
,
289 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
290 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
);
292 void si_llvm_dispose(struct si_shader_context
*ctx
);
294 void si_llvm_optimize_module(struct si_shader_context
*ctx
);
296 LLVMValueRef
si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
301 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
302 const struct tgsi_full_src_register
*reg
,
303 enum tgsi_opcode_type type
,
306 void si_llvm_emit_kill(struct ac_shader_abi
*abi
, LLVMValueRef visible
);
308 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
310 LLVMValueRef vertex_index
,
311 LLVMValueRef param_index
,
312 unsigned const_index
,
314 unsigned driver_location
,
316 unsigned num_components
,
321 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
322 unsigned input_index
,
323 unsigned vtx_offset_param
,
327 LLVMValueRef
si_nir_lookup_interp_param(struct ac_shader_abi
*abi
,
328 enum glsl_interp_mode interp
,
331 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
332 const struct tgsi_full_instruction
*inst
,
333 const struct tgsi_opcode_info
*info
,
335 LLVMValueRef dst
[4]);
337 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
338 const struct tgsi_ind_register
*ind
,
339 unsigned addr_mul
, int rel_index
);
340 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
341 const struct tgsi_ind_register
*ind
,
342 int rel_index
, unsigned num
);
343 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
);
345 void si_shader_context_init_alu(struct lp_build_tgsi_context
*bld_base
);
346 void si_shader_context_init_mem(struct si_shader_context
*ctx
);
348 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
349 LLVMValueRef list
, LLVMValueRef index
,
350 enum ac_descriptor_type type
);
351 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
352 LLVMValueRef list
, LLVMValueRef index
,
353 enum ac_descriptor_type desc_type
,
354 bool uses_store
, bool bindless
);
355 LLVMValueRef
si_nir_emit_fbfetch(struct ac_shader_abi
*abi
);
357 void si_load_system_value(struct si_shader_context
*ctx
,
359 const struct tgsi_full_declaration
*decl
);
360 void si_declare_compute_memory(struct si_shader_context
*ctx
);
361 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
362 const struct tgsi_full_declaration
*decl
);
364 LLVMValueRef
si_get_primitive_id(struct si_shader_context
*ctx
,
366 void si_llvm_export_vs(struct si_shader_context
*ctx
,
367 struct si_shader_output_values
*outputs
,
369 void si_emit_streamout_output(struct si_shader_context
*ctx
,
370 LLVMValueRef
const *so_buffers
,
371 LLVMValueRef
const *so_write_offsets
,
372 struct pipe_stream_output
*stream_out
,
373 struct si_shader_output_values
*shader_out
);
375 void si_llvm_load_input_vs(
376 struct si_shader_context
*ctx
,
377 unsigned input_index
,
378 LLVMValueRef out
[4]);
379 void si_llvm_load_input_fs(
380 struct si_shader_context
*ctx
,
381 unsigned input_index
,
382 LLVMValueRef out
[4]);
384 bool si_nir_build_llvm(struct si_shader_context
*ctx
, struct nir_shader
*nir
);
386 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
387 unsigned param
, unsigned rshift
,
390 void gfx10_emit_ngg_epilogue(struct ac_shader_abi
*abi
,
391 unsigned max_outputs
,
392 LLVMValueRef
*addrs
);
393 void gfx10_ngg_gs_emit_vertex(struct si_shader_context
*ctx
,
395 LLVMValueRef
*addrs
);
396 void gfx10_ngg_gs_emit_prologue(struct si_shader_context
*ctx
);
397 void gfx10_ngg_gs_emit_epilogue(struct si_shader_context
*ctx
);
398 void gfx10_ngg_calculate_subgroup_info(struct si_shader
*shader
);