1 /*
2 * Copyright © 2014 Rob Clark <robclark@freedesktop.org>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <robclark@freedesktop.org>
7 */
8
9 #include "util/format/u_format.h"
10 #include "util/u_atomic.h"
11 #include "util/u_math.h"
12 #include "util/u_memory.h"
13 #include "util/u_string.h"
14
15 #include "drm/freedreno_drmif.h"
16
17 #include "ir3_assembler.h"
18 #include "ir3_compiler.h"
19 #include "ir3_nir.h"
20 #include "ir3_parser.h"
21 #include "ir3_shader.h"
22
23 #include "freedreno/isa/ir3-isa.h"
24 #include "isa/isa.h"
25
26 #include "disasm.h"
27
28 static uint16_t
const_imm_index_to_reg(const struct ir3_const_state * const_state,unsigned i)29 const_imm_index_to_reg(const struct ir3_const_state *const_state, unsigned i)
30 {
31 return i + (4 * const_state->allocs.max_const_offset_vec4);
32 }
33
34 uint16_t
ir3_const_find_imm(struct ir3_shader_variant * v,uint32_t imm)35 ir3_const_find_imm(struct ir3_shader_variant *v, uint32_t imm)
36 {
37 const struct ir3_const_state *const_state = ir3_const_state(v);
38
39 for (unsigned i = 0; i < const_state->immediates_count; i++) {
40 if (const_state->immediates[i] == imm)
41 return const_imm_index_to_reg(const_state, i);
42 }
43
44 return INVALID_CONST_REG;
45 }
46
47 uint16_t
ir3_const_add_imm(struct ir3_shader_variant * v,uint32_t imm)48 ir3_const_add_imm(struct ir3_shader_variant *v, uint32_t imm)
49 {
50 struct ir3_const_state *const_state = ir3_const_state_mut(v);
51
52 /* Reallocate for 4 more elements whenever it's necessary. Note that ir3
53 * printing relies on having groups of 4 dwords, so we fill the unused
54 * slots with a dummy value.
55 */
56 if (const_state->immediates_count == const_state->immediates_size) {
57 const_state->immediates = rerzalloc(
58 const_state, const_state->immediates,
59 __typeof__(const_state->immediates[0]), const_state->immediates_size,
60 const_state->immediates_size + 4);
61 const_state->immediates_size += 4;
62
63 for (int i = const_state->immediates_count;
64 i < const_state->immediates_size; i++) {
65 const_state->immediates[i] = 0xd0d0d0d0;
66 }
67 }
68
69 /* Add on a new immediate to be pushed, if we have space left in the
70 * constbuf.
71 */
72 if (const_state->allocs.max_const_offset_vec4 +
73 const_state->immediates_count / 4 >=
74 ir3_max_const(v)) {
75 return INVALID_CONST_REG;
76 }
77
78 const_state->immediates[const_state->immediates_count] = imm;
79 return const_imm_index_to_reg(const_state, const_state->immediates_count++);
80 }
81
82 int
ir3_glsl_type_size(const struct glsl_type * type,bool bindless)83 ir3_glsl_type_size(const struct glsl_type *type, bool bindless)
84 {
85 return glsl_count_attribute_slots(type, false);
86 }
87
88 /* wrapper for ir3_assemble() which does some info fixup based on
89 * shader state. Non-static since used by ir3_cmdline too.
90 */
91 void *
ir3_shader_assemble(struct ir3_shader_variant * v)92 ir3_shader_assemble(struct ir3_shader_variant *v)
93 {
94 const struct ir3_compiler *compiler = v->compiler;
95 struct ir3_info *info = &v->info;
96 uint32_t *bin;
97
98 ir3_collect_info(v);
99
100 if (v->constant_data_size) {
101 /* Make sure that where we're about to place the constant_data is safe
102 * to indirectly upload from.
103 */
104 info->constant_data_offset =
105 align(info->size, v->compiler->const_upload_unit * 16);
106 info->size = info->constant_data_offset + v->constant_data_size;
107 }
108
109 /* Pad out the size so that when turnip uploads the shaders in
110 * sequence, the starting offset of the next one is properly aligned.
111 */
112 info->size = align(info->size, compiler->instr_align * sizeof(uint64_t));
113
114 bin = isa_assemble(v);
115 if (!bin)
116 return NULL;
117
118 /* Append the immediates after the end of the program. This lets us emit
119 * the immediates as an indirect load, while avoiding creating another BO.
120 */
121 if (v->constant_data_size)
122 memcpy(&bin[info->constant_data_offset / 4], v->constant_data,
123 v->constant_data_size);
124 ralloc_free(v->constant_data);
125 v->constant_data = NULL;
126
127 /* NOTE: if relative addressing is used, we set constlen in
128 * the compiler (to worst-case value) since we don't know in
129 * the assembler what the max addr reg value can be:
130 */
131 v->constlen = MAX2(v->constlen, info->max_const + 1);
132
133 const struct ir3_const_state *const_state = ir3_const_state(v);
134 if (ir3_const_can_upload(&const_state->allocs, IR3_CONST_ALLOC_DRIVER_PARAMS,
135 v->constlen) ||
136 (const_state->driver_params_ubo.idx >= 0))
137 v->need_driver_params = true;
138
139 /* On a4xx and newer, constlen must be a multiple of 16 dwords even though
140 * uploads are in units of 4 dwords. Round it up here to make calculations
141 * regarding the shared constlen simpler.
142 */
143 if (compiler->gen >= 4)
144 v->constlen = align(v->constlen, 4);
145
146 /* Use the per-wave layout by default on a6xx for compute shaders. It
147 * should result in better performance when loads/stores are to a uniform
148 * index.
149 */
150 v->pvtmem_per_wave = compiler->gen >= 6 && !info->multi_dword_ldp_stp &&
151 ((v->type == MESA_SHADER_COMPUTE) ||
152 (v->type == MESA_SHADER_KERNEL));
153
154 return bin;
155 }
156
157 static bool
try_override_shader_variant(struct ir3_shader_variant * v,const char * identifier)158 try_override_shader_variant(struct ir3_shader_variant *v,
159 const char *identifier)
160 {
161 assert(ir3_shader_override_path);
162
163 char *name =
164 ralloc_asprintf(NULL, "%s/%s.asm", ir3_shader_override_path, identifier);
165
166 FILE *f = fopen(name, "r");
167
168 if (!f) {
169 ralloc_free(name);
170 return false;
171 }
172
173 struct ir3_kernel_info info;
174 info.numwg = INVALID_REG;
175 v->ir = ir3_parse(v, &info, f);
176
177 fclose(f);
178
179 if (!v->ir) {
180 fprintf(stderr, "Failed to parse %s\n", name);
181 exit(1);
182 }
183
184 v->bin = ir3_shader_assemble(v);
185 if (!v->bin) {
186 fprintf(stderr, "Failed to assemble %s\n", name);
187 exit(1);
188 }
189
190 ralloc_free(name);
191 return true;
192 }
193
194 static void
assemble_variant(struct ir3_shader_variant * v,bool internal)195 assemble_variant(struct ir3_shader_variant *v, bool internal)
196 {
197 v->bin = ir3_shader_assemble(v);
198
199 bool dbg_enabled = shader_debug_enabled(v->type, internal);
200 if (dbg_enabled || ir3_shader_override_path || v->disasm_info.write_disasm) {
201 unsigned char sha1[21];
202 char sha1buf[41];
203
204 _mesa_sha1_compute(v->bin, v->info.size, sha1);
205 _mesa_sha1_format(sha1buf, sha1);
206
207 bool shader_overridden =
208 ir3_shader_override_path && try_override_shader_variant(v, sha1buf);
209
210 if (v->disasm_info.write_disasm) {
211 char *stream_data = NULL;
212 size_t stream_size = 0;
213 FILE *stream = open_memstream(&stream_data, &stream_size);
214
215 fprintf(stream,
216 "Native code%s for unnamed %s shader %s with sha1 %s:\n",
217 shader_overridden ? " (overridden)" : "", ir3_shader_stage(v),
218 v->name, sha1buf);
219 ir3_shader_disasm(v, v->bin, stream);
220
221 fclose(stream);
222
223 v->disasm_info.disasm = ralloc_size(v, stream_size + 1);
224 memcpy(v->disasm_info.disasm, stream_data, stream_size);
225 v->disasm_info.disasm[stream_size] = 0;
226 free(stream_data);
227 }
228
229 if (dbg_enabled || shader_overridden) {
230 char *stream_data = NULL;
231 size_t stream_size = 0;
232 FILE *stream = open_memstream(&stream_data, &stream_size);
233
234 fprintf(stream,
235 "Native code%s for unnamed %s shader %s with sha1 %s:\n",
236 shader_overridden ? " (overridden)" : "", ir3_shader_stage(v),
237 v->name, sha1buf);
238 if (v->type == MESA_SHADER_FRAGMENT)
239 fprintf(stream, "SIMD0\n");
240 ir3_shader_disasm(v, v->bin, stream);
241 fclose(stream);
242
243 mesa_log_multiline(MESA_LOG_INFO, stream_data);
244 free(stream_data);
245 }
246 }
247
248 /* no need to keep the ir around beyond this point: */
249 ir3_destroy(v->ir);
250 v->ir = NULL;
251 }
252
253 static bool
compile_variant(struct ir3_shader * shader,struct ir3_shader_variant * v)254 compile_variant(struct ir3_shader *shader, struct ir3_shader_variant *v)
255 {
256 int ret = ir3_compile_shader_nir(shader->compiler, shader, v);
257 if (ret) {
258 mesa_loge("compile failed! (%s:%s)", shader->nir->info.name,
259 shader->nir->info.label);
260 return false;
261 }
262
263 assemble_variant(v, shader->nir->info.internal);
264 if (!v->bin) {
265 mesa_loge("assemble failed! (%s:%s)", shader->nir->info.name,
266 shader->nir->info.label);
267 return false;
268 }
269
270 return true;
271 }
272
273 /*
274 * For creating normal shader variants, 'nonbinning' is NULL. For
275 * creating binning pass shader, it is link to corresponding normal
276 * (non-binning) variant.
277 */
278 static struct ir3_shader_variant *
alloc_variant(struct ir3_shader * shader,const struct ir3_shader_key * key,struct ir3_shader_variant * nonbinning,void * mem_ctx)279 alloc_variant(struct ir3_shader *shader, const struct ir3_shader_key *key,
280 struct ir3_shader_variant *nonbinning, void *mem_ctx)
281 {
282 /* hang the binning variant off it's non-binning counterpart instead
283 * of the shader, to simplify the error cleanup paths
284 */
285 if (nonbinning)
286 mem_ctx = nonbinning;
287 struct ir3_shader_variant *v = rzalloc_size(mem_ctx, sizeof(*v));
288
289 if (!v)
290 return NULL;
291
292 v->id = ++shader->variant_count;
293 v->shader_id = shader->id;
294 v->binning_pass = !!nonbinning;
295 v->nonbinning = nonbinning;
296 v->key = *key;
297 v->type = shader->type;
298 v->compiler = shader->compiler;
299 v->mergedregs = shader->compiler->gen >= 6;
300 v->stream_output = shader->stream_output;
301
302 v->name = ralloc_strdup(v, shader->nir->info.name);
303
304 struct shader_info *info = &shader->nir->info;
305 switch (v->type) {
306 case MESA_SHADER_TESS_CTRL:
307 case MESA_SHADER_TESS_EVAL:
308 v->tess.primitive_mode = info->tess._primitive_mode;
309 v->tess.tcs_vertices_out = info->tess.tcs_vertices_out;
310 v->tess.spacing = info->tess.spacing;
311 v->tess.ccw = info->tess.ccw;
312 v->tess.point_mode = info->tess.point_mode;
313 break;
314
315 case MESA_SHADER_GEOMETRY:
316 v->gs.output_primitive = info->gs.output_primitive;
317 v->gs.vertices_out = info->gs.vertices_out;
318 v->gs.invocations = info->gs.invocations;
319 v->gs.vertices_in = info->gs.vertices_in;
320 break;
321
322 case MESA_SHADER_FRAGMENT:
323 v->fs.early_fragment_tests = info->fs.early_fragment_tests;
324 v->fs.color_is_dual_source = info->fs.color_is_dual_source;
325 v->fs.uses_fbfetch_output = info->fs.uses_fbfetch_output;
326 v->fs.fbfetch_coherent = info->fs.fbfetch_coherent;
327 break;
328
329 case MESA_SHADER_COMPUTE:
330 case MESA_SHADER_KERNEL:
331 v->cs.req_input_mem = shader->cs.req_input_mem;
332 v->cs.req_local_mem = shader->cs.req_local_mem;
333 break;
334
335 default:
336 break;
337 }
338
339 v->num_ssbos = info->num_ssbos;
340 v->num_ibos = info->num_ssbos + info->num_images;
341 v->shader_options = shader->options;
342
343 if (!v->binning_pass) {
344 v->const_state = rzalloc_size(v, sizeof(*v->const_state));
345 v->const_state->allocs = shader->options.const_allocs;
346 v->const_state->push_consts_type = shader->options.push_consts_type;
347 v->const_state->consts_ubo.idx = -1;
348 v->const_state->driver_params_ubo.idx = -1;
349 v->const_state->primitive_map_ubo.idx = -1;
350 v->const_state->primitive_param_ubo.idx = -1;
351 }
352
353 return v;
354 }
355
356 static bool
needs_binning_variant(struct ir3_shader_variant * v)357 needs_binning_variant(struct ir3_shader_variant *v)
358 {
359 if ((v->type == MESA_SHADER_VERTEX) && ir3_has_binning_vs(&v->key))
360 return true;
361 return false;
362 }
363
364 static struct ir3_shader_variant *
create_variant(struct ir3_shader * shader,const struct ir3_shader_key * key,bool write_disasm,void * mem_ctx)365 create_variant(struct ir3_shader *shader, const struct ir3_shader_key *key,
366 bool write_disasm, void *mem_ctx)
367 {
368 struct ir3_shader_variant *v = alloc_variant(shader, key, NULL, mem_ctx);
369
370 if (!v)
371 goto fail;
372
373 v->disasm_info.write_disasm = write_disasm;
374
375 if (needs_binning_variant(v)) {
376 v->binning = alloc_variant(shader, key, v, mem_ctx);
377 if (!v->binning)
378 goto fail;
379 v->binning->disasm_info.write_disasm = write_disasm;
380 }
381
382 if (ir3_disk_cache_retrieve(shader, v))
383 return v;
384
385 if (!shader->nir_finalized) {
386 ir3_nir_post_finalize(shader);
387
388 if (ir3_shader_debug & IR3_DBG_DISASM) {
389 mesa_logi("dump nir%d: type=%d", shader->id, shader->type);
390 nir_log_shaderi(shader->nir);
391 }
392
393 if (v->disasm_info.write_disasm) {
394 v->disasm_info.nir = nir_shader_as_str(shader->nir, v);
395 }
396
397 shader->nir_finalized = true;
398 }
399
400 if (v->type == MESA_SHADER_COMPUTE ||
401 v->type == MESA_SHADER_KERNEL) {
402 v->cs.force_linear_dispatch = shader->cs.force_linear_dispatch;
403 }
404
405 struct ir3_const_state *const_state = ir3_const_state_mut(v);
406 const_state->num_app_ubos = MAX2(1, shader->nir->info.num_ubos);
407
408 if (!compile_variant(shader, v))
409 goto fail;
410
411 if (needs_binning_variant(v) && !compile_variant(shader, v->binning))
412 goto fail;
413
414 ir3_disk_cache_store(shader, v);
415
416 return v;
417
418 fail:
419 ralloc_free(v);
420 return NULL;
421 }
422
423 struct ir3_shader_variant *
ir3_shader_create_variant(struct ir3_shader * shader,const struct ir3_shader_key * key,bool keep_ir)424 ir3_shader_create_variant(struct ir3_shader *shader,
425 const struct ir3_shader_key *key,
426 bool keep_ir)
427 {
428 return create_variant(shader, key, keep_ir, NULL);
429 }
430
431 static inline struct ir3_shader_variant *
shader_variant(struct ir3_shader * shader,const struct ir3_shader_key * key)432 shader_variant(struct ir3_shader *shader, const struct ir3_shader_key *key)
433 {
434 struct ir3_shader_variant *v;
435
436 for (v = shader->variants; v; v = v->next)
437 if (ir3_shader_key_equal(key, &v->key))
438 return v;
439
440 return NULL;
441 }
442
443 struct ir3_shader_variant *
ir3_shader_get_variant(struct ir3_shader * shader,const struct ir3_shader_key * key,bool binning_pass,bool write_disasm,bool * created)444 ir3_shader_get_variant(struct ir3_shader *shader,
445 const struct ir3_shader_key *key, bool binning_pass,
446 bool write_disasm, bool *created)
447 {
448 MESA_TRACE_FUNC();
449
450 mtx_lock(&shader->variants_lock);
451 struct ir3_shader_variant *v = shader_variant(shader, key);
452
453 if (!v) {
454 /* compile new variant if it doesn't exist already: */
455 v = create_variant(shader, key, write_disasm, shader);
456 if (v) {
457 v->next = shader->variants;
458 shader->variants = v;
459 *created = true;
460 }
461 }
462
463 if (v && binning_pass) {
464 v = v->binning;
465 assert(v);
466 }
467
468 mtx_unlock(&shader->variants_lock);
469
470 return v;
471 }
472
473 struct ir3_shader *
ir3_shader_passthrough_tcs(struct ir3_shader * vs,unsigned patch_vertices)474 ir3_shader_passthrough_tcs(struct ir3_shader *vs, unsigned patch_vertices)
475 {
476 assert(vs->type == MESA_SHADER_VERTEX);
477 assert(patch_vertices > 0);
478 assert(patch_vertices <= 32);
479
480 unsigned n = patch_vertices - 1;
481 if (!vs->vs.passthrough_tcs[n]) {
482 const nir_shader_compiler_options *options =
483 ir3_get_compiler_options(vs->compiler);
484 nir_shader *tcs =
485 nir_create_passthrough_tcs(options, vs->nir, patch_vertices);
486
487 /* Technically it is an internal shader but it is confusing to
488 * not have it show up in debug output
489 */
490 tcs->info.internal = false;
491
492 nir_assign_io_var_locations(tcs, nir_var_shader_in,
493 &tcs->num_inputs,
494 tcs->info.stage);
495
496 nir_assign_io_var_locations(tcs, nir_var_shader_out,
497 &tcs->num_outputs,
498 tcs->info.stage);
499
500 NIR_PASS_V(tcs, nir_lower_system_values);
501
502 nir_shader_gather_info(tcs, nir_shader_get_entrypoint(tcs));
503
504 struct ir3_shader_options ir3_options = {};
505
506 ir3_finalize_nir(vs->compiler, &ir3_options.nir_options, tcs);
507
508 vs->vs.passthrough_tcs[n] =
509 ir3_shader_from_nir(vs->compiler, tcs, &ir3_options, NULL);
510
511 vs->vs.passthrough_tcs_compiled |= BITFIELD_BIT(n);
512 }
513
514 return vs->vs.passthrough_tcs[n];
515 }
516
517 void
ir3_shader_destroy(struct ir3_shader * shader)518 ir3_shader_destroy(struct ir3_shader *shader)
519 {
520 if (shader->type == MESA_SHADER_VERTEX) {
521 u_foreach_bit (b, shader->vs.passthrough_tcs_compiled) {
522 ir3_shader_destroy(shader->vs.passthrough_tcs[b]);
523 }
524 }
525 ralloc_free(shader->nir);
526 mtx_destroy(&shader->variants_lock);
527 ralloc_free(shader);
528 }
529
530 /**
531 * Creates a bitmask of the used bits of the shader key by this particular
532 * shader. Used by the gallium driver to skip state-dependent recompiles when
533 * possible.
534 */
535 static void
ir3_setup_used_key(struct ir3_shader * shader)536 ir3_setup_used_key(struct ir3_shader *shader)
537 {
538 nir_shader *nir = shader->nir;
539 struct shader_info *info = &nir->info;
540 struct ir3_shader_key *key = &shader->key_mask;
541
542 /* This key flag is just used to make for a cheaper ir3_shader_key_equal
543 * check in the common case.
544 */
545 key->has_per_samp = true;
546
547 key->safe_constlen = true;
548
549 /* When clip/cull distances are natively supported, we only use
550 * ucp_enables to determine whether to lower legacy clip planes to
551 * gl_ClipDistance.
552 */
553 if (info->stage != MESA_SHADER_COMPUTE && (info->stage != MESA_SHADER_FRAGMENT || !shader->compiler->has_clip_cull))
554 key->ucp_enables = 0xff;
555
556 if (info->stage == MESA_SHADER_FRAGMENT) {
557 key->fastc_srgb = ~0;
558 key->fsamples = ~0;
559 memset(key->fsampler_swizzles, 0xff, sizeof(key->fsampler_swizzles));
560
561 if (info->inputs_read & VARYING_BITS_COLOR) {
562 key->rasterflat = true;
563 }
564
565 /* Only used for deciding on behavior of
566 * nir_intrinsic_load_barycentric_sample and the centroid demotion
567 * on older HW.
568 */
569 key->msaa = shader->compiler->gen < 6 &&
570 (info->fs.uses_sample_qualifier ||
571 (BITSET_TEST(info->system_values_read,
572 SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID) ||
573 BITSET_TEST(info->system_values_read,
574 SYSTEM_VALUE_BARYCENTRIC_LINEAR_CENTROID)));
575
576 /* Only enable this shader key bit if "dual_color_blend_by_location" is
577 * enabled:
578 */
579 key->force_dual_color_blend = shader->compiler->options.dual_color_blend_by_location;
580 } else if (info->stage == MESA_SHADER_COMPUTE) {
581 key->fastc_srgb = ~0;
582 key->fsamples = ~0;
583 memset(key->fsampler_swizzles, 0xff, sizeof(key->fsampler_swizzles));
584 } else {
585 key->tessellation = ~0;
586 key->has_gs = true;
587
588 if (info->stage == MESA_SHADER_VERTEX) {
589 key->vastc_srgb = ~0;
590 key->vsamples = ~0;
591 memset(key->vsampler_swizzles, 0xff, sizeof(key->vsampler_swizzles));
592 }
593
594 if (info->stage == MESA_SHADER_TESS_CTRL)
595 key->tcs_store_primid = true;
596 }
597 }
598
599 /* Given an array of constlen's, decrease some of them so that the sum stays
600 * within "combined_limit" while trying to fairly share the reduction. Returns
601 * a bitfield of which stages should be trimmed.
602 */
603 static uint32_t
trim_constlens(unsigned * constlens,unsigned first_stage,unsigned last_stage,unsigned combined_limit,unsigned safe_limit)604 trim_constlens(unsigned *constlens, unsigned first_stage, unsigned last_stage,
605 unsigned combined_limit, unsigned safe_limit)
606 {
607 unsigned cur_total = 0;
608 for (unsigned i = first_stage; i <= last_stage; i++) {
609 cur_total += constlens[i];
610 }
611
612 unsigned max_stage = 0;
613 unsigned max_const = 0;
614 uint32_t trimmed = 0;
615
616 while (cur_total > combined_limit) {
617 for (unsigned i = first_stage; i <= last_stage; i++) {
618 if (constlens[i] >= max_const) {
619 max_stage = i;
620 max_const = constlens[i];
621 }
622 }
623
624 assert(max_const > safe_limit);
625 trimmed |= 1 << max_stage;
626 cur_total = cur_total - max_const + safe_limit;
627 constlens[max_stage] = safe_limit;
628 }
629
630 return trimmed;
631 }
632
633 /* Figures out which stages in the pipeline to use the "safe" constlen for, in
634 * order to satisfy all shared constlen limits.
635 */
636 uint32_t
ir3_trim_constlen(const struct ir3_shader_variant ** variants,const struct ir3_compiler * compiler)637 ir3_trim_constlen(const struct ir3_shader_variant **variants,
638 const struct ir3_compiler *compiler)
639 {
640 unsigned constlens[MESA_SHADER_STAGES] = {};
641
642 bool shared_consts_enable = false;
643
644 for (unsigned i = 0; i < MESA_SHADER_STAGES; i++) {
645 if (variants[i]) {
646 constlens[i] = variants[i]->constlen;
647 shared_consts_enable =
648 ir3_const_state(variants[i])->push_consts_type == IR3_PUSH_CONSTS_SHARED;
649 }
650 }
651
652 uint32_t trimmed = 0;
653 STATIC_ASSERT(MESA_SHADER_STAGES <= 8 * sizeof(trimmed));
654
655 /* Use a hw quirk for geometry shared consts, not matched with actual
656 * shared consts size (on a6xx).
657 */
658 uint32_t shared_consts_size_geom = shared_consts_enable ?
659 compiler->geom_shared_consts_size_quirk : 0;
660
661 uint32_t shared_consts_size = shared_consts_enable ?
662 compiler->shared_consts_size : 0;
663
664 uint32_t safe_shared_consts_size = shared_consts_enable ?
665 ALIGN_POT(MAX2(DIV_ROUND_UP(shared_consts_size_geom, 4),
666 DIV_ROUND_UP(shared_consts_size, 5)), 4) : 0;
667
668 /* There are two shared limits to take into account, the geometry limit on
669 * a6xx and the total limit. The frag limit on a6xx only matters for a
670 * single stage, so it's always satisfied with the first variant.
671 */
672 if (compiler->gen >= 6) {
673 trimmed |=
674 trim_constlens(constlens, MESA_SHADER_VERTEX, MESA_SHADER_GEOMETRY,
675 compiler->max_const_geom - shared_consts_size_geom,
676 compiler->max_const_safe - safe_shared_consts_size);
677 }
678 trimmed |=
679 trim_constlens(constlens, MESA_SHADER_VERTEX, MESA_SHADER_FRAGMENT,
680 compiler->max_const_pipeline - shared_consts_size,
681 compiler->max_const_safe - safe_shared_consts_size);
682
683 return trimmed;
684 }
685
686 struct ir3_shader *
ir3_shader_from_nir(struct ir3_compiler * compiler,nir_shader * nir,const struct ir3_shader_options * options,struct ir3_stream_output_info * stream_output)687 ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir,
688 const struct ir3_shader_options *options,
689 struct ir3_stream_output_info *stream_output)
690 {
691 struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));
692
693 mtx_init(&shader->variants_lock, mtx_plain);
694 shader->compiler = compiler;
695 shader->id = p_atomic_inc_return(&shader->compiler->shader_count);
696 shader->type = nir->info.stage;
697 if (stream_output)
698 memcpy(&shader->stream_output, stream_output,
699 sizeof(shader->stream_output));
700 shader->options = *options;
701 shader->nir = nir;
702
703 ir3_disk_cache_init_shader_key(compiler, shader);
704
705 ir3_setup_used_key(shader);
706
707 return shader;
708 }
709
710 static void
dump_reg(FILE * out,const char * name,uint32_t r)711 dump_reg(FILE *out, const char *name, uint32_t r)
712 {
713 if (r != regid(63, 0)) {
714 const char *reg_type = (r & HALF_REG_ID) ? "hr" : "r";
715 fprintf(out, "; %s: %s%d.%c\n", name, reg_type, (r & ~HALF_REG_ID) >> 2,
716 "xyzw"[r & 0x3]);
717 }
718 }
719
720 static void
dump_output(FILE * out,struct ir3_shader_variant * so,unsigned slot,const char * name)721 dump_output(FILE *out, struct ir3_shader_variant *so, unsigned slot,
722 const char *name)
723 {
724 uint32_t regid;
725 regid = ir3_find_output_regid(so, slot);
726 dump_reg(out, name, regid);
727 }
728
729 static const char *
input_name(struct ir3_shader_variant * so,int i)730 input_name(struct ir3_shader_variant *so, int i)
731 {
732 if (so->inputs[i].sysval) {
733 return gl_system_value_name(so->inputs[i].slot);
734 } else if (so->type == MESA_SHADER_VERTEX) {
735 return gl_vert_attrib_name(so->inputs[i].slot);
736 } else {
737 return gl_varying_slot_name_for_stage(so->inputs[i].slot, so->type);
738 }
739 }
740
741 static const char *
output_name(struct ir3_shader_variant * so,int i)742 output_name(struct ir3_shader_variant *so, int i)
743 {
744 if (so->type == MESA_SHADER_FRAGMENT) {
745 return gl_frag_result_name(so->outputs[i].slot);
746 } else {
747 switch (so->outputs[i].slot) {
748 case VARYING_SLOT_GS_HEADER_IR3:
749 return "GS_HEADER";
750 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3:
751 return "GS_VERTEX_FLAGS";
752 case VARYING_SLOT_TCS_HEADER_IR3:
753 return "TCS_HEADER";
754 default:
755 return gl_varying_slot_name_for_stage(so->outputs[i].slot, so->type);
756 }
757 }
758 }
759
760 static const char *
ir3_const_alloc_type_to_string(enum ir3_const_alloc_type type)761 ir3_const_alloc_type_to_string(enum ir3_const_alloc_type type)
762 {
763 switch (type) {
764 case IR3_CONST_ALLOC_PUSH_CONSTS:
765 return "push_consts";
766 case IR3_CONST_ALLOC_DYN_DESCRIPTOR_OFFSET:
767 return "dyn_descriptor_offset";
768 case IR3_CONST_ALLOC_INLINE_UNIFORM_ADDRS:
769 return "inline_uniform_addresses";
770 case IR3_CONST_ALLOC_DRIVER_PARAMS:
771 return "driver_params";
772 case IR3_CONST_ALLOC_UBO_RANGES:
773 return "ubo_ranges";
774 case IR3_CONST_ALLOC_PREAMBLE:
775 return "preamble";
776 case IR3_CONST_ALLOC_GLOBAL:
777 return "global";
778 case IR3_CONST_ALLOC_UBO_PTRS:
779 return "ubo_ptrs";
780 case IR3_CONST_ALLOC_IMAGE_DIMS:
781 return "image_dims";
782 case IR3_CONST_ALLOC_KERNEL_PARAMS:
783 return "kernel_params";
784 case IR3_CONST_ALLOC_TFBO:
785 return "tfbo";
786 case IR3_CONST_ALLOC_PRIMITIVE_PARAM:
787 return "primitive_param";
788 case IR3_CONST_ALLOC_PRIMITIVE_MAP:
789 return "primitive_map";
790 default:
791 return "unknown";
792 }
793 }
794
795 static void
dump_const_state(struct ir3_shader_variant * so,FILE * out)796 dump_const_state(struct ir3_shader_variant *so, FILE *out)
797 {
798 const struct ir3_const_state *cs = ir3_const_state(so);
799 const struct ir3_ubo_analysis_state *us = &cs->ubo_state;
800
801 fprintf(out, "; num_ubos: %u\n", cs->num_ubos);
802 fprintf(out, "; num_driver_params: %u\n", cs->num_driver_params);
803 fprintf(out, "; offsets:\n");
804
805 for (uint32_t i = 0; i < IR3_CONST_ALLOC_MAX; i++) {
806 if (cs->allocs.consts[i].size_vec4) {
807 fprintf(out, "; %-26s c%u.x (%u vec4)\n",
808 ir3_const_alloc_type_to_string(i),
809 cs->allocs.consts[i].offset_vec4,
810 cs->allocs.consts[i].size_vec4);
811 }
812 }
813
814 fprintf(out, "; ubo_state:\n");
815 fprintf(out, "; num_enabled: %u\n", us->num_enabled);
816 for (unsigned i = 0; i < us->num_enabled; i++) {
817 const struct ir3_ubo_range *r = &us->range[i];
818
819 assert((r->offset % 16) == 0);
820
821 fprintf(out, "; range[%u]:\n", i);
822 fprintf(out, "; block: %u\n", r->ubo.block);
823 if (r->ubo.bindless)
824 fprintf(out, "; bindless_base: %u\n", r->ubo.bindless_base);
825 fprintf(out, "; offset: c%u.x\n", r->offset/16);
826
827 unsigned size = r->end - r->start;
828 assert((size % 16) == 0);
829
830 fprintf(out, "; size: %u vec4 (%ub -> %ub)\n", (size/16), r->start, r->end);
831 }
832 }
833
834 static uint8_t
find_input_reg_id(struct ir3_shader_variant * so,uint32_t input_idx)835 find_input_reg_id(struct ir3_shader_variant *so, uint32_t input_idx)
836 {
837 uint8_t reg = so->inputs[input_idx].regid;
838 if (so->type != MESA_SHADER_FRAGMENT || !so->ir || VALIDREG(reg))
839 return reg;
840
841 reg = INVALID_REG;
842
843 /* In FS we don't know into which register the input is loaded
844 * until the shader is scanned for the input load instructions.
845 */
846 foreach_block (block, &so->ir->block_list) {
847 foreach_instr_safe (instr, &block->instr_list) {
848 if (instr->opc == OPC_FLAT_B || instr->opc == OPC_BARY_F ||
849 instr->opc == OPC_LDLV) {
850 if (instr->srcs[0]->flags & IR3_REG_IMMED) {
851 unsigned inloc = so->inputs[input_idx].inloc;
852 unsigned instr_inloc = instr->srcs[0]->uim_val;
853 unsigned size = util_bitcount(so->inputs[input_idx].compmask);
854
855 if (instr_inloc == inloc) {
856 return instr->dsts[0]->num;
857 }
858
859 if (instr_inloc > inloc && instr_inloc < (inloc + size)) {
860 reg = MIN2(reg, instr->dsts[0]->num);
861 }
862
863 if (instr->dsts[0]->flags & IR3_REG_EI) {
864 return reg;
865 }
866 }
867 }
868 }
869 }
870
871 return reg;
872 }
873
874 void
print_raw(FILE * out,const BITSET_WORD * data,size_t size)875 print_raw(FILE *out, const BITSET_WORD *data, size_t size) {
876 fprintf(out, "raw 0x%X%X\n", data[0], data[1]);
877 }
878
879 void
ir3_shader_disasm(struct ir3_shader_variant * so,uint32_t * bin,FILE * out)880 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out)
881 {
882 struct ir3 *ir = so->ir;
883 struct ir3_register *reg;
884 const char *type = ir3_shader_stage(so);
885 uint8_t regid;
886 unsigned i;
887
888 dump_const_state(so, out);
889
890 foreach_input_n (instr, i, ir) {
891 reg = instr->dsts[0];
892 regid = reg->num;
893 fprintf(out, "@in(%sr%d.%c)\tin%d",
894 (reg->flags & IR3_REG_HALF) ? "h" : "", (regid >> 2),
895 "xyzw"[regid & 0x3], i);
896
897 if (reg->wrmask > 0x1)
898 fprintf(out, " (wrmask=0x%x)", reg->wrmask);
899 fprintf(out, "\n");
900 }
901
902 /* print pre-dispatch texture fetches: */
903 for (i = 0; i < so->num_sampler_prefetch; i++) {
904 const struct ir3_sampler_prefetch *fetch = &so->sampler_prefetch[i];
905 fprintf(out,
906 "@tex(%sr%d.%c)\tsrc=%u, bindless=%u, samp=%u, tex=%u, wrmask=0x%x, opc=%s\n",
907 fetch->half_precision ? "h" : "", fetch->dst >> 2,
908 "xyzw"[fetch->dst & 0x3], fetch->src, fetch->bindless,
909 fetch->bindless ? fetch->samp_bindless_id : fetch->samp_id,
910 fetch->bindless ? fetch->tex_bindless_id : fetch->tex_id,
911 fetch->wrmask, disasm_a3xx_instr_name(fetch->tex_opc));
912 }
913
914 const struct ir3_const_state *const_state = ir3_const_state(so);
915 for (i = 0; i < DIV_ROUND_UP(const_state->immediates_count, 4); i++) {
916 fprintf(out, "@const(c%d.x)\t",
917 const_state->allocs.max_const_offset_vec4 + i);
918 fprintf(out, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
919 const_state->immediates[i * 4 + 0],
920 const_state->immediates[i * 4 + 1],
921 const_state->immediates[i * 4 + 2],
922 const_state->immediates[i * 4 + 3]);
923 }
924
925 ir3_isa_disasm(bin, so->info.sizedwords * 4, out,
926 &(struct isa_decode_options){
927 .gpu_id = ir->compiler->gen * 100,
928 .show_errors = true,
929 .branch_labels = true,
930 .no_match_cb = print_raw,
931 });
932
933 fprintf(out, "; %s: outputs:", type);
934 for (i = 0; i < so->outputs_count; i++) {
935 uint8_t regid = so->outputs[i].regid;
936 const char *reg_type = so->outputs[i].half ? "hr" : "r";
937 fprintf(out, " %s%d.%c (%s)", reg_type, (regid >> 2), "xyzw"[regid & 0x3],
938 output_name(so, i));
939 }
940 fprintf(out, "\n");
941
942 fprintf(out, "; %s: inputs:", type);
943 for (i = 0; i < so->inputs_count; i++) {
944 uint8_t regid = find_input_reg_id(so, i);
945
946 fprintf(out, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)", (regid >> 2),
947 "xyzw"[regid & 0x3], input_name(so, i), so -> inputs[i].slot,
948 so->inputs[i].compmask, so->inputs[i].inloc, so->inputs[i].bary);
949 }
950 fprintf(out, "\n");
951
952 /* print generic shader info: */
953 fprintf(
954 out,
955 "; %s prog %d/%d: %u instr, %u nops, %u non-nops, %u mov, %u cov, %u dwords\n",
956 type, so->shader_id, so->id, so->info.instrs_count, so->info.nops_count,
957 so->info.instrs_count - so->info.nops_count, so->info.mov_count,
958 so->info.cov_count, so->info.sizedwords);
959
960 fprintf(out,
961 "; %s prog %d/%d: %u last-baryf, %u last-helper, %d half, %d full, %u constlen\n",
962 type, so->shader_id, so->id, so->info.last_baryf,
963 so->info.last_helper, so->info.max_half_reg + 1,
964 so->info.max_reg + 1, so->constlen);
965
966 fprintf(
967 out,
968 "; %s prog %d/%d: %u cat0, %u cat1, %u cat2, %u cat3, %u cat4, %u cat5, %u cat6, %u cat7\n",
969 type, so->shader_id, so->id, so->info.instrs_per_cat[0],
970 so->info.instrs_per_cat[1], so->info.instrs_per_cat[2],
971 so->info.instrs_per_cat[3], so->info.instrs_per_cat[4],
972 so->info.instrs_per_cat[5], so->info.instrs_per_cat[6],
973 so->info.instrs_per_cat[7]);
974
975 fprintf(
976 out,
977 "; %s prog %d/%d: %u sstall, %u (ss), %u systall, %u (sy), %d loops\n",
978 type, so->shader_id, so->id, so->info.sstall, so->info.ss,
979 so->info.systall, so->info.sy, so->loops);
980
981 if (so->info.preamble_instrs_count) {
982 fprintf(
983 out, "; %u preamble instr, %d early-preamble\n",
984 so->info.preamble_instrs_count, so->info.early_preamble);
985 }
986
987 /* print shader type specific info: */
988 switch (so->type) {
989 case MESA_SHADER_VERTEX:
990 dump_output(out, so, VARYING_SLOT_POS, "pos");
991 dump_output(out, so, VARYING_SLOT_PSIZ, "psize");
992 dump_output(out, so, VARYING_SLOT_PRIMITIVE_SHADING_RATE, "shading_rate");
993 break;
994 case MESA_SHADER_FRAGMENT:
995 dump_reg(out, "pos (ij_pixel)",
996 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL));
997 dump_reg(
998 out, "pos (ij_centroid)",
999 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID));
1000 dump_reg(out, "pos (center_rhw)",
1001 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTER_RHW));
1002 dump_output(out, so, FRAG_RESULT_DEPTH, "posz");
1003 if (so->color0_mrt) {
1004 dump_output(out, so, FRAG_RESULT_COLOR, "color");
1005 } else {
1006 dump_output(out, so, FRAG_RESULT_DATA0, "data0");
1007 dump_output(out, so, FRAG_RESULT_DATA1, "data1");
1008 dump_output(out, so, FRAG_RESULT_DATA2, "data2");
1009 dump_output(out, so, FRAG_RESULT_DATA3, "data3");
1010 dump_output(out, so, FRAG_RESULT_DATA4, "data4");
1011 dump_output(out, so, FRAG_RESULT_DATA5, "data5");
1012 dump_output(out, so, FRAG_RESULT_DATA6, "data6");
1013 dump_output(out, so, FRAG_RESULT_DATA7, "data7");
1014 }
1015 dump_reg(out, "fragcoord",
1016 ir3_find_sysval_regid(so, SYSTEM_VALUE_FRAG_COORD));
1017 dump_reg(out, "fragface",
1018 ir3_find_sysval_regid(so, SYSTEM_VALUE_FRONT_FACE));
1019 break;
1020 default:
1021 /* TODO */
1022 break;
1023 }
1024
1025 fprintf(out, "\n");
1026 }
1027
1028 uint64_t
ir3_shader_outputs(const struct ir3_shader * so)1029 ir3_shader_outputs(const struct ir3_shader *so)
1030 {
1031 return so->nir->info.outputs_written;
1032 }
1033
1034 void
ir3_shader_get_subgroup_size(const struct ir3_compiler * compiler,const struct ir3_shader_options * options,gl_shader_stage stage,unsigned * subgroup_size,unsigned * max_subgroup_size)1035 ir3_shader_get_subgroup_size(const struct ir3_compiler *compiler,
1036 const struct ir3_shader_options *options,
1037 gl_shader_stage stage, unsigned *subgroup_size,
1038 unsigned *max_subgroup_size)
1039 {
1040 switch (options->api_wavesize) {
1041 case IR3_SINGLE_ONLY:
1042 *subgroup_size = *max_subgroup_size = compiler->threadsize_base;
1043 break;
1044 case IR3_DOUBLE_ONLY:
1045 *subgroup_size = *max_subgroup_size = compiler->threadsize_base * 2;
1046 break;
1047 case IR3_SINGLE_OR_DOUBLE:
1048 /* For vertex stages, we know the wavesize will never be doubled.
1049 * Lower subgroup_size here, to avoid having to deal with it when
1050 * translating from NIR. Otherwise use the "real" wavesize obtained as
1051 * a driver param.
1052 */
1053 if (stage != MESA_SHADER_COMPUTE && stage != MESA_SHADER_FRAGMENT) {
1054 *subgroup_size = *max_subgroup_size = compiler->threadsize_base;
1055 } else {
1056 *subgroup_size = 0;
1057 *max_subgroup_size = compiler->threadsize_base * 2;
1058 }
1059 break;
1060 }
1061 }
1062
1063 /* Add any missing varyings needed for stream-out. Otherwise varyings not
1064 * used by fragment shader will be stripped out.
1065 */
1066 void
ir3_link_stream_out(struct ir3_shader_linkage * l,const struct ir3_shader_variant * v)1067 ir3_link_stream_out(struct ir3_shader_linkage *l,
1068 const struct ir3_shader_variant *v)
1069 {
1070 const struct ir3_stream_output_info *strmout = &v->stream_output;
1071
1072 /*
1073 * First, any stream-out varyings not already in linkage map (ie. also
1074 * consumed by frag shader) need to be added:
1075 */
1076 for (unsigned i = 0; i < strmout->num_outputs; i++) {
1077 const struct ir3_stream_output *out = &strmout->output[i];
1078 unsigned k = out->register_index;
1079 unsigned compmask =
1080 (1 << (out->num_components + out->start_component)) - 1;
1081 unsigned idx, nextloc = 0;
1082
1083 /* psize/pos need to be the last entries in linkage map, and will
1084 * get added link_stream_out, so skip over them:
1085 */
1086 if ((v->outputs[k].slot == VARYING_SLOT_PSIZ) ||
1087 (v->outputs[k].slot == VARYING_SLOT_POS))
1088 continue;
1089
1090 for (idx = 0; idx < l->cnt; idx++) {
1091 if (l->var[idx].slot == v->outputs[k].slot)
1092 break;
1093 nextloc = MAX2(nextloc, l->var[idx].loc + 4);
1094 }
1095
1096 /* add if not already in linkage map: */
1097 if (idx == l->cnt) {
1098 ir3_link_add(l, v->outputs[k].slot, v->outputs[k].regid,
1099 compmask, nextloc);
1100 }
1101
1102 /* expand component-mask if needed, ie streaming out all components
1103 * but frag shader doesn't consume all components:
1104 */
1105 if (compmask & ~l->var[idx].compmask) {
1106 l->var[idx].compmask |= compmask;
1107 l->max_loc = MAX2(
1108 l->max_loc, l->var[idx].loc + util_last_bit(l->var[idx].compmask));
1109 }
1110 }
1111 }
1112