1/* 2 * Copyright (C) 2019 Collabora, Ltd. 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 * Authors (Collabora): 24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com> 25 */ 26 27/** 28 * @file 29 * 30 * Flushes undefined SSA values to a zero vector fo the appropriate component 31 * count, to avoid undefined behaviour in the resulting shader. Not required 32 * for conformance as use of uninitialized variables is explicitly left 33 * undefined by the spec. Works around buggy apps, however. 34 * 35 * Call immediately after nir_opt_undef. If called before, larger optimization 36 * opportunities from the former pass will be missed. If called outside of an 37 * optimization loop, constant propagation and algebraic optimizations won't be 38 * able to kick in to reduce stuff consuming the zero. 39 */ 40 41#include "nir_builder.h" 42 43static bool 44lower_undef_instr_to_zero(nir_builder *b, nir_instr *instr, UNUSED void *_state) 45{ 46 if (instr->type != nir_instr_type_ssa_undef) 47 return false; 48 49 nir_ssa_undef_instr *und = nir_instr_as_ssa_undef(instr); 50 b->cursor = nir_instr_remove(&und->instr); 51 nir_ssa_def *zero = nir_imm_zero(b, und->def.num_components, 52 und->def.bit_size); 53 nir_ssa_def_rewrite_uses(&und->def, zero); 54 return true; 55} 56 57bool 58nir_lower_undef_to_zero(nir_shader *shader) 59{ 60 return nir_shader_instructions_pass(shader, lower_undef_instr_to_zero, 61 nir_metadata_block_index | 62 nir_metadata_dominance, NULL); 63} 64