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->no_signed_wrap = alu->no_signed_wrap;
220 nalu->no_unsigned_wrap = alu->no_unsigned_wrap;
221
222 __clone_def(state, &nalu->instr, &nalu->def, &alu->def);
223
224 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
225 __clone_src(state, &nalu->instr, &nalu->src[i].src, &alu->src[i].src);
226 memcpy(nalu->src[i].swizzle, alu->src[i].swizzle,
227 sizeof(nalu->src[i].swizzle));
228 }
229
230 return nalu;
231 }
232
233 nir_alu_instr *
nir_alu_instr_clone(nir_shader * shader,const nir_alu_instr * orig)234 nir_alu_instr_clone(nir_shader *shader, const nir_alu_instr *orig)
235 {
236 clone_state state = {
237 .allow_remap_fallback = true,
238 .ns = shader,
239 };
240 return clone_alu(&state, orig);
241 }
242
243 static nir_deref_instr *
clone_deref_instr(clone_state * state,const nir_deref_instr * deref)244 clone_deref_instr(clone_state *state, const nir_deref_instr *deref)
245 {
246 nir_deref_instr *nderef =
247 nir_deref_instr_create(state->ns, deref->deref_type);
248
249 __clone_def(state, &nderef->instr, &nderef->def, &deref->def);
250
251 nderef->modes = deref->modes;
252 nderef->type = deref->type;
253
254 if (deref->deref_type == nir_deref_type_var) {
255 nderef->var = remap_var(state, deref->var);
256 return nderef;
257 }
258
259 __clone_src(state, &nderef->instr, &nderef->parent, &deref->parent);
260
261 switch (deref->deref_type) {
262 case nir_deref_type_struct:
263 nderef->strct.index = deref->strct.index;
264 break;
265
266 case nir_deref_type_array:
267 case nir_deref_type_ptr_as_array:
268 __clone_src(state, &nderef->instr,
269 &nderef->arr.index, &deref->arr.index);
270 nderef->arr.in_bounds = deref->arr.in_bounds;
271 break;
272
273 case nir_deref_type_array_wildcard:
274 /* Nothing to do */
275 break;
276
277 case nir_deref_type_cast:
278 nderef->cast.ptr_stride = deref->cast.ptr_stride;
279 nderef->cast.align_mul = deref->cast.align_mul;
280 nderef->cast.align_offset = deref->cast.align_offset;
281 break;
282
283 default:
284 unreachable("Invalid instruction deref type");
285 }
286
287 return nderef;
288 }
289
290 static nir_intrinsic_instr *
clone_intrinsic(clone_state * state,const nir_intrinsic_instr * itr)291 clone_intrinsic(clone_state *state, const nir_intrinsic_instr *itr)
292 {
293 nir_intrinsic_instr *nitr =
294 nir_intrinsic_instr_create(state->ns, itr->intrinsic);
295
296 unsigned num_srcs = nir_intrinsic_infos[itr->intrinsic].num_srcs;
297
298 if (nir_intrinsic_infos[itr->intrinsic].has_dest)
299 __clone_def(state, &nitr->instr, &nitr->def, &itr->def);
300
301 nitr->num_components = itr->num_components;
302 memcpy(nitr->const_index, itr->const_index, sizeof(nitr->const_index));
303
304 for (unsigned i = 0; i < num_srcs; i++)
305 __clone_src(state, &nitr->instr, &nitr->src[i], &itr->src[i]);
306
307 return nitr;
308 }
309
310 static nir_load_const_instr *
clone_load_const(clone_state * state,const nir_load_const_instr * lc)311 clone_load_const(clone_state *state, const nir_load_const_instr *lc)
312 {
313 nir_load_const_instr *nlc =
314 nir_load_const_instr_create(state->ns, lc->def.num_components,
315 lc->def.bit_size);
316
317 memcpy(&nlc->value, &lc->value, sizeof(*nlc->value) * lc->def.num_components);
318
319 add_remap(state, &nlc->def, &lc->def);
320
321 return nlc;
322 }
323
324 static nir_undef_instr *
clone_ssa_undef(clone_state * state,const nir_undef_instr * sa)325 clone_ssa_undef(clone_state *state, const nir_undef_instr *sa)
326 {
327 nir_undef_instr *nsa =
328 nir_undef_instr_create(state->ns, sa->def.num_components,
329 sa->def.bit_size);
330
331 add_remap(state, &nsa->def, &sa->def);
332
333 return nsa;
334 }
335
336 static nir_tex_instr *
clone_tex(clone_state * state,const nir_tex_instr * tex)337 clone_tex(clone_state *state, const nir_tex_instr *tex)
338 {
339 nir_tex_instr *ntex = nir_tex_instr_create(state->ns, tex->num_srcs);
340
341 ntex->sampler_dim = tex->sampler_dim;
342 ntex->dest_type = tex->dest_type;
343 ntex->op = tex->op;
344 __clone_def(state, &ntex->instr, &ntex->def, &tex->def);
345 for (unsigned i = 0; i < ntex->num_srcs; i++) {
346 ntex->src[i].src_type = tex->src[i].src_type;
347 __clone_src(state, &ntex->instr, &ntex->src[i].src, &tex->src[i].src);
348 }
349 ntex->coord_components = tex->coord_components;
350 ntex->is_array = tex->is_array;
351 ntex->array_is_lowered_cube = tex->array_is_lowered_cube;
352 ntex->is_shadow = tex->is_shadow;
353 ntex->is_new_style_shadow = tex->is_new_style_shadow;
354 ntex->is_sparse = tex->is_sparse;
355 ntex->component = tex->component;
356 memcpy(ntex->tg4_offsets, tex->tg4_offsets, sizeof(tex->tg4_offsets));
357
358 ntex->texture_index = tex->texture_index;
359 ntex->sampler_index = tex->sampler_index;
360
361 ntex->texture_non_uniform = tex->texture_non_uniform;
362 ntex->sampler_non_uniform = tex->sampler_non_uniform;
363
364 ntex->backend_flags = tex->backend_flags;
365
366 return ntex;
367 }
368
369 static nir_phi_instr *
clone_phi(clone_state * state,const nir_phi_instr * phi,nir_block * nblk)370 clone_phi(clone_state *state, const nir_phi_instr *phi, nir_block *nblk)
371 {
372 nir_phi_instr *nphi = nir_phi_instr_create(state->ns);
373
374 __clone_def(state, &nphi->instr, &nphi->def, &phi->def);
375
376 /* Cloning a phi node is a bit different from other instructions. The
377 * sources of phi instructions are the only time where we can use an SSA
378 * def before it is defined. In order to handle this, we just copy over
379 * the sources from the old phi instruction directly and then fix them up
380 * in a second pass once all the instrutions in the function have been
381 * properly cloned.
382 *
383 * In order to ensure that the copied sources (which are the same as the
384 * old phi instruction's sources for now) don't get inserted into the old
385 * shader's use-def lists, we have to add the phi instruction *before* we
386 * set up its sources.
387 */
388 nir_instr_insert_after_block(nblk, &nphi->instr);
389
390 nir_foreach_phi_src(src, phi) {
391 nir_phi_src *nsrc = nir_phi_instr_add_src(nphi, src->pred, src->src.ssa);
392
393 /* Stash it in the list of phi sources. We'll walk this list and fix up
394 * sources at the very end of clone_function_impl.
395 */
396 list_add(&nsrc->src.use_link, &state->phi_srcs);
397 }
398
399 return nphi;
400 }
401
402 static nir_jump_instr *
clone_jump(clone_state * state,const nir_jump_instr * jmp)403 clone_jump(clone_state *state, const nir_jump_instr *jmp)
404 {
405 /* These aren't handled because they require special block linking */
406 assert(jmp->type != nir_jump_goto && jmp->type != nir_jump_goto_if);
407
408 nir_jump_instr *njmp = nir_jump_instr_create(state->ns, jmp->type);
409
410 return njmp;
411 }
412
413 static nir_call_instr *
clone_call(clone_state * state,const nir_call_instr * call)414 clone_call(clone_state *state, const nir_call_instr *call)
415 {
416 nir_function *ncallee = remap_global(state, call->callee);
417 nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee);
418
419 for (unsigned i = 0; i < ncall->num_params; i++)
420 __clone_src(state, ncall, &ncall->params[i], &call->params[i]);
421
422 return ncall;
423 }
424
425 static nir_instr *
clone_instr(clone_state * state,const nir_instr * instr)426 clone_instr(clone_state *state, const nir_instr *instr)
427 {
428 switch (instr->type) {
429 case nir_instr_type_alu:
430 return &clone_alu(state, nir_instr_as_alu(instr))->instr;
431 case nir_instr_type_deref:
432 return &clone_deref_instr(state, nir_instr_as_deref(instr))->instr;
433 case nir_instr_type_intrinsic:
434 return &clone_intrinsic(state, nir_instr_as_intrinsic(instr))->instr;
435 case nir_instr_type_load_const:
436 return &clone_load_const(state, nir_instr_as_load_const(instr))->instr;
437 case nir_instr_type_undef:
438 return &clone_ssa_undef(state, nir_instr_as_undef(instr))->instr;
439 case nir_instr_type_tex:
440 return &clone_tex(state, nir_instr_as_tex(instr))->instr;
441 case nir_instr_type_phi:
442 unreachable("Cannot clone phis with clone_instr");
443 case nir_instr_type_jump:
444 return &clone_jump(state, nir_instr_as_jump(instr))->instr;
445 case nir_instr_type_call:
446 return &clone_call(state, nir_instr_as_call(instr))->instr;
447 case nir_instr_type_parallel_copy:
448 unreachable("Cannot clone parallel copies");
449 default:
450 unreachable("bad instr type");
451 return NULL;
452 }
453 }
454
455 nir_instr *
nir_instr_clone(nir_shader * shader,const nir_instr * orig)456 nir_instr_clone(nir_shader *shader, const nir_instr *orig)
457 {
458 clone_state state = {
459 .allow_remap_fallback = true,
460 .ns = shader,
461 };
462 return clone_instr(&state, orig);
463 }
464
465 nir_instr *
nir_instr_clone_deep(nir_shader * shader,const nir_instr * orig,struct hash_table * remap_table)466 nir_instr_clone_deep(nir_shader *shader, const nir_instr *orig,
467 struct hash_table *remap_table)
468 {
469 clone_state state = {
470 .allow_remap_fallback = true,
471 .ns = shader,
472 .remap_table = remap_table,
473 };
474 return clone_instr(&state, orig);
475 }
476
477 static nir_block *
clone_block(clone_state * state,struct exec_list * cf_list,const nir_block * blk)478 clone_block(clone_state *state, struct exec_list *cf_list, const nir_block *blk)
479 {
480 /* Don't actually create a new block. Just use the one from the tail of
481 * the list. NIR guarantees that the tail of the list is a block and that
482 * no two blocks are side-by-side in the IR; It should be empty.
483 */
484 nir_block *nblk =
485 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
486 assert(nblk->cf_node.type == nir_cf_node_block);
487 assert(exec_list_is_empty(&nblk->instr_list));
488
489 /* We need this for phi sources */
490 add_remap(state, nblk, blk);
491
492 nir_foreach_instr(instr, blk) {
493 if (instr->type == nir_instr_type_phi) {
494 /* Phi instructions are a bit of a special case when cloning because
495 * we don't want inserting the instruction to automatically handle
496 * use/defs for us. Instead, we need to wait until all the
497 * blocks/instructions are in so that we can set their sources up.
498 */
499 clone_phi(state, nir_instr_as_phi(instr), nblk);
500 } else {
501 nir_instr *ninstr = clone_instr(state, instr);
502 nir_instr_insert_after_block(nblk, ninstr);
503 }
504 }
505
506 return nblk;
507 }
508
509 static void
510 clone_cf_list(clone_state *state, struct exec_list *dst,
511 const struct exec_list *list);
512
513 static nir_if *
clone_if(clone_state * state,struct exec_list * cf_list,const nir_if * i)514 clone_if(clone_state *state, struct exec_list *cf_list, const nir_if *i)
515 {
516 nir_if *ni = nir_if_create(state->ns);
517 ni->control = i->control;
518
519 __clone_src(state, ni, &ni->condition, &i->condition);
520
521 nir_cf_node_insert_end(cf_list, &ni->cf_node);
522
523 clone_cf_list(state, &ni->then_list, &i->then_list);
524 clone_cf_list(state, &ni->else_list, &i->else_list);
525
526 return ni;
527 }
528
529 static nir_loop *
clone_loop(clone_state * state,struct exec_list * cf_list,const nir_loop * loop)530 clone_loop(clone_state *state, struct exec_list *cf_list, const nir_loop *loop)
531 {
532 nir_loop *nloop = nir_loop_create(state->ns);
533 nloop->control = loop->control;
534 nloop->partially_unrolled = loop->partially_unrolled;
535
536 nir_cf_node_insert_end(cf_list, &nloop->cf_node);
537
538 clone_cf_list(state, &nloop->body, &loop->body);
539 if (nir_loop_has_continue_construct(loop)) {
540 nir_loop_add_continue_construct(nloop);
541 clone_cf_list(state, &nloop->continue_list, &loop->continue_list);
542 }
543
544 return nloop;
545 }
546
547 /* clone list of nir_cf_node: */
548 static void
clone_cf_list(clone_state * state,struct exec_list * dst,const struct exec_list * list)549 clone_cf_list(clone_state *state, struct exec_list *dst,
550 const struct exec_list *list)
551 {
552 foreach_list_typed(nir_cf_node, cf, node, list) {
553 switch (cf->type) {
554 case nir_cf_node_block:
555 clone_block(state, dst, nir_cf_node_as_block(cf));
556 break;
557 case nir_cf_node_if:
558 clone_if(state, dst, nir_cf_node_as_if(cf));
559 break;
560 case nir_cf_node_loop:
561 clone_loop(state, dst, nir_cf_node_as_loop(cf));
562 break;
563 default:
564 unreachable("bad cf type");
565 }
566 }
567 }
568
569 /* After we've cloned almost everything, we have to walk the list of phi
570 * sources and fix them up. Thanks to loops, the block and SSA value for a
571 * phi source may not be defined when we first encounter it. Instead, we
572 * add it to the phi_srcs list and we fix it up here.
573 */
574 static void
fixup_phi_srcs(clone_state * state)575 fixup_phi_srcs(clone_state *state)
576 {
577 list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
578 src->pred = remap_local(state, src->pred);
579
580 /* Remove from this list */
581 list_del(&src->src.use_link);
582
583 src->src.ssa = remap_local(state, src->src.ssa);
584 list_addtail(&src->src.use_link, &src->src.ssa->uses);
585 }
586 assert(list_is_empty(&state->phi_srcs));
587 }
588
589 void
nir_cf_list_clone(nir_cf_list * dst,nir_cf_list * src,nir_cf_node * parent,struct hash_table * remap_table)590 nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
591 struct hash_table *remap_table)
592 {
593 exec_list_make_empty(&dst->list);
594 dst->impl = src->impl;
595
596 if (exec_list_is_empty(&src->list))
597 return;
598
599 clone_state state;
600 init_clone_state(&state, remap_table, false, true);
601
602 /* We use the same shader */
603 state.ns = src->impl->function->shader;
604
605 /* The control-flow code assumes that the list of cf_nodes always starts
606 * and ends with a block. We start by adding an empty block.
607 */
608 nir_block *nblk = nir_block_create(state.ns);
609 nblk->cf_node.parent = parent;
610 exec_list_push_tail(&dst->list, &nblk->cf_node.node);
611
612 clone_cf_list(&state, &dst->list, &src->list);
613
614 fixup_phi_srcs(&state);
615
616 if (!remap_table)
617 free_clone_state(&state);
618 }
619
620 static nir_function_impl *
clone_function_impl(clone_state * state,const nir_function_impl * fi)621 clone_function_impl(clone_state *state, const nir_function_impl *fi)
622 {
623 nir_function_impl *nfi = nir_function_impl_create_bare(state->ns);
624
625 if (fi->preamble)
626 nfi->preamble = remap_global(state, fi->preamble);
627
628 clone_var_list(state, &nfi->locals, &fi->locals);
629
630 assert(list_is_empty(&state->phi_srcs));
631
632 clone_cf_list(state, &nfi->body, &fi->body);
633
634 fixup_phi_srcs(state);
635
636 /* All metadata is invalidated in the cloning process */
637 nfi->valid_metadata = 0;
638
639 return nfi;
640 }
641
642 nir_function_impl *
nir_function_impl_clone(nir_shader * shader,const nir_function_impl * fi)643 nir_function_impl_clone(nir_shader *shader, const nir_function_impl *fi)
644 {
645 clone_state state;
646 init_clone_state(&state, NULL, false, false);
647
648 state.ns = shader;
649
650 nir_function_impl *nfi = clone_function_impl(&state, fi);
651
652 free_clone_state(&state);
653
654 return nfi;
655 }
656
657 nir_function *
nir_function_clone(nir_shader * ns,const nir_function * fxn)658 nir_function_clone(nir_shader *ns, const nir_function *fxn)
659 {
660 nir_function *nfxn = nir_function_create(ns, fxn->name);
661 nfxn->num_params = fxn->num_params;
662 if (fxn->num_params) {
663 nfxn->params = ralloc_array(ns, nir_parameter, fxn->num_params);
664 memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params);
665 }
666 nfxn->is_entrypoint = fxn->is_entrypoint;
667 nfxn->is_preamble = fxn->is_preamble;
668 nfxn->should_inline = fxn->should_inline;
669 nfxn->dont_inline = fxn->dont_inline;
670
671 /* At first glance, it looks like we should clone the function_impl here.
672 * However, call instructions need to be able to reference at least the
673 * function and those will get processed as we clone the function_impls.
674 * We stop here and do function_impls as a second pass.
675 */
676 return nfxn;
677 }
678
679 static nir_function *
clone_function(clone_state * state,const nir_function * fxn,nir_shader * ns)680 clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
681 {
682 assert(ns == state->ns);
683
684 nir_function *nfxn = nir_function_clone(ns, fxn);
685 /* Needed for call instructions */
686 add_remap(state, nfxn, fxn);
687 return nfxn;
688 }
689
690 static u_printf_info *
clone_printf_info(void * mem_ctx,const nir_shader * s)691 clone_printf_info(void *mem_ctx, const nir_shader *s)
692 {
693 u_printf_info *infos = ralloc_array(mem_ctx, u_printf_info, s->printf_info_count);
694
695 for (unsigned i = 0; i < s->printf_info_count; i++) {
696 const u_printf_info *src_info = &s->printf_info[i];
697
698 infos[i].num_args = src_info->num_args;
699 infos[i].arg_sizes = ralloc_memdup(mem_ctx, src_info->arg_sizes,
700 sizeof(infos[i].arg_sizes[0]) * src_info->num_args);
701
702
703 infos[i].string_size = src_info->string_size;
704 infos[i].strings = ralloc_memdup(mem_ctx, src_info->strings,
705 src_info->string_size);
706 }
707
708 return infos;
709 }
710
711 nir_shader *
nir_shader_clone(void * mem_ctx,const nir_shader * s)712 nir_shader_clone(void *mem_ctx, const nir_shader *s)
713 {
714 clone_state state;
715 init_clone_state(&state, NULL, true, false);
716
717 nir_shader *ns = nir_shader_create(mem_ctx, s->info.stage, s->options, NULL);
718 state.ns = ns;
719
720 clone_var_list(&state, &ns->variables, &s->variables);
721
722 /* Go through and clone functions */
723 foreach_list_typed(nir_function, fxn, node, &s->functions)
724 clone_function(&state, fxn, ns);
725
726 /* Only after all functions are cloned can we clone the actual function
727 * implementations. This is because nir_call_instrs and preambles need to
728 * reference the functions of other functions and we don't know what order
729 * the functions will have in the list.
730 */
731 nir_foreach_function_with_impl(fxn, impl, s) {
732 nir_function *nfxn = remap_global(&state, fxn);
733 nir_function_set_impl(nfxn, clone_function_impl(&state, impl));
734 }
735
736 ns->info = s->info;
737 ns->info.name = ralloc_strdup(ns, ns->info.name);
738 if (ns->info.label)
739 ns->info.label = ralloc_strdup(ns, ns->info.label);
740
741 ns->num_inputs = s->num_inputs;
742 ns->num_uniforms = s->num_uniforms;
743 ns->num_outputs = s->num_outputs;
744 ns->scratch_size = s->scratch_size;
745
746 ns->constant_data_size = s->constant_data_size;
747 if (s->constant_data_size > 0) {
748 ns->constant_data = ralloc_memdup(ns, s->constant_data,
749 s->constant_data_size);
750 }
751
752 if (s->xfb_info) {
753 size_t size = nir_xfb_info_size(s->xfb_info->output_count);
754 ns->xfb_info = ralloc_memdup(ns, s->xfb_info, size);
755 }
756
757 if (s->printf_info_count > 0) {
758 ns->printf_info = clone_printf_info(ns, s);
759 ns->printf_info_count = s->printf_info_count;
760 }
761
762 free_clone_state(&state);
763
764 return ns;
765 }
766
767 /** Overwrites dst and replaces its contents with src
768 *
769 * Everything ralloc parented to dst and src itself (but not its children)
770 * will be freed.
771 *
772 * This should only be used by test code which needs to swap out shaders with
773 * a cloned or deserialized version.
774 */
775 void
nir_shader_replace(nir_shader * dst,nir_shader * src)776 nir_shader_replace(nir_shader *dst, nir_shader *src)
777 {
778 /* Delete all of dest's ralloc children */
779 void *dead_ctx = ralloc_context(NULL);
780 ralloc_adopt(dead_ctx, dst);
781 ralloc_free(dead_ctx);
782
783 /* Re-parent all of src's ralloc children to dst */
784 ralloc_adopt(dst, src);
785
786 memcpy(dst, src, sizeof(*dst));
787
788 /* We have to move all the linked lists over separately because we need the
789 * pointers in the list elements to point to the lists in dst and not src.
790 */
791 exec_list_move_nodes_to(&src->variables, &dst->variables);
792
793 /* Now move the functions over. This takes a tiny bit more work */
794 exec_list_move_nodes_to(&src->functions, &dst->functions);
795 nir_foreach_function(function, dst)
796 function->shader = dst;
797
798 ralloc_free(src);
799 }
800