nir: Add nir_foreach_shader_in/out_variable helpers
[mesa.git] / src / panfrost / midgard / nir_fuse_io_16.c
1 /*
2 * Copyright (C) 2020 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 /* Fuses f2f16 modifiers into loads */
28
29 #include "compiler/nir/nir.h"
30 #include "compiler/nir/nir_builder.h"
31 #include "panfrost/util/pan_ir.h"
32
33 bool nir_fuse_io_16(nir_shader *shader);
34
35 static bool
36 nir_src_is_f2fmp(nir_src *use)
37 {
38 nir_instr *parent = use->parent_instr;
39
40 if (parent->type != nir_instr_type_alu)
41 return false;
42
43 nir_alu_instr *alu = nir_instr_as_alu(parent);
44 return (alu->op == nir_op_f2fmp);
45 }
46
47 bool
48 nir_fuse_io_16(nir_shader *shader)
49 {
50 bool progress = false;
51
52 nir_foreach_function(function, shader) {
53 if (!function->impl) continue;
54
55 nir_builder b;
56 nir_builder_init(&b, function->impl);
57
58 nir_foreach_block(block, function->impl) {
59 nir_foreach_instr_safe(instr, block) {
60 if (instr->type != nir_instr_type_intrinsic) continue;
61
62 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
63
64 if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
65 continue;
66
67 if (nir_dest_bit_size(intr->dest) != 32)
68 continue;
69
70 /* We swizzle at a 32-bit level so need a multiple of 2. We could
71 * do a bit better and handle even components though */
72 if (nir_intrinsic_component(intr))
73 continue;
74
75 if (!intr->dest.is_ssa)
76 continue;
77
78 if (!list_is_empty(&intr->dest.ssa.if_uses))
79 return false;
80
81 bool valid = true;
82
83 nir_foreach_use(src, &intr->dest.ssa)
84 valid &= nir_src_is_f2fmp(src);
85
86 if (!valid)
87 continue;
88
89 intr->dest.ssa.bit_size = 16;
90 progress |= true;
91 }
92 }
93
94 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
95
96 }
97
98 return progress;
99 }