1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Imagination Technologies Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 5bf215546Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal 6bf215546Sopenharmony_ci * in the Software without restriction, including without limitation the rights 7bf215546Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8bf215546Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is 9bf215546Sopenharmony_ci * 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 THE 18bf215546Sopenharmony_ci * 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 FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "compiler/spirv/nir_spirv.h" 25bf215546Sopenharmony_ci#include "nir/nir.h" 26bf215546Sopenharmony_ci#include "nir/nir_schedule.h" 27bf215546Sopenharmony_ci#include "rogue_nir.h" 28bf215546Sopenharmony_ci#include "rogue_operand.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci/** 31bf215546Sopenharmony_ci * \file rogue_nir.c 32bf215546Sopenharmony_ci * 33bf215546Sopenharmony_ci * \brief Contains NIR-specific functions. 34bf215546Sopenharmony_ci */ 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci/** 37bf215546Sopenharmony_ci * \brief SPIR-V to NIR compilation options. 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_cistatic const struct spirv_to_nir_options spirv_options = { 40bf215546Sopenharmony_ci .environment = NIR_SPIRV_VULKAN, 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci /* Buffer address: (descriptor_set, binding), offset. */ 43bf215546Sopenharmony_ci .ubo_addr_format = nir_address_format_vec2_index_32bit_offset, 44bf215546Sopenharmony_ci}; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_cistatic const nir_shader_compiler_options nir_options = { 47bf215546Sopenharmony_ci .lower_fsat = true, 48bf215546Sopenharmony_ci .fuse_ffma32 = true, 49bf215546Sopenharmony_ci}; 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ciconst struct spirv_to_nir_options * 52bf215546Sopenharmony_cirogue_get_spirv_options(const struct rogue_compiler *compiler) 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci return &spirv_options; 55bf215546Sopenharmony_ci} 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ciconst nir_shader_compiler_options * 58bf215546Sopenharmony_cirogue_get_compiler_options(const struct rogue_compiler *compiler) 59bf215546Sopenharmony_ci{ 60bf215546Sopenharmony_ci return &nir_options; 61bf215546Sopenharmony_ci} 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_cistatic int rogue_glsl_type_size(const struct glsl_type *type, bool bindless) 64bf215546Sopenharmony_ci{ 65bf215546Sopenharmony_ci return glsl_count_attribute_slots(type, false); 66bf215546Sopenharmony_ci} 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci/** 69bf215546Sopenharmony_ci * \brief Applies optimizations and passes required to lower the NIR shader into 70bf215546Sopenharmony_ci * a form suitable for lowering to Rogue IR. 71bf215546Sopenharmony_ci * 72bf215546Sopenharmony_ci * \param[in] ctx Shared multi-stage build context. 73bf215546Sopenharmony_ci * \param[in] shader Rogue shader. 74bf215546Sopenharmony_ci * \param[in] stage Shader stage. 75bf215546Sopenharmony_ci * \return true if successful, otherwise false. 76bf215546Sopenharmony_ci */ 77bf215546Sopenharmony_cibool rogue_nir_passes(struct rogue_build_ctx *ctx, 78bf215546Sopenharmony_ci nir_shader *nir, 79bf215546Sopenharmony_ci gl_shader_stage stage) 80bf215546Sopenharmony_ci{ 81bf215546Sopenharmony_ci bool progress; 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_ci nir_validate_shader(nir, "after spirv_to_nir"); 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci /* Splitting. */ 86bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_split_var_copies); 87bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_split_per_member_structs); 88bf215546Sopenharmony_ci 89bf215546Sopenharmony_ci /* Ensure fs outputs are in the [0.0f...1.0f] range. */ 90bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_clamp_color_outputs); 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci /* Replace references to I/O variables with intrinsics. */ 93bf215546Sopenharmony_ci NIR_PASS_V(nir, 94bf215546Sopenharmony_ci nir_lower_io, 95bf215546Sopenharmony_ci nir_var_shader_in | nir_var_shader_out, 96bf215546Sopenharmony_ci rogue_glsl_type_size, 97bf215546Sopenharmony_ci (nir_lower_io_options)0); 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci /* Load inputs to scalars (single registers later). */ 100bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_io_to_scalar, nir_var_shader_in); 101bf215546Sopenharmony_ci 102bf215546Sopenharmony_ci /* Optimize GL access qualifiers. */ 103bf215546Sopenharmony_ci const nir_opt_access_options opt_access_options = { 104bf215546Sopenharmony_ci .is_vulkan = true, 105bf215546Sopenharmony_ci .infer_non_readable = true, 106bf215546Sopenharmony_ci }; 107bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_access, &opt_access_options); 108bf215546Sopenharmony_ci 109bf215546Sopenharmony_ci /* Apply PFO code to the fragment shader output. */ 110bf215546Sopenharmony_ci if (nir->info.stage == MESA_SHADER_FRAGMENT) 111bf215546Sopenharmony_ci NIR_PASS_V(nir, rogue_nir_pfo); 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci /* Load outputs to scalars (single registers later). */ 114bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_io_to_scalar, nir_var_shader_out); 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ci /* Lower ALU operations to scalars. */ 117bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL, NULL); 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci /* Algebraic opts. */ 120bf215546Sopenharmony_ci do { 121bf215546Sopenharmony_ci progress = false; 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_copy_prop); 124bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_opt_cse); 125bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_opt_algebraic); 126bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_opt_constant_folding); 127bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_opt_dce); 128bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_gcm, false); 129bf215546Sopenharmony_ci } while (progress); 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci /* Additional I/O lowering. */ 132bf215546Sopenharmony_ci NIR_PASS_V(nir, 133bf215546Sopenharmony_ci nir_lower_explicit_io, 134bf215546Sopenharmony_ci nir_var_mem_ubo, 135bf215546Sopenharmony_ci spirv_options.ubo_addr_format); 136bf215546Sopenharmony_ci NIR_PASS_V(nir, rogue_nir_lower_io, NULL); 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci /* Late algebraic opts. */ 139bf215546Sopenharmony_ci do { 140bf215546Sopenharmony_ci progress = false; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci NIR_PASS(progress, nir, nir_opt_algebraic_late); 143bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_constant_folding); 144bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_copy_prop); 145bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_dce); 146bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_cse); 147bf215546Sopenharmony_ci } while (progress); 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci /* Replace SSA constant references with a register that loads the value. */ 150bf215546Sopenharmony_ci NIR_PASS_V(nir, rogue_nir_constreg); 151bf215546Sopenharmony_ci /* Remove unused constant registers. */ 152bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_dce); 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci /* Move loads to just before they're needed. */ 155bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_opt_move, nir_move_load_ubo | nir_move_load_input); 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci /* Convert vecNs to movs so we can sequentially allocate them later. */ 158bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_lower_vec_to_movs, NULL, NULL); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci /* Out of SSA pass. */ 161bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_convert_from_ssa, false); 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci /* TODO: Re-enable scheduling after register pressure tweaks. */ 164bf215546Sopenharmony_ci#if 0 165bf215546Sopenharmony_ci /* Instruction scheduling. */ 166bf215546Sopenharmony_ci struct nir_schedule_options schedule_options = { 167bf215546Sopenharmony_ci .threshold = ROGUE_MAX_REG_TEMP / 2, 168bf215546Sopenharmony_ci }; 169bf215546Sopenharmony_ci NIR_PASS_V(nir, nir_schedule, &schedule_options); 170bf215546Sopenharmony_ci#endif 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_ci /* Assign I/O locations. */ 173bf215546Sopenharmony_ci nir_assign_io_var_locations(nir, 174bf215546Sopenharmony_ci nir_var_shader_in, 175bf215546Sopenharmony_ci &nir->num_inputs, 176bf215546Sopenharmony_ci nir->info.stage); 177bf215546Sopenharmony_ci nir_assign_io_var_locations(nir, 178bf215546Sopenharmony_ci nir_var_shader_out, 179bf215546Sopenharmony_ci &nir->num_outputs, 180bf215546Sopenharmony_ci nir->info.stage); 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_ci /* Gather info into nir shader struct. */ 183bf215546Sopenharmony_ci nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir)); 184bf215546Sopenharmony_ci 185bf215546Sopenharmony_ci /* Clean-up after passes. */ 186bf215546Sopenharmony_ci nir_sweep(nir); 187bf215546Sopenharmony_ci 188bf215546Sopenharmony_ci nir_validate_shader(nir, "after passes"); 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci return true; 191bf215546Sopenharmony_ci} 192