r600: enable NIR backend DEBUG flag for supported architectures
authorGert Wollny <gert.wollny@collabora.com>
Sat, 28 Dec 2019 14:34:43 +0000 (15:34 +0100)
committerMarge Bot <eric+marge@anholt.net>
Mon, 10 Feb 2020 19:09:08 +0000 (19:09 +0000)
When NIR is enabled, a few features that are not yet supported will be
explicitely disabled.

Signed-off-by: Gert Wollny <gert.wollny@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3225>

src/gallium/drivers/r600/r600_pipe.c
src/gallium/drivers/r600/r600_shader.c
src/gallium/drivers/r600/r600_shader.h

index f26da31d2ae503501994ac3cd1747a1f54e5fdec..97f20355d599faf5b96cf4378dbe7c8edf8b9d25 100644 (file)
@@ -249,6 +249,12 @@ fail:
        return NULL;
 }
 
+static bool is_nir_enabled(struct r600_common_screen *screen) {
+   return (screen->debug_flags & DBG_NIR &&
+       screen->family >= CHIP_CEDAR &&
+       screen->family < CHIP_CAYMAN);
+}
+
 /*
  * pipe_screen
  */
@@ -333,7 +339,7 @@ static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
                return rscreen->b.chip_class > R700;
 
        case PIPE_CAP_TGSI_TEXCOORD:
-               return 0;
+               return is_nir_enabled(&rscreen->b);
 
        case PIPE_CAP_FAKE_SW_MSAA:
                return 0;
@@ -414,7 +420,7 @@ static int r600_get_param(struct pipe_screen* pscreen, enum pipe_cap param)
                return 1;
 
        case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
-               if (family >= CHIP_CEDAR)
+               if (family >= CHIP_CEDAR && !is_nir_enabled(&rscreen->b))
                        return 256;
                return 0;
 
@@ -542,7 +548,6 @@ static int r600_get_shader_param(struct pipe_screen* pscreen,
        {
        case PIPE_SHADER_FRAGMENT:
        case PIPE_SHADER_VERTEX:
-       case PIPE_SHADER_COMPUTE:
                break;
        case PIPE_SHADER_GEOMETRY:
                if (rscreen->b.family >= CHIP_CEDAR)
@@ -551,9 +556,14 @@ static int r600_get_shader_param(struct pipe_screen* pscreen,
                if (rscreen->b.info.drm_minor >= 37)
                        break;
                return 0;
+      /* With NIR we currently disable TES, TCS and COMP shaders */
        case PIPE_SHADER_TESS_CTRL:
        case PIPE_SHADER_TESS_EVAL:
-               if (rscreen->b.family >= CHIP_CEDAR)
+               if (rscreen->b.family >= CHIP_CEDAR &&
+                   !is_nir_enabled(&rscreen->b))
+                       break;
+       case PIPE_SHADER_COMPUTE:
+               if (!is_nir_enabled(&rscreen->b))
                        break;
        default:
                return 0;
@@ -576,9 +586,11 @@ static int r600_get_shader_param(struct pipe_screen* pscreen,
        case PIPE_SHADER_CAP_MAX_CONST_BUFFER_SIZE:
                if (shader == PIPE_SHADER_COMPUTE) {
                        uint64_t max_const_buffer_size;
-                       pscreen->get_compute_param(pscreen, PIPE_SHADER_IR_TGSI,
-                               PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE,
-                               &max_const_buffer_size);
+                       enum pipe_shader_ir ir_type = is_nir_enabled(&rscreen->b) ?
+                               PIPE_SHADER_IR_NIR: PIPE_SHADER_IR_TGSI;
+                       pscreen->get_compute_param(pscreen, ir_type,
+                                                  PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE,
+                                                  &max_const_buffer_size);
                        return MIN2(max_const_buffer_size, INT_MAX);
 
                } else {
@@ -605,14 +617,19 @@ static int r600_get_shader_param(struct pipe_screen* pscreen,
        case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
        case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
                return 16;
-        case PIPE_SHADER_CAP_PREFERRED_IR:
+       case PIPE_SHADER_CAP_PREFERRED_IR:
+               if (is_nir_enabled(&rscreen->b))
+                       return PIPE_SHADER_IR_NIR;
                return PIPE_SHADER_IR_TGSI;
        case PIPE_SHADER_CAP_SUPPORTED_IRS: {
                int ir = 0;
                if (shader == PIPE_SHADER_COMPUTE)
                        ir = 1 << PIPE_SHADER_IR_NATIVE;
-               if (rscreen->b.family >= CHIP_CEDAR)
+               if (rscreen->b.family >= CHIP_CEDAR) {
                        ir |= 1 << PIPE_SHADER_IR_TGSI;
+                       if (is_nir_enabled(&rscreen->b))
+                               ir |= 1 << PIPE_SHADER_IR_NIR;
+               }
                return ir;
        }
        case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
index 85e584baff50ee13a406a202344c072d9fb6d36c..734565baa22d98c754ca629ea7d81f993578e12e 100644 (file)
@@ -24,7 +24,9 @@
 #include "r600_formats.h"
 #include "r600_opcodes.h"
 #include "r600_shader.h"
+#include "r600_dump.h"
 #include "r600d.h"
+#include "sfn/sfn_nir.h"
 
 #include "sb/sb_public.h"
 
 #include "tgsi/tgsi_parse.h"
 #include "tgsi/tgsi_scan.h"
 #include "tgsi/tgsi_dump.h"
+#include "tgsi/tgsi_from_mesa.h"
+#include "nir/tgsi_to_nir.h"
+#include "nir/nir_to_tgsi_info.h"
+#include "compiler/nir/nir.h"
 #include "util/u_bitcast.h"
 #include "util/u_memory.h"
 #include "util/u_math.h"
@@ -157,6 +163,8 @@ static int store_shader(struct pipe_context *ctx,
        return 0;
 }
 
+extern const struct nir_shader_compiler_options r600_nir_options;
+static int nshader = 0;
 int r600_pipe_shader_create(struct pipe_context *ctx,
                            struct r600_pipe_shader *shader,
                            union r600_shader_key key)
@@ -164,27 +172,61 @@ int r600_pipe_shader_create(struct pipe_context *ctx,
        struct r600_context *rctx = (struct r600_context *)ctx;
        struct r600_pipe_shader_selector *sel = shader->selector;
        int r;
-       bool dump = r600_can_dump_shader(&rctx->screen->b,
-                                        tgsi_get_processor_type(sel->tokens));
-       unsigned use_sb = !(rctx->screen->b.debug_flags & DBG_NO_SB);
+       struct r600_screen *rscreen = (struct r600_screen *)ctx->screen;
+       
+       int processor = sel->ir_type == PIPE_SHADER_IR_TGSI ?
+               tgsi_get_processor_type(sel->tokens):
+               pipe_shader_type_from_mesa(sel->nir->info.stage);
+       
+       bool dump = r600_can_dump_shader(&rctx->screen->b, processor);
+       unsigned use_sb = !(rctx->screen->b.debug_flags & DBG_NO_SB) &&
+               !(rscreen->b.debug_flags & DBG_NIR);
        unsigned sb_disasm;
        unsigned export_shader;
-
+       
        shader->shader.bc.isa = rctx->isa;
-
+       
+       if (!(rscreen->b.debug_flags & DBG_NIR)) {
+               assert(sel->ir_type == PIPE_SHADER_IR_TGSI);
+               r = r600_shader_from_tgsi(rctx, shader, key);
+               if (r) {
+                       R600_ERR("translation from TGSI failed !\n");
+                       goto error;
+               }
+       } else {
+               if (sel->ir_type == PIPE_SHADER_IR_TGSI)
+                       sel->nir = tgsi_to_nir_noscreen(sel->tokens, &r600_nir_options);
+               nir_tgsi_scan_shader(sel->nir, &sel->info, true);
+               r = r600_shader_from_nir(rctx, shader, &key);
+               if (r) {
+                       fprintf(stderr, "--Failed shader--------------------------------------------------\n");
+                       
+                       if (sel->ir_type == PIPE_SHADER_IR_TGSI) {
+                               fprintf(stderr, "--TGSI--------------------------------------------------------\n");
+                               tgsi_dump(sel->tokens, 0);
+                       }
+                       
+                       if (rscreen->b.debug_flags & DBG_NIR) {
+                               fprintf(stderr, "--NIR --------------------------------------------------------\n");
+                               nir_print_shader(sel->nir, stderr);
+                       }
+                       
+                       R600_ERR("translation from NIR failed !\n");
+                       goto error;
+               }
+       }
+       
        if (dump) {
-               fprintf(stderr, "--------------------------------------------------------------\n");
-               tgsi_dump(sel->tokens, 0);
-
+               if (sel->ir_type == PIPE_SHADER_IR_TGSI) {
+                       fprintf(stderr, "--TGSI--------------------------------------------------------\n");
+                       tgsi_dump(sel->tokens, 0);
+               }
+               
                if (sel->so.num_outputs) {
                        r600_dump_streamout(&sel->so);
                }
        }
-       r = r600_shader_from_tgsi(rctx, shader, key);
-       if (r) {
-               R600_ERR("translation from TGSI failed !\n");
-               goto error;
-       }
+       
        if (shader->shader.processor_type == PIPE_SHADER_VERTEX) {
                /* only disable for vertex shaders in tess paths */
                if (key.vs.as_ls)
@@ -216,7 +258,7 @@ int r600_pipe_shader_create(struct pipe_context *ctx,
                r600_bytecode_disasm(&shader->shader.bc);
                fprintf(stderr, "______________________________________________________________\n");
        } else if ((dump && sb_disasm) || use_sb) {
-               r = r600_sb_bytecode_process(rctx, &shader->shader.bc, &shader->shader,
+                r = r600_sb_bytecode_process(rctx, &shader->shader.bc, &shader->shader,
                                             dump, use_sb);
                if (r) {
                        R600_ERR("r600_sb_bytecode_process failed !\n");
@@ -224,6 +266,30 @@ int r600_pipe_shader_create(struct pipe_context *ctx,
                }
        }
 
+        if (dump) {
+           FILE *f;
+           char fname[1024];
+           snprintf(fname, 1024, "shader_from_%s_%d.cpp",
+                    (sel->ir_type == PIPE_SHADER_IR_TGSI ?
+                        (rscreen->b.debug_flags & DBG_NIR ? "tgsi-nir" : "tgsi")
+                      : "nir"), nshader);
+           f = fopen(fname, "w");
+           print_shader_info(f, nshader++, &shader->shader);
+           print_shader_info(stderr, nshader++, &shader->shader);
+           print_pipe_info(stderr, &sel->info);
+           if (sel->ir_type == PIPE_SHADER_IR_TGSI) {
+              fprintf(f, "/****TGSI**********************************\n");
+              tgsi_dump_to_file(sel->tokens, 0, f);
+           }
+
+           if (rscreen->b.debug_flags & DBG_NIR){
+              fprintf(f, "/****NIR **********************************\n");
+              nir_print_shader(sel->nir, f);
+           }
+           fprintf(f, "******************************************/\n");
+           fclose(f);
+        }
+
        if (shader->gs_copy_shader) {
                if (dump) {
                        // dump copy shader
@@ -301,7 +367,8 @@ error:
 void r600_pipe_shader_destroy(struct pipe_context *ctx UNUSED, struct r600_pipe_shader *shader)
 {
        r600_resource_reference(&shader->bo, NULL);
-       r600_bytecode_clear(&shader->shader.bc);
+        if (shader->shader.bc.cf.next)
+               r600_bytecode_clear(&shader->shader.bc);
        r600_release_command_buffer(&shader->command_buffer);
 }
 
@@ -2469,9 +2536,9 @@ static void convert_edgeflag_to_int(struct r600_shader_ctx *ctx)
        r600_bytecode_add_alu(ctx->bc, &alu);
 }
 
-static int generate_gs_copy_shader(struct r600_context *rctx,
-                                  struct r600_pipe_shader *gs,
-                                  struct pipe_stream_output_info *so)
+int generate_gs_copy_shader(struct r600_context *rctx,
+                            struct r600_pipe_shader *gs,
+                            struct pipe_stream_output_info *so)
 {
        struct r600_shader_ctx ctx = {};
        struct r600_shader *gs_shader = &gs->shader;
index fd7dac752660aea8abbc2c16441753c4fe27ab75..1e00a9527b521bb1c4afc1ca3e6a49e551bddc2e 100644 (file)
@@ -191,6 +191,10 @@ int eg_get_interpolator_index(unsigned interpolate, unsigned location);
 
 int r600_get_lds_unique_index(unsigned semantic_name, unsigned index);
 
+int generate_gs_copy_shader(struct r600_context *rctx,
+                            struct r600_pipe_shader *gs,
+                            struct pipe_stream_output_info *so);
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif