nir: Fix indexing of atomic counter arrays with a constant value.
[mesa.git] / src / glsl / nir / nir_lower_atomics.c
1 /*
2 * Copyright © 2014 Intel Corporation
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 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "main/config.h"
30 #include <assert.h>
31
32 /*
33 * replace atomic counter intrinsics that use a variable with intrinsics
34 * that directly store the buffer index and byte offset
35 */
36
37 static void
38 lower_instr(nir_intrinsic_instr *instr, nir_function_impl *impl)
39 {
40 nir_intrinsic_op op;
41 switch (instr->intrinsic) {
42 case nir_intrinsic_atomic_counter_read_var:
43 op = nir_intrinsic_atomic_counter_read;
44 break;
45
46 case nir_intrinsic_atomic_counter_inc_var:
47 op = nir_intrinsic_atomic_counter_inc;
48 break;
49
50 case nir_intrinsic_atomic_counter_dec_var:
51 op = nir_intrinsic_atomic_counter_dec;
52 break;
53
54 default:
55 return;
56 }
57
58 if (instr->variables[0]->var->data.mode != nir_var_uniform)
59 return; /* atomics passed as function arguments can't be lowered */
60
61 void *mem_ctx = ralloc_parent(instr);
62
63 nir_intrinsic_instr *new_instr = nir_intrinsic_instr_create(mem_ctx, op);
64 new_instr->const_index[0] =
65 (int) instr->variables[0]->var->data.atomic.buffer_index;
66
67 nir_load_const_instr *offset_const = nir_load_const_instr_create(mem_ctx, 1);
68 offset_const->value.u[0] = instr->variables[0]->var->data.atomic.offset;
69
70 nir_instr_insert_before(&instr->instr, &offset_const->instr);
71
72 nir_ssa_def *offset_def = &offset_const->def;
73
74 if (instr->variables[0]->deref.child != NULL) {
75 assert(instr->variables[0]->deref.child->deref_type ==
76 nir_deref_type_array);
77 nir_deref_array *deref_array =
78 nir_deref_as_array(instr->variables[0]->deref.child);
79 assert(deref_array->deref.child == NULL);
80
81 offset_const->value.u[0] +=
82 deref_array->base_offset * ATOMIC_COUNTER_SIZE;
83
84 if (deref_array->deref_array_type == nir_deref_array_type_indirect) {
85 nir_load_const_instr *atomic_counter_size =
86 nir_load_const_instr_create(mem_ctx, 1);
87 atomic_counter_size->value.u[0] = ATOMIC_COUNTER_SIZE;
88 nir_instr_insert_before(&instr->instr, &atomic_counter_size->instr);
89
90 nir_alu_instr *mul = nir_alu_instr_create(mem_ctx, nir_op_imul);
91 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, NULL);
92 mul->dest.write_mask = 0x1;
93 nir_src_copy(&mul->src[0].src, &deref_array->indirect, mem_ctx);
94 mul->src[1].src.is_ssa = true;
95 mul->src[1].src.ssa = &atomic_counter_size->def;
96 nir_instr_insert_before(&instr->instr, &mul->instr);
97
98 nir_alu_instr *add = nir_alu_instr_create(mem_ctx, nir_op_iadd);
99 nir_ssa_dest_init(&add->instr, &add->dest.dest, 1, NULL);
100 add->dest.write_mask = 0x1;
101 add->src[0].src.is_ssa = true;
102 add->src[0].src.ssa = &mul->dest.dest.ssa;
103 add->src[1].src.is_ssa = true;
104 add->src[1].src.ssa = &offset_const->def;
105 nir_instr_insert_before(&instr->instr, &add->instr);
106
107 offset_def = &add->dest.dest.ssa;
108 }
109 }
110
111 new_instr->src[0].is_ssa = true;
112 new_instr->src[0].ssa = offset_def;;
113
114 if (instr->dest.is_ssa) {
115 nir_ssa_dest_init(&new_instr->instr, &new_instr->dest,
116 instr->dest.ssa.num_components, NULL);
117 nir_ssa_def_rewrite_uses(&instr->dest.ssa,
118 nir_src_for_ssa(&new_instr->dest.ssa),
119 mem_ctx);
120 } else {
121 nir_dest_copy(&new_instr->dest, &instr->dest, mem_ctx);
122 }
123
124 nir_instr_insert_before(&instr->instr, &new_instr->instr);
125 nir_instr_remove(&instr->instr);
126 }
127
128 static bool
129 lower_block(nir_block *block, void *state)
130 {
131 nir_foreach_instr_safe(block, instr) {
132 if (instr->type == nir_instr_type_intrinsic)
133 lower_instr(nir_instr_as_intrinsic(instr), state);
134 }
135
136 return true;
137 }
138
139 void
140 nir_lower_atomics(nir_shader *shader)
141 {
142 nir_foreach_overload(shader, overload) {
143 if (overload->impl) {
144 nir_foreach_block(overload->impl, lower_block, overload->impl);
145 nir_metadata_preserve(overload->impl, nir_metadata_block_index |
146 nir_metadata_dominance);
147 }
148 }
149 }