freedreno/ir3: add Sethi–Ullman numbering pass
[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 /* do first pass optimization, ignoring the key: */
266 shader->nir = ir3_optimize_nir(shader, nir, NULL);
267 if (ir3_shader_debug & IR3_DBG_DISASM) {
268 printf("dump nir%d: type=%d", shader->id, shader->type);
269 nir_print_shader(shader->nir, stdout);
270 }
271
272 return shader;
273 }
274
275 static void dump_reg(FILE *out, const char *name, uint32_t r)
276 {
277 if (r != regid(63,0))
278 fprintf(out, "; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
279 }
280
281 static void dump_output(FILE *out, struct ir3_shader_variant *so,
282 unsigned slot, const char *name)
283 {
284 uint32_t regid;
285 regid = ir3_find_output_regid(so, slot);
286 dump_reg(out, name, regid);
287 }
288
289 void
290 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out)
291 {
292 struct ir3 *ir = so->ir;
293 struct ir3_register *reg;
294 const char *type = ir3_shader_stage(so->shader);
295 uint8_t regid;
296 unsigned i;
297
298 for (i = 0; i < ir->ninputs; i++) {
299 if (!ir->inputs[i]) {
300 fprintf(out, "; in%d unused\n", i);
301 continue;
302 }
303 reg = ir->inputs[i]->regs[0];
304 regid = reg->num;
305 fprintf(out, "@in(%sr%d.%c)\tin%d\n",
306 (reg->flags & IR3_REG_HALF) ? "h" : "",
307 (regid >> 2), "xyzw"[regid & 0x3], i);
308 }
309
310 for (i = 0; i < ir->noutputs; i++) {
311 if (!ir->outputs[i]) {
312 fprintf(out, "; out%d unused\n", i);
313 continue;
314 }
315 /* kill shows up as a virtual output.. skip it! */
316 if (is_kill(ir->outputs[i]))
317 continue;
318 reg = ir->outputs[i]->regs[0];
319 regid = reg->num;
320 fprintf(out, "@out(%sr%d.%c)\tout%d\n",
321 (reg->flags & IR3_REG_HALF) ? "h" : "",
322 (regid >> 2), "xyzw"[regid & 0x3], i);
323 }
324
325 for (i = 0; i < so->immediates_count; i++) {
326 fprintf(out, "@const(c%d.x)\t", so->constbase.immediate + i);
327 fprintf(out, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
328 so->immediates[i].val[0],
329 so->immediates[i].val[1],
330 so->immediates[i].val[2],
331 so->immediates[i].val[3]);
332 }
333
334 disasm_a3xx(bin, so->info.sizedwords, 0, out, ir->compiler->gpu_id);
335
336 switch (so->type) {
337 case MESA_SHADER_VERTEX:
338 fprintf(out, "; %s: outputs:", type);
339 for (i = 0; i < so->outputs_count; i++) {
340 uint8_t regid = so->outputs[i].regid;
341 fprintf(out, " r%d.%c (%s)",
342 (regid >> 2), "xyzw"[regid & 0x3],
343 gl_varying_slot_name(so->outputs[i].slot));
344 }
345 fprintf(out, "\n");
346 fprintf(out, "; %s: inputs:", type);
347 for (i = 0; i < so->inputs_count; i++) {
348 uint8_t regid = so->inputs[i].regid;
349 fprintf(out, " r%d.%c (cm=%x,il=%u,b=%u)",
350 (regid >> 2), "xyzw"[regid & 0x3],
351 so->inputs[i].compmask,
352 so->inputs[i].inloc,
353 so->inputs[i].bary);
354 }
355 fprintf(out, "\n");
356 break;
357 case MESA_SHADER_FRAGMENT:
358 fprintf(out, "; %s: outputs:", type);
359 for (i = 0; i < so->outputs_count; i++) {
360 uint8_t regid = so->outputs[i].regid;
361 fprintf(out, " r%d.%c (%s)",
362 (regid >> 2), "xyzw"[regid & 0x3],
363 gl_frag_result_name(so->outputs[i].slot));
364 }
365 fprintf(out, "\n");
366 fprintf(out, "; %s: inputs:", type);
367 for (i = 0; i < so->inputs_count; i++) {
368 uint8_t regid = so->inputs[i].regid;
369 fprintf(out, " r%d.%c (%s,cm=%x,il=%u,b=%u)",
370 (regid >> 2), "xyzw"[regid & 0x3],
371 gl_varying_slot_name(so->inputs[i].slot),
372 so->inputs[i].compmask,
373 so->inputs[i].inloc,
374 so->inputs[i].bary);
375 }
376 fprintf(out, "\n");
377 break;
378 default:
379 /* TODO */
380 break;
381 }
382
383 /* print generic shader info: */
384 fprintf(out, "; %s prog %d/%d: %u instructions, %d half, %d full\n",
385 type, so->shader->id, so->id,
386 so->info.instrs_count,
387 so->info.max_half_reg + 1,
388 so->info.max_reg + 1);
389
390 fprintf(out, "; %d const, %u constlen\n",
391 so->info.max_const + 1,
392 so->constlen);
393
394 fprintf(out, "; %u (ss), %u (sy)\n", so->info.ss, so->info.sy);
395
396 fprintf(out, "; max_sun=%u\n", ir->max_sun);
397
398 /* print shader type specific info: */
399 switch (so->type) {
400 case MESA_SHADER_VERTEX:
401 dump_output(out, so, VARYING_SLOT_POS, "pos");
402 dump_output(out, so, VARYING_SLOT_PSIZ, "psize");
403 break;
404 case MESA_SHADER_FRAGMENT:
405 dump_reg(out, "pos (bary)",
406 ir3_find_sysval_regid(so, SYSTEM_VALUE_VARYING_COORD));
407 dump_output(out, so, FRAG_RESULT_DEPTH, "posz");
408 if (so->color0_mrt) {
409 dump_output(out, so, FRAG_RESULT_COLOR, "color");
410 } else {
411 dump_output(out, so, FRAG_RESULT_DATA0, "data0");
412 dump_output(out, so, FRAG_RESULT_DATA1, "data1");
413 dump_output(out, so, FRAG_RESULT_DATA2, "data2");
414 dump_output(out, so, FRAG_RESULT_DATA3, "data3");
415 dump_output(out, so, FRAG_RESULT_DATA4, "data4");
416 dump_output(out, so, FRAG_RESULT_DATA5, "data5");
417 dump_output(out, so, FRAG_RESULT_DATA6, "data6");
418 dump_output(out, so, FRAG_RESULT_DATA7, "data7");
419 }
420 /* these two are hard-coded since we don't know how to
421 * program them to anything but all 0's...
422 */
423 if (so->frag_coord)
424 fprintf(out, "; fragcoord: r0.x\n");
425 if (so->frag_face)
426 fprintf(out, "; fragface: hr0.x\n");
427 break;
428 default:
429 /* TODO */
430 break;
431 }
432
433 fprintf(out, "\n");
434 }
435
436 uint64_t
437 ir3_shader_outputs(const struct ir3_shader *so)
438 {
439 return so->nir->info.outputs_written;
440 }