freedreno/ir3: drop dot graph dumping
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cmdline.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 <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <err.h>
35
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 #include "tgsi/tgsi_dump.h"
39
40 #include "freedreno_util.h"
41
42 #include "ir3_compiler.h"
43 #include "instr-a3xx.h"
44 #include "ir3.h"
45
46 static void dump_reg(const char *name, uint32_t r)
47 {
48 if (r != regid(63,0))
49 debug_printf("; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
50 }
51
52 static void dump_semantic(struct ir3_shader_variant *so,
53 unsigned sem, const char *name)
54 {
55 uint32_t regid;
56 regid = ir3_find_output_regid(so, ir3_semantic_name(sem, 0));
57 dump_reg(name, regid);
58 }
59
60 static void dump_info(struct ir3_shader_variant *so, const char *str)
61 {
62 uint32_t *bin;
63 const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";
64
65 // for debug, dump some before/after info:
66 // TODO make gpu_id configurable on cmdline
67 bin = ir3_shader_assemble(so, 320);
68 if (fd_mesa_debug & FD_DBG_DISASM) {
69 struct ir3_block *block = so->ir->block;
70 struct ir3_register *reg;
71 uint8_t regid;
72 unsigned i;
73
74 debug_printf("; %s: %s\n", type, str);
75
76 for (i = 0; i < block->ninputs; i++) {
77 if (!block->inputs[i]) {
78 debug_printf("; in%d unused\n", i);
79 continue;
80 }
81 reg = block->inputs[i]->regs[0];
82 regid = reg->num;
83 debug_printf("@in(%sr%d.%c)\tin%d\n",
84 (reg->flags & IR3_REG_HALF) ? "h" : "",
85 (regid >> 2), "xyzw"[regid & 0x3], i);
86 }
87
88 for (i = 0; i < block->noutputs; i++) {
89 if (!block->outputs[i]) {
90 debug_printf("; out%d unused\n", i);
91 continue;
92 }
93 /* kill shows up as a virtual output.. skip it! */
94 if (is_kill(block->outputs[i]))
95 continue;
96 reg = block->outputs[i]->regs[0];
97 regid = reg->num;
98 debug_printf("@out(%sr%d.%c)\tout%d\n",
99 (reg->flags & IR3_REG_HALF) ? "h" : "",
100 (regid >> 2), "xyzw"[regid & 0x3], i);
101 }
102
103 for (i = 0; i < so->immediates_count; i++) {
104 debug_printf("@const(c%d.x)\t", so->first_immediate + i);
105 debug_printf("0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
106 so->immediates[i].val[0],
107 so->immediates[i].val[1],
108 so->immediates[i].val[2],
109 so->immediates[i].val[3]);
110 }
111
112 disasm_a3xx(bin, so->info.sizedwords, 0, so->type);
113
114 debug_printf("; %s: outputs:", type);
115 for (i = 0; i < so->outputs_count; i++) {
116 uint8_t regid = so->outputs[i].regid;
117 ir3_semantic sem = so->outputs[i].semantic;
118 debug_printf(" r%d.%c (%u:%u)",
119 (regid >> 2), "xyzw"[regid & 0x3],
120 sem2name(sem), sem2idx(sem));
121 }
122 debug_printf("\n");
123 debug_printf("; %s: inputs:", type);
124 for (i = 0; i < so->inputs_count; i++) {
125 uint8_t regid = so->inputs[i].regid;
126 ir3_semantic sem = so->inputs[i].semantic;
127 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
128 (regid >> 2), "xyzw"[regid & 0x3],
129 sem2name(sem), sem2idx(sem),
130 so->inputs[i].compmask,
131 so->inputs[i].inloc,
132 so->inputs[i].bary);
133 }
134 debug_printf("\n");
135 }
136
137 /* print generic shader info: */
138 debug_printf("; %s: %u instructions, %d half, %d full\n", type,
139 so->info.instrs_count,
140 so->info.max_half_reg + 1,
141 so->info.max_reg + 1);
142
143 /* print shader type specific info: */
144 switch (so->type) {
145 case SHADER_VERTEX:
146 dump_semantic(so, TGSI_SEMANTIC_POSITION, "pos");
147 dump_semantic(so, TGSI_SEMANTIC_PSIZE, "psize");
148 break;
149 case SHADER_FRAGMENT:
150 dump_reg("pos (bary)", so->pos_regid);
151 dump_semantic(so, TGSI_SEMANTIC_POSITION, "posz");
152 dump_semantic(so, TGSI_SEMANTIC_COLOR, "color");
153 /* these two are hard-coded since we don't know how to
154 * program them to anything but all 0's...
155 */
156 if (so->frag_coord)
157 debug_printf("; fragcoord: r0.x\n");
158 if (so->frag_face)
159 debug_printf("; fragface: hr0.x\n");
160 break;
161 case SHADER_COMPUTE:
162 break;
163 }
164 free(bin);
165
166 debug_printf("\n");
167 }
168
169
170 static int
171 read_file(const char *filename, void **ptr, size_t *size)
172 {
173 int fd, ret;
174 struct stat st;
175
176 *ptr = MAP_FAILED;
177
178 fd = open(filename, O_RDONLY);
179 if (fd == -1) {
180 warnx("couldn't open `%s'", filename);
181 return 1;
182 }
183
184 ret = fstat(fd, &st);
185 if (ret)
186 errx(1, "couldn't stat `%s'", filename);
187
188 *size = st.st_size;
189 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
190 if (*ptr == MAP_FAILED)
191 errx(1, "couldn't map `%s'", filename);
192
193 close(fd);
194
195 return 0;
196 }
197
198 static void reset_variant(struct ir3_shader_variant *v, const char *msg)
199 {
200 printf("; %s\n", msg);
201 v->inputs_count = 0;
202 v->outputs_count = 0;
203 v->total_in = 0;
204 v->has_samp = false;
205 v->immediates_count = 0;
206 }
207
208 static void print_usage(void)
209 {
210 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
211 printf(" --verbose - verbose compiler/debug messages\n");
212 printf(" --binning-pass - generate binning pass shader (VERT)\n");
213 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
214 printf(" --half-precision - use half-precision\n");
215 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
216 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
217 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
218 printf(" --nocp - disable copy propagation\n");
219 printf(" --nir - use NIR compiler\n");
220 printf(" --help - show this message\n");
221 }
222
223 int main(int argc, char **argv)
224 {
225 int ret = 0, n = 1;
226 const char *filename;
227 struct tgsi_token toks[65536];
228 struct tgsi_parse_context parse;
229 struct ir3_shader_variant v;
230 struct ir3_shader_key key = {};
231 const char *info;
232 void *ptr;
233 size_t size;
234 int use_nir = 0;
235
236 fd_mesa_debug |= FD_DBG_DISASM;
237
238 /* cmdline args which impact shader variant get spit out in a
239 * comment on the first line.. a quick/dirty way to preserve
240 * that info so when ir3test recompiles the shader with a new
241 * compiler version, we use the same shader-key settings:
242 */
243 debug_printf("; options:");
244
245 while (n < argc) {
246 if (!strcmp(argv[n], "--verbose")) {
247 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS;
248 n++;
249 continue;
250 }
251
252 if (!strcmp(argv[n], "--binning-pass")) {
253 debug_printf(" %s", argv[n]);
254 key.binning_pass = true;
255 n++;
256 continue;
257 }
258
259 if (!strcmp(argv[n], "--color-two-side")) {
260 debug_printf(" %s", argv[n]);
261 key.color_two_side = true;
262 n++;
263 continue;
264 }
265
266 if (!strcmp(argv[n], "--half-precision")) {
267 debug_printf(" %s", argv[n]);
268 key.half_precision = true;
269 n++;
270 continue;
271 }
272
273 if (!strcmp(argv[n], "--saturate-s")) {
274 debug_printf(" %s %s", argv[n], argv[n+1]);
275 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
276 n += 2;
277 continue;
278 }
279
280 if (!strcmp(argv[n], "--saturate-t")) {
281 debug_printf(" %s %s", argv[n], argv[n+1]);
282 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
283 n += 2;
284 continue;
285 }
286
287 if (!strcmp(argv[n], "--saturate-r")) {
288 debug_printf(" %s %s", argv[n], argv[n+1]);
289 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
290 n += 2;
291 continue;
292 }
293
294 if (!strcmp(argv[n], "--nocp")) {
295 fd_mesa_debug |= FD_DBG_NOCP;
296 n++;
297 continue;
298 }
299 if (!strcmp(argv[n], "--nir")) {
300 use_nir = true;
301 n++;
302 continue;
303 }
304
305 if (!strcmp(argv[n], "--help")) {
306 print_usage();
307 return 0;
308 }
309
310 break;
311 }
312 debug_printf("\n");
313
314 filename = argv[n];
315
316 memset(&v, 0, sizeof(v));
317 v.key = key;
318
319 ret = read_file(filename, &ptr, &size);
320 if (ret) {
321 print_usage();
322 return ret;
323 }
324
325 if (fd_mesa_debug & FD_DBG_OPTMSGS)
326 debug_printf("%s\n", (char *)ptr);
327
328 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
329 errx(1, "could not parse `%s'", filename);
330
331 tgsi_parse_init(&parse, toks);
332 switch (parse.FullHeader.Processor.Processor) {
333 case TGSI_PROCESSOR_FRAGMENT:
334 v.type = SHADER_FRAGMENT;
335 break;
336 case TGSI_PROCESSOR_VERTEX:
337 v.type = SHADER_VERTEX;
338 break;
339 case TGSI_PROCESSOR_COMPUTE:
340 v.type = SHADER_COMPUTE;
341 break;
342 }
343
344 if (use_nir) {
345 info = "NIR compiler";
346 ret = ir3_compile_shader_nir(&v, toks, key);
347 } else {
348 info = "TGSI compiler";
349 ret = ir3_compile_shader(&v, toks, key, true);
350 }
351
352 if (ret) {
353 reset_variant(&v, "compiler failed, trying without copy propagation!");
354 info = "compiler (no copy propagation)";
355 ret = ir3_compile_shader(&v, toks, key, false);
356 }
357
358 if (ret) {
359 fprintf(stderr, "compiler failed!\n");
360 return ret;
361 }
362 dump_info(&v, info);
363 }