From 87e49ea9779270f09591e5294b2e36e99b4a0f82 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Mon, 9 May 2022 12:15:44 -0500 Subject: [PATCH] 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 Part-of: --- src/compiler/nir/nir_builder.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/compiler/nir/nir_builder.h b/src/compiler/nir/nir_builder.h index 02abd9cd59d..c422eaf87d6 100644 --- a/src/compiler/nir/nir_builder.h +++ b/src/compiler/nir/nir_builder.h @@ -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.