1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2016 Bas Nieuwenhuizen 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 DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "ac_nir.h" 25bf215546Sopenharmony_ci#include "nir_builder.h" 26bf215546Sopenharmony_ci 27bf215546Sopenharmony_cinir_ssa_def * 28bf215546Sopenharmony_ciac_nir_load_arg(nir_builder *b, const struct ac_shader_args *ac_args, struct ac_arg arg) 29bf215546Sopenharmony_ci{ 30bf215546Sopenharmony_ci unsigned num_components = ac_args->args[arg.arg_index].size; 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci if (ac_args->args[arg.arg_index].file == AC_ARG_SGPR) 33bf215546Sopenharmony_ci return nir_load_scalar_arg_amd(b, num_components, .base = arg.arg_index); 34bf215546Sopenharmony_ci else 35bf215546Sopenharmony_ci return nir_load_vector_arg_amd(b, num_components, .base = arg.arg_index); 36bf215546Sopenharmony_ci} 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_ci/** 39bf215546Sopenharmony_ci * This function takes an I/O intrinsic like load/store_input, 40bf215546Sopenharmony_ci * and emits a sequence that calculates the full offset of that instruction, 41bf215546Sopenharmony_ci * including a stride to the base and component offsets. 42bf215546Sopenharmony_ci */ 43bf215546Sopenharmony_cinir_ssa_def * 44bf215546Sopenharmony_ciac_nir_calc_io_offset(nir_builder *b, 45bf215546Sopenharmony_ci nir_intrinsic_instr *intrin, 46bf215546Sopenharmony_ci nir_ssa_def *base_stride, 47bf215546Sopenharmony_ci unsigned component_stride, 48bf215546Sopenharmony_ci ac_nir_map_io_driver_location map_io) 49bf215546Sopenharmony_ci{ 50bf215546Sopenharmony_ci unsigned base = nir_intrinsic_base(intrin); 51bf215546Sopenharmony_ci unsigned semantic = nir_intrinsic_io_semantics(intrin).location; 52bf215546Sopenharmony_ci unsigned mapped_driver_location = map_io ? map_io(semantic) : base; 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci /* base is the driver_location, which is in slots (1 slot = 4x4 bytes) */ 55bf215546Sopenharmony_ci nir_ssa_def *base_op = nir_imul_imm(b, base_stride, mapped_driver_location); 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci /* offset should be interpreted in relation to the base, 58bf215546Sopenharmony_ci * so the instruction effectively reads/writes another input/output 59bf215546Sopenharmony_ci * when it has an offset 60bf215546Sopenharmony_ci */ 61bf215546Sopenharmony_ci nir_ssa_def *offset_op = nir_imul(b, base_stride, nir_ssa_for_src(b, *nir_get_io_offset_src(intrin), 1)); 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci /* component is in bytes */ 64bf215546Sopenharmony_ci unsigned const_op = nir_intrinsic_component(intrin) * component_stride; 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci return nir_iadd_imm_nuw(b, nir_iadd_nuw(b, base_op, offset_op), const_op); 67bf215546Sopenharmony_ci} 68bf215546Sopenharmony_ci 69bf215546Sopenharmony_cibool 70bf215546Sopenharmony_ciac_nir_lower_indirect_derefs(nir_shader *shader, 71bf215546Sopenharmony_ci enum amd_gfx_level gfx_level) 72bf215546Sopenharmony_ci{ 73bf215546Sopenharmony_ci bool progress = false; 74bf215546Sopenharmony_ci 75bf215546Sopenharmony_ci /* Lower large variables to scratch first so that we won't bloat the 76bf215546Sopenharmony_ci * shader by generating large if ladders for them. We later lower 77bf215546Sopenharmony_ci * scratch to alloca's, assuming LLVM won't generate VGPR indexing. 78bf215546Sopenharmony_ci */ 79bf215546Sopenharmony_ci NIR_PASS(progress, shader, nir_lower_vars_to_scratch, nir_var_function_temp, 256, 80bf215546Sopenharmony_ci glsl_get_natural_size_align_bytes); 81bf215546Sopenharmony_ci 82bf215546Sopenharmony_ci /* LLVM doesn't support VGPR indexing on GFX9. */ 83bf215546Sopenharmony_ci bool llvm_has_working_vgpr_indexing = gfx_level != GFX9; 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci /* TODO: Indirect indexing of GS inputs is unimplemented. 86bf215546Sopenharmony_ci * 87bf215546Sopenharmony_ci * TCS and TES load inputs directly from LDS or offchip memory, so 88bf215546Sopenharmony_ci * indirect indexing is trivial. 89bf215546Sopenharmony_ci */ 90bf215546Sopenharmony_ci nir_variable_mode indirect_mask = 0; 91bf215546Sopenharmony_ci if (shader->info.stage == MESA_SHADER_GEOMETRY || 92bf215546Sopenharmony_ci (shader->info.stage != MESA_SHADER_TESS_CTRL && shader->info.stage != MESA_SHADER_TESS_EVAL && 93bf215546Sopenharmony_ci !llvm_has_working_vgpr_indexing)) { 94bf215546Sopenharmony_ci indirect_mask |= nir_var_shader_in; 95bf215546Sopenharmony_ci } 96bf215546Sopenharmony_ci if (!llvm_has_working_vgpr_indexing && shader->info.stage != MESA_SHADER_TESS_CTRL) 97bf215546Sopenharmony_ci indirect_mask |= nir_var_shader_out; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci /* TODO: We shouldn't need to do this, however LLVM isn't currently 100bf215546Sopenharmony_ci * smart enough to handle indirects without causing excess spilling 101bf215546Sopenharmony_ci * causing the gpu to hang. 102bf215546Sopenharmony_ci * 103bf215546Sopenharmony_ci * See the following thread for more details of the problem: 104bf215546Sopenharmony_ci * https://lists.freedesktop.org/archives/mesa-dev/2017-July/162106.html 105bf215546Sopenharmony_ci */ 106bf215546Sopenharmony_ci indirect_mask |= nir_var_function_temp; 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci NIR_PASS(progress, shader, nir_lower_indirect_derefs, indirect_mask, UINT32_MAX); 109bf215546Sopenharmony_ci return progress; 110bf215546Sopenharmony_ci} 111