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