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"
34 #include "ac_llvm_util.h"
35 #include "ac_llvm_build.h"
37 #include <llvm-c/Core.h>
38 #include <llvm-c/TargetMachine.h>
40 struct pipe_debug_callback
;
41 struct ac_shader_binary
;
43 #define RADEON_LLVM_MAX_INPUT_SLOTS 32
44 #define RADEON_LLVM_MAX_INPUTS 32 * 4
45 #define RADEON_LLVM_MAX_OUTPUTS 32 * 4
47 #define RADEON_LLVM_MAX_SYSTEM_VALUES 11
48 #define RADEON_LLVM_MAX_ADDRS 16
50 struct si_shader_context
{
51 struct lp_build_tgsi_context bld_base
;
52 struct gallivm_state gallivm
;
53 struct ac_llvm_context ac
;
54 struct si_shader
*shader
;
55 struct si_screen
*screen
;
57 unsigned type
; /* PIPE_SHADER_* specifies the type of shader. */
59 /* For clamping the non-constant index in resource indexing: */
60 unsigned num_const_buffers
;
61 unsigned num_shader_buffers
;
63 unsigned num_samplers
;
65 /* Whether the prolog will be compiled separately. */
68 struct ac_shader_abi abi
;
70 /** This function is responsible for initilizing the inputs array and will be
71 * called once for each input declared in the TGSI shader.
73 void (*load_input
)(struct si_shader_context
*,
75 const struct tgsi_full_declaration
*decl
,
78 /** This array contains the input values for the shader. Typically these
79 * values will be in the form of a target intrinsic that will inform the
80 * backend how to load the actual inputs to the shader.
82 struct tgsi_full_declaration input_decls
[RADEON_LLVM_MAX_INPUT_SLOTS
];
83 LLVMValueRef inputs
[RADEON_LLVM_MAX_INPUTS
];
84 LLVMValueRef outputs
[RADEON_LLVM_MAX_OUTPUTS
][TGSI_NUM_CHANNELS
];
85 LLVMValueRef addrs
[RADEON_LLVM_MAX_ADDRS
][TGSI_NUM_CHANNELS
];
87 /** This pointer is used to contain the temporary values.
88 * The amount of temporary used in tgsi can't be bound to a max value and
89 * thus we must allocate this array at runtime.
93 LLVMValueRef system_values
[RADEON_LLVM_MAX_SYSTEM_VALUES
];
98 struct lp_build_if_state merged_wrap_if_state
;
100 struct tgsi_array_info
*temp_arrays
;
101 LLVMValueRef
*temp_array_allocas
;
103 LLVMValueRef undef_alloca
;
105 LLVMValueRef main_fn
;
106 LLVMTypeRef return_type
;
108 /* Parameter indices for LLVMGetParam. */
109 int param_rw_buffers
;
110 int param_const_and_shader_buffers
;
111 int param_samplers_and_images
;
112 int param_bindless_samplers_and_images
;
113 /* Common inputs for merged shaders. */
114 int param_merged_wave_info
;
115 int param_merged_scratch_offset
;
117 int param_vertex_buffers
;
118 int param_rel_auto_id
;
119 int param_vs_prim_id
;
120 int param_vertex_index0
;
121 /* VS states and layout of LS outputs / TCS inputs at the end
122 * [0] = clamp vertex color
124 * [8:20] = stride between patches in DW = num_inputs * num_vertices * 4
125 * max = 32*32*4 + 32*4
126 * [24:31] = stride between vertices in DW = num_inputs * 4
129 int param_vs_state_bits
;
130 int param_vs_blit_inputs
;
132 int param_streamout_config
;
133 int param_streamout_write_index
;
134 int param_streamout_offset
[4];
137 /* Layout of TCS outputs in the offchip buffer
139 * [0:5] = the number of patches per threadgroup, max = NUM_PATCHES (40)
141 * [6:11] = the number of output vertices per patch, max = 32
143 * [12:31] = the offset of per patch attributes in the buffer in bytes.
144 * max = NUM_PATCHES*32*32*16
146 int param_tcs_offchip_layout
;
149 /* Offsets where TCS outputs and TCS patch outputs live in LDS:
150 * [0:15] = TCS output patch0 offset / 16, max = NUM_PATCHES * 32 * 32
151 * [16:31] = TCS output patch0 offset for per-patch / 16
152 * max = (NUM_PATCHES + 1) * 32*32
154 int param_tcs_out_lds_offsets
;
155 /* Layout of TCS outputs / TES inputs:
156 * [0:12] = stride between output patches in DW, num_outputs * num_vertices * 4
157 * max = 32*32*4 + 32*4
158 * [13:18] = gl_PatchVerticesIn, max = 32
159 * [19:31] = high 13 bits of the 32-bit address of tessellation ring buffers
161 int param_tcs_out_lds_layout
;
162 int param_tcs_offchip_offset
;
163 int param_tcs_factor_offset
;
166 int param_tes_offchip_addr
;
169 int param_tes_rel_patch_id
;
171 int param_es2gs_offset
;
173 int param_gs2vs_offset
;
174 int param_gs_wave_id
; /* GFX6 */
175 LLVMValueRef gs_vtx_offset
[6]; /* in dwords (GFX6) */
176 int param_gs_vtx01_offset
; /* in dwords (GFX9) */
177 int param_gs_vtx23_offset
; /* in dwords (GFX9) */
178 int param_gs_vtx45_offset
; /* in dwords (GFX9) */
180 int param_block_size
;
182 struct si_compiler
*compiler
;
184 /* Preloaded descriptors. */
185 LLVMValueRef esgs_ring
;
186 LLVMValueRef gsvs_ring
[4];
187 LLVMValueRef tess_offchip_ring
;
189 LLVMValueRef invoc0_tess_factors
[6]; /* outer[4], inner[2] */
190 LLVMValueRef gs_next_vertex
[4];
191 LLVMValueRef postponed_kill
;
192 LLVMValueRef return_value
;
210 static inline struct si_shader_context
*
211 si_shader_context(struct lp_build_tgsi_context
*bld_base
)
213 return (struct si_shader_context
*)bld_base
;
216 static inline struct si_shader_context
*
217 si_shader_context_from_abi(struct ac_shader_abi
*abi
)
219 struct si_shader_context
*ctx
= NULL
;
220 return container_of(abi
, ctx
, abi
);
223 unsigned si_llvm_compile(LLVMModuleRef M
, struct ac_shader_binary
*binary
,
224 struct si_compiler
*compiler
,
225 struct pipe_debug_callback
*debug
);
227 LLVMTypeRef
tgsi2llvmtype(struct lp_build_tgsi_context
*bld_base
,
228 enum tgsi_opcode_type type
);
230 LLVMValueRef
bitcast(struct lp_build_tgsi_context
*bld_base
,
231 enum tgsi_opcode_type type
, LLVMValueRef value
);
233 LLVMValueRef
si_llvm_bound_index(struct si_shader_context
*ctx
,
237 void si_llvm_context_init(struct si_shader_context
*ctx
,
238 struct si_screen
*sscreen
,
239 struct si_compiler
*compiler
);
240 void si_llvm_context_set_tgsi(struct si_shader_context
*ctx
,
241 struct si_shader
*shader
);
243 void si_llvm_create_func(struct si_shader_context
*ctx
,
245 LLVMTypeRef
*return_types
, unsigned num_return_elems
,
246 LLVMTypeRef
*ParamTypes
, unsigned ParamCount
);
248 void si_llvm_dispose(struct si_shader_context
*ctx
);
250 void si_llvm_optimize_module(struct si_shader_context
*ctx
);
252 LLVMValueRef
si_llvm_emit_fetch_64bit(struct lp_build_tgsi_context
*bld_base
,
257 LLVMValueRef
si_llvm_emit_fetch(struct lp_build_tgsi_context
*bld_base
,
258 const struct tgsi_full_src_register
*reg
,
259 enum tgsi_opcode_type type
,
262 void si_llvm_emit_kill(struct ac_shader_abi
*abi
, LLVMValueRef visible
);
264 LLVMValueRef
si_nir_load_input_tes(struct ac_shader_abi
*abi
,
266 LLVMValueRef vertex_index
,
267 LLVMValueRef param_index
,
268 unsigned const_index
,
270 unsigned driver_location
,
272 unsigned num_components
,
277 LLVMValueRef
si_llvm_load_input_gs(struct ac_shader_abi
*abi
,
278 unsigned input_index
,
279 unsigned vtx_offset_param
,
283 LLVMValueRef
si_nir_lookup_interp_param(struct ac_shader_abi
*abi
,
284 enum glsl_interp_mode interp
,
287 void si_llvm_emit_store(struct lp_build_tgsi_context
*bld_base
,
288 const struct tgsi_full_instruction
*inst
,
289 const struct tgsi_opcode_info
*info
,
291 LLVMValueRef dst
[4]);
293 /* Combine these with & instead of |. */
294 #define NOOP_WAITCNT 0xf7f
295 #define LGKM_CNT 0x07f
298 LLVMValueRef
si_get_indirect_index(struct si_shader_context
*ctx
,
299 const struct tgsi_ind_register
*ind
,
300 unsigned addr_mul
, int rel_index
);
301 LLVMValueRef
si_get_bounded_indirect_index(struct si_shader_context
*ctx
,
302 const struct tgsi_ind_register
*ind
,
303 int rel_index
, unsigned num
);
304 LLVMValueRef
si_get_sample_id(struct si_shader_context
*ctx
);
306 void si_shader_context_init_alu(struct lp_build_tgsi_context
*bld_base
);
307 void si_shader_context_init_mem(struct si_shader_context
*ctx
);
309 LLVMValueRef
si_load_sampler_desc(struct si_shader_context
*ctx
,
310 LLVMValueRef list
, LLVMValueRef index
,
311 enum ac_descriptor_type type
);
312 LLVMValueRef
si_load_image_desc(struct si_shader_context
*ctx
,
313 LLVMValueRef list
, LLVMValueRef index
,
314 enum ac_descriptor_type desc_type
, bool dcc_off
);
316 void si_load_system_value(struct si_shader_context
*ctx
,
318 const struct tgsi_full_declaration
*decl
);
319 void si_declare_compute_memory(struct si_shader_context
*ctx
);
320 void si_tgsi_declare_compute_memory(struct si_shader_context
*ctx
,
321 const struct tgsi_full_declaration
*decl
);
323 void si_llvm_load_input_vs(
324 struct si_shader_context
*ctx
,
325 unsigned input_index
,
326 LLVMValueRef out
[4]);
327 void si_llvm_load_input_fs(
328 struct si_shader_context
*ctx
,
329 unsigned input_index
,
330 LLVMValueRef out
[4]);
332 bool si_nir_build_llvm(struct si_shader_context
*ctx
, struct nir_shader
*nir
);
334 LLVMValueRef
si_unpack_param(struct si_shader_context
*ctx
,
335 unsigned param
, unsigned rshift
,