nir: Remove deref chain support from lower_system_values
[mesa.git] / src / compiler / nir / nir_lower_system_values.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 "nir_builder.h"
30
31 static bool
32 convert_block(nir_block *block, nir_builder *b)
33 {
34 bool progress = false;
35
36 nir_foreach_instr_safe(instr, block) {
37 if (instr->type != nir_instr_type_intrinsic)
38 continue;
39
40 nir_intrinsic_instr *load_deref = nir_instr_as_intrinsic(instr);
41 if (load_deref->intrinsic != nir_intrinsic_load_deref)
42 continue;
43
44 nir_variable *var =
45 nir_deref_instr_get_variable(nir_src_as_deref(load_deref->src[0]));
46
47 if (var->data.mode != nir_var_system_value)
48 continue;
49
50 b->cursor = nir_after_instr(&load_deref->instr);
51
52 nir_ssa_def *sysval = NULL;
53 switch (var->data.location) {
54 case SYSTEM_VALUE_GLOBAL_INVOCATION_ID: {
55 /* From the GLSL man page for gl_GlobalInvocationID:
56 *
57 * "The value of gl_GlobalInvocationID is equal to
58 * gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID"
59 */
60
61 nir_const_value local_size;
62 memset(&local_size, 0, sizeof(local_size));
63 local_size.u32[0] = b->shader->info.cs.local_size[0];
64 local_size.u32[1] = b->shader->info.cs.local_size[1];
65 local_size.u32[2] = b->shader->info.cs.local_size[2];
66
67 nir_ssa_def *group_id = nir_load_work_group_id(b);
68 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
69
70 sysval = nir_iadd(b, nir_imul(b, group_id,
71 nir_build_imm(b, 3, 32, local_size)),
72 local_id);
73 break;
74 }
75
76 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX: {
77 /* If lower_cs_local_index_from_id is true, then we derive the local
78 * index from the local id.
79 */
80 if (!b->shader->options->lower_cs_local_index_from_id)
81 break;
82
83 /* From the GLSL man page for gl_LocalInvocationIndex:
84 *
85 * "The value of gl_LocalInvocationIndex is equal to
86 * gl_LocalInvocationID.z * gl_WorkGroupSize.x *
87 * gl_WorkGroupSize.y + gl_LocalInvocationID.y *
88 * gl_WorkGroupSize.x + gl_LocalInvocationID.x"
89 */
90 nir_ssa_def *local_id = nir_load_local_invocation_id(b);
91
92 nir_ssa_def *size_x =
93 nir_imm_int(b, b->shader->info.cs.local_size[0]);
94 nir_ssa_def *size_y =
95 nir_imm_int(b, b->shader->info.cs.local_size[1]);
96
97 sysval = nir_imul(b, nir_channel(b, local_id, 2),
98 nir_imul(b, size_x, size_y));
99 sysval = nir_iadd(b, sysval,
100 nir_imul(b, nir_channel(b, local_id, 1), size_x));
101 sysval = nir_iadd(b, sysval, nir_channel(b, local_id, 0));
102 break;
103 }
104
105 case SYSTEM_VALUE_LOCAL_GROUP_SIZE: {
106 nir_const_value local_size;
107 memset(&local_size, 0, sizeof(local_size));
108 local_size.u32[0] = b->shader->info.cs.local_size[0];
109 local_size.u32[1] = b->shader->info.cs.local_size[1];
110 local_size.u32[2] = b->shader->info.cs.local_size[2];
111 sysval = nir_build_imm(b, 3, 32, local_size);
112 break;
113 }
114
115 case SYSTEM_VALUE_VERTEX_ID:
116 if (b->shader->options->vertex_id_zero_based) {
117 sysval = nir_iadd(b,
118 nir_load_vertex_id_zero_base(b),
119 nir_load_first_vertex(b));
120 } else {
121 sysval = nir_load_vertex_id(b);
122 }
123 break;
124
125 case SYSTEM_VALUE_BASE_VERTEX:
126 /**
127 * From the OpenGL 4.6 (11.1.3.9 Shader Inputs) specification:
128 *
129 * "gl_BaseVertex holds the integer value passed to the baseVertex
130 * parameter to the command that resulted in the current shader
131 * invocation. In the case where the command has no baseVertex
132 * parameter, the value of gl_BaseVertex is zero."
133 */
134 if (b->shader->options->lower_base_vertex)
135 sysval = nir_iand(b,
136 nir_load_is_indexed_draw(b),
137 nir_load_first_vertex(b));
138 break;
139
140 case SYSTEM_VALUE_INSTANCE_INDEX:
141 sysval = nir_iadd(b,
142 nir_load_instance_id(b),
143 nir_load_base_instance(b));
144 break;
145
146 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
147 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
148 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
149 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
150 case SYSTEM_VALUE_SUBGROUP_LT_MASK: {
151 nir_intrinsic_op op =
152 nir_intrinsic_from_system_value(var->data.location);
153 nir_intrinsic_instr *load = nir_intrinsic_instr_create(b->shader, op);
154 nir_ssa_dest_init_for_type(&load->instr, &load->dest,
155 var->type, NULL);
156 load->num_components = load->dest.ssa.num_components;
157 nir_builder_instr_insert(b, &load->instr);
158 sysval = &load->dest.ssa;
159 break;
160 }
161
162 case SYSTEM_VALUE_DEVICE_INDEX:
163 if (b->shader->options->lower_device_index_to_zero)
164 sysval = nir_imm_int(b, 0);
165 break;
166
167 default:
168 break;
169 }
170
171 if (sysval == NULL) {
172 nir_intrinsic_op sysval_op =
173 nir_intrinsic_from_system_value(var->data.location);
174 sysval = nir_load_system_value(b, sysval_op, 0);
175 }
176
177 nir_ssa_def_rewrite_uses(&load_deref->dest.ssa, nir_src_for_ssa(sysval));
178 nir_instr_remove(&load_deref->instr);
179
180 progress = true;
181 }
182
183 return progress;
184 }
185
186 static bool
187 convert_impl(nir_function_impl *impl)
188 {
189 bool progress = false;
190 nir_builder builder;
191 nir_builder_init(&builder, impl);
192
193 nir_foreach_block(block, impl) {
194 progress |= convert_block(block, &builder);
195 }
196
197 nir_metadata_preserve(impl, nir_metadata_block_index |
198 nir_metadata_dominance);
199 return progress;
200 }
201
202 bool
203 nir_lower_system_values(nir_shader *shader)
204 {
205 bool progress = false;
206
207 nir_assert_unlowered_derefs(shader, nir_lower_load_store_derefs);
208
209 nir_foreach_function(function, shader) {
210 if (function->impl)
211 progress = convert_impl(function->impl) || progress;
212 }
213
214 /* We're going to delete the variables so we need to clean up all those
215 * derefs we left lying around.
216 */
217 nir_remove_dead_derefs(shader);
218
219 exec_list_make_empty(&shader->system_values);
220
221 return progress;
222 }