17ec681f3Smrg/* 27ec681f3Smrg * Copyright © 2018 Intel Corporation 37ec681f3Smrg * Copyright © 2021 Valve Corporation 47ec681f3Smrg * 57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 67ec681f3Smrg * copy of this software and associated documentation files (the "Software"), 77ec681f3Smrg * to deal in the Software without restriction, including without limitation 87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the 107ec681f3Smrg * Software is furnished to do so, subject to the following conditions: 117ec681f3Smrg * 127ec681f3Smrg * The above copyright notice and this permission notice (including the next 137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the 147ec681f3Smrg * Software. 157ec681f3Smrg * 167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 217ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 227ec681f3Smrg * DEALINGS IN THE SOFTWARE. 237ec681f3Smrg */ 247ec681f3Smrg 257ec681f3Smrg#include <gtest/gtest.h> 267ec681f3Smrg 277ec681f3Smrg#include "nir.h" 287ec681f3Smrg#include "nir_builder.h" 297ec681f3Smrg 307ec681f3Smrgnamespace { 317ec681f3Smrg 327ec681f3Smrgclass algebraic_test_base : public ::testing::Test { 337ec681f3Smrgprotected: 347ec681f3Smrg algebraic_test_base(); 357ec681f3Smrg ~algebraic_test_base(); 367ec681f3Smrg 377ec681f3Smrg virtual void run_pass()=0; 387ec681f3Smrg 397ec681f3Smrg void test_op(nir_op op, nir_ssa_def *src0, nir_ssa_def *src1, nir_ssa_def *src2, 407ec681f3Smrg nir_ssa_def *src3, const char *desc); 417ec681f3Smrg 427ec681f3Smrg void test_2src_op(nir_op op, int64_t src0, int64_t src1); 437ec681f3Smrg 447ec681f3Smrg nir_variable *res_var; 457ec681f3Smrg nir_builder *b, _b; 467ec681f3Smrg}; 477ec681f3Smrg 487ec681f3Smrgalgebraic_test_base::algebraic_test_base() 497ec681f3Smrg{ 507ec681f3Smrg glsl_type_singleton_init_or_ref(); 517ec681f3Smrg 527ec681f3Smrg static const nir_shader_compiler_options options = { }; 537ec681f3Smrg _b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, &options, "opt_algebraic test"); 547ec681f3Smrg b = &_b; 557ec681f3Smrg 567ec681f3Smrg res_var = nir_local_variable_create(b->impl, glsl_int_type(), "res"); 577ec681f3Smrg} 587ec681f3Smrg 597ec681f3Smrgalgebraic_test_base::~algebraic_test_base() 607ec681f3Smrg{ 617ec681f3Smrg ralloc_free(b->shader); 627ec681f3Smrg 637ec681f3Smrg glsl_type_singleton_decref(); 647ec681f3Smrg} 657ec681f3Smrg 667ec681f3Smrgvoid algebraic_test_base::test_op(nir_op op, nir_ssa_def *src0, nir_ssa_def *src1, 677ec681f3Smrg nir_ssa_def *src2, nir_ssa_def *src3, const char *desc) 687ec681f3Smrg{ 697ec681f3Smrg nir_ssa_def *res_deref = &nir_build_deref_var(b, res_var)->dest.ssa; 707ec681f3Smrg 717ec681f3Smrg /* create optimized expression */ 727ec681f3Smrg nir_intrinsic_instr *optimized_instr = nir_build_store_deref( 737ec681f3Smrg b, res_deref, nir_build_alu(b, op, src0, src1, src2, src3), 0x1); 747ec681f3Smrg 757ec681f3Smrg run_pass(); 767ec681f3Smrg b->cursor = nir_after_cf_list(&b->impl->body); 777ec681f3Smrg 787ec681f3Smrg /* create reference expression */ 797ec681f3Smrg nir_intrinsic_instr *ref_instr = nir_build_store_deref( 807ec681f3Smrg b, res_deref, nir_build_alu(b, op, src0, src1, src2, src3), 0x1); 817ec681f3Smrg 827ec681f3Smrg /* test equality */ 837ec681f3Smrg nir_opt_constant_folding(b->shader); 847ec681f3Smrg 857ec681f3Smrg ASSERT_TRUE(nir_src_is_const(ref_instr->src[1])); 867ec681f3Smrg ASSERT_TRUE(nir_src_is_const(optimized_instr->src[1])); 877ec681f3Smrg 887ec681f3Smrg int32_t ref = nir_src_as_int(ref_instr->src[1]); 897ec681f3Smrg int32_t optimized = nir_src_as_int(optimized_instr->src[1]); 907ec681f3Smrg 917ec681f3Smrg EXPECT_EQ(ref, optimized) << "Test input: " << desc; 927ec681f3Smrg 937ec681f3Smrg /* reset shader */ 947ec681f3Smrg exec_list_make_empty(&nir_start_block(b->impl)->instr_list); 957ec681f3Smrg b->cursor = nir_after_cf_list(&b->impl->body); 967ec681f3Smrg} 977ec681f3Smrg 987ec681f3Smrgvoid algebraic_test_base::test_2src_op(nir_op op, int64_t src0, int64_t src1) 997ec681f3Smrg{ 1007ec681f3Smrg char desc[128]; 1017ec681f3Smrg snprintf(desc, sizeof(desc), "%s(%" PRId64 ", %" PRId64 ")", nir_op_infos[op].name, src0, src1); 1027ec681f3Smrg test_op(op, nir_imm_int(b, src0), nir_imm_int(b, src1), NULL, NULL, desc); 1037ec681f3Smrg} 1047ec681f3Smrg 1057ec681f3Smrgclass nir_opt_algebraic_test : public algebraic_test_base { 1067ec681f3Smrgprotected: 1077ec681f3Smrg virtual void run_pass() { 1087ec681f3Smrg nir_opt_algebraic(b->shader); 1097ec681f3Smrg } 1107ec681f3Smrg}; 1117ec681f3Smrg 1127ec681f3Smrgclass nir_opt_idiv_const_test : public algebraic_test_base { 1137ec681f3Smrgprotected: 1147ec681f3Smrg virtual void run_pass() { 1157ec681f3Smrg nir_opt_idiv_const(b->shader, 8); 1167ec681f3Smrg } 1177ec681f3Smrg}; 1187ec681f3Smrg 1197ec681f3SmrgTEST_F(nir_opt_algebraic_test, umod_pow2_src2) 1207ec681f3Smrg{ 1217ec681f3Smrg for (int i = 0; i <= 9; i++) 1227ec681f3Smrg test_2src_op(nir_op_umod, i, 4); 1237ec681f3Smrg test_2src_op(nir_op_umod, UINT32_MAX, 4); 1247ec681f3Smrg} 1257ec681f3Smrg 1267ec681f3SmrgTEST_F(nir_opt_algebraic_test, imod_pow2_src2) 1277ec681f3Smrg{ 1287ec681f3Smrg for (int i = -9; i <= 9; i++) { 1297ec681f3Smrg test_2src_op(nir_op_imod, i, 4); 1307ec681f3Smrg test_2src_op(nir_op_imod, i, -4); 1317ec681f3Smrg test_2src_op(nir_op_imod, i, INT32_MIN); 1327ec681f3Smrg } 1337ec681f3Smrg test_2src_op(nir_op_imod, INT32_MAX, 4); 1347ec681f3Smrg test_2src_op(nir_op_imod, INT32_MAX, -4); 1357ec681f3Smrg test_2src_op(nir_op_imod, INT32_MIN, 4); 1367ec681f3Smrg test_2src_op(nir_op_imod, INT32_MIN, -4); 1377ec681f3Smrg test_2src_op(nir_op_imod, INT32_MIN, INT32_MIN); 1387ec681f3Smrg} 1397ec681f3Smrg 1407ec681f3SmrgTEST_F(nir_opt_algebraic_test, irem_pow2_src2) 1417ec681f3Smrg{ 1427ec681f3Smrg for (int i = -9; i <= 9; i++) { 1437ec681f3Smrg test_2src_op(nir_op_irem, i, 4); 1447ec681f3Smrg test_2src_op(nir_op_irem, i, -4); 1457ec681f3Smrg } 1467ec681f3Smrg test_2src_op(nir_op_irem, INT32_MAX, 4); 1477ec681f3Smrg test_2src_op(nir_op_irem, INT32_MAX, -4); 1487ec681f3Smrg test_2src_op(nir_op_irem, INT32_MIN, 4); 1497ec681f3Smrg test_2src_op(nir_op_irem, INT32_MIN, -4); 1507ec681f3Smrg} 1517ec681f3Smrg 1527ec681f3SmrgTEST_F(nir_opt_idiv_const_test, umod) 1537ec681f3Smrg{ 1547ec681f3Smrg for (uint32_t d : {16u, 17u, 0u, UINT32_MAX}) { 1557ec681f3Smrg for (int i = 0; i <= 40; i++) 1567ec681f3Smrg test_2src_op(nir_op_umod, i, d); 1577ec681f3Smrg for (int i = 0; i < 20; i++) 1587ec681f3Smrg test_2src_op(nir_op_umod, UINT32_MAX - i, d); 1597ec681f3Smrg } 1607ec681f3Smrg} 1617ec681f3Smrg 1627ec681f3SmrgTEST_F(nir_opt_idiv_const_test, imod) 1637ec681f3Smrg{ 1647ec681f3Smrg for (int32_t d : {16, -16, 17, -17, 0, INT32_MIN, INT32_MAX}) { 1657ec681f3Smrg for (int i = -40; i <= 40; i++) 1667ec681f3Smrg test_2src_op(nir_op_imod, i, d); 1677ec681f3Smrg for (int i = 0; i < 20; i++) 1687ec681f3Smrg test_2src_op(nir_op_imod, INT32_MIN + i, d); 1697ec681f3Smrg for (int i = 0; i < 20; i++) 1707ec681f3Smrg test_2src_op(nir_op_imod, INT32_MAX - i, d); 1717ec681f3Smrg } 1727ec681f3Smrg} 1737ec681f3Smrg 1747ec681f3SmrgTEST_F(nir_opt_idiv_const_test, irem) 1757ec681f3Smrg{ 1767ec681f3Smrg for (int32_t d : {16, -16, 17, -17, 0, INT32_MIN, INT32_MAX}) { 1777ec681f3Smrg for (int i = -40; i <= 40; i++) 1787ec681f3Smrg test_2src_op(nir_op_irem, i, d); 1797ec681f3Smrg for (int i = 0; i < 20; i++) 1807ec681f3Smrg test_2src_op(nir_op_irem, INT32_MIN + i, d); 1817ec681f3Smrg for (int i = 0; i < 20; i++) 1827ec681f3Smrg test_2src_op(nir_op_irem, INT32_MAX - i, d); 1837ec681f3Smrg } 1847ec681f3Smrg} 1857ec681f3Smrg 1867ec681f3Smrg} 187