From 6eb0a3a5b7c6726735218c59e023d36490570af0 Mon Sep 17 00:00:00 2001 From: Konstantin Seurer Date: Thu, 20 Jul 2023 10:33:52 +0200 Subject: [PATCH] nir/tests: Refactor boilerplate into a common header Reviewed-by: Rhys Perry Part-of: --- src/compiler/nir/tests/algebraic_tests.cpp | 23 +-- src/compiler/nir/tests/builder_tests.cpp | 36 +--- src/compiler/nir/tests/control_flow_tests.cpp | 45 ++-- src/compiler/nir/tests/core_tests.cpp | 38 +--- src/compiler/nir/tests/dce_tests.cpp | 47 ++--- .../nir/tests/load_store_vectorizer_tests.cpp | 35 +--- .../nir/tests/lower_alu_width_tests.cpp | 43 ++-- src/compiler/nir/tests/mod_analysis_tests.cpp | 57 +++-- src/compiler/nir/tests/nir_test.h | 41 ++++ src/compiler/nir/tests/opt_if_tests.cpp | 94 ++++----- .../nir/tests/opt_shrink_vectors_tests.cpp | 194 ++++++++---------- .../nir/tests/range_analysis_tests.cpp | 84 +++----- src/compiler/nir/tests/vars_tests.cpp | 20 +- 13 files changed, 293 insertions(+), 464 deletions(-) create mode 100644 src/compiler/nir/tests/nir_test.h diff --git a/src/compiler/nir/tests/algebraic_tests.cpp b/src/compiler/nir/tests/algebraic_tests.cpp index 2cadb3927f8..649a6896d2e 100644 --- a/src/compiler/nir/tests/algebraic_tests.cpp +++ b/src/compiler/nir/tests/algebraic_tests.cpp @@ -22,17 +22,13 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" namespace { -class algebraic_test_base : public ::testing::Test { +class algebraic_test_base : public nir_test { protected: algebraic_test_base(); - ~algebraic_test_base(); virtual void run_pass()=0; @@ -42,27 +38,14 @@ protected: void test_2src_op(nir_op op, int64_t src0, int64_t src1); nir_variable *res_var; - nir_builder *b, _b; }; algebraic_test_base::algebraic_test_base() + : nir_test::nir_test("nir_opt_algebraic_test") { - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "opt_algebraic test"); - b = &_b; - res_var = nir_local_variable_create(b->impl, glsl_int_type(), "res"); } -algebraic_test_base::~algebraic_test_base() -{ - ralloc_free(b->shader); - - glsl_type_singleton_decref(); -} - void algebraic_test_base::test_op(nir_op op, nir_ssa_def *src0, nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3, const char *desc) { diff --git a/src/compiler/nir/tests/builder_tests.cpp b/src/compiler/nir/tests/builder_tests.cpp index 6b476ce155c..637b014d614 100644 --- a/src/compiler/nir/tests/builder_tests.cpp +++ b/src/compiler/nir/tests/builder_tests.cpp @@ -21,14 +21,11 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" namespace { -class nir_builder_test : public ::testing::Test { +class nir_builder_test : public nir_test { private: const glsl_type *type_for_def(nir_ssa_def *def) { @@ -42,8 +39,10 @@ private: } protected: - nir_builder_test(); - ~nir_builder_test(); + nir_builder_test() + : nir_test::nir_test("nir_builder_test") + { + } void store_test_val(nir_ssa_def *val) { @@ -66,31 +65,8 @@ protected: } std::vector stores; - - nir_builder *b, _b; }; -nir_builder_test::nir_builder_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "builder test"); - b = &_b; -} - -nir_builder_test::~nir_builder_test() -{ - if (HasFailure()) { - printf("\nShader from the failed test:\n\n"); - nir_print_shader(b->shader, stdout); - } - - ralloc_free(b->shader); - - glsl_type_singleton_decref(); -} - /* Allow grouping the tests while still sharing the helpers. */ class nir_extract_bits_test : public nir_builder_test {}; diff --git a/src/compiler/nir/tests/control_flow_tests.cpp b/src/compiler/nir/tests/control_flow_tests.cpp index aad4566eb6d..31e1398d67e 100644 --- a/src/compiler/nir/tests/control_flow_tests.cpp +++ b/src/compiler/nir/tests/control_flow_tests.cpp @@ -20,45 +20,30 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include "nir.h" -#include "nir_builder.h" -class nir_cf_test : public ::testing::Test { +#include "nir_test.h" + +class nir_cf_test : public nir_test { protected: - nir_cf_test(); - ~nir_cf_test(); - - nir_builder b; + nir_cf_test() + : nir_test::nir_test("nir_cf_test") + { + } }; -nir_cf_test::nir_cf_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "cf test"); -} - -nir_cf_test::~nir_cf_test() -{ - ralloc_free(b.shader); - glsl_type_singleton_decref(); -} - TEST_F(nir_cf_test, delete_break_in_loop) { /* Create IR: * * while (...) { break; } */ - nir_loop *loop = nir_loop_create(b.shader); - nir_cf_node_insert(nir_after_cf_list(&b.impl->body), &loop->cf_node); + nir_loop *loop = nir_loop_create(b->shader); + nir_cf_node_insert(nir_after_cf_list(&b->impl->body), &loop->cf_node); - b.cursor = nir_after_cf_list(&loop->body); + b->cursor = nir_after_cf_list(&loop->body); - nir_jump_instr *jump = nir_jump_instr_create(b.shader, nir_jump_break); - nir_builder_instr_insert(&b, &jump->instr); + nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_break); + nir_builder_instr_insert(b, &jump->instr); /* At this point, we should have: * @@ -78,10 +63,10 @@ TEST_F(nir_cf_test, delete_break_in_loop) * block block_3: * } */ - nir_block *block_0 = nir_start_block(b.impl); + nir_block *block_0 = nir_start_block(b->impl); nir_block *block_1 = nir_loop_first_block(loop); nir_block *block_2 = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node)); - nir_block *block_3 = b.impl->end_block; + nir_block *block_3 = b->impl->end_block; ASSERT_EQ(nir_cf_node_block, block_0->cf_node.type); ASSERT_EQ(nir_cf_node_block, block_1->cf_node.type); ASSERT_EQ(nir_cf_node_block, block_2->cf_node.type); @@ -143,5 +128,5 @@ TEST_F(nir_cf_test, delete_break_in_loop) EXPECT_FALSE(_mesa_set_search(block_2->predecessors, block_1)); EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2)); - nir_metadata_require(b.impl, nir_metadata_dominance); + nir_metadata_require(b->impl, nir_metadata_dominance); } diff --git a/src/compiler/nir/tests/core_tests.cpp b/src/compiler/nir/tests/core_tests.cpp index 0d5820e6b3f..4bed211763a 100644 --- a/src/compiler/nir/tests/core_tests.cpp +++ b/src/compiler/nir/tests/core_tests.cpp @@ -21,43 +21,19 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" namespace { -class nir_core_test : public ::testing::Test { +class nir_core_test : public nir_test { protected: - nir_core_test(); - ~nir_core_test(); - - bool shader_contains_def(nir_ssa_def *def); - - nir_builder *b, _b; -}; - -nir_core_test::nir_core_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "builder test"); - b = &_b; -} - -nir_core_test::~nir_core_test() -{ - if (HasFailure()) { - printf("\nShader from the failed test:\n\n"); - nir_print_shader(b->shader, stdout); + nir_core_test() + : nir_test::nir_test("nir_core_test") + { } - ralloc_free(b->shader); - - glsl_type_singleton_decref(); -} + bool shader_contains_def(nir_ssa_def *def); +}; struct contains_def_state { nir_ssa_def *def; diff --git a/src/compiler/nir/tests/dce_tests.cpp b/src/compiler/nir/tests/dce_tests.cpp index bcd230e32f5..42afcc01af5 100644 --- a/src/compiler/nir/tests/dce_tests.cpp +++ b/src/compiler/nir/tests/dce_tests.cpp @@ -20,32 +20,17 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include "nir.h" -#include "nir_builder.h" -class nir_opt_dce_test : public ::testing::Test { +#include "nir_test.h" + +class nir_opt_dce_test : public nir_test { protected: - nir_opt_dce_test(); - ~nir_opt_dce_test(); - - nir_builder bld; + nir_opt_dce_test() + : nir_test::nir_test("nir_opt_dce_test") + { + } }; -nir_opt_dce_test::nir_opt_dce_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "dce test"); -} - -nir_opt_dce_test::~nir_opt_dce_test() -{ - ralloc_free(bld.shader); - glsl_type_singleton_decref(); -} - nir_phi_instr *create_one_source_phi(nir_shader *shader, nir_block *pred, nir_ssa_def *def) { @@ -84,22 +69,22 @@ TEST_F(nir_opt_dce_test, return_before_loop) * If the fast path is taken here, ssa_0 will be incorrectly DCE'd. */ - nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_int_type(), "out"); + nir_variable *var = nir_variable_create(b->shader, nir_var_shader_out, glsl_int_type(), "out"); - nir_jump(&bld, nir_jump_return); + nir_jump(b, nir_jump_return); - nir_loop *loop = nir_push_loop(&bld); + nir_loop *loop = nir_push_loop(b); - nir_ssa_def *one = nir_imm_int(&bld, 1); + nir_ssa_def *one = nir_imm_int(b, 1); - nir_phi_instr *phi = create_one_source_phi(bld.shader, one->parent_instr->block, one); + nir_phi_instr *phi = create_one_source_phi(b->shader, one->parent_instr->block, one); nir_instr_insert_before_block(one->parent_instr->block, &phi->instr); - nir_store_var(&bld, var, &phi->dest.ssa, 0x1); + nir_store_var(b, var, &phi->dest.ssa, 0x1); - nir_pop_loop(&bld, loop); + nir_pop_loop(b, loop); - ASSERT_FALSE(nir_opt_dce(bld.shader)); + ASSERT_FALSE(nir_opt_dce(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); } diff --git a/src/compiler/nir/tests/load_store_vectorizer_tests.cpp b/src/compiler/nir/tests/load_store_vectorizer_tests.cpp index 3e748aeda65..3ef46b30b85 100644 --- a/src/compiler/nir/tests/load_store_vectorizer_tests.cpp +++ b/src/compiler/nir/tests/load_store_vectorizer_tests.cpp @@ -21,10 +21,7 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" /* This is a macro so you get good line numbers */ #define EXPECT_INSTR_SWIZZLES(instr, load, expected_swizzle) \ @@ -33,10 +30,12 @@ namespace { -class nir_load_store_vectorize_test : public ::testing::Test { +class nir_load_store_vectorize_test : public nir_test { protected: - nir_load_store_vectorize_test(); - ~nir_load_store_vectorize_test(); + nir_load_store_vectorize_test() + : nir_test::nir_test("nir_load_store_vectorize_test") + { + } unsigned count_intrinsics(nir_intrinsic_op intrinsic); @@ -79,33 +78,11 @@ protected: std::string swizzle(nir_alu_instr *instr, int src); - nir_builder *b, _b; std::map movs; std::map loads; std::map res_map; }; -nir_load_store_vectorize_test::nir_load_store_vectorize_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "load store tests"); - b = &_b; -} - -nir_load_store_vectorize_test::~nir_load_store_vectorize_test() -{ - if (HasFailure()) { - printf("\nShader from the failed test:\n\n"); - nir_print_shader(b->shader, stdout); - } - - ralloc_free(b->shader); - - glsl_type_singleton_decref(); -} - std::string nir_load_store_vectorize_test::swizzle(nir_alu_instr *instr, int src) { diff --git a/src/compiler/nir/tests/lower_alu_width_tests.cpp b/src/compiler/nir/tests/lower_alu_width_tests.cpp index 5eb63c85160..2b1a2793d95 100644 --- a/src/compiler/nir/tests/lower_alu_width_tests.cpp +++ b/src/compiler/nir/tests/lower_alu_width_tests.cpp @@ -21,50 +21,33 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" namespace { -class nir_lower_alu_width_test : public ::testing::Test { +class nir_lower_alu_width_test : public nir_test { protected: - nir_lower_alu_width_test(); - ~nir_lower_alu_width_test(); - - nir_builder b; + nir_lower_alu_width_test() + : nir_test::nir_test("nir_lower_alu_width_test") + { + } }; -nir_lower_alu_width_test::nir_lower_alu_width_test() -{ - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "nir_lower_alu_width test"); -} - -nir_lower_alu_width_test::~nir_lower_alu_width_test() -{ - ralloc_free(b.shader); - glsl_type_singleton_decref(); -} - TEST_F(nir_lower_alu_width_test, fdot_order) { - nir_variable *res_var = nir_local_variable_create(b.impl, glsl_float_type(), "res"); + nir_variable *res_var = nir_local_variable_create(b->impl, glsl_float_type(), "res"); - b.exact = true; + b->exact = true; /* If this isn't done in xyz order, it evaluates to infinity. */ nir_ssa_def *val = nir_fdot( - &b, nir_imm_vec3(&b, 1.7014118346046923e+38, 1.7014118346046923e+38, 8.507059173023462e+37), - nir_imm_vec3(&b, -0.5, 1.5, 1.0)); + b, nir_imm_vec3(b, 1.7014118346046923e+38, 1.7014118346046923e+38, 8.507059173023462e+37), + nir_imm_vec3(b, -0.5, 1.5, 1.0)); nir_intrinsic_instr *store = - nir_build_store_deref(&b, &nir_build_deref_var(&b, res_var)->dest.ssa, val); + nir_build_store_deref(b, &nir_build_deref_var(b, res_var)->dest.ssa, val); - nir_lower_alu_width(b.shader, NULL, NULL); - nir_opt_constant_folding(b.shader); + nir_lower_alu_width(b->shader, NULL, NULL); + nir_opt_constant_folding(b->shader); ASSERT_TRUE(nir_src_is_const(store->src[1])); EXPECT_EQ(nir_src_as_float(store->src[1]), 2.5521177519070385e+38); diff --git a/src/compiler/nir/tests/mod_analysis_tests.cpp b/src/compiler/nir/tests/mod_analysis_tests.cpp index 768152d7e85..dc7fc64d1ca 100644 --- a/src/compiler/nir/tests/mod_analysis_tests.cpp +++ b/src/compiler/nir/tests/mod_analysis_tests.cpp @@ -21,10 +21,7 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" #include "util/u_math.h" static inline bool @@ -33,30 +30,22 @@ nir_mod_analysis_comp0(nir_ssa_def *val, nir_alu_type val_type, unsigned div, un return nir_mod_analysis(nir_get_ssa_scalar(val, 0), val_type, div, mod); } -class nir_mod_analysis_test : public ::testing::Test { +class nir_mod_analysis_test : public nir_test { protected: nir_mod_analysis_test(); - ~nir_mod_analysis_test(); + nir_ssa_def *nir_imul_vec2y(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1); - nir_builder b; nir_ssa_def *v[50]; nir_ssa_def *invocation; }; nir_mod_analysis_test::nir_mod_analysis_test() + : nir_test::nir_test("nir_mod_analysis_test") { - static const nir_shader_compiler_options options = { }; - b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, - "mod analysis"); for (int i = 0; i < 50; ++i) - v[i] = nir_imm_int(&b, i); - invocation = nir_load_local_invocation_index(&b); -} - -nir_mod_analysis_test::~nir_mod_analysis_test() -{ - ralloc_free(b.shader); + v[i] = nir_imm_int(b, i); + invocation = nir_load_local_invocation_index(b); } /* returns src0 * src1.y */ @@ -108,7 +97,7 @@ TEST_F(nir_mod_analysis_test, const_plus_const) for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c1 = 0; c1 < 10; ++c1) { for (unsigned c2 = 0; c2 < 10; ++c2) { - nir_ssa_def *sum = nir_iadd(&b, v[c1], v[c2]); + nir_ssa_def *sum = nir_iadd(b, v[c1], v[c2]); unsigned mod = INT32_MAX; @@ -124,7 +113,7 @@ TEST_F(nir_mod_analysis_test, dynamic_plus_const) /* (invocation + const) % const_mod should never be known unless const_mod is 1 */ for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 10; ++c) { - nir_ssa_def *sum = nir_iadd(&b, invocation, v[c]); + nir_ssa_def *sum = nir_iadd(b, invocation, v[c]); unsigned mod = INT32_MAX; @@ -144,7 +133,7 @@ TEST_F(nir_mod_analysis_test, const_mul_const) for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c1 = 0; c1 < 10; ++c1) { for (unsigned c2 = 0; c2 < 10; ++c2) { - nir_ssa_def *mul = nir_imul(&b, v[c1], v[c2]); + nir_ssa_def *mul = nir_imul(b, v[c1], v[c2]); unsigned mod = INT32_MAX; @@ -160,7 +149,7 @@ TEST_F(nir_mod_analysis_test, dynamic_mul_const) /* (invocation * const) % const_mod == 0 only if const % const_mod == 0, unknown otherwise */ for (unsigned const_mod = 2; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 10; ++c) { - nir_ssa_def *mul = nir_imul(&b, invocation, v[c]); + nir_ssa_def *mul = nir_imul(b, invocation, v[c]); unsigned mod = INT32_MAX; @@ -179,8 +168,8 @@ TEST_F(nir_mod_analysis_test, dynamic_mul_const_swizzled) /* (invocation * const.y) % const_mod == 0 only if const.y % const_mod == 0, unknown otherwise */ for (unsigned const_mod = 2; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 10; ++c) { - nir_ssa_def *vec2 = nir_imm_ivec2(&b, 10 - c, c); - nir_ssa_def *mul = nir_imul_vec2y(&b, invocation, vec2); + nir_ssa_def *vec2 = nir_imm_ivec2(b, 10 - c, c); + nir_ssa_def *mul = nir_imul_vec2y(b, invocation, vec2); unsigned mod = INT32_MAX; @@ -201,7 +190,7 @@ TEST_F(nir_mod_analysis_test, dynamic_mul32x16_const) */ for (unsigned const_mod = 1; const_mod <= (1u << 24); const_mod *= 2) { for (unsigned c = 0; c < 10; ++c) { - nir_ssa_def *mul = nir_imul_32x16(&b, invocation, v[c]); + nir_ssa_def *mul = nir_imul_32x16(b, invocation, v[c]); unsigned mod = INT32_MAX; @@ -220,7 +209,7 @@ TEST_F(nir_mod_analysis_test, dynamic_shl_const) /* (invocation << const) % const_mod == 0 only if const >= log2(const_mod), unknown otherwise */ for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 10; ++c) { - nir_ssa_def *shl = nir_ishl(&b, invocation, v[c]); + nir_ssa_def *shl = nir_ishl(b, invocation, v[c]); unsigned mod = INT32_MAX; @@ -239,7 +228,7 @@ TEST_F(nir_mod_analysis_test, dynamic_shr_const) /* (invocation >> const) % const_mod should never be known, unless const_mod is 1 */ for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned i = 0; i < 10; ++i) { - nir_ssa_def *shr = nir_ishr(&b, invocation, v[i]); + nir_ssa_def *shr = nir_ishr(b, invocation, v[i]); unsigned mod = INT32_MAX; @@ -260,10 +249,10 @@ TEST_F(nir_mod_analysis_test, dynamic_mul_const_shr_const) * (32 >> const) is not 0 and (32 >> const) % const_mod == 0 * */ - nir_ssa_def *inv_mul_32 = nir_imul(&b, invocation, v[32]); + nir_ssa_def *inv_mul_32 = nir_imul(b, invocation, v[32]); for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 8; ++c) { - nir_ssa_def *shr = nir_ishr(&b, inv_mul_32, v[c]); + nir_ssa_def *shr = nir_ishr(b, inv_mul_32, v[c]); unsigned mod = INT32_MAX; @@ -284,12 +273,12 @@ TEST_F(nir_mod_analysis_test, dynamic_mul_const_swizzled_shr_const) * (32 >> const) is not 0 and (32 >> const) % const_mod == 0 * */ - nir_ssa_def *vec2 = nir_imm_ivec2(&b, 31, 32); - nir_ssa_def *inv_mul_32 = nir_imul_vec2y(&b, invocation, vec2); + nir_ssa_def *vec2 = nir_imm_ivec2(b, 31, 32); + nir_ssa_def *inv_mul_32 = nir_imul_vec2y(b, invocation, vec2); for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned c = 0; c < 8; ++c) { - nir_ssa_def *shr = nir_ishr(&b, inv_mul_32, v[c]); + nir_ssa_def *shr = nir_ishr(b, inv_mul_32, v[c]); unsigned mod = INT32_MAX; @@ -309,7 +298,7 @@ TEST_F(nir_mod_analysis_test, const_shr_const) for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { for (unsigned i = 0; i < 50; ++i) { for (unsigned j = 0; j < 6; ++j) { - nir_ssa_def *shr = nir_ishr(&b, v[i], v[j]); + nir_ssa_def *shr = nir_ishr(b, v[i], v[j]); unsigned mod = INT32_MAX; @@ -326,10 +315,10 @@ TEST_F(nir_mod_analysis_test, const_shr_const_overflow) * const_mod << const_shr is still below UINT32_MAX. */ unsigned large_const_int = 0x12345678; - nir_ssa_def *large_const = nir_imm_int(&b, large_const_int); + nir_ssa_def *large_const = nir_imm_int(b, large_const_int); for (unsigned shift = 0; shift < 30; ++shift) { - nir_ssa_def *shr = nir_ishr(&b, large_const, v[shift]); + nir_ssa_def *shr = nir_ishr(b, large_const, v[shift]); for (unsigned const_mod = 1; const_mod <= 1024; const_mod *= 2) { unsigned mod = INT32_MAX; diff --git a/src/compiler/nir/tests/nir_test.h b/src/compiler/nir/tests/nir_test.h new file mode 100644 index 00000000000..c94fce23902 --- /dev/null +++ b/src/compiler/nir/tests/nir_test.h @@ -0,0 +1,41 @@ +/* + * Copyright © 2023 Valve Corporation + * SPDX-License-Identifier: MIT + */ + +#ifndef NIR_TESTS_NIR_TEST_H +#define NIR_TESTS_NIR_TEST_H + +#include + +#include "nir.h" +#include "nir_builder.h" + +class nir_test : public ::testing::Test { + protected: + nir_test(const char *name) + { + glsl_type_singleton_init_or_ref(); + + static const nir_shader_compiler_options options = {}; + _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "%s", name); + b = &_b; + } + + virtual ~nir_test() + { + if (HasFailure()) { + printf("\nShader from the failed test:\n\n"); + nir_print_shader(b->shader, stdout); + } + + ralloc_free(b->shader); + + glsl_type_singleton_decref(); + } + + nir_builder _b; + nir_builder *b; +}; + +#endif diff --git a/src/compiler/nir/tests/opt_if_tests.cpp b/src/compiler/nir/tests/opt_if_tests.cpp index 0291a8bcb4c..30b5285b925 100644 --- a/src/compiler/nir/tests/opt_if_tests.cpp +++ b/src/compiler/nir/tests/opt_if_tests.cpp @@ -20,14 +20,12 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include "nir.h" -#include "nir_builder.h" -class nir_opt_if_test : public ::testing::Test { +#include "nir_test.h" + +class nir_opt_if_test : public nir_test { protected: nir_opt_if_test(); - ~nir_opt_if_test(); nir_builder bld; @@ -36,22 +34,12 @@ protected: }; nir_opt_if_test::nir_opt_if_test() + : nir_test::nir_test("nir_opt_if_test") { - glsl_type_singleton_init_or_ref(); + nir_variable *var = nir_variable_create(b->shader, nir_var_shader_in, glsl_int_type(), "in"); + in_def = nir_load_var(b, var); - static const nir_shader_compiler_options options = { }; - bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "if test"); - - nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_in, glsl_int_type(), "in"); - in_def = nir_load_var(&bld, var); - - out_var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_int_type(), "out"); -} - -nir_opt_if_test::~nir_opt_if_test() -{ - ralloc_free(bld.shader); - glsl_type_singleton_decref(); + out_var = nir_variable_create(b->shader, nir_var_shader_out, glsl_int_type(), "out"); } TEST_F(nir_opt_if_test, opt_if_simplification) @@ -67,21 +55,21 @@ TEST_F(nir_opt_if_test, opt_if_simplification) * } */ - nir_ssa_def *one = nir_imm_int(&bld, 1); + nir_ssa_def *one = nir_imm_int(b, 1); - nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one); - nir_if *nif = nir_push_if(&bld, cmp_result); + nir_ssa_def *cmp_result = nir_ieq(b, in_def, one); + nir_if *nif = nir_push_if(b, cmp_result); - nir_push_else(&bld, NULL); + nir_push_else(b, NULL); // do_work - nir_store_var(&bld, out_var, one, 1); + nir_store_var(b, out_var, one, 1); - nir_pop_if(&bld, NULL); + nir_pop_if(b, NULL); - ASSERT_TRUE(nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false)); + ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); ASSERT_TRUE(!exec_list_is_empty((&nir_if_first_then_block(nif)->instr_list))); ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list))); @@ -104,35 +92,35 @@ TEST_F(nir_opt_if_test, opt_if_simplification_single_source_phi_after_if) * vec1 32 ssa_3 = phi block_2: ssa_0 */ - nir_ssa_def *one = nir_imm_int(&bld, 1); + nir_ssa_def *one = nir_imm_int(b, 1); - nir_ssa_def *cmp_result = nir_ieq(&bld, in_def, one); - nir_if *nif = nir_push_if(&bld, cmp_result); + nir_ssa_def *cmp_result = nir_ieq(b, in_def, one); + nir_if *nif = nir_push_if(b, cmp_result); - nir_push_else(&bld, NULL); + nir_push_else(b, NULL); // do_work - nir_store_var(&bld, out_var, one, 1); + nir_store_var(b, out_var, one, 1); - nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_return); - nir_builder_instr_insert(&bld, &jump->instr); + nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_return); + nir_builder_instr_insert(b, &jump->instr); - nir_pop_if(&bld, NULL); + nir_pop_if(b, NULL); nir_block *then_block = nir_if_last_then_block(nif); - nir_phi_instr *const phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *const phi = nir_phi_instr_create(b->shader); nir_phi_instr_add_src(phi, then_block, nir_src_for_ssa(one)); nir_ssa_dest_init(&phi->instr, &phi->dest, one->num_components, one->bit_size); - nir_builder_instr_insert(&bld, &phi->instr); + nir_builder_instr_insert(b, &phi->instr); - ASSERT_TRUE(nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false)); + ASSERT_TRUE(nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); ASSERT_TRUE(nir_block_ends_in_jump(nir_if_last_then_block(nif))); ASSERT_TRUE(exec_list_is_empty((&nir_if_first_else_block(nif)->instr_list))); @@ -140,41 +128,41 @@ TEST_F(nir_opt_if_test, opt_if_simplification_single_source_phi_after_if) TEST_F(nir_opt_if_test, opt_if_alu_of_phi_progress) { - nir_ssa_def *two = nir_imm_int(&bld, 2); - nir_ssa_def *x = nir_imm_int(&bld, 0); + nir_ssa_def *two = nir_imm_int(b, 2); + nir_ssa_def *x = nir_imm_int(b, 0); - nir_phi_instr *phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *phi = nir_phi_instr_create(b->shader); - nir_loop *loop = nir_push_loop(&bld); + nir_loop *loop = nir_push_loop(b); { nir_ssa_dest_init(&phi->instr, &phi->dest, x->num_components, x->bit_size); nir_phi_instr_add_src(phi, x->parent_instr->block, nir_src_for_ssa(x)); - nir_ssa_def *y = nir_iadd(&bld, &phi->dest.ssa, two); - nir_store_var(&bld, out_var, - nir_imul(&bld, &phi->dest.ssa, two), 1); + nir_ssa_def *y = nir_iadd(b, &phi->dest.ssa, two); + nir_store_var(b, out_var, + nir_imul(b, &phi->dest.ssa, two), 1); - nir_phi_instr_add_src(phi, nir_cursor_current_block(bld.cursor), nir_src_for_ssa(y)); + nir_phi_instr_add_src(phi, nir_cursor_current_block(b->cursor), nir_src_for_ssa(y)); } - nir_pop_loop(&bld, loop); + nir_pop_loop(b, loop); - bld.cursor = nir_before_block(nir_loop_first_block(loop)); - nir_builder_instr_insert(&bld, &phi->instr); + b->cursor = nir_before_block(nir_loop_first_block(loop)); + nir_builder_instr_insert(b, &phi->instr); - nir_validate_shader(bld.shader, "input"); + nir_validate_shader(b->shader, "input"); bool progress; int progress_count = 0; for (int i = 0; i < 10; i++) { - progress = nir_opt_if(bld.shader, nir_opt_if_optimize_phi_true_false); + progress = nir_opt_if(b->shader, nir_opt_if_optimize_phi_true_false); if (progress) progress_count++; else break; - nir_opt_constant_folding(bld.shader); + nir_opt_constant_folding(b->shader); } EXPECT_LE(progress_count, 2); diff --git a/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp b/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp index 89c5ce268ff..060a2e003e8 100644 --- a/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp +++ b/src/compiler/nir/tests/opt_shrink_vectors_tests.cpp @@ -20,38 +20,24 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include "nir.h" -#include "nir_builder.h" -class nir_opt_shrink_vectors_test : public ::testing::Test { +#include "nir_test.h" + +class nir_opt_shrink_vectors_test : public nir_test { protected: nir_opt_shrink_vectors_test(); - ~nir_opt_shrink_vectors_test(); - - nir_builder bld; nir_ssa_def *in_def; nir_variable *out_var; }; nir_opt_shrink_vectors_test::nir_opt_shrink_vectors_test() + : nir_test::nir_test("nir_opt_shrink_vectors_test") { - glsl_type_singleton_init_or_ref(); + nir_variable *var = nir_variable_create(b->shader, nir_var_shader_in, glsl_vec_type(2), "in"); + in_def = nir_load_var(b, var); - static const nir_shader_compiler_options options = { }; - bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "opt shrink vectors test"); - - nir_variable *var = nir_variable_create(bld.shader, nir_var_shader_in, glsl_vec_type(2), "in"); - in_def = nir_load_var(&bld, var); - - out_var = nir_variable_create(bld.shader, nir_var_shader_out, glsl_vec_type(1), "out"); -} - -nir_opt_shrink_vectors_test::~nir_opt_shrink_vectors_test() -{ - ralloc_free(bld.shader); - glsl_type_singleton_decref(); + out_var = nir_variable_create(b->shader, nir_var_shader_out, glsl_vec_type(1), "out"); } static unsigned translate_swizzle(char swz) @@ -98,25 +84,25 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_load_const_trailing_compo * vec1 32 ssa_2 = fmov ssa_1.x */ - nir_ssa_def *imm_vec = nir_imm_vec4(&bld, 1.0, 2.0, 3.0, 4.0); + nir_ssa_def *imm_vec = nir_imm_vec4(b, 1.0, 2.0, 3.0, 4.0); - nir_ssa_def *alu_result = nir_build_alu1(&bld, nir_op_mov, imm_vec); + nir_ssa_def *alu_result = nir_build_alu1(b, nir_op_mov, imm_vec); nir_alu_instr *alu_instr = nir_instr_as_alu(alu_result->parent_instr); set_swizzle(&alu_instr->src[0], "x"); alu_result->num_components = 1; alu_instr->dest.write_mask = BITFIELD_MASK(1); - nir_store_var(&bld, out_var, alu_result, 1); + nir_store_var(b, out_var, alu_result, 1); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); ASSERT_TRUE(imm_vec->num_components == 1); nir_load_const_instr * imm_vec_instr = nir_instr_as_load_const(imm_vec->parent_instr); ASSERT_TRUE(nir_const_value_as_float(imm_vec_instr->value[0], 32) == 1.0); - ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_FALSE(nir_opt_shrink_vectors(b->shader)); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_alu_trailing_component_only) @@ -133,28 +119,28 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_alu_trailing_component_on * vec1 32 ssa_2 = fmov ssa_1.x */ - nir_ssa_def *alu_result = nir_build_alu1(&bld, nir_op_mov, in_def); + nir_ssa_def *alu_result = nir_build_alu1(b, nir_op_mov, in_def); nir_alu_instr *alu_instr = nir_instr_as_alu(alu_result->parent_instr); alu_result->num_components = 4; alu_instr->dest.write_mask = BITFIELD_MASK(4); set_swizzle(&alu_instr->src[0], "xyxx"); - nir_ssa_def *alu2_result = nir_build_alu1(&bld, nir_op_mov, alu_result); + nir_ssa_def *alu2_result = nir_build_alu1(b, nir_op_mov, alu_result); nir_alu_instr *alu2_instr = nir_instr_as_alu(alu2_result->parent_instr); set_swizzle(&alu2_instr->src[0], "x"); alu2_result->num_components = 1; alu2_instr->dest.write_mask = BITFIELD_MASK(1); - nir_store_var(&bld, out_var, alu2_result, 1); + nir_store_var(b, out_var, alu2_result, 1); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); check_swizzle(&alu_instr->src[0], "x"); ASSERT_TRUE(alu_result->num_components == 1); - ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_FALSE(nir_opt_shrink_vectors(b->shader)); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_simple) @@ -172,25 +158,25 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_simple) * vec1 32 ssa_4 = fdot3 ssa_3.xxy ssa_3.xxy */ - nir_ssa_def *imm_vec = nir_imm_vec4(&bld, 3.0, 1.0, 2.0, 1.0); + nir_ssa_def *imm_vec = nir_imm_vec4(b, 3.0, 1.0, 2.0, 1.0); - nir_ssa_def *alu_result = nir_build_alu2(&bld, nir_op_fadd, in_def, imm_vec); + nir_ssa_def *alu_result = nir_build_alu2(b, nir_op_fadd, in_def, imm_vec); nir_alu_instr *alu_instr = nir_instr_as_alu(alu_result->parent_instr); alu_result->num_components = 4; alu_instr->dest.write_mask = BITFIELD_MASK(4); set_swizzle(&alu_instr->src[0], "xxxy"); set_swizzle(&alu_instr->src[1], "ywyz"); - nir_ssa_def *alu2_result = nir_build_alu2(&bld, nir_op_fdot3, alu_result, alu_result); + nir_ssa_def *alu2_result = nir_build_alu2(b, nir_op_fdot3, alu_result, alu_result); nir_alu_instr *alu2_instr = nir_instr_as_alu(alu2_result->parent_instr); set_swizzle(&alu2_instr->src[0], "xzw"); set_swizzle(&alu2_instr->src[1], "xzw"); - nir_store_var(&bld, out_var, alu2_result, 1); + nir_store_var(b, out_var, alu2_result, 1); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); ASSERT_TRUE(imm_vec->num_components == 2); nir_load_const_instr * imm_vec_instr = nir_instr_as_load_const(imm_vec->parent_instr); @@ -204,9 +190,9 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_simple) check_swizzle(&alu2_instr->src[0], "xxy"); check_swizzle(&alu2_instr->src[1], "xxy"); - ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_FALSE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_vec8) @@ -238,25 +224,25 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_vec8) nir_const_value_for_float(2.0, 32), nir_const_value_for_float(6.0, 32), }; - nir_ssa_def *imm_vec = nir_build_imm(&bld, 8, 32, v); + nir_ssa_def *imm_vec = nir_build_imm(b, 8, 32, v); - nir_ssa_def *alu_result = nir_build_alu2(&bld, nir_op_fadd, in_def, imm_vec); + nir_ssa_def *alu_result = nir_build_alu2(b, nir_op_fadd, in_def, imm_vec); nir_alu_instr *alu_instr = nir_instr_as_alu(alu_result->parent_instr); alu_result->num_components = 8; alu_instr->dest.write_mask = BITFIELD_MASK(8); set_swizzle(&alu_instr->src[0], "xxxxxxxy"); set_swizzle(&alu_instr->src[1], "afhdefgh"); - nir_ssa_def *alu2_result = nir_build_alu2(&bld, nir_op_fdot8, alu_result, alu_result); + nir_ssa_def *alu2_result = nir_build_alu2(b, nir_op_fdot8, alu_result, alu_result); nir_alu_instr *alu2_instr = nir_instr_as_alu(alu2_result->parent_instr); set_swizzle(&alu2_instr->src[0], "accdefgh"); set_swizzle(&alu2_instr->src[1], "accdefgh"); - nir_store_var(&bld, out_var, alu2_result, 1); + nir_store_var(b, out_var, alu2_result, 1); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); ASSERT_TRUE(imm_vec->num_components == 8); nir_load_const_instr * imm_vec_instr = nir_instr_as_load_const(imm_vec->parent_instr); @@ -274,9 +260,9 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_vectors_vec8) check_swizzle(&alu2_instr->src[0], "abbcdefg"); check_swizzle(&alu2_instr->src[1], "abbcdefg"); - ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_FALSE(nir_opt_shrink_vectors(b->shader)); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_simple) @@ -290,14 +276,14 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_simple) * * This mimics nir for loops that come out of nine+ttn. */ - nir_ssa_def *v = nir_imm_vec4(&bld, 0.0, 0.0, 0.0, 0.0); - nir_ssa_def *increment = nir_imm_float(&bld, 1.0); - nir_ssa_def *loop_max = nir_imm_float(&bld, 3.0); + nir_ssa_def *v = nir_imm_vec4(b, 0.0, 0.0, 0.0, 0.0); + nir_ssa_def *increment = nir_imm_float(b, 1.0); + nir_ssa_def *loop_max = nir_imm_float(b, 3.0); - nir_phi_instr *const phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *const phi = nir_phi_instr_create(b->shader); nir_ssa_def *phi_def = &phi->dest.ssa; - nir_loop *loop = nir_push_loop(&bld); + nir_loop *loop = nir_push_loop(b); nir_ssa_dest_init(&phi->instr, &phi->dest, v->num_components, v->bit_size); @@ -305,20 +291,20 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_simple) nir_phi_instr_add_src(phi, v->parent_instr->block, nir_src_for_ssa(v)); - nir_ssa_def *fge = nir_fge(&bld, phi_def, loop_max); + nir_ssa_def *fge = nir_fge(b, phi_def, loop_max); nir_alu_instr *fge_alu_instr = nir_instr_as_alu(fge->parent_instr); fge->num_components = 1; fge_alu_instr->dest.write_mask = BITFIELD_MASK(1); fge_alu_instr->src[0].swizzle[0] = 1; - nir_if *nif = nir_push_if(&bld, fge); + nir_if *nif = nir_push_if(b, fge); { - nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_break); - nir_builder_instr_insert(&bld, &jump->instr); + nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_break); + nir_builder_instr_insert(b, &jump->instr); } - nir_pop_if(&bld, nif); + nir_pop_if(b, nif); - nir_ssa_def *fadd = nir_fadd(&bld, phi_def, increment); + nir_ssa_def *fadd = nir_fadd(b, phi_def, increment); nir_alu_instr *fadd_alu_instr = nir_instr_as_alu(fadd->parent_instr); fadd->num_components = 1; fadd_alu_instr->dest.write_mask = BITFIELD_MASK(1); @@ -329,15 +315,15 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_simple) srcs[i] = nir_get_ssa_scalar(phi_def, i); } srcs[1] = nir_get_ssa_scalar(fadd, 0); - nir_ssa_def *vec = nir_vec_scalars(&bld, srcs, 4); + nir_ssa_def *vec = nir_vec_scalars(b, srcs, 4); nir_phi_instr_add_src(phi, vec->parent_instr->block, nir_src_for_ssa(vec)); - nir_pop_loop(&bld, loop); + nir_pop_loop(b, loop); - bld.cursor = nir_before_block(nir_loop_first_block(loop)); - nir_builder_instr_insert(&bld, &phi->instr); + b->cursor = nir_before_block(nir_loop_first_block(loop)); + nir_builder_instr_insert(b, &phi->instr); /* Generated nir: * @@ -379,14 +365,14 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_simple) * } */ - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); ASSERT_TRUE(phi_def->num_components == 1); check_swizzle(&fge_alu_instr->src[0], "x"); check_swizzle(&fadd_alu_instr->src[0], "x"); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_swizzle) @@ -399,14 +385,14 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_swizzle) * v = vec4(v.x, v.z + 1, v.y, v.w}; * } */ - nir_ssa_def *v = nir_imm_vec4(&bld, 0.0, 0.0, 0.0, 0.0); - nir_ssa_def *increment = nir_imm_float(&bld, 1.0); - nir_ssa_def *loop_max = nir_imm_float(&bld, 3.0); + nir_ssa_def *v = nir_imm_vec4(b, 0.0, 0.0, 0.0, 0.0); + nir_ssa_def *increment = nir_imm_float(b, 1.0); + nir_ssa_def *loop_max = nir_imm_float(b, 3.0); - nir_phi_instr *const phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *const phi = nir_phi_instr_create(b->shader); nir_ssa_def *phi_def = &phi->dest.ssa; - nir_loop *loop = nir_push_loop(&bld); + nir_loop *loop = nir_push_loop(b); nir_ssa_dest_init(&phi->instr, &phi->dest, v->num_components, v->bit_size); @@ -414,20 +400,20 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_swizzle) nir_phi_instr_add_src(phi, v->parent_instr->block, nir_src_for_ssa(v)); - nir_ssa_def *fge = nir_fge(&bld, phi_def, loop_max); + nir_ssa_def *fge = nir_fge(b, phi_def, loop_max); nir_alu_instr *fge_alu_instr = nir_instr_as_alu(fge->parent_instr); fge->num_components = 1; fge_alu_instr->dest.write_mask = BITFIELD_MASK(1); fge_alu_instr->src[0].swizzle[0] = 2; - nir_if *nif = nir_push_if(&bld, fge); + nir_if *nif = nir_push_if(b, fge); - nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_break); - nir_builder_instr_insert(&bld, &jump->instr); + nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_break); + nir_builder_instr_insert(b, &jump->instr); - nir_pop_if(&bld, nif); + nir_pop_if(b, nif); - nir_ssa_def *fadd = nir_fadd(&bld, phi_def, increment); + nir_ssa_def *fadd = nir_fadd(b, phi_def, increment); nir_alu_instr *fadd_alu_instr = nir_instr_as_alu(fadd->parent_instr); fadd->num_components = 1; fadd_alu_instr->dest.write_mask = BITFIELD_MASK(1); @@ -438,15 +424,15 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_swizzle) srcs[1] = nir_get_ssa_scalar(fadd, 0); srcs[2] = nir_get_ssa_scalar(phi_def, 1); srcs[3] = nir_get_ssa_scalar(phi_def, 3); - nir_ssa_def *vec = nir_vec_scalars(&bld, srcs, 4); + nir_ssa_def *vec = nir_vec_scalars(b, srcs, 4); nir_phi_instr_add_src(phi, vec->parent_instr->block, nir_src_for_ssa(vec)); - nir_pop_loop(&bld, loop); + nir_pop_loop(b, loop); - bld.cursor = nir_before_block(nir_loop_first_block(loop)); - nir_builder_instr_insert(&bld, &phi->instr); + b->cursor = nir_before_block(nir_loop_first_block(loop)); + nir_builder_instr_insert(b, &phi->instr); /* Generated nir: * @@ -488,15 +474,15 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_swizzle) * } */ - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); - ASSERT_TRUE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_TRUE(nir_opt_shrink_vectors(b->shader)); ASSERT_TRUE(phi_def->num_components == 2); check_swizzle(&fge_alu_instr->src[0], "y"); check_swizzle(&fadd_alu_instr->src[0], "y"); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); } TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_phi_out) @@ -509,14 +495,14 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_phi_out) * } * out = v; */ - nir_ssa_def *v = nir_imm_vec4(&bld, 0.0, 0.0, 0.0, 0.0); - nir_ssa_def *increment = nir_imm_float(&bld, 1.0); - nir_ssa_def *loop_max = nir_imm_float(&bld, 3.0); + nir_ssa_def *v = nir_imm_vec4(b, 0.0, 0.0, 0.0, 0.0); + nir_ssa_def *increment = nir_imm_float(b, 1.0); + nir_ssa_def *loop_max = nir_imm_float(b, 3.0); - nir_phi_instr *const phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *const phi = nir_phi_instr_create(b->shader); nir_ssa_def *phi_def = &phi->dest.ssa; - nir_loop *loop = nir_push_loop(&bld); + nir_loop *loop = nir_push_loop(b); nir_ssa_dest_init(&phi->instr, &phi->dest, v->num_components, v->bit_size); @@ -524,20 +510,20 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_phi_out) nir_phi_instr_add_src(phi, v->parent_instr->block, nir_src_for_ssa(v)); - nir_ssa_def *fge = nir_fge(&bld, phi_def, loop_max); + nir_ssa_def *fge = nir_fge(b, phi_def, loop_max); nir_alu_instr *fge_alu_instr = nir_instr_as_alu(fge->parent_instr); fge->num_components = 1; fge_alu_instr->dest.write_mask = BITFIELD_MASK(1); fge_alu_instr->src[0].swizzle[0] = 1; - nir_if *nif = nir_push_if(&bld, fge); + nir_if *nif = nir_push_if(b, fge); { - nir_jump_instr *jump = nir_jump_instr_create(bld.shader, nir_jump_break); - nir_builder_instr_insert(&bld, &jump->instr); + nir_jump_instr *jump = nir_jump_instr_create(b->shader, nir_jump_break); + nir_builder_instr_insert(b, &jump->instr); } - nir_pop_if(&bld, nif); + nir_pop_if(b, nif); - nir_ssa_def *fadd = nir_fadd(&bld, phi_def, increment); + nir_ssa_def *fadd = nir_fadd(b, phi_def, increment); nir_alu_instr *fadd_alu_instr = nir_instr_as_alu(fadd->parent_instr); fadd->num_components = 1; fadd_alu_instr->dest.write_mask = BITFIELD_MASK(1); @@ -548,21 +534,21 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_phi_out) srcs[i] = nir_get_ssa_scalar(phi_def, i); } srcs[1] = nir_get_ssa_scalar(fadd, 0); - nir_ssa_def *vec = nir_vec_scalars(&bld, srcs, 4); + nir_ssa_def *vec = nir_vec_scalars(b, srcs, 4); nir_phi_instr_add_src(phi, vec->parent_instr->block, nir_src_for_ssa(vec)); - nir_pop_loop(&bld, loop); + nir_pop_loop(b, loop); - out_var = nir_variable_create(bld.shader, + out_var = nir_variable_create(b->shader, nir_var_shader_out, glsl_vec_type(4), "out4"); - nir_store_var(&bld, out_var, phi_def, BITFIELD_MASK(4)); + nir_store_var(b, out_var, phi_def, BITFIELD_MASK(4)); - bld.cursor = nir_before_block(nir_loop_first_block(loop)); - nir_builder_instr_insert(&bld, &phi->instr); + b->cursor = nir_before_block(nir_loop_first_block(loop)); + nir_builder_instr_insert(b, &phi->instr); /* Generated nir: * @@ -606,8 +592,8 @@ TEST_F(nir_opt_shrink_vectors_test, opt_shrink_phis_loop_phi_out) * } */ - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); - ASSERT_FALSE(nir_opt_shrink_vectors(bld.shader)); + ASSERT_FALSE(nir_opt_shrink_vectors(b->shader)); ASSERT_TRUE(phi_def->num_components == 4); } diff --git a/src/compiler/nir/tests/range_analysis_tests.cpp b/src/compiler/nir/tests/range_analysis_tests.cpp index 5b8d94c4a76..3ab0f0b9a86 100644 --- a/src/compiler/nir/tests/range_analysis_tests.cpp +++ b/src/compiler/nir/tests/range_analysis_tests.cpp @@ -20,51 +20,25 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ -#include -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" #include "nir_range_analysis.h" -class ssa_def_bits_used_test : public ::testing::Test { +class ssa_def_bits_used_test : public nir_test { protected: ssa_def_bits_used_test() + : nir_test::nir_test("nir_ssa_def_bits_used_test") { - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, - "ssa_def_bits_used test"); - } - - ~ssa_def_bits_used_test() - { - ralloc_free(bld.shader); - glsl_type_singleton_decref(); } nir_alu_instr *build_alu_instr(nir_op op, nir_ssa_def *, nir_ssa_def *); - - struct nir_builder bld; }; -class unsigned_upper_bound_test : public ::testing::Test { +class unsigned_upper_bound_test : public nir_test { protected: unsigned_upper_bound_test() + : nir_test::nir_test("nir_unsigned_upper_bound_test") { - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, - "unsigned_upper_bound test"); } - - ~unsigned_upper_bound_test() - { - ralloc_free(bld.shader); - glsl_type_singleton_decref(); - } - - struct nir_builder bld; }; static bool @@ -77,7 +51,7 @@ nir_alu_instr * ssa_def_bits_used_test::build_alu_instr(nir_op op, nir_ssa_def *src0, nir_ssa_def *src1) { - nir_ssa_def *def = nir_build_alu(&bld, op, src0, src1, NULL, NULL); + nir_ssa_def *def = nir_build_alu(b, op, src0, src1, NULL, NULL); if (def == NULL) return NULL; @@ -97,10 +71,10 @@ TEST_F(ssa_def_bits_used_test, iand_with_const_vector) { static const unsigned src0_imm[4] = { 255u << 24, 255u << 16, 255u << 8, 255u }; - nir_ssa_def *src0 = nir_imm_ivec4(&bld, + nir_ssa_def *src0 = nir_imm_ivec4(b, src0_imm[0], src0_imm[1], src0_imm[2], src0_imm[3]); - nir_ssa_def *src1 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src1 = nir_imm_int(b, 0xffffffff); nir_alu_instr *alu = build_alu_instr(nir_op_iand, src0, src1); @@ -126,10 +100,10 @@ TEST_F(ssa_def_bits_used_test, ior_with_const_vector) { static const unsigned src0_imm[4] = { 255u << 24, 255u << 16, 255u << 8, 255u }; - nir_ssa_def *src0 = nir_imm_ivec4(&bld, + nir_ssa_def *src0 = nir_imm_ivec4(b, src0_imm[0], src0_imm[1], src0_imm[2], src0_imm[3]); - nir_ssa_def *src1 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src1 = nir_imm_int(b, 0xffffffff); nir_alu_instr *alu = build_alu_instr(nir_op_ior, src0, src1); @@ -153,11 +127,11 @@ TEST_F(ssa_def_bits_used_test, ior_with_const_vector) TEST_F(ssa_def_bits_used_test, extract_i16_with_const_index) { - nir_ssa_def *src0 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src0 = nir_imm_int(b, 0xffffffff); static const unsigned src1_imm[4] = { 9, 1, 0, 9 }; - nir_ssa_def *src1 = nir_imm_ivec4(&bld, + nir_ssa_def *src1 = nir_imm_ivec4(b, src1_imm[0], src1_imm[1], src1_imm[2], @@ -184,11 +158,11 @@ TEST_F(ssa_def_bits_used_test, extract_i16_with_const_index) TEST_F(ssa_def_bits_used_test, extract_u16_with_const_index) { - nir_ssa_def *src0 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src0 = nir_imm_int(b, 0xffffffff); static const unsigned src1_imm[4] = { 9, 1, 0, 9 }; - nir_ssa_def *src1 = nir_imm_ivec4(&bld, + nir_ssa_def *src1 = nir_imm_ivec4(b, src1_imm[0], src1_imm[1], src1_imm[2], @@ -215,11 +189,11 @@ TEST_F(ssa_def_bits_used_test, extract_u16_with_const_index) TEST_F(ssa_def_bits_used_test, extract_i8_with_const_index) { - nir_ssa_def *src0 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src0 = nir_imm_int(b, 0xffffffff); static const unsigned src1_imm[4] = { 3, 2, 1, 0 }; - nir_ssa_def *src1 = nir_imm_ivec4(&bld, + nir_ssa_def *src1 = nir_imm_ivec4(b, src1_imm[0], src1_imm[1], src1_imm[2], @@ -246,11 +220,11 @@ TEST_F(ssa_def_bits_used_test, extract_i8_with_const_index) TEST_F(ssa_def_bits_used_test, extract_u8_with_const_index) { - nir_ssa_def *src0 = nir_imm_int(&bld, 0xffffffff); + nir_ssa_def *src0 = nir_imm_int(b, 0xffffffff); static const unsigned src1_imm[4] = { 3, 2, 1, 0 }; - nir_ssa_def *src1 = nir_imm_ivec4(&bld, + nir_ssa_def *src1 = nir_imm_ivec4(b, src1_imm[0], src1_imm[1], src1_imm[2], @@ -295,28 +269,28 @@ TEST_F(unsigned_upper_bound_test, loop_phi_bcsel) * block b3: * } */ - nir_ssa_def *zero = nir_imm_int(&bld, 0); - nir_ssa_def *two = nir_imm_int(&bld, 2); - nir_ssa_def *cond = nir_imm_false(&bld); + nir_ssa_def *zero = nir_imm_int(b, 0); + nir_ssa_def *two = nir_imm_int(b, 2); + nir_ssa_def *cond = nir_imm_false(b); - nir_phi_instr *const phi = nir_phi_instr_create(bld.shader); + nir_phi_instr *const phi = nir_phi_instr_create(b->shader); nir_ssa_dest_init(&phi->instr, &phi->dest, 1, 32); - nir_push_loop(&bld); - nir_ssa_def *sel = nir_bcsel(&bld, cond, &phi->dest.ssa, two); - nir_pop_loop(&bld, NULL); + nir_push_loop(b); + nir_ssa_def *sel = nir_bcsel(b, cond, &phi->dest.ssa, two); + nir_pop_loop(b, NULL); nir_phi_instr_add_src(phi, zero->parent_instr->block, nir_src_for_ssa(zero)); nir_phi_instr_add_src(phi, sel->parent_instr->block, nir_src_for_ssa(sel)); - bld.cursor = nir_before_instr(sel->parent_instr); - nir_builder_instr_insert(&bld, &phi->instr); + b->cursor = nir_before_instr(sel->parent_instr); + nir_builder_instr_insert(b, &phi->instr); - nir_validate_shader(bld.shader, NULL); + nir_validate_shader(b->shader, NULL); struct hash_table *range_ht = _mesa_pointer_hash_table_create(NULL); nir_ssa_scalar scalar = nir_get_ssa_scalar(&phi->dest.ssa, 0); - EXPECT_EQ(nir_unsigned_upper_bound(bld.shader, range_ht, scalar, NULL), 2); + EXPECT_EQ(nir_unsigned_upper_bound(b->shader, range_ht, scalar, NULL), 2); _mesa_hash_table_destroy(range_ht, NULL); } diff --git a/src/compiler/nir/tests/vars_tests.cpp b/src/compiler/nir/tests/vars_tests.cpp index 6a80c33f9a3..c60dd4b98bc 100644 --- a/src/compiler/nir/tests/vars_tests.cpp +++ b/src/compiler/nir/tests/vars_tests.cpp @@ -21,15 +21,12 @@ * DEALINGS IN THE SOFTWARE. */ -#include - -#include "nir.h" -#include "nir_builder.h" +#include "nir_test.h" #include "nir_deref.h" namespace { -class nir_vars_test : public ::testing::Test { +class nir_vars_test : public nir_test { protected: nir_vars_test(); ~nir_vars_test(); @@ -94,18 +91,11 @@ protected: nir_deref_instr *get_deref(nir_deref_type deref_type, unsigned index); void *lin_ctx; - - nir_builder *b, _b; }; nir_vars_test::nir_vars_test() + : nir_test::nir_test("nir_vars_test") { - glsl_type_singleton_init_or_ref(); - - static const nir_shader_compiler_options options = { }; - _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, - "vars test"); - b = &_b; lin_ctx = linear_alloc_parent(b->shader, 0); } @@ -115,10 +105,6 @@ nir_vars_test::~nir_vars_test() printf("\nShader from the failed test:\n\n"); nir_print_shader(b->shader, stdout); } - - ralloc_free(b->shader); - - glsl_type_singleton_decref(); } unsigned