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