xref: /aosp_15_r20/external/mesa3d/src/panfrost/midgard/nir_fuse_io_16.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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
nir_src_is_f2fmp(nir_src * use)36 nir_src_is_f2fmp(nir_src *use)
37 {
38    nir_instr *parent = nir_src_parent_instr(use);
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
nir_fuse_io_16(nir_shader * shader)48 nir_fuse_io_16(nir_shader *shader)
49 {
50    bool progress = false;
51 
52    nir_foreach_function_impl(impl, shader) {
53       nir_foreach_block(block, impl) {
54          nir_foreach_instr_safe(instr, block) {
55             if (instr->type != nir_instr_type_intrinsic)
56                continue;
57 
58             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
59 
60             if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
61                continue;
62 
63             if (intr->def.bit_size != 32)
64                continue;
65 
66             /* We swizzle at a 32-bit level so need a multiple of 2. We could
67              * do a bit better and handle even components though */
68             if (nir_intrinsic_component(intr))
69                continue;
70 
71             bool valid = true;
72 
73             nir_foreach_use_including_if(src, &intr->def)
74                valid &= !nir_src_is_if(src) && nir_src_is_f2fmp(src);
75 
76             if (!valid)
77                continue;
78 
79             intr->def.bit_size = 16;
80 
81             nir_builder b = nir_builder_at(nir_after_instr(instr));
82 
83             /* The f2f32(f2fmp(x)) will cancel by opt_algebraic */
84             nir_def *conv = nir_f2f32(&b, &intr->def);
85             nir_def_rewrite_uses_after(&intr->def, conv, conv->parent_instr);
86 
87             progress |= true;
88          }
89       }
90 
91       nir_metadata_preserve(impl,
92                             nir_metadata_control_flow);
93    }
94 
95    return progress;
96 }
97