nir/builder: Add a helper for storing to variable derefs
[mesa.git] / src / compiler / nir / nir_builder.h
1 /*
2 * Copyright © 2014-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 #ifndef NIR_BUILDER_H
25 #define NIR_BUILDER_H
26
27 #include "nir_control_flow.h"
28
29 struct exec_list;
30
31 typedef struct nir_builder {
32 nir_cursor cursor;
33
34 /* Whether new ALU instructions will be marked "exact" */
35 bool exact;
36
37 nir_shader *shader;
38 nir_function_impl *impl;
39 } nir_builder;
40
41 static inline void
42 nir_builder_init(nir_builder *build, nir_function_impl *impl)
43 {
44 memset(build, 0, sizeof(*build));
45 build->exact = false;
46 build->impl = impl;
47 build->shader = impl->function->shader;
48 }
49
50 static inline void
51 nir_builder_init_simple_shader(nir_builder *build, void *mem_ctx,
52 gl_shader_stage stage,
53 const nir_shader_compiler_options *options)
54 {
55 build->shader = nir_shader_create(mem_ctx, stage, options);
56 nir_function *func = nir_function_create(build->shader, "main");
57 build->exact = false;
58 build->impl = nir_function_impl_create(func);
59 build->cursor = nir_after_cf_list(&build->impl->body);
60 }
61
62 static inline void
63 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
64 {
65 nir_instr_insert(build->cursor, instr);
66
67 /* Move the cursor forward. */
68 build->cursor = nir_after_instr(instr);
69 }
70
71 static inline void
72 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
73 {
74 nir_cf_node_insert(build->cursor, cf);
75 }
76
77 static inline nir_ssa_def *
78 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
79 {
80 nir_load_const_instr *load_const =
81 nir_load_const_instr_create(build->shader, num_components);
82 if (!load_const)
83 return NULL;
84
85 load_const->value = value;
86
87 nir_builder_instr_insert(build, &load_const->instr);
88
89 return &load_const->def;
90 }
91
92 static inline nir_ssa_def *
93 nir_imm_float(nir_builder *build, float x)
94 {
95 nir_const_value v;
96
97 memset(&v, 0, sizeof(v));
98 v.f32[0] = x;
99
100 return nir_build_imm(build, 1, v);
101 }
102
103 static inline nir_ssa_def *
104 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
105 {
106 nir_const_value v;
107
108 memset(&v, 0, sizeof(v));
109 v.f32[0] = x;
110 v.f32[1] = y;
111 v.f32[2] = z;
112 v.f32[3] = w;
113
114 return nir_build_imm(build, 4, v);
115 }
116
117 static inline nir_ssa_def *
118 nir_imm_int(nir_builder *build, int x)
119 {
120 nir_const_value v;
121
122 memset(&v, 0, sizeof(v));
123 v.i32[0] = x;
124
125 return nir_build_imm(build, 1, v);
126 }
127
128 static inline nir_ssa_def *
129 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
130 {
131 nir_const_value v;
132
133 memset(&v, 0, sizeof(v));
134 v.i32[0] = x;
135 v.i32[1] = y;
136 v.i32[2] = z;
137 v.i32[3] = w;
138
139 return nir_build_imm(build, 4, v);
140 }
141
142 static inline nir_ssa_def *
143 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
144 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
145 {
146 const nir_op_info *op_info = &nir_op_infos[op];
147 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
148 if (!instr)
149 return NULL;
150
151 instr->exact = build->exact;
152
153 instr->src[0].src = nir_src_for_ssa(src0);
154 if (src1)
155 instr->src[1].src = nir_src_for_ssa(src1);
156 if (src2)
157 instr->src[2].src = nir_src_for_ssa(src2);
158 if (src3)
159 instr->src[3].src = nir_src_for_ssa(src3);
160
161 /* Guess the number of components the destination temporary should have
162 * based on our input sizes, if it's not fixed for the op.
163 */
164 unsigned num_components = op_info->output_size;
165 if (num_components == 0) {
166 for (unsigned i = 0; i < op_info->num_inputs; i++) {
167 if (op_info->input_sizes[i] == 0)
168 num_components = MAX2(num_components,
169 instr->src[i].src.ssa->num_components);
170 }
171 }
172 assert(num_components != 0);
173
174 /* Figure out the bitwidth based on the source bitwidth if the instruction
175 * is variable-width.
176 */
177 unsigned bit_size = nir_alu_type_get_type_size(op_info->output_type);
178 if (bit_size == 0) {
179 for (unsigned i = 0; i < op_info->num_inputs; i++) {
180 unsigned src_bit_size = instr->src[i].src.ssa->bit_size;
181 if (nir_alu_type_get_type_size(op_info->input_types[i]) == 0) {
182 if (bit_size)
183 assert(src_bit_size == bit_size);
184 else
185 bit_size = src_bit_size;
186 } else {
187 assert(src_bit_size ==
188 nir_alu_type_get_type_size(op_info->input_types[i]));
189 }
190 }
191 }
192
193 /* Make sure we don't swizzle from outside of our source vector (like if a
194 * scalar value was passed into a multiply with a vector).
195 */
196 for (unsigned i = 0; i < op_info->num_inputs; i++) {
197 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
198 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
199 }
200 }
201
202 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components,
203 bit_size, NULL);
204 instr->dest.write_mask = (1 << num_components) - 1;
205
206 nir_builder_instr_insert(build, &instr->instr);
207
208 return &instr->dest.dest.ssa;
209 }
210
211 #define ALU1(op) \
212 static inline nir_ssa_def * \
213 nir_##op(nir_builder *build, nir_ssa_def *src0) \
214 { \
215 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
216 }
217
218 #define ALU2(op) \
219 static inline nir_ssa_def * \
220 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
221 { \
222 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
223 }
224
225 #define ALU3(op) \
226 static inline nir_ssa_def * \
227 nir_##op(nir_builder *build, nir_ssa_def *src0, \
228 nir_ssa_def *src1, nir_ssa_def *src2) \
229 { \
230 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
231 }
232
233 #define ALU4(op) \
234 static inline nir_ssa_def * \
235 nir_##op(nir_builder *build, nir_ssa_def *src0, \
236 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
237 { \
238 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
239 }
240
241 #include "nir_builder_opcodes.h"
242
243 static inline nir_ssa_def *
244 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
245 {
246 switch (num_components) {
247 case 4:
248 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
249 case 3:
250 return nir_vec3(build, comp[0], comp[1], comp[2]);
251 case 2:
252 return nir_vec2(build, comp[0], comp[1]);
253 case 1:
254 return comp[0];
255 default:
256 unreachable("bad component count");
257 return NULL;
258 }
259 }
260
261 /**
262 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
263 */
264 static inline nir_ssa_def *
265 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
266 {
267 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
268 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
269 nir_src_bit_size(src.src), NULL);
270 mov->exact = build->exact;
271 mov->dest.write_mask = (1 << num_components) - 1;
272 mov->src[0] = src;
273 nir_builder_instr_insert(build, &mov->instr);
274
275 return &mov->dest.dest.ssa;
276 }
277
278 static inline nir_ssa_def *
279 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
280 {
281 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
282 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
283 nir_src_bit_size(src.src), NULL);
284 mov->exact = build->exact;
285 mov->dest.write_mask = (1 << num_components) - 1;
286 mov->src[0] = src;
287 nir_builder_instr_insert(build, &mov->instr);
288
289 return &mov->dest.dest.ssa;
290 }
291
292 /**
293 * Construct an fmov or imov that reswizzles the source's components.
294 */
295 static inline nir_ssa_def *
296 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
297 unsigned num_components, bool use_fmov)
298 {
299 nir_alu_src alu_src = { NIR_SRC_INIT };
300 alu_src.src = nir_src_for_ssa(src);
301 for (unsigned i = 0; i < num_components; i++)
302 alu_src.swizzle[i] = swiz[i];
303
304 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
305 nir_imov_alu(build, alu_src, num_components);
306 }
307
308 /* Selects the right fdot given the number of components in each source. */
309 static inline nir_ssa_def *
310 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
311 {
312 assert(src0->num_components == src1->num_components);
313 switch (src0->num_components) {
314 case 1: return nir_fmul(build, src0, src1);
315 case 2: return nir_fdot2(build, src0, src1);
316 case 3: return nir_fdot3(build, src0, src1);
317 case 4: return nir_fdot4(build, src0, src1);
318 default:
319 unreachable("bad component size");
320 }
321
322 return NULL;
323 }
324
325 static inline nir_ssa_def *
326 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
327 {
328 unsigned swizzle[4] = {c, c, c, c};
329 return nir_swizzle(b, def, swizzle, 1, false);
330 }
331
332 /**
333 * Turns a nir_src into a nir_ssa_def * so it can be passed to
334 * nir_build_alu()-based builder calls.
335 *
336 * See nir_ssa_for_alu_src() for alu instructions.
337 */
338 static inline nir_ssa_def *
339 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
340 {
341 if (src.is_ssa && src.ssa->num_components == num_components)
342 return src.ssa;
343
344 nir_alu_src alu = { NIR_SRC_INIT };
345 alu.src = src;
346 for (int j = 0; j < 4; j++)
347 alu.swizzle[j] = j;
348
349 return nir_imov_alu(build, alu, num_components);
350 }
351
352 /**
353 * Similar to nir_ssa_for_src(), but for alu src's, respecting the
354 * nir_alu_src's swizzle.
355 */
356 static inline nir_ssa_def *
357 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
358 {
359 static uint8_t trivial_swizzle[4] = { 0, 1, 2, 3 };
360 nir_alu_src *src = &instr->src[srcn];
361 unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
362
363 if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
364 !src->abs && !src->negate &&
365 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
366 return src->src.ssa;
367
368 return nir_imov_alu(build, *src, num_components);
369 }
370
371 static inline nir_ssa_def *
372 nir_load_var(nir_builder *build, nir_variable *var)
373 {
374 const unsigned num_components = glsl_get_vector_elements(var->type);
375
376 nir_intrinsic_instr *load =
377 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_var);
378 load->num_components = num_components;
379 load->variables[0] = nir_deref_var_create(load, var);
380 nir_ssa_dest_init(&load->instr, &load->dest, num_components,
381 glsl_get_bit_size(glsl_get_base_type(var->type)), NULL);
382 nir_builder_instr_insert(build, &load->instr);
383 return &load->dest.ssa;
384 }
385
386 static inline void
387 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
388 unsigned writemask)
389 {
390 const unsigned num_components = glsl_get_vector_elements(var->type);
391
392 nir_intrinsic_instr *store =
393 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
394 store->num_components = num_components;
395 nir_intrinsic_set_write_mask(store, writemask);
396 store->variables[0] = nir_deref_var_create(store, var);
397 store->src[0] = nir_src_for_ssa(value);
398 nir_builder_instr_insert(build, &store->instr);
399 }
400
401 static inline void
402 nir_store_deref_var(nir_builder *build, nir_deref_var *deref,
403 nir_ssa_def *value, unsigned writemask)
404 {
405 const unsigned num_components =
406 glsl_get_vector_elements(nir_deref_tail(&deref->deref)->type);
407
408 nir_intrinsic_instr *store =
409 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
410 store->num_components = num_components;
411 store->const_index[0] = writemask & ((1 << num_components) - 1);
412 store->variables[0] = nir_deref_as_var(nir_copy_deref(store, &deref->deref));
413 store->src[0] = nir_src_for_ssa(value);
414 nir_builder_instr_insert(build, &store->instr);
415 }
416
417 static inline void
418 nir_copy_deref_var(nir_builder *build, nir_deref_var *dest, nir_deref_var *src)
419 {
420 assert(nir_deref_tail(&dest->deref)->type ==
421 nir_deref_tail(&src->deref)->type);
422
423 nir_intrinsic_instr *copy =
424 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
425 copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
426 copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
427 nir_builder_instr_insert(build, &copy->instr);
428 }
429
430 static inline void
431 nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
432 {
433 nir_intrinsic_instr *copy =
434 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
435 copy->variables[0] = nir_deref_var_create(copy, dest);
436 copy->variables[1] = nir_deref_var_create(copy, src);
437 nir_builder_instr_insert(build, &copy->instr);
438 }
439
440 static inline nir_ssa_def *
441 nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
442 {
443 nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
444 load->num_components = nir_intrinsic_infos[op].dest_components;
445 load->const_index[0] = index;
446 nir_ssa_dest_init(&load->instr, &load->dest,
447 nir_intrinsic_infos[op].dest_components, 32, NULL);
448 nir_builder_instr_insert(build, &load->instr);
449 return &load->dest.ssa;
450 }
451
452 static inline void
453 nir_jump(nir_builder *build, nir_jump_type jump_type)
454 {
455 nir_jump_instr *jump = nir_jump_instr_create(build->shader, jump_type);
456 nir_builder_instr_insert(build, &jump->instr);
457 }
458
459 #endif /* NIR_BUILDER_H */