brw_nir.c revision faf5f174ddbc7680f6947ceababb94fdb552bcdb
1/* 2 * Copyright © 2014 Intel Corporation 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 "brw_nir.h" 25#include "brw_shader.h" 26#include "glsl/glsl_parser_extras.h" 27#include "glsl/nir/glsl_to_nir.h" 28#include "program/prog_to_nir.h" 29 30static void 31nir_optimize(nir_shader *nir, bool is_scalar) 32{ 33 bool progress; 34 do { 35 progress = false; 36 nir_lower_vars_to_ssa(nir); 37 nir_validate_shader(nir); 38 39 if (is_scalar) { 40 nir_lower_alu_to_scalar(nir); 41 nir_validate_shader(nir); 42 } 43 44 progress |= nir_copy_prop(nir); 45 nir_validate_shader(nir); 46 47 if (is_scalar) { 48 nir_lower_phis_to_scalar(nir); 49 nir_validate_shader(nir); 50 } 51 52 progress |= nir_copy_prop(nir); 53 nir_validate_shader(nir); 54 progress |= nir_opt_dce(nir); 55 nir_validate_shader(nir); 56 progress |= nir_opt_cse(nir); 57 nir_validate_shader(nir); 58 progress |= nir_opt_peephole_select(nir); 59 nir_validate_shader(nir); 60 progress |= nir_opt_algebraic(nir); 61 nir_validate_shader(nir); 62 progress |= nir_opt_constant_folding(nir); 63 nir_validate_shader(nir); 64 progress |= nir_opt_dead_cf(nir); 65 nir_validate_shader(nir); 66 progress |= nir_opt_remove_phis(nir); 67 nir_validate_shader(nir); 68 progress |= nir_opt_undef(nir); 69 nir_validate_shader(nir); 70 } while (progress); 71} 72 73nir_shader * 74brw_create_nir(struct brw_context *brw, 75 const struct gl_shader_program *shader_prog, 76 const struct gl_program *prog, 77 gl_shader_stage stage, 78 bool is_scalar) 79{ 80 struct gl_context *ctx = &brw->ctx; 81 const nir_shader_compiler_options *options = 82 ctx->Const.ShaderCompilerOptions[stage].NirOptions; 83 static const nir_lower_tex_options tex_options = { 84 .lower_txp = ~0, 85 }; 86 struct gl_shader *shader = shader_prog ? shader_prog->_LinkedShaders[stage] : NULL; 87 bool debug_enabled = INTEL_DEBUG & intel_debug_flag_for_shader_stage(stage); 88 nir_shader *nir; 89 90 /* First, lower the GLSL IR or Mesa IR to NIR */ 91 if (shader_prog) { 92 nir = glsl_to_nir(shader, options); 93 } else { 94 nir = prog_to_nir(prog, options); 95 nir_convert_to_ssa(nir); /* turn registers into SSA */ 96 } 97 nir_validate_shader(nir); 98 99 nir_lower_global_vars_to_local(nir); 100 nir_validate_shader(nir); 101 102 nir_lower_tex(nir, &tex_options); 103 nir_validate_shader(nir); 104 105 nir_normalize_cubemap_coords(nir); 106 nir_validate_shader(nir); 107 108 nir_split_var_copies(nir); 109 nir_validate_shader(nir); 110 111 nir_optimize(nir, is_scalar); 112 113 /* Lower a bunch of stuff */ 114 nir_lower_var_copies(nir); 115 nir_validate_shader(nir); 116 117 /* Get rid of split copies */ 118 nir_optimize(nir, is_scalar); 119 120 if (is_scalar) { 121 nir_assign_var_locations(&nir->uniforms, 122 &nir->num_uniforms, 123 type_size_scalar); 124 nir_assign_var_locations(&nir->inputs, &nir->num_inputs, type_size_scalar); 125 nir_assign_var_locations(&nir->outputs, &nir->num_outputs, type_size_scalar); 126 nir_lower_io(nir, type_size_scalar); 127 } else { 128 nir_assign_var_locations(&nir->uniforms, 129 &nir->num_uniforms, 130 type_size_vec4); 131 132 nir_assign_var_locations(&nir->inputs, &nir->num_inputs, type_size_vec4); 133 134 foreach_list_typed(nir_variable, var, node, &nir->outputs) 135 var->data.driver_location = var->data.location; 136 137 nir_lower_io(nir, type_size_vec4); 138 } 139 140 nir_validate_shader(nir); 141 142 nir_remove_dead_variables(nir); 143 nir_validate_shader(nir); 144 145 if (shader_prog) { 146 nir_lower_samplers(nir, shader_prog); 147 nir_validate_shader(nir); 148 } 149 150 nir_lower_system_values(nir); 151 nir_validate_shader(nir); 152 153 nir_lower_atomics(nir); 154 nir_validate_shader(nir); 155 156 nir_optimize(nir, is_scalar); 157 158 if (brw->gen >= 6) { 159 /* Try and fuse multiply-adds */ 160 nir_opt_peephole_ffma(nir); 161 nir_validate_shader(nir); 162 } 163 164 nir_opt_algebraic_late(nir); 165 nir_validate_shader(nir); 166 167 nir_lower_locals_to_regs(nir); 168 nir_validate_shader(nir); 169 170 nir_lower_to_source_mods(nir); 171 nir_validate_shader(nir); 172 nir_copy_prop(nir); 173 nir_validate_shader(nir); 174 nir_opt_dce(nir); 175 nir_validate_shader(nir); 176 177 if (unlikely(debug_enabled)) { 178 /* Re-index SSA defs so we print more sensible numbers. */ 179 nir_foreach_overload(nir, overload) { 180 if (overload->impl) 181 nir_index_ssa_defs(overload->impl); 182 } 183 184 fprintf(stderr, "NIR (SSA form) for %s shader:\n", 185 _mesa_shader_stage_to_string(stage)); 186 nir_print_shader(nir, stderr); 187 } 188 189 nir_convert_from_ssa(nir, true); 190 nir_validate_shader(nir); 191 192 if (!is_scalar) { 193 nir_move_vec_src_uses_to_dest(nir); 194 nir_validate_shader(nir); 195 196 nir_lower_vec_to_movs(nir); 197 nir_validate_shader(nir); 198 } 199 200 /* This is the last pass we run before we start emitting stuff. It 201 * determines when we need to insert boolean resolves on Gen <= 5. We 202 * run it last because it stashes data in instr->pass_flags and we don't 203 * want that to be squashed by other NIR passes. 204 */ 205 if (brw->gen <= 5) 206 brw_nir_analyze_boolean_resolves(nir); 207 208 nir_sweep(nir); 209 210 if (unlikely(debug_enabled)) { 211 fprintf(stderr, "NIR (final form) for %s shader:\n", 212 _mesa_shader_stage_to_string(stage)); 213 nir_print_shader(nir, stderr); 214 } 215 216 return nir; 217} 218 219enum brw_reg_type 220brw_type_for_nir_type(nir_alu_type type) 221{ 222 switch (type) { 223 case nir_type_unsigned: 224 return BRW_REGISTER_TYPE_UD; 225 case nir_type_bool: 226 case nir_type_int: 227 return BRW_REGISTER_TYPE_D; 228 case nir_type_float: 229 return BRW_REGISTER_TYPE_F; 230 default: 231 unreachable("unknown type"); 232 } 233 234 return BRW_REGISTER_TYPE_F; 235} 236 237/* Returns the glsl_base_type corresponding to a nir_alu_type. 238 * This is used by both brw_vec4_nir and brw_fs_nir. 239 */ 240enum glsl_base_type 241brw_glsl_base_type_for_nir_type(nir_alu_type type) 242{ 243 switch (type) { 244 case nir_type_float: 245 return GLSL_TYPE_FLOAT; 246 247 case nir_type_int: 248 return GLSL_TYPE_INT; 249 250 case nir_type_unsigned: 251 return GLSL_TYPE_UINT; 252 253 default: 254 unreachable("bad type"); 255 } 256} 257