1/* 2 * Copyright © 2019 Red Hat 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#include "ir3_nir.h" 25#include "compiler/nir/nir_builder.h" 26 27/** 28 * This pass moves varying fetches (and the instructions they depend on 29 * into the start block. 30 * 31 * We need to set the (ei) "end input" flag on the last varying fetch. 32 * And we want to ensure that all threads execute the instruction that 33 * sets (ei). The easiest way to ensure this is to move all varying 34 * fetches into the start block. Which is something we used to get for 35 * free by using lower_all_io_to_temps=true. 36 * 37 * This may come at the cost of additional register usage. OTOH setting 38 * the (ei) flag earlier probably frees up more VS to run. 39 */ 40 41 42typedef struct { 43 nir_shader *shader; 44 nir_block *start_block; 45 struct nir_instr *cursor; 46} state; 47 48static void move_instruction_to_start_block(state *state, nir_instr *instr); 49 50static bool 51move_src(nir_src *src, void *state) 52{ 53 /* At this point we shouldn't have any non-ssa src: */ 54 debug_assert(src->is_ssa); 55 move_instruction_to_start_block(state, src->ssa->parent_instr); 56 return true; 57} 58 59static void 60move_instruction_to_start_block(state *state, nir_instr *instr) 61{ 62 /* first move (recursively) all src's to ensure they appear before 63 * load*_input that we are trying to move: 64 */ 65 nir_foreach_src(instr, move_src, state); 66 67 /* and then move the instruction itself: 68 */ 69 exec_node_remove(&instr->node); 70 71 if (state->cursor) { 72 exec_node_insert_after(&state->cursor->node, &instr->node); 73 } else { 74 exec_list_push_head(&state->start_block->instr_list, &instr->node); 75 } 76 77 state->cursor = instr; 78 instr->block = state->start_block; 79} 80 81static bool 82move_varying_inputs_block(state *state, nir_block *block) 83{ 84 bool progress = false; 85 86 nir_foreach_instr_safe(instr, block) { 87 if (instr->type != nir_instr_type_intrinsic) 88 continue; 89 90 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); 91 92 switch (intr->intrinsic) { 93 case nir_intrinsic_load_interpolated_input: 94 case nir_intrinsic_load_input: 95 /* TODO any others to handle? */ 96 break; 97 default: 98 continue; 99 } 100 101 debug_assert(intr->dest.is_ssa); 102 103 state->cursor = NULL; 104 move_instruction_to_start_block(state, instr); 105 106 progress = true; 107 } 108 109 return progress; 110} 111 112bool 113ir3_nir_move_varying_inputs(nir_shader *shader) 114{ 115 bool progress = false; 116 117 debug_assert(shader->info.stage == MESA_SHADER_FRAGMENT); 118 119 nir_foreach_function (function, shader) { 120 state state; 121 122 if (!function->impl) 123 continue; 124 125 state.shader = shader; 126 state.start_block = nir_start_block(function->impl); 127 128 bool progress = false; 129 nir_foreach_block (block, function->impl) { 130 /* don't need to move anything that is already in the first block */ 131 if (block == state.start_block) 132 continue; 133 progress |= move_varying_inputs_block(&state, block); 134 } 135 136 if (progress) { 137 nir_metadata_preserve(function->impl, 138 nir_metadata_block_index | nir_metadata_dominance); 139 } 140 } 141 142 return progress; 143} 144