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 bin
= ir3_assemble(so
->ir
, &info
);
53 if (fd_mesa_debug
& FD_DBG_DISASM
) {
54 struct ir3_block
*block
= so
->ir
->block
;
57 debug_printf("; %s: %s\n", type
, str
);
59 for (i
= 0; i
< block
->ninputs
; i
++) {
61 if (!block
->inputs
[i
])
63 regid
= block
->inputs
[i
]->regs
[0]->num
;
64 debug_printf("@in(r%d.%c)\tin%d\n",
65 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
68 for (i
= 0; i
< block
->noutputs
; i
++) {
70 if (!block
->outputs
[i
])
72 regid
= block
->outputs
[i
]->regs
[0]->num
;
73 debug_printf("@out(r%d.%c)\tout%d\n",
74 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
77 disasm_a3xx(bin
, info
.sizedwords
, 0, so
->type
);
79 debug_printf("; %s: outputs:", type
);
80 for (i
= 0; i
< so
->outputs_count
; i
++) {
81 uint8_t regid
= so
->outputs
[i
].regid
;
82 ir3_semantic sem
= so
->outputs
[i
].semantic
;
83 debug_printf(" r%d.%c (%u:%u)",
84 (regid
>> 2), "xyzw"[regid
& 0x3],
85 sem2name(sem
), sem2idx(sem
));
88 debug_printf("; %s: inputs:", type
);
89 for (i
= 0; i
< so
->inputs_count
; i
++) {
90 uint8_t regid
= so
->inputs
[i
].regid
;
91 ir3_semantic sem
= so
->inputs
[i
].semantic
;
92 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
93 (regid
>> 2), "xyzw"[regid
& 0x3],
94 sem2name(sem
), sem2idx(sem
),
95 so
->inputs
[i
].compmask
,
101 debug_printf("; %s: %u instructions, %d half, %d full\n\n",
102 type
, info
.instrs_count
, info
.max_half_reg
+ 1, info
.max_reg
+ 1);
108 read_file(const char *filename
, void **ptr
, size_t *size
)
115 fd
= open(filename
, O_RDONLY
);
117 warnx("couldn't open `%s'", filename
);
121 ret
= fstat(fd
, &st
);
123 errx(1, "couldn't stat `%s'", filename
);
126 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
127 if (*ptr
== MAP_FAILED
)
128 errx(1, "couldn't map `%s'", filename
);
135 static void reset_variant(struct ir3_shader_variant
*v
, const char *msg
)
139 v
->outputs_count
= 0;
142 v
->immediates_count
= 0;
145 static void print_usage(void)
147 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
148 printf(" --verbose - verbose compiler/debug messages\n");
149 printf(" --binning-pass - generate binning pass shader (VERT)\n");
150 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
151 printf(" --half-precision - use half-precision\n");
152 printf(" --alpha - generate render-to-alpha shader (FRAG)\n");
153 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
154 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
155 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
156 printf(" --nocp - disable copy propagation\n");
157 printf(" --help - show this message\n");
160 int main(int argc
, char **argv
)
163 const char *filename
;
164 struct tgsi_token toks
[65536];
165 struct tgsi_parse_context parse
;
166 struct ir3_shader_variant v
;
167 struct ir3_shader_key key
= {};
172 fd_mesa_debug
|= FD_DBG_DISASM
;
174 /* cmdline args which impact shader variant get spit out in a
175 * comment on the first line.. a quick/dirty way to preserve
176 * that info so when ir3test recompiles the shader with a new
177 * compiler version, we use the same shader-key settings:
179 debug_printf("; options:");
182 if (!strcmp(argv
[n
], "--verbose")) {
183 fd_mesa_debug
|= FD_DBG_OPTDUMP
| FD_DBG_MSGS
| FD_DBG_OPTMSGS
;
188 if (!strcmp(argv
[n
], "--binning-pass")) {
189 debug_printf(" %s", argv
[n
]);
190 key
.binning_pass
= true;
195 if (!strcmp(argv
[n
], "--color-two-side")) {
196 debug_printf(" %s", argv
[n
]);
197 key
.color_two_side
= true;
202 if (!strcmp(argv
[n
], "--half-precision")) {
203 debug_printf(" %s", argv
[n
]);
204 key
.half_precision
= true;
209 if (!strcmp(argv
[n
], "--alpha")) {
210 debug_printf(" %s", argv
[n
]);
216 if (!strcmp(argv
[n
], "--saturate-s")) {
217 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
218 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
223 if (!strcmp(argv
[n
], "--saturate-t")) {
224 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
225 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
230 if (!strcmp(argv
[n
], "--saturate-r")) {
231 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
232 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
237 if (!strcmp(argv
[n
], "--nocp")) {
238 fd_mesa_debug
|= FD_DBG_NOCP
;
243 if (!strcmp(argv
[n
], "--help")) {
254 memset(&v
, 0, sizeof(v
));
257 ret
= read_file(filename
, &ptr
, &size
);
263 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
264 errx(1, "could not parse `%s'", filename
);
266 tgsi_parse_init(&parse
, toks
);
267 switch (parse
.FullHeader
.Processor
.Processor
) {
268 case TGSI_PROCESSOR_FRAGMENT
:
269 v
.type
= SHADER_FRAGMENT
;
271 case TGSI_PROCESSOR_VERTEX
:
272 v
.type
= SHADER_VERTEX
;
274 case TGSI_PROCESSOR_COMPUTE
:
275 v
.type
= SHADER_COMPUTE
;
279 if (!(fd_mesa_debug
& FD_DBG_NOOPT
)) {
280 /* with new compiler: */
281 info
= "new compiler";
282 ret
= ir3_compile_shader(&v
, toks
, key
, true);
285 reset_variant(&v
, "new compiler failed, trying without copy propagation!");
286 info
= "new compiler (no copy propagation)";
287 ret
= ir3_compile_shader(&v
, toks
, key
, false);
289 reset_variant(&v
, "new compiler failed, trying fallback!\n");
294 info
= "old compiler";
295 ret
= ir3_compile_shader_old(&v
, toks
, key
);
299 fprintf(stderr
, "old compiler failed!\n");