1 /*
2 * Copyright (c) 2019 Qiang Yu <[email protected]>
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include "nir.h"
26 #include "nir_builder.h"
27 #include "lima_ir.h"
28
29 static void
lower_load_uniform_to_scalar(nir_builder * b,nir_intrinsic_instr * intr)30 lower_load_uniform_to_scalar(nir_builder *b, nir_intrinsic_instr *intr)
31 {
32 b->cursor = nir_before_instr(&intr->instr);
33
34 nir_def *loads[4];
35 for (unsigned i = 0; i < intr->num_components; i++) {
36 nir_intrinsic_instr *chan_intr =
37 nir_intrinsic_instr_create(b->shader, intr->intrinsic);
38 nir_def_init(&chan_intr->instr, &chan_intr->def, 1,
39 intr->def.bit_size);
40 chan_intr->num_components = 1;
41
42 nir_intrinsic_set_base(chan_intr, nir_intrinsic_base(intr) * 4 + i);
43 nir_intrinsic_set_range(chan_intr, nir_intrinsic_range(intr) * 4);
44 nir_intrinsic_set_dest_type(chan_intr, nir_intrinsic_dest_type(intr));
45
46 chan_intr->src[0] =
47 nir_src_for_ssa(nir_imul_imm(b, intr->src[0].ssa, 4));
48
49 nir_builder_instr_insert(b, &chan_intr->instr);
50
51 loads[i] = &chan_intr->def;
52 }
53
54 nir_def_replace(&intr->def, nir_vec(b, loads, intr->num_components));
55 }
56
57 void
lima_nir_lower_uniform_to_scalar(nir_shader * shader)58 lima_nir_lower_uniform_to_scalar(nir_shader *shader)
59 {
60 nir_foreach_function_impl(impl, shader) {
61 nir_builder b = nir_builder_create(impl);
62
63 nir_foreach_block(block, impl) {
64 nir_foreach_instr_safe(instr, block) {
65 if (instr->type != nir_instr_type_intrinsic)
66 continue;
67
68 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
69
70 if (intr->intrinsic != nir_intrinsic_load_uniform)
71 continue;
72
73 lower_load_uniform_to_scalar(&b, intr);
74 }
75 }
76 }
77 }
78