v3d: Stop scalarizing our uniform loads.
[mesa.git] / src / broadcom / compiler / v3d_nir_lower_io.c
1 /*
2 * Copyright © 2015 Broadcom
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
24 #include "compiler/v3d_compiler.h"
25 #include "compiler/nir/nir_builder.h"
26
27 /**
28 * Walks the NIR generated by TGSI-to-NIR or GLSL-to-NIR to lower its io
29 * intrinsics into something amenable to the V3D architecture.
30 *
31 * After moving more and more logic to NIR, all that's left here is fixing up
32 * addressing on uniform loads. FS input and VS output scalarization is
33 * handled by nir_lower_io_to_scalar().
34 */
35
36 /* Convert the uniform offset to bytes. If it happens to be a constant,
37 * constant-folding will clean up the shift for us.
38 */
39 static void
40 v3d_nir_lower_uniform(struct v3d_compile *c, nir_builder *b,
41 nir_intrinsic_instr *intr)
42 {
43 b->cursor = nir_before_instr(&intr->instr);
44
45 nir_intrinsic_set_base(intr, nir_intrinsic_base(intr) * 16);
46
47 nir_instr_rewrite_src(&intr->instr,
48 &intr->src[0],
49 nir_src_for_ssa(nir_ishl(b, intr->src[0].ssa,
50 nir_imm_int(b, 4))));
51 }
52
53 static void
54 v3d_nir_lower_io_instr(struct v3d_compile *c, nir_builder *b,
55 struct nir_instr *instr)
56 {
57 if (instr->type != nir_instr_type_intrinsic)
58 return;
59 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
60
61 switch (intr->intrinsic) {
62 case nir_intrinsic_load_uniform:
63 v3d_nir_lower_uniform(c, b, intr);
64 break;
65
66 default:
67 break;
68 }
69 }
70
71 static bool
72 v3d_nir_lower_io_impl(struct v3d_compile *c, nir_function_impl *impl)
73 {
74 nir_builder b;
75 nir_builder_init(&b, impl);
76
77 nir_foreach_block(block, impl) {
78 nir_foreach_instr_safe(instr, block)
79 v3d_nir_lower_io_instr(c, &b, instr);
80 }
81
82 nir_metadata_preserve(impl, nir_metadata_block_index |
83 nir_metadata_dominance);
84
85 return true;
86 }
87
88 void
89 v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c)
90 {
91 nir_foreach_function(function, s) {
92 if (function->impl)
93 v3d_nir_lower_io_impl(c, function->impl);
94 }
95 }