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
;
39 struct ac_shader_binary
;
41 #define RADEON_LLVM_MAX_INPUT_SLOTS 32
42 #define RADEON_LLVM_MAX_INPUTS 32 * 4
43 #define RADEON_LLVM_MAX_OUTPUTS 32 * 4
45 #define RADEON_LLVM_MAX_SYSTEM_VALUES 11
46 #define RADEON_LLVM_MAX_ADDRS 16
54 * Used to collect types and other info about arguments of the LLVM function
55 * before the function is created.
57 struct si_function_info
{
58 LLVMTypeRef types
[100];
59 LLVMValueRef
*assign
[100];
60 unsigned num_sgpr_params
;
64 struct si_shader_context
{
65 struct lp_build_tgsi_context bld_base
;
66 struct gallivm_state gallivm
;
67 struct ac_llvm_context ac
;
68 struct si_shader
*shader
;
69 struct si_screen
*screen
;
71 unsigned type
; /* PIPE_SHADER_* specifies the type of shader. */
73 /* For clamping the non-constant index in resource indexing: */
74 unsigned num_const_buffers
;
75 unsigned num_shader_buffers
;
77 unsigned num_samplers
;
79 struct ac_shader_abi abi
;
81 /** This function is responsible for initilizing the inputs array and will be
82 * called once for each input declared in the TGSI shader.
84 void (*load_input
)(struct si_shader_context
*,
86 const struct tgsi_full_declaration
*decl
,
89 /** This array contains the input values for the shader. Typically these
90 * values will be in the form of a target intrinsic that will inform the
91 * backend how to load the actual inputs to the shader.
93 struct tgsi_full_declaration input_decls
[RADEON_LLVM_MAX_INPUT_SLOTS
];
94 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
];
95 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
][TGSI_NUM_CHANNELS
];
96 LLVMValueRef addrs
[RADEON_LLVM_MAX_ADDRS
][TGSI_NUM_CHANNELS
];
98 /** This pointer is used to contain the temporary values.
99 * The amount of temporary used in tgsi can't be bound to a max value and
100 * thus we must allocate this array at runtime.
103 unsigned temps_count
;
104 LLVMValueRef system_values
[RADEON_LLVM_MAX_SYSTEM_VALUES
];
109 struct lp_build_if_state merged_wrap_if_state
;
111 struct tgsi_array_info
*temp_arrays
;
112 LLVMValueRef
*temp_array_allocas
;
114 LLVMValueRef undef_alloca
;
116 LLVMValueRef main_fn
;
117 LLVMTypeRef return_type
;
119 /* Parameter indices for LLVMGetParam. */
120 int param_rw_buffers
;
121 int param_const_and_shader_buffers
;
122 int param_samplers_and_images
;
123 int param_bindless_samplers_and_images
;
124 /* Common inputs for merged shaders. */
125 int param_merged_wave_info
;
126 int param_merged_scratch_offset
;
128 int param_vertex_buffers
;
129 int param_rel_auto_id
;
130 int param_vs_prim_id
;
131 int param_vertex_index0
;
132 /* VS states and layout of LS outputs / TCS inputs at the end
133 * [0] = clamp vertex color
135 * [8:20] = stride between patches in DW = num_inputs * num_vertices * 4
136 * max = 32*32*4 + 32*4
137 * [24:31] = stride between vertices in DW = num_inputs * 4
140 int param_vs_state_bits
;
141 int param_vs_blit_inputs
;
143 int param_streamout_config
;
144 int param_streamout_write_index
;
145 int param_streamout_offset
[4];
148 /* Layout of TCS outputs in the offchip buffer
150 * [0:5] = the number of patches per threadgroup, max = NUM_PATCHES (40)
152 * [6:11] = the number of output vertices per patch, max = 32
154 * [12:31] = the offset of per patch attributes in the buffer in bytes.
155 * max = NUM_PATCHES*32*32*16
157 int param_tcs_offchip_layout
;
160 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
161 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
162 * [16:31] = TCS output patch0 offset for per-patch / 16
163 * max = (NUM_PATCHES + 1) * 32*32
165 int param_tcs_out_lds_offsets
;
166 /* Layout of TCS outputs / TES inputs:
167 * [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
168 * max = 32*32*4 + 32*4
169 * [13:18] = gl_PatchVerticesIn, max = 32
170 * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
172 int param_tcs_out_lds_layout
;
173 int param_tcs_offchip_offset
;
174 int param_tcs_factor_offset
;
177 int param_tes_offchip_addr
;
180 int param_tes_rel_patch_id
;
182 int param_es2gs_offset
;
184 int param_gs2vs_offset
;
185 int param_gs_wave_id
; /* GFX6 */
186 LLVMValueRef gs_vtx_offset
[6]; /* in dwords (GFX6) */
187 int param_gs_vtx01_offset
; /* in dwords (GFX9) */
188 int param_gs_vtx23_offset
; /* in dwords (GFX9) */
189 int param_gs_vtx45_offset
; /* in dwords (GFX9) */
191 int param_block_size
;
192 int param_cs_user_data
;
194 struct ac_llvm_compiler
*compiler
;
196 /* Preloaded descriptors. */
197 LLVMValueRef esgs_ring
;
198 LLVMValueRef gsvs_ring
[4];
199 LLVMValueRef tess_offchip_ring
;
201 LLVMValueRef invoc0_tess_factors
[6]; /* outer[4], inner[2] */
202 LLVMValueRef gs_next_vertex
[4];
203 LLVMValueRef postponed_kill
;
204 LLVMValueRef return_value
;
220 LLVMValueRef i1false
;
224 static inline struct si_shader_context
*
225 si_shader_context(struct lp_build_tgsi_context
*bld_base
)
227 return (struct si_shader_context
*)bld_base
;
230 static inline struct si_shader_context
*
231 si_shader_context_from_abi(struct ac_shader_abi
*abi
)
233 struct si_shader_context
*ctx
= NULL
;
234 return container_of(abi
, ctx
, abi
);
237 void si_init_function_info(struct si_function_info
*fninfo
);
238 unsigned add_arg_assign(struct si_function_info
*fninfo
,
239 enum si_arg_regfile regfile
, LLVMTypeRef type
,
240 LLVMValueRef
*assign
);
241 void si_create_function(struct si_shader_context
*ctx
,
243 LLVMTypeRef
*returns
, unsigned num_returns
,
244 struct si_function_info
*fninfo
,
245 unsigned max_workgroup_size
);
246 unsigned si_llvm_compile(LLVMModuleRef M
, struct ac_shader_binary
*binary
,
247 struct ac_llvm_compiler
*compiler
,
248 struct pipe_debug_callback
*debug
,
249 bool less_optimized
);
251 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
252 enum tgsi_opcode_type type
);
254 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
255 enum tgsi_opcode_type type
, LLVMValueRef value
);
257 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
261 void si_llvm_context_init(struct si_shader_context
*ctx
,
262 struct si_screen
*sscreen
,
263 struct ac_llvm_compiler
*compiler
);
264 void si_llvm_context_set_tgsi(struct si_shader_context
*ctx
,
265 struct si_shader
*shader
);
267 void si_llvm_create_func(struct si_shader_context
*ctx
,
269 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
270 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
);
272 void si_llvm_dispose(struct si_shader_context
*ctx
);
274 void si_llvm_optimize_module(struct si_shader_context
*ctx
);
276 LLVMValueRef
si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
281 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
282 const struct tgsi_full_src_register
*reg
,
283 enum tgsi_opcode_type type
,
286 void si_llvm_emit_kill(struct ac_shader_abi
*abi
, LLVMValueRef visible
);
288 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
290 LLVMValueRef vertex_index
,
291 LLVMValueRef param_index
,
292 unsigned const_index
,
294 unsigned driver_location
,
296 unsigned num_components
,
301 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
302 unsigned input_index
,
303 unsigned vtx_offset_param
,
307 LLVMValueRef
si_nir_lookup_interp_param(struct ac_shader_abi
*abi
,
308 enum glsl_interp_mode interp
,
311 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
312 const struct tgsi_full_instruction
*inst
,
313 const struct tgsi_opcode_info
*info
,
315 LLVMValueRef dst
[4]);
317 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
318 const struct tgsi_ind_register
*ind
,
319 unsigned addr_mul
, int rel_index
);
320 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
321 const struct tgsi_ind_register
*ind
,
322 int rel_index
, unsigned num
);
323 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
);
325 void si_shader_context_init_alu(struct lp_build_tgsi_context
*bld_base
);
326 void si_shader_context_init_mem(struct si_shader_context
*ctx
);
328 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
329 LLVMValueRef list
, LLVMValueRef index
,
330 enum ac_descriptor_type type
);
331 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
332 LLVMValueRef list
, LLVMValueRef index
,
333 enum ac_descriptor_type desc_type
, bool dcc_off
,
336 void si_load_system_value(struct si_shader_context
*ctx
,
338 const struct tgsi_full_declaration
*decl
);
339 void si_declare_compute_memory(struct si_shader_context
*ctx
);
340 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
341 const struct tgsi_full_declaration
*decl
);
343 void si_llvm_load_input_vs(
344 struct si_shader_context
*ctx
,
345 unsigned input_index
,
346 LLVMValueRef out
[4]);
347 void si_llvm_load_input_fs(
348 struct si_shader_context
*ctx
,
349 unsigned input_index
,
350 LLVMValueRef out
[4]);
352 bool si_nir_build_llvm(struct si_shader_context
*ctx
, struct nir_shader
*nir
);
354 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
355 unsigned param
, unsigned rshift
,