nir: Use a single list for all shader variables
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cmdline.c
index 39f6c12c6bc9608ad45db07dfe383971caab02a5..15451af4c480a1a01529322701767ccf0b60efe1 100644 (file)
 #include <stdio.h>
 #include <err.h>
 
+#include "nir/tgsi_to_nir.h"
 #include "tgsi/tgsi_parse.h"
 #include "tgsi/tgsi_text.h"
 #include "tgsi/tgsi_dump.h"
 
-#include "freedreno_util.h"
+#include "ir3/ir3_compiler.h"
+#include "ir3/ir3_gallium.h"
+#include "ir3/ir3_nir.h"
+#include "ir3/instr-a3xx.h"
+#include "ir3/ir3.h"
 
-#include "ir3_compiler.h"
-#include "ir3_nir.h"
-#include "instr-a3xx.h"
-#include "ir3.h"
+#include "main/mtypes.h"
 
 #include "compiler/glsl/standalone.h"
 #include "compiler/glsl/glsl_to_nir.h"
 #include "compiler/nir_types.h"
 #include "compiler/spirv/nir_spirv.h"
 
+#include "pipe/p_context.h"
+
 static void dump_info(struct ir3_shader_variant *so, const char *str)
 {
        uint32_t *bin;
-       const char *type = ir3_shader_stage(so->shader);
-       bin = ir3_shader_assemble(so, so->shader->compiler->gpu_id);
+       const char *type = ir3_shader_stage(so);
+       bin = ir3_shader_assemble(so);
        debug_printf("; %s: %s\n", type, str);
        ir3_shader_disasm(so, bin, stdout);
        free(bin);
@@ -63,7 +67,7 @@ static void dump_info(struct ir3_shader_variant *so, const char *str)
 static void
 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
 {
-       nir_foreach_variable(var, var_list) {
+       nir_foreach_variable_in_list(var, var_list) {
                if (var->data.location > new_var->data.location) {
                        exec_node_insert_node_before(&var->node, &new_var->node);
                        return;
@@ -73,21 +77,21 @@ insert_sorted(struct exec_list *var_list, nir_variable *new_var)
 }
 
 static void
-sort_varyings(struct exec_list *var_list)
+sort_varyings(nir_shader *nir, nir_variable_mode mode)
 {
        struct exec_list new_list;
        exec_list_make_empty(&new_list);
-       nir_foreach_variable_safe(var, var_list) {
+       nir_foreach_variable_with_modes_safe(var, nir, mode) {
                exec_node_remove(&var->node);
                insert_sorted(&new_list, var);
        }
-       exec_list_move_nodes_to(&new_list, var_list);
+       exec_list_append(&nir->variables, &new_list);
 }
 
 static void
-fixup_varying_slots(struct exec_list *var_list)
+fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)
 {
-       nir_foreach_variable(var, var_list) {
+       nir_foreach_variable_with_modes(var, nir, mode) {
                if (var->data.location >= VARYING_SLOT_VAR0) {
                        var->data.location += 9;
                } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
@@ -109,12 +113,13 @@ load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
        struct gl_shader_program *prog;
        const nir_shader_compiler_options *nir_options =
                        ir3_get_compiler_options(compiler);
+       static struct gl_context local_ctx;
 
-       prog = standalone_compile_shader(&options, num_files, files);
+       prog = standalone_compile_shader(&options, num_files, files, &local_ctx);
        if (!prog)
                errx(1, "couldn't parse `%s'", files[0]);
 
-       nir_shader *nir = glsl_to_nir(prog, stage, nir_options);
+       nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options);
 
        /* required NIR passes: */
        if (nir_options->lower_all_io_to_temps ||
@@ -137,46 +142,50 @@ load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
        NIR_PASS_V(nir, nir_lower_var_copies);
        nir_print_shader(nir, stdout);
        NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);
-       NIR_PASS_V(nir, nir_lower_atomics_to_ssbo, 8);
+       NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);
        nir_print_shader(nir, stdout);
 
        switch (stage) {
        case MESA_SHADER_VERTEX:
-               nir_assign_var_locations(&nir->inputs,
+               nir_assign_var_locations(nir, nir_var_shader_in,
                                &nir->num_inputs,
                                ir3_glsl_type_size);
 
                /* Re-lower global vars, to deal with any dead VS inputs. */
                NIR_PASS_V(nir, nir_lower_global_vars_to_local);
 
-               sort_varyings(&nir->outputs);
-               nir_assign_var_locations(&nir->outputs,
+               sort_varyings(nir, nir_var_shader_out);
+               nir_assign_var_locations(nir, nir_var_shader_out,
                                &nir->num_outputs,
                                ir3_glsl_type_size);
-               fixup_varying_slots(&nir->outputs);
+               fixup_varying_slots(nir, nir_var_shader_out);
                break;
        case MESA_SHADER_FRAGMENT:
-               sort_varyings(&nir->inputs);
-               nir_assign_var_locations(&nir->inputs,
+               sort_varyings(nir, nir_var_shader_in);
+               nir_assign_var_locations(nir, nir_var_shader_in,
                                &nir->num_inputs,
                                ir3_glsl_type_size);
-               fixup_varying_slots(&nir->inputs);
-               nir_assign_var_locations(&nir->outputs,
+               fixup_varying_slots(nir, nir_var_shader_in);
+               nir_assign_var_locations(nir, nir_var_shader_out,
                                &nir->num_outputs,
                                ir3_glsl_type_size);
                break;
        case MESA_SHADER_COMPUTE:
+       case MESA_SHADER_KERNEL:
                break;
        default:
                errx(1, "unhandled shader stage: %d", stage);
        }
 
-       nir_assign_var_locations(&nir->uniforms,
+       nir_assign_var_locations(nir, nir_var_uniform,
                        &nir->num_uniforms,
                        ir3_glsl_type_size);
 
        NIR_PASS_V(nir, nir_lower_system_values);
-       NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size, 0);
+       NIR_PASS_V(nir, nir_lower_frexp);
+       NIR_PASS_V(nir, nir_lower_io,
+                  nir_var_shader_in | nir_var_shader_out,
+                  ir3_glsl_type_size, (nir_lower_io_options)0);
        NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
 
        return nir;
@@ -229,26 +238,26 @@ load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
                        .int64 = true,
                        .variable_pointers = true,
                },
-               .lower_workgroup_access_to_offsets = true,
+               .lower_ubo_ssbo_access_to_offsets = true,
                .debug = {
                        .func = debug_func,
                }
        };
-       nir_function *entry_point;
+       nir_shader *nir;
        void *buf;
        size_t size;
 
        read_file(filename, &buf, &size);
 
-       entry_point = spirv_to_nir(buf, size / 4,
+       nir = spirv_to_nir(buf, size / 4,
                        NULL, 0, /* spec_entries */
                        stage, entry,
                        &spirv_options,
                        ir3_get_compiler_options(compiler));
 
-       nir_print_shader(entry_point->shader, stdout);
+       nir_print_shader(nir, stdout);
 
-       return entry_point->shader;
+       return nir;
 }
 
 static void print_usage(void)
@@ -280,9 +289,9 @@ int main(int argc, char **argv)
        /* TODO cmdline option to target different gpus: */
        unsigned gpu_id = 320;
        const char *info;
-       const char *entry;
+       const char *spirv_entry = NULL;
        void *ptr;
-       bool from_spirv = false;
+       bool from_tgsi = false;
        size_t size;
 
        memset(&s, 0, sizeof(s));
@@ -297,7 +306,7 @@ int main(int argc, char **argv)
 
        while (n < argc) {
                if (!strcmp(argv[n], "--verbose")) {
-                       fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
+                       ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
                        n++;
                        continue;
                }
@@ -318,7 +327,6 @@ int main(int argc, char **argv)
 
                if (!strcmp(argv[n], "--half-precision")) {
                        debug_printf(" %s", argv[n]);
-                       key.half_precision = true;
                        n++;
                        continue;
                }
@@ -352,7 +360,7 @@ int main(int argc, char **argv)
                }
 
                if (!strcmp(argv[n], "--stream-out")) {
-                       struct pipe_stream_output_info *so = &s.stream_output;
+                       struct ir3_stream_output_info *so = &s.stream_output;
                        debug_printf(" %s", argv[n]);
                        /* TODO more dynamic config based on number of outputs, etc
                         * rather than just hard-code for first output:
@@ -399,32 +407,31 @@ int main(int argc, char **argv)
                if (strcmp(ext, ".tgsi") == 0) {
                        if (num_files != 0)
                                errx(1, "in TGSI mode, only a single file may be specified");
-                       s.from_tgsi = true;
+                       from_tgsi = true;
                } else if (strcmp(ext, ".spv") == 0) {
                        if (num_files != 0)
                                errx(1, "in SPIR-V mode, only a single file may be specified");
                        stage = MESA_SHADER_COMPUTE;
-                       from_spirv = true;
                        filenames[num_files++] = filename;
                        n++;
                        if (n == argc)
                                errx(1, "in SPIR-V mode, an entry point must be specified");
-                       entry = argv[n];
+                       spirv_entry = argv[n];
                        n++;
                } else if (strcmp(ext, ".comp") == 0) {
-                       if (s.from_tgsi || from_spirv)
+                       if (from_tgsi || spirv_entry)
                                errx(1, "cannot mix GLSL/TGSI/SPIRV");
                        if (num_files >= ARRAY_SIZE(filenames))
                                errx(1, "too many GLSL files");
                        stage = MESA_SHADER_COMPUTE;
                } else if (strcmp(ext, ".frag") == 0) {
-                       if (s.from_tgsi || from_spirv)
+                       if (from_tgsi || spirv_entry)
                                errx(1, "cannot mix GLSL/TGSI/SPIRV");
                        if (num_files >= ARRAY_SIZE(filenames))
                                errx(1, "too many GLSL files");
                        stage = MESA_SHADER_FRAGMENT;
                } else if (strcmp(ext, ".vert") == 0) {
-                       if (s.from_tgsi)
+                       if (from_tgsi)
                                errx(1, "cannot mix GLSL and TGSI");
                        if (num_files >= ARRAY_SIZE(filenames))
                                errx(1, "too many GLSL files");
@@ -443,8 +450,10 @@ int main(int argc, char **argv)
 
        compiler = ir3_compiler_create(NULL, gpu_id);
 
-       if (s.from_tgsi) {
+       if (from_tgsi) {
                struct tgsi_token toks[65536];
+               const nir_shader_compiler_options *nir_options =
+                       ir3_get_compiler_options(compiler);
 
                ret = read_file(filenames[0], &ptr, &size);
                if (ret) {
@@ -452,22 +461,23 @@ int main(int argc, char **argv)
                        return ret;
                }
 
-               if (fd_mesa_debug & FD_DBG_OPTMSGS)
+               if (ir3_shader_debug & IR3_DBG_OPTMSGS)
                        debug_printf("%s\n", (char *)ptr);
 
                if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
                        errx(1, "could not parse `%s'", filenames[0]);
 
-               if (fd_mesa_debug & FD_DBG_OPTMSGS)
+               if (ir3_shader_debug & IR3_DBG_OPTMSGS)
                        tgsi_dump(toks, 0);
 
-               nir = ir3_tgsi_to_nir(toks);
+               nir = tgsi_to_nir_noscreen(toks, nir_options);
                NIR_PASS_V(nir, nir_lower_global_vars_to_local);
-       } else if (from_spirv) {
-               nir = load_spirv(filenames[0], entry, stage);
+       } else if (spirv_entry) {
+               nir = load_spirv(filenames[0], spirv_entry, stage);
 
-               NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
-                               (nir_lower_io_options)0);
+               NIR_PASS_V(nir, nir_lower_io,
+                          nir_var_shader_in | nir_var_shader_out,
+                          ir3_glsl_type_size, (nir_lower_io_options)0);
 
                /* TODO do this somewhere else */
                nir_lower_int64(nir, ~0);
@@ -480,24 +490,15 @@ int main(int argc, char **argv)
        }
 
        s.compiler = compiler;
-       s.nir = ir3_optimize_nir(&s, nir, NULL);
+       s.nir = nir;
+
+       ir3_finalize_nir(compiler, nir);
 
        v.key = key;
        v.shader = &s;
+       s.type = v.type = nir->info.stage;
 
-       switch (nir->info.stage) {
-       case MESA_SHADER_FRAGMENT:
-               s.type = v.type = SHADER_FRAGMENT;
-               break;
-       case MESA_SHADER_VERTEX:
-               s.type = v.type = SHADER_VERTEX;
-               break;
-       case MESA_SHADER_COMPUTE:
-               s.type = v.type = SHADER_COMPUTE;
-               break;
-       default:
-               errx(1, "unhandled shader stage: %d", nir->info.stage);
-       }
+       ir3_nir_lower_variant(&v, nir);
 
        info = "NIR compiler";
        ret = ir3_compile_shader_nir(s.compiler, &v);