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 "compiler/nir/nir.h"
42 #include "compiler/nir/nir_builder.h"
43
44 bool nir_undef_to_zero(nir_shader *shader);
45
46 bool
nir_undef_to_zero(nir_shader * shader)47 nir_undef_to_zero(nir_shader *shader)
48 {
49 bool progress = false;
50
51 nir_foreach_function(function, shader) {
52 if (!function->impl) continue;
53
54 nir_builder b;
55 nir_builder_init(&b, function->impl);
56
57 nir_foreach_block(block, function->impl) {
58 nir_foreach_instr_safe(instr, block) {
59 if (instr->type != nir_instr_type_ssa_undef) continue;
60
61 nir_ssa_undef_instr *und = nir_instr_as_ssa_undef(instr);
62
63 /* Get the required size */
64 unsigned c = und->def.num_components;
65 unsigned s = und->def.bit_size;
66
67 nir_const_value v[NIR_MAX_VEC_COMPONENTS];
68 memset(v, 0, sizeof(v));
69
70 b.cursor = nir_before_instr(instr);
71 nir_ssa_def *zero = nir_build_imm(&b, c, s, v);
72 nir_src zerosrc = nir_src_for_ssa(zero);
73
74 nir_ssa_def_rewrite_uses(&und->def, zerosrc);
75
76 progress |= true;
77 }
78 }
79
80 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
81
82 }
83
84 return progress;
85 }
86
87
88