ir_structure_splitting: New pass to chop structures into their components.
This doesn't do anything if your structure goes through an uninlined function call or if whole-structure assignment occurs. As such, the impact is limited, at least until we do some global copy propagation to reduce whole-structure assignment.
This commit is contained in:
@@ -55,6 +55,7 @@ CXX_SOURCES = \
|
||||
ir_mod_to_fract.cpp \
|
||||
ir_print_visitor.cpp \
|
||||
ir_reader.cpp \
|
||||
ir_structure_splitting.cpp \
|
||||
ir_swizzle_swizzle.cpp \
|
||||
ir_tree_grafting.cpp \
|
||||
ir_validate.cpp \
|
||||
|
@@ -44,6 +44,7 @@ bool do_if_simplification(exec_list *instructions);
|
||||
bool do_if_to_cond_assign(exec_list *instructions);
|
||||
bool do_mat_op_to_vec(exec_list *instructions);
|
||||
bool do_mod_to_fract(exec_list *instructions);
|
||||
bool do_structure_splitting(exec_list *instructions);
|
||||
bool do_swizzle_swizzle(exec_list *instructions);
|
||||
bool do_tree_grafting(exec_list *instructions);
|
||||
bool do_vec_index_to_cond_assign(exec_list *instructions);
|
||||
|
378
src/glsl/ir_structure_splitting.cpp
Normal file
378
src/glsl/ir_structure_splitting.cpp
Normal file
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
* Copyright © 2010 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice (including the next
|
||||
* paragraph) shall be included in all copies or substantial portions of the
|
||||
* Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \file ir_structure_splitting.cpp
|
||||
*
|
||||
* If a structure is only ever referenced by its components, then
|
||||
* split those components out to individual variables so they can be
|
||||
* handled normally by other optimization passes.
|
||||
*
|
||||
* This skips structures like uniforms, which need to be accessible as
|
||||
* structures for their access by the GL.
|
||||
*/
|
||||
|
||||
#include "ir.h"
|
||||
#include "ir_visitor.h"
|
||||
#include "glsl_types.h"
|
||||
|
||||
class variable_entry : public exec_node
|
||||
{
|
||||
public:
|
||||
variable_entry(ir_variable *var)
|
||||
{
|
||||
this->var = var;
|
||||
this->whole_structure_access = 0;
|
||||
this->declaration = false;
|
||||
this->components = NULL;
|
||||
this->mem_ctx = NULL;
|
||||
}
|
||||
|
||||
ir_variable *var; /* The key: the variable's pointer. */
|
||||
|
||||
/** Number of times the variable is referenced, including assignments. */
|
||||
unsigned whole_structure_access;
|
||||
|
||||
bool declaration; /* If the variable had a decl in the instruction stream */
|
||||
|
||||
ir_variable **components;
|
||||
|
||||
/** talloc_parent(this->var) -- the shader's talloc context. */
|
||||
void *mem_ctx;
|
||||
};
|
||||
|
||||
class ir_structure_reference_visitor : public ir_hierarchical_visitor {
|
||||
public:
|
||||
ir_structure_reference_visitor(void)
|
||||
{
|
||||
this->mem_ctx = talloc_new(NULL);
|
||||
this->variable_list.make_empty();
|
||||
}
|
||||
|
||||
~ir_structure_reference_visitor(void)
|
||||
{
|
||||
talloc_free(mem_ctx);
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit(ir_variable *);
|
||||
virtual ir_visitor_status visit(ir_dereference_variable *);
|
||||
virtual ir_visitor_status visit(ir_dereference_record *);
|
||||
|
||||
virtual ir_visitor_status visit_enter(ir_function_signature *);
|
||||
|
||||
variable_entry *get_variable_entry(ir_variable *var);
|
||||
|
||||
/* List of variable_entry */
|
||||
exec_list variable_list;
|
||||
|
||||
void *mem_ctx;
|
||||
};
|
||||
|
||||
variable_entry *
|
||||
ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
|
||||
{
|
||||
assert(var);
|
||||
|
||||
if (!var->type->is_record())
|
||||
return NULL;
|
||||
|
||||
foreach_iter(exec_list_iterator, iter, this->variable_list) {
|
||||
variable_entry *entry = (variable_entry *)iter.get();
|
||||
if (entry->var == var)
|
||||
return entry;
|
||||
}
|
||||
|
||||
variable_entry *entry = new(mem_ctx) variable_entry(var);
|
||||
this->variable_list.push_tail(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_reference_visitor::visit(ir_variable *ir)
|
||||
{
|
||||
variable_entry *entry = this->get_variable_entry(ir);
|
||||
|
||||
if (entry)
|
||||
entry->declaration = true;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
|
||||
{
|
||||
ir_variable *const var = ir->variable_referenced();
|
||||
variable_entry *entry = this->get_variable_entry(var);
|
||||
|
||||
if (entry)
|
||||
entry->whole_structure_access++;
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_reference_visitor::visit(ir_dereference_record *ir)
|
||||
{
|
||||
/* Don't descend into the ir_dereference_variable below. */
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
|
||||
{
|
||||
/* We don't want to descend into the function parameters and
|
||||
* dead-code eliminate them, so just accept the body here.
|
||||
*/
|
||||
visit_list_elements(this, &ir->body);
|
||||
return visit_continue_with_parent;
|
||||
}
|
||||
|
||||
class ir_structure_splitting_visitor : public ir_hierarchical_visitor {
|
||||
public:
|
||||
ir_structure_splitting_visitor(exec_list *vars)
|
||||
{
|
||||
this->variable_list = vars;
|
||||
}
|
||||
|
||||
virtual ~ir_structure_splitting_visitor()
|
||||
{
|
||||
}
|
||||
|
||||
virtual ir_visitor_status visit_leave(ir_assignment *);
|
||||
virtual ir_visitor_status visit_leave(ir_call *);
|
||||
virtual ir_visitor_status visit_leave(ir_dereference_array *);
|
||||
virtual ir_visitor_status visit_leave(ir_expression *);
|
||||
virtual ir_visitor_status visit_leave(ir_if *);
|
||||
virtual ir_visitor_status visit_leave(ir_return *);
|
||||
virtual ir_visitor_status visit_leave(ir_swizzle *);
|
||||
virtual ir_visitor_status visit_leave(ir_texture *);
|
||||
|
||||
void split_deref(ir_dereference **deref);
|
||||
void split_rvalue(ir_rvalue **rvalue);
|
||||
struct variable_entry *get_splitting_entry(ir_variable *var);
|
||||
|
||||
exec_list *variable_list;
|
||||
void *mem_ctx;
|
||||
};
|
||||
|
||||
struct variable_entry *
|
||||
ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
|
||||
{
|
||||
assert(var);
|
||||
|
||||
if (!var->type->is_record())
|
||||
return NULL;
|
||||
|
||||
foreach_iter(exec_list_iterator, iter, *this->variable_list) {
|
||||
variable_entry *entry = (variable_entry *)iter.get();
|
||||
if (entry->var == var) {
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
|
||||
{
|
||||
if ((*deref)->ir_type != ir_type_dereference_record)
|
||||
return;
|
||||
|
||||
ir_dereference_record *deref_record = (ir_dereference_record *)deref;
|
||||
ir_dereference_variable *deref_var = deref_record->as_dereference_variable();
|
||||
if (!deref_var)
|
||||
return;
|
||||
|
||||
variable_entry *entry = get_splitting_entry(deref_var->var);
|
||||
if (entry)
|
||||
return;
|
||||
|
||||
unsigned int i;
|
||||
for (i = 0; i < entry->var->type->length; i++) {
|
||||
if (strcmp(deref_record->field,
|
||||
entry->var->type->fields.structure[i].name) == 0)
|
||||
break;
|
||||
}
|
||||
assert(i != entry->var->type->length);
|
||||
|
||||
*deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
|
||||
}
|
||||
|
||||
void
|
||||
ir_structure_splitting_visitor::split_rvalue(ir_rvalue **rvalue)
|
||||
{
|
||||
ir_dereference *deref = (*rvalue)->as_dereference();
|
||||
|
||||
if (!deref)
|
||||
return;
|
||||
|
||||
split_deref(&deref);
|
||||
*rvalue = deref;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_expression *ir)
|
||||
{
|
||||
unsigned int operand;
|
||||
|
||||
for (operand = 0; operand < ir->get_num_operands(); operand++) {
|
||||
split_rvalue(&ir->operands[operand]);
|
||||
}
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_texture *ir)
|
||||
{
|
||||
split_rvalue(&ir->coordinate);
|
||||
split_rvalue(&ir->projector);
|
||||
split_rvalue(&ir->shadow_comparitor);
|
||||
|
||||
switch (ir->op) {
|
||||
case ir_tex:
|
||||
break;
|
||||
case ir_txb:
|
||||
split_rvalue(&ir->lod_info.bias);
|
||||
break;
|
||||
case ir_txf:
|
||||
case ir_txl:
|
||||
split_rvalue(&ir->lod_info.lod);
|
||||
break;
|
||||
case ir_txd:
|
||||
split_rvalue(&ir->lod_info.grad.dPdx);
|
||||
split_rvalue(&ir->lod_info.grad.dPdy);
|
||||
break;
|
||||
}
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_swizzle *ir)
|
||||
{
|
||||
split_rvalue(&ir->val);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_dereference_array *ir)
|
||||
{
|
||||
split_rvalue(&ir->array_index);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
|
||||
{
|
||||
split_rvalue(&ir->rhs);
|
||||
split_rvalue(&ir->condition);
|
||||
split_deref(&ir->lhs);
|
||||
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_call *ir)
|
||||
{
|
||||
foreach_iter(exec_list_iterator, iter, *ir) {
|
||||
ir_rvalue *param = (ir_rvalue *)iter.get();
|
||||
ir_rvalue *new_param = param;
|
||||
split_rvalue(&new_param);
|
||||
|
||||
if (new_param != param) {
|
||||
param->replace_with(new_param);
|
||||
}
|
||||
}
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_return *ir)
|
||||
{
|
||||
split_rvalue(&ir->value);;
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
ir_visitor_status
|
||||
ir_structure_splitting_visitor::visit_leave(ir_if *ir)
|
||||
{
|
||||
split_rvalue(&ir->condition);
|
||||
return visit_continue;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
do_structure_splitting(exec_list *instructions)
|
||||
{
|
||||
ir_structure_reference_visitor refs;
|
||||
void *mem_ctx = talloc_new(NULL);
|
||||
|
||||
/* Trim out variables we can't split. */
|
||||
foreach_iter(exec_list_iterator, iter, refs.variable_list) {
|
||||
variable_entry *entry = (variable_entry *)iter.get();
|
||||
if (!entry->declaration || entry->whole_structure_access) {
|
||||
entry->remove();
|
||||
}
|
||||
}
|
||||
|
||||
if (refs.variable_list.is_empty())
|
||||
return false;
|
||||
|
||||
/* Replace the decls of the structures to be split with their split
|
||||
* components.
|
||||
*/
|
||||
foreach_iter(exec_list_iterator, iter, refs.variable_list) {
|
||||
variable_entry *entry = (variable_entry *)iter.get();
|
||||
const struct glsl_type *type = entry->var->type;
|
||||
|
||||
entry->mem_ctx = talloc_parent(entry->var);
|
||||
|
||||
entry->components = talloc_array(mem_ctx,
|
||||
ir_variable *,
|
||||
type->length);
|
||||
|
||||
for (unsigned int i = 0; i < entry->var->type->length; i++) {
|
||||
const char *name = talloc_asprintf(mem_ctx, "%s_%s",
|
||||
type->name,
|
||||
type->fields.structure[i].name);
|
||||
|
||||
entry->components[i] =
|
||||
new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
|
||||
name,
|
||||
ir_var_temporary);
|
||||
entry->var->insert_before(entry->components[i]);
|
||||
}
|
||||
|
||||
entry->var->remove();
|
||||
}
|
||||
|
||||
ir_structure_splitting_visitor split(&refs.variable_list);
|
||||
visit_list_elements(&split, instructions);
|
||||
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
return true;
|
||||
}
|
@@ -1287,6 +1287,7 @@ link_shaders(struct gl_shader_program *prog)
|
||||
|
||||
progress = do_function_inlining(ir) || progress;
|
||||
progress = do_dead_functions(ir) || progress;
|
||||
progress = do_structure_splitting(ir) || progress;
|
||||
progress = do_if_simplification(ir) || progress;
|
||||
progress = do_copy_propagation(ir) || progress;
|
||||
progress = do_dead_code_local(ir) || progress;
|
||||
|
Reference in New Issue
Block a user