nir: Export deref comparison functions
[mesa.git] / src / compiler / nir / nir_clone.c
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
27 /* Secret Decoder Ring:
28 * clone_foo():
29 * Allocate and clone a foo.
30 * __clone_foo():
31 * Clone body of foo (ie. parent class, embedded struct, etc)
32 */
33
34 typedef struct {
35 /* True if we are cloning an entire shader. */
36 bool global_clone;
37
38 /* If true allows the clone operation to fall back to the original pointer
39 * if no clone pointer is found in the remap table. This allows us to
40 * clone a loop body without having to add srcs from outside the loop to
41 * the remap table. This is useful for loop unrolling.
42 */
43 bool allow_remap_fallback;
44
45 /* maps orig ptr -> cloned ptr: */
46 struct hash_table *remap_table;
47
48 /* List of phi sources. */
49 struct list_head phi_srcs;
50
51 /* new shader object, used as memctx for just about everything else: */
52 nir_shader *ns;
53 } clone_state;
54
55 static void
56 init_clone_state(clone_state *state, struct hash_table *remap_table,
57 bool global, bool allow_remap_fallback)
58 {
59 state->global_clone = global;
60 state->allow_remap_fallback = allow_remap_fallback;
61
62 if (remap_table) {
63 state->remap_table = remap_table;
64 } else {
65 state->remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
66 _mesa_key_pointer_equal);
67 }
68
69 list_inithead(&state->phi_srcs);
70 }
71
72 static void
73 free_clone_state(clone_state *state)
74 {
75 _mesa_hash_table_destroy(state->remap_table, NULL);
76 }
77
78 static inline void *
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 entry = _mesa_hash_table_search(state->remap_table, ptr);
90 if (!entry) {
91 assert(state->allow_remap_fallback);
92 return (void *)ptr;
93 }
94
95 return entry->data;
96 }
97
98 static void
99 add_remap(clone_state *state, void *nptr, const void *ptr)
100 {
101 _mesa_hash_table_insert(state->remap_table, ptr, nptr);
102 }
103
104 static void *
105 remap_local(clone_state *state, const void *ptr)
106 {
107 return _lookup_ptr(state, ptr, false);
108 }
109
110 static void *
111 remap_global(clone_state *state, const void *ptr)
112 {
113 return _lookup_ptr(state, ptr, true);
114 }
115
116 static nir_register *
117 remap_reg(clone_state *state, const nir_register *reg)
118 {
119 return _lookup_ptr(state, reg, reg->is_global);
120 }
121
122 static nir_variable *
123 remap_var(clone_state *state, const nir_variable *var)
124 {
125 return _lookup_ptr(state, var, nir_variable_is_global(var));
126 }
127
128 nir_constant *
129 nir_constant_clone(const nir_constant *c, nir_variable *nvar)
130 {
131 nir_constant *nc = ralloc(nvar, nir_constant);
132
133 memcpy(nc->values, c->values, sizeof(nc->values));
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 *
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 nvar->state_slots = ralloc_array(nvar, nir_state_slot, var->num_state_slots);
156 memcpy(nvar->state_slots, var->state_slots,
157 var->num_state_slots * sizeof(nir_state_slot));
158 if (var->constant_initializer) {
159 nvar->constant_initializer =
160 nir_constant_clone(var->constant_initializer, nvar);
161 }
162 nvar->interface_type = var->interface_type;
163
164 nvar->num_members = var->num_members;
165 if (var->num_members) {
166 nvar->members = ralloc_array(nvar, struct nir_variable_data,
167 var->num_members);
168 memcpy(nvar->members, var->members,
169 var->num_members * sizeof(*var->members));
170 }
171
172 return nvar;
173 }
174
175 static nir_variable *
176 clone_variable(clone_state *state, const nir_variable *var)
177 {
178 nir_variable *nvar = nir_variable_clone(var, state->ns);
179 add_remap(state, nvar, var);
180
181 return nvar;
182 }
183
184 /* clone list of nir_variable: */
185 static void
186 clone_var_list(clone_state *state, struct exec_list *dst,
187 const struct exec_list *list)
188 {
189 exec_list_make_empty(dst);
190 foreach_list_typed(nir_variable, var, node, list) {
191 nir_variable *nvar = clone_variable(state, var);
192 exec_list_push_tail(dst, &nvar->node);
193 }
194 }
195
196 /* NOTE: for cloning nir_registers, bypass nir_global/local_reg_create()
197 * to avoid having to deal with locals and globals separately:
198 */
199 static nir_register *
200 clone_register(clone_state *state, const nir_register *reg)
201 {
202 nir_register *nreg = rzalloc(state->ns, nir_register);
203 add_remap(state, nreg, reg);
204
205 nreg->num_components = reg->num_components;
206 nreg->bit_size = reg->bit_size;
207 nreg->num_array_elems = reg->num_array_elems;
208 nreg->index = reg->index;
209 nreg->name = ralloc_strdup(nreg, reg->name);
210 nreg->is_global = reg->is_global;
211 nreg->is_packed = reg->is_packed;
212
213 /* reconstructing uses/defs/if_uses handled by nir_instr_insert() */
214 list_inithead(&nreg->uses);
215 list_inithead(&nreg->defs);
216 list_inithead(&nreg->if_uses);
217
218 return nreg;
219 }
220
221 /* clone list of nir_register: */
222 static void
223 clone_reg_list(clone_state *state, struct exec_list *dst,
224 const struct exec_list *list)
225 {
226 exec_list_make_empty(dst);
227 foreach_list_typed(nir_register, reg, node, list) {
228 nir_register *nreg = clone_register(state, reg);
229 exec_list_push_tail(dst, &nreg->node);
230 }
231 }
232
233 static void
234 __clone_src(clone_state *state, void *ninstr_or_if,
235 nir_src *nsrc, const nir_src *src)
236 {
237 nsrc->is_ssa = src->is_ssa;
238 if (src->is_ssa) {
239 nsrc->ssa = remap_local(state, src->ssa);
240 } else {
241 nsrc->reg.reg = remap_reg(state, src->reg.reg);
242 if (src->reg.indirect) {
243 nsrc->reg.indirect = ralloc(ninstr_or_if, nir_src);
244 __clone_src(state, ninstr_or_if, nsrc->reg.indirect, src->reg.indirect);
245 }
246 nsrc->reg.base_offset = src->reg.base_offset;
247 }
248 }
249
250 static void
251 __clone_dst(clone_state *state, nir_instr *ninstr,
252 nir_dest *ndst, const nir_dest *dst)
253 {
254 ndst->is_ssa = dst->is_ssa;
255 if (dst->is_ssa) {
256 nir_ssa_dest_init(ninstr, ndst, dst->ssa.num_components,
257 dst->ssa.bit_size, dst->ssa.name);
258 add_remap(state, &ndst->ssa, &dst->ssa);
259 } else {
260 ndst->reg.reg = remap_reg(state, dst->reg.reg);
261 if (dst->reg.indirect) {
262 ndst->reg.indirect = ralloc(ninstr, nir_src);
263 __clone_src(state, ninstr, ndst->reg.indirect, dst->reg.indirect);
264 }
265 ndst->reg.base_offset = dst->reg.base_offset;
266 }
267 }
268
269 static nir_alu_instr *
270 clone_alu(clone_state *state, const nir_alu_instr *alu)
271 {
272 nir_alu_instr *nalu = nir_alu_instr_create(state->ns, alu->op);
273 nalu->exact = alu->exact;
274
275 __clone_dst(state, &nalu->instr, &nalu->dest.dest, &alu->dest.dest);
276 nalu->dest.saturate = alu->dest.saturate;
277 nalu->dest.write_mask = alu->dest.write_mask;
278
279 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
280 __clone_src(state, &nalu->instr, &nalu->src[i].src, &alu->src[i].src);
281 nalu->src[i].negate = alu->src[i].negate;
282 nalu->src[i].abs = alu->src[i].abs;
283 memcpy(nalu->src[i].swizzle, alu->src[i].swizzle,
284 sizeof(nalu->src[i].swizzle));
285 }
286
287 return nalu;
288 }
289
290 static nir_deref_instr *
291 clone_deref_instr(clone_state *state, const nir_deref_instr *deref)
292 {
293 nir_deref_instr *nderef =
294 nir_deref_instr_create(state->ns, deref->deref_type);
295
296 __clone_dst(state, &nderef->instr, &nderef->dest, &deref->dest);
297
298 nderef->mode = deref->mode;
299 nderef->type = deref->type;
300
301 if (deref->deref_type == nir_deref_type_var) {
302 nderef->var = remap_var(state, deref->var);
303 return nderef;
304 }
305
306 __clone_src(state, &nderef->instr, &nderef->parent, &deref->parent);
307
308 switch (deref->deref_type) {
309 case nir_deref_type_struct:
310 nderef->strct.index = deref->strct.index;
311 break;
312
313 case nir_deref_type_array:
314 __clone_src(state, &nderef->instr,
315 &nderef->arr.index, &deref->arr.index);
316 break;
317
318 case nir_deref_type_array_wildcard:
319 case nir_deref_type_cast:
320 /* Nothing to do */
321 break;
322
323 default:
324 unreachable("Invalid instruction deref type");
325 }
326
327 return nderef;
328 }
329
330 static nir_intrinsic_instr *
331 clone_intrinsic(clone_state *state, const nir_intrinsic_instr *itr)
332 {
333 nir_intrinsic_instr *nitr =
334 nir_intrinsic_instr_create(state->ns, itr->intrinsic);
335
336 unsigned num_srcs = nir_intrinsic_infos[itr->intrinsic].num_srcs;
337
338 if (nir_intrinsic_infos[itr->intrinsic].has_dest)
339 __clone_dst(state, &nitr->instr, &nitr->dest, &itr->dest);
340
341 nitr->num_components = itr->num_components;
342 memcpy(nitr->const_index, itr->const_index, sizeof(nitr->const_index));
343
344 for (unsigned i = 0; i < num_srcs; i++)
345 __clone_src(state, &nitr->instr, &nitr->src[i], &itr->src[i]);
346
347 return nitr;
348 }
349
350 static nir_load_const_instr *
351 clone_load_const(clone_state *state, const nir_load_const_instr *lc)
352 {
353 nir_load_const_instr *nlc =
354 nir_load_const_instr_create(state->ns, lc->def.num_components,
355 lc->def.bit_size);
356
357 memcpy(&nlc->value, &lc->value, sizeof(nlc->value));
358
359 add_remap(state, &nlc->def, &lc->def);
360
361 return nlc;
362 }
363
364 static nir_ssa_undef_instr *
365 clone_ssa_undef(clone_state *state, const nir_ssa_undef_instr *sa)
366 {
367 nir_ssa_undef_instr *nsa =
368 nir_ssa_undef_instr_create(state->ns, sa->def.num_components,
369 sa->def.bit_size);
370
371 add_remap(state, &nsa->def, &sa->def);
372
373 return nsa;
374 }
375
376 static nir_tex_instr *
377 clone_tex(clone_state *state, const nir_tex_instr *tex)
378 {
379 nir_tex_instr *ntex = nir_tex_instr_create(state->ns, tex->num_srcs);
380
381 ntex->sampler_dim = tex->sampler_dim;
382 ntex->dest_type = tex->dest_type;
383 ntex->op = tex->op;
384 __clone_dst(state, &ntex->instr, &ntex->dest, &tex->dest);
385 for (unsigned i = 0; i < ntex->num_srcs; i++) {
386 ntex->src[i].src_type = tex->src[i].src_type;
387 __clone_src(state, &ntex->instr, &ntex->src[i].src, &tex->src[i].src);
388 }
389 ntex->coord_components = tex->coord_components;
390 ntex->is_array = tex->is_array;
391 ntex->is_shadow = tex->is_shadow;
392 ntex->is_new_style_shadow = tex->is_new_style_shadow;
393 ntex->component = tex->component;
394
395 ntex->texture_index = tex->texture_index;
396 ntex->texture_array_size = tex->texture_array_size;
397 ntex->sampler_index = tex->sampler_index;
398
399 return ntex;
400 }
401
402 static nir_phi_instr *
403 clone_phi(clone_state *state, const nir_phi_instr *phi, nir_block *nblk)
404 {
405 nir_phi_instr *nphi = nir_phi_instr_create(state->ns);
406
407 __clone_dst(state, &nphi->instr, &nphi->dest, &phi->dest);
408
409 /* Cloning a phi node is a bit different from other instructions. The
410 * sources of phi instructions are the only time where we can use an SSA
411 * def before it is defined. In order to handle this, we just copy over
412 * the sources from the old phi instruction directly and then fix them up
413 * in a second pass once all the instrutions in the function have been
414 * properly cloned.
415 *
416 * In order to ensure that the copied sources (which are the same as the
417 * old phi instruction's sources for now) don't get inserted into the old
418 * shader's use-def lists, we have to add the phi instruction *before* we
419 * set up its sources.
420 */
421 nir_instr_insert_after_block(nblk, &nphi->instr);
422
423 foreach_list_typed(nir_phi_src, src, node, &phi->srcs) {
424 nir_phi_src *nsrc = ralloc(nphi, nir_phi_src);
425
426 /* Just copy the old source for now. */
427 memcpy(nsrc, src, sizeof(*src));
428
429 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
430 * we have to set the parent_instr manually. It doesn't really matter
431 * when we do it, so we might as well do it here.
432 */
433 nsrc->src.parent_instr = &nphi->instr;
434
435 /* Stash it in the list of phi sources. We'll walk this list and fix up
436 * sources at the very end of clone_function_impl.
437 */
438 list_add(&nsrc->src.use_link, &state->phi_srcs);
439
440 exec_list_push_tail(&nphi->srcs, &nsrc->node);
441 }
442
443 return nphi;
444 }
445
446 static nir_jump_instr *
447 clone_jump(clone_state *state, const nir_jump_instr *jmp)
448 {
449 nir_jump_instr *njmp = nir_jump_instr_create(state->ns, jmp->type);
450
451 return njmp;
452 }
453
454 static nir_call_instr *
455 clone_call(clone_state *state, const nir_call_instr *call)
456 {
457 nir_function *ncallee = remap_global(state, call->callee);
458 nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee);
459
460 for (unsigned i = 0; i < ncall->num_params; i++)
461 __clone_src(state, ncall, &ncall->params[i], &call->params[i]);
462
463 return ncall;
464 }
465
466 static nir_instr *
467 clone_instr(clone_state *state, const nir_instr *instr)
468 {
469 switch (instr->type) {
470 case nir_instr_type_alu:
471 return &clone_alu(state, nir_instr_as_alu(instr))->instr;
472 case nir_instr_type_deref:
473 return &clone_deref_instr(state, nir_instr_as_deref(instr))->instr;
474 case nir_instr_type_intrinsic:
475 return &clone_intrinsic(state, nir_instr_as_intrinsic(instr))->instr;
476 case nir_instr_type_load_const:
477 return &clone_load_const(state, nir_instr_as_load_const(instr))->instr;
478 case nir_instr_type_ssa_undef:
479 return &clone_ssa_undef(state, nir_instr_as_ssa_undef(instr))->instr;
480 case nir_instr_type_tex:
481 return &clone_tex(state, nir_instr_as_tex(instr))->instr;
482 case nir_instr_type_phi:
483 unreachable("Cannot clone phis with clone_instr");
484 case nir_instr_type_jump:
485 return &clone_jump(state, nir_instr_as_jump(instr))->instr;
486 case nir_instr_type_call:
487 return &clone_call(state, nir_instr_as_call(instr))->instr;
488 case nir_instr_type_parallel_copy:
489 unreachable("Cannot clone parallel copies");
490 default:
491 unreachable("bad instr type");
492 return NULL;
493 }
494 }
495
496 static nir_block *
497 clone_block(clone_state *state, struct exec_list *cf_list, const nir_block *blk)
498 {
499 /* Don't actually create a new block. Just use the one from the tail of
500 * the list. NIR guarantees that the tail of the list is a block and that
501 * no two blocks are side-by-side in the IR; It should be empty.
502 */
503 nir_block *nblk =
504 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
505 assert(nblk->cf_node.type == nir_cf_node_block);
506 assert(exec_list_is_empty(&nblk->instr_list));
507
508 /* We need this for phi sources */
509 add_remap(state, nblk, blk);
510
511 nir_foreach_instr(instr, blk) {
512 if (instr->type == nir_instr_type_phi) {
513 /* Phi instructions are a bit of a special case when cloning because
514 * we don't want inserting the instruction to automatically handle
515 * use/defs for us. Instead, we need to wait until all the
516 * blocks/instructions are in so that we can set their sources up.
517 */
518 clone_phi(state, nir_instr_as_phi(instr), nblk);
519 } else {
520 nir_instr *ninstr = clone_instr(state, instr);
521 nir_instr_insert_after_block(nblk, ninstr);
522 }
523 }
524
525 return nblk;
526 }
527
528 static void
529 clone_cf_list(clone_state *state, struct exec_list *dst,
530 const struct exec_list *list);
531
532 static nir_if *
533 clone_if(clone_state *state, struct exec_list *cf_list, const nir_if *i)
534 {
535 nir_if *ni = nir_if_create(state->ns);
536
537 __clone_src(state, ni, &ni->condition, &i->condition);
538
539 nir_cf_node_insert_end(cf_list, &ni->cf_node);
540
541 clone_cf_list(state, &ni->then_list, &i->then_list);
542 clone_cf_list(state, &ni->else_list, &i->else_list);
543
544 return ni;
545 }
546
547 static nir_loop *
548 clone_loop(clone_state *state, struct exec_list *cf_list, const nir_loop *loop)
549 {
550 nir_loop *nloop = nir_loop_create(state->ns);
551
552 nir_cf_node_insert_end(cf_list, &nloop->cf_node);
553
554 clone_cf_list(state, &nloop->body, &loop->body);
555
556 return nloop;
557 }
558
559 /* clone list of nir_cf_node: */
560 static void
561 clone_cf_list(clone_state *state, struct exec_list *dst,
562 const struct exec_list *list)
563 {
564 foreach_list_typed(nir_cf_node, cf, node, list) {
565 switch (cf->type) {
566 case nir_cf_node_block:
567 clone_block(state, dst, nir_cf_node_as_block(cf));
568 break;
569 case nir_cf_node_if:
570 clone_if(state, dst, nir_cf_node_as_if(cf));
571 break;
572 case nir_cf_node_loop:
573 clone_loop(state, dst, nir_cf_node_as_loop(cf));
574 break;
575 default:
576 unreachable("bad cf type");
577 }
578 }
579 }
580
581 /* After we've cloned almost everything, we have to walk the list of phi
582 * sources and fix them up. Thanks to loops, the block and SSA value for a
583 * phi source may not be defined when we first encounter it. Instead, we
584 * add it to the phi_srcs list and we fix it up here.
585 */
586 static void
587 fixup_phi_srcs(clone_state *state)
588 {
589 list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) {
590 src->pred = remap_local(state, src->pred);
591
592 /* Remove from this list */
593 list_del(&src->src.use_link);
594
595 if (src->src.is_ssa) {
596 src->src.ssa = remap_local(state, src->src.ssa);
597 list_addtail(&src->src.use_link, &src->src.ssa->uses);
598 } else {
599 src->src.reg.reg = remap_reg(state, src->src.reg.reg);
600 list_addtail(&src->src.use_link, &src->src.reg.reg->uses);
601 }
602 }
603 assert(list_empty(&state->phi_srcs));
604 }
605
606 void
607 nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
608 struct hash_table *remap_table)
609 {
610 exec_list_make_empty(&dst->list);
611 dst->impl = src->impl;
612
613 if (exec_list_is_empty(&src->list))
614 return;
615
616 clone_state state;
617 init_clone_state(&state, remap_table, false, true);
618
619 /* We use the same shader */
620 state.ns = src->impl->function->shader;
621
622 /* The control-flow code assumes that the list of cf_nodes always starts
623 * and ends with a block. We start by adding an empty block.
624 */
625 nir_block *nblk = nir_block_create(state.ns);
626 nblk->cf_node.parent = parent;
627 exec_list_push_tail(&dst->list, &nblk->cf_node.node);
628
629 clone_cf_list(&state, &dst->list, &src->list);
630
631 fixup_phi_srcs(&state);
632 }
633
634 static nir_function_impl *
635 clone_function_impl(clone_state *state, const nir_function_impl *fi)
636 {
637 nir_function_impl *nfi = nir_function_impl_create_bare(state->ns);
638
639 clone_var_list(state, &nfi->locals, &fi->locals);
640 clone_reg_list(state, &nfi->registers, &fi->registers);
641 nfi->reg_alloc = fi->reg_alloc;
642
643 assert(list_empty(&state->phi_srcs));
644
645 clone_cf_list(state, &nfi->body, &fi->body);
646
647 fixup_phi_srcs(state);
648
649 /* All metadata is invalidated in the cloning process */
650 nfi->valid_metadata = 0;
651
652 return nfi;
653 }
654
655 nir_function_impl *
656 nir_function_impl_clone(const nir_function_impl *fi)
657 {
658 clone_state state;
659 init_clone_state(&state, NULL, false, false);
660
661 /* We use the same shader */
662 state.ns = fi->function->shader;
663
664 nir_function_impl *nfi = clone_function_impl(&state, fi);
665
666 free_clone_state(&state);
667
668 return nfi;
669 }
670
671 static nir_function *
672 clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns)
673 {
674 assert(ns == state->ns);
675 nir_function *nfxn = nir_function_create(ns, fxn->name);
676
677 /* Needed for call instructions */
678 add_remap(state, nfxn, fxn);
679
680 nfxn->num_params = fxn->num_params;
681 nfxn->params = ralloc_array(state->ns, nir_parameter, fxn->num_params);
682 memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params);
683
684 /* At first glance, it looks like we should clone the function_impl here.
685 * However, call instructions need to be able to reference at least the
686 * function and those will get processed as we clone the function_impls.
687 * We stop here and do function_impls as a second pass.
688 */
689
690 return nfxn;
691 }
692
693 nir_shader *
694 nir_shader_clone(void *mem_ctx, const nir_shader *s)
695 {
696 clone_state state;
697 init_clone_state(&state, NULL, true, false);
698
699 nir_shader *ns = nir_shader_create(mem_ctx, s->info.stage, s->options, NULL);
700 state.ns = ns;
701
702 clone_var_list(&state, &ns->uniforms, &s->uniforms);
703 clone_var_list(&state, &ns->inputs, &s->inputs);
704 clone_var_list(&state, &ns->outputs, &s->outputs);
705 clone_var_list(&state, &ns->shared, &s->shared);
706 clone_var_list(&state, &ns->globals, &s->globals);
707 clone_var_list(&state, &ns->system_values, &s->system_values);
708
709 /* Go through and clone functions */
710 foreach_list_typed(nir_function, fxn, node, &s->functions)
711 clone_function(&state, fxn, ns);
712
713 /* Only after all functions are cloned can we clone the actual function
714 * implementations. This is because nir_call_instrs need to reference the
715 * functions of other functions and we don't know what order the functions
716 * will have in the list.
717 */
718 nir_foreach_function(fxn, s) {
719 nir_function *nfxn = remap_global(&state, fxn);
720 nfxn->impl = clone_function_impl(&state, fxn->impl);
721 nfxn->impl->function = nfxn;
722 }
723
724 clone_reg_list(&state, &ns->registers, &s->registers);
725 ns->reg_alloc = s->reg_alloc;
726
727 ns->info = s->info;
728 ns->info.name = ralloc_strdup(ns, ns->info.name);
729 if (ns->info.label)
730 ns->info.label = ralloc_strdup(ns, ns->info.label);
731
732 ns->num_inputs = s->num_inputs;
733 ns->num_uniforms = s->num_uniforms;
734 ns->num_outputs = s->num_outputs;
735 ns->num_shared = s->num_shared;
736
737 ns->constant_data_size = s->constant_data_size;
738 if (s->constant_data_size > 0) {
739 ns->constant_data = ralloc_size(ns, s->constant_data_size);
740 memcpy(ns->constant_data, s->constant_data, s->constant_data_size);
741 }
742
743 free_clone_state(&state);
744
745 return ns;
746 }