v3d: Use nir_remove_unused_io_vars to handle binner shader output DCE
[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 * Currently, it splits VS inputs and uniforms into scalars, drops any
32 * non-position outputs in coordinate shaders, and fixes up the addressing on
33 * indirect uniform loads. FS input and VS output scalarization is handled by
34 * nir_lower_io_to_scalar().
35 */
36
37 static void
38 replace_intrinsic_with_vec(nir_builder *b, nir_intrinsic_instr *intr,
39 nir_ssa_def **comps)
40 {
41
42 /* Batch things back together into a vector. This will get split by
43 * the later ALU scalarization pass.
44 */
45 nir_ssa_def *vec = nir_vec(b, comps, intr->num_components);
46
47 /* Replace the old intrinsic with a reference to our reconstructed
48 * vector.
49 */
50 nir_ssa_def_rewrite_uses(&intr->dest.ssa, nir_src_for_ssa(vec));
51 nir_instr_remove(&intr->instr);
52 }
53
54 static void
55 v3d_nir_lower_uniform(struct v3d_compile *c, nir_builder *b,
56 nir_intrinsic_instr *intr)
57 {
58 b->cursor = nir_before_instr(&intr->instr);
59
60 /* Generate scalar loads equivalent to the original vector. */
61 nir_ssa_def *dests[4];
62 for (unsigned i = 0; i < intr->num_components; i++) {
63 nir_intrinsic_instr *intr_comp =
64 nir_intrinsic_instr_create(c->s, intr->intrinsic);
65 intr_comp->num_components = 1;
66 nir_ssa_dest_init(&intr_comp->instr, &intr_comp->dest, 1, 32, NULL);
67
68 /* Convert the uniform offset to bytes. If it happens
69 * to be a constant, constant-folding will clean up
70 * the shift for us.
71 */
72 nir_intrinsic_set_base(intr_comp,
73 nir_intrinsic_base(intr) * 16 +
74 i * 4);
75
76 intr_comp->src[0] =
77 nir_src_for_ssa(nir_ishl(b, intr->src[0].ssa,
78 nir_imm_int(b, 4)));
79
80 dests[i] = &intr_comp->dest.ssa;
81
82 nir_builder_instr_insert(b, &intr_comp->instr);
83 }
84
85 replace_intrinsic_with_vec(b, intr, dests);
86 }
87
88 static void
89 v3d_nir_lower_io_instr(struct v3d_compile *c, nir_builder *b,
90 struct nir_instr *instr)
91 {
92 if (instr->type != nir_instr_type_intrinsic)
93 return;
94 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
95
96 switch (intr->intrinsic) {
97 case nir_intrinsic_load_input:
98 break;
99
100 case nir_intrinsic_load_uniform:
101 v3d_nir_lower_uniform(c, b, intr);
102 break;
103
104 case nir_intrinsic_load_user_clip_plane:
105 default:
106 break;
107 }
108 }
109
110 static bool
111 v3d_nir_lower_io_impl(struct v3d_compile *c, nir_function_impl *impl)
112 {
113 nir_builder b;
114 nir_builder_init(&b, impl);
115
116 nir_foreach_block(block, impl) {
117 nir_foreach_instr_safe(instr, block)
118 v3d_nir_lower_io_instr(c, &b, instr);
119 }
120
121 nir_metadata_preserve(impl, nir_metadata_block_index |
122 nir_metadata_dominance);
123
124 return true;
125 }
126
127 void
128 v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c)
129 {
130 nir_foreach_function(function, s) {
131 if (function->impl)
132 v3d_nir_lower_io_impl(c, function->impl);
133 }
134 }