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