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