2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include <sys/types.h>
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_text.h"
37 #include "tgsi/tgsi_dump.h"
39 #include "freedreno_util.h"
41 #include "ir3_compiler.h"
42 #include "instr-a3xx.h"
45 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
49 const char *type
= (so
->type
== SHADER_VERTEX
) ? "VERT" : "FRAG";
51 // for debug, dump some before/after info:
52 // TODO make gpu_id configurable on cmdline
53 bin
= ir3_assemble(so
->ir
, &info
, 320);
54 if (fd_mesa_debug
& FD_DBG_DISASM
) {
55 struct ir3_block
*block
= so
->ir
->block
;
56 struct ir3_register
*reg
;
60 debug_printf("; %s: %s\n", type
, str
);
63 for (i
= 0; i
< block
->ninputs
; i
++) {
64 if (!block
->inputs
[i
]) {
65 debug_printf("; in%d unused\n", i
);
68 reg
= block
->inputs
[i
]->regs
[0];
70 debug_printf("@in(%sr%d.%c)\tin%d\n",
71 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
72 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
75 for (i
= 0; i
< block
->noutputs
; i
++) {
76 if (!block
->outputs
[i
]) {
77 debug_printf("; out%d unused\n", i
);
80 /* kill shows up as a virtual output.. skip it! */
81 if (is_kill(block
->outputs
[i
]))
83 reg
= block
->outputs
[i
]->regs
[0];
85 debug_printf("@out(%sr%d.%c)\tout%d\n",
86 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
87 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
90 /* hack to deal w/ old compiler:
91 * TODO maybe we can just keep this path? I guess should
92 * be good enough (other than not able to deal w/ half)
94 for (i
= 0; i
< so
->inputs_count
; i
++) {
95 unsigned j
, regid
= so
->inputs
[i
].regid
;
96 for (j
= 0; j
< so
->inputs
[i
].ncomp
; j
++) {
97 debug_printf("@in(r%d.%c)\tin%d\n",
98 (regid
>> 2), "xyzw"[regid
& 0x3], (i
* 4) + j
);
102 for (i
= 0; i
< so
->outputs_count
; i
++) {
103 unsigned j
, regid
= so
->outputs
[i
].regid
;
104 for (j
= 0; j
< 4; j
++) {
105 debug_printf("@out(r%d.%c)\tout%d\n",
106 (regid
>> 2), "xyzw"[regid
& 0x3], (i
* 4) + j
);
112 disasm_a3xx(bin
, info
.sizedwords
, 0, so
->type
);
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
));
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
,
136 debug_printf("; %s: %u instructions, %d half, %d full\n\n",
137 type
, info
.instrs_count
, info
.max_half_reg
+ 1, info
.max_reg
+ 1);
143 read_file(const char *filename
, void **ptr
, size_t *size
)
150 fd
= open(filename
, O_RDONLY
);
152 warnx("couldn't open `%s'", filename
);
156 ret
= fstat(fd
, &st
);
158 errx(1, "couldn't stat `%s'", filename
);
161 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
162 if (*ptr
== MAP_FAILED
)
163 errx(1, "couldn't map `%s'", filename
);
170 static void reset_variant(struct ir3_shader_variant
*v
, const char *msg
)
174 v
->outputs_count
= 0;
177 v
->immediates_count
= 0;
180 static void print_usage(void)
182 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
183 printf(" --verbose - verbose compiler/debug messages\n");
184 printf(" --binning-pass - generate binning pass shader (VERT)\n");
185 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
186 printf(" --half-precision - use half-precision\n");
187 printf(" --alpha - generate render-to-alpha shader (FRAG)\n");
188 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
189 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
190 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
191 printf(" --nocp - disable copy propagation\n");
192 printf(" --help - show this message\n");
195 int main(int argc
, char **argv
)
198 const char *filename
;
199 struct tgsi_token toks
[65536];
200 struct tgsi_parse_context parse
;
201 struct ir3_shader_variant v
;
202 struct ir3_shader_key key
= {};
207 fd_mesa_debug
|= FD_DBG_DISASM
;
209 /* cmdline args which impact shader variant get spit out in a
210 * comment on the first line.. a quick/dirty way to preserve
211 * that info so when ir3test recompiles the shader with a new
212 * compiler version, we use the same shader-key settings:
214 debug_printf("; options:");
217 if (!strcmp(argv
[n
], "--verbose")) {
218 fd_mesa_debug
|= FD_DBG_OPTDUMP
| FD_DBG_MSGS
| FD_DBG_OPTMSGS
;
223 if (!strcmp(argv
[n
], "--binning-pass")) {
224 debug_printf(" %s", argv
[n
]);
225 key
.binning_pass
= true;
230 if (!strcmp(argv
[n
], "--color-two-side")) {
231 debug_printf(" %s", argv
[n
]);
232 key
.color_two_side
= true;
237 if (!strcmp(argv
[n
], "--half-precision")) {
238 debug_printf(" %s", argv
[n
]);
239 key
.half_precision
= true;
244 if (!strcmp(argv
[n
], "--alpha")) {
245 debug_printf(" %s", argv
[n
]);
251 if (!strcmp(argv
[n
], "--saturate-s")) {
252 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
253 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
258 if (!strcmp(argv
[n
], "--saturate-t")) {
259 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
260 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
265 if (!strcmp(argv
[n
], "--saturate-r")) {
266 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
267 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
272 if (!strcmp(argv
[n
], "--nocp")) {
273 fd_mesa_debug
|= FD_DBG_NOCP
;
278 if (!strcmp(argv
[n
], "--help")) {
289 memset(&v
, 0, sizeof(v
));
292 ret
= read_file(filename
, &ptr
, &size
);
298 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
299 errx(1, "could not parse `%s'", filename
);
301 tgsi_parse_init(&parse
, toks
);
302 switch (parse
.FullHeader
.Processor
.Processor
) {
303 case TGSI_PROCESSOR_FRAGMENT
:
304 v
.type
= SHADER_FRAGMENT
;
306 case TGSI_PROCESSOR_VERTEX
:
307 v
.type
= SHADER_VERTEX
;
309 case TGSI_PROCESSOR_COMPUTE
:
310 v
.type
= SHADER_COMPUTE
;
314 if (!(fd_mesa_debug
& FD_DBG_NOOPT
)) {
315 /* with new compiler: */
316 info
= "new compiler";
317 ret
= ir3_compile_shader(&v
, toks
, key
, true);
320 reset_variant(&v
, "new compiler failed, trying without copy propagation!");
321 info
= "new compiler (no copy propagation)";
322 ret
= ir3_compile_shader(&v
, toks
, key
, false);
324 reset_variant(&v
, "new compiler failed, trying fallback!\n");
329 info
= "old compiler";
330 ret
= ir3_compile_shader_old(&v
, toks
, key
);
334 fprintf(stderr
, "old compiler failed!\n");