1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2015 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#include <gtest/gtest.h> 24bf215546Sopenharmony_ci#include "nir.h" 25bf215546Sopenharmony_ci#include "nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_ciclass nir_cf_test : public ::testing::Test { 28bf215546Sopenharmony_ciprotected: 29bf215546Sopenharmony_ci nir_cf_test(); 30bf215546Sopenharmony_ci ~nir_cf_test(); 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci nir_builder b; 33bf215546Sopenharmony_ci}; 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_cinir_cf_test::nir_cf_test() 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci glsl_type_singleton_init_or_ref(); 38bf215546Sopenharmony_ci 39bf215546Sopenharmony_ci static const nir_shader_compiler_options options = { }; 40bf215546Sopenharmony_ci b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options, "cf test"); 41bf215546Sopenharmony_ci} 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_cinir_cf_test::~nir_cf_test() 44bf215546Sopenharmony_ci{ 45bf215546Sopenharmony_ci ralloc_free(b.shader); 46bf215546Sopenharmony_ci glsl_type_singleton_decref(); 47bf215546Sopenharmony_ci} 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ciTEST_F(nir_cf_test, delete_break_in_loop) 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci /* Create IR: 52bf215546Sopenharmony_ci * 53bf215546Sopenharmony_ci * while (...) { break; } 54bf215546Sopenharmony_ci */ 55bf215546Sopenharmony_ci nir_loop *loop = nir_loop_create(b.shader); 56bf215546Sopenharmony_ci nir_cf_node_insert(nir_after_cf_list(&b.impl->body), &loop->cf_node); 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci b.cursor = nir_after_cf_list(&loop->body); 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci nir_jump_instr *jump = nir_jump_instr_create(b.shader, nir_jump_break); 61bf215546Sopenharmony_ci nir_builder_instr_insert(&b, &jump->instr); 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci /* At this point, we should have: 64bf215546Sopenharmony_ci * 65bf215546Sopenharmony_ci * impl main { 66bf215546Sopenharmony_ci * block block_0: 67bf215546Sopenharmony_ci * // preds: 68bf215546Sopenharmony_ci * // succs: block_1 69bf215546Sopenharmony_ci * loop { 70bf215546Sopenharmony_ci * block block_1: 71bf215546Sopenharmony_ci * // preds: block_0 72bf215546Sopenharmony_ci * break 73bf215546Sopenharmony_ci * // succs: block_2 74bf215546Sopenharmony_ci * } 75bf215546Sopenharmony_ci * block block_2: 76bf215546Sopenharmony_ci * // preds: block_1 77bf215546Sopenharmony_ci * // succs: block_3 78bf215546Sopenharmony_ci * block block_3: 79bf215546Sopenharmony_ci * } 80bf215546Sopenharmony_ci */ 81bf215546Sopenharmony_ci nir_block *block_0 = nir_start_block(b.impl); 82bf215546Sopenharmony_ci nir_block *block_1 = nir_loop_first_block(loop); 83bf215546Sopenharmony_ci nir_block *block_2 = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node)); 84bf215546Sopenharmony_ci nir_block *block_3 = b.impl->end_block; 85bf215546Sopenharmony_ci ASSERT_EQ(nir_cf_node_block, block_0->cf_node.type); 86bf215546Sopenharmony_ci ASSERT_EQ(nir_cf_node_block, block_1->cf_node.type); 87bf215546Sopenharmony_ci ASSERT_EQ(nir_cf_node_block, block_2->cf_node.type); 88bf215546Sopenharmony_ci ASSERT_EQ(nir_cf_node_block, block_3->cf_node.type); 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci /* Verify the successors and predecessors. */ 91bf215546Sopenharmony_ci EXPECT_EQ(block_1, block_0->successors[0]); 92bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_0->successors[1]); 93bf215546Sopenharmony_ci EXPECT_EQ(block_2, block_1->successors[0]); 94bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_1->successors[1]); 95bf215546Sopenharmony_ci EXPECT_EQ(block_3, block_2->successors[0]); 96bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_2->successors[1]); 97bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_3->successors[0]); 98bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_3->successors[1]); 99bf215546Sopenharmony_ci EXPECT_EQ(0, block_0->predecessors->entries); 100bf215546Sopenharmony_ci EXPECT_EQ(1, block_1->predecessors->entries); 101bf215546Sopenharmony_ci EXPECT_EQ(1, block_2->predecessors->entries); 102bf215546Sopenharmony_ci EXPECT_EQ(1, block_3->predecessors->entries); 103bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0)); 104bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_2->predecessors, block_1)); 105bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2)); 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci nir_print_shader(b.shader, stderr); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci /* Now remove the break. */ 110bf215546Sopenharmony_ci nir_instr_remove(&jump->instr); 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci nir_print_shader(b.shader, stderr); 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci /* At this point, we should have: 115bf215546Sopenharmony_ci * 116bf215546Sopenharmony_ci * impl main { 117bf215546Sopenharmony_ci * block block_0: 118bf215546Sopenharmony_ci * // preds: 119bf215546Sopenharmony_ci * // succs: block_1 120bf215546Sopenharmony_ci * loop { 121bf215546Sopenharmony_ci * block block_1: 122bf215546Sopenharmony_ci * // preds: block_0 block_1 123bf215546Sopenharmony_ci * // succs: block_1 124bf215546Sopenharmony_ci * } 125bf215546Sopenharmony_ci * block block_2: 126bf215546Sopenharmony_ci * // preds: 127bf215546Sopenharmony_ci * // succs: block_3 128bf215546Sopenharmony_ci * block block_3: 129bf215546Sopenharmony_ci * } 130bf215546Sopenharmony_ci * 131bf215546Sopenharmony_ci * Re-verify the predecessors and successors. 132bf215546Sopenharmony_ci */ 133bf215546Sopenharmony_ci EXPECT_EQ(block_1, block_0->successors[0]); 134bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_0->successors[1]); 135bf215546Sopenharmony_ci EXPECT_EQ(block_1, block_1->successors[0]); /* back to itself */ 136bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_1->successors[1]); 137bf215546Sopenharmony_ci EXPECT_EQ(block_3, block_2->successors[0]); 138bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_2->successors[1]); 139bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_3->successors[0]); 140bf215546Sopenharmony_ci EXPECT_EQ(NULL, block_3->successors[1]); 141bf215546Sopenharmony_ci EXPECT_EQ(0, block_0->predecessors->entries); 142bf215546Sopenharmony_ci EXPECT_EQ(2, block_1->predecessors->entries); 143bf215546Sopenharmony_ci EXPECT_EQ(0, block_2->predecessors->entries); 144bf215546Sopenharmony_ci EXPECT_EQ(1, block_3->predecessors->entries); 145bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_0)); 146bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_1->predecessors, block_1)); 147bf215546Sopenharmony_ci EXPECT_FALSE(_mesa_set_search(block_2->predecessors, block_1)); 148bf215546Sopenharmony_ci EXPECT_TRUE(_mesa_set_search(block_3->predecessors, block_2)); 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ci nir_metadata_require(b.impl, nir_metadata_dominance); 151bf215546Sopenharmony_ci} 152