1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <gtest/gtest.h> 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include "nir.h" 27bf215546Sopenharmony_ci#include "nir_builder.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_cinamespace { 30bf215546Sopenharmony_ci 31bf215546Sopenharmony_ciclass nir_builder_test : public ::testing::Test { 32bf215546Sopenharmony_ciprivate: 33bf215546Sopenharmony_ci const glsl_type *type_for_def(nir_ssa_def *def) 34bf215546Sopenharmony_ci { 35bf215546Sopenharmony_ci switch (def->bit_size) { 36bf215546Sopenharmony_ci case 8: return glsl_type::u8vec(def->num_components); 37bf215546Sopenharmony_ci case 16: return glsl_type::u16vec(def->num_components); 38bf215546Sopenharmony_ci case 32: return glsl_type::uvec(def->num_components); 39bf215546Sopenharmony_ci case 64: return glsl_type::u64vec(def->num_components); 40bf215546Sopenharmony_ci default: unreachable("Invalid bit size"); 41bf215546Sopenharmony_ci } 42bf215546Sopenharmony_ci } 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ciprotected: 45bf215546Sopenharmony_ci nir_builder_test(); 46bf215546Sopenharmony_ci ~nir_builder_test(); 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci void store_test_val(nir_ssa_def *val) 49bf215546Sopenharmony_ci { 50bf215546Sopenharmony_ci nir_variable *var = nir_variable_create(b->shader, nir_var_mem_ssbo, 51bf215546Sopenharmony_ci type_for_def(val), NULL); 52bf215546Sopenharmony_ci nir_intrinsic_instr *store = 53bf215546Sopenharmony_ci nir_intrinsic_instr_create(b->shader, nir_intrinsic_store_deref); 54bf215546Sopenharmony_ci store->num_components = val->num_components; 55bf215546Sopenharmony_ci store->src[0] = nir_src_for_ssa(&nir_build_deref_var(b, var)->dest.ssa); 56bf215546Sopenharmony_ci store->src[1] = nir_src_for_ssa(val); 57bf215546Sopenharmony_ci nir_intrinsic_set_write_mask(store, ((1 << val->num_components) - 1)); 58bf215546Sopenharmony_ci nir_builder_instr_insert(b, &store->instr); 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci stores.push_back(store); 61bf215546Sopenharmony_ci } 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci nir_ssa_def *test_val(unsigned idx) 64bf215546Sopenharmony_ci { 65bf215546Sopenharmony_ci return stores[idx]->src[1].ssa; 66bf215546Sopenharmony_ci } 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci std::vector<nir_intrinsic_instr *> stores; 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci nir_builder *b, _b; 71bf215546Sopenharmony_ci}; 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_cinir_builder_test::nir_builder_test() 74bf215546Sopenharmony_ci{ 75bf215546Sopenharmony_ci glsl_type_singleton_init_or_ref(); 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci static const nir_shader_compiler_options options = { }; 78bf215546Sopenharmony_ci _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "builder test"); 79bf215546Sopenharmony_ci b = &_b; 80bf215546Sopenharmony_ci} 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_cinir_builder_test::~nir_builder_test() 83bf215546Sopenharmony_ci{ 84bf215546Sopenharmony_ci if (HasFailure()) { 85bf215546Sopenharmony_ci printf("\nShader from the failed test:\n\n"); 86bf215546Sopenharmony_ci nir_print_shader(b->shader, stdout); 87bf215546Sopenharmony_ci } 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci ralloc_free(b->shader); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci glsl_type_singleton_decref(); 92bf215546Sopenharmony_ci} 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci/* Allow grouping the tests while still sharing the helpers. */ 95bf215546Sopenharmony_ciclass nir_extract_bits_test : public nir_builder_test {}; 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci} // namespace 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci// TODO: Re-enable this once we get vec8 support in NIR 100bf215546Sopenharmony_ciTEST_F(nir_extract_bits_test, DISABLED_unaligned8) 101bf215546Sopenharmony_ci{ 102bf215546Sopenharmony_ci nir_ssa_def *srcs[] = { 103bf215546Sopenharmony_ci nir_imm_int(b, 0x03020100), 104bf215546Sopenharmony_ci nir_imm_ivec2(b, 0x07060504, 0x0b0a0908), 105bf215546Sopenharmony_ci }; 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci store_test_val(nir_extract_bits(b, srcs, 2, 24, 1, 64)); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci NIR_PASS_V(b->shader, nir_opt_constant_folding); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci nir_src val = nir_src_for_ssa(test_val(0)); 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci ASSERT_EQ(nir_src_as_uint(val), 0x0a09080706050403); 114bf215546Sopenharmony_ci} 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ciTEST_F(nir_extract_bits_test, unaligned16_disabled) 117bf215546Sopenharmony_ci{ 118bf215546Sopenharmony_ci nir_ssa_def *srcs[] = { 119bf215546Sopenharmony_ci nir_imm_int(b, 0x03020100), 120bf215546Sopenharmony_ci nir_imm_ivec2(b, 0x07060504, 0x0b0a0908), 121bf215546Sopenharmony_ci }; 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci store_test_val(nir_extract_bits(b, srcs, 2, 16, 1, 64)); 124bf215546Sopenharmony_ci 125bf215546Sopenharmony_ci NIR_PASS_V(b->shader, nir_opt_constant_folding); 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci nir_src val = nir_src_for_ssa(test_val(0)); 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci ASSERT_EQ(nir_src_as_uint(val), 0x0908070605040302); 130bf215546Sopenharmony_ci} 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ciTEST_F(nir_extract_bits_test, mixed_bit_sizes) 133bf215546Sopenharmony_ci{ 134bf215546Sopenharmony_ci nir_ssa_def *srcs[] = { 135bf215546Sopenharmony_ci nir_imm_int(b, 0x03020100), 136bf215546Sopenharmony_ci nir_imm_intN_t(b, 0x04, 8), 137bf215546Sopenharmony_ci nir_imm_intN_t(b, 0x08070605, 32), 138bf215546Sopenharmony_ci nir_vec2(b, nir_imm_intN_t(b, 0x0a09, 16), 139bf215546Sopenharmony_ci nir_imm_intN_t(b, 0x0c0b, 16)), 140bf215546Sopenharmony_ci }; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci store_test_val(nir_extract_bits(b, srcs, 4, 24, 2, 32)); 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci NIR_PASS_V(b->shader, nir_opt_constant_folding); 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci nir_src val = nir_src_for_ssa(test_val(0)); 147bf215546Sopenharmony_ci 148bf215546Sopenharmony_ci ASSERT_EQ(nir_src_comp_as_uint(val, 0), 0x06050403); 149bf215546Sopenharmony_ci ASSERT_EQ(nir_src_comp_as_uint(val, 1), 0x0a090807); 150bf215546Sopenharmony_ci} 151