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