nir: Get rid of global registers
[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_string.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30
31 #include "drm/freedreno_drmif.h"
32
33 #include "ir3_shader.h"
34 #include "ir3_compiler.h"
35 #include "ir3_nir.h"
36
37 int
38 ir3_glsl_type_size(const struct glsl_type *type)
39 {
40 return glsl_count_attribute_slots(type, false);
41 }
42
43 static void
44 delete_variant(struct ir3_shader_variant *v)
45 {
46 if (v->ir)
47 ir3_destroy(v->ir);
48 if (v->bo)
49 fd_bo_del(v->bo);
50 if (v->immediates)
51 free(v->immediates);
52 free(v);
53 }
54
55 /* for vertex shader, the inputs are loaded into registers before the shader
56 * is executed, so max_regs from the shader instructions might not properly
57 * reflect the # of registers actually used, especially in case passthrough
58 * varyings.
59 *
60 * Likewise, for fragment shader, we can have some regs which are passed
61 * input values but never touched by the resulting shader (ie. as result
62 * of dead code elimination or simply because we don't know how to turn
63 * the reg off.
64 */
65 static void
66 fixup_regfootprint(struct ir3_shader_variant *v)
67 {
68 unsigned i;
69
70 for (i = 0; i < v->inputs_count; i++) {
71 /* skip frag inputs fetch via bary.f since their reg's are
72 * not written by gpu before shader starts (and in fact the
73 * regid's might not even be valid)
74 */
75 if (v->inputs[i].bary)
76 continue;
77
78 /* ignore high regs that are global to all threads in a warp
79 * (they exist by default) (a5xx+)
80 */
81 if (v->inputs[i].regid >= regid(48,0))
82 continue;
83
84 if (v->inputs[i].compmask) {
85 unsigned n = util_last_bit(v->inputs[i].compmask) - 1;
86 int32_t regid = (v->inputs[i].regid + n) >> 2;
87 v->info.max_reg = MAX2(v->info.max_reg, regid);
88 }
89 }
90
91 for (i = 0; i < v->outputs_count; i++) {
92 int32_t regid = (v->outputs[i].regid + 3) >> 2;
93 v->info.max_reg = MAX2(v->info.max_reg, regid);
94 }
95 }
96
97 /* wrapper for ir3_assemble() which does some info fixup based on
98 * shader state. Non-static since used by ir3_cmdline too.
99 */
100 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id)
101 {
102 void *bin;
103
104 bin = ir3_assemble(v->ir, &v->info, gpu_id);
105 if (!bin)
106 return NULL;
107
108 if (gpu_id >= 400) {
109 v->instrlen = v->info.sizedwords / (2 * 16);
110 } else {
111 v->instrlen = v->info.sizedwords / (2 * 4);
112 }
113
114 /* NOTE: if relative addressing is used, we set constlen in
115 * the compiler (to worst-case value) since we don't know in
116 * the assembler what the max addr reg value can be:
117 */
118 v->constlen = MIN2(255, MAX2(v->constlen, v->info.max_const + 1));
119
120 fixup_regfootprint(v);
121
122 return bin;
123 }
124
125 static void
126 assemble_variant(struct ir3_shader_variant *v)
127 {
128 struct ir3_compiler *compiler = v->shader->compiler;
129 struct shader_info *info = &v->shader->nir->info;
130 uint32_t gpu_id = compiler->gpu_id;
131 uint32_t sz, *bin;
132
133 bin = ir3_shader_assemble(v, gpu_id);
134 sz = v->info.sizedwords * 4;
135
136 v->bo = fd_bo_new(compiler->dev, sz,
137 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
138 DRM_FREEDRENO_GEM_TYPE_KMEM,
139 "%s:%s", ir3_shader_stage(v->shader), info->name);
140
141 memcpy(fd_bo_map(v->bo), bin, sz);
142
143 if (ir3_shader_debug & IR3_DBG_DISASM) {
144 struct ir3_shader_key key = v->key;
145 printf("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}", v->type,
146 v->binning_pass, key.color_two_side, key.half_precision);
147 ir3_shader_disasm(v, bin, stdout);
148 }
149
150 if (shader_debug_enabled(v->shader->type)) {
151 fprintf(stderr, "Native code for unnamed %s shader %s:\n",
152 _mesa_shader_stage_to_string(v->shader->type),
153 v->shader->nir->info.name);
154 if (v->shader->type == MESA_SHADER_FRAGMENT)
155 fprintf(stderr, "SIMD0\n");
156 ir3_shader_disasm(v, bin, stderr);
157 }
158
159 free(bin);
160
161 /* no need to keep the ir around beyond this point: */
162 ir3_destroy(v->ir);
163 v->ir = NULL;
164 }
165
166 static struct ir3_shader_variant *
167 create_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
168 bool binning_pass)
169 {
170 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
171 int ret;
172
173 if (!v)
174 return NULL;
175
176 v->id = ++shader->variant_count;
177 v->shader = shader;
178 v->binning_pass = binning_pass;
179 v->key = *key;
180 v->type = shader->type;
181
182 ret = ir3_compile_shader_nir(shader->compiler, v);
183 if (ret) {
184 debug_error("compile failed!");
185 goto fail;
186 }
187
188 assemble_variant(v);
189 if (!v->bo) {
190 debug_error("assemble failed!");
191 goto fail;
192 }
193
194 return v;
195
196 fail:
197 delete_variant(v);
198 return NULL;
199 }
200
201 static inline struct ir3_shader_variant *
202 shader_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
203 bool *created)
204 {
205 struct ir3_shader_variant *v;
206
207 *created = false;
208
209 for (v = shader->variants; v; v = v->next)
210 if (ir3_shader_key_equal(key, &v->key))
211 return v;
212
213 /* compile new variant if it doesn't exist already: */
214 v = create_variant(shader, key, false);
215 if (v) {
216 v->next = shader->variants;
217 shader->variants = v;
218 *created = true;
219 }
220
221 return v;
222 }
223
224 struct ir3_shader_variant *
225 ir3_shader_get_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
226 bool binning_pass, bool *created)
227 {
228 struct ir3_shader_variant *v =
229 shader_variant(shader, key, created);
230
231 if (v && binning_pass) {
232 if (!v->binning)
233 v->binning = create_variant(shader, key, true);
234 return v->binning;
235 }
236
237 return v;
238 }
239
240 void
241 ir3_shader_destroy(struct ir3_shader *shader)
242 {
243 struct ir3_shader_variant *v, *t;
244 for (v = shader->variants; v; ) {
245 t = v;
246 v = v->next;
247 delete_variant(t);
248 }
249 ralloc_free(shader->nir);
250 free(shader);
251 }
252
253 struct ir3_shader *
254 ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir)
255 {
256 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
257
258 shader->compiler = compiler;
259 shader->id = ++shader->compiler->shader_count;
260 shader->type = nir->info.stage;
261
262 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
263 (nir_lower_io_options)0);
264
265 if (nir->info.stage == MESA_SHADER_FRAGMENT)
266 NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
267
268 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
269
270 /* do first pass optimization, ignoring the key: */
271 shader->nir = ir3_optimize_nir(shader, nir, NULL);
272 if (ir3_shader_debug & IR3_DBG_DISASM) {
273 printf("dump nir%d: type=%d", shader->id, shader->type);
274 nir_print_shader(shader->nir, stdout);
275 }
276
277 return shader;
278 }
279
280 static void dump_reg(FILE *out, const char *name, uint32_t r)
281 {
282 if (r != regid(63,0))
283 fprintf(out, "; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
284 }
285
286 static void dump_output(FILE *out, struct ir3_shader_variant *so,
287 unsigned slot, const char *name)
288 {
289 uint32_t regid;
290 regid = ir3_find_output_regid(so, slot);
291 dump_reg(out, name, regid);
292 }
293
294 void
295 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out)
296 {
297 struct ir3 *ir = so->ir;
298 struct ir3_register *reg;
299 const char *type = ir3_shader_stage(so->shader);
300 uint8_t regid;
301 unsigned i;
302
303 for (i = 0; i < ir->ninputs; i++) {
304 if (!ir->inputs[i]) {
305 fprintf(out, "; in%d unused\n", i);
306 continue;
307 }
308 reg = ir->inputs[i]->regs[0];
309 regid = reg->num;
310 fprintf(out, "@in(%sr%d.%c)\tin%d\n",
311 (reg->flags & IR3_REG_HALF) ? "h" : "",
312 (regid >> 2), "xyzw"[regid & 0x3], i);
313 }
314
315 for (i = 0; i < ir->noutputs; i++) {
316 if (!ir->outputs[i]) {
317 fprintf(out, "; out%d unused\n", i);
318 continue;
319 }
320 /* kill shows up as a virtual output.. skip it! */
321 if (is_kill(ir->outputs[i]))
322 continue;
323 reg = ir->outputs[i]->regs[0];
324 regid = reg->num;
325 fprintf(out, "@out(%sr%d.%c)\tout%d\n",
326 (reg->flags & IR3_REG_HALF) ? "h" : "",
327 (regid >> 2), "xyzw"[regid & 0x3], i);
328 }
329
330 for (i = 0; i < so->immediates_count; i++) {
331 fprintf(out, "@const(c%d.x)\t", so->constbase.immediate + i);
332 fprintf(out, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
333 so->immediates[i].val[0],
334 so->immediates[i].val[1],
335 so->immediates[i].val[2],
336 so->immediates[i].val[3]);
337 }
338
339 disasm_a3xx(bin, so->info.sizedwords, 0, out, ir->compiler->gpu_id);
340
341 switch (so->type) {
342 case MESA_SHADER_VERTEX:
343 fprintf(out, "; %s: outputs:", type);
344 for (i = 0; i < so->outputs_count; i++) {
345 uint8_t regid = so->outputs[i].regid;
346 fprintf(out, " r%d.%c (%s)",
347 (regid >> 2), "xyzw"[regid & 0x3],
348 gl_varying_slot_name(so->outputs[i].slot));
349 }
350 fprintf(out, "\n");
351 fprintf(out, "; %s: inputs:", type);
352 for (i = 0; i < so->inputs_count; i++) {
353 uint8_t regid = so->inputs[i].regid;
354 fprintf(out, " r%d.%c (cm=%x,il=%u,b=%u)",
355 (regid >> 2), "xyzw"[regid & 0x3],
356 so->inputs[i].compmask,
357 so->inputs[i].inloc,
358 so->inputs[i].bary);
359 }
360 fprintf(out, "\n");
361 break;
362 case MESA_SHADER_FRAGMENT:
363 fprintf(out, "; %s: outputs:", type);
364 for (i = 0; i < so->outputs_count; i++) {
365 uint8_t regid = so->outputs[i].regid;
366 fprintf(out, " r%d.%c (%s)",
367 (regid >> 2), "xyzw"[regid & 0x3],
368 gl_frag_result_name(so->outputs[i].slot));
369 }
370 fprintf(out, "\n");
371 fprintf(out, "; %s: inputs:", type);
372 for (i = 0; i < so->inputs_count; i++) {
373 uint8_t regid = so->inputs[i].regid;
374 fprintf(out, " r%d.%c (%s,cm=%x,il=%u,b=%u)",
375 (regid >> 2), "xyzw"[regid & 0x3],
376 gl_varying_slot_name(so->inputs[i].slot),
377 so->inputs[i].compmask,
378 so->inputs[i].inloc,
379 so->inputs[i].bary);
380 }
381 fprintf(out, "\n");
382 break;
383 default:
384 /* TODO */
385 break;
386 }
387
388 /* print generic shader info: */
389 fprintf(out, "; %s prog %d/%d: %u instructions, %d half, %d full\n",
390 type, so->shader->id, so->id,
391 so->info.instrs_count,
392 so->info.max_half_reg + 1,
393 so->info.max_reg + 1);
394
395 fprintf(out, "; %d const, %u constlen\n",
396 so->info.max_const + 1,
397 so->constlen);
398
399 fprintf(out, "; %u (ss), %u (sy)\n", so->info.ss, so->info.sy);
400
401 fprintf(out, "; max_sun=%u\n", ir->max_sun);
402
403 /* print shader type specific info: */
404 switch (so->type) {
405 case MESA_SHADER_VERTEX:
406 dump_output(out, so, VARYING_SLOT_POS, "pos");
407 dump_output(out, so, VARYING_SLOT_PSIZ, "psize");
408 break;
409 case MESA_SHADER_FRAGMENT:
410 dump_reg(out, "pos (bary)",
411 ir3_find_sysval_regid(so, SYSTEM_VALUE_VARYING_COORD));
412 dump_output(out, so, FRAG_RESULT_DEPTH, "posz");
413 if (so->color0_mrt) {
414 dump_output(out, so, FRAG_RESULT_COLOR, "color");
415 } else {
416 dump_output(out, so, FRAG_RESULT_DATA0, "data0");
417 dump_output(out, so, FRAG_RESULT_DATA1, "data1");
418 dump_output(out, so, FRAG_RESULT_DATA2, "data2");
419 dump_output(out, so, FRAG_RESULT_DATA3, "data3");
420 dump_output(out, so, FRAG_RESULT_DATA4, "data4");
421 dump_output(out, so, FRAG_RESULT_DATA5, "data5");
422 dump_output(out, so, FRAG_RESULT_DATA6, "data6");
423 dump_output(out, so, FRAG_RESULT_DATA7, "data7");
424 }
425 /* these two are hard-coded since we don't know how to
426 * program them to anything but all 0's...
427 */
428 if (so->frag_coord)
429 fprintf(out, "; fragcoord: r0.x\n");
430 if (so->frag_face)
431 fprintf(out, "; fragface: hr0.x\n");
432 break;
433 default:
434 /* TODO */
435 break;
436 }
437
438 fprintf(out, "\n");
439 }
440
441 uint64_t
442 ir3_shader_outputs(const struct ir3_shader *so)
443 {
444 return so->nir->info.outputs_written;
445 }