From 1e8808a4a0f7f28dfc885bbe6c50e7a65ad15bbf Mon Sep 17 00:00:00 2001 From: Rob Clark Date: Wed, 17 Jun 2020 08:13:12 -0700 Subject: [PATCH] freedreno/ir3: refactor out helper to compile shader from asm Deduplicate a bit of hand-building of ir3_shader/_variant from computerator and delay test. This also removes the need for external things to depend on generated ir3_parser header. Signed-off-by: Rob Clark Part-of: --- src/freedreno/Makefile.sources | 2 + src/freedreno/computerator/ir3_asm.c | 22 ++------- src/freedreno/computerator/meson.build | 1 - src/freedreno/ir3/ir3_assembler.c | 62 ++++++++++++++++++++++++++ src/freedreno/ir3/ir3_assembler.h | 46 +++++++++++++++++++ src/freedreno/ir3/ir3_parser.y | 12 +---- src/freedreno/ir3/meson.build | 6 ++- src/freedreno/ir3/tests/delay.c | 24 ++++------ 8 files changed, 127 insertions(+), 48 deletions(-) create mode 100644 src/freedreno/ir3/ir3_assembler.c create mode 100644 src/freedreno/ir3/ir3_assembler.h diff --git a/src/freedreno/Makefile.sources b/src/freedreno/Makefile.sources index c24f7064339..ee839445cd7 100644 --- a/src/freedreno/Makefile.sources +++ b/src/freedreno/Makefile.sources @@ -22,6 +22,8 @@ ir3_SOURCES := \ ir3/ir3.c \ ir3/ir3_a4xx.c \ ir3/ir3_a6xx.c \ + ir3/ir3_assembler.c \ + ir3/ir3_assembler.h \ ir3/ir3_compiler.c \ ir3/ir3_compiler.h \ ir3/ir3_compiler_nir.c \ diff --git a/src/freedreno/computerator/ir3_asm.c b/src/freedreno/computerator/ir3_asm.c index fd27212eb1e..e1e845a9a7c 100644 --- a/src/freedreno/computerator/ir3_asm.c +++ b/src/freedreno/computerator/ir3_asm.c @@ -21,8 +21,8 @@ * SOFTWARE. */ +#include "ir3/ir3_assembler.h" #include "ir3/ir3_compiler.h" -#include "ir3/ir3_parser.h" #include "ir3_asm.h" @@ -30,32 +30,18 @@ struct ir3_kernel * ir3_asm_assemble(struct ir3_compiler *c, FILE *in) { struct ir3_kernel *kernel = calloc(1, sizeof(*kernel)); + struct ir3_shader *shader = ir3_parse_asm(c, &kernel->info, in); + struct ir3_shader_variant *v = shader->variants; - struct ir3_shader *shader = calloc(1, sizeof(*shader)); - shader->compiler = c; - shader->type = MESA_SHADER_COMPUTE; - - struct ir3_shader_variant *v = calloc(1, sizeof(*v)); - v->type = MESA_SHADER_COMPUTE; - v->shader = shader; v->mergedregs = true; kernel->v = v; - - kernel->info.numwg = INVALID_REG; - - v->ir = ir3_parse(v, &kernel->info, in); - if (!v->ir) - errx(-1, "parse failed"); - - ir3_debug_print(v->ir, "AFTER PARSING"); + kernel->bin = v->bin; memcpy(kernel->base.local_size, kernel->info.local_size, sizeof(kernel->base.local_size)); kernel->base.num_bufs = kernel->info.num_bufs; memcpy(kernel->base.buf_sizes, kernel->info.buf_sizes, sizeof(kernel->base.buf_sizes)); - kernel->bin = ir3_shader_assemble(v); - unsigned sz = v->info.sizedwords * 4; v->bo = fd_bo_new(c->dev, sz, diff --git a/src/freedreno/computerator/meson.build b/src/freedreno/computerator/meson.build index 3203e48f3e2..e810dfb78e4 100644 --- a/src/freedreno/computerator/meson.build +++ b/src/freedreno/computerator/meson.build @@ -23,7 +23,6 @@ computerator_files = [ 'ir3_asm.c', 'main.c', freedreno_xml_header_files, - ir3_parser, ] computerator = executable( diff --git a/src/freedreno/ir3/ir3_assembler.c b/src/freedreno/ir3/ir3_assembler.c new file mode 100644 index 00000000000..99104243a53 --- /dev/null +++ b/src/freedreno/ir3/ir3_assembler.c @@ -0,0 +1,62 @@ +/* + * Copyright © 2020 Google, Inc. + * + * 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. + */ + +#include + +#include "ir3_assembler.h" +#include "ir3_compiler.h" +#include "ir3_parser.h" + +/** + * A helper to go from ir3 assembly to assembled shader. The shader has a + * single variant. + */ +struct ir3_shader * +ir3_parse_asm(struct ir3_compiler *c, struct ir3_kernel_info *info, FILE *in) +{ + struct ir3_shader *shader = calloc(1, sizeof(*shader)); + shader->compiler = c; + shader->type = MESA_SHADER_COMPUTE; + mtx_init(&shader->variants_lock, mtx_plain); + + struct ir3_shader_variant *v = calloc(1, sizeof(*v)); + v->type = MESA_SHADER_COMPUTE; + v->shader = shader; + + shader->variants = v; + shader->variant_count = 1; + + info->numwg = INVALID_REG; + + v->ir = ir3_parse(v, info, in); + if (!v->ir) + errx(-1, "parse failed"); + + ir3_debug_print(v->ir, "AFTER PARSING"); + + v->bin = ir3_shader_assemble(v); + if (!v->bin) + errx(-1, "assembler failed"); + + return shader; +} diff --git a/src/freedreno/ir3/ir3_assembler.h b/src/freedreno/ir3/ir3_assembler.h new file mode 100644 index 00000000000..93f40aeea57 --- /dev/null +++ b/src/freedreno/ir3/ir3_assembler.h @@ -0,0 +1,46 @@ +/* + * Copyright © 2020 Google, Inc. + * + * 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. + */ + +#ifndef __IR3_ASSEMBLER_H__ +#define __IR3_ASSEMBLER_H__ + +#include +#include + +#define MAX_BUFS 4 + +struct ir3_kernel_info { + uint32_t local_size[3]; + uint32_t num_bufs; + uint32_t buf_sizes[MAX_BUFS]; /* size in dwords */ + + /* driver-param uniforms: */ + unsigned numwg; +}; + +struct ir3_shader; +struct ir3_compiler; + +struct ir3_shader * ir3_parse_asm(struct ir3_compiler *c, struct ir3_kernel_info *info, FILE *in); + +#endif /* __IR3_ASSEMBLER_H__ */ diff --git a/src/freedreno/ir3/ir3_parser.y b/src/freedreno/ir3/ir3_parser.y index 08779380b12..2b024d358cc 100644 --- a/src/freedreno/ir3/ir3_parser.y +++ b/src/freedreno/ir3/ir3_parser.y @@ -22,17 +22,7 @@ */ %code requires { - -#define MAX_BUFS 4 - -struct ir3_kernel_info { - uint32_t local_size[3]; - uint32_t num_bufs; - uint32_t buf_sizes[MAX_BUFS]; /* size in dwords */ - - /* driver-param uniforms: */ - unsigned numwg; -}; +#include "ir3/ir3_assembler.h" struct ir3 * ir3_parse(struct ir3_shader_variant *v, struct ir3_kernel_info *k, FILE *f); diff --git a/src/freedreno/ir3/meson.build b/src/freedreno/ir3/meson.build index 1bc4ce2f2c6..466b065d134 100644 --- a/src/freedreno/ir3/meson.build +++ b/src/freedreno/ir3/meson.build @@ -66,6 +66,8 @@ libfreedreno_ir3_files = files( 'ir3.c', 'ir3_a4xx.c', 'ir3_a6xx.c', + 'ir3_assembler.c', + 'ir3_assembler.h', 'ir3_compiler_nir.c', 'ir3_compiler.c', 'ir3_compiler.h', @@ -126,9 +128,9 @@ test('ir3_disasm', test('ir3_delay_test', executable( 'ir3_delay_test', - ['tests/delay.c', ir3_parser], + 'tests/delay.c', link_with: libfreedreno_ir3, - dependencies: [idep_mesautil, idep_nir_headers], + dependencies: [idep_mesautil, idep_nir], include_directories: [inc_freedreno, inc_include, inc_src, inc_mesa, inc_gallium], ), suite: ['freedreno'], diff --git a/src/freedreno/ir3/tests/delay.c b/src/freedreno/ir3/tests/delay.c index 0bfdceabab8..4e4d1cc00a0 100644 --- a/src/freedreno/ir3/tests/delay.c +++ b/src/freedreno/ir3/tests/delay.c @@ -24,8 +24,8 @@ #include #include "ir3.h" +#include "ir3_assembler.h" #include "ir3_compiler.h" -#include "ir3_parser.h" /* * A test for delay-slot calculation. Each test specifies ir3 assembly @@ -85,25 +85,16 @@ static const struct test { ), }; -static struct ir3 * +static struct ir3_shader * parse_asm(struct ir3_compiler *c, const char *asmstr) { - struct ir3_shader shader = { - .type = MESA_SHADER_COMPUTE, - .compiler = c, - }; - struct ir3_shader_variant v = { - .type = shader.type, - .shader = &shader, - }; struct ir3_kernel_info info = {}; FILE *in = fmemopen((void *)asmstr, strlen(asmstr), "r"); - - struct ir3 *ir = ir3_parse(&v, &info, in); + struct ir3_shader *shader = ir3_parse_asm(c, &info, in); fclose(in); - return ir; + return shader; } static unsigned @@ -190,9 +181,8 @@ main(int argc, char **argv) for (int i = 0; i < ARRAY_SIZE(tests); i++) { const struct test *test = &tests[i]; - struct ir3 *ir = parse_asm(c, test->asmstr); - - ir3_debug_print(ir, "AFTER PARSING"); + struct ir3_shader *shader = parse_asm(c, test->asmstr); + struct ir3 *ir = shader->variants->ir; regs_to_ssa(ir); @@ -224,6 +214,8 @@ main(int argc, char **argv) } else { printf("%d: PASS\n", i); } + + ir3_shader_destroy(shader); } return result; -- 2.30.2