[Mesa-dev] [RFCv0 6/8] freedreno/ir3: add support for NIR as preferred IR

Rob Clark robdclark at gmail.com
Mon Oct 19 12:54:39 PDT 2015


For now under debug flag, since only suitable for debugging/testing.
---
 src/gallium/drivers/freedreno/freedreno_screen.c   |  5 +++-
 src/gallium/drivers/freedreno/freedreno_util.h     |  1 +
 .../drivers/freedreno/ir3/ir3_compiler_nir.c       | 15 ++++++++++-
 src/gallium/drivers/freedreno/ir3/ir3_shader.c     | 30 +++++++++++++++++++---
 src/gallium/drivers/freedreno/ir3/ir3_shader.h     |  3 +++
 5 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/src/gallium/drivers/freedreno/freedreno_screen.c b/src/gallium/drivers/freedreno/freedreno_screen.c
index b64f78c..30def8e 100644
--- a/src/gallium/drivers/freedreno/freedreno_screen.c
+++ b/src/gallium/drivers/freedreno/freedreno_screen.c
@@ -71,6 +71,7 @@ static const struct debug_named_value debug_options[] = {
 		{"glsl120",   FD_DBG_GLSL120,"Temporary flag to force GLSL 1.20 (rather than 1.30) on a3xx+"},
 		{"shaderdb",  FD_DBG_SHADERDB, "Enable shaderdb output"},
 		{"flush",     FD_DBG_FLUSH,  "Force flush after every draw"},
+		{"nir",       FD_DBG_NIR,    "Prefer NIR as native IR"},
 		DEBUG_NAMED_VALUE_END
 };
 
@@ -398,7 +399,7 @@ fd_screen_get_shader_param(struct pipe_screen *pscreen, unsigned shader,
 	case PIPE_SHADER_CAP_TGSI_DROUND_SUPPORTED:
 	case PIPE_SHADER_CAP_TGSI_DFRACEXP_DLDEXP_SUPPORTED:
 	case PIPE_SHADER_CAP_TGSI_FMA_SUPPORTED:
-        case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
+	case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
 		return 0;
 	case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
 		return 1;
@@ -410,6 +411,8 @@ fd_screen_get_shader_param(struct pipe_screen *pscreen, unsigned shader,
 	case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
 		return 16;
 	case PIPE_SHADER_CAP_PREFERRED_IR:
+		if ((fd_mesa_debug & FD_DBG_NIR) && is_ir3(screen))
+			return PIPE_SHADER_IR_NIR;
 		return PIPE_SHADER_IR_TGSI;
 	}
 	debug_printf("unknown shader param %d\n", param);
diff --git a/src/gallium/drivers/freedreno/freedreno_util.h b/src/gallium/drivers/freedreno/freedreno_util.h
index 0d2418e..56d3235 100644
--- a/src/gallium/drivers/freedreno/freedreno_util.h
+++ b/src/gallium/drivers/freedreno/freedreno_util.h
@@ -73,6 +73,7 @@ enum adreno_stencil_op fd_stencil_op(unsigned op);
 #define FD_DBG_GLSL120  0x0400
 #define FD_DBG_SHADERDB 0x0800
 #define FD_DBG_FLUSH    0x1000
+#define FD_DBG_NIR      0x2000
 
 extern int fd_mesa_debug;
 extern bool fd_binning_enabled;
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c b/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
index b4e5483..5a53e32 100644
--- a/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
+++ b/src/gallium/drivers/freedreno/ir3/ir3_compiler_nir.c
@@ -164,7 +164,14 @@ to_nir(struct ir3_compile *ctx, struct ir3_shader_variant *so)
 		tex_options.lower_txp = (1 << GLSL_SAMPLER_DIM_3D);
 	}
 
-	struct nir_shader *s = tgsi_to_nir(so->shader->tokens, &options);
+	struct nir_shader *s;
+
+	if (so->shader->nir) {
+		// XXX need nir_clone() here..
+		s = so->shader->nir;
+	} else {
+		s = tgsi_to_nir(so->shader->tokens, &options);
+	}
 
 	if (fd_mesa_debug & FD_DBG_OPTMSGS) {
 		debug_printf("----------------------\n");
@@ -292,6 +299,12 @@ compile_error(struct ir3_compile *ctx, const char *format, ...)
 static void
 compile_free(struct ir3_compile *ctx)
 {
+	/* TODO .. probably need a ralloc_free(ctx->s) here, at least
+	 * for the tgsi_to_nir case.. right now in the case we get
+	 * NIR directly, we want to skip this since the nir_shader isn't
+	 * getting cloned..  and/or if it was we might be wanting to
+	 * free it in ir3_shader_destroy()??
+	 */
 	ralloc_free(ctx);
 }
 
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_shader.c b/src/gallium/drivers/freedreno/ir3/ir3_shader.c
index 7b56533..0cbf8fe 100644
--- a/src/gallium/drivers/freedreno/ir3/ir3_shader.c
+++ b/src/gallium/drivers/freedreno/ir3/ir3_shader.c
@@ -39,6 +39,7 @@
 
 #include "ir3_shader.h"
 #include "ir3_compiler.h"
+#include "ir3_nir.h"
 
 
 static void
@@ -188,9 +189,15 @@ create_variant(struct ir3_shader *shader, struct ir3_shader_key key)
 	v->type = shader->type;
 
 	if (fd_mesa_debug & FD_DBG_DISASM) {
-		DBG("dump tgsi: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
-			key.binning_pass, key.color_two_side, key.half_precision);
-		tgsi_dump(shader->tokens, 0);
+		if (shader->nir) {
+			DBG("dump nir: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
+					key.binning_pass, key.color_two_side, key.half_precision);
+			nir_print_shader(shader->nir, stderr);
+		} else {
+			DBG("dump tgsi: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
+					key.binning_pass, key.color_two_side, key.half_precision);
+			tgsi_dump(shader->tokens, 0);
+		}
 	}
 
 	ret = ir3_compile_shader_nir(shader->compiler, v);
@@ -267,7 +274,9 @@ ir3_shader_destroy(struct ir3_shader *shader)
 		v = v->next;
 		delete_variant(t);
 	}
+
 	free((void *)shader->tokens);
+	/* XXX ralloc_free(shader->nir); */
 	free(shader);
 }
 
@@ -281,7 +290,20 @@ ir3_shader_create(struct pipe_context *pctx,
 	shader->id = ++shader->compiler->shader_count;
 	shader->pctx = pctx;
 	shader->type = type;
-	shader->tokens = tgsi_dup_tokens(cso->tokens);
+	if (cso->ir == PIPE_SHADER_IR_NIR) {
+		/* TODO might need nir_clone() here.. Depends a bit on how we
+		 * define the lifecycle of cso->nir.  If we could assume that it
+		 * always sticks around until the cso is destroyed, then we'd
+		 * only need to clone in to_nir() (for the variant specific
+		 * lowering), rather than both here and to_nir().  This is a bit
+		 * different from the lifetime of cso->tokens which sometimes
+		 * but not always goes away after the pipe->pipe_create_xyz_state()
+		 * call (which is unfortunate, and maybe worth fixing).
+		 */
+		shader->nir = cso->nir;
+	} else {
+		shader->tokens = tgsi_dup_tokens(cso->tokens);
+	}
 	shader->stream_output = cso->stream_output;
 	if (fd_mesa_debug & FD_DBG_SHADERDB) {
 		/* if shader-db run, create a standard variant immediately
diff --git a/src/gallium/drivers/freedreno/ir3/ir3_shader.h b/src/gallium/drivers/freedreno/ir3/ir3_shader.h
index 7e2c27d..9ef3277 100644
--- a/src/gallium/drivers/freedreno/ir3/ir3_shader.h
+++ b/src/gallium/drivers/freedreno/ir3/ir3_shader.h
@@ -220,6 +220,8 @@ struct ir3_shader_variant {
 	struct ir3_shader *shader;
 };
 
+typedef struct nir_shader nir_shader;
+
 struct ir3_shader {
 	enum shader_t type;
 
@@ -230,6 +232,7 @@ struct ir3_shader {
 	struct ir3_compiler *compiler;
 
 	struct pipe_context *pctx;
+	nir_shader *nir;
 	const struct tgsi_token *tokens;
 	struct pipe_stream_output_info stream_output;
 
-- 
2.5.0



More information about the mesa-dev mailing list