[Mesa-dev] [PATCH 05/10] glsl: add several EmitNo* options, and MaxUnrollIterations

Luca Barbieri luca at luca-barbieri.com
Sun Sep 5 18:30:47 PDT 2010


This increases the chance that GLSL programs will actually work.

Note that continues and returns are not yet lowered, so linking
will just fail if not supported.
---
 src/glsl/glsl_parser_extras.cpp        |    4 ++--
 src/glsl/ir_optimization.h             |    2 +-
 src/glsl/linker.cpp                    |    2 +-
 src/glsl/loop_analysis.h               |    2 +-
 src/glsl/loop_unroll.cpp               |   10 ++++++----
 src/glsl/main.cpp                      |    2 +-
 src/mesa/drivers/dri/i965/brw_fs.cpp   |    2 +-
 src/mesa/main/mtypes.h                 |    7 ++++++-
 src/mesa/main/shaderapi.c              |    6 +++++-
 src/mesa/program/ir_to_mesa.cpp        |   26 ++++++++++++++++++++++----
 src/mesa/state_tracker/st_extensions.c |    5 +----
 11 files changed, 47 insertions(+), 21 deletions(-)

diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
index 390d1f0..51838b0 100644
--- a/src/glsl/glsl_parser_extras.cpp
+++ b/src/glsl/glsl_parser_extras.cpp
@@ -709,7 +709,7 @@ ast_struct_specifier::ast_struct_specifier(char *identifier,
 }
 
 bool
-do_common_optimization(exec_list *ir, bool linked)
+do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations)
 {
    GLboolean progress = GL_FALSE;
 
@@ -742,7 +742,7 @@ do_common_optimization(exec_list *ir, bool linked)
 
    loop_state *ls = analyze_loop_variables(ir);
    progress = set_loop_controls(ir, ls) || progress;
-   progress = unroll_loops(ir, ls) || progress;
+   progress = unroll_loops(ir, ls, max_unroll_iterations) || progress;
    delete ls;
 
    return progress;
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 33f4bc7..df25673 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -28,7 +28,7 @@
  * Prototypes for optimization passes to be called by the compiler and drivers.
  */
 
-bool do_common_optimization(exec_list *ir, bool linked);
+bool do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations);
 
 bool do_algebraic(exec_list *instructions);
 bool do_constant_folding(exec_list *instructions);
diff --git a/src/glsl/linker.cpp b/src/glsl/linker.cpp
index e0823c3..b5a9439 100644
--- a/src/glsl/linker.cpp
+++ b/src/glsl/linker.cpp
@@ -1470,7 +1470,7 @@ link_shaders(GLcontext *ctx, struct gl_shader_program *prog)
     * some of that unused.
     */
    for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
-      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true))
+      while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
 	 ;
    }
 
diff --git a/src/glsl/loop_analysis.h b/src/glsl/loop_analysis.h
index 893dd46..7b0511f 100644
--- a/src/glsl/loop_analysis.h
+++ b/src/glsl/loop_analysis.h
@@ -57,7 +57,7 @@ set_loop_controls(exec_list *instructions, loop_state *ls);
 
 
 extern bool
-unroll_loops(exec_list *instructions, loop_state *ls);
+unroll_loops(exec_list *instructions, loop_state *ls, unsigned max_iterations);
 
 
 /**
diff --git a/src/glsl/loop_unroll.cpp b/src/glsl/loop_unroll.cpp
index e204251..80f9217 100644
--- a/src/glsl/loop_unroll.cpp
+++ b/src/glsl/loop_unroll.cpp
@@ -27,10 +27,11 @@
 
 class loop_unroll_visitor : public ir_hierarchical_visitor {
 public:
-   loop_unroll_visitor(loop_state *state)
+   loop_unroll_visitor(loop_state *state, unsigned max_iterations)
    {
       this->state = state;
       this->progress = false;
+      this->max_iterations = max_iterations;
    }
 
    virtual ir_visitor_status visit_leave(ir_loop *ir);
@@ -38,6 +39,7 @@ public:
    loop_state *state;
 
    bool progress;
+   unsigned max_iterations;
 };
 
 
@@ -62,7 +64,7 @@ loop_unroll_visitor::visit_leave(ir_loop *ir)
 
    /* Don't try to unroll loops that have zillions of iterations either.
     */
-   if (ls->max_iterations > 32)
+   if (ls->max_iterations > max_iterations)
       return visit_continue;
 
    if (ls->num_loop_jumps > 0)
@@ -90,9 +92,9 @@ loop_unroll_visitor::visit_leave(ir_loop *ir)
 
 
 bool
-unroll_loops(exec_list *instructions, loop_state *ls)
+unroll_loops(exec_list *instructions, loop_state *ls, unsigned max_iterations)
 {
-   loop_unroll_visitor v(ls);
+   loop_unroll_visitor v(ls, max_iterations);
 
    v.run(instructions);
 
diff --git a/src/glsl/main.cpp b/src/glsl/main.cpp
index 30f11a5..8e73eb8 100644
--- a/src/glsl/main.cpp
+++ b/src/glsl/main.cpp
@@ -178,7 +178,7 @@ compile_shader(struct gl_shader *shader)
 
 	 loop_state *ls = analyze_loop_variables(shader->ir);
 	 progress = set_loop_controls(shader->ir, ls) || progress;
-	 progress = unroll_loops(shader->ir, ls) || progress;
+	 progress = unroll_loops(shader->ir, ls, 32) || progress;
 	 delete ls;
       } while (progress);
 
diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 34c5d52..025f7c2 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -141,7 +141,7 @@ brw_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
 	 do {
 	    progress = false;
 
-	    progress = do_common_optimization(shader->ir, true) || progress;
+	    progress = do_common_optimization(shader->ir, true, 32) || progress;
 	 } while (progress);
 
 	 validate_ir_tree(shader->ir);
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 657df68..ccf3700 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -2187,7 +2187,6 @@ struct gl_shader_compiler_options
 {
    /** Driver-selectable options: */
    GLboolean EmitHighLevelInstructions; /**< IF/ELSE/ENDIF vs. BRA, etc. */
-   GLboolean EmitContReturn;            /**< Emit CONT/RET opcodes? */
    GLboolean EmitCondCodes;             /**< Use condition codes? */
    GLboolean EmitComments;              /**< Annotated instructions */
    GLboolean EmitNVTempInitialization;  /**< 0-fill NV temp registers */
@@ -2196,6 +2195,12 @@ struct gl_shader_compiler_options
     * support control flow.
     */
    GLboolean EmitNoIfs;
+   GLboolean EmitNoLoops;
+   GLboolean EmitNoFunctions;
+   GLboolean EmitNoCont;                  /**< Emit CONT opcode? */
+   GLboolean EmitNoMainReturn;            /**< Emit CONT/RET opcodes? */
+
+   GLuint MaxUnrollIterations;
 
    struct gl_sl_pragmas DefaultPragmas; /**< Default #pragma settings */
 };
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 2977a29..c32c09f 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -97,10 +97,14 @@ _mesa_init_shader_state(GLcontext *ctx)
    struct gl_shader_compiler_options options;
    GLuint i;
    options.EmitHighLevelInstructions = GL_TRUE;
-   options.EmitContReturn = GL_TRUE;
    options.EmitCondCodes = GL_FALSE;
    options.EmitComments = GL_FALSE;
    options.EmitNoIfs = GL_FALSE;
+   options.EmitNoLoops = GL_FALSE;
+   options.EmitNoFunctions = GL_FALSE;
+   options.EmitNoCont = GL_FALSE;
+   options.EmitNoMainReturn = GL_FALSE;
+   options.MaxUnrollIterations = 32;
 
    /* Default pragma settings */
    options.DefaultPragmas.IgnoreOptimize = GL_FALSE;
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index d73e338..d7de13f 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2541,6 +2541,7 @@ get_mesa_program(GLcontext *ctx, struct gl_shader_program *shader_program,
    GLenum target;
    const char *target_string;
    GLboolean progress;
+   GLboolean in_sub = GL_FALSE;
    struct gl_shader_compiler_options *options =
          &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(shader->Type)];
 
@@ -2647,16 +2648,32 @@ get_mesa_program(GLcontext *ctx, struct gl_shader_program *shader_program,
          if (mesa_inst->SrcReg[src].RelAddr)
             prog->IndirectRegisterFiles |= 1 << mesa_inst->SrcReg[src].File;
 
-      if (options->EmitNoIfs && mesa_inst->Opcode == OPCODE_IF) {
+      if (options->EmitNoIfs && mesa_inst->Opcode == OPCODE_IF)
 	 fail_link(shader_program, "Couldn't flatten if statement\n");
-      }
+
+      if (options->EmitNoLoops && mesa_inst->Opcode == OPCODE_BGNLOOP)
+         fail_link(shader_program, "Couldn't unroll loop\n");
+
+      if (options->EmitNoLoops && mesa_inst->Opcode == OPCODE_CONT)
+         fail_link(shader_program, "Couldn't convert continue to if/else\n");
+
+      if (options->EmitNoFunctions
+		      && (mesa_inst->Opcode == OPCODE_BGNSUB || mesa_inst->Opcode == OPCODE_CAL))
+	 fail_link(shader_program, "Couldn't inline function\n");
+
+      if (options->EmitNoMainReturn && !in_sub && mesa_inst->Opcode == OPCODE_RET)
+         fail_link(shader_program, "Couldn't convert return in main to if/else\n");
 
       switch (mesa_inst->Opcode) {
       case OPCODE_BGNSUB:
+         ASSERT(!in_sub);
+         in_sub = GL_TRUE;
 	 inst->function->inst = i;
 	 mesa_inst->Comment = strdup(inst->function->sig->function_name());
 	 break;
       case OPCODE_ENDSUB:
+         ASSERT(in_sub);
+         in_sub = GL_FALSE;
 	 mesa_inst->Comment = strdup(inst->function->sig->function_name());
 	 break;
       case OPCODE_CAL:
@@ -2720,6 +2737,7 @@ _mesa_ir_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
 
    for (unsigned i = 0; i < prog->_NumLinkedShaders; i++) {
       bool progress;
+      unsigned max_unroll_iterations = 32;
       exec_list *ir = prog->_LinkedShaders[i]->ir;
       struct gl_shader_compiler_options *options =
             &ctx->ShaderCompilerOptions[_mesa_shader_type_to_index(prog->_LinkedShaders[i]->Type)];
@@ -2733,7 +2751,7 @@ _mesa_ir_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
 	 do_div_to_mul_rcp(ir);
 	 do_explog_to_explog2(ir);
 
-	 progress = do_common_optimization(ir, true) || progress;
+	 progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
 
 	 if (options->EmitNoIfs)
 	    progress = do_if_to_cond_assign(ir) || progress;
@@ -2805,7 +2823,7 @@ _mesa_glsl_compile_shader(GLcontext *ctx, struct gl_shader *shader)
       /* Do some optimization at compile time to reduce shader IR size
        * and reduce later work if the same shader is linked multiple times
        */
-      while (do_common_optimization(shader->ir, false))
+      while (do_common_optimization(shader->ir, false, 32))
 	 ;
 
       validate_ir_tree(shader->ir);
diff --git a/src/mesa/state_tracker/st_extensions.c b/src/mesa/state_tracker/st_extensions.c
index 82be149..91bd5a9 100644
--- a/src/mesa/state_tracker/st_extensions.c
+++ b/src/mesa/state_tracker/st_extensions.c
@@ -135,11 +135,8 @@ void st_init_limits(struct st_context *st)
       = CLAMP(screen->get_param(screen, PIPE_CAP_MAX_RENDER_TARGETS),
               1, MAX_DRAW_BUFFERS);
 
-   /* Is TGSI_OPCODE_CONT supported? */
-   /* XXX separate query for early function return? */
    for(i = 0; i < MESA_SHADER_TYPES; ++i)
-      st->ctx->ShaderCompilerOptions[i].EmitContReturn =
-            screen->get_param(screen, PIPE_CAP_TGSI_CONT_SUPPORTED);
+      st->ctx->ShaderCompilerOptions[i].EmitNoCont = !screen->get_param(screen, PIPE_CAP_TGSI_CONT_SUPPORTED);
 
    /* Quads always follow GL provoking rules. */
    c->QuadsFollowProvokingVertexConvention = GL_FALSE;
-- 
1.7.0.4



More information about the mesa-dev mailing list