1/* 2 * Copyright © 2018 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 "nir.h" 25#include "nir_builder.h" 26 27static bool 28assert_ssa_def_is_not_1bit(nir_ssa_def *def, UNUSED void *unused) 29{ 30 assert(def->bit_size > 1); 31 return true; 32} 33 34static bool 35rewrite_1bit_ssa_def_to_32bit(nir_ssa_def *def, void *_progress) 36{ 37 bool *progress = _progress; 38 if (def->bit_size == 1) { 39 def->bit_size = 32; 40 *progress = true; 41 } 42 return true; 43} 44 45static bool 46lower_alu_instr(nir_builder *b, nir_alu_instr *alu) 47{ 48 const nir_op_info *op_info = &nir_op_infos[alu->op]; 49 50 b->cursor = nir_before_instr(&alu->instr); 51 52 /* Replacement SSA value */ 53 nir_ssa_def *rep = NULL; 54 switch (alu->op) { 55 case nir_op_vec2: 56 case nir_op_vec3: 57 case nir_op_vec4: 58 /* These we expect to have booleans but the opcode doesn't change */ 59 break; 60 61 case nir_op_b2f32: alu->op = nir_op_fmov; break; 62 case nir_op_b2i32: alu->op = nir_op_fmov; break; 63 case nir_op_f2b1: 64 case nir_op_i2b1: 65 rep = nir_sne(b, nir_ssa_for_alu_src(b, alu, 0), 66 nir_imm_float(b, 0)); 67 break; 68 69 case nir_op_flt: alu->op = nir_op_slt; break; 70 case nir_op_fge: alu->op = nir_op_sge; break; 71 case nir_op_feq: alu->op = nir_op_seq; break; 72 case nir_op_fne: alu->op = nir_op_sne; break; 73 case nir_op_ilt: alu->op = nir_op_slt; break; 74 case nir_op_ige: alu->op = nir_op_sge; break; 75 case nir_op_ieq: alu->op = nir_op_seq; break; 76 case nir_op_ine: alu->op = nir_op_sne; break; 77 case nir_op_ult: alu->op = nir_op_slt; break; 78 case nir_op_uge: alu->op = nir_op_sge; break; 79 80 case nir_op_ball_fequal2: alu->op = nir_op_fall_equal2; break; 81 case nir_op_ball_fequal3: alu->op = nir_op_fall_equal3; break; 82 case nir_op_ball_fequal4: alu->op = nir_op_fall_equal4; break; 83 case nir_op_bany_fnequal2: alu->op = nir_op_fany_nequal2; break; 84 case nir_op_bany_fnequal3: alu->op = nir_op_fany_nequal3; break; 85 case nir_op_bany_fnequal4: alu->op = nir_op_fany_nequal4; break; 86 case nir_op_ball_iequal2: alu->op = nir_op_fall_equal2; break; 87 case nir_op_ball_iequal3: alu->op = nir_op_fall_equal3; break; 88 case nir_op_ball_iequal4: alu->op = nir_op_fall_equal4; break; 89 case nir_op_bany_inequal2: alu->op = nir_op_fany_nequal2; break; 90 case nir_op_bany_inequal3: alu->op = nir_op_fany_nequal3; break; 91 case nir_op_bany_inequal4: alu->op = nir_op_fany_nequal4; break; 92 93 case nir_op_bcsel: alu->op = nir_op_fcsel; break; 94 95 case nir_op_imov: alu->op = nir_op_fmov; break; 96 case nir_op_iand: alu->op = nir_op_fmul; break; 97 case nir_op_ixor: alu->op = nir_op_sne; break; 98 case nir_op_ior: alu->op = nir_op_fmax; break; 99 100 case nir_op_inot: 101 rep = nir_seq(b, nir_ssa_for_alu_src(b, alu, 0), 102 nir_imm_float(b, 0)); 103 break; 104 105 default: 106 assert(alu->dest.dest.ssa.bit_size > 1); 107 for (unsigned i = 0; i < op_info->num_inputs; i++) 108 assert(alu->src[i].src.ssa->bit_size > 1); 109 return false; 110 } 111 112 if (rep) { 113 /* We've emitted a replacement instruction */ 114 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(rep)); 115 nir_instr_remove(&alu->instr); 116 } else { 117 if (alu->dest.dest.ssa.bit_size == 1) 118 alu->dest.dest.ssa.bit_size = 32; 119 } 120 121 return true; 122} 123 124static bool 125nir_lower_bool_to_float_impl(nir_function_impl *impl) 126{ 127 bool progress = false; 128 129 nir_builder b; 130 nir_builder_init(&b, impl); 131 132 nir_foreach_block(block, impl) { 133 nir_foreach_instr_safe(instr, block) { 134 switch (instr->type) { 135 case nir_instr_type_alu: 136 progress |= lower_alu_instr(&b, nir_instr_as_alu(instr)); 137 break; 138 139 case nir_instr_type_load_const: { 140 nir_load_const_instr *load = nir_instr_as_load_const(instr); 141 if (load->def.bit_size == 1) { 142 nir_const_value *value = load->value; 143 for (unsigned i = 0; i < load->def.num_components; i++) 144 load->value[i].f32 = value[i].b ? 1.0 : 0.0; 145 load->def.bit_size = 32; 146 progress = true; 147 } 148 break; 149 } 150 151 case nir_instr_type_intrinsic: 152 case nir_instr_type_ssa_undef: 153 case nir_instr_type_phi: 154 case nir_instr_type_tex: 155 nir_foreach_ssa_def(instr, rewrite_1bit_ssa_def_to_32bit, 156 &progress); 157 break; 158 159 default: 160 nir_foreach_ssa_def(instr, assert_ssa_def_is_not_1bit, NULL); 161 } 162 } 163 } 164 165 if (progress) { 166 nir_metadata_preserve(impl, nir_metadata_block_index | 167 nir_metadata_dominance); 168 } 169 170 return progress; 171} 172 173bool 174nir_lower_bool_to_float(nir_shader *shader) 175{ 176 bool progress = false; 177 178 nir_foreach_function(function, shader) { 179 if (function->impl && nir_lower_bool_to_float_impl(function->impl)) 180 progress = true; 181 } 182 183 return progress; 184} 185