1 /*
2 * Copyright © 2015 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
24 #include "nir.h"
25 #include "nir_control_flow.h"
26 #include "nir_xfb_info.h"
27
28 /* Secret Decoder Ring:
29 * clone_foo():
30 * Allocate and clone a foo.
31 * __clone_foo():
32 * Clone body of foo (ie. parent class, embedded struct, etc)
33 */
34
35 typedef struct {
36 /* True if we are cloning an entire shader. */
37 bool global_clone;
38
39 /* If true allows the clone operation to fall back to the original pointer
40 * if no clone pointer is found in the remap table. This allows us to
41 * clone a loop body without having to add srcs from outside the loop to
42 * the remap table. This is useful for loop unrolling.
43 */
44 bool allow_remap_fallback;
45
46 /* maps orig ptr -> cloned ptr: */
47 struct hash_table *remap_table;
48
49 /* List of phi sources. */
50 struct list_head phi_srcs;
51
52 /* new shader object, used as memctx for just about everything else: */
53 nir_shader *ns;
54 } clone_state;
55
56 static void
init_clone_state(clone_state * state,struct hash_table * remap_table,bool global,bool allow_remap_fallback)57 init_clone_state(clone_state *state, struct hash_table *remap_table,
58 bool global, bool allow_remap_fallback)
59 {
60 state->global_clone = global;
61 state->allow_remap_fallback = allow_remap_fallback;
62
63 if (remap_table) {
64 state->remap_table = remap_table;
65 } else {
66 state->remap_table = _mesa_pointer_hash_table_create(NULL);
67 }
68
69 list_inithead(&state->phi_srcs);
70 }
71
72 static void
free_clone_state(clone_state * state)73 free_clone_state(clone_state *state)
74 {
75 _mesa_hash_table_destroy(state->remap_table, NULL);
76 }
77
78 static inline void *
_lookup_ptr(clone_state * state,const void * ptr,bool global)79 _lookup_ptr(clone_state *state, const void *ptr, bool global)
80 {
81 struct hash_entry *entry;
82
83 if (!ptr)
84 return NULL;
85
86 if (!state->global_clone && global)
87 return (void *)ptr;
88
89 if (unlikely(!state->remap_table)) {
90 assert(state->allow_remap_fallback);
91 return (void *)ptr;
92 }
93
94 entry = _mesa_hash_table_search(state->remap_table, ptr);
95 if (!entry) {
96 assert(state->allow_remap_fallback);
97 return (void *)ptr;
98 }
99
100 return entry->data;
101 }
102
103 static void
add_remap(clone_state * state,void * nptr,const void * ptr)104 add_remap(clone_state *state, void *nptr, const void *ptr)
105 {
106 _mesa_hash_table_insert(state->remap_table, ptr, nptr);
107 }
108
109 static void *
remap_local(clone_state * state,const void * ptr)110 remap_local(clone_state *state, const void *ptr)
111 {
112 return _lookup_ptr(state, ptr, false);
113 }
114
115 static void *
remap_global(clone_state * state,const void * ptr)116 remap_global(clone_state *state, const void *ptr)
117 {
118 return _lookup_ptr(state, ptr, true);
119 }
120
121 static nir_variable *
remap_var(clone_state * state,const nir_variable * var)122 remap_var(clone_state *state, const nir_variable *var)
123 {
124 return _lookup_ptr(state, var, nir_variable_is_global(var));
125 }
126
127 nir_constant *
nir_constant_clone(const nir_constant * c,nir_variable * nvar)128 nir_constant_clone(const nir_constant *c, nir_variable *nvar)
129 {
130 nir_constant *nc = ralloc(nvar, nir_constant);
131
132 memcpy(nc->values, c->values, sizeof(nc->values));
133 nc->is_null_constant = c->is_null_constant;
134 nc->num_elements = c->num_elements;
135 nc->elements = ralloc_array(nvar, nir_constant *, c->num_elements);
136 for (unsigned i = 0; i < c->num_elements; i++) {
137 nc->elements[i] = nir_constant_clone(c->elements[i], nvar);
138 }
139
140 return nc;
141 }
142
143 /* NOTE: for cloning nir_variables, bypass nir_variable_create to avoid
144 * having to deal with locals and globals separately:
145 */
146 nir_variable *
nir_variable_clone(const nir_variable * var,nir_shader * shader)147 nir_variable_clone(const nir_variable *var, nir_shader *shader)
148 {
149 nir_variable *nvar = rzalloc(shader, nir_variable);
150
151 nvar->type = var->type;
152 nvar->name = ralloc_strdup(nvar, var->name);
153 nvar->data = var->data;
154 nvar->num_state_slots = var->num_state_slots;
155 if (var->num_state_slots) {
156 nvar->state_slots = ralloc_array(nvar, nir_state_slot, var->num_state_slots);
157 memcpy(nvar->state_slots, var->state_slots,
158 var->num_state_slots * sizeof(nir_state_slot));
159 }
160 if (var->constant_initializer) {
161 nvar->constant_initializer =
162 nir_constant_clone(var->constant_initializer, nvar);
163 }
164 nvar->interface_type = var->interface_type;
165
166 nvar->num_members = var->num_members;
167 if (var->num_members) {
168 nvar->members = ralloc_array(nvar, struct nir_variable_data,
169 var->num_members);
170 memcpy(nvar->members, var->members,
171 var->num_members * sizeof(*var->members));
172 }
173
174 return nvar;
175 }
176
177 static nir_variable *
clone_variable(clone_state * state,const nir_variable * var)178 clone_variable(clone_state *state, const nir_variable *var)
179 {
180 nir_variable *nvar = nir_variable_clone(var, state->ns);
181 add_remap(state, nvar, var);
182
183 return nvar;
184 }
185
186 /* clone list of nir_variable: */
187 static void
clone_var_list(clone_state * state,struct exec_list * dst,const struct exec_list * list)188 clone_var_list(clone_state *state, struct exec_list *dst,
189 const struct exec_list *list)
190 {
191 exec_list_make_empty(dst);
192 foreach_list_typed(nir_variable, var, node, list) {
193 nir_variable *nvar = clone_variable(state, var);
194 exec_list_push_tail(dst, &nvar->node);
195 }
196 }
197
198 static void
__clone_src(clone_state * state,void * ninstr_or_if,nir_src * nsrc,const nir_src * src)199 __clone_src(clone_state *state, void *ninstr_or_if,
200 nir_src *nsrc, const nir_src *src)
201 {
202 nsrc->ssa = remap_local(state, src->ssa);
203 }
204
205 static void
__clone_def(clone_state * state,nir_instr * ninstr,nir_def * ndef,const nir_def * def)206 __clone_def(clone_state *state, nir_instr *ninstr,
207 nir_def *ndef, const nir_def *def)
208 {
209 nir_def_init(ninstr, ndef, def->num_components, def->bit_size);
210 if (likely(state->remap_table))
211 add_remap(state, ndef, def);
212 }
213
214 static nir_alu_instr *
clone_alu(clone_state * state,const nir_alu_instr * alu)215 clone_alu(clone_state *state, const nir_alu_instr *alu)
216 {
217 nir_alu_instr *nalu = nir_alu_instr_create(state->ns, alu->op);
218 nalu->exact = alu->exact;
219 nalu->fp_fast_math = alu->fp_fast_math;
220 nalu->no_signed_wrap = alu->no_signed_wrap;
221 nalu->no_unsigned_wrap = alu->no_unsigned_wrap;
222
223 __clone_def(state, &nalu->instr, &nalu->def, &alu->def);
224
225 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
226 __clone_src(state, &nalu->instr, &nalu->src[i].src, &alu->src[i].src);
227 memcpy(nalu->src[i].swizzle, alu->src[i].swizzle,
228 sizeof(nalu->src[i].swizzle));
229 }
230
231 return nalu;
232 }
233
234 nir_alu_instr *
nir_alu_instr_clone(nir_shader * shader,const nir_alu_instr * orig)235 nir_alu_instr_clone(nir_shader *shader, const nir_alu_instr *orig)
236 {
237 clone_state state = {
238 .allow_remap_fallback = true,
239 .ns = shader,
240 };
241 return clone_alu(&state, orig);
242 }
243
244 static nir_deref_instr *
clone_deref_instr(clone_state * state,const nir_deref_instr * deref)245 clone_deref_instr(clone_state *state, const nir_deref_instr *deref)
246 {
247 nir_deref_instr *nderef =
248 nir_deref_instr_create(state->ns, deref->deref_type);
249
250 __clone_def(state, &nderef->instr, &nderef->def, &deref->def);
251
252 nderef->modes = deref->modes;
253 nderef->type = deref->type;
254
255 if (deref->deref_type == nir_deref_type_var) {
256 nderef->var = remap_var(state, deref->var);
257 return nderef;
258 }
259
260 __clone_src(state, &nderef->instr, &nderef->parent, &deref->parent);
261
262 switch (deref->deref_type) {
263 case nir_deref_type_struct:
264 nderef->strct.index = deref->strct.index;
265 break;
266
267 case nir_deref_type_array:
268 case nir_deref_type_ptr_as_array:
269 __clone_src(state, &nderef->instr,
270 &nderef->arr.index, &deref->arr.index);
271 nderef->arr.in_bounds = deref->arr.in_bounds;
272 break;
273
274 case nir_deref_type_array_wildcard:
275 /* Nothing to do */
276 break;
277
278 case nir_deref_type_cast:
279 nderef->cast.ptr_stride = deref->cast.ptr_stride;
280 nderef->cast.align_mul = deref->cast.align_mul;
281 nderef->cast.align_offset = deref->cast.align_offset;
282 break;
283
284 default:
285 unreachable("Invalid instruction deref type");
286 }
287
288 return nderef;
289 }
290
291 static nir_intrinsic_instr *
clone_intrinsic(clone_state * state,const nir_intrinsic_instr * itr)292 clone_intrinsic(clone_state *state, const nir_intrinsic_instr *itr)
293 {
294 nir_intrinsic_instr *nitr =
295 nir_intrinsic_instr_create(state->ns, itr->intrinsic);
296
297 unsigned num_srcs = nir_intrinsic_infos[itr->intrinsic].num_srcs;
298
299 if (nir_intrinsic_infos[itr->intrinsic].has_dest)
300 __clone_def(state, &nitr->instr, &nitr->def, &itr->def);
301
302 nitr->num_components = itr->num_components;
303 memcpy(nitr->const_index, itr->const_index, sizeof(nitr->const_index));
304 nitr->name = ralloc_strdup(state->ns, itr->name);
305
306 for (unsigned i = 0; i < num_srcs; i++)
307 __clone_src(state, &nitr->instr, &nitr->src[i], &itr->src[i]);
308
309 return nitr;
310 }
311
312 static nir_load_const_instr *
clone_load_const(clone_state * state,const nir_load_const_instr * lc)313 clone_load_const(clone_state *state, const nir_load_const_instr *lc)
314 {
315 nir_load_const_instr *nlc =
316 nir_load_const_instr_create(state->ns, lc->def.num_components,
317 lc->def.bit_size);
318
319 memcpy(&nlc->value, &lc->value, sizeof(*nlc->value) * lc->def.num_components);
320
321 add_remap(state, &nlc->def, &lc->def);
322
323 return nlc;
324 }
325
326 static nir_undef_instr *
clone_ssa_undef(clone_state * state,const nir_undef_instr * sa)327 clone_ssa_undef(clone_state *state, const nir_undef_instr *sa)
328 {
329 nir_undef_instr *nsa =
330 nir_undef_instr_create(state->ns, sa->def.num_components,
331 sa->def.bit_size);
332
333 add_remap(state, &nsa->def, &sa->def);
334
335 return nsa;
336 }
337
338 static nir_tex_instr *
clone_tex(clone_state * state,const nir_tex_instr * tex)339 clone_tex(clone_state *state, const nir_tex_instr *tex)
340 {
341 nir_tex_instr *ntex = nir_tex_instr_create(state->ns, tex->num_srcs);
342
343 ntex->sampler_dim = tex->sampler_dim;
344 ntex->dest_type = tex->dest_type;
345 ntex->op = tex->op;
346 __clone_def(state, &ntex->instr, &ntex->def, &tex->def);
347 for (unsigned i = 0; i < ntex->num_srcs; i++) {
348 ntex->src[i].src_type = tex->src[i].src_type;
349 __clone_src(state, &ntex->instr, &ntex->src[i].src, &tex->src[i].src);
350 }
351 ntex->coord_components = tex->coord_components;
352 ntex->is_array = tex->is_array;
353 ntex->array_is_lowered_cube = tex->array_is_lowered_cube;
354 ntex->is_shadow = tex->is_shadow;
355 ntex->is_new_style_shadow = tex->is_new_style_shadow;
356 ntex->is_sparse = tex->is_sparse;
357 ntex->component = tex->component;
358 memcpy(ntex->tg4_offsets, tex->tg4_offsets, sizeof(tex->tg4_offsets));
359
360 ntex->texture_index = tex->texture_index;
361 ntex->sampler_index = tex->sampler_index;
362
363 ntex->texture_non_uniform = tex->texture_non_uniform;
364 ntex->sampler_non_uniform = tex->sampler_non_uniform;
365
366 ntex->backend_flags = tex->backend_flags;
367
368 return ntex;
369 }
370
371 static nir_phi_instr *
clone_phi(clone_state * state,const nir_phi_instr * phi,nir_block * nblk)372 clone_phi(clone_state *state, const nir_phi_instr *phi, nir_block *nblk)
373 {
374 nir_phi_instr *nphi = nir_phi_instr_create(state->ns);
375
376 __clone_def(state, &nphi->instr, &nphi->def, &phi->def);
377
378 /* Cloning a phi node is a bit different from other instructions. The
379 * sources of phi instructions are the only time where we can use an SSA
380 * def before it is defined. In order to handle this, we just copy over
381 * the sources from the old phi instruction directly and then fix them up
382 * in a second pass once all the instrutions in the function have been
383 * properly cloned.
384 *
385 * In order to ensure that the copied sources (which are the same as the
386 * old phi instruction's sources for now) don't get inserted into the old
387 * shader's use-def lists, we have to add the phi instruction *before* we
388 * set up its sources.
389 */
390 nir_instr_insert_after_block(nblk, &nphi->instr);
391
392 nir_foreach_phi_src(src, phi) {
393 nir_phi_src *nsrc = nir_phi_instr_add_src(nphi, src->pred, src->src.ssa);
394
395 /* Stash it in the list of phi sources. We'll walk this list and fix up
396 * sources at the very end of clone_function_impl.
397 */
398 list_add(&nsrc->src.use_link, &state->phi_srcs);
399 }
400
401 return nphi;
402 }
403
404 static nir_jump_instr *
clone_jump(clone_state * state,const nir_jump_instr * jmp)405 clone_jump(clone_state *state, const nir_jump_instr *jmp)
406 {
407 /* These aren't handled because they require special block linking */
408 assert(jmp->type != nir_jump_goto && jmp->type != nir_jump_goto_if);
409
410 nir_jump_instr *njmp = nir_jump_instr_create(state->ns, jmp->type);
411
412 return njmp;
413 }
414
415 static nir_call_instr *
clone_call(clone_state * state,const nir_call_instr * call)416 clone_call(clone_state *state, const nir_call_instr *call)
417 {
418 nir_function *ncallee = remap_global(state, call->callee);
419 nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee);
420
421 for (unsigned i = 0; i < ncall->num_params; i++)
422 __clone_src(state, ncall, &ncall->params[i], &call->params[i]);
423
424 return ncall;
425 }
426
427 static nir_debug_info_instr *
clone_debug_info(clone_state * state,nir_debug_info_instr * di)428 clone_debug_info(clone_state *state, nir_debug_info_instr *di)
429 {
430 nir_debug_info_instr *instr =
431 nir_debug_info_instr_create(state->ns, di->type, di->string_length);
432
433 switch (di->type) {
434 case nir_debug_info_src_loc:
435 if (di->src_loc.line)
436 __clone_src(state, instr, &instr->src_loc.filename, &di->src_loc.filename);
437 instr->src_loc.line = di->src_loc.line;
438 instr->src_loc.column = di->src_loc.column;
439 instr->src_loc.spirv_offset = di->src_loc.spirv_offset;
440 instr->src_loc.source = di->src_loc.source;
441 return instr;
442 case nir_debug_info_string:
443 memcpy(instr->string, di->string, di->string_length);
444 __clone_def(state, &instr->instr, &instr->def, &di->def);
445 return instr;
446 }
447
448 unreachable("Unimplemented nir_debug_info_type");
449 }
450
451 static nir_instr *
clone_instr(clone_state * state,const nir_instr * instr)452 clone_instr(clone_state *state, const nir_instr *instr)
453 {
454 switch (instr->type) {
455 case nir_instr_type_alu:
456 return &clone_alu(state, nir_instr_as_alu(instr))->instr;
457 case nir_instr_type_deref:
458 return &clone_deref_instr(state, nir_instr_as_deref(instr))->instr;
459 case nir_instr_type_intrinsic:
460 return &clone_intrinsic(state, nir_instr_as_intrinsic(instr))->instr;
461 case nir_instr_type_load_const:
462 return &clone_load_const(state, nir_instr_as_load_const(instr))->instr;
463 case nir_instr_type_undef:
464 return &clone_ssa_undef(state, nir_instr_as_undef(instr))->instr;
465 case nir_instr_type_tex:
466 return &clone_tex(state, nir_instr_as_tex(instr))->instr;
467 case nir_instr_type_phi:
468 unreachable("Cannot clone phis with clone_instr");
469 case nir_instr_type_jump:
470 return &clone_jump(state, nir_instr_as_jump(instr))->instr;
471 case nir_instr_type_call:
472 return &clone_call(state, nir_instr_as_call(instr))->instr;
473 case nir_instr_type_debug_info:
474 return &clone_debug_info(state, nir_instr_as_debug_info(instr))->instr;
475 case nir_instr_type_parallel_copy:
476 unreachable("Cannot clone parallel copies");
477 default:
478 unreachable("bad instr type");
479 return NULL;
480 }
481 }
482
483 nir_instr *
nir_instr_clone(nir_shader * shader,const nir_instr * orig)484 nir_instr_clone(nir_shader *shader, const nir_instr *orig)
485 {
486 clone_state state = {
487 .allow_remap_fallback = true,
488 .ns = shader,
489 };
490 return clone_instr(&state, orig);
491 }
492
493 nir_instr *
nir_instr_clone_deep(nir_shader * shader,const nir_instr * orig,struct hash_table * remap_table)494 nir_instr_clone_deep(nir_shader *shader, const nir_instr *orig,
495 struct hash_table *remap_table)
496 {
497 clone_state state = {
498 .allow_remap_fallback = true,
499 .ns = shader,
500 .remap_table = remap_table,
501 };
502 return clone_instr(&state, orig);
503 }
504
505 static nir_block *
clone_block(clone_state * state,struct exec_list * cf_list,const nir_block * blk)506 clone_block(clone_state *state, struct exec_list *cf_list, const nir_block *blk)
507 {
508 /* Don't actually create a new block. Just use the one from the tail of
509 * the list. NIR guarantees that the tail of the list is a block and that
510 * no two blocks are side-by-side in the IR; It should be empty.
511 */
512 nir_block *nblk =
513 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
514 assert(nblk->cf_node.type == nir_cf_node_block);
515 assert(exec_list_is_empty(&nblk->instr_list));
516
517 /* We need this for phi sources */
518 add_remap(state, nblk, blk);
519
520 nir_foreach_instr(instr, blk) {
521 if (instr->type == nir_instr_type_phi) {
522 /* Phi instructions are a bit of a special case when cloning because
523 * we don't want inserting the instruction to automatically handle
524 * use/defs for us. Instead, we need to wait until all the
525 * blocks/instructions are in so that we can set their sources up.
526 */
527 clone_phi(state, nir_instr_as_phi(instr), nblk);
528 } else {
529 nir_instr *ninstr = clone_instr(state, instr);
530 nir_instr_insert_after_block(nblk, ninstr);
531 }
532 }
533
534 return nblk;
535 }
536
537 static void
538 clone_cf_list(clone_state *state, struct exec_list *dst,
539 const struct exec_list *list);
540
541 static nir_if *
clone_if(clone_state * state,struct exec_list * cf_list,const nir_if * i)542 clone_if(clone_state *state, struct exec_list *cf_list, const nir_if *i)
543 {
544 nir_if *ni = nir_if_create(state->ns);
545 ni->control = i->control;
546
547 __clone_src(state, ni, &ni->condition, &i->condition);
548
549 nir_cf_node_insert_end(cf_list, &ni->cf_node);
550
551 clone_cf_list(state, &ni->then_list, &i->then_list);
552 clone_cf_list(state, &ni->else_list, &i->else_list);
553
554 return ni;
555 }
556
557 static nir_loop *
clone_loop(clone_state * state,struct exec_list * cf_list,const nir_loop * loop)558 clone_loop(clone_state *state, struct exec_list *cf_list, const nir_loop *loop)
559 {
560 nir_loop *nloop = nir_loop_create(state->ns);
561 nloop->control = loop->control;
562 nloop->partially_unrolled = loop->partially_unrolled;
563
564 nir_cf_node_insert_end(cf_list, &nloop->cf_node);
565
566 clone_cf_list(state, &nloop->body, &loop->body);
567 if (nir_loop_has_continue_construct(loop)) {
568 nir_loop_add_continue_construct(nloop);
569 clone_cf_list(state, &nloop->continue_list, &loop->continue_list);
570 }
571
572 return nloop;
573 }
574
575 /* clone list of nir_cf_node: */
576 static void
clone_cf_list(clone_state * state,struct exec_list * dst,const struct exec_list * list)577 clone_cf_list(clone_state *state, struct exec_list *dst,
578 const struct exec_list *list)
579 {
580 foreach_list_typed(nir_cf_node, cf, node, list) {
581 switch (cf->type) {
582 case nir_cf_node_block:
583 clone_block(state, dst, nir_cf_node_as_block(cf));
584 break;
585 case nir_cf_node_if:
586 clone_if(state, dst, nir_cf_node_as_if(cf));
587 break;
588 case nir_cf_node_loop:
589 clone_loop(state, dst, nir_cf_node_as_loop(cf));
590 break;
591 default:
592 unreachable("bad cf type");
593 }
594 }
595 }
596
597 /* After we've cloned almost everything, we have to walk the list of phi
598 * sources and fix them up. Thanks to loops, the block and SSA value for a
599 * phi source may not be defined when we first encounter it. Instead, we
600 * add it to the phi_srcs list and we fix it up here.
601 */
602 static void
fixup_phi_srcs(clone_state * state)603 fixup_phi_srcs(clone_state *state)
604 {
605 list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
606 src->pred = remap_local(state, src->pred);
607
608 /* Remove from this list */
609 list_del(&src->src.use_link);
610
611 src->src.ssa = remap_local(state, src->src.ssa);
612 list_addtail(&src->src.use_link, &src->src.ssa->uses);
613 }
614 assert(list_is_empty(&state->phi_srcs));
615 }
616
617 void
nir_cf_list_clone(nir_cf_list * dst,nir_cf_list * src,nir_cf_node * parent,struct hash_table * remap_table)618 nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
619 struct hash_table *remap_table)
620 {
621 exec_list_make_empty(&dst->list);
622 dst->impl = src->impl;
623
624 if (exec_list_is_empty(&src->list))
625 return;
626
627 clone_state state;
628 init_clone_state(&state, remap_table, false, true);
629
630 /* We use the same shader */
631 state.ns = src->impl->function->shader;
632
633 /* The control-flow code assumes that the list of cf_nodes always starts
634 * and ends with a block. We start by adding an empty block.
635 */
636 nir_block *nblk = nir_block_create(state.ns);
637 nblk->cf_node.parent = parent;
638 exec_list_push_tail(&dst->list, &nblk->cf_node.node);
639
640 clone_cf_list(&state, &dst->list, &src->list);
641
642 fixup_phi_srcs(&state);
643
644 if (!remap_table)
645 free_clone_state(&state);
646 }
647
648 static nir_function_impl *
clone_function_impl(clone_state * state,const nir_function_impl * fi)649 clone_function_impl(clone_state *state, const nir_function_impl *fi)
650 {
651 nir_function_impl *nfi = nir_function_impl_create_bare(state->ns);
652
653 if (fi->preamble)
654 nfi->preamble = remap_global(state, fi->preamble);
655
656 clone_var_list(state, &nfi->locals, &fi->locals);
657
658 assert(list_is_empty(&state->phi_srcs));
659
660 clone_cf_list(state, &nfi->body, &fi->body);
661
662 fixup_phi_srcs(state);
663
664 /* All metadata is invalidated in the cloning process */
665 nfi->valid_metadata = 0;
666
667 return nfi;
668 }
669
670 nir_function_impl *
nir_function_impl_clone(nir_shader * shader,const nir_function_impl * fi)671 nir_function_impl_clone(nir_shader *shader, const nir_function_impl *fi)
672 {
673 clone_state state;
674 init_clone_state(&state, NULL, false, false);
675
676 state.ns = shader;
677
678 nir_function_impl *nfi = clone_function_impl(&state, fi);
679
680 free_clone_state(&state);
681
682 return nfi;
683 }
684
685 nir_function *
nir_function_clone(nir_shader * ns,const nir_function * fxn)686 nir_function_clone(nir_shader *ns, const nir_function *fxn)
687 {
688 nir_function *nfxn = nir_function_create(ns, fxn->name);
689 nfxn->num_params = fxn->num_params;
690 if (fxn->num_params) {
691 nfxn->params = ralloc_array(ns, nir_parameter, fxn->num_params);
692 memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params);
693 }
694 nfxn->is_entrypoint = fxn->is_entrypoint;
695 nfxn->is_preamble = fxn->is_preamble;
696 nfxn->should_inline = fxn->should_inline;
697 nfxn->dont_inline = fxn->dont_inline;
698 nfxn->is_subroutine = fxn->is_subroutine;
699 nfxn->num_subroutine_types = fxn->num_subroutine_types;
700 nfxn->subroutine_index = fxn->subroutine_index;
701 if (fxn->num_subroutine_types) {
702 nfxn->subroutine_types = ralloc_array(ns, const struct glsl_type *,
703 fxn->num_subroutine_types);
704 for (unsigned i = 0; i < fxn->num_subroutine_types; i++) {
705 nfxn->subroutine_types[i] = fxn->subroutine_types[i];
706 }
707 }
708
709 /* At first glance, it looks like we should clone the function_impl here.
710 * However, call instructions need to be able to reference at least the
711 * function and those will get processed as we clone the function_impls.
712 * We stop here and do function_impls as a second pass.
713 */
714 return nfxn;
715 }
716
717 static nir_function *
clone_function(clone_state * state,const nir_function * fxn,nir_shader * ns)718 clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
719 {
720 assert(ns == state->ns);
721
722 nir_function *nfxn = nir_function_clone(ns, fxn);
723 /* Needed for call instructions */
724 add_remap(state, nfxn, fxn);
725 return nfxn;
726 }
727
728 static u_printf_info *
clone_printf_info(void * mem_ctx,const nir_shader * s)729 clone_printf_info(void *mem_ctx, const nir_shader *s)
730 {
731 u_printf_info *infos = ralloc_array(mem_ctx, u_printf_info, s->printf_info_count);
732
733 for (unsigned i = 0; i < s->printf_info_count; i++) {
734 const u_printf_info *src_info = &s->printf_info[i];
735
736 infos[i].num_args = src_info->num_args;
737 infos[i].arg_sizes = ralloc_memdup(mem_ctx, src_info->arg_sizes,
738 sizeof(infos[i].arg_sizes[0]) * src_info->num_args);
739
740
741 infos[i].string_size = src_info->string_size;
742 infos[i].strings = ralloc_memdup(mem_ctx, src_info->strings,
743 src_info->string_size);
744 }
745
746 return infos;
747 }
748
749 nir_shader *
nir_shader_clone(void * mem_ctx,const nir_shader * s)750 nir_shader_clone(void *mem_ctx, const nir_shader *s)
751 {
752 clone_state state;
753 init_clone_state(&state, NULL, true, false);
754
755 nir_shader *ns = nir_shader_create(mem_ctx, s->info.stage, s->options, NULL);
756 state.ns = ns;
757
758 clone_var_list(&state, &ns->variables, &s->variables);
759
760 /* Go through and clone functions */
761 foreach_list_typed(nir_function, fxn, node, &s->functions)
762 clone_function(&state, fxn, ns);
763
764 /* Only after all functions are cloned can we clone the actual function
765 * implementations. This is because nir_call_instrs and preambles need to
766 * reference the functions of other functions and we don't know what order
767 * the functions will have in the list.
768 */
769 nir_foreach_function_with_impl(fxn, impl, s) {
770 nir_function *nfxn = remap_global(&state, fxn);
771 nir_function_set_impl(nfxn, clone_function_impl(&state, impl));
772 }
773
774 ns->info = s->info;
775 ns->info.name = ralloc_strdup(ns, ns->info.name);
776 if (ns->info.label)
777 ns->info.label = ralloc_strdup(ns, ns->info.label);
778
779 ns->num_inputs = s->num_inputs;
780 ns->num_uniforms = s->num_uniforms;
781 ns->num_outputs = s->num_outputs;
782 ns->scratch_size = s->scratch_size;
783
784 ns->constant_data_size = s->constant_data_size;
785 if (s->constant_data_size > 0) {
786 ns->constant_data = ralloc_memdup(ns, s->constant_data,
787 s->constant_data_size);
788 }
789
790 if (s->xfb_info) {
791 size_t size = nir_xfb_info_size(s->xfb_info->output_count);
792 ns->xfb_info = ralloc_memdup(ns, s->xfb_info, size);
793 }
794
795 if (s->printf_info_count > 0) {
796 ns->printf_info = clone_printf_info(ns, s);
797 ns->printf_info_count = s->printf_info_count;
798 }
799
800 free_clone_state(&state);
801
802 return ns;
803 }
804
805 /** Overwrites dst and replaces its contents with src
806 *
807 * Everything ralloc parented to dst and src itself (but not its children)
808 * will be freed.
809 *
810 * This should only be used by test code which needs to swap out shaders with
811 * a cloned or deserialized version.
812 */
813 void
nir_shader_replace(nir_shader * dst,nir_shader * src)814 nir_shader_replace(nir_shader *dst, nir_shader *src)
815 {
816 /* Delete all of dest's ralloc children */
817 void *dead_ctx = ralloc_context(NULL);
818 ralloc_adopt(dead_ctx, dst);
819 ralloc_free(dead_ctx);
820
821 /* Re-parent all of src's ralloc children to dst */
822 ralloc_adopt(dst, src);
823
824 memcpy(dst, src, sizeof(*dst));
825
826 /* We have to move all the linked lists over separately because we need the
827 * pointers in the list elements to point to the lists in dst and not src.
828 */
829 exec_list_move_nodes_to(&src->variables, &dst->variables);
830
831 /* Now move the functions over. This takes a tiny bit more work */
832 exec_list_move_nodes_to(&src->functions, &dst->functions);
833 nir_foreach_function(function, dst)
834 function->shader = dst;
835
836 ralloc_free(src);
837 }
838