ir3: Support variants with different constlen's
[mesa.git] / src / freedreno / ir3 / ir3_shader.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_atomic.h"
28 #include "util/u_string.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/format/u_format.h"
32
33 #include "drm/freedreno_drmif.h"
34
35 #include "ir3_shader.h"
36 #include "ir3_compiler.h"
37 #include "ir3_nir.h"
38
39 int
40 ir3_glsl_type_size(const struct glsl_type *type, bool bindless)
41 {
42 return glsl_count_attribute_slots(type, false);
43 }
44
45 /* for vertex shader, the inputs are loaded into registers before the shader
46 * is executed, so max_regs from the shader instructions might not properly
47 * reflect the # of registers actually used, especially in case passthrough
48 * varyings.
49 *
50 * Likewise, for fragment shader, we can have some regs which are passed
51 * input values but never touched by the resulting shader (ie. as result
52 * of dead code elimination or simply because we don't know how to turn
53 * the reg off.
54 */
55 static void
56 fixup_regfootprint(struct ir3_shader_variant *v)
57 {
58 unsigned i;
59
60 for (i = 0; i < v->inputs_count; i++) {
61 /* skip frag inputs fetch via bary.f since their reg's are
62 * not written by gpu before shader starts (and in fact the
63 * regid's might not even be valid)
64 */
65 if (v->inputs[i].bary)
66 continue;
67
68 /* ignore high regs that are global to all threads in a warp
69 * (they exist by default) (a5xx+)
70 */
71 if (v->inputs[i].regid >= regid(48,0))
72 continue;
73
74 if (v->inputs[i].compmask) {
75 unsigned n = util_last_bit(v->inputs[i].compmask) - 1;
76 int32_t regid = v->inputs[i].regid + n;
77 if (v->inputs[i].half) {
78 if (!v->mergedregs) {
79 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
80 } else {
81 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
82 }
83 } else {
84 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
85 }
86 }
87 }
88
89 for (i = 0; i < v->outputs_count; i++) {
90 /* for ex, VS shaders with tess don't have normal varying outs: */
91 if (!VALIDREG(v->outputs[i].regid))
92 continue;
93 int32_t regid = v->outputs[i].regid + 3;
94 if (v->outputs[i].half) {
95 if (!v->mergedregs) {
96 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
97 } else {
98 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
99 }
100 } else {
101 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
102 }
103 }
104
105 for (i = 0; i < v->num_sampler_prefetch; i++) {
106 unsigned n = util_last_bit(v->sampler_prefetch[i].wrmask) - 1;
107 int32_t regid = v->sampler_prefetch[i].dst + n;
108 if (v->sampler_prefetch[i].half_precision) {
109 if (!v->mergedregs) {
110 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
111 } else {
112 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
113 }
114 } else {
115 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
116 }
117 }
118 }
119
120 /* wrapper for ir3_assemble() which does some info fixup based on
121 * shader state. Non-static since used by ir3_cmdline too.
122 */
123 void * ir3_shader_assemble(struct ir3_shader_variant *v)
124 {
125 unsigned gpu_id = v->shader->compiler->gpu_id;
126 void *bin;
127
128 bin = ir3_assemble(v);
129 if (!bin)
130 return NULL;
131
132 if (gpu_id >= 400) {
133 v->instrlen = v->info.sizedwords / (2 * 16);
134 } else {
135 v->instrlen = v->info.sizedwords / (2 * 4);
136 }
137
138 /* NOTE: if relative addressing is used, we set constlen in
139 * the compiler (to worst-case value) since we don't know in
140 * the assembler what the max addr reg value can be:
141 */
142 v->constlen = MAX2(v->constlen, v->info.max_const + 1);
143
144 /* On a4xx and newer, constlen must be a multiple of 16 dwords even though
145 * uploads are in units of 4 dwords. Round it up here to make calculations
146 * regarding the shared constlen simpler.
147 */
148 if (gpu_id >= 400)
149 v->constlen = align(v->constlen, 4);
150
151 fixup_regfootprint(v);
152
153 return bin;
154 }
155
156 static void
157 assemble_variant(struct ir3_shader_variant *v)
158 {
159 v->bin = ir3_shader_assemble(v);
160
161 if (shader_debug_enabled(v->shader->type)) {
162 fprintf(stdout, "Native code for unnamed %s shader %s:\n",
163 ir3_shader_stage(v), v->shader->nir->info.name);
164 if (v->shader->type == MESA_SHADER_FRAGMENT)
165 fprintf(stdout, "SIMD0\n");
166 ir3_shader_disasm(v, v->bin, stdout);
167 }
168
169 /* no need to keep the ir around beyond this point: */
170 ir3_destroy(v->ir);
171 v->ir = NULL;
172 }
173
174 /*
175 * For creating normal shader variants, 'nonbinning' is NULL. For
176 * creating binning pass shader, it is link to corresponding normal
177 * (non-binning) variant.
178 */
179 static struct ir3_shader_variant *
180 create_variant(struct ir3_shader *shader, const struct ir3_shader_key *key,
181 struct ir3_shader_variant *nonbinning)
182 {
183 struct ir3_shader_variant *v = rzalloc_size(shader, sizeof(*v));
184 int ret;
185
186 if (!v)
187 return NULL;
188
189 v->id = ++shader->variant_count;
190 v->shader = shader;
191 v->binning_pass = !!nonbinning;
192 v->nonbinning = nonbinning;
193 v->key = *key;
194 v->type = shader->type;
195 v->mergedregs = shader->compiler->gpu_id >= 600;
196
197 if (!v->binning_pass)
198 v->const_state = rzalloc_size(v, sizeof(*v->const_state));
199
200 ret = ir3_compile_shader_nir(shader->compiler, v);
201 if (ret) {
202 debug_error("compile failed!");
203 goto fail;
204 }
205
206 assemble_variant(v);
207 if (!v->bin) {
208 debug_error("assemble failed!");
209 goto fail;
210 }
211
212 return v;
213
214 fail:
215 ralloc_free(v);
216 return NULL;
217 }
218
219 static inline struct ir3_shader_variant *
220 shader_variant(struct ir3_shader *shader, const struct ir3_shader_key *key,
221 bool *created)
222 {
223 struct ir3_shader_variant *v;
224
225 *created = false;
226
227 for (v = shader->variants; v; v = v->next)
228 if (ir3_shader_key_equal(key, &v->key))
229 return v;
230
231 /* compile new variant if it doesn't exist already: */
232 v = create_variant(shader, key, NULL);
233 if (v) {
234 v->next = shader->variants;
235 shader->variants = v;
236 *created = true;
237 }
238
239 return v;
240 }
241
242 struct ir3_shader_variant *
243 ir3_shader_get_variant(struct ir3_shader *shader, const struct ir3_shader_key *key,
244 bool binning_pass, bool *created)
245 {
246 mtx_lock(&shader->variants_lock);
247 struct ir3_shader_variant *v =
248 shader_variant(shader, key, created);
249
250 if (v && binning_pass) {
251 if (!v->binning) {
252 v->binning = create_variant(shader, key, v);
253 *created = true;
254 }
255 mtx_unlock(&shader->variants_lock);
256 return v->binning;
257 }
258 mtx_unlock(&shader->variants_lock);
259
260 return v;
261 }
262
263 void
264 ir3_shader_destroy(struct ir3_shader *shader)
265 {
266 ralloc_free(shader->nir);
267 mtx_destroy(&shader->variants_lock);
268 ralloc_free(shader);
269 }
270
271 /**
272 * Creates a bitmask of the used bits of the shader key by this particular
273 * shader. Used by the gallium driver to skip state-dependent recompiles when
274 * possible.
275 */
276 static void
277 ir3_setup_used_key(struct ir3_shader *shader)
278 {
279 nir_shader *nir = shader->nir;
280 struct shader_info *info = &nir->info;
281 struct ir3_shader_key *key = &shader->key_mask;
282
283 /* This key flag is just used to make for a cheaper ir3_shader_key_equal
284 * check in the common case.
285 */
286 key->has_per_samp = true;
287
288 key->safe_constlen = true;
289
290 if (info->stage == MESA_SHADER_FRAGMENT) {
291 key->fsaturate_s = ~0;
292 key->fsaturate_t = ~0;
293 key->fsaturate_r = ~0;
294 key->fastc_srgb = ~0;
295 key->fsamples = ~0;
296
297 if (info->inputs_read & VARYING_BITS_COLOR) {
298 key->rasterflat = true;
299 key->color_two_side = true;
300 }
301
302 if ((info->outputs_written & ~(FRAG_RESULT_DEPTH |
303 FRAG_RESULT_STENCIL |
304 FRAG_RESULT_SAMPLE_MASK)) != 0) {
305 key->fclamp_color = true;
306 }
307
308 /* Only used for deciding on behavior of
309 * nir_intrinsic_load_barycentric_sample
310 */
311 key->msaa = info->fs.uses_sample_qualifier;
312 } else {
313 key->tessellation = ~0;
314 key->has_gs = true;
315
316 if (info->outputs_written & VARYING_BITS_COLOR)
317 key->vclamp_color = true;
318
319 if (info->stage == MESA_SHADER_VERTEX) {
320 key->vsaturate_s = ~0;
321 key->vsaturate_t = ~0;
322 key->vsaturate_r = ~0;
323 key->vastc_srgb = ~0;
324 key->vsamples = ~0;
325 }
326 }
327 }
328
329 struct ir3_shader *
330 ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir,
331 unsigned reserved_user_consts, struct ir3_stream_output_info *stream_output)
332 {
333 struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));
334
335 mtx_init(&shader->variants_lock, mtx_plain);
336 shader->compiler = compiler;
337 shader->id = p_atomic_inc_return(&shader->compiler->shader_count);
338 shader->type = nir->info.stage;
339 if (stream_output)
340 memcpy(&shader->stream_output, stream_output, sizeof(shader->stream_output));
341 shader->num_reserved_user_consts = reserved_user_consts;
342
343 if (nir->info.stage == MESA_SHADER_GEOMETRY)
344 NIR_PASS_V(nir, ir3_nir_lower_gs);
345
346 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
347 (nir_lower_io_options)0);
348
349 if (compiler->gpu_id >= 600 &&
350 nir->info.stage == MESA_SHADER_FRAGMENT &&
351 !(ir3_shader_debug & IR3_DBG_NOFP16))
352 NIR_PASS_V(nir, nir_lower_mediump_outputs);
353
354 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
355 /* NOTE: lower load_barycentric_at_sample first, since it
356 * produces load_barycentric_at_offset:
357 */
358 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_sample);
359 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_offset);
360
361 NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
362 }
363
364 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
365
366 NIR_PASS_V(nir, nir_lower_amul, ir3_glsl_type_size);
367
368 /* do first pass optimization, ignoring the key: */
369 ir3_optimize_nir(shader, nir);
370
371 shader->nir = nir;
372 if (ir3_shader_debug & IR3_DBG_DISASM) {
373 printf("dump nir%d: type=%d", shader->id, shader->type);
374 nir_print_shader(shader->nir, stdout);
375 }
376
377 ir3_setup_used_key(shader);
378
379 return shader;
380 }
381
382 static void dump_reg(FILE *out, const char *name, uint32_t r)
383 {
384 if (r != regid(63,0)) {
385 const char *reg_type = (r & HALF_REG_ID) ? "hr" : "r";
386 fprintf(out, "; %s: %s%d.%c\n", name, reg_type,
387 (r & ~HALF_REG_ID) >> 2, "xyzw"[r & 0x3]);
388 }
389 }
390
391 static void dump_output(FILE *out, struct ir3_shader_variant *so,
392 unsigned slot, const char *name)
393 {
394 uint32_t regid;
395 regid = ir3_find_output_regid(so, slot);
396 dump_reg(out, name, regid);
397 }
398
399 static const char *
400 input_name(struct ir3_shader_variant *so, int i)
401 {
402 if (so->inputs[i].sysval) {
403 return gl_system_value_name(so->inputs[i].slot);
404 } else if (so->type == MESA_SHADER_VERTEX) {
405 return gl_vert_attrib_name(so->inputs[i].slot);
406 } else {
407 return gl_varying_slot_name(so->inputs[i].slot);
408 }
409 }
410
411 static const char *
412 output_name(struct ir3_shader_variant *so, int i)
413 {
414 if (so->type == MESA_SHADER_FRAGMENT) {
415 return gl_frag_result_name(so->outputs[i].slot);
416 } else {
417 switch (so->outputs[i].slot) {
418 case VARYING_SLOT_GS_HEADER_IR3:
419 return "GS_HEADER";
420 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3:
421 return "GS_VERTEX_FLAGS";
422 case VARYING_SLOT_TCS_HEADER_IR3:
423 return "TCS_HEADER";
424 default:
425 return gl_varying_slot_name(so->outputs[i].slot);
426 }
427 }
428 }
429
430 void
431 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out)
432 {
433 struct ir3 *ir = so->ir;
434 struct ir3_register *reg;
435 const char *type = ir3_shader_stage(so);
436 uint8_t regid;
437 unsigned i;
438
439 foreach_input_n (instr, i, ir) {
440 reg = instr->regs[0];
441 regid = reg->num;
442 fprintf(out, "@in(%sr%d.%c)\tin%d",
443 (reg->flags & IR3_REG_HALF) ? "h" : "",
444 (regid >> 2), "xyzw"[regid & 0x3], i);
445
446 if (reg->wrmask > 0x1)
447 fprintf(out, " (wrmask=0x%x)", reg->wrmask);
448 fprintf(out, "\n");
449 }
450
451 /* print pre-dispatch texture fetches: */
452 for (i = 0; i < so->num_sampler_prefetch; i++) {
453 const struct ir3_sampler_prefetch *fetch = &so->sampler_prefetch[i];
454 fprintf(out, "@tex(%sr%d.%c)\tsrc=%u, samp=%u, tex=%u, wrmask=0x%x, cmd=%u\n",
455 fetch->half_precision ? "h" : "",
456 fetch->dst >> 2, "xyzw"[fetch->dst & 0x3],
457 fetch->src, fetch->samp_id, fetch->tex_id,
458 fetch->wrmask, fetch->cmd);
459 }
460
461 foreach_output_n (instr, i, ir) {
462 reg = instr->regs[0];
463 regid = reg->num;
464 fprintf(out, "@out(%sr%d.%c)\tout%d",
465 (reg->flags & IR3_REG_HALF) ? "h" : "",
466 (regid >> 2), "xyzw"[regid & 0x3], i);
467 if (reg->wrmask > 0x1)
468 fprintf(out, " (wrmask=0x%x)", reg->wrmask);
469 fprintf(out, "\n");
470 }
471
472 const struct ir3_const_state *const_state = ir3_const_state(so);
473 for (i = 0; i < const_state->immediates_count; i++) {
474 fprintf(out, "@const(c%d.x)\t", const_state->offsets.immediate + i);
475 fprintf(out, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
476 const_state->immediates[i].val[0],
477 const_state->immediates[i].val[1],
478 const_state->immediates[i].val[2],
479 const_state->immediates[i].val[3]);
480 }
481
482 disasm_a3xx(bin, so->info.sizedwords, 0, out, ir->compiler->gpu_id);
483
484 fprintf(out, "; %s: outputs:", type);
485 for (i = 0; i < so->outputs_count; i++) {
486 uint8_t regid = so->outputs[i].regid;
487 const char *reg_type = so->outputs[i].half ? "hr" : "r";
488 fprintf(out, " %s%d.%c (%s)",
489 reg_type, (regid >> 2), "xyzw"[regid & 0x3],
490 output_name(so, i));
491 }
492 fprintf(out, "\n");
493
494 fprintf(out, "; %s: inputs:", type);
495 for (i = 0; i < so->inputs_count; i++) {
496 uint8_t regid = so->inputs[i].regid;
497 fprintf(out, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)",
498 (regid >> 2), "xyzw"[regid & 0x3],
499 input_name(so, i),
500 so->inputs[i].slot,
501 so->inputs[i].compmask,
502 so->inputs[i].inloc,
503 so->inputs[i].bary);
504 }
505 fprintf(out, "\n");
506
507 /* print generic shader info: */
508 fprintf(out, "; %s prog %d/%d: %u instr, %u nops, %u non-nops, %u mov, %u cov, %u dwords\n",
509 type, so->shader->id, so->id,
510 so->info.instrs_count,
511 so->info.nops_count,
512 so->info.instrs_count - so->info.nops_count,
513 so->info.mov_count, so->info.cov_count,
514 so->info.sizedwords);
515
516 fprintf(out, "; %s prog %d/%d: %u last-baryf, %d half, %d full, %u constlen\n",
517 type, so->shader->id, so->id,
518 so->info.last_baryf,
519 so->info.max_half_reg + 1,
520 so->info.max_reg + 1,
521 so->constlen);
522
523 fprintf(out, "; %s prog %d/%d: %u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
524 type, so->shader->id, so->id,
525 so->info.sstall,
526 so->info.ss,
527 so->info.sy,
528 so->max_sun,
529 so->loops);
530
531 /* print shader type specific info: */
532 switch (so->type) {
533 case MESA_SHADER_VERTEX:
534 dump_output(out, so, VARYING_SLOT_POS, "pos");
535 dump_output(out, so, VARYING_SLOT_PSIZ, "psize");
536 break;
537 case MESA_SHADER_FRAGMENT:
538 dump_reg(out, "pos (ij_pixel)",
539 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL));
540 dump_reg(out, "pos (ij_centroid)",
541 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID));
542 dump_reg(out, "pos (ij_size)",
543 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PERSP_SIZE));
544 dump_output(out, so, FRAG_RESULT_DEPTH, "posz");
545 if (so->color0_mrt) {
546 dump_output(out, so, FRAG_RESULT_COLOR, "color");
547 } else {
548 dump_output(out, so, FRAG_RESULT_DATA0, "data0");
549 dump_output(out, so, FRAG_RESULT_DATA1, "data1");
550 dump_output(out, so, FRAG_RESULT_DATA2, "data2");
551 dump_output(out, so, FRAG_RESULT_DATA3, "data3");
552 dump_output(out, so, FRAG_RESULT_DATA4, "data4");
553 dump_output(out, so, FRAG_RESULT_DATA5, "data5");
554 dump_output(out, so, FRAG_RESULT_DATA6, "data6");
555 dump_output(out, so, FRAG_RESULT_DATA7, "data7");
556 }
557 dump_reg(out, "fragcoord",
558 ir3_find_sysval_regid(so, SYSTEM_VALUE_FRAG_COORD));
559 dump_reg(out, "fragface",
560 ir3_find_sysval_regid(so, SYSTEM_VALUE_FRONT_FACE));
561 break;
562 default:
563 /* TODO */
564 break;
565 }
566
567 fprintf(out, "\n");
568 }
569
570 uint64_t
571 ir3_shader_outputs(const struct ir3_shader *so)
572 {
573 return so->nir->info.outputs_written;
574 }