freedreno/ir3: dump nocp option
[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 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(" --nir - use NIR compiler\n");
210 printf(" --help - show this message\n");
211 }
212
213 int main(int argc, char **argv)
214 {
215 int ret = 0, n = 1;
216 const char *filename;
217 struct tgsi_token toks[65536];
218 struct tgsi_parse_context parse;
219 struct ir3_shader_variant v;
220 struct ir3_shader_key key = {};
221 const char *info;
222 void *ptr;
223 size_t size;
224
225 fd_mesa_debug |= FD_DBG_DISASM;
226
227 /* cmdline args which impact shader variant get spit out in a
228 * comment on the first line.. a quick/dirty way to preserve
229 * that info so when ir3test recompiles the shader with a new
230 * compiler version, we use the same shader-key settings:
231 */
232 debug_printf("; options:");
233
234 while (n < argc) {
235 if (!strcmp(argv[n], "--verbose")) {
236 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS;
237 n++;
238 continue;
239 }
240
241 if (!strcmp(argv[n], "--binning-pass")) {
242 debug_printf(" %s", argv[n]);
243 key.binning_pass = true;
244 n++;
245 continue;
246 }
247
248 if (!strcmp(argv[n], "--color-two-side")) {
249 debug_printf(" %s", argv[n]);
250 key.color_two_side = true;
251 n++;
252 continue;
253 }
254
255 if (!strcmp(argv[n], "--half-precision")) {
256 debug_printf(" %s", argv[n]);
257 key.half_precision = true;
258 n++;
259 continue;
260 }
261
262 if (!strcmp(argv[n], "--saturate-s")) {
263 debug_printf(" %s %s", argv[n], argv[n+1]);
264 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
265 n += 2;
266 continue;
267 }
268
269 if (!strcmp(argv[n], "--saturate-t")) {
270 debug_printf(" %s %s", argv[n], argv[n+1]);
271 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
272 n += 2;
273 continue;
274 }
275
276 if (!strcmp(argv[n], "--saturate-r")) {
277 debug_printf(" %s %s", argv[n], argv[n+1]);
278 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
279 n += 2;
280 continue;
281 }
282
283 if (!strcmp(argv[n], "--help")) {
284 print_usage();
285 return 0;
286 }
287
288 break;
289 }
290 debug_printf("\n");
291
292 filename = argv[n];
293
294 memset(&v, 0, sizeof(v));
295 v.key = key;
296
297 ret = read_file(filename, &ptr, &size);
298 if (ret) {
299 print_usage();
300 return ret;
301 }
302
303 if (fd_mesa_debug & FD_DBG_OPTMSGS)
304 debug_printf("%s\n", (char *)ptr);
305
306 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
307 errx(1, "could not parse `%s'", filename);
308
309 tgsi_parse_init(&parse, toks);
310 switch (parse.FullHeader.Processor.Processor) {
311 case TGSI_PROCESSOR_FRAGMENT:
312 v.type = SHADER_FRAGMENT;
313 break;
314 case TGSI_PROCESSOR_VERTEX:
315 v.type = SHADER_VERTEX;
316 break;
317 case TGSI_PROCESSOR_COMPUTE:
318 v.type = SHADER_COMPUTE;
319 break;
320 }
321
322 info = "NIR compiler";
323 ret = ir3_compile_shader_nir(&v, toks, key);
324 if (ret) {
325 fprintf(stderr, "compiler failed!\n");
326 return ret;
327 }
328 dump_info(&v, info);
329 }