freedreno: add a standalone ir3_compiler binary for building TGSI
authorIlia Mirkin <imirkin@alum.mit.edu>
Tue, 16 Sep 2014 01:42:01 +0000 (21:42 -0400)
committerRob Clark <robclark@freedesktop.org>
Tue, 16 Sep 2014 16:13:22 +0000 (12:13 -0400)
Compiler taken from the combo old/new compiler comparer + simulator.

Signed-off-by: Ilia Mirkin <imirkin@alum.mit.edu>
src/gallium/drivers/freedreno/Makefile.am
src/gallium/drivers/freedreno/ir3/ir3_cmdline.c [new file with mode: 0644]

index 33563f5952ba11e6456b18fccab92e708b4b761b..ee5d506f2a388f8cd13ea52371e30312792b1cac 100644 (file)
@@ -17,4 +17,16 @@ libfreedreno_la_SOURCES = \
        $(a3xx_SOURCES) \
        $(ir3_SOURCES)
 
+noinst_PROGRAMS = ir3_compiler
+
+ir3_compiler_SOURCES = \
+       ir3/ir3_cmdline.c
+
+ir3_compiler_LDADD = \
+       libfreedreno.la \
+       ../../auxiliary/libgallium.la \
+       $(top_builddir)/src/util/libmesautil.la \
+       $(GALLIUM_COMMON_LIB_DEPS) \
+       $(FREEDRENO_LIBS)
+
 EXTRA_DIST = Android.mk
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c b/src/gallium/drivers/freedreno/ir3/ir3_cmdline.c
new file mode 100644 (file)
index 0000000..9944521
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+ * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ *    Rob Clark <robclark@freedesktop.org>
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <err.h>
+
+#include "tgsi/tgsi_parse.h"
+#include "tgsi/tgsi_text.h"
+#include "tgsi/tgsi_dump.h"
+
+#include "freedreno_util.h"
+#include "freedreno_lowering.h"
+
+#include "ir3_compiler.h"
+#include "instr-a3xx.h"
+#include "ir3.h"
+
+static void dump_info(struct ir3_shader_variant *so)
+{
+       struct ir3_info info;
+       uint32_t *bin;
+       const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";
+
+       // for debug, dump some before/after info:
+       bin = ir3_assemble(so->ir, &info);
+       if (fd_mesa_debug & FD_DBG_DISASM) {
+               unsigned i;
+
+               debug_printf("%s: disasm:\n", type);
+               disasm_a3xx(bin, info.sizedwords, 0, so->type);
+
+               debug_printf("%s: outputs:", type);
+               for (i = 0; i < so->outputs_count; i++) {
+                       uint8_t regid = so->outputs[i].regid;
+                       ir3_semantic sem = so->outputs[i].semantic;
+                       debug_printf(" r%d.%c (%u:%u)",
+                                       (regid >> 2), "xyzw"[regid & 0x3],
+                                       sem2name(sem), sem2idx(sem));
+               }
+               debug_printf("\n");
+               debug_printf("%s: inputs:", type);
+               for (i = 0; i < so->inputs_count; i++) {
+                       uint8_t regid = so->inputs[i].regid;
+                       ir3_semantic sem = so->inputs[i].semantic;
+                       debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
+                                       (regid >> 2), "xyzw"[regid & 0x3],
+                                       sem2name(sem), sem2idx(sem),
+                                       so->inputs[i].compmask,
+                                       so->inputs[i].inloc,
+                                       so->inputs[i].bary);
+               }
+               debug_printf("\n");
+       }
+       debug_printf("%s: %u instructions, %d half, %d full\n\n",
+                       type, info.instrs_count, info.max_half_reg + 1, info.max_reg + 1);
+       free(bin);
+}
+
+
+static void
+read_file(const char *filename, void **ptr, size_t *size)
+{
+       int fd, ret;
+       struct stat st;
+
+       *ptr = MAP_FAILED;
+
+       fd = open(filename, O_RDONLY);
+       if (fd == -1)
+               errx(1, "couldn't open `%s'", filename);
+
+       ret = fstat(fd, &st);
+       if (ret)
+               errx(1, "couldn't stat `%s'", filename);
+
+       *size = st.st_size;
+       *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+       if (*ptr == MAP_FAILED)
+               errx(1, "couldn't map `%s'", filename);
+
+       close(fd);
+}
+
+static void reset_variant(struct ir3_shader_variant *v, const char *msg)
+{
+       debug_error(msg);
+       v->inputs_count = 0;
+       v->outputs_count = 0;
+       v->total_in = 0;
+       v->has_samp = false;
+       v->immediates_count = 0;
+}
+
+int main(int argc, char **argv)
+{
+       int ret = 0;
+       const char *filename = argv[1];
+       struct tgsi_token toks[65536];
+       struct tgsi_parse_context parse;
+       struct ir3_shader_variant v;
+       struct ir3_shader_key key = {
+               .color_two_side = true,
+       };
+       void *ptr;
+       size_t size;
+
+       fd_mesa_debug |= FD_DBG_DISASM | FD_DBG_OPTDUMP | FD_DBG_MSGS | FD_DBG_OPTMSGS;
+
+
+       memset(&v, 0, sizeof(v));
+       v.key = key;
+
+       read_file(filename, &ptr, &size);
+
+       if (!tgsi_text_translate(ptr, toks, Elements(toks)))
+               errx(1, "could not parse `%s'", filename);
+
+       tgsi_parse_init(&parse, toks);
+       switch (parse.FullHeader.Processor.Processor) {
+       case TGSI_PROCESSOR_FRAGMENT:
+               v.type = SHADER_FRAGMENT;
+               break;
+       case TGSI_PROCESSOR_VERTEX:
+               v.type = SHADER_VERTEX;
+               break;
+       case TGSI_PROCESSOR_COMPUTE:
+               v.type = SHADER_COMPUTE;
+               break;
+       }
+
+       if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
+               /* with new compiler: */
+               ret = ir3_compile_shader(&v, toks, key, true);
+
+               if (ret) {
+                       reset_variant(&v, "new compiler failed, trying without copy propagation!");
+                       ret = ir3_compile_shader(&v, toks, key, false);
+                       if (ret)
+                               reset_variant(&v, "new compiler failed, trying fallback!\n");
+               }
+       }
+
+       if (ret)
+               ret = ir3_compile_shader_old(&v, toks, key);
+
+       if (ret) {
+               fprintf(stderr, "old compiler failed!\n");
+               return ret;
+       }
+       dump_info(&v);
+}