/* * Copyright © 2024 Valve Corporation * SPDX-License-Identifier: MIT */ #include "nir_builder.h" #include "nir_test.h" class nir_opt_reassociate_test : public nir_test { protected: nir_opt_reassociate_test(); nir_def *x, *y, *z; void run_pass(nir_reference_shader expected); }; nir_opt_reassociate_test::nir_opt_reassociate_test() : nir_test::nir_test("nir_opt_reassociate_test", MESA_SHADER_FRAGMENT) { x = nir_div(b, 1, 32, 0); y = nir_div(b, 1, 32, 1); z = nir_div(b, 1, 32, 2); } void nir_opt_reassociate_test::run_pass(nir_reference_shader expected) { nir_validate_shader(b->shader, "before nir_opt_reassociate"); NIR_PASS(_, b->shader, nir_opt_reassociate); check_nir_string(expected); } TEST_F(nir_opt_reassociate_test, constants) { nir_iadd_imm(b, nir_iadd_imm(b, x, 3), 5); run_pass(NIR_REFERENCE_SHADER(R"( shader: MESA_SHADER_FRAGMENT name: nir_opt_reassociate_test subgroup_size: 0 decl_function main () (entrypoint) impl main { block b0: // preds: 32 %0 = @div (base=0) 32 %1 = @div (base=1) 32 %2 = @div (base=2) 32 %3 = load_const (0x00000003) 32 %4 = iadd %0, %3 (0x3) 32 %5 = load_const (0x00000004) 32 %6 = iadd %5 (0x4), %3 (0x3) 32 %7 = iadd %0, %6 // succs: b1 block b1: } )")); shader: MESA_SHADER_FRAGMENT name: nir_opt_reassociate_test subgroup_size: 0 decl_function main () (entrypoint) impl main { block b0: // preds: 32 %0 = @div (base=0) 32 %1 = @div (base=1) 32 %2 = @div (base=2) 32 %3 = load_const (0x00000003) 32 %4 = iadd %0, %3 (0x3) 32 %5 = load_const (0x00000005) 32 %6 = iadd %5 (0x5), %3 (0x3) 32 %7 = iadd %0, %6 // succs: b1 block b1: }