freedreno/ir3: simplify RA
[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_info(struct ir3_shader_variant *so, const char *str)
46 {
47 struct ir3_info info;
48 uint32_t *bin;
49 const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";
50
51 // for debug, dump some before/after info:
52 // TODO make gpu_id configurable on cmdline
53 bin = ir3_assemble(so->ir, &info, 320);
54 if (fd_mesa_debug & FD_DBG_DISASM) {
55 struct ir3_block *block = so->ir->block;
56 struct ir3_register *reg;
57 uint8_t regid;
58 unsigned i;
59
60 debug_printf("; %s: %s\n", type, str);
61
62 if (block) {
63 for (i = 0; i < block->ninputs; i++) {
64 if (!block->inputs[i]) {
65 debug_printf("; in%d unused\n", i);
66 continue;
67 }
68 reg = block->inputs[i]->regs[0];
69 regid = reg->num;
70 debug_printf("@in(%sr%d.%c)\tin%d\n",
71 (reg->flags & IR3_REG_HALF) ? "h" : "",
72 (regid >> 2), "xyzw"[regid & 0x3], i);
73 }
74
75 for (i = 0; i < block->noutputs; i++) {
76 if (!block->outputs[i]) {
77 debug_printf("; out%d unused\n", i);
78 continue;
79 }
80 /* kill shows up as a virtual output.. skip it! */
81 if (is_kill(block->outputs[i]))
82 continue;
83 reg = block->outputs[i]->regs[0];
84 regid = reg->num;
85 debug_printf("@out(%sr%d.%c)\tout%d\n",
86 (reg->flags & IR3_REG_HALF) ? "h" : "",
87 (regid >> 2), "xyzw"[regid & 0x3], i);
88 }
89 } else {
90 /* hack to deal w/ old compiler:
91 * TODO maybe we can just keep this path? I guess should
92 * be good enough (other than not able to deal w/ half)
93 */
94 for (i = 0; i < so->inputs_count; i++) {
95 unsigned j, regid = so->inputs[i].regid;
96 for (j = 0; j < so->inputs[i].ncomp; j++) {
97 debug_printf("@in(r%d.%c)\tin%d\n",
98 (regid >> 2), "xyzw"[regid & 0x3], (i * 4) + j);
99 regid++;
100 }
101 }
102 for (i = 0; i < so->outputs_count; i++) {
103 unsigned j, regid = so->outputs[i].regid;
104 for (j = 0; j < 4; j++) {
105 debug_printf("@out(r%d.%c)\tout%d\n",
106 (regid >> 2), "xyzw"[regid & 0x3], (i * 4) + j);
107 regid++;
108 }
109 }
110 }
111
112 disasm_a3xx(bin, 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 debug_printf("; %s: %u instructions, %d half, %d full\n\n",
137 type, info.instrs_count, info.max_half_reg + 1, info.max_reg + 1);
138 free(bin);
139 }
140
141
142 static int
143 read_file(const char *filename, void **ptr, size_t *size)
144 {
145 int fd, ret;
146 struct stat st;
147
148 *ptr = MAP_FAILED;
149
150 fd = open(filename, O_RDONLY);
151 if (fd == -1) {
152 warnx("couldn't open `%s'", filename);
153 return 1;
154 }
155
156 ret = fstat(fd, &st);
157 if (ret)
158 errx(1, "couldn't stat `%s'", filename);
159
160 *size = st.st_size;
161 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
162 if (*ptr == MAP_FAILED)
163 errx(1, "couldn't map `%s'", filename);
164
165 close(fd);
166
167 return 0;
168 }
169
170 static void reset_variant(struct ir3_shader_variant *v, const char *msg)
171 {
172 debug_error(msg);
173 v->inputs_count = 0;
174 v->outputs_count = 0;
175 v->total_in = 0;
176 v->has_samp = false;
177 v->immediates_count = 0;
178 }
179
180 static void print_usage(void)
181 {
182 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
183 printf(" --verbose - verbose compiler/debug messages\n");
184 printf(" --binning-pass - generate binning pass shader (VERT)\n");
185 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
186 printf(" --half-precision - use half-precision\n");
187 printf(" --alpha - generate render-to-alpha shader (FRAG)\n");
188 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
189 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
190 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
191 printf(" --nocp - disable copy propagation\n");
192 printf(" --help - show this message\n");
193 }
194
195 int main(int argc, char **argv)
196 {
197 int ret = 0, n = 1;
198 const char *filename;
199 struct tgsi_token toks[65536];
200 struct tgsi_parse_context parse;
201 struct ir3_shader_variant v;
202 struct ir3_shader_key key = {};
203 const char *info;
204 void *ptr;
205 size_t size;
206
207 fd_mesa_debug |= FD_DBG_DISASM;
208
209 /* cmdline args which impact shader variant get spit out in a
210 * comment on the first line.. a quick/dirty way to preserve
211 * that info so when ir3test recompiles the shader with a new
212 * compiler version, we use the same shader-key settings:
213 */
214 debug_printf("; options:");
215
216 while (n < argc) {
217 if (!strcmp(argv[n], "--verbose")) {
218 fd_mesa_debug |= FD_DBG_OPTDUMP | FD_DBG_MSGS | FD_DBG_OPTMSGS;
219 n++;
220 continue;
221 }
222
223 if (!strcmp(argv[n], "--binning-pass")) {
224 debug_printf(" %s", argv[n]);
225 key.binning_pass = true;
226 n++;
227 continue;
228 }
229
230 if (!strcmp(argv[n], "--color-two-side")) {
231 debug_printf(" %s", argv[n]);
232 key.color_two_side = true;
233 n++;
234 continue;
235 }
236
237 if (!strcmp(argv[n], "--half-precision")) {
238 debug_printf(" %s", argv[n]);
239 key.half_precision = true;
240 n++;
241 continue;
242 }
243
244 if (!strcmp(argv[n], "--alpha")) {
245 debug_printf(" %s", argv[n]);
246 key.alpha = true;
247 n++;
248 continue;
249 }
250
251 if (!strcmp(argv[n], "--saturate-s")) {
252 debug_printf(" %s %s", argv[n], argv[n+1]);
253 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
254 n += 2;
255 continue;
256 }
257
258 if (!strcmp(argv[n], "--saturate-t")) {
259 debug_printf(" %s %s", argv[n], argv[n+1]);
260 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
261 n += 2;
262 continue;
263 }
264
265 if (!strcmp(argv[n], "--saturate-r")) {
266 debug_printf(" %s %s", argv[n], argv[n+1]);
267 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
268 n += 2;
269 continue;
270 }
271
272 if (!strcmp(argv[n], "--nocp")) {
273 fd_mesa_debug |= FD_DBG_NOCP;
274 n++;
275 continue;
276 }
277
278 if (!strcmp(argv[n], "--help")) {
279 print_usage();
280 return 0;
281 }
282
283 break;
284 }
285 debug_printf("\n");
286
287 filename = argv[n];
288
289 memset(&v, 0, sizeof(v));
290 v.key = key;
291
292 ret = read_file(filename, &ptr, &size);
293 if (ret) {
294 print_usage();
295 return ret;
296 }
297
298 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
299 errx(1, "could not parse `%s'", filename);
300
301 tgsi_parse_init(&parse, toks);
302 switch (parse.FullHeader.Processor.Processor) {
303 case TGSI_PROCESSOR_FRAGMENT:
304 v.type = SHADER_FRAGMENT;
305 break;
306 case TGSI_PROCESSOR_VERTEX:
307 v.type = SHADER_VERTEX;
308 break;
309 case TGSI_PROCESSOR_COMPUTE:
310 v.type = SHADER_COMPUTE;
311 break;
312 }
313
314 if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
315 /* with new compiler: */
316 info = "new compiler";
317 ret = ir3_compile_shader(&v, toks, key, true);
318
319 if (ret) {
320 reset_variant(&v, "new compiler failed, trying without copy propagation!");
321 info = "new compiler (no copy propagation)";
322 ret = ir3_compile_shader(&v, toks, key, false);
323 if (ret)
324 reset_variant(&v, "new compiler failed, trying fallback!\n");
325 }
326 }
327
328 if (ret) {
329 info = "old compiler";
330 ret = ir3_compile_shader_old(&v, toks, key);
331 }
332
333 if (ret) {
334 fprintf(stderr, "old compiler failed!\n");
335 return ret;
336 }
337 dump_info(&v, info);
338 }