From 08b93c841acf427541fcf09644ce8cd7ce8a7891 Mon Sep 17 00:00:00 2001 From: Timothy Arceri Date: Wed, 14 Aug 2024 15:18:53 +1000 Subject: [PATCH] nir: make static assert more flexible The static assert used in encode deref modes used the fact there was less than 16 modes that we wanted to compress as an opportunity to reuse MODE_ENC_GENERIC_BIT as it just happened to represent 16. However if we add more than 16 modes i.e need to compress to 6 bits not 5 bits then MODE_ENC_GENERIC_BIT becomes 32 and the logic in the assert breaks. Instead we more precisely make sure MODE_ENC_GENERIC_BIT is large enough to fit all but the last 4 generic modes and that the last 4 modes defined in the enum are in fact the 4 generic modes. Acked-by: Alyssa Rosenzweig Part-of: --- src/compiler/nir/nir_serialize.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/compiler/nir/nir_serialize.c b/src/compiler/nir/nir_serialize.c index 17f687f47f7..d574050c3c9 100644 --- a/src/compiler/nir/nir_serialize.c +++ b/src/compiler/nir/nir_serialize.c @@ -825,6 +825,7 @@ read_alu(read_ctx *ctx, union packed_instr header) return alu; } +#define NUM_GENERIC_MODES 4 #define MODE_ENC_GENERIC_BIT (1 << 4) static nir_variable_mode @@ -848,8 +849,16 @@ encode_deref_modes(nir_variable_mode modes) * can compress them by only storing the bit position. This, plus one bit * to select encoding, lets us pack the entire bitfield in 5 bits. */ + + /* Assert that the modes we are compressing fit along with the generic bit + */ + STATIC_ASSERT((nir_num_variable_modes - NUM_GENERIC_MODES) < + MODE_ENC_GENERIC_BIT); + + /* Assert that the generic modes are defined at the end of the modes enum + */ STATIC_ASSERT((nir_var_all & ~nir_var_mem_generic) < - (1 << MODE_ENC_GENERIC_BIT)); + (1 << (nir_num_variable_modes - NUM_GENERIC_MODES))); unsigned enc; if (modes == 0 || (modes & nir_var_mem_generic)) {