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