mesa/src/compiler/nir/tests/builder_tests.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
3.9 KiB
C++
Raw Normal View History

2019-11-08 19:24:05 -06:00
/*
* Copyright © 2018 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.
*/
#include "nir_test.h"
2019-11-08 19:24:05 -06:00
namespace {
class nir_builder_test : public nir_test {
2019-11-08 19:24:05 -06:00
private:
const glsl_type *type_for_def(nir_def *def)
2019-11-08 19:24:05 -06:00
{
switch (def->bit_size) {
case 8: return glsl_u8vec_type(def->num_components);
case 16: return glsl_u16vec_type(def->num_components);
case 32: return glsl_uvec_type(def->num_components);
case 64: return glsl_u64vec_type(def->num_components);
build: avoid redefining unreachable() which is standard in C23 In the C23 standard unreachable() is now a predefined function-like macro in <stddef.h> See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in And this causes build errors when building for C23: ----------------------------------------------------------------------- In file included from ../src/util/log.h:30, from ../src/util/log.c:30: ../src/util/macros.h:123:9: warning: "unreachable" redefined 123 | #define unreachable(str) \ | ^~~~~~~~~~~ In file included from ../src/util/macros.h:31: /usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition 456 | #define unreachable() (__builtin_unreachable ()) | ^~~~~~~~~~~ ----------------------------------------------------------------------- So don't redefine it with the same name, but use the name UNREACHABLE() to also signify it's a macro. Using a different name also makes sense because the behavior of the macro was extending the one of __builtin_unreachable() anyway, and it also had a different signature, accepting one argument, compared to the standard unreachable() with no arguments. This change improves the chances of building mesa with the C23 standard, which for instance is the default in recent AOSP versions. All the instances of the macro, including the definition, were updated with the following command line: git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \ while read file; \ do \ sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \ done && \ sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-23 09:17:35 +02:00
default: UNREACHABLE("Invalid bit size");
2019-11-08 19:24:05 -06:00
}
}
protected:
nir_builder_test()
: nir_test::nir_test("nir_builder_test")
{
}
2019-11-08 19:24:05 -06:00
void store_test_val(nir_def *val)
2019-11-08 19:24:05 -06:00
{
nir_variable *var = nir_variable_create(b->shader, nir_var_mem_ssbo,
type_for_def(val), NULL);
nir_intrinsic_instr *store =
nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_deref);
store->num_components = val->num_components;
store->src[0] = nir_src_for_ssa(&nir_build_deref_var(b, var)->def);
2019-11-08 19:24:05 -06:00
store->src[1] = nir_src_for_ssa(val);
nir_intrinsic_set_write_mask(store, ((1 << val->num_components) - 1));
nir_builder_instr_insert(b, &store->instr);
stores.push_back(store);
}
nir_def *test_val(unsigned idx)
2019-11-08 19:24:05 -06:00
{
return stores[idx]->src[1].ssa;
}
std::vector<nir_intrinsic_instr *> stores;
};
/* Allow grouping the tests while still sharing the helpers. */
class nir_extract_bits_test : public nir_builder_test {};
} // namespace
// TODO: Re-enable this once we get vec8 support in NIR
TEST_F(nir_extract_bits_test, DISABLED_unaligned8)
{
nir_def *srcs[] = {
2019-11-08 19:24:05 -06:00
nir_imm_int(b, 0x03020100),
nir_imm_ivec2(b, 0x07060504, 0x0b0a0908),
};
store_test_val(nir_extract_bits(b, srcs, 2, 24, 1, 64));
NIR_PASS(_, b->shader, nir_opt_constant_folding);
2019-11-08 19:24:05 -06:00
nir_src val = nir_src_for_ssa(test_val(0));
ASSERT_EQ(nir_src_as_uint(val), 0x0a09080706050403);
}
TEST_F(nir_extract_bits_test, unaligned16_disabled)
{
nir_def *srcs[] = {
2019-11-08 19:24:05 -06:00
nir_imm_int(b, 0x03020100),
nir_imm_ivec2(b, 0x07060504, 0x0b0a0908),
};
store_test_val(nir_extract_bits(b, srcs, 2, 16, 1, 64));
NIR_PASS(_, b->shader, nir_opt_constant_folding);
2019-11-08 19:24:05 -06:00
nir_src val = nir_src_for_ssa(test_val(0));
ASSERT_EQ(nir_src_as_uint(val), 0x0908070605040302);
}
TEST_F(nir_extract_bits_test, mixed_bit_sizes)
{
nir_def *srcs[] = {
2019-11-08 19:24:05 -06:00
nir_imm_int(b, 0x03020100),
nir_imm_intN_t(b, 0x04, 8),
nir_imm_intN_t(b, 0x08070605, 32),
nir_vec2(b, nir_imm_intN_t(b, 0x0a09, 16),
nir_imm_intN_t(b, 0x0c0b, 16)),
};
store_test_val(nir_extract_bits(b, srcs, 4, 24, 2, 32));
NIR_PASS(_, b->shader, nir_opt_constant_folding);
2019-11-08 19:24:05 -06:00
nir_src val = nir_src_for_ssa(test_val(0));
ASSERT_EQ(nir_src_comp_as_uint(val, 0), 0x06050403);
ASSERT_EQ(nir_src_comp_as_uint(val, 1), 0x0a090807);
}