nir/builder: Add a nir_trim_vector helper

This pattern pops up a bunch and the semantics of nir_channels() aren't
very convenient much of the time.  Let's add a nir_trim_vector() which
matches nir_pad_vector().

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16309>
This commit is contained in:
Jason Ekstrand 2022-05-09 12:15:44 -05:00 committed by Dylan Baker
parent 23bcaaeaaa
commit 87e49ea977

View file

@ -1003,6 +1003,16 @@ nir_bitcast_vector(nir_builder *b, nir_ssa_def *src, unsigned dest_bit_size)
return nir_extract_bits(b, &src, 1, 0, dest_num_components, dest_bit_size);
}
static inline nir_ssa_def *
nir_trim_vector(nir_builder *b, nir_ssa_def *src, unsigned num_components)
{
assert(src->num_components >= num_components);
if (src->num_components == num_components)
return src;
return nir_channels(b, src, nir_component_mask(num_components));
}
/**
* Pad a value to N components with undefs of matching bit size.
* If the value already contains >= num_components, it is returned without change.