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_reg(const char *name
, uint32_t r
)
48 debug_printf("; %s: r%d.%c\n", name
, r
>> 2, "xyzw"[r
& 0x3]);
51 static void dump_semantic(struct ir3_shader_variant
*so
,
52 unsigned sem
, const char *name
)
55 regid
= ir3_find_output_regid(so
, ir3_semantic_name(sem
, 0));
56 dump_reg(name
, regid
);
59 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
62 const char *type
= (so
->type
== SHADER_VERTEX
) ? "VERT" : "FRAG";
64 // for debug, dump some before/after info:
65 // TODO make gpu_id configurable on cmdline
66 bin
= ir3_shader_assemble(so
, 320);
67 if (fd_mesa_debug
& FD_DBG_DISASM
) {
68 struct ir3_block
*block
= so
->ir
->block
;
69 struct ir3_register
*reg
;
73 debug_printf("; %s: %s\n", type
, str
);
75 for (i
= 0; i
< block
->ninputs
; i
++) {
76 if (!block
->inputs
[i
]) {
77 debug_printf("; in%d unused\n", i
);
80 reg
= block
->inputs
[i
]->regs
[0];
82 debug_printf("@in(%sr%d.%c)\tin%d\n",
83 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
84 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
87 for (i
= 0; i
< block
->noutputs
; i
++) {
88 if (!block
->outputs
[i
]) {
89 debug_printf("; out%d unused\n", i
);
92 /* kill shows up as a virtual output.. skip it! */
93 if (is_kill(block
->outputs
[i
]))
95 reg
= block
->outputs
[i
]->regs
[0];
97 debug_printf("@out(%sr%d.%c)\tout%d\n",
98 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
99 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
102 disasm_a3xx(bin
, so
->info
.sizedwords
, 0, so
->type
);
104 debug_printf("; %s: outputs:", type
);
105 for (i
= 0; i
< so
->outputs_count
; i
++) {
106 uint8_t regid
= so
->outputs
[i
].regid
;
107 ir3_semantic sem
= so
->outputs
[i
].semantic
;
108 debug_printf(" r%d.%c (%u:%u)",
109 (regid
>> 2), "xyzw"[regid
& 0x3],
110 sem2name(sem
), sem2idx(sem
));
113 debug_printf("; %s: inputs:", type
);
114 for (i
= 0; i
< so
->inputs_count
; i
++) {
115 uint8_t regid
= so
->inputs
[i
].regid
;
116 ir3_semantic sem
= so
->inputs
[i
].semantic
;
117 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
118 (regid
>> 2), "xyzw"[regid
& 0x3],
119 sem2name(sem
), sem2idx(sem
),
120 so
->inputs
[i
].compmask
,
127 /* print generic shader info: */
128 debug_printf("; %s: %u instructions, %d half, %d full\n", type
,
129 so
->info
.instrs_count
,
130 so
->info
.max_half_reg
+ 1,
131 so
->info
.max_reg
+ 1);
133 /* print shader type specific info: */
136 dump_semantic(so
, TGSI_SEMANTIC_POSITION
, "pos");
137 dump_semantic(so
, TGSI_SEMANTIC_PSIZE
, "psize");
139 case SHADER_FRAGMENT
:
140 dump_reg("pos (bary)", so
->pos_regid
);
141 dump_semantic(so
, TGSI_SEMANTIC_POSITION
, "posz");
142 dump_semantic(so
, TGSI_SEMANTIC_COLOR
, "color");
143 /* these two are hard-coded since we don't know how to
144 * program them to anything but all 0's...
147 debug_printf("; fragcoord: r0.x\n");
149 debug_printf("; fragface: hr0.x\n");
161 read_file(const char *filename
, void **ptr
, size_t *size
)
168 fd
= open(filename
, O_RDONLY
);
170 warnx("couldn't open `%s'", filename
);
174 ret
= fstat(fd
, &st
);
176 errx(1, "couldn't stat `%s'", filename
);
179 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
180 if (*ptr
== MAP_FAILED
)
181 errx(1, "couldn't map `%s'", filename
);
188 static void reset_variant(struct ir3_shader_variant
*v
, const char *msg
)
190 printf("; %s\n", msg
);
192 v
->outputs_count
= 0;
195 v
->immediates_count
= 0;
198 static void print_usage(void)
200 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
201 printf(" --verbose - verbose compiler/debug messages\n");
202 printf(" --binning-pass - generate binning pass shader (VERT)\n");
203 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
204 printf(" --half-precision - use half-precision\n");
205 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
206 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
207 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
208 printf(" --nocp - disable copy propagation\n");
209 printf(" --help - show this message\n");
212 int main(int argc
, char **argv
)
215 const char *filename
;
216 struct tgsi_token toks
[65536];
217 struct tgsi_parse_context parse
;
218 struct ir3_shader_variant v
;
219 struct ir3_shader_key key
= {};
224 fd_mesa_debug
|= FD_DBG_DISASM
;
226 /* cmdline args which impact shader variant get spit out in a
227 * comment on the first line.. a quick/dirty way to preserve
228 * that info so when ir3test recompiles the shader with a new
229 * compiler version, we use the same shader-key settings:
231 debug_printf("; options:");
234 if (!strcmp(argv
[n
], "--verbose")) {
235 fd_mesa_debug
|= FD_DBG_OPTDUMP
| FD_DBG_MSGS
| FD_DBG_OPTMSGS
;
240 if (!strcmp(argv
[n
], "--binning-pass")) {
241 debug_printf(" %s", argv
[n
]);
242 key
.binning_pass
= true;
247 if (!strcmp(argv
[n
], "--color-two-side")) {
248 debug_printf(" %s", argv
[n
]);
249 key
.color_two_side
= true;
254 if (!strcmp(argv
[n
], "--half-precision")) {
255 debug_printf(" %s", argv
[n
]);
256 key
.half_precision
= true;
261 if (!strcmp(argv
[n
], "--saturate-s")) {
262 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
263 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
268 if (!strcmp(argv
[n
], "--saturate-t")) {
269 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
270 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
275 if (!strcmp(argv
[n
], "--saturate-r")) {
276 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
277 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
282 if (!strcmp(argv
[n
], "--nocp")) {
283 fd_mesa_debug
|= FD_DBG_NOCP
;
288 if (!strcmp(argv
[n
], "--help")) {
299 memset(&v
, 0, sizeof(v
));
302 ret
= read_file(filename
, &ptr
, &size
);
308 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
309 debug_printf("%s\n", (char *)ptr
);
311 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
312 errx(1, "could not parse `%s'", filename
);
314 tgsi_parse_init(&parse
, toks
);
315 switch (parse
.FullHeader
.Processor
.Processor
) {
316 case TGSI_PROCESSOR_FRAGMENT
:
317 v
.type
= SHADER_FRAGMENT
;
319 case TGSI_PROCESSOR_VERTEX
:
320 v
.type
= SHADER_VERTEX
;
322 case TGSI_PROCESSOR_COMPUTE
:
323 v
.type
= SHADER_COMPUTE
;
328 ret
= ir3_compile_shader(&v
, toks
, key
, true);
331 reset_variant(&v
, "compiler failed, trying without copy propagation!");
332 info
= "compiler (no copy propagation)";
333 ret
= ir3_compile_shader(&v
, toks
, key
, false);
337 fprintf(stderr
, "compiler failed!\n");