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