1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2020 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors (Collabora):
24bf215546Sopenharmony_ci *   Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* Fuses f2f16 modifiers into loads */
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "compiler/nir/nir.h"
30bf215546Sopenharmony_ci#include "compiler/nir/nir_builder.h"
31bf215546Sopenharmony_ci#include "panfrost/util/pan_ir.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cibool nir_fuse_io_16(nir_shader *shader);
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic bool
36bf215546Sopenharmony_cinir_src_is_f2fmp(nir_src *use)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci   nir_instr *parent = use->parent_instr;
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci   if (parent->type != nir_instr_type_alu)
41bf215546Sopenharmony_ci      return false;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   nir_alu_instr *alu = nir_instr_as_alu(parent);
44bf215546Sopenharmony_ci   return (alu->op == nir_op_f2fmp);
45bf215546Sopenharmony_ci}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_cibool
48bf215546Sopenharmony_cinir_fuse_io_16(nir_shader *shader)
49bf215546Sopenharmony_ci{
50bf215546Sopenharmony_ci   bool progress = false;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
53bf215546Sopenharmony_ci      if (!function->impl) continue;
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci      nir_builder b;
56bf215546Sopenharmony_ci      nir_builder_init(&b, function->impl);
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      nir_foreach_block(block, function->impl) {
59bf215546Sopenharmony_ci         nir_foreach_instr_safe(instr, block) {
60bf215546Sopenharmony_ci            if (instr->type != nir_instr_type_intrinsic) continue;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci            nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci            if (intr->intrinsic != nir_intrinsic_load_interpolated_input)
65bf215546Sopenharmony_ci                    continue;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci            if (nir_dest_bit_size(intr->dest) != 32)
68bf215546Sopenharmony_ci                    continue;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci            /* We swizzle at a 32-bit level so need a multiple of 2. We could
71bf215546Sopenharmony_ci             * do a bit better and handle even components though */
72bf215546Sopenharmony_ci            if (nir_intrinsic_component(intr))
73bf215546Sopenharmony_ci               continue;
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci            if (!intr->dest.is_ssa)
76bf215546Sopenharmony_ci               continue;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci            if (!list_is_empty(&intr->dest.ssa.if_uses))
79bf215546Sopenharmony_ci               return false;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci            bool valid = true;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci            nir_foreach_use(src, &intr->dest.ssa)
84bf215546Sopenharmony_ci               valid &= nir_src_is_f2fmp(src);
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci            if (!valid)
87bf215546Sopenharmony_ci               continue;
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci            intr->dest.ssa.bit_size = 16;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci            nir_builder b;
92bf215546Sopenharmony_ci            nir_builder_init(&b, function->impl);
93bf215546Sopenharmony_ci            b.cursor = nir_after_instr(instr);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci            /* The f2f32(f2fmp(x)) will cancel by opt_algebraic */
96bf215546Sopenharmony_ci            nir_ssa_def *conv = nir_f2f32(&b, &intr->dest.ssa);
97bf215546Sopenharmony_ci            nir_ssa_def_rewrite_uses_after(&intr->dest.ssa, conv,
98bf215546Sopenharmony_ci                                           conv->parent_instr);
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci            progress |= true;
101bf215546Sopenharmony_ci         }
102bf215546Sopenharmony_ci      }
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci      nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   return progress;
109bf215546Sopenharmony_ci}
110