glsl: Add uniform_locations_assigned parameter to do_dead_code opt pass

Setting this flag prevents declarations of uniforms from being removed
from the IR.  Since the IR is directly used by several API functions
that query uniforms in shaders, uniform declarations cannot be removed
after the locations have been set.  However, it should still be safe
to reorder the declarations (this is not tested).

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=41980
Tested-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Bryan Cain <bryancain3@gmail.com>
Cc: Vinson Lee <vlee@vmware.com>
Cc: José Fonseca <jfonseca@vmware.com>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
This commit is contained in:
Ian Romanick
2011-10-21 11:17:39 -07:00
parent 384ad987a1
commit 1d5d67f8ad
10 changed files with 49 additions and 17 deletions

View File

@@ -883,8 +883,27 @@ ast_struct_specifier::ast_struct_specifier(char *identifier,
this->declarations.push_degenerate_list_at_head(&declarator_list->link);
}
/**
* Do the set of common optimizations passes
*
* \param ir List of instructions to be optimized
* \param linked Is the shader linked? This enables
* optimizations passes that remove code at
* global scope and could cause linking to
* fail.
* \param uniform_locations_assigned Have locations already been assigned for
* uniforms? This prevents the declarations
* of unused uniforms from being removed.
* The setting of this flag only matters if
* \c linked is \c true.
* \param max_unroll_iterations Maximum number of loop iterations to be
* unrolled. Setting to 0 forces all loops
* to be unrolled.
*/
bool
do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations)
do_common_optimization(exec_list *ir, bool linked,
bool uniform_locations_assigned,
unsigned max_unroll_iterations)
{
GLboolean progress = GL_FALSE;
@@ -900,7 +919,7 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
progress = do_copy_propagation(ir) || progress;
progress = do_copy_propagation_elements(ir) || progress;
if (linked)
progress = do_dead_code(ir) || progress;
progress = do_dead_code(ir, uniform_locations_assigned) || progress;
else
progress = do_dead_code_unlinked(ir) || progress;
progress = do_dead_code_local(ir) || progress;

View File

@@ -37,7 +37,9 @@
#define MOD_TO_FRACT 0x20
#define INT_DIV_TO_MUL_RCP 0x40
bool do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iterations);
bool do_common_optimization(exec_list *ir, bool linked,
bool uniform_locations_assigned,
unsigned max_unroll_iterations);
bool do_algebraic(exec_list *instructions);
bool do_constant_folding(exec_list *instructions);
@@ -46,7 +48,7 @@ bool do_constant_variable_unlinked(exec_list *instructions);
bool do_copy_propagation(exec_list *instructions);
bool do_copy_propagation_elements(exec_list *instructions);
bool do_constant_propagation(exec_list *instructions);
bool do_dead_code(exec_list *instructions);
bool do_dead_code(exec_list *instructions, bool uniform_locations_assigned);
bool do_dead_code_local(exec_list *instructions);
bool do_dead_code_unlinked(exec_list *instructions);
bool do_dead_functions(exec_list *instructions);

View File

@@ -1742,7 +1742,7 @@ link_shaders(struct gl_context *ctx, struct gl_shader_program *prog)
if (ctx->ShaderCompilerOptions[i].LowerClipDistance)
lower_clip_distance(prog->_LinkedShaders[i]->ir);
while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, 32))
while (do_common_optimization(prog->_LinkedShaders[i]->ir, true, false, 32))
;
}

View File

@@ -166,7 +166,7 @@ compile_shader(struct gl_context *ctx, struct gl_shader *shader)
if (!state->error && !shader->ir->is_empty()) {
bool progress;
do {
progress = do_common_optimization(shader->ir, false, 32);
progress = do_common_optimization(shader->ir, false, false, 32);
} while (progress);
validate_ir_tree(shader->ir);

View File

@@ -42,7 +42,7 @@ static bool debug = false;
* for usage on an unlinked instruction stream.
*/
bool
do_dead_code(exec_list *instructions)
do_dead_code(exec_list *instructions, bool uniform_locations_assigned)
{
ir_variable_refcount_visitor v;
bool progress = false;
@@ -94,10 +94,11 @@ do_dead_code(exec_list *instructions)
*/
/* uniform initializers are precious, and could get used by another
* stage.
* stage. Also, once uniform locations have been assigned, the
* declaration cannot be deleted.
*/
if (entry->var->mode == ir_var_uniform &&
entry->var->constant_value)
(uniform_locations_assigned || entry->var->constant_value))
continue;
entry->var->remove();
@@ -132,7 +133,12 @@ do_dead_code_unlinked(exec_list *instructions)
foreach_iter(exec_list_iterator, sigiter, *f) {
ir_function_signature *sig =
(ir_function_signature *) sigiter.get();
if (do_dead_code(&sig->body))
/* The setting of the uniform_locations_assigned flag here is
* irrelevent. If there is a uniform declaration encountered
* inside the body of the function, something has already gone
* terribly, terribly wrong.
*/
if (do_dead_code(&sig->body, false))
progress = true;
}
}

View File

@@ -64,7 +64,7 @@ do_optimization(struct exec_list *ir, const char *optimization)
if (sscanf(optimization, "do_common_optimization ( %d , %d ) ",
&int_0, &int_1) == 2) {
return do_common_optimization(ir, int_0 != 0, int_1);
return do_common_optimization(ir, int_0 != 0, false, int_1);
} else if (strcmp(optimization, "do_algebraic") == 0) {
return do_algebraic(ir);
} else if (strcmp(optimization, "do_constant_folding") == 0) {
@@ -80,7 +80,7 @@ do_optimization(struct exec_list *ir, const char *optimization)
} else if (strcmp(optimization, "do_constant_propagation") == 0) {
return do_constant_propagation(ir);
} else if (strcmp(optimization, "do_dead_code") == 0) {
return do_dead_code(ir);
return do_dead_code(ir, false);
} else if (strcmp(optimization, "do_dead_code_local") == 0) {
return do_dead_code_local(ir);
} else if (strcmp(optimization, "do_dead_code_unlinked") == 0) {

View File

@@ -138,7 +138,8 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
false /* loops */
) || progress;
progress = do_common_optimization(shader->ir, true, 32) || progress;
progress = do_common_optimization(shader->ir, true, true, 32)
|| progress;
} while (progress);
validate_ir_tree(shader->ir);

View File

@@ -1464,7 +1464,7 @@ create_new_program(struct gl_context *ctx, struct state_key *key)
validate_ir_tree(p.shader->ir);
while (do_common_optimization(p.shader->ir, false, 32))
while (do_common_optimization(p.shader->ir, false, false, 32))
;
reparent_ir(p.shader->ir, p.shader->ir);

View File

@@ -3212,7 +3212,9 @@ _mesa_ir_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
progress = do_common_optimization(ir, true, true,
options->MaxUnrollIterations)
|| progress;
progress = lower_quadop_vector(ir, true) || progress;
@@ -3321,7 +3323,7 @@ _mesa_glsl_compile_shader(struct gl_context *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, 32))
while (do_common_optimization(shader->ir, false, false, 32))
;
validate_ir_tree(shader->ir);

View File

@@ -5058,7 +5058,9 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
progress = do_lower_jumps(ir, true, true, options->EmitNoMainReturn, options->EmitNoCont, options->EmitNoLoops) || progress;
progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
progress = do_common_optimization(ir, true, true,
options->MaxUnrollIterations)
|| progress;
progress = lower_quadop_vector(ir, false) || progress;