nir: Add a couple of iand/ior optimizations
[mesa.git] / src / compiler / nir / nir_lower_atomics_to_ssbo.c
1 /*
2 * Copyright © 2017 Red Hat
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:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "nir.h"
28 #include "nir_builder.h"
29
30 #if defined(_WIN32) && !defined(snprintf)
31 #define snprintf _snprintf
32 #endif
33
34 /*
35 * Remap atomic counters to SSBOs. Atomic counters get remapped to
36 * SSBO binding points [0..ssbo_offset) and the original SSBOs are
37 * remapped to [ssbo_offset..n) (mostly to align with what mesa/st
38 * does.
39 */
40
41 static bool
42 lower_instr(nir_intrinsic_instr *instr, unsigned ssbo_offset, nir_builder *b)
43 {
44 nir_intrinsic_op op;
45 int idx_src;
46
47 b->cursor = nir_before_instr(&instr->instr);
48
49 switch (instr->intrinsic) {
50 case nir_intrinsic_ssbo_atomic_add:
51 case nir_intrinsic_ssbo_atomic_imin:
52 case nir_intrinsic_ssbo_atomic_umin:
53 case nir_intrinsic_ssbo_atomic_imax:
54 case nir_intrinsic_ssbo_atomic_umax:
55 case nir_intrinsic_ssbo_atomic_and:
56 case nir_intrinsic_ssbo_atomic_or:
57 case nir_intrinsic_ssbo_atomic_xor:
58 case nir_intrinsic_ssbo_atomic_exchange:
59 case nir_intrinsic_ssbo_atomic_comp_swap:
60 case nir_intrinsic_store_ssbo:
61 case nir_intrinsic_load_ssbo:
62 case nir_intrinsic_get_buffer_size:
63 /* easy case, keep same opcode and just remap SSBO buffer index: */
64 op = instr->intrinsic;
65 idx_src = (op == nir_intrinsic_store_ssbo) ? 1 : 0;
66 nir_ssa_def *old_idx = nir_ssa_for_src(b, instr->src[idx_src], 1);
67 nir_ssa_def *new_idx = nir_iadd(b, old_idx, nir_imm_int(b, ssbo_offset));
68 nir_instr_rewrite_src(&instr->instr,
69 &instr->src[idx_src],
70 nir_src_for_ssa(new_idx));
71 return true;
72 case nir_intrinsic_atomic_counter_inc:
73 case nir_intrinsic_atomic_counter_add:
74 case nir_intrinsic_atomic_counter_pre_dec:
75 case nir_intrinsic_atomic_counter_post_dec:
76 /* inc and dec get remapped to add: */
77 op = nir_intrinsic_ssbo_atomic_add;
78 break;
79 case nir_intrinsic_atomic_counter_read:
80 op = nir_intrinsic_load_ssbo;
81 break;
82 case nir_intrinsic_atomic_counter_min:
83 op = nir_intrinsic_ssbo_atomic_umin;
84 break;
85 case nir_intrinsic_atomic_counter_max:
86 op = nir_intrinsic_ssbo_atomic_umax;
87 break;
88 case nir_intrinsic_atomic_counter_and:
89 op = nir_intrinsic_ssbo_atomic_and;
90 break;
91 case nir_intrinsic_atomic_counter_or:
92 op = nir_intrinsic_ssbo_atomic_or;
93 break;
94 case nir_intrinsic_atomic_counter_xor:
95 op = nir_intrinsic_ssbo_atomic_xor;
96 break;
97 case nir_intrinsic_atomic_counter_exchange:
98 op = nir_intrinsic_ssbo_atomic_exchange;
99 break;
100 case nir_intrinsic_atomic_counter_comp_swap:
101 op = nir_intrinsic_ssbo_atomic_comp_swap;
102 break;
103 default:
104 return false;
105 }
106
107 nir_ssa_def *buffer = nir_imm_int(b, nir_intrinsic_base(instr));
108 nir_ssa_def *temp = NULL;
109 nir_intrinsic_instr *new_instr =
110 nir_intrinsic_instr_create(ralloc_parent(instr), op);
111
112 /* a couple instructions need special handling since they don't map
113 * 1:1 with ssbo atomics
114 */
115 switch (instr->intrinsic) {
116 case nir_intrinsic_atomic_counter_inc:
117 /* remapped to ssbo_atomic_add: { buffer_idx, offset, +1 } */
118 temp = nir_imm_int(b, +1);
119 new_instr->src[0] = nir_src_for_ssa(buffer);
120 nir_src_copy(&new_instr->src[1], &instr->src[0], new_instr);
121 new_instr->src[2] = nir_src_for_ssa(temp);
122 break;
123 case nir_intrinsic_atomic_counter_pre_dec:
124 case nir_intrinsic_atomic_counter_post_dec:
125 /* remapped to ssbo_atomic_add: { buffer_idx, offset, -1 } */
126 /* NOTE semantic difference so we adjust the return value below */
127 temp = nir_imm_int(b, -1);
128 new_instr->src[0] = nir_src_for_ssa(buffer);
129 nir_src_copy(&new_instr->src[1], &instr->src[0], new_instr);
130 new_instr->src[2] = nir_src_for_ssa(temp);
131 break;
132 case nir_intrinsic_atomic_counter_read:
133 /* remapped to load_ssbo: { buffer_idx, offset } */
134 new_instr->src[0] = nir_src_for_ssa(buffer);
135 nir_src_copy(&new_instr->src[1], &instr->src[0], new_instr);
136 break;
137 default:
138 /* remapped to ssbo_atomic_x: { buffer_idx, offset, data, (compare)? } */
139 new_instr->src[0] = nir_src_for_ssa(buffer);
140 nir_src_copy(&new_instr->src[1], &instr->src[0], new_instr);
141 nir_src_copy(&new_instr->src[2], &instr->src[1], new_instr);
142 if (op == nir_intrinsic_ssbo_atomic_comp_swap)
143 nir_src_copy(&new_instr->src[3], &instr->src[2], new_instr);
144 break;
145 }
146
147 nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
148 instr->dest.ssa.num_components,
149 instr->dest.ssa.bit_size, NULL);
150 nir_instr_insert_before(&instr->instr, &new_instr->instr);
151 nir_instr_remove(&instr->instr);
152
153 if (instr->intrinsic == nir_intrinsic_atomic_counter_pre_dec) {
154 b->cursor = nir_after_instr(&new_instr->instr);
155 nir_ssa_def *result = nir_iadd(b, &new_instr->dest.ssa, temp);
156 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(result));
157 } else {
158 nir_ssa_def_rewrite_uses(&instr->dest.ssa, nir_src_for_ssa(&new_instr->dest.ssa));
159 }
160
161 /* we could be replacing an intrinsic with fixed # of dest num_components
162 * with one that has variable number. So best to take this from the dest:
163 */
164 new_instr->num_components = instr->dest.ssa.num_components;
165
166 return true;
167 }
168
169 static bool
170 is_atomic_uint(const struct glsl_type *type)
171 {
172 if (glsl_get_base_type(type) == GLSL_TYPE_ARRAY)
173 return is_atomic_uint(glsl_get_array_element(type));
174 return glsl_get_base_type(type) == GLSL_TYPE_ATOMIC_UINT;
175 }
176
177 bool
178 nir_lower_atomics_to_ssbo(nir_shader *shader, unsigned ssbo_offset)
179 {
180 bool progress = false;
181
182 nir_foreach_function(function, shader) {
183 if (function->impl) {
184 nir_builder builder;
185 nir_builder_init(&builder, function->impl);
186 nir_foreach_block(block, function->impl) {
187 nir_foreach_instr_safe(instr, block) {
188 if (instr->type == nir_instr_type_intrinsic)
189 progress |= lower_instr(nir_instr_as_intrinsic(instr),
190 ssbo_offset, &builder);
191 }
192 }
193
194 nir_metadata_preserve(function->impl, nir_metadata_block_index |
195 nir_metadata_dominance);
196 }
197 }
198
199 if (progress) {
200 /* replace atomic_uint uniforms with ssbo's: */
201 unsigned replaced = 0;
202 nir_foreach_variable_safe(var, &shader->uniforms) {
203 if (is_atomic_uint(var->type)) {
204 exec_node_remove(&var->node);
205
206 if (replaced & (1 << var->data.binding))
207 continue;
208
209 nir_variable *ssbo;
210 char name[16];
211
212 /* A length of 0 is used to denote unsized arrays */
213 const struct glsl_type *type = glsl_array_type(glsl_uint_type(), 0);
214
215 snprintf(name, sizeof(name), "counter%d", var->data.binding);
216
217 ssbo = nir_variable_create(shader, nir_var_shader_storage,
218 type, name);
219 ssbo->data.binding = var->data.binding;
220
221 struct glsl_struct_field field = {
222 .type = type,
223 .name = "counters",
224 .location = -1,
225 };
226
227 ssbo->interface_type =
228 glsl_interface_type(&field, 1, GLSL_INTERFACE_PACKING_STD430,
229 false, "counters");
230
231 replaced |= (1 << var->data.binding);
232 }
233 }
234 }
235
236 return progress;
237 }
238