nir: Add a nir_deref_foreach_leaf helper

Signed-off-by: Jason Ekstrand <jason@jlekstrand.net>
Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Cc: "12.0" <mesa-stable@lists.freedesktop.org>
This commit is contained in:
Jason Ekstrand 2016-07-15 15:21:19 -07:00
parent 106946153f
commit dc9f2436c3
2 changed files with 120 additions and 0 deletions

View file

@ -659,6 +659,122 @@ nir_copy_deref(void *mem_ctx, nir_deref *deref)
return NULL;
}
/* This is the second step in the recursion. We've found the tail and made a
* copy. Now we need to iterate over all possible leaves and call the
* callback on each one.
*/
static bool
deref_foreach_leaf_build_recur(nir_deref_var *deref, nir_deref *tail,
nir_deref_foreach_leaf_cb cb, void *state)
{
unsigned length;
union {
nir_deref_array arr;
nir_deref_struct str;
} tmp;
assert(tail->child == NULL);
switch (glsl_get_base_type(tail->type)) {
case GLSL_TYPE_UINT:
case GLSL_TYPE_INT:
case GLSL_TYPE_FLOAT:
case GLSL_TYPE_DOUBLE:
case GLSL_TYPE_BOOL:
if (glsl_type_is_vector_or_scalar(tail->type))
return cb(deref, state);
/* Fall Through */
case GLSL_TYPE_ARRAY:
tmp.arr.deref.deref_type = nir_deref_type_array;
tmp.arr.deref.type = glsl_get_array_element(tail->type);
tmp.arr.deref_array_type = nir_deref_array_type_direct;
tmp.arr.indirect = NIR_SRC_INIT;
tail->child = &tmp.arr.deref;
length = glsl_get_length(tail->type);
for (unsigned i = 0; i < length; i++) {
tmp.arr.deref.child = NULL;
tmp.arr.base_offset = i;
if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
return false;
}
return true;
case GLSL_TYPE_STRUCT:
tmp.str.deref.deref_type = nir_deref_type_struct;
tail->child = &tmp.str.deref;
length = glsl_get_length(tail->type);
for (unsigned i = 0; i < length; i++) {
tmp.arr.deref.child = NULL;
tmp.str.deref.type = glsl_get_struct_field(tail->type, i);
tmp.str.index = i;
if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
return false;
}
return true;
default:
unreachable("Invalid type for dereference");
}
}
/* This is the first step of the foreach_leaf recursion. In this step we are
* walking to the end of the deref chain and making a copy in the stack as we
* go. This is because we don't want to mutate the deref chain that was
* passed in by the caller. The downside is that this deref chain is on the
* stack and , if the caller wants to do anything with it, they will have to
* make their own copy because this one will go away.
*/
static bool
deref_foreach_leaf_copy_recur(nir_deref_var *deref, nir_deref *tail,
nir_deref_foreach_leaf_cb cb, void *state)
{
union {
nir_deref_array arr;
nir_deref_struct str;
} c;
if (tail->child) {
switch (tail->child->deref_type) {
case nir_deref_type_array:
c.arr = *nir_deref_as_array(tail->child);
tail->child = &c.arr.deref;
return deref_foreach_leaf_copy_recur(deref, &c.arr.deref, cb, state);
case nir_deref_type_struct:
c.str = *nir_deref_as_struct(tail->child);
tail->child = &c.str.deref;
return deref_foreach_leaf_copy_recur(deref, &c.str.deref, cb, state);
case nir_deref_type_var:
default:
unreachable("Invalid deref type for a child");
}
} else {
/* We've gotten to the end of the original deref. Time to start
* building our own derefs.
*/
return deref_foreach_leaf_build_recur(deref, tail, cb, state);
}
}
/**
* This function iterates over all of the possible derefs that can be created
* with the given deref as the head. It then calls the provided callback with
* a full deref for each one.
*
* The deref passed to the callback will be allocated on the stack. You will
* need to make a copy if you want it to hang around.
*/
bool
nir_deref_foreach_leaf(nir_deref_var *deref,
nir_deref_foreach_leaf_cb cb, void *state)
{
nir_deref_var copy = *deref;
return deref_foreach_leaf_copy_recur(&copy, &copy.deref, cb, state);
}
/* Returns a load_const instruction that represents the constant
* initializer for the given deref chain. The caller is responsible for
* ensuring that there actually is a constant initializer.

View file

@ -1943,6 +1943,10 @@ nir_deref_struct *nir_deref_struct_create(void *mem_ctx, unsigned field_index);
nir_deref *nir_copy_deref(void *mem_ctx, nir_deref *deref);
typedef bool (*nir_deref_foreach_leaf_cb)(nir_deref_var *deref, void *state);
bool nir_deref_foreach_leaf(nir_deref_var *deref,
nir_deref_foreach_leaf_cb cb, void *state);
nir_load_const_instr *
nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref);