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