e42afeaeb21805ff72295f2a798b8c3bf4a1c92a
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>
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 #include "tgsi/tgsi_dump.h"
40 #include "freedreno_util.h"
42 #include "ir3_compiler.h"
43 #include "instr-a3xx.h"
46 static void dump_reg(const char *name
, uint32_t r
)
49 debug_printf("; %s: r%d.%c\n", name
, r
>> 2, "xyzw"[r
& 0x3]);
52 static void dump_semantic(struct ir3_shader_variant
*so
,
53 unsigned sem
, const char *name
)
56 regid
= ir3_find_output_regid(so
, ir3_semantic_name(sem
, 0));
57 dump_reg(name
, regid
);
60 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
63 const char *type
= (so
->type
== SHADER_VERTEX
) ? "VERT" : "FRAG";
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
;
74 debug_printf("; %s: %s\n", type
, str
);
76 for (i
= 0; i
< block
->ninputs
; i
++) {
77 if (!block
->inputs
[i
]) {
78 debug_printf("; in%d unused\n", i
);
81 reg
= block
->inputs
[i
]->regs
[0];
83 debug_printf("@in(%sr%d.%c)\tin%d\n",
84 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
85 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
88 for (i
= 0; i
< block
->noutputs
; i
++) {
89 if (!block
->outputs
[i
]) {
90 debug_printf("; out%d unused\n", i
);
93 /* kill shows up as a virtual output.. skip it! */
94 if (is_kill(block
->outputs
[i
]))
96 reg
= block
->outputs
[i
]->regs
[0];
98 debug_printf("@out(%sr%d.%c)\tout%d\n",
99 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
100 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
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]);
112 disasm_a3xx(bin
, so
->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
,
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);
143 /* print shader type specific info: */
146 dump_semantic(so
, TGSI_SEMANTIC_POSITION
, "pos");
147 dump_semantic(so
, TGSI_SEMANTIC_PSIZE
, "psize");
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...
157 debug_printf("; fragcoord: r0.x\n");
159 debug_printf("; fragface: hr0.x\n");
171 read_file(const char *filename
, void **ptr
, size_t *size
)
178 fd
= open(filename
, O_RDONLY
);
180 warnx("couldn't open `%s'", filename
);
184 ret
= fstat(fd
, &st
);
186 errx(1, "couldn't stat `%s'", filename
);
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
);
198 static void reset_variant(struct ir3_shader_variant
*v
, const char *msg
)
200 printf("; %s\n", msg
);
202 v
->outputs_count
= 0;
205 v
->immediates_count
= 0;
208 static void print_usage(void)
210 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
211 printf(" --verbose - verbose compiler/debug messages\n");
212 printf(" --binning-pass - generate binning pass shader (VERT)\n");
213 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
214 printf(" --half-precision - use half-precision\n");
215 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
216 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
217 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
218 printf(" --nocp - disable copy propagation\n");
219 printf(" --nir - use NIR compiler\n");
220 printf(" --help - show this message\n");
223 int main(int argc
, char **argv
)
226 const char *filename
;
227 struct tgsi_token toks
[65536];
228 struct tgsi_parse_context parse
;
229 struct ir3_shader_variant v
;
230 struct ir3_shader_key key
= {};
236 fd_mesa_debug
|= FD_DBG_DISASM
;
238 /* cmdline args which impact shader variant get spit out in a
239 * comment on the first line.. a quick/dirty way to preserve
240 * that info so when ir3test recompiles the shader with a new
241 * compiler version, we use the same shader-key settings:
243 debug_printf("; options:");
246 if (!strcmp(argv
[n
], "--verbose")) {
247 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
;
252 if (!strcmp(argv
[n
], "--binning-pass")) {
253 debug_printf(" %s", argv
[n
]);
254 key
.binning_pass
= true;
259 if (!strcmp(argv
[n
], "--color-two-side")) {
260 debug_printf(" %s", argv
[n
]);
261 key
.color_two_side
= true;
266 if (!strcmp(argv
[n
], "--half-precision")) {
267 debug_printf(" %s", argv
[n
]);
268 key
.half_precision
= true;
273 if (!strcmp(argv
[n
], "--saturate-s")) {
274 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
275 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
280 if (!strcmp(argv
[n
], "--saturate-t")) {
281 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
282 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
287 if (!strcmp(argv
[n
], "--saturate-r")) {
288 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
289 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
294 if (!strcmp(argv
[n
], "--nocp")) {
295 fd_mesa_debug
|= FD_DBG_NOCP
;
299 if (!strcmp(argv
[n
], "--nir")) {
305 if (!strcmp(argv
[n
], "--help")) {
316 memset(&v
, 0, sizeof(v
));
319 ret
= read_file(filename
, &ptr
, &size
);
325 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
326 debug_printf("%s\n", (char *)ptr
);
328 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
329 errx(1, "could not parse `%s'", filename
);
331 tgsi_parse_init(&parse
, toks
);
332 switch (parse
.FullHeader
.Processor
.Processor
) {
333 case TGSI_PROCESSOR_FRAGMENT
:
334 v
.type
= SHADER_FRAGMENT
;
336 case TGSI_PROCESSOR_VERTEX
:
337 v
.type
= SHADER_VERTEX
;
339 case TGSI_PROCESSOR_COMPUTE
:
340 v
.type
= SHADER_COMPUTE
;
345 info
= "NIR compiler";
346 ret
= ir3_compile_shader_nir(&v
, toks
, key
);
348 info
= "TGSI compiler";
349 ret
= ir3_compile_shader(&v
, toks
, key
, true);
353 reset_variant(&v
, "compiler failed, trying without copy propagation!");
354 info
= "compiler (no copy propagation)";
355 ret
= ir3_compile_shader(&v
, toks
, key
, false);
359 fprintf(stderr
, "compiler failed!\n");