freedreno/ir3: redirectable ir3 disasm 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 <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 "ir3_nir.h"
44 #include "instr-a3xx.h"
45 #include "ir3.h"
46
47 #include "compiler/glsl/standalone.h"
48 #include "compiler/glsl/glsl_to_nir.h"
49 #include "compiler/glsl/gl_nir.h"
50 #include "compiler/nir_types.h"
51 #include "compiler/spirv/nir_spirv.h"
52
53 static void dump_info(struct ir3_shader_variant *so, const char *str)
54 {
55 uint32_t *bin;
56 const char *type = ir3_shader_stage(so->shader);
57 bin = ir3_shader_assemble(so, so->shader->compiler->gpu_id);
58 debug_printf("; %s: %s\n", type, str);
59 ir3_shader_disasm(so, bin, stdout);
60 free(bin);
61 }
62
63 static void
64 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
65 {
66 nir_foreach_variable(var, var_list) {
67 if (var->data.location > new_var->data.location) {
68 exec_node_insert_node_before(&var->node, &new_var->node);
69 return;
70 }
71 }
72 exec_list_push_tail(var_list, &new_var->node);
73 }
74
75 static void
76 sort_varyings(struct exec_list *var_list)
77 {
78 struct exec_list new_list;
79 exec_list_make_empty(&new_list);
80 nir_foreach_variable_safe(var, var_list) {
81 exec_node_remove(&var->node);
82 insert_sorted(&new_list, var);
83 }
84 exec_list_move_nodes_to(&new_list, var_list);
85 }
86
87 static void
88 fixup_varying_slots(struct exec_list *var_list)
89 {
90 nir_foreach_variable(var, var_list) {
91 if (var->data.location >= VARYING_SLOT_VAR0) {
92 var->data.location += 9;
93 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
94 (var->data.location <= VARYING_SLOT_TEX7)) {
95 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
96 }
97 }
98 }
99
100 static struct ir3_compiler *compiler;
101
102 static nir_shader *
103 load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
104 {
105 static const struct standalone_options options = {
106 .glsl_version = 140,
107 .do_link = true,
108 };
109 struct gl_shader_program *prog;
110
111 prog = standalone_compile_shader(&options, num_files, files);
112 if (!prog)
113 errx(1, "couldn't parse `%s'", files[0]);
114
115 nir_shader *nir = glsl_to_nir(prog, stage, ir3_get_compiler_options(compiler));
116
117 /* required NIR passes: */
118 /* TODO cmdline args for some of the conditional lowering passes? */
119
120 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
121 nir_shader_get_entrypoint(nir),
122 true, true);
123 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
124 NIR_PASS_V(nir, nir_split_var_copies);
125 NIR_PASS_V(nir, nir_lower_var_copies);
126
127 NIR_PASS_V(nir, nir_split_var_copies);
128 NIR_PASS_V(nir, nir_lower_var_copies);
129
130 switch (stage) {
131 case MESA_SHADER_VERTEX:
132 nir_assign_var_locations(&nir->inputs,
133 &nir->num_inputs,
134 ir3_glsl_type_size);
135
136 /* Re-lower global vars, to deal with any dead VS inputs. */
137 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
138
139 sort_varyings(&nir->outputs);
140 nir_assign_var_locations(&nir->outputs,
141 &nir->num_outputs,
142 ir3_glsl_type_size);
143 fixup_varying_slots(&nir->outputs);
144 break;
145 case MESA_SHADER_FRAGMENT:
146 sort_varyings(&nir->inputs);
147 nir_assign_var_locations(&nir->inputs,
148 &nir->num_inputs,
149 ir3_glsl_type_size);
150 fixup_varying_slots(&nir->inputs);
151 nir_assign_var_locations(&nir->outputs,
152 &nir->num_outputs,
153 ir3_glsl_type_size);
154 break;
155 default:
156 errx(1, "unhandled shader stage: %d", stage);
157 }
158
159 nir_assign_var_locations(&nir->uniforms,
160 &nir->num_uniforms,
161 ir3_glsl_type_size);
162
163 NIR_PASS_V(nir, nir_lower_system_values);
164 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
165 NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
166
167 return nir;
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 debug_func(void *priv, enum nir_spirv_debug_level level,
199 size_t spirv_offset, const char *message)
200 {
201 // printf("%s\n", message);
202 }
203
204 static nir_shader *
205 load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
206 {
207 const struct spirv_to_nir_options spirv_options = {
208 /* these caps are just make-believe */
209 .caps = {
210 .draw_parameters = true,
211 .float64 = true,
212 .image_read_without_format = true,
213 .image_write_without_format = true,
214 .int64 = true,
215 .variable_pointers = true,
216 },
217 .lower_workgroup_access_to_offsets = true,
218 .debug = {
219 .func = debug_func,
220 }
221 };
222 nir_function *entry_point;
223 void *buf;
224 size_t size;
225
226 read_file(filename, &buf, &size);
227
228 entry_point = spirv_to_nir(buf, size / 4,
229 NULL, 0, /* spec_entries */
230 stage, entry,
231 &spirv_options,
232 ir3_get_compiler_options(compiler));
233
234 nir_print_shader(entry_point->shader, stdout);
235
236 return entry_point->shader;
237 }
238
239 static void print_usage(void)
240 {
241 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
242 printf(" --verbose - verbose compiler/debug messages\n");
243 printf(" --binning-pass - generate binning pass shader (VERT)\n");
244 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
245 printf(" --half-precision - use half-precision\n");
246 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
247 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
248 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
249 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
250 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
251 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
252 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
253 printf(" --help - show this message\n");
254 }
255
256 int main(int argc, char **argv)
257 {
258 int ret = 0, n = 1;
259 char *filenames[2];
260 int num_files = 0;
261 unsigned stage = 0;
262 struct ir3_shader_variant v;
263 struct ir3_shader s;
264 struct ir3_shader_key key = {};
265 /* TODO cmdline option to target different gpus: */
266 unsigned gpu_id = 320;
267 const char *info;
268 const char *entry;
269 void *ptr;
270 bool from_spirv = false;
271 size_t size;
272
273 memset(&s, 0, sizeof(s));
274 memset(&v, 0, sizeof(v));
275
276 /* cmdline args which impact shader variant get spit out in a
277 * comment on the first line.. a quick/dirty way to preserve
278 * that info so when ir3test recompiles the shader with a new
279 * compiler version, we use the same shader-key settings:
280 */
281 debug_printf("; options:");
282
283 while (n < argc) {
284 if (!strcmp(argv[n], "--verbose")) {
285 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
286 n++;
287 continue;
288 }
289
290 if (!strcmp(argv[n], "--binning-pass")) {
291 debug_printf(" %s", argv[n]);
292 key.binning_pass = true;
293 n++;
294 continue;
295 }
296
297 if (!strcmp(argv[n], "--color-two-side")) {
298 debug_printf(" %s", argv[n]);
299 key.color_two_side = true;
300 n++;
301 continue;
302 }
303
304 if (!strcmp(argv[n], "--half-precision")) {
305 debug_printf(" %s", argv[n]);
306 key.half_precision = true;
307 n++;
308 continue;
309 }
310
311 if (!strcmp(argv[n], "--saturate-s")) {
312 debug_printf(" %s %s", argv[n], argv[n+1]);
313 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
314 n += 2;
315 continue;
316 }
317
318 if (!strcmp(argv[n], "--saturate-t")) {
319 debug_printf(" %s %s", argv[n], argv[n+1]);
320 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
321 n += 2;
322 continue;
323 }
324
325 if (!strcmp(argv[n], "--saturate-r")) {
326 debug_printf(" %s %s", argv[n], argv[n+1]);
327 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
328 n += 2;
329 continue;
330 }
331
332 if (!strcmp(argv[n], "--astc-srgb")) {
333 debug_printf(" %s %s", argv[n], argv[n+1]);
334 key.vastc_srgb = key.fastc_srgb = strtol(argv[n+1], NULL, 0);
335 n += 2;
336 continue;
337 }
338
339 if (!strcmp(argv[n], "--stream-out")) {
340 struct pipe_stream_output_info *so = &s.stream_output;
341 debug_printf(" %s", argv[n]);
342 /* TODO more dynamic config based on number of outputs, etc
343 * rather than just hard-code for first output:
344 */
345 so->num_outputs = 1;
346 so->stride[0] = 4;
347 so->output[0].register_index = 0;
348 so->output[0].start_component = 0;
349 so->output[0].num_components = 4;
350 so->output[0].output_buffer = 0;
351 so->output[0].dst_offset = 2;
352 so->output[0].stream = 0;
353 n++;
354 continue;
355 }
356
357 if (!strcmp(argv[n], "--ucp")) {
358 debug_printf(" %s %s", argv[n], argv[n+1]);
359 key.ucp_enables = strtol(argv[n+1], NULL, 0);
360 n += 2;
361 continue;
362 }
363
364 if (!strcmp(argv[n], "--gpu")) {
365 debug_printf(" %s %s", argv[n], argv[n+1]);
366 gpu_id = strtol(argv[n+1], NULL, 0);
367 n += 2;
368 continue;
369 }
370
371 if (!strcmp(argv[n], "--help")) {
372 print_usage();
373 return 0;
374 }
375
376 break;
377 }
378 debug_printf("\n");
379
380 while (n < argc) {
381 char *filename = argv[n];
382 char *ext = rindex(filename, '.');
383
384 if (strcmp(ext, ".tgsi") == 0) {
385 if (num_files != 0)
386 errx(1, "in TGSI mode, only a single file may be specified");
387 s.from_tgsi = true;
388 } else if (strcmp(ext, ".spv") == 0) {
389 if (num_files != 0)
390 errx(1, "in SPIR-V mode, only a single file may be specified");
391 stage = MESA_SHADER_COMPUTE;
392 from_spirv = true;
393 filenames[num_files++] = filename;
394 n++;
395 if (n == argc)
396 errx(1, "in SPIR-V mode, an entry point must be specified");
397 entry = argv[n];
398 n++;
399 } else if (strcmp(ext, ".frag") == 0) {
400 if (s.from_tgsi || from_spirv)
401 errx(1, "cannot mix GLSL/TGSI/SPIRV");
402 if (num_files >= ARRAY_SIZE(filenames))
403 errx(1, "too many GLSL files");
404 stage = MESA_SHADER_FRAGMENT;
405 } else if (strcmp(ext, ".vert") == 0) {
406 if (s.from_tgsi)
407 errx(1, "cannot mix GLSL and TGSI");
408 if (num_files >= ARRAY_SIZE(filenames))
409 errx(1, "too many GLSL files");
410 stage = MESA_SHADER_VERTEX;
411 } else {
412 print_usage();
413 return -1;
414 }
415
416 filenames[num_files++] = filename;
417
418 n++;
419 }
420
421 nir_shader *nir;
422
423 compiler = ir3_compiler_create(NULL, gpu_id);
424
425 if (s.from_tgsi) {
426 struct tgsi_token toks[65536];
427
428 ret = read_file(filenames[0], &ptr, &size);
429 if (ret) {
430 print_usage();
431 return ret;
432 }
433
434 if (fd_mesa_debug & FD_DBG_OPTMSGS)
435 debug_printf("%s\n", (char *)ptr);
436
437 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
438 errx(1, "could not parse `%s'", filenames[0]);
439
440 if (fd_mesa_debug & FD_DBG_OPTMSGS)
441 tgsi_dump(toks, 0);
442
443 nir = ir3_tgsi_to_nir(toks);
444 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
445 } else if (from_spirv) {
446 nir = load_spirv(filenames[0], entry, stage);
447
448 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
449 (nir_lower_io_options)0);
450
451 /* TODO do this somewhere else */
452 nir_lower_int64(nir, ~0);
453 nir_lower_system_values(nir);
454 } else if (num_files > 0) {
455 nir = load_glsl(num_files, filenames, stage);
456 } else {
457 print_usage();
458 return -1;
459 }
460
461 s.compiler = compiler;
462 s.nir = ir3_optimize_nir(&s, nir, NULL);
463
464 v.key = key;
465 v.shader = &s;
466
467 switch (nir->info.stage) {
468 case MESA_SHADER_FRAGMENT:
469 s.type = v.type = SHADER_FRAGMENT;
470 break;
471 case MESA_SHADER_VERTEX:
472 s.type = v.type = SHADER_VERTEX;
473 break;
474 case MESA_SHADER_COMPUTE:
475 s.type = v.type = SHADER_COMPUTE;
476 break;
477 default:
478 errx(1, "unhandled shader stage: %d", nir->info.stage);
479 }
480
481 info = "NIR compiler";
482 ret = ir3_compile_shader_nir(s.compiler, &v);
483 if (ret) {
484 fprintf(stderr, "compiler failed!\n");
485 return ret;
486 }
487 dump_info(&v, info);
488 }