8c8f90b80e23de98a0526bc4d8c76fe2c93baeb9
[mesa.git] / src / gallium / drivers / radeonsi / si_shader.c
1 /*
2 * Copyright 2012 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 #include "util/u_memory.h"
26 #include "tgsi/tgsi_strings.h"
27 #include "tgsi/tgsi_from_mesa.h"
28
29 #include "ac_exp_param.h"
30 #include "ac_rtld.h"
31 #include "si_shader_internal.h"
32 #include "si_pipe.h"
33 #include "sid.h"
34
35 #include "compiler/nir/nir.h"
36 #include "compiler/nir/nir_serialize.h"
37
38 static const char scratch_rsrc_dword0_symbol[] =
39 "SCRATCH_RSRC_DWORD0";
40
41 static const char scratch_rsrc_dword1_symbol[] =
42 "SCRATCH_RSRC_DWORD1";
43
44 static void si_dump_shader_key(const struct si_shader *shader, FILE *f);
45
46 /** Whether the shader runs as a combination of multiple API shaders */
47 bool si_is_multi_part_shader(struct si_shader *shader)
48 {
49 if (shader->selector->screen->info.chip_class <= GFX8)
50 return false;
51
52 return shader->key.as_ls ||
53 shader->key.as_es ||
54 shader->selector->type == PIPE_SHADER_TESS_CTRL ||
55 shader->selector->type == PIPE_SHADER_GEOMETRY;
56 }
57
58 /** Whether the shader runs on a merged HW stage (LSHS or ESGS) */
59 bool si_is_merged_shader(struct si_shader *shader)
60 {
61 return shader->key.as_ngg || si_is_multi_part_shader(shader);
62 }
63
64 /**
65 * Returns a unique index for a per-patch semantic name and index. The index
66 * must be less than 32, so that a 32-bit bitmask of used inputs or outputs
67 * can be calculated.
68 */
69 unsigned si_shader_io_get_unique_index_patch(unsigned semantic_name, unsigned index)
70 {
71 switch (semantic_name) {
72 case TGSI_SEMANTIC_TESSOUTER:
73 return 0;
74 case TGSI_SEMANTIC_TESSINNER:
75 return 1;
76 case TGSI_SEMANTIC_PATCH:
77 assert(index < 30);
78 return 2 + index;
79
80 default:
81 assert(!"invalid semantic name");
82 return 0;
83 }
84 }
85
86 /**
87 * Returns a unique index for a semantic name and index. The index must be
88 * less than 64, so that a 64-bit bitmask of used inputs or outputs can be
89 * calculated.
90 */
91 unsigned si_shader_io_get_unique_index(unsigned semantic_name, unsigned index,
92 unsigned is_varying)
93 {
94 switch (semantic_name) {
95 case TGSI_SEMANTIC_POSITION:
96 return 0;
97 case TGSI_SEMANTIC_GENERIC:
98 /* Since some shader stages use the the highest used IO index
99 * to determine the size to allocate for inputs/outputs
100 * (in LDS, tess and GS rings). GENERIC should be placed right
101 * after POSITION to make that size as small as possible.
102 */
103 if (index < SI_MAX_IO_GENERIC)
104 return 1 + index;
105
106 assert(!"invalid generic index");
107 return 0;
108 case TGSI_SEMANTIC_FOG:
109 return SI_MAX_IO_GENERIC + 1;
110 case TGSI_SEMANTIC_COLOR:
111 assert(index < 2);
112 return SI_MAX_IO_GENERIC + 2 + index;
113 case TGSI_SEMANTIC_BCOLOR:
114 assert(index < 2);
115 /* If it's a varying, COLOR and BCOLOR alias. */
116 if (is_varying)
117 return SI_MAX_IO_GENERIC + 2 + index;
118 else
119 return SI_MAX_IO_GENERIC + 4 + index;
120 case TGSI_SEMANTIC_TEXCOORD:
121 assert(index < 8);
122 return SI_MAX_IO_GENERIC + 6 + index;
123
124 /* These are rarely used between LS and HS or ES and GS. */
125 case TGSI_SEMANTIC_CLIPDIST:
126 assert(index < 2);
127 return SI_MAX_IO_GENERIC + 6 + 8 + index;
128 case TGSI_SEMANTIC_CLIPVERTEX:
129 return SI_MAX_IO_GENERIC + 6 + 8 + 2;
130 case TGSI_SEMANTIC_PSIZE:
131 return SI_MAX_IO_GENERIC + 6 + 8 + 3;
132
133 /* These can't be written by LS, HS, and ES. */
134 case TGSI_SEMANTIC_LAYER:
135 return SI_MAX_IO_GENERIC + 6 + 8 + 4;
136 case TGSI_SEMANTIC_VIEWPORT_INDEX:
137 return SI_MAX_IO_GENERIC + 6 + 8 + 5;
138 case TGSI_SEMANTIC_PRIMID:
139 STATIC_ASSERT(SI_MAX_IO_GENERIC + 6 + 8 + 6 <= 63);
140 return SI_MAX_IO_GENERIC + 6 + 8 + 6;
141 default:
142 fprintf(stderr, "invalid semantic name = %u\n", semantic_name);
143 assert(!"invalid semantic name");
144 return 0;
145 }
146 }
147
148 static void si_dump_streamout(struct pipe_stream_output_info *so)
149 {
150 unsigned i;
151
152 if (so->num_outputs)
153 fprintf(stderr, "STREAMOUT\n");
154
155 for (i = 0; i < so->num_outputs; i++) {
156 unsigned mask = ((1 << so->output[i].num_components) - 1) <<
157 so->output[i].start_component;
158 fprintf(stderr, " %i: BUF%i[%i..%i] <- OUT[%i].%s%s%s%s\n",
159 i, so->output[i].output_buffer,
160 so->output[i].dst_offset, so->output[i].dst_offset + so->output[i].num_components - 1,
161 so->output[i].register_index,
162 mask & 1 ? "x" : "",
163 mask & 2 ? "y" : "",
164 mask & 4 ? "z" : "",
165 mask & 8 ? "w" : "");
166 }
167 }
168
169 static void declare_streamout_params(struct si_shader_context *ctx,
170 struct pipe_stream_output_info *so)
171 {
172 if (ctx->screen->use_ngg_streamout) {
173 if (ctx->type == PIPE_SHADER_TESS_EVAL)
174 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
175 return;
176 }
177
178 /* Streamout SGPRs. */
179 if (so->num_outputs) {
180 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->streamout_config);
181 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->streamout_write_index);
182 } else if (ctx->type == PIPE_SHADER_TESS_EVAL) {
183 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
184 }
185
186 /* A streamout buffer offset is loaded if the stride is non-zero. */
187 for (int i = 0; i < 4; i++) {
188 if (!so->stride[i])
189 continue;
190
191 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->streamout_offset[i]);
192 }
193 }
194
195 unsigned si_get_max_workgroup_size(const struct si_shader *shader)
196 {
197 switch (shader->selector->type) {
198 case PIPE_SHADER_VERTEX:
199 case PIPE_SHADER_TESS_EVAL:
200 return shader->key.as_ngg ? 128 : 0;
201
202 case PIPE_SHADER_TESS_CTRL:
203 /* Return this so that LLVM doesn't remove s_barrier
204 * instructions on chips where we use s_barrier. */
205 return shader->selector->screen->info.chip_class >= GFX7 ? 128 : 0;
206
207 case PIPE_SHADER_GEOMETRY:
208 return shader->selector->screen->info.chip_class >= GFX9 ? 128 : 0;
209
210 case PIPE_SHADER_COMPUTE:
211 break; /* see below */
212
213 default:
214 return 0;
215 }
216
217 const unsigned *properties = shader->selector->info.properties;
218 unsigned max_work_group_size =
219 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] *
220 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_HEIGHT] *
221 properties[TGSI_PROPERTY_CS_FIXED_BLOCK_DEPTH];
222
223 if (!max_work_group_size) {
224 /* This is a variable group size compute shader,
225 * compile it for the maximum possible group size.
226 */
227 max_work_group_size = SI_MAX_VARIABLE_THREADS_PER_BLOCK;
228 }
229 return max_work_group_size;
230 }
231
232 static void declare_const_and_shader_buffers(struct si_shader_context *ctx,
233 bool assign_params)
234 {
235 enum ac_arg_type const_shader_buf_type;
236
237 if (ctx->shader->selector->info.const_buffers_declared == 1 &&
238 ctx->shader->selector->info.shader_buffers_declared == 0)
239 const_shader_buf_type = AC_ARG_CONST_FLOAT_PTR;
240 else
241 const_shader_buf_type = AC_ARG_CONST_DESC_PTR;
242
243 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, const_shader_buf_type,
244 assign_params ? &ctx->const_and_shader_buffers :
245 &ctx->other_const_and_shader_buffers);
246 }
247
248 static void declare_samplers_and_images(struct si_shader_context *ctx,
249 bool assign_params)
250 {
251 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_CONST_IMAGE_PTR,
252 assign_params ? &ctx->samplers_and_images :
253 &ctx->other_samplers_and_images);
254 }
255
256 static void declare_per_stage_desc_pointers(struct si_shader_context *ctx,
257 bool assign_params)
258 {
259 declare_const_and_shader_buffers(ctx, assign_params);
260 declare_samplers_and_images(ctx, assign_params);
261 }
262
263 static void declare_global_desc_pointers(struct si_shader_context *ctx)
264 {
265 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_CONST_DESC_PTR,
266 &ctx->rw_buffers);
267 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_CONST_IMAGE_PTR,
268 &ctx->bindless_samplers_and_images);
269 }
270
271 static void declare_vs_specific_input_sgprs(struct si_shader_context *ctx)
272 {
273 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->vs_state_bits);
274 if (!ctx->shader->is_gs_copy_shader) {
275 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->args.base_vertex);
276 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->args.start_instance);
277 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->args.draw_id);
278 }
279 }
280
281 static void declare_vb_descriptor_input_sgprs(struct si_shader_context *ctx)
282 {
283 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_CONST_DESC_PTR, &ctx->vertex_buffers);
284
285 unsigned num_vbos_in_user_sgprs = ctx->shader->selector->num_vbos_in_user_sgprs;
286 if (num_vbos_in_user_sgprs) {
287 unsigned user_sgprs = ctx->args.num_sgprs_used;
288
289 if (si_is_merged_shader(ctx->shader))
290 user_sgprs -= 8;
291 assert(user_sgprs <= SI_SGPR_VS_VB_DESCRIPTOR_FIRST);
292
293 /* Declare unused SGPRs to align VB descriptors to 4 SGPRs (hw requirement). */
294 for (unsigned i = user_sgprs; i < SI_SGPR_VS_VB_DESCRIPTOR_FIRST; i++)
295 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* unused */
296
297 assert(num_vbos_in_user_sgprs <= ARRAY_SIZE(ctx->vb_descriptors));
298 for (unsigned i = 0; i < num_vbos_in_user_sgprs; i++)
299 ac_add_arg(&ctx->args, AC_ARG_SGPR, 4, AC_ARG_INT, &ctx->vb_descriptors[i]);
300 }
301 }
302
303 static void declare_vs_input_vgprs(struct si_shader_context *ctx,
304 unsigned *num_prolog_vgprs,
305 bool ngg_cull_shader)
306 {
307 struct si_shader *shader = ctx->shader;
308
309 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.vertex_id);
310 if (shader->key.as_ls) {
311 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->rel_auto_id);
312 if (ctx->screen->info.chip_class >= GFX10) {
313 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* user VGPR */
314 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.instance_id);
315 } else {
316 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.instance_id);
317 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* unused */
318 }
319 } else if (ctx->screen->info.chip_class >= GFX10) {
320 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* user VGPR */
321 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
322 &ctx->vs_prim_id); /* user vgpr or PrimID (legacy) */
323 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.instance_id);
324 } else {
325 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.instance_id);
326 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->vs_prim_id);
327 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL); /* unused */
328 }
329
330 if (!shader->is_gs_copy_shader) {
331 if (shader->key.opt.ngg_culling && !ngg_cull_shader) {
332 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
333 &ctx->ngg_old_thread_id);
334 }
335
336 /* Vertex load indices. */
337 if (shader->selector->info.num_inputs) {
338 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
339 &ctx->vertex_index0);
340 for (unsigned i = 1; i < shader->selector->info.num_inputs; i++)
341 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, NULL);
342 }
343 *num_prolog_vgprs += shader->selector->info.num_inputs;
344 }
345 }
346
347 static void declare_vs_blit_inputs(struct si_shader_context *ctx,
348 unsigned vs_blit_property)
349 {
350 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
351 &ctx->vs_blit_inputs); /* i16 x1, y1 */
352 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* i16 x1, y1 */
353 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* depth */
354
355 if (vs_blit_property == SI_VS_BLIT_SGPRS_POS_COLOR) {
356 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* color0 */
357 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* color1 */
358 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* color2 */
359 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* color3 */
360 } else if (vs_blit_property == SI_VS_BLIT_SGPRS_POS_TEXCOORD) {
361 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.x1 */
362 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.y1 */
363 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.x2 */
364 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.y2 */
365 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.z */
366 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_FLOAT, NULL); /* texcoord.w */
367 }
368 }
369
370 static void declare_tes_input_vgprs(struct si_shader_context *ctx, bool ngg_cull_shader)
371 {
372 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, &ctx->tes_u);
373 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, &ctx->tes_v);
374 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->tes_rel_patch_id);
375 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.tes_patch_id);
376
377 if (ctx->shader->key.opt.ngg_culling && !ngg_cull_shader) {
378 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
379 &ctx->ngg_old_thread_id);
380 }
381 }
382
383 enum {
384 /* Convenient merged shader definitions. */
385 SI_SHADER_MERGED_VERTEX_TESSCTRL = PIPE_SHADER_TYPES,
386 SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY,
387 };
388
389 void si_add_arg_checked(struct ac_shader_args *args,
390 enum ac_arg_regfile file,
391 unsigned registers, enum ac_arg_type type,
392 struct ac_arg *arg,
393 unsigned idx)
394 {
395 assert(args->arg_count == idx);
396 ac_add_arg(args, file, registers, type, arg);
397 }
398
399 void si_create_function(struct si_shader_context *ctx, bool ngg_cull_shader)
400 {
401 struct si_shader *shader = ctx->shader;
402 LLVMTypeRef returns[AC_MAX_ARGS];
403 unsigned i, num_return_sgprs;
404 unsigned num_returns = 0;
405 unsigned num_prolog_vgprs = 0;
406 unsigned type = ctx->type;
407 unsigned vs_blit_property =
408 shader->selector->info.properties[TGSI_PROPERTY_VS_BLIT_SGPRS_AMD];
409
410 memset(&ctx->args, 0, sizeof(ctx->args));
411
412 /* Set MERGED shaders. */
413 if (ctx->screen->info.chip_class >= GFX9) {
414 if (shader->key.as_ls || type == PIPE_SHADER_TESS_CTRL)
415 type = SI_SHADER_MERGED_VERTEX_TESSCTRL; /* LS or HS */
416 else if (shader->key.as_es || shader->key.as_ngg || type == PIPE_SHADER_GEOMETRY)
417 type = SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY;
418 }
419
420 switch (type) {
421 case PIPE_SHADER_VERTEX:
422 declare_global_desc_pointers(ctx);
423
424 if (vs_blit_property) {
425 declare_vs_blit_inputs(ctx, vs_blit_property);
426
427 /* VGPRs */
428 declare_vs_input_vgprs(ctx, &num_prolog_vgprs, ngg_cull_shader);
429 break;
430 }
431
432 declare_per_stage_desc_pointers(ctx, true);
433 declare_vs_specific_input_sgprs(ctx);
434 if (!shader->is_gs_copy_shader)
435 declare_vb_descriptor_input_sgprs(ctx);
436
437 if (shader->key.as_es) {
438 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
439 &ctx->es2gs_offset);
440 } else if (shader->key.as_ls) {
441 /* no extra parameters */
442 } else {
443 /* The locations of the other parameters are assigned dynamically. */
444 declare_streamout_params(ctx, &shader->selector->so);
445 }
446
447 /* VGPRs */
448 declare_vs_input_vgprs(ctx, &num_prolog_vgprs, ngg_cull_shader);
449
450 /* Return values */
451 if (shader->key.opt.vs_as_prim_discard_cs) {
452 for (i = 0; i < 4; i++)
453 returns[num_returns++] = ctx->ac.f32; /* VGPRs */
454 }
455 break;
456
457 case PIPE_SHADER_TESS_CTRL: /* GFX6-GFX8 */
458 declare_global_desc_pointers(ctx);
459 declare_per_stage_desc_pointers(ctx, true);
460 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_layout);
461 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_out_lds_offsets);
462 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_out_lds_layout);
463 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->vs_state_bits);
464 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_offset);
465 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_factor_offset);
466
467 /* VGPRs */
468 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.tcs_patch_id);
469 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.tcs_rel_ids);
470
471 /* param_tcs_offchip_offset and param_tcs_factor_offset are
472 * placed after the user SGPRs.
473 */
474 for (i = 0; i < GFX6_TCS_NUM_USER_SGPR + 2; i++)
475 returns[num_returns++] = ctx->ac.i32; /* SGPRs */
476 for (i = 0; i < 11; i++)
477 returns[num_returns++] = ctx->ac.f32; /* VGPRs */
478 break;
479
480 case SI_SHADER_MERGED_VERTEX_TESSCTRL:
481 /* Merged stages have 8 system SGPRs at the beginning. */
482 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_HS */
483 declare_per_stage_desc_pointers(ctx,
484 ctx->type == PIPE_SHADER_TESS_CTRL);
485 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_offset);
486 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->merged_wave_info);
487 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_factor_offset);
488 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->merged_scratch_offset);
489 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* unused */
490 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* unused */
491
492 declare_global_desc_pointers(ctx);
493 declare_per_stage_desc_pointers(ctx,
494 ctx->type == PIPE_SHADER_VERTEX);
495 declare_vs_specific_input_sgprs(ctx);
496
497 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_layout);
498 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_out_lds_offsets);
499 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_out_lds_layout);
500 declare_vb_descriptor_input_sgprs(ctx);
501
502 /* VGPRs (first TCS, then VS) */
503 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.tcs_patch_id);
504 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.tcs_rel_ids);
505
506 if (ctx->type == PIPE_SHADER_VERTEX) {
507 declare_vs_input_vgprs(ctx, &num_prolog_vgprs, ngg_cull_shader);
508
509 /* LS return values are inputs to the TCS main shader part. */
510 for (i = 0; i < 8 + GFX9_TCS_NUM_USER_SGPR; i++)
511 returns[num_returns++] = ctx->ac.i32; /* SGPRs */
512 for (i = 0; i < 2; i++)
513 returns[num_returns++] = ctx->ac.f32; /* VGPRs */
514 } else {
515 /* TCS return values are inputs to the TCS epilog.
516 *
517 * param_tcs_offchip_offset, param_tcs_factor_offset,
518 * param_tcs_offchip_layout, and param_rw_buffers
519 * should be passed to the epilog.
520 */
521 for (i = 0; i <= 8 + GFX9_SGPR_TCS_OUT_LAYOUT; i++)
522 returns[num_returns++] = ctx->ac.i32; /* SGPRs */
523 for (i = 0; i < 11; i++)
524 returns[num_returns++] = ctx->ac.f32; /* VGPRs */
525 }
526 break;
527
528 case SI_SHADER_MERGED_VERTEX_OR_TESSEVAL_GEOMETRY:
529 /* Merged stages have 8 system SGPRs at the beginning. */
530 /* SPI_SHADER_USER_DATA_ADDR_LO/HI_GS */
531 declare_per_stage_desc_pointers(ctx,
532 ctx->type == PIPE_SHADER_GEOMETRY);
533
534 if (ctx->shader->key.as_ngg)
535 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->gs_tg_info);
536 else
537 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->gs2vs_offset);
538
539 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->merged_wave_info);
540 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_offset);
541 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->merged_scratch_offset);
542 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_CONST_DESC_PTR,
543 &ctx->small_prim_cull_info); /* SPI_SHADER_PGM_LO_GS << 8 */
544 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL); /* unused (SPI_SHADER_PGM_LO/HI_GS >> 24) */
545
546 declare_global_desc_pointers(ctx);
547 if (ctx->type != PIPE_SHADER_VERTEX || !vs_blit_property) {
548 declare_per_stage_desc_pointers(ctx,
549 (ctx->type == PIPE_SHADER_VERTEX ||
550 ctx->type == PIPE_SHADER_TESS_EVAL));
551 }
552
553 if (ctx->type == PIPE_SHADER_VERTEX) {
554 if (vs_blit_property)
555 declare_vs_blit_inputs(ctx, vs_blit_property);
556 else
557 declare_vs_specific_input_sgprs(ctx);
558 } else {
559 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->vs_state_bits);
560 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_layout);
561 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tes_offchip_addr);
562 /* Declare as many input SGPRs as the VS has. */
563 }
564
565 if (ctx->type == PIPE_SHADER_VERTEX)
566 declare_vb_descriptor_input_sgprs(ctx);
567
568 /* VGPRs (first GS, then VS/TES) */
569 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx01_offset);
570 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx23_offset);
571 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.gs_prim_id);
572 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.gs_invocation_id);
573 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx45_offset);
574
575 if (ctx->type == PIPE_SHADER_VERTEX) {
576 declare_vs_input_vgprs(ctx, &num_prolog_vgprs, ngg_cull_shader);
577 } else if (ctx->type == PIPE_SHADER_TESS_EVAL) {
578 declare_tes_input_vgprs(ctx, ngg_cull_shader);
579 }
580
581 if ((ctx->shader->key.as_es || ngg_cull_shader) &&
582 (ctx->type == PIPE_SHADER_VERTEX ||
583 ctx->type == PIPE_SHADER_TESS_EVAL)) {
584 unsigned num_user_sgprs, num_vgprs;
585
586 if (ctx->type == PIPE_SHADER_VERTEX) {
587 /* For the NGG cull shader, add 1 SGPR to hold
588 * the vertex buffer pointer.
589 */
590 num_user_sgprs = GFX9_VSGS_NUM_USER_SGPR + ngg_cull_shader;
591
592 if (ngg_cull_shader && shader->selector->num_vbos_in_user_sgprs) {
593 assert(num_user_sgprs <= 8 + SI_SGPR_VS_VB_DESCRIPTOR_FIRST);
594 num_user_sgprs = SI_SGPR_VS_VB_DESCRIPTOR_FIRST +
595 shader->selector->num_vbos_in_user_sgprs * 4;
596 }
597 } else {
598 num_user_sgprs = GFX9_TESGS_NUM_USER_SGPR;
599 }
600
601 /* The NGG cull shader has to return all 9 VGPRs + the old thread ID.
602 *
603 * The normal merged ESGS shader only has to return the 5 VGPRs
604 * for the GS stage.
605 */
606 num_vgprs = ngg_cull_shader ? 10 : 5;
607
608 /* ES return values are inputs to GS. */
609 for (i = 0; i < 8 + num_user_sgprs; i++)
610 returns[num_returns++] = ctx->ac.i32; /* SGPRs */
611 for (i = 0; i < num_vgprs; i++)
612 returns[num_returns++] = ctx->ac.f32; /* VGPRs */
613 }
614 break;
615
616 case PIPE_SHADER_TESS_EVAL:
617 declare_global_desc_pointers(ctx);
618 declare_per_stage_desc_pointers(ctx, true);
619 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->vs_state_bits);
620 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_layout);
621 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tes_offchip_addr);
622
623 if (shader->key.as_es) {
624 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_offset);
625 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL);
626 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->es2gs_offset);
627 } else {
628 declare_streamout_params(ctx, &shader->selector->so);
629 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->tcs_offchip_offset);
630 }
631
632 /* VGPRs */
633 declare_tes_input_vgprs(ctx, ngg_cull_shader);
634 break;
635
636 case PIPE_SHADER_GEOMETRY:
637 declare_global_desc_pointers(ctx);
638 declare_per_stage_desc_pointers(ctx, true);
639 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->gs2vs_offset);
640 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->gs_wave_id);
641
642 /* VGPRs */
643 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[0]);
644 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[1]);
645 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.gs_prim_id);
646 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[2]);
647 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[3]);
648 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[4]);
649 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->gs_vtx_offset[5]);
650 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT, &ctx->args.gs_invocation_id);
651 break;
652
653 case PIPE_SHADER_FRAGMENT:
654 declare_global_desc_pointers(ctx);
655 declare_per_stage_desc_pointers(ctx, true);
656 si_add_arg_checked(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, NULL,
657 SI_PARAM_ALPHA_REF);
658 si_add_arg_checked(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
659 &ctx->args.prim_mask, SI_PARAM_PRIM_MASK);
660
661 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT, &ctx->args.persp_sample,
662 SI_PARAM_PERSP_SAMPLE);
663 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT,
664 &ctx->args.persp_center, SI_PARAM_PERSP_CENTER);
665 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT,
666 &ctx->args.persp_centroid, SI_PARAM_PERSP_CENTROID);
667 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 3, AC_ARG_INT,
668 NULL, SI_PARAM_PERSP_PULL_MODEL);
669 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT,
670 &ctx->args.linear_sample, SI_PARAM_LINEAR_SAMPLE);
671 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT,
672 &ctx->args.linear_center, SI_PARAM_LINEAR_CENTER);
673 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 2, AC_ARG_INT,
674 &ctx->args.linear_centroid, SI_PARAM_LINEAR_CENTROID);
675 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 3, AC_ARG_FLOAT,
676 NULL, SI_PARAM_LINE_STIPPLE_TEX);
677 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT,
678 &ctx->args.frag_pos[0], SI_PARAM_POS_X_FLOAT);
679 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT,
680 &ctx->args.frag_pos[1], SI_PARAM_POS_Y_FLOAT);
681 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT,
682 &ctx->args.frag_pos[2], SI_PARAM_POS_Z_FLOAT);
683 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT,
684 &ctx->args.frag_pos[3], SI_PARAM_POS_W_FLOAT);
685 shader->info.face_vgpr_index = ctx->args.num_vgprs_used;
686 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
687 &ctx->args.front_face, SI_PARAM_FRONT_FACE);
688 shader->info.ancillary_vgpr_index = ctx->args.num_vgprs_used;
689 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
690 &ctx->args.ancillary, SI_PARAM_ANCILLARY);
691 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT,
692 &ctx->args.sample_coverage, SI_PARAM_SAMPLE_COVERAGE);
693 si_add_arg_checked(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_INT,
694 &ctx->pos_fixed_pt, SI_PARAM_POS_FIXED_PT);
695
696 /* Color inputs from the prolog. */
697 if (shader->selector->info.colors_read) {
698 unsigned num_color_elements =
699 util_bitcount(shader->selector->info.colors_read);
700
701 for (i = 0; i < num_color_elements; i++)
702 ac_add_arg(&ctx->args, AC_ARG_VGPR, 1, AC_ARG_FLOAT, NULL);
703
704 num_prolog_vgprs += num_color_elements;
705 }
706
707 /* Outputs for the epilog. */
708 num_return_sgprs = SI_SGPR_ALPHA_REF + 1;
709 num_returns =
710 num_return_sgprs +
711 util_bitcount(shader->selector->info.colors_written) * 4 +
712 shader->selector->info.writes_z +
713 shader->selector->info.writes_stencil +
714 shader->selector->info.writes_samplemask +
715 1 /* SampleMaskIn */;
716
717 num_returns = MAX2(num_returns,
718 num_return_sgprs +
719 PS_EPILOG_SAMPLEMASK_MIN_LOC + 1);
720
721 for (i = 0; i < num_return_sgprs; i++)
722 returns[i] = ctx->ac.i32;
723 for (; i < num_returns; i++)
724 returns[i] = ctx->ac.f32;
725 break;
726
727 case PIPE_SHADER_COMPUTE:
728 declare_global_desc_pointers(ctx);
729 declare_per_stage_desc_pointers(ctx, true);
730 if (shader->selector->info.uses_grid_size)
731 ac_add_arg(&ctx->args, AC_ARG_SGPR, 3, AC_ARG_INT,
732 &ctx->args.num_work_groups);
733 if (shader->selector->info.uses_block_size &&
734 shader->selector->info.properties[TGSI_PROPERTY_CS_FIXED_BLOCK_WIDTH] == 0)
735 ac_add_arg(&ctx->args, AC_ARG_SGPR, 3, AC_ARG_INT, &ctx->block_size);
736
737 unsigned cs_user_data_dwords =
738 shader->selector->info.properties[TGSI_PROPERTY_CS_USER_DATA_COMPONENTS_AMD];
739 if (cs_user_data_dwords) {
740 ac_add_arg(&ctx->args, AC_ARG_SGPR, cs_user_data_dwords, AC_ARG_INT,
741 &ctx->cs_user_data);
742 }
743
744 /* Hardware SGPRs. */
745 for (i = 0; i < 3; i++) {
746 if (shader->selector->info.uses_block_id[i]) {
747 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT,
748 &ctx->args.workgroup_ids[i]);
749 }
750 }
751 if (shader->selector->info.uses_subgroup_info)
752 ac_add_arg(&ctx->args, AC_ARG_SGPR, 1, AC_ARG_INT, &ctx->args.tg_size);
753
754 /* Hardware VGPRs. */
755 ac_add_arg(&ctx->args, AC_ARG_VGPR, 3, AC_ARG_INT,
756 &ctx->args.local_invocation_ids);
757 break;
758 default:
759 assert(0 && "unimplemented shader");
760 return;
761 }
762
763 si_llvm_create_func(ctx, ngg_cull_shader ? "ngg_cull_main" : "main",
764 returns, num_returns, si_get_max_workgroup_size(shader));
765
766 /* Reserve register locations for VGPR inputs the PS prolog may need. */
767 if (ctx->type == PIPE_SHADER_FRAGMENT && !ctx->shader->is_monolithic) {
768 ac_llvm_add_target_dep_function_attr(ctx->main_fn,
769 "InitialPSInputAddr",
770 S_0286D0_PERSP_SAMPLE_ENA(1) |
771 S_0286D0_PERSP_CENTER_ENA(1) |
772 S_0286D0_PERSP_CENTROID_ENA(1) |
773 S_0286D0_LINEAR_SAMPLE_ENA(1) |
774 S_0286D0_LINEAR_CENTER_ENA(1) |
775 S_0286D0_LINEAR_CENTROID_ENA(1) |
776 S_0286D0_FRONT_FACE_ENA(1) |
777 S_0286D0_ANCILLARY_ENA(1) |
778 S_0286D0_POS_FIXED_PT_ENA(1));
779 }
780
781 shader->info.num_input_sgprs = ctx->args.num_sgprs_used;
782 shader->info.num_input_vgprs = ctx->args.num_vgprs_used;
783
784 assert(shader->info.num_input_vgprs >= num_prolog_vgprs);
785 shader->info.num_input_vgprs -= num_prolog_vgprs;
786
787 if (shader->key.as_ls || ctx->type == PIPE_SHADER_TESS_CTRL) {
788 if (USE_LDS_SYMBOLS && LLVM_VERSION_MAJOR >= 9) {
789 /* The LSHS size is not known until draw time, so we append it
790 * at the end of whatever LDS use there may be in the rest of
791 * the shader (currently none, unless LLVM decides to do its
792 * own LDS-based lowering).
793 */
794 ctx->ac.lds = LLVMAddGlobalInAddressSpace(
795 ctx->ac.module, LLVMArrayType(ctx->ac.i32, 0),
796 "__lds_end", AC_ADDR_SPACE_LDS);
797 LLVMSetAlignment(ctx->ac.lds, 256);
798 } else {
799 ac_declare_lds_as_pointer(&ctx->ac);
800 }
801 }
802
803 /* Unlike radv, we override these arguments in the prolog, so to the
804 * API shader they appear as normal arguments.
805 */
806 if (ctx->type == PIPE_SHADER_VERTEX) {
807 ctx->abi.vertex_id = ac_get_arg(&ctx->ac, ctx->args.vertex_id);
808 ctx->abi.instance_id = ac_get_arg(&ctx->ac, ctx->args.instance_id);
809 } else if (ctx->type == PIPE_SHADER_FRAGMENT) {
810 ctx->abi.persp_centroid = ac_get_arg(&ctx->ac, ctx->args.persp_centroid);
811 ctx->abi.linear_centroid = ac_get_arg(&ctx->ac, ctx->args.linear_centroid);
812 }
813 }
814
815 /* For the UMR disassembler. */
816 #define DEBUGGER_END_OF_CODE_MARKER 0xbf9f0000 /* invalid instruction */
817 #define DEBUGGER_NUM_MARKERS 5
818
819 static bool si_shader_binary_open(struct si_screen *screen,
820 struct si_shader *shader,
821 struct ac_rtld_binary *rtld)
822 {
823 const struct si_shader_selector *sel = shader->selector;
824 const char *part_elfs[5];
825 size_t part_sizes[5];
826 unsigned num_parts = 0;
827
828 #define add_part(shader_or_part) \
829 if (shader_or_part) { \
830 part_elfs[num_parts] = (shader_or_part)->binary.elf_buffer; \
831 part_sizes[num_parts] = (shader_or_part)->binary.elf_size; \
832 num_parts++; \
833 }
834
835 add_part(shader->prolog);
836 add_part(shader->previous_stage);
837 add_part(shader->prolog2);
838 add_part(shader);
839 add_part(shader->epilog);
840
841 #undef add_part
842
843 struct ac_rtld_symbol lds_symbols[2];
844 unsigned num_lds_symbols = 0;
845
846 if (sel && screen->info.chip_class >= GFX9 && !shader->is_gs_copy_shader &&
847 (sel->type == PIPE_SHADER_GEOMETRY || shader->key.as_ngg)) {
848 /* We add this symbol even on LLVM <= 8 to ensure that
849 * shader->config.lds_size is set correctly below.
850 */
851 struct ac_rtld_symbol *sym = &lds_symbols[num_lds_symbols++];
852 sym->name = "esgs_ring";
853 sym->size = shader->gs_info.esgs_ring_size;
854 sym->align = 64 * 1024;
855 }
856
857 if (shader->key.as_ngg && sel->type == PIPE_SHADER_GEOMETRY) {
858 struct ac_rtld_symbol *sym = &lds_symbols[num_lds_symbols++];
859 sym->name = "ngg_emit";
860 sym->size = shader->ngg.ngg_emit_size * 4;
861 sym->align = 4;
862 }
863
864 bool ok = ac_rtld_open(rtld, (struct ac_rtld_open_info){
865 .info = &screen->info,
866 .options = {
867 .halt_at_entry = screen->options.halt_shaders,
868 },
869 .shader_type = tgsi_processor_to_shader_stage(sel->type),
870 .wave_size = si_get_shader_wave_size(shader),
871 .num_parts = num_parts,
872 .elf_ptrs = part_elfs,
873 .elf_sizes = part_sizes,
874 .num_shared_lds_symbols = num_lds_symbols,
875 .shared_lds_symbols = lds_symbols });
876
877 if (rtld->lds_size > 0) {
878 unsigned alloc_granularity = screen->info.chip_class >= GFX7 ? 512 : 256;
879 shader->config.lds_size =
880 align(rtld->lds_size, alloc_granularity) / alloc_granularity;
881 }
882
883 return ok;
884 }
885
886 static unsigned si_get_shader_binary_size(struct si_screen *screen, struct si_shader *shader)
887 {
888 struct ac_rtld_binary rtld;
889 si_shader_binary_open(screen, shader, &rtld);
890 return rtld.exec_size;
891 }
892
893 static bool si_get_external_symbol(void *data, const char *name, uint64_t *value)
894 {
895 uint64_t *scratch_va = data;
896
897 if (!strcmp(scratch_rsrc_dword0_symbol, name)) {
898 *value = (uint32_t)*scratch_va;
899 return true;
900 }
901 if (!strcmp(scratch_rsrc_dword1_symbol, name)) {
902 /* Enable scratch coalescing. */
903 *value = S_008F04_BASE_ADDRESS_HI(*scratch_va >> 32) |
904 S_008F04_SWIZZLE_ENABLE(1);
905 return true;
906 }
907
908 return false;
909 }
910
911 bool si_shader_binary_upload(struct si_screen *sscreen, struct si_shader *shader,
912 uint64_t scratch_va)
913 {
914 struct ac_rtld_binary binary;
915 if (!si_shader_binary_open(sscreen, shader, &binary))
916 return false;
917
918 si_resource_reference(&shader->bo, NULL);
919 shader->bo = si_aligned_buffer_create(&sscreen->b,
920 sscreen->info.cpdma_prefetch_writes_memory ?
921 0 : SI_RESOURCE_FLAG_READ_ONLY,
922 PIPE_USAGE_IMMUTABLE,
923 align(binary.rx_size, SI_CPDMA_ALIGNMENT),
924 256);
925 if (!shader->bo)
926 return false;
927
928 /* Upload. */
929 struct ac_rtld_upload_info u = {};
930 u.binary = &binary;
931 u.get_external_symbol = si_get_external_symbol;
932 u.cb_data = &scratch_va;
933 u.rx_va = shader->bo->gpu_address;
934 u.rx_ptr = sscreen->ws->buffer_map(shader->bo->buf, NULL,
935 PIPE_TRANSFER_READ_WRITE |
936 PIPE_TRANSFER_UNSYNCHRONIZED |
937 RADEON_TRANSFER_TEMPORARY);
938 if (!u.rx_ptr)
939 return false;
940
941 bool ok = ac_rtld_upload(&u);
942
943 sscreen->ws->buffer_unmap(shader->bo->buf);
944 ac_rtld_close(&binary);
945
946 return ok;
947 }
948
949 static void si_shader_dump_disassembly(struct si_screen *screen,
950 const struct si_shader_binary *binary,
951 enum pipe_shader_type shader_type,
952 unsigned wave_size,
953 struct pipe_debug_callback *debug,
954 const char *name, FILE *file)
955 {
956 struct ac_rtld_binary rtld_binary;
957
958 if (!ac_rtld_open(&rtld_binary, (struct ac_rtld_open_info){
959 .info = &screen->info,
960 .shader_type = tgsi_processor_to_shader_stage(shader_type),
961 .wave_size = wave_size,
962 .num_parts = 1,
963 .elf_ptrs = &binary->elf_buffer,
964 .elf_sizes = &binary->elf_size }))
965 return;
966
967 const char *disasm;
968 size_t nbytes;
969
970 if (!ac_rtld_get_section_by_name(&rtld_binary, ".AMDGPU.disasm", &disasm, &nbytes))
971 goto out;
972
973 if (nbytes > INT_MAX)
974 goto out;
975
976 if (debug && debug->debug_message) {
977 /* Very long debug messages are cut off, so send the
978 * disassembly one line at a time. This causes more
979 * overhead, but on the plus side it simplifies
980 * parsing of resulting logs.
981 */
982 pipe_debug_message(debug, SHADER_INFO,
983 "Shader Disassembly Begin");
984
985 uint64_t line = 0;
986 while (line < nbytes) {
987 int count = nbytes - line;
988 const char *nl = memchr(disasm + line, '\n', nbytes - line);
989 if (nl)
990 count = nl - (disasm + line);
991
992 if (count) {
993 pipe_debug_message(debug, SHADER_INFO,
994 "%.*s", count, disasm + line);
995 }
996
997 line += count + 1;
998 }
999
1000 pipe_debug_message(debug, SHADER_INFO,
1001 "Shader Disassembly End");
1002 }
1003
1004 if (file) {
1005 fprintf(file, "Shader %s disassembly:\n", name);
1006 fprintf(file, "%*s", (int)nbytes, disasm);
1007 }
1008
1009 out:
1010 ac_rtld_close(&rtld_binary);
1011 }
1012
1013 static void si_calculate_max_simd_waves(struct si_shader *shader)
1014 {
1015 struct si_screen *sscreen = shader->selector->screen;
1016 struct ac_shader_config *conf = &shader->config;
1017 unsigned num_inputs = shader->selector->info.num_inputs;
1018 unsigned lds_increment = sscreen->info.chip_class >= GFX7 ? 512 : 256;
1019 unsigned lds_per_wave = 0;
1020 unsigned max_simd_waves;
1021
1022 max_simd_waves = sscreen->info.max_wave64_per_simd;
1023
1024 /* Compute LDS usage for PS. */
1025 switch (shader->selector->type) {
1026 case PIPE_SHADER_FRAGMENT:
1027 /* The minimum usage per wave is (num_inputs * 48). The maximum
1028 * usage is (num_inputs * 48 * 16).
1029 * We can get anything in between and it varies between waves.
1030 *
1031 * The 48 bytes per input for a single primitive is equal to
1032 * 4 bytes/component * 4 components/input * 3 points.
1033 *
1034 * Other stages don't know the size at compile time or don't
1035 * allocate LDS per wave, but instead they do it per thread group.
1036 */
1037 lds_per_wave = conf->lds_size * lds_increment +
1038 align(num_inputs * 48, lds_increment);
1039 break;
1040 case PIPE_SHADER_COMPUTE:
1041 if (shader->selector) {
1042 unsigned max_workgroup_size =
1043 si_get_max_workgroup_size(shader);
1044 lds_per_wave = (conf->lds_size * lds_increment) /
1045 DIV_ROUND_UP(max_workgroup_size,
1046 sscreen->compute_wave_size);
1047 }
1048 break;
1049 default:;
1050 }
1051
1052 /* Compute the per-SIMD wave counts. */
1053 if (conf->num_sgprs) {
1054 max_simd_waves =
1055 MIN2(max_simd_waves,
1056 sscreen->info.num_physical_sgprs_per_simd / conf->num_sgprs);
1057 }
1058
1059 if (conf->num_vgprs) {
1060 /* Always print wave limits as Wave64, so that we can compare
1061 * Wave32 and Wave64 with shader-db fairly. */
1062 unsigned max_vgprs = sscreen->info.num_physical_wave64_vgprs_per_simd;
1063 max_simd_waves = MIN2(max_simd_waves, max_vgprs / conf->num_vgprs);
1064 }
1065
1066 unsigned max_lds_per_simd = sscreen->info.lds_size_per_workgroup / 4;
1067 if (lds_per_wave)
1068 max_simd_waves = MIN2(max_simd_waves, max_lds_per_simd / lds_per_wave);
1069
1070 shader->info.max_simd_waves = max_simd_waves;
1071 }
1072
1073 void si_shader_dump_stats_for_shader_db(struct si_screen *screen,
1074 struct si_shader *shader,
1075 struct pipe_debug_callback *debug)
1076 {
1077 const struct ac_shader_config *conf = &shader->config;
1078
1079 if (screen->options.debug_disassembly)
1080 si_shader_dump_disassembly(screen, &shader->binary,
1081 shader->selector->type,
1082 si_get_shader_wave_size(shader),
1083 debug, "main", NULL);
1084
1085 pipe_debug_message(debug, SHADER_INFO,
1086 "Shader Stats: SGPRS: %d VGPRS: %d Code Size: %d "
1087 "LDS: %d Scratch: %d Max Waves: %d Spilled SGPRs: %d "
1088 "Spilled VGPRs: %d PrivMem VGPRs: %d",
1089 conf->num_sgprs, conf->num_vgprs,
1090 si_get_shader_binary_size(screen, shader),
1091 conf->lds_size, conf->scratch_bytes_per_wave,
1092 shader->info.max_simd_waves, conf->spilled_sgprs,
1093 conf->spilled_vgprs, shader->info.private_mem_vgprs);
1094 }
1095
1096 static void si_shader_dump_stats(struct si_screen *sscreen,
1097 struct si_shader *shader,
1098 FILE *file,
1099 bool check_debug_option)
1100 {
1101 const struct ac_shader_config *conf = &shader->config;
1102
1103 if (!check_debug_option ||
1104 si_can_dump_shader(sscreen, shader->selector->type)) {
1105 if (shader->selector->type == PIPE_SHADER_FRAGMENT) {
1106 fprintf(file, "*** SHADER CONFIG ***\n"
1107 "SPI_PS_INPUT_ADDR = 0x%04x\n"
1108 "SPI_PS_INPUT_ENA = 0x%04x\n",
1109 conf->spi_ps_input_addr, conf->spi_ps_input_ena);
1110 }
1111
1112 fprintf(file, "*** SHADER STATS ***\n"
1113 "SGPRS: %d\n"
1114 "VGPRS: %d\n"
1115 "Spilled SGPRs: %d\n"
1116 "Spilled VGPRs: %d\n"
1117 "Private memory VGPRs: %d\n"
1118 "Code Size: %d bytes\n"
1119 "LDS: %d blocks\n"
1120 "Scratch: %d bytes per wave\n"
1121 "Max Waves: %d\n"
1122 "********************\n\n\n",
1123 conf->num_sgprs, conf->num_vgprs,
1124 conf->spilled_sgprs, conf->spilled_vgprs,
1125 shader->info.private_mem_vgprs,
1126 si_get_shader_binary_size(sscreen, shader),
1127 conf->lds_size, conf->scratch_bytes_per_wave,
1128 shader->info.max_simd_waves);
1129 }
1130 }
1131
1132 const char *si_get_shader_name(const struct si_shader *shader)
1133 {
1134 switch (shader->selector->type) {
1135 case PIPE_SHADER_VERTEX:
1136 if (shader->key.as_es)
1137 return "Vertex Shader as ES";
1138 else if (shader->key.as_ls)
1139 return "Vertex Shader as LS";
1140 else if (shader->key.opt.vs_as_prim_discard_cs)
1141 return "Vertex Shader as Primitive Discard CS";
1142 else if (shader->key.as_ngg)
1143 return "Vertex Shader as ESGS";
1144 else
1145 return "Vertex Shader as VS";
1146 case PIPE_SHADER_TESS_CTRL:
1147 return "Tessellation Control Shader";
1148 case PIPE_SHADER_TESS_EVAL:
1149 if (shader->key.as_es)
1150 return "Tessellation Evaluation Shader as ES";
1151 else if (shader->key.as_ngg)
1152 return "Tessellation Evaluation Shader as ESGS";
1153 else
1154 return "Tessellation Evaluation Shader as VS";
1155 case PIPE_SHADER_GEOMETRY:
1156 if (shader->is_gs_copy_shader)
1157 return "GS Copy Shader as VS";
1158 else
1159 return "Geometry Shader";
1160 case PIPE_SHADER_FRAGMENT:
1161 return "Pixel Shader";
1162 case PIPE_SHADER_COMPUTE:
1163 return "Compute Shader";
1164 default:
1165 return "Unknown Shader";
1166 }
1167 }
1168
1169 void si_shader_dump(struct si_screen *sscreen, struct si_shader *shader,
1170 struct pipe_debug_callback *debug,
1171 FILE *file, bool check_debug_option)
1172 {
1173 enum pipe_shader_type shader_type = shader->selector->type;
1174
1175 if (!check_debug_option ||
1176 si_can_dump_shader(sscreen, shader_type))
1177 si_dump_shader_key(shader, file);
1178
1179 if (!check_debug_option && shader->binary.llvm_ir_string) {
1180 if (shader->previous_stage &&
1181 shader->previous_stage->binary.llvm_ir_string) {
1182 fprintf(file, "\n%s - previous stage - LLVM IR:\n\n",
1183 si_get_shader_name(shader));
1184 fprintf(file, "%s\n", shader->previous_stage->binary.llvm_ir_string);
1185 }
1186
1187 fprintf(file, "\n%s - main shader part - LLVM IR:\n\n",
1188 si_get_shader_name(shader));
1189 fprintf(file, "%s\n", shader->binary.llvm_ir_string);
1190 }
1191
1192 if (!check_debug_option ||
1193 (si_can_dump_shader(sscreen, shader_type) &&
1194 !(sscreen->debug_flags & DBG(NO_ASM)))) {
1195 unsigned wave_size = si_get_shader_wave_size(shader);
1196
1197 fprintf(file, "\n%s:\n", si_get_shader_name(shader));
1198
1199 if (shader->prolog)
1200 si_shader_dump_disassembly(sscreen, &shader->prolog->binary,
1201 shader_type, wave_size, debug, "prolog", file);
1202 if (shader->previous_stage)
1203 si_shader_dump_disassembly(sscreen, &shader->previous_stage->binary,
1204 shader_type, wave_size, debug, "previous stage", file);
1205 if (shader->prolog2)
1206 si_shader_dump_disassembly(sscreen, &shader->prolog2->binary,
1207 shader_type, wave_size, debug, "prolog2", file);
1208
1209 si_shader_dump_disassembly(sscreen, &shader->binary, shader_type,
1210 wave_size, debug, "main", file);
1211
1212 if (shader->epilog)
1213 si_shader_dump_disassembly(sscreen, &shader->epilog->binary,
1214 shader_type, wave_size, debug, "epilog", file);
1215 fprintf(file, "\n");
1216 }
1217
1218 si_shader_dump_stats(sscreen, shader, file, check_debug_option);
1219 }
1220
1221 static void si_dump_shader_key_vs(const struct si_shader_key *key,
1222 const struct si_vs_prolog_bits *prolog,
1223 const char *prefix, FILE *f)
1224 {
1225 fprintf(f, " %s.instance_divisor_is_one = %u\n",
1226 prefix, prolog->instance_divisor_is_one);
1227 fprintf(f, " %s.instance_divisor_is_fetched = %u\n",
1228 prefix, prolog->instance_divisor_is_fetched);
1229 fprintf(f, " %s.unpack_instance_id_from_vertex_id = %u\n",
1230 prefix, prolog->unpack_instance_id_from_vertex_id);
1231 fprintf(f, " %s.ls_vgpr_fix = %u\n",
1232 prefix, prolog->ls_vgpr_fix);
1233
1234 fprintf(f, " mono.vs.fetch_opencode = %x\n", key->mono.vs_fetch_opencode);
1235 fprintf(f, " mono.vs.fix_fetch = {");
1236 for (int i = 0; i < SI_MAX_ATTRIBS; i++) {
1237 union si_vs_fix_fetch fix = key->mono.vs_fix_fetch[i];
1238 if (i)
1239 fprintf(f, ", ");
1240 if (!fix.bits)
1241 fprintf(f, "0");
1242 else
1243 fprintf(f, "%u.%u.%u.%u", fix.u.reverse, fix.u.log_size,
1244 fix.u.num_channels_m1, fix.u.format);
1245 }
1246 fprintf(f, "}\n");
1247 }
1248
1249 static void si_dump_shader_key(const struct si_shader *shader, FILE *f)
1250 {
1251 const struct si_shader_key *key = &shader->key;
1252 enum pipe_shader_type shader_type = shader->selector->type;
1253
1254 fprintf(f, "SHADER KEY\n");
1255
1256 switch (shader_type) {
1257 case PIPE_SHADER_VERTEX:
1258 si_dump_shader_key_vs(key, &key->part.vs.prolog,
1259 "part.vs.prolog", f);
1260 fprintf(f, " as_es = %u\n", key->as_es);
1261 fprintf(f, " as_ls = %u\n", key->as_ls);
1262 fprintf(f, " as_ngg = %u\n", key->as_ngg);
1263 fprintf(f, " mono.u.vs_export_prim_id = %u\n",
1264 key->mono.u.vs_export_prim_id);
1265 fprintf(f, " opt.vs_as_prim_discard_cs = %u\n",
1266 key->opt.vs_as_prim_discard_cs);
1267 fprintf(f, " opt.cs_prim_type = %s\n",
1268 tgsi_primitive_names[key->opt.cs_prim_type]);
1269 fprintf(f, " opt.cs_indexed = %u\n",
1270 key->opt.cs_indexed);
1271 fprintf(f, " opt.cs_instancing = %u\n",
1272 key->opt.cs_instancing);
1273 fprintf(f, " opt.cs_primitive_restart = %u\n",
1274 key->opt.cs_primitive_restart);
1275 fprintf(f, " opt.cs_provoking_vertex_first = %u\n",
1276 key->opt.cs_provoking_vertex_first);
1277 fprintf(f, " opt.cs_need_correct_orientation = %u\n",
1278 key->opt.cs_need_correct_orientation);
1279 fprintf(f, " opt.cs_cull_front = %u\n",
1280 key->opt.cs_cull_front);
1281 fprintf(f, " opt.cs_cull_back = %u\n",
1282 key->opt.cs_cull_back);
1283 fprintf(f, " opt.cs_cull_z = %u\n",
1284 key->opt.cs_cull_z);
1285 fprintf(f, " opt.cs_halfz_clip_space = %u\n",
1286 key->opt.cs_halfz_clip_space);
1287 break;
1288
1289 case PIPE_SHADER_TESS_CTRL:
1290 if (shader->selector->screen->info.chip_class >= GFX9) {
1291 si_dump_shader_key_vs(key, &key->part.tcs.ls_prolog,
1292 "part.tcs.ls_prolog", f);
1293 }
1294 fprintf(f, " part.tcs.epilog.prim_mode = %u\n", key->part.tcs.epilog.prim_mode);
1295 fprintf(f, " mono.u.ff_tcs_inputs_to_copy = 0x%"PRIx64"\n", key->mono.u.ff_tcs_inputs_to_copy);
1296 break;
1297
1298 case PIPE_SHADER_TESS_EVAL:
1299 fprintf(f, " as_es = %u\n", key->as_es);
1300 fprintf(f, " as_ngg = %u\n", key->as_ngg);
1301 fprintf(f, " mono.u.vs_export_prim_id = %u\n",
1302 key->mono.u.vs_export_prim_id);
1303 break;
1304
1305 case PIPE_SHADER_GEOMETRY:
1306 if (shader->is_gs_copy_shader)
1307 break;
1308
1309 if (shader->selector->screen->info.chip_class >= GFX9 &&
1310 key->part.gs.es->type == PIPE_SHADER_VERTEX) {
1311 si_dump_shader_key_vs(key, &key->part.gs.vs_prolog,
1312 "part.gs.vs_prolog", f);
1313 }
1314 fprintf(f, " part.gs.prolog.tri_strip_adj_fix = %u\n", key->part.gs.prolog.tri_strip_adj_fix);
1315 fprintf(f, " part.gs.prolog.gfx9_prev_is_vs = %u\n", key->part.gs.prolog.gfx9_prev_is_vs);
1316 fprintf(f, " as_ngg = %u\n", key->as_ngg);
1317 break;
1318
1319 case PIPE_SHADER_COMPUTE:
1320 break;
1321
1322 case PIPE_SHADER_FRAGMENT:
1323 fprintf(f, " part.ps.prolog.color_two_side = %u\n", key->part.ps.prolog.color_two_side);
1324 fprintf(f, " part.ps.prolog.flatshade_colors = %u\n", key->part.ps.prolog.flatshade_colors);
1325 fprintf(f, " part.ps.prolog.poly_stipple = %u\n", key->part.ps.prolog.poly_stipple);
1326 fprintf(f, " part.ps.prolog.force_persp_sample_interp = %u\n", key->part.ps.prolog.force_persp_sample_interp);
1327 fprintf(f, " part.ps.prolog.force_linear_sample_interp = %u\n", key->part.ps.prolog.force_linear_sample_interp);
1328 fprintf(f, " part.ps.prolog.force_persp_center_interp = %u\n", key->part.ps.prolog.force_persp_center_interp);
1329 fprintf(f, " part.ps.prolog.force_linear_center_interp = %u\n", key->part.ps.prolog.force_linear_center_interp);
1330 fprintf(f, " part.ps.prolog.bc_optimize_for_persp = %u\n", key->part.ps.prolog.bc_optimize_for_persp);
1331 fprintf(f, " part.ps.prolog.bc_optimize_for_linear = %u\n", key->part.ps.prolog.bc_optimize_for_linear);
1332 fprintf(f, " part.ps.prolog.samplemask_log_ps_iter = %u\n", key->part.ps.prolog.samplemask_log_ps_iter);
1333 fprintf(f, " part.ps.epilog.spi_shader_col_format = 0x%x\n", key->part.ps.epilog.spi_shader_col_format);
1334 fprintf(f, " part.ps.epilog.color_is_int8 = 0x%X\n", key->part.ps.epilog.color_is_int8);
1335 fprintf(f, " part.ps.epilog.color_is_int10 = 0x%X\n", key->part.ps.epilog.color_is_int10);
1336 fprintf(f, " part.ps.epilog.last_cbuf = %u\n", key->part.ps.epilog.last_cbuf);
1337 fprintf(f, " part.ps.epilog.alpha_func = %u\n", key->part.ps.epilog.alpha_func);
1338 fprintf(f, " part.ps.epilog.alpha_to_one = %u\n", key->part.ps.epilog.alpha_to_one);
1339 fprintf(f, " part.ps.epilog.poly_line_smoothing = %u\n", key->part.ps.epilog.poly_line_smoothing);
1340 fprintf(f, " part.ps.epilog.clamp_color = %u\n", key->part.ps.epilog.clamp_color);
1341 fprintf(f, " mono.u.ps.interpolate_at_sample_force_center = %u\n", key->mono.u.ps.interpolate_at_sample_force_center);
1342 fprintf(f, " mono.u.ps.fbfetch_msaa = %u\n", key->mono.u.ps.fbfetch_msaa);
1343 fprintf(f, " mono.u.ps.fbfetch_is_1D = %u\n", key->mono.u.ps.fbfetch_is_1D);
1344 fprintf(f, " mono.u.ps.fbfetch_layered = %u\n", key->mono.u.ps.fbfetch_layered);
1345 break;
1346
1347 default:
1348 assert(0);
1349 }
1350
1351 if ((shader_type == PIPE_SHADER_GEOMETRY ||
1352 shader_type == PIPE_SHADER_TESS_EVAL ||
1353 shader_type == PIPE_SHADER_VERTEX) &&
1354 !key->as_es && !key->as_ls) {
1355 fprintf(f, " opt.kill_outputs = 0x%"PRIx64"\n", key->opt.kill_outputs);
1356 fprintf(f, " opt.clip_disable = %u\n", key->opt.clip_disable);
1357 if (shader_type != PIPE_SHADER_GEOMETRY)
1358 fprintf(f, " opt.ngg_culling = 0x%x\n", key->opt.ngg_culling);
1359 }
1360 }
1361
1362 static void si_optimize_vs_outputs(struct si_shader_context *ctx)
1363 {
1364 struct si_shader *shader = ctx->shader;
1365 struct si_shader_info *info = &shader->selector->info;
1366
1367 if ((ctx->type != PIPE_SHADER_VERTEX &&
1368 ctx->type != PIPE_SHADER_TESS_EVAL) ||
1369 shader->key.as_ls ||
1370 shader->key.as_es)
1371 return;
1372
1373 ac_optimize_vs_outputs(&ctx->ac,
1374 ctx->main_fn,
1375 shader->info.vs_output_param_offset,
1376 info->num_outputs,
1377 &shader->info.nr_param_exports);
1378 }
1379
1380 static bool si_vs_needs_prolog(const struct si_shader_selector *sel,
1381 const struct si_vs_prolog_bits *prolog_key,
1382 const struct si_shader_key *key,
1383 bool ngg_cull_shader)
1384 {
1385 /* VGPR initialization fixup for Vega10 and Raven is always done in the
1386 * VS prolog. */
1387 return sel->vs_needs_prolog ||
1388 prolog_key->ls_vgpr_fix ||
1389 prolog_key->unpack_instance_id_from_vertex_id ||
1390 (ngg_cull_shader && key->opt.ngg_culling & SI_NGG_CULL_GS_FAST_LAUNCH_ALL);
1391 }
1392
1393 static bool si_build_main_function(struct si_shader_context *ctx,
1394 struct si_shader *shader,
1395 struct nir_shader *nir, bool free_nir,
1396 bool ngg_cull_shader)
1397 {
1398 struct si_shader_selector *sel = shader->selector;
1399 const struct si_shader_info *info = &sel->info;
1400
1401 ctx->shader = shader;
1402 ctx->type = sel->type;
1403
1404 ctx->num_const_buffers = util_last_bit(info->const_buffers_declared);
1405 ctx->num_shader_buffers = util_last_bit(info->shader_buffers_declared);
1406
1407 ctx->num_samplers = util_last_bit(info->samplers_declared);
1408 ctx->num_images = util_last_bit(info->images_declared);
1409
1410 si_llvm_init_resource_callbacks(ctx);
1411
1412 switch (ctx->type) {
1413 case PIPE_SHADER_VERTEX:
1414 si_llvm_init_vs_callbacks(ctx, ngg_cull_shader);
1415 break;
1416 case PIPE_SHADER_TESS_CTRL:
1417 si_llvm_init_tcs_callbacks(ctx);
1418 break;
1419 case PIPE_SHADER_TESS_EVAL:
1420 si_llvm_init_tes_callbacks(ctx, ngg_cull_shader);
1421 break;
1422 case PIPE_SHADER_GEOMETRY:
1423 si_llvm_init_gs_callbacks(ctx);
1424 break;
1425 case PIPE_SHADER_FRAGMENT:
1426 si_llvm_init_ps_callbacks(ctx);
1427 break;
1428 case PIPE_SHADER_COMPUTE:
1429 ctx->abi.load_local_group_size = si_llvm_get_block_size;
1430 break;
1431 default:
1432 assert(!"Unsupported shader type");
1433 return false;
1434 }
1435
1436 si_create_function(ctx, ngg_cull_shader);
1437
1438 if (ctx->shader->key.as_es || ctx->type == PIPE_SHADER_GEOMETRY)
1439 si_preload_esgs_ring(ctx);
1440
1441 if (ctx->type == PIPE_SHADER_GEOMETRY)
1442 si_preload_gs_rings(ctx);
1443 else if (ctx->type == PIPE_SHADER_TESS_EVAL)
1444 si_llvm_preload_tes_rings(ctx);
1445
1446 if (ctx->type == PIPE_SHADER_TESS_CTRL &&
1447 sel->info.tessfactors_are_def_in_all_invocs) {
1448 for (unsigned i = 0; i < 6; i++) {
1449 ctx->invoc0_tess_factors[i] =
1450 ac_build_alloca_undef(&ctx->ac, ctx->ac.i32, "");
1451 }
1452 }
1453
1454 if (ctx->type == PIPE_SHADER_GEOMETRY) {
1455 for (unsigned i = 0; i < 4; i++) {
1456 ctx->gs_next_vertex[i] =
1457 ac_build_alloca(&ctx->ac, ctx->ac.i32, "");
1458 }
1459 if (shader->key.as_ngg) {
1460 for (unsigned i = 0; i < 4; ++i) {
1461 ctx->gs_curprim_verts[i] =
1462 ac_build_alloca(&ctx->ac, ctx->ac.i32, "");
1463 ctx->gs_generated_prims[i] =
1464 ac_build_alloca(&ctx->ac, ctx->ac.i32, "");
1465 }
1466
1467 unsigned scratch_size = 8;
1468 if (sel->so.num_outputs)
1469 scratch_size = 44;
1470
1471 assert(!ctx->gs_ngg_scratch);
1472 LLVMTypeRef ai32 = LLVMArrayType(ctx->ac.i32, scratch_size);
1473 ctx->gs_ngg_scratch = LLVMAddGlobalInAddressSpace(ctx->ac.module,
1474 ai32, "ngg_scratch", AC_ADDR_SPACE_LDS);
1475 LLVMSetInitializer(ctx->gs_ngg_scratch, LLVMGetUndef(ai32));
1476 LLVMSetAlignment(ctx->gs_ngg_scratch, 4);
1477
1478 ctx->gs_ngg_emit = LLVMAddGlobalInAddressSpace(ctx->ac.module,
1479 LLVMArrayType(ctx->ac.i32, 0), "ngg_emit", AC_ADDR_SPACE_LDS);
1480 LLVMSetLinkage(ctx->gs_ngg_emit, LLVMExternalLinkage);
1481 LLVMSetAlignment(ctx->gs_ngg_emit, 4);
1482 }
1483 }
1484
1485 if (ctx->type != PIPE_SHADER_GEOMETRY &&
1486 (shader->key.as_ngg && !shader->key.as_es)) {
1487 /* Unconditionally declare scratch space base for streamout and
1488 * vertex compaction. Whether space is actually allocated is
1489 * determined during linking / PM4 creation.
1490 *
1491 * Add an extra dword per vertex to ensure an odd stride, which
1492 * avoids bank conflicts for SoA accesses.
1493 */
1494 if (!gfx10_is_ngg_passthrough(shader))
1495 si_llvm_declare_esgs_ring(ctx);
1496
1497 /* This is really only needed when streamout and / or vertex
1498 * compaction is enabled.
1499 */
1500 if (!ctx->gs_ngg_scratch &&
1501 (sel->so.num_outputs || shader->key.opt.ngg_culling)) {
1502 LLVMTypeRef asi32 = LLVMArrayType(ctx->ac.i32, 8);
1503 ctx->gs_ngg_scratch = LLVMAddGlobalInAddressSpace(ctx->ac.module,
1504 asi32, "ngg_scratch", AC_ADDR_SPACE_LDS);
1505 LLVMSetInitializer(ctx->gs_ngg_scratch, LLVMGetUndef(asi32));
1506 LLVMSetAlignment(ctx->gs_ngg_scratch, 4);
1507 }
1508 }
1509
1510 /* For GFX9 merged shaders:
1511 * - Set EXEC for the first shader. If the prolog is present, set
1512 * EXEC there instead.
1513 * - Add a barrier before the second shader.
1514 * - In the second shader, reset EXEC to ~0 and wrap the main part in
1515 * an if-statement. This is required for correctness in geometry
1516 * shaders, to ensure that empty GS waves do not send GS_EMIT and
1517 * GS_CUT messages.
1518 *
1519 * For monolithic merged shaders, the first shader is wrapped in an
1520 * if-block together with its prolog in si_build_wrapper_function.
1521 *
1522 * NGG vertex and tess eval shaders running as the last
1523 * vertex/geometry stage handle execution explicitly using
1524 * if-statements.
1525 */
1526 if (ctx->screen->info.chip_class >= GFX9) {
1527 if (!shader->is_monolithic &&
1528 (shader->key.as_es || shader->key.as_ls) &&
1529 (ctx->type == PIPE_SHADER_TESS_EVAL ||
1530 (ctx->type == PIPE_SHADER_VERTEX &&
1531 !si_vs_needs_prolog(sel, &shader->key.part.vs.prolog,
1532 &shader->key, ngg_cull_shader)))) {
1533 si_init_exec_from_input(ctx,
1534 ctx->merged_wave_info, 0);
1535 } else if (ctx->type == PIPE_SHADER_TESS_CTRL ||
1536 ctx->type == PIPE_SHADER_GEOMETRY ||
1537 (shader->key.as_ngg && !shader->key.as_es)) {
1538 LLVMValueRef thread_enabled;
1539 bool nested_barrier;
1540
1541 if (!shader->is_monolithic ||
1542 (ctx->type == PIPE_SHADER_TESS_EVAL &&
1543 shader->key.as_ngg && !shader->key.as_es &&
1544 !shader->key.opt.ngg_culling))
1545 ac_init_exec_full_mask(&ctx->ac);
1546
1547 if ((ctx->type == PIPE_SHADER_VERTEX ||
1548 ctx->type == PIPE_SHADER_TESS_EVAL) &&
1549 shader->key.as_ngg && !shader->key.as_es &&
1550 !shader->key.opt.ngg_culling) {
1551 gfx10_ngg_build_sendmsg_gs_alloc_req(ctx);
1552
1553 /* Build the primitive export at the beginning
1554 * of the shader if possible.
1555 */
1556 if (gfx10_ngg_export_prim_early(shader))
1557 gfx10_ngg_build_export_prim(ctx, NULL, NULL);
1558 }
1559
1560 if (ctx->type == PIPE_SHADER_TESS_CTRL ||
1561 ctx->type == PIPE_SHADER_GEOMETRY) {
1562 if (ctx->type == PIPE_SHADER_GEOMETRY && shader->key.as_ngg) {
1563 gfx10_ngg_gs_emit_prologue(ctx);
1564 nested_barrier = false;
1565 } else {
1566 nested_barrier = true;
1567 }
1568
1569 thread_enabled = si_is_gs_thread(ctx);
1570 } else {
1571 thread_enabled = si_is_es_thread(ctx);
1572 nested_barrier = false;
1573 }
1574
1575 ctx->merged_wrap_if_entry_block = LLVMGetInsertBlock(ctx->ac.builder);
1576 ctx->merged_wrap_if_label = 11500;
1577 ac_build_ifcc(&ctx->ac, thread_enabled, ctx->merged_wrap_if_label);
1578
1579 if (nested_barrier) {
1580 /* Execute a barrier before the second shader in
1581 * a merged shader.
1582 *
1583 * Execute the barrier inside the conditional block,
1584 * so that empty waves can jump directly to s_endpgm,
1585 * which will also signal the barrier.
1586 *
1587 * This is possible in gfx9, because an empty wave
1588 * for the second shader does not participate in
1589 * the epilogue. With NGG, empty waves may still
1590 * be required to export data (e.g. GS output vertices),
1591 * so we cannot let them exit early.
1592 *
1593 * If the shader is TCS and the TCS epilog is present
1594 * and contains a barrier, it will wait there and then
1595 * reach s_endpgm.
1596 */
1597 si_llvm_emit_barrier(ctx);
1598 }
1599 }
1600 }
1601
1602 bool success = si_nir_build_llvm(ctx, nir);
1603 if (free_nir)
1604 ralloc_free(nir);
1605 if (!success) {
1606 fprintf(stderr, "Failed to translate shader from NIR to LLVM\n");
1607 return false;
1608 }
1609
1610 si_llvm_build_ret(ctx, ctx->return_value);
1611 return true;
1612 }
1613
1614 /**
1615 * Compute the VS prolog key, which contains all the information needed to
1616 * build the VS prolog function, and set shader->info bits where needed.
1617 *
1618 * \param info Shader info of the vertex shader.
1619 * \param num_input_sgprs Number of input SGPRs for the vertex shader.
1620 * \param has_old_ Whether the preceding shader part is the NGG cull shader.
1621 * \param prolog_key Key of the VS prolog
1622 * \param shader_out The vertex shader, or the next shader if merging LS+HS or ES+GS.
1623 * \param key Output shader part key.
1624 */
1625 static void si_get_vs_prolog_key(const struct si_shader_info *info,
1626 unsigned num_input_sgprs,
1627 bool ngg_cull_shader,
1628 const struct si_vs_prolog_bits *prolog_key,
1629 struct si_shader *shader_out,
1630 union si_shader_part_key *key)
1631 {
1632 memset(key, 0, sizeof(*key));
1633 key->vs_prolog.states = *prolog_key;
1634 key->vs_prolog.num_input_sgprs = num_input_sgprs;
1635 key->vs_prolog.num_inputs = info->num_inputs;
1636 key->vs_prolog.as_ls = shader_out->key.as_ls;
1637 key->vs_prolog.as_es = shader_out->key.as_es;
1638 key->vs_prolog.as_ngg = shader_out->key.as_ngg;
1639
1640 if (ngg_cull_shader) {
1641 key->vs_prolog.gs_fast_launch_tri_list = !!(shader_out->key.opt.ngg_culling &
1642 SI_NGG_CULL_GS_FAST_LAUNCH_TRI_LIST);
1643 key->vs_prolog.gs_fast_launch_tri_strip = !!(shader_out->key.opt.ngg_culling &
1644 SI_NGG_CULL_GS_FAST_LAUNCH_TRI_STRIP);
1645 } else {
1646 key->vs_prolog.has_ngg_cull_inputs = !!shader_out->key.opt.ngg_culling;
1647 }
1648
1649 if (shader_out->selector->type == PIPE_SHADER_TESS_CTRL) {
1650 key->vs_prolog.as_ls = 1;
1651 key->vs_prolog.num_merged_next_stage_vgprs = 2;
1652 } else if (shader_out->selector->type == PIPE_SHADER_GEOMETRY) {
1653 key->vs_prolog.as_es = 1;
1654 key->vs_prolog.num_merged_next_stage_vgprs = 5;
1655 } else if (shader_out->key.as_ngg) {
1656 key->vs_prolog.num_merged_next_stage_vgprs = 5;
1657 }
1658
1659 /* Enable loading the InstanceID VGPR. */
1660 uint16_t input_mask = u_bit_consecutive(0, info->num_inputs);
1661
1662 if ((key->vs_prolog.states.instance_divisor_is_one |
1663 key->vs_prolog.states.instance_divisor_is_fetched) & input_mask)
1664 shader_out->info.uses_instanceid = true;
1665 }
1666
1667 static bool si_should_optimize_less(struct ac_llvm_compiler *compiler,
1668 struct si_shader_selector *sel)
1669 {
1670 if (!compiler->low_opt_passes)
1671 return false;
1672
1673 /* Assume a slow CPU. */
1674 assert(!sel->screen->info.has_dedicated_vram &&
1675 sel->screen->info.chip_class <= GFX8);
1676
1677 /* For a crazy dEQP test containing 2597 memory opcodes, mostly
1678 * buffer stores. */
1679 return sel->type == PIPE_SHADER_COMPUTE &&
1680 sel->info.num_memory_instructions > 1000;
1681 }
1682
1683 static struct nir_shader *get_nir_shader(struct si_shader_selector *sel,
1684 bool *free_nir)
1685 {
1686 *free_nir = false;
1687
1688 if (sel->nir) {
1689 return sel->nir;
1690 } else if (sel->nir_binary) {
1691 struct pipe_screen *screen = &sel->screen->b;
1692 const void *options =
1693 screen->get_compiler_options(screen, PIPE_SHADER_IR_NIR,
1694 sel->type);
1695
1696 struct blob_reader blob_reader;
1697 blob_reader_init(&blob_reader, sel->nir_binary, sel->nir_size);
1698 *free_nir = true;
1699 return nir_deserialize(NULL, options, &blob_reader);
1700 }
1701 return NULL;
1702 }
1703
1704 static bool si_llvm_compile_shader(struct si_screen *sscreen,
1705 struct ac_llvm_compiler *compiler,
1706 struct si_shader *shader,
1707 struct pipe_debug_callback *debug,
1708 struct nir_shader *nir,
1709 bool free_nir)
1710 {
1711 struct si_shader_selector *sel = shader->selector;
1712 struct si_shader_context ctx;
1713
1714 si_llvm_context_init(&ctx, sscreen, compiler, si_get_shader_wave_size(shader));
1715
1716 LLVMValueRef ngg_cull_main_fn = NULL;
1717 if (shader->key.opt.ngg_culling) {
1718 if (!si_build_main_function(&ctx, shader, nir, false, true)) {
1719 si_llvm_dispose(&ctx);
1720 return false;
1721 }
1722 ngg_cull_main_fn = ctx.main_fn;
1723 ctx.main_fn = NULL;
1724 }
1725
1726 if (!si_build_main_function(&ctx, shader, nir, free_nir, false)) {
1727 si_llvm_dispose(&ctx);
1728 return false;
1729 }
1730
1731 if (shader->is_monolithic && ctx.type == PIPE_SHADER_VERTEX) {
1732 LLVMValueRef parts[4];
1733 unsigned num_parts = 0;
1734 bool has_prolog = false;
1735 LLVMValueRef main_fn = ctx.main_fn;
1736
1737 if (ngg_cull_main_fn) {
1738 if (si_vs_needs_prolog(sel, &shader->key.part.vs.prolog,
1739 &shader->key, true)) {
1740 union si_shader_part_key prolog_key;
1741 si_get_vs_prolog_key(&sel->info,
1742 shader->info.num_input_sgprs,
1743 true,
1744 &shader->key.part.vs.prolog,
1745 shader, &prolog_key);
1746 prolog_key.vs_prolog.is_monolithic = true;
1747 si_llvm_build_vs_prolog(&ctx, &prolog_key);
1748 parts[num_parts++] = ctx.main_fn;
1749 has_prolog = true;
1750 }
1751 parts[num_parts++] = ngg_cull_main_fn;
1752 }
1753
1754 if (si_vs_needs_prolog(sel, &shader->key.part.vs.prolog,
1755 &shader->key, false)) {
1756 union si_shader_part_key prolog_key;
1757 si_get_vs_prolog_key(&sel->info,
1758 shader->info.num_input_sgprs,
1759 false,
1760 &shader->key.part.vs.prolog,
1761 shader, &prolog_key);
1762 prolog_key.vs_prolog.is_monolithic = true;
1763 si_llvm_build_vs_prolog(&ctx, &prolog_key);
1764 parts[num_parts++] = ctx.main_fn;
1765 has_prolog = true;
1766 }
1767 parts[num_parts++] = main_fn;
1768
1769 si_build_wrapper_function(&ctx, parts, num_parts,
1770 has_prolog ? 1 : 0, 0);
1771
1772 if (ctx.shader->key.opt.vs_as_prim_discard_cs)
1773 si_build_prim_discard_compute_shader(&ctx);
1774 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_TESS_EVAL &&
1775 ngg_cull_main_fn) {
1776 LLVMValueRef parts[2];
1777
1778 parts[0] = ngg_cull_main_fn;
1779 parts[1] = ctx.main_fn;
1780
1781 si_build_wrapper_function(&ctx, parts, 2, 0, 0);
1782 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_TESS_CTRL) {
1783 if (sscreen->info.chip_class >= GFX9) {
1784 struct si_shader_selector *ls = shader->key.part.tcs.ls;
1785 LLVMValueRef parts[4];
1786 bool vs_needs_prolog =
1787 si_vs_needs_prolog(ls, &shader->key.part.tcs.ls_prolog,
1788 &shader->key, false);
1789
1790 /* TCS main part */
1791 parts[2] = ctx.main_fn;
1792
1793 /* TCS epilog */
1794 union si_shader_part_key tcs_epilog_key;
1795 memset(&tcs_epilog_key, 0, sizeof(tcs_epilog_key));
1796 tcs_epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
1797 si_llvm_build_tcs_epilog(&ctx, &tcs_epilog_key);
1798 parts[3] = ctx.main_fn;
1799
1800 /* VS as LS main part */
1801 nir = get_nir_shader(ls, &free_nir);
1802 struct si_shader shader_ls = {};
1803 shader_ls.selector = ls;
1804 shader_ls.key.as_ls = 1;
1805 shader_ls.key.mono = shader->key.mono;
1806 shader_ls.key.opt = shader->key.opt;
1807 shader_ls.is_monolithic = true;
1808
1809 if (!si_build_main_function(&ctx, &shader_ls, nir, free_nir, false)) {
1810 si_llvm_dispose(&ctx);
1811 return false;
1812 }
1813 shader->info.uses_instanceid |= ls->info.uses_instanceid;
1814 parts[1] = ctx.main_fn;
1815
1816 /* LS prolog */
1817 if (vs_needs_prolog) {
1818 union si_shader_part_key vs_prolog_key;
1819 si_get_vs_prolog_key(&ls->info,
1820 shader_ls.info.num_input_sgprs,
1821 false,
1822 &shader->key.part.tcs.ls_prolog,
1823 shader, &vs_prolog_key);
1824 vs_prolog_key.vs_prolog.is_monolithic = true;
1825 si_llvm_build_vs_prolog(&ctx, &vs_prolog_key);
1826 parts[0] = ctx.main_fn;
1827 }
1828
1829 /* Reset the shader context. */
1830 ctx.shader = shader;
1831 ctx.type = PIPE_SHADER_TESS_CTRL;
1832
1833 si_build_wrapper_function(&ctx,
1834 parts + !vs_needs_prolog,
1835 4 - !vs_needs_prolog, vs_needs_prolog,
1836 vs_needs_prolog ? 2 : 1);
1837 } else {
1838 LLVMValueRef parts[2];
1839 union si_shader_part_key epilog_key;
1840
1841 parts[0] = ctx.main_fn;
1842
1843 memset(&epilog_key, 0, sizeof(epilog_key));
1844 epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
1845 si_llvm_build_tcs_epilog(&ctx, &epilog_key);
1846 parts[1] = ctx.main_fn;
1847
1848 si_build_wrapper_function(&ctx, parts, 2, 0, 0);
1849 }
1850 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_GEOMETRY) {
1851 if (ctx.screen->info.chip_class >= GFX9) {
1852 struct si_shader_selector *es = shader->key.part.gs.es;
1853 LLVMValueRef es_prolog = NULL;
1854 LLVMValueRef es_main = NULL;
1855 LLVMValueRef gs_prolog = NULL;
1856 LLVMValueRef gs_main = ctx.main_fn;
1857
1858 /* GS prolog */
1859 union si_shader_part_key gs_prolog_key;
1860 memset(&gs_prolog_key, 0, sizeof(gs_prolog_key));
1861 gs_prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
1862 gs_prolog_key.gs_prolog.is_monolithic = true;
1863 gs_prolog_key.gs_prolog.as_ngg = shader->key.as_ngg;
1864 si_llvm_build_gs_prolog(&ctx, &gs_prolog_key);
1865 gs_prolog = ctx.main_fn;
1866
1867 /* ES main part */
1868 nir = get_nir_shader(es, &free_nir);
1869 struct si_shader shader_es = {};
1870 shader_es.selector = es;
1871 shader_es.key.as_es = 1;
1872 shader_es.key.as_ngg = shader->key.as_ngg;
1873 shader_es.key.mono = shader->key.mono;
1874 shader_es.key.opt = shader->key.opt;
1875 shader_es.is_monolithic = true;
1876
1877 if (!si_build_main_function(&ctx, &shader_es, nir, free_nir, false)) {
1878 si_llvm_dispose(&ctx);
1879 return false;
1880 }
1881 shader->info.uses_instanceid |= es->info.uses_instanceid;
1882 es_main = ctx.main_fn;
1883
1884 /* ES prolog */
1885 if (es->type == PIPE_SHADER_VERTEX &&
1886 si_vs_needs_prolog(es, &shader->key.part.gs.vs_prolog,
1887 &shader->key, false)) {
1888 union si_shader_part_key vs_prolog_key;
1889 si_get_vs_prolog_key(&es->info,
1890 shader_es.info.num_input_sgprs,
1891 false,
1892 &shader->key.part.gs.vs_prolog,
1893 shader, &vs_prolog_key);
1894 vs_prolog_key.vs_prolog.is_monolithic = true;
1895 si_llvm_build_vs_prolog(&ctx, &vs_prolog_key);
1896 es_prolog = ctx.main_fn;
1897 }
1898
1899 /* Reset the shader context. */
1900 ctx.shader = shader;
1901 ctx.type = PIPE_SHADER_GEOMETRY;
1902
1903 /* Prepare the array of shader parts. */
1904 LLVMValueRef parts[4];
1905 unsigned num_parts = 0, main_part, next_first_part;
1906
1907 if (es_prolog)
1908 parts[num_parts++] = es_prolog;
1909
1910 parts[main_part = num_parts++] = es_main;
1911 parts[next_first_part = num_parts++] = gs_prolog;
1912 parts[num_parts++] = gs_main;
1913
1914 si_build_wrapper_function(&ctx, parts, num_parts,
1915 main_part, next_first_part);
1916 } else {
1917 LLVMValueRef parts[2];
1918 union si_shader_part_key prolog_key;
1919
1920 parts[1] = ctx.main_fn;
1921
1922 memset(&prolog_key, 0, sizeof(prolog_key));
1923 prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
1924 si_llvm_build_gs_prolog(&ctx, &prolog_key);
1925 parts[0] = ctx.main_fn;
1926
1927 si_build_wrapper_function(&ctx, parts, 2, 1, 0);
1928 }
1929 } else if (shader->is_monolithic && ctx.type == PIPE_SHADER_FRAGMENT) {
1930 si_llvm_build_monolithic_ps(&ctx, shader);
1931 }
1932
1933 si_llvm_optimize_module(&ctx);
1934
1935 /* Post-optimization transformations and analysis. */
1936 si_optimize_vs_outputs(&ctx);
1937
1938 if ((debug && debug->debug_message) ||
1939 si_can_dump_shader(sscreen, ctx.type)) {
1940 ctx.shader->info.private_mem_vgprs =
1941 ac_count_scratch_private_memory(ctx.main_fn);
1942 }
1943
1944 /* Make sure the input is a pointer and not integer followed by inttoptr. */
1945 assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(ctx.main_fn, 0))) ==
1946 LLVMPointerTypeKind);
1947
1948 /* Compile to bytecode. */
1949 if (!si_compile_llvm(sscreen, &shader->binary, &shader->config, compiler,
1950 &ctx.ac, debug, ctx.type, si_get_shader_name(shader),
1951 si_should_optimize_less(compiler, shader->selector))) {
1952 si_llvm_dispose(&ctx);
1953 fprintf(stderr, "LLVM failed to compile shader\n");
1954 return false;
1955 }
1956
1957 si_llvm_dispose(&ctx);
1958 return true;
1959 }
1960
1961 bool si_compile_shader(struct si_screen *sscreen,
1962 struct ac_llvm_compiler *compiler,
1963 struct si_shader *shader,
1964 struct pipe_debug_callback *debug)
1965 {
1966 struct si_shader_selector *sel = shader->selector;
1967 bool free_nir;
1968 struct nir_shader *nir = get_nir_shader(sel, &free_nir);
1969
1970 /* Dump NIR before doing NIR->LLVM conversion in case the
1971 * conversion fails. */
1972 if (si_can_dump_shader(sscreen, sel->type) &&
1973 !(sscreen->debug_flags & DBG(NO_NIR))) {
1974 nir_print_shader(nir, stderr);
1975 si_dump_streamout(&sel->so);
1976 }
1977
1978 memset(shader->info.vs_output_param_offset, AC_EXP_PARAM_UNDEFINED,
1979 sizeof(shader->info.vs_output_param_offset));
1980
1981 shader->info.uses_instanceid = sel->info.uses_instanceid;
1982
1983 /* TODO: ACO could compile non-monolithic shaders here (starting
1984 * with PS and NGG VS), but monolithic shaders should be compiled
1985 * by LLVM due to more complicated compilation.
1986 */
1987 if (!si_llvm_compile_shader(sscreen, compiler, shader, debug, nir, free_nir))
1988 return false;
1989
1990 /* Validate SGPR and VGPR usage for compute to detect compiler bugs.
1991 * LLVM 3.9svn has this bug.
1992 */
1993 if (sel->type == PIPE_SHADER_COMPUTE) {
1994 unsigned wave_size = sscreen->compute_wave_size;
1995 unsigned max_vgprs = sscreen->info.num_physical_wave64_vgprs_per_simd *
1996 (wave_size == 32 ? 2 : 1);
1997 unsigned max_sgprs = sscreen->info.num_physical_sgprs_per_simd;
1998 unsigned max_sgprs_per_wave = 128;
1999 unsigned simds_per_tg = 4; /* assuming WGP mode on gfx10 */
2000 unsigned threads_per_tg = si_get_max_workgroup_size(shader);
2001 unsigned waves_per_tg = DIV_ROUND_UP(threads_per_tg, wave_size);
2002 unsigned waves_per_simd = DIV_ROUND_UP(waves_per_tg, simds_per_tg);
2003
2004 max_vgprs = max_vgprs / waves_per_simd;
2005 max_sgprs = MIN2(max_sgprs / waves_per_simd, max_sgprs_per_wave);
2006
2007 if (shader->config.num_sgprs > max_sgprs ||
2008 shader->config.num_vgprs > max_vgprs) {
2009 fprintf(stderr, "LLVM failed to compile a shader correctly: "
2010 "SGPR:VGPR usage is %u:%u, but the hw limit is %u:%u\n",
2011 shader->config.num_sgprs, shader->config.num_vgprs,
2012 max_sgprs, max_vgprs);
2013
2014 /* Just terminate the process, because dependent
2015 * shaders can hang due to bad input data, but use
2016 * the env var to allow shader-db to work.
2017 */
2018 if (!debug_get_bool_option("SI_PASS_BAD_SHADERS", false))
2019 abort();
2020 }
2021 }
2022
2023 /* Add the scratch offset to input SGPRs. */
2024 if (shader->config.scratch_bytes_per_wave && !si_is_merged_shader(shader))
2025 shader->info.num_input_sgprs += 1; /* scratch byte offset */
2026
2027 /* Calculate the number of fragment input VGPRs. */
2028 if (sel->type == PIPE_SHADER_FRAGMENT) {
2029 shader->info.num_input_vgprs = ac_get_fs_input_vgpr_cnt(&shader->config,
2030 &shader->info.face_vgpr_index,
2031 &shader->info.ancillary_vgpr_index);
2032 }
2033
2034 si_calculate_max_simd_waves(shader);
2035 si_shader_dump_stats_for_shader_db(sscreen, shader, debug);
2036 return true;
2037 }
2038
2039 /**
2040 * Create, compile and return a shader part (prolog or epilog).
2041 *
2042 * \param sscreen screen
2043 * \param list list of shader parts of the same category
2044 * \param type shader type
2045 * \param key shader part key
2046 * \param prolog whether the part being requested is a prolog
2047 * \param tm LLVM target machine
2048 * \param debug debug callback
2049 * \param build the callback responsible for building the main function
2050 * \return non-NULL on success
2051 */
2052 static struct si_shader_part *
2053 si_get_shader_part(struct si_screen *sscreen,
2054 struct si_shader_part **list,
2055 enum pipe_shader_type type,
2056 bool prolog,
2057 union si_shader_part_key *key,
2058 struct ac_llvm_compiler *compiler,
2059 struct pipe_debug_callback *debug,
2060 void (*build)(struct si_shader_context *,
2061 union si_shader_part_key *),
2062 const char *name)
2063 {
2064 struct si_shader_part *result;
2065
2066 simple_mtx_lock(&sscreen->shader_parts_mutex);
2067
2068 /* Find existing. */
2069 for (result = *list; result; result = result->next) {
2070 if (memcmp(&result->key, key, sizeof(*key)) == 0) {
2071 simple_mtx_unlock(&sscreen->shader_parts_mutex);
2072 return result;
2073 }
2074 }
2075
2076 /* Compile a new one. */
2077 result = CALLOC_STRUCT(si_shader_part);
2078 result->key = *key;
2079
2080 struct si_shader_selector sel = {};
2081 sel.screen = sscreen;
2082
2083 struct si_shader shader = {};
2084 shader.selector = &sel;
2085
2086 switch (type) {
2087 case PIPE_SHADER_VERTEX:
2088 shader.key.as_ls = key->vs_prolog.as_ls;
2089 shader.key.as_es = key->vs_prolog.as_es;
2090 shader.key.as_ngg = key->vs_prolog.as_ngg;
2091 break;
2092 case PIPE_SHADER_TESS_CTRL:
2093 assert(!prolog);
2094 shader.key.part.tcs.epilog = key->tcs_epilog.states;
2095 break;
2096 case PIPE_SHADER_GEOMETRY:
2097 assert(prolog);
2098 shader.key.as_ngg = key->gs_prolog.as_ngg;
2099 break;
2100 case PIPE_SHADER_FRAGMENT:
2101 if (prolog)
2102 shader.key.part.ps.prolog = key->ps_prolog.states;
2103 else
2104 shader.key.part.ps.epilog = key->ps_epilog.states;
2105 break;
2106 default:
2107 unreachable("bad shader part");
2108 }
2109
2110 struct si_shader_context ctx;
2111 si_llvm_context_init(&ctx, sscreen, compiler,
2112 si_get_wave_size(sscreen, type, shader.key.as_ngg,
2113 shader.key.as_es));
2114 ctx.shader = &shader;
2115 ctx.type = type;
2116
2117 build(&ctx, key);
2118
2119 /* Compile. */
2120 si_llvm_optimize_module(&ctx);
2121
2122 if (!si_compile_llvm(sscreen, &result->binary, &result->config, compiler,
2123 &ctx.ac, debug, ctx.type, name, false)) {
2124 FREE(result);
2125 result = NULL;
2126 goto out;
2127 }
2128
2129 result->next = *list;
2130 *list = result;
2131
2132 out:
2133 si_llvm_dispose(&ctx);
2134 simple_mtx_unlock(&sscreen->shader_parts_mutex);
2135 return result;
2136 }
2137
2138 static bool si_get_vs_prolog(struct si_screen *sscreen,
2139 struct ac_llvm_compiler *compiler,
2140 struct si_shader *shader,
2141 struct pipe_debug_callback *debug,
2142 struct si_shader *main_part,
2143 const struct si_vs_prolog_bits *key)
2144 {
2145 struct si_shader_selector *vs = main_part->selector;
2146
2147 if (!si_vs_needs_prolog(vs, key, &shader->key, false))
2148 return true;
2149
2150 /* Get the prolog. */
2151 union si_shader_part_key prolog_key;
2152 si_get_vs_prolog_key(&vs->info, main_part->info.num_input_sgprs, false,
2153 key, shader, &prolog_key);
2154
2155 shader->prolog =
2156 si_get_shader_part(sscreen, &sscreen->vs_prologs,
2157 PIPE_SHADER_VERTEX, true, &prolog_key, compiler,
2158 debug, si_llvm_build_vs_prolog,
2159 "Vertex Shader Prolog");
2160 return shader->prolog != NULL;
2161 }
2162
2163 /**
2164 * Select and compile (or reuse) vertex shader parts (prolog & epilog).
2165 */
2166 static bool si_shader_select_vs_parts(struct si_screen *sscreen,
2167 struct ac_llvm_compiler *compiler,
2168 struct si_shader *shader,
2169 struct pipe_debug_callback *debug)
2170 {
2171 return si_get_vs_prolog(sscreen, compiler, shader, debug, shader,
2172 &shader->key.part.vs.prolog);
2173 }
2174
2175 /**
2176 * Select and compile (or reuse) TCS parts (epilog).
2177 */
2178 static bool si_shader_select_tcs_parts(struct si_screen *sscreen,
2179 struct ac_llvm_compiler *compiler,
2180 struct si_shader *shader,
2181 struct pipe_debug_callback *debug)
2182 {
2183 if (sscreen->info.chip_class >= GFX9) {
2184 struct si_shader *ls_main_part =
2185 shader->key.part.tcs.ls->main_shader_part_ls;
2186
2187 if (!si_get_vs_prolog(sscreen, compiler, shader, debug, ls_main_part,
2188 &shader->key.part.tcs.ls_prolog))
2189 return false;
2190
2191 shader->previous_stage = ls_main_part;
2192 }
2193
2194 /* Get the epilog. */
2195 union si_shader_part_key epilog_key;
2196 memset(&epilog_key, 0, sizeof(epilog_key));
2197 epilog_key.tcs_epilog.states = shader->key.part.tcs.epilog;
2198
2199 shader->epilog = si_get_shader_part(sscreen, &sscreen->tcs_epilogs,
2200 PIPE_SHADER_TESS_CTRL, false,
2201 &epilog_key, compiler, debug,
2202 si_llvm_build_tcs_epilog,
2203 "Tessellation Control Shader Epilog");
2204 return shader->epilog != NULL;
2205 }
2206
2207 /**
2208 * Select and compile (or reuse) GS parts (prolog).
2209 */
2210 static bool si_shader_select_gs_parts(struct si_screen *sscreen,
2211 struct ac_llvm_compiler *compiler,
2212 struct si_shader *shader,
2213 struct pipe_debug_callback *debug)
2214 {
2215 if (sscreen->info.chip_class >= GFX9) {
2216 struct si_shader *es_main_part;
2217 enum pipe_shader_type es_type = shader->key.part.gs.es->type;
2218
2219 if (shader->key.as_ngg)
2220 es_main_part = shader->key.part.gs.es->main_shader_part_ngg_es;
2221 else
2222 es_main_part = shader->key.part.gs.es->main_shader_part_es;
2223
2224 if (es_type == PIPE_SHADER_VERTEX &&
2225 !si_get_vs_prolog(sscreen, compiler, shader, debug, es_main_part,
2226 &shader->key.part.gs.vs_prolog))
2227 return false;
2228
2229 shader->previous_stage = es_main_part;
2230 }
2231
2232 if (!shader->key.part.gs.prolog.tri_strip_adj_fix)
2233 return true;
2234
2235 union si_shader_part_key prolog_key;
2236 memset(&prolog_key, 0, sizeof(prolog_key));
2237 prolog_key.gs_prolog.states = shader->key.part.gs.prolog;
2238 prolog_key.gs_prolog.as_ngg = shader->key.as_ngg;
2239
2240 shader->prolog2 = si_get_shader_part(sscreen, &sscreen->gs_prologs,
2241 PIPE_SHADER_GEOMETRY, true,
2242 &prolog_key, compiler, debug,
2243 si_llvm_build_gs_prolog,
2244 "Geometry Shader Prolog");
2245 return shader->prolog2 != NULL;
2246 }
2247
2248 /**
2249 * Compute the PS prolog key, which contains all the information needed to
2250 * build the PS prolog function, and set related bits in shader->config.
2251 */
2252 void si_get_ps_prolog_key(struct si_shader *shader,
2253 union si_shader_part_key *key,
2254 bool separate_prolog)
2255 {
2256 struct si_shader_info *info = &shader->selector->info;
2257
2258 memset(key, 0, sizeof(*key));
2259 key->ps_prolog.states = shader->key.part.ps.prolog;
2260 key->ps_prolog.colors_read = info->colors_read;
2261 key->ps_prolog.num_input_sgprs = shader->info.num_input_sgprs;
2262 key->ps_prolog.num_input_vgprs = shader->info.num_input_vgprs;
2263 key->ps_prolog.wqm = info->uses_derivatives &&
2264 (key->ps_prolog.colors_read ||
2265 key->ps_prolog.states.force_persp_sample_interp ||
2266 key->ps_prolog.states.force_linear_sample_interp ||
2267 key->ps_prolog.states.force_persp_center_interp ||
2268 key->ps_prolog.states.force_linear_center_interp ||
2269 key->ps_prolog.states.bc_optimize_for_persp ||
2270 key->ps_prolog.states.bc_optimize_for_linear);
2271 key->ps_prolog.ancillary_vgpr_index = shader->info.ancillary_vgpr_index;
2272
2273 if (info->colors_read) {
2274 unsigned *color = shader->selector->color_attr_index;
2275
2276 if (shader->key.part.ps.prolog.color_two_side) {
2277 /* BCOLORs are stored after the last input. */
2278 key->ps_prolog.num_interp_inputs = info->num_inputs;
2279 key->ps_prolog.face_vgpr_index = shader->info.face_vgpr_index;
2280 if (separate_prolog)
2281 shader->config.spi_ps_input_ena |= S_0286CC_FRONT_FACE_ENA(1);
2282 }
2283
2284 for (unsigned i = 0; i < 2; i++) {
2285 unsigned interp = info->input_interpolate[color[i]];
2286 unsigned location = info->input_interpolate_loc[color[i]];
2287
2288 if (!(info->colors_read & (0xf << i*4)))
2289 continue;
2290
2291 key->ps_prolog.color_attr_index[i] = color[i];
2292
2293 if (shader->key.part.ps.prolog.flatshade_colors &&
2294 interp == TGSI_INTERPOLATE_COLOR)
2295 interp = TGSI_INTERPOLATE_CONSTANT;
2296
2297 switch (interp) {
2298 case TGSI_INTERPOLATE_CONSTANT:
2299 key->ps_prolog.color_interp_vgpr_index[i] = -1;
2300 break;
2301 case TGSI_INTERPOLATE_PERSPECTIVE:
2302 case TGSI_INTERPOLATE_COLOR:
2303 /* Force the interpolation location for colors here. */
2304 if (shader->key.part.ps.prolog.force_persp_sample_interp)
2305 location = TGSI_INTERPOLATE_LOC_SAMPLE;
2306 if (shader->key.part.ps.prolog.force_persp_center_interp)
2307 location = TGSI_INTERPOLATE_LOC_CENTER;
2308
2309 switch (location) {
2310 case TGSI_INTERPOLATE_LOC_SAMPLE:
2311 key->ps_prolog.color_interp_vgpr_index[i] = 0;
2312 if (separate_prolog) {
2313 shader->config.spi_ps_input_ena |=
2314 S_0286CC_PERSP_SAMPLE_ENA(1);
2315 }
2316 break;
2317 case TGSI_INTERPOLATE_LOC_CENTER:
2318 key->ps_prolog.color_interp_vgpr_index[i] = 2;
2319 if (separate_prolog) {
2320 shader->config.spi_ps_input_ena |=
2321 S_0286CC_PERSP_CENTER_ENA(1);
2322 }
2323 break;
2324 case TGSI_INTERPOLATE_LOC_CENTROID:
2325 key->ps_prolog.color_interp_vgpr_index[i] = 4;
2326 if (separate_prolog) {
2327 shader->config.spi_ps_input_ena |=
2328 S_0286CC_PERSP_CENTROID_ENA(1);
2329 }
2330 break;
2331 default:
2332 assert(0);
2333 }
2334 break;
2335 case TGSI_INTERPOLATE_LINEAR:
2336 /* Force the interpolation location for colors here. */
2337 if (shader->key.part.ps.prolog.force_linear_sample_interp)
2338 location = TGSI_INTERPOLATE_LOC_SAMPLE;
2339 if (shader->key.part.ps.prolog.force_linear_center_interp)
2340 location = TGSI_INTERPOLATE_LOC_CENTER;
2341
2342 /* The VGPR assignment for non-monolithic shaders
2343 * works because InitialPSInputAddr is set on the
2344 * main shader and PERSP_PULL_MODEL is never used.
2345 */
2346 switch (location) {
2347 case TGSI_INTERPOLATE_LOC_SAMPLE:
2348 key->ps_prolog.color_interp_vgpr_index[i] =
2349 separate_prolog ? 6 : 9;
2350 if (separate_prolog) {
2351 shader->config.spi_ps_input_ena |=
2352 S_0286CC_LINEAR_SAMPLE_ENA(1);
2353 }
2354 break;
2355 case TGSI_INTERPOLATE_LOC_CENTER:
2356 key->ps_prolog.color_interp_vgpr_index[i] =
2357 separate_prolog ? 8 : 11;
2358 if (separate_prolog) {
2359 shader->config.spi_ps_input_ena |=
2360 S_0286CC_LINEAR_CENTER_ENA(1);
2361 }
2362 break;
2363 case TGSI_INTERPOLATE_LOC_CENTROID:
2364 key->ps_prolog.color_interp_vgpr_index[i] =
2365 separate_prolog ? 10 : 13;
2366 if (separate_prolog) {
2367 shader->config.spi_ps_input_ena |=
2368 S_0286CC_LINEAR_CENTROID_ENA(1);
2369 }
2370 break;
2371 default:
2372 assert(0);
2373 }
2374 break;
2375 default:
2376 assert(0);
2377 }
2378 }
2379 }
2380 }
2381
2382 /**
2383 * Check whether a PS prolog is required based on the key.
2384 */
2385 bool si_need_ps_prolog(const union si_shader_part_key *key)
2386 {
2387 return key->ps_prolog.colors_read ||
2388 key->ps_prolog.states.force_persp_sample_interp ||
2389 key->ps_prolog.states.force_linear_sample_interp ||
2390 key->ps_prolog.states.force_persp_center_interp ||
2391 key->ps_prolog.states.force_linear_center_interp ||
2392 key->ps_prolog.states.bc_optimize_for_persp ||
2393 key->ps_prolog.states.bc_optimize_for_linear ||
2394 key->ps_prolog.states.poly_stipple ||
2395 key->ps_prolog.states.samplemask_log_ps_iter;
2396 }
2397
2398 /**
2399 * Compute the PS epilog key, which contains all the information needed to
2400 * build the PS epilog function.
2401 */
2402 void si_get_ps_epilog_key(struct si_shader *shader,
2403 union si_shader_part_key *key)
2404 {
2405 struct si_shader_info *info = &shader->selector->info;
2406 memset(key, 0, sizeof(*key));
2407 key->ps_epilog.colors_written = info->colors_written;
2408 key->ps_epilog.writes_z = info->writes_z;
2409 key->ps_epilog.writes_stencil = info->writes_stencil;
2410 key->ps_epilog.writes_samplemask = info->writes_samplemask;
2411 key->ps_epilog.states = shader->key.part.ps.epilog;
2412 }
2413
2414 /**
2415 * Select and compile (or reuse) pixel shader parts (prolog & epilog).
2416 */
2417 static bool si_shader_select_ps_parts(struct si_screen *sscreen,
2418 struct ac_llvm_compiler *compiler,
2419 struct si_shader *shader,
2420 struct pipe_debug_callback *debug)
2421 {
2422 union si_shader_part_key prolog_key;
2423 union si_shader_part_key epilog_key;
2424
2425 /* Get the prolog. */
2426 si_get_ps_prolog_key(shader, &prolog_key, true);
2427
2428 /* The prolog is a no-op if these aren't set. */
2429 if (si_need_ps_prolog(&prolog_key)) {
2430 shader->prolog =
2431 si_get_shader_part(sscreen, &sscreen->ps_prologs,
2432 PIPE_SHADER_FRAGMENT, true,
2433 &prolog_key, compiler, debug,
2434 si_llvm_build_ps_prolog,
2435 "Fragment Shader Prolog");
2436 if (!shader->prolog)
2437 return false;
2438 }
2439
2440 /* Get the epilog. */
2441 si_get_ps_epilog_key(shader, &epilog_key);
2442
2443 shader->epilog =
2444 si_get_shader_part(sscreen, &sscreen->ps_epilogs,
2445 PIPE_SHADER_FRAGMENT, false,
2446 &epilog_key, compiler, debug,
2447 si_llvm_build_ps_epilog,
2448 "Fragment Shader Epilog");
2449 if (!shader->epilog)
2450 return false;
2451
2452 /* Enable POS_FIXED_PT if polygon stippling is enabled. */
2453 if (shader->key.part.ps.prolog.poly_stipple) {
2454 shader->config.spi_ps_input_ena |= S_0286CC_POS_FIXED_PT_ENA(1);
2455 assert(G_0286CC_POS_FIXED_PT_ENA(shader->config.spi_ps_input_addr));
2456 }
2457
2458 /* Set up the enable bits for per-sample shading if needed. */
2459 if (shader->key.part.ps.prolog.force_persp_sample_interp &&
2460 (G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_ena) ||
2461 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
2462 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTER_ENA;
2463 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
2464 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_SAMPLE_ENA(1);
2465 }
2466 if (shader->key.part.ps.prolog.force_linear_sample_interp &&
2467 (G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_ena) ||
2468 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
2469 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTER_ENA;
2470 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
2471 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_SAMPLE_ENA(1);
2472 }
2473 if (shader->key.part.ps.prolog.force_persp_center_interp &&
2474 (G_0286CC_PERSP_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
2475 G_0286CC_PERSP_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
2476 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_SAMPLE_ENA;
2477 shader->config.spi_ps_input_ena &= C_0286CC_PERSP_CENTROID_ENA;
2478 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
2479 }
2480 if (shader->key.part.ps.prolog.force_linear_center_interp &&
2481 (G_0286CC_LINEAR_SAMPLE_ENA(shader->config.spi_ps_input_ena) ||
2482 G_0286CC_LINEAR_CENTROID_ENA(shader->config.spi_ps_input_ena))) {
2483 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_SAMPLE_ENA;
2484 shader->config.spi_ps_input_ena &= C_0286CC_LINEAR_CENTROID_ENA;
2485 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
2486 }
2487
2488 /* POW_W_FLOAT requires that one of the perspective weights is enabled. */
2489 if (G_0286CC_POS_W_FLOAT_ENA(shader->config.spi_ps_input_ena) &&
2490 !(shader->config.spi_ps_input_ena & 0xf)) {
2491 shader->config.spi_ps_input_ena |= S_0286CC_PERSP_CENTER_ENA(1);
2492 assert(G_0286CC_PERSP_CENTER_ENA(shader->config.spi_ps_input_addr));
2493 }
2494
2495 /* At least one pair of interpolation weights must be enabled. */
2496 if (!(shader->config.spi_ps_input_ena & 0x7f)) {
2497 shader->config.spi_ps_input_ena |= S_0286CC_LINEAR_CENTER_ENA(1);
2498 assert(G_0286CC_LINEAR_CENTER_ENA(shader->config.spi_ps_input_addr));
2499 }
2500
2501 /* Samplemask fixup requires the sample ID. */
2502 if (shader->key.part.ps.prolog.samplemask_log_ps_iter) {
2503 shader->config.spi_ps_input_ena |= S_0286CC_ANCILLARY_ENA(1);
2504 assert(G_0286CC_ANCILLARY_ENA(shader->config.spi_ps_input_addr));
2505 }
2506
2507 /* The sample mask input is always enabled, because the API shader always
2508 * passes it through to the epilog. Disable it here if it's unused.
2509 */
2510 if (!shader->key.part.ps.epilog.poly_line_smoothing &&
2511 !shader->selector->info.reads_samplemask)
2512 shader->config.spi_ps_input_ena &= C_0286CC_SAMPLE_COVERAGE_ENA;
2513
2514 return true;
2515 }
2516
2517 void si_multiwave_lds_size_workaround(struct si_screen *sscreen,
2518 unsigned *lds_size)
2519 {
2520 /* If tessellation is all offchip and on-chip GS isn't used, this
2521 * workaround is not needed.
2522 */
2523 return;
2524
2525 /* SPI barrier management bug:
2526 * Make sure we have at least 4k of LDS in use to avoid the bug.
2527 * It applies to workgroup sizes of more than one wavefront.
2528 */
2529 if (sscreen->info.family == CHIP_BONAIRE ||
2530 sscreen->info.family == CHIP_KABINI)
2531 *lds_size = MAX2(*lds_size, 8);
2532 }
2533
2534 void si_fix_resource_usage(struct si_screen *sscreen, struct si_shader *shader)
2535 {
2536 unsigned min_sgprs = shader->info.num_input_sgprs + 2; /* VCC */
2537
2538 shader->config.num_sgprs = MAX2(shader->config.num_sgprs, min_sgprs);
2539
2540 if (shader->selector->type == PIPE_SHADER_COMPUTE &&
2541 si_get_max_workgroup_size(shader) > sscreen->compute_wave_size) {
2542 si_multiwave_lds_size_workaround(sscreen,
2543 &shader->config.lds_size);
2544 }
2545 }
2546
2547 bool si_create_shader_variant(struct si_screen *sscreen,
2548 struct ac_llvm_compiler *compiler,
2549 struct si_shader *shader,
2550 struct pipe_debug_callback *debug)
2551 {
2552 struct si_shader_selector *sel = shader->selector;
2553 struct si_shader *mainp = *si_get_main_shader_part(sel, &shader->key);
2554
2555 /* LS, ES, VS are compiled on demand if the main part hasn't been
2556 * compiled for that stage.
2557 *
2558 * GS are compiled on demand if the main part hasn't been compiled
2559 * for the chosen NGG-ness.
2560 *
2561 * Vertex shaders are compiled on demand when a vertex fetch
2562 * workaround must be applied.
2563 */
2564 if (shader->is_monolithic) {
2565 /* Monolithic shader (compiled as a whole, has many variants,
2566 * may take a long time to compile).
2567 */
2568 if (!si_compile_shader(sscreen, compiler, shader, debug))
2569 return false;
2570 } else {
2571 /* The shader consists of several parts:
2572 *
2573 * - the middle part is the user shader, it has 1 variant only
2574 * and it was compiled during the creation of the shader
2575 * selector
2576 * - the prolog part is inserted at the beginning
2577 * - the epilog part is inserted at the end
2578 *
2579 * The prolog and epilog have many (but simple) variants.
2580 *
2581 * Starting with gfx9, geometry and tessellation control
2582 * shaders also contain the prolog and user shader parts of
2583 * the previous shader stage.
2584 */
2585
2586 if (!mainp)
2587 return false;
2588
2589 /* Copy the compiled shader data over. */
2590 shader->is_binary_shared = true;
2591 shader->binary = mainp->binary;
2592 shader->config = mainp->config;
2593 shader->info.num_input_sgprs = mainp->info.num_input_sgprs;
2594 shader->info.num_input_vgprs = mainp->info.num_input_vgprs;
2595 shader->info.face_vgpr_index = mainp->info.face_vgpr_index;
2596 shader->info.ancillary_vgpr_index = mainp->info.ancillary_vgpr_index;
2597 memcpy(shader->info.vs_output_param_offset,
2598 mainp->info.vs_output_param_offset,
2599 sizeof(mainp->info.vs_output_param_offset));
2600 shader->info.uses_instanceid = mainp->info.uses_instanceid;
2601 shader->info.nr_pos_exports = mainp->info.nr_pos_exports;
2602 shader->info.nr_param_exports = mainp->info.nr_param_exports;
2603
2604 /* Select prologs and/or epilogs. */
2605 switch (sel->type) {
2606 case PIPE_SHADER_VERTEX:
2607 if (!si_shader_select_vs_parts(sscreen, compiler, shader, debug))
2608 return false;
2609 break;
2610 case PIPE_SHADER_TESS_CTRL:
2611 if (!si_shader_select_tcs_parts(sscreen, compiler, shader, debug))
2612 return false;
2613 break;
2614 case PIPE_SHADER_TESS_EVAL:
2615 break;
2616 case PIPE_SHADER_GEOMETRY:
2617 if (!si_shader_select_gs_parts(sscreen, compiler, shader, debug))
2618 return false;
2619 break;
2620 case PIPE_SHADER_FRAGMENT:
2621 if (!si_shader_select_ps_parts(sscreen, compiler, shader, debug))
2622 return false;
2623
2624 /* Make sure we have at least as many VGPRs as there
2625 * are allocated inputs.
2626 */
2627 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
2628 shader->info.num_input_vgprs);
2629 break;
2630 default:;
2631 }
2632
2633 /* Update SGPR and VGPR counts. */
2634 if (shader->prolog) {
2635 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
2636 shader->prolog->config.num_sgprs);
2637 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
2638 shader->prolog->config.num_vgprs);
2639 }
2640 if (shader->previous_stage) {
2641 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
2642 shader->previous_stage->config.num_sgprs);
2643 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
2644 shader->previous_stage->config.num_vgprs);
2645 shader->config.spilled_sgprs =
2646 MAX2(shader->config.spilled_sgprs,
2647 shader->previous_stage->config.spilled_sgprs);
2648 shader->config.spilled_vgprs =
2649 MAX2(shader->config.spilled_vgprs,
2650 shader->previous_stage->config.spilled_vgprs);
2651 shader->info.private_mem_vgprs =
2652 MAX2(shader->info.private_mem_vgprs,
2653 shader->previous_stage->info.private_mem_vgprs);
2654 shader->config.scratch_bytes_per_wave =
2655 MAX2(shader->config.scratch_bytes_per_wave,
2656 shader->previous_stage->config.scratch_bytes_per_wave);
2657 shader->info.uses_instanceid |=
2658 shader->previous_stage->info.uses_instanceid;
2659 }
2660 if (shader->prolog2) {
2661 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
2662 shader->prolog2->config.num_sgprs);
2663 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
2664 shader->prolog2->config.num_vgprs);
2665 }
2666 if (shader->epilog) {
2667 shader->config.num_sgprs = MAX2(shader->config.num_sgprs,
2668 shader->epilog->config.num_sgprs);
2669 shader->config.num_vgprs = MAX2(shader->config.num_vgprs,
2670 shader->epilog->config.num_vgprs);
2671 }
2672 si_calculate_max_simd_waves(shader);
2673 }
2674
2675 if (shader->key.as_ngg) {
2676 assert(!shader->key.as_es && !shader->key.as_ls);
2677 gfx10_ngg_calculate_subgroup_info(shader);
2678 } else if (sscreen->info.chip_class >= GFX9 && sel->type == PIPE_SHADER_GEOMETRY) {
2679 gfx9_get_gs_info(shader->previous_stage_sel, sel, &shader->gs_info);
2680 }
2681
2682 si_fix_resource_usage(sscreen, shader);
2683 si_shader_dump(sscreen, shader, debug, stderr, true);
2684
2685 /* Upload. */
2686 if (!si_shader_binary_upload(sscreen, shader, 0)) {
2687 fprintf(stderr, "LLVM failed to upload shader\n");
2688 return false;
2689 }
2690
2691 return true;
2692 }
2693
2694 void si_shader_binary_clean(struct si_shader_binary *binary)
2695 {
2696 free((void *)binary->elf_buffer);
2697 binary->elf_buffer = NULL;
2698
2699 free(binary->llvm_ir_string);
2700 binary->llvm_ir_string = NULL;
2701 }
2702
2703 void si_shader_destroy(struct si_shader *shader)
2704 {
2705 if (shader->scratch_bo)
2706 si_resource_reference(&shader->scratch_bo, NULL);
2707
2708 si_resource_reference(&shader->bo, NULL);
2709
2710 if (!shader->is_binary_shared)
2711 si_shader_binary_clean(&shader->binary);
2712
2713 free(shader->shader_log);
2714 }