2 * Copyright © 2015 Red Hat
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:
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
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
25 #include "nir_control_flow_private.h"
27 /* Secret Decoder Ring:
29 * Allocate and clone a foo.
31 * Clone body of foo (ie. parent class, embedded struct, etc)
35 /* True if we are cloning an entire shader. */
38 /* maps orig ptr -> cloned ptr: */
39 struct hash_table
*remap_table
;
41 /* List of phi sources. */
42 struct list_head phi_srcs
;
44 /* new shader object, used as memctx for just about everything else: */
49 init_clone_state(clone_state
*state
, bool global
)
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
);
58 free_clone_state(clone_state
*state
)
60 _mesa_hash_table_destroy(state
->remap_table
, NULL
);
64 _lookup_ptr(clone_state
*state
, const void *ptr
, bool global
)
66 struct hash_entry
*entry
;
71 if (!state
->global_clone
&& global
)
74 entry
= _mesa_hash_table_search(state
->remap_table
, ptr
);
75 assert(entry
&& "Failed to find pointer!");
83 add_remap(clone_state
*state
, void *nptr
, const void *ptr
)
85 _mesa_hash_table_insert(state
->remap_table
, ptr
, nptr
);
89 remap_local(clone_state
*state
, const void *ptr
)
91 return _lookup_ptr(state
, ptr
, false);
95 remap_global(clone_state
*state
, const void *ptr
)
97 return _lookup_ptr(state
, ptr
, true);
100 static nir_register
*
101 remap_reg(clone_state
*state
, const nir_register
*reg
)
103 return _lookup_ptr(state
, reg
, reg
->is_global
);
106 static nir_variable
*
107 remap_var(clone_state
*state
, const nir_variable
*var
)
109 return _lookup_ptr(state
, var
, nir_variable_is_global(var
));
113 nir_constant_clone(const nir_constant
*c
, nir_variable
*nvar
)
115 nir_constant
*nc
= ralloc(nvar
, nir_constant
);
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
);
127 /* NOTE: for cloning nir_variable's, bypass nir_variable_create to avoid
128 * having to deal with locals and globals separately:
131 nir_variable_clone(const nir_variable
*var
, nir_shader
*shader
)
133 nir_variable
*nvar
= rzalloc(shader
, nir_variable
);
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
);
146 nvar
->interface_type
= var
->interface_type
;
151 static nir_variable
*
152 clone_variable(clone_state
*state
, const nir_variable
*var
)
154 nir_variable
*nvar
= nir_variable_clone(var
, state
->ns
);
155 add_remap(state
, nvar
, var
);
160 /* clone list of nir_variable: */
162 clone_var_list(clone_state
*state
, struct exec_list
*dst
,
163 const struct exec_list
*list
)
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
);
172 /* NOTE: for cloning nir_register's, bypass nir_global/local_reg_create()
173 * to avoid having to deal with locals and globals separately:
175 static nir_register
*
176 clone_register(clone_state
*state
, const nir_register
*reg
)
178 nir_register
*nreg
= rzalloc(state
->ns
, nir_register
);
179 add_remap(state
, nreg
, reg
);
181 nreg
->num_components
= reg
->num_components
;
182 nreg
->num_array_elems
= reg
->num_array_elems
;
183 nreg
->index
= reg
->index
;
184 nreg
->name
= ralloc_strdup(nreg
, reg
->name
);
185 nreg
->is_global
= reg
->is_global
;
186 nreg
->is_packed
= reg
->is_packed
;
188 /* reconstructing uses/defs/if_uses handled by nir_instr_insert() */
189 list_inithead(&nreg
->uses
);
190 list_inithead(&nreg
->defs
);
191 list_inithead(&nreg
->if_uses
);
196 /* clone list of nir_register: */
198 clone_reg_list(clone_state
*state
, struct exec_list
*dst
,
199 const struct exec_list
*list
)
201 exec_list_make_empty(dst
);
202 foreach_list_typed(nir_register
, reg
, node
, list
) {
203 nir_register
*nreg
= clone_register(state
, reg
);
204 exec_list_push_tail(dst
, &nreg
->node
);
209 __clone_src(clone_state
*state
, void *ninstr_or_if
,
210 nir_src
*nsrc
, const nir_src
*src
)
212 nsrc
->is_ssa
= src
->is_ssa
;
214 nsrc
->ssa
= remap_local(state
, src
->ssa
);
216 nsrc
->reg
.reg
= remap_reg(state
, src
->reg
.reg
);
217 if (src
->reg
.indirect
) {
218 nsrc
->reg
.indirect
= ralloc(ninstr_or_if
, nir_src
);
219 __clone_src(state
, ninstr_or_if
, nsrc
->reg
.indirect
, src
->reg
.indirect
);
221 nsrc
->reg
.base_offset
= src
->reg
.base_offset
;
226 __clone_dst(clone_state
*state
, nir_instr
*ninstr
,
227 nir_dest
*ndst
, const nir_dest
*dst
)
229 ndst
->is_ssa
= dst
->is_ssa
;
231 nir_ssa_dest_init(ninstr
, ndst
, dst
->ssa
.num_components
,
232 dst
->ssa
.bit_size
, dst
->ssa
.name
);
233 add_remap(state
, &ndst
->ssa
, &dst
->ssa
);
235 ndst
->reg
.reg
= remap_reg(state
, dst
->reg
.reg
);
236 if (dst
->reg
.indirect
) {
237 ndst
->reg
.indirect
= ralloc(ninstr
, nir_src
);
238 __clone_src(state
, ninstr
, ndst
->reg
.indirect
, dst
->reg
.indirect
);
240 ndst
->reg
.base_offset
= dst
->reg
.base_offset
;
244 static nir_deref
*clone_deref(clone_state
*state
, const nir_deref
*deref
,
245 nir_instr
*ninstr
, nir_deref
*parent
);
247 static nir_deref_var
*
248 clone_deref_var(clone_state
*state
, const nir_deref_var
*dvar
,
251 nir_variable
*nvar
= remap_var(state
, dvar
->var
);
252 nir_deref_var
*ndvar
= nir_deref_var_create(ninstr
, nvar
);
254 if (dvar
->deref
.child
)
255 ndvar
->deref
.child
= clone_deref(state
, dvar
->deref
.child
,
256 ninstr
, &ndvar
->deref
);
261 static nir_deref_array
*
262 clone_deref_array(clone_state
*state
, const nir_deref_array
*darr
,
263 nir_instr
*ninstr
, nir_deref
*parent
)
265 nir_deref_array
*ndarr
= nir_deref_array_create(parent
);
267 ndarr
->deref
.type
= darr
->deref
.type
;
268 if (darr
->deref
.child
)
269 ndarr
->deref
.child
= clone_deref(state
, darr
->deref
.child
,
270 ninstr
, &ndarr
->deref
);
272 ndarr
->deref_array_type
= darr
->deref_array_type
;
273 ndarr
->base_offset
= darr
->base_offset
;
274 if (ndarr
->deref_array_type
== nir_deref_array_type_indirect
)
275 __clone_src(state
, ninstr
, &ndarr
->indirect
, &darr
->indirect
);
280 static nir_deref_struct
*
281 clone_deref_struct(clone_state
*state
, const nir_deref_struct
*dstr
,
282 nir_instr
*ninstr
, nir_deref
*parent
)
284 nir_deref_struct
*ndstr
= nir_deref_struct_create(parent
, dstr
->index
);
286 ndstr
->deref
.type
= dstr
->deref
.type
;
287 if (dstr
->deref
.child
)
288 ndstr
->deref
.child
= clone_deref(state
, dstr
->deref
.child
,
289 ninstr
, &ndstr
->deref
);
295 clone_deref(clone_state
*state
, const nir_deref
*dref
,
296 nir_instr
*ninstr
, nir_deref
*parent
)
298 switch (dref
->deref_type
) {
299 case nir_deref_type_array
:
300 return &clone_deref_array(state
, nir_deref_as_array(dref
),
301 ninstr
, parent
)->deref
;
302 case nir_deref_type_struct
:
303 return &clone_deref_struct(state
, nir_deref_as_struct(dref
),
304 ninstr
, parent
)->deref
;
306 unreachable("bad deref type");
311 static nir_alu_instr
*
312 clone_alu(clone_state
*state
, const nir_alu_instr
*alu
)
314 nir_alu_instr
*nalu
= nir_alu_instr_create(state
->ns
, alu
->op
);
316 __clone_dst(state
, &nalu
->instr
, &nalu
->dest
.dest
, &alu
->dest
.dest
);
317 nalu
->dest
.saturate
= alu
->dest
.saturate
;
318 nalu
->dest
.write_mask
= alu
->dest
.write_mask
;
320 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
321 __clone_src(state
, &nalu
->instr
, &nalu
->src
[i
].src
, &alu
->src
[i
].src
);
322 nalu
->src
[i
].negate
= alu
->src
[i
].negate
;
323 nalu
->src
[i
].abs
= alu
->src
[i
].abs
;
324 memcpy(nalu
->src
[i
].swizzle
, alu
->src
[i
].swizzle
,
325 sizeof(nalu
->src
[i
].swizzle
));
331 static nir_intrinsic_instr
*
332 clone_intrinsic(clone_state
*state
, const nir_intrinsic_instr
*itr
)
334 nir_intrinsic_instr
*nitr
=
335 nir_intrinsic_instr_create(state
->ns
, itr
->intrinsic
);
337 unsigned num_variables
= nir_intrinsic_infos
[itr
->intrinsic
].num_variables
;
338 unsigned num_srcs
= nir_intrinsic_infos
[itr
->intrinsic
].num_srcs
;
340 if (nir_intrinsic_infos
[itr
->intrinsic
].has_dest
)
341 __clone_dst(state
, &nitr
->instr
, &nitr
->dest
, &itr
->dest
);
343 nitr
->num_components
= itr
->num_components
;
344 memcpy(nitr
->const_index
, itr
->const_index
, sizeof(nitr
->const_index
));
346 for (unsigned i
= 0; i
< num_variables
; i
++) {
347 nitr
->variables
[i
] = clone_deref_var(state
, itr
->variables
[i
],
351 for (unsigned i
= 0; i
< num_srcs
; i
++)
352 __clone_src(state
, &nitr
->instr
, &nitr
->src
[i
], &itr
->src
[i
]);
357 static nir_load_const_instr
*
358 clone_load_const(clone_state
*state
, const nir_load_const_instr
*lc
)
360 nir_load_const_instr
*nlc
=
361 nir_load_const_instr_create(state
->ns
, lc
->def
.num_components
);
363 memcpy(&nlc
->value
, &lc
->value
, sizeof(nlc
->value
));
365 add_remap(state
, &nlc
->def
, &lc
->def
);
370 static nir_ssa_undef_instr
*
371 clone_ssa_undef(clone_state
*state
, const nir_ssa_undef_instr
*sa
)
373 nir_ssa_undef_instr
*nsa
=
374 nir_ssa_undef_instr_create(state
->ns
, sa
->def
.num_components
);
376 add_remap(state
, &nsa
->def
, &sa
->def
);
381 static nir_tex_instr
*
382 clone_tex(clone_state
*state
, const nir_tex_instr
*tex
)
384 nir_tex_instr
*ntex
= nir_tex_instr_create(state
->ns
, tex
->num_srcs
);
386 ntex
->sampler_dim
= tex
->sampler_dim
;
387 ntex
->dest_type
= tex
->dest_type
;
389 __clone_dst(state
, &ntex
->instr
, &ntex
->dest
, &tex
->dest
);
390 for (unsigned i
= 0; i
< ntex
->num_srcs
; i
++) {
391 ntex
->src
[i
].src_type
= tex
->src
[i
].src_type
;
392 __clone_src(state
, &ntex
->instr
, &ntex
->src
[i
].src
, &tex
->src
[i
].src
);
394 ntex
->coord_components
= tex
->coord_components
;
395 ntex
->is_array
= tex
->is_array
;
396 ntex
->is_shadow
= tex
->is_shadow
;
397 ntex
->is_new_style_shadow
= tex
->is_new_style_shadow
;
398 ntex
->component
= tex
->component
;
400 ntex
->texture_index
= tex
->texture_index
;
402 ntex
->texture
= clone_deref_var(state
, tex
->texture
, &ntex
->instr
);
403 ntex
->texture_array_size
= tex
->texture_array_size
;
405 ntex
->sampler_index
= tex
->sampler_index
;
407 ntex
->sampler
= clone_deref_var(state
, tex
->sampler
, &ntex
->instr
);
412 static nir_phi_instr
*
413 clone_phi(clone_state
*state
, const nir_phi_instr
*phi
, nir_block
*nblk
)
415 nir_phi_instr
*nphi
= nir_phi_instr_create(state
->ns
);
417 __clone_dst(state
, &nphi
->instr
, &nphi
->dest
, &phi
->dest
);
419 /* Cloning a phi node is a bit different from other instructions. The
420 * sources of phi instructions are the only time where we can use an SSA
421 * def before it is defined. In order to handle this, we just copy over
422 * the sources from the old phi instruction directly and then fix them up
423 * in a second pass once all the instrutions in the function have been
426 * In order to ensure that the copied sources (which are the same as the
427 * old phi instruction's sources for now) don't get inserted into the old
428 * shader's use-def lists, we have to add the phi instruction *before* we
429 * set up its sources.
431 nir_instr_insert_after_block(nblk
, &nphi
->instr
);
433 foreach_list_typed(nir_phi_src
, src
, node
, &phi
->srcs
) {
434 nir_phi_src
*nsrc
= ralloc(nphi
, nir_phi_src
);
436 /* Just copy the old source for now. */
437 memcpy(nsrc
, src
, sizeof(*src
));
439 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
440 * we have to set the parent_instr manually. It doesn't really matter
441 * when we do it, so we might as well do it here.
443 nsrc
->src
.parent_instr
= &nphi
->instr
;
445 /* Stash it in the list of phi sources. We'll walk this list and fix up
446 * sources at the very end of clone_function_impl.
448 list_add(&nsrc
->src
.use_link
, &state
->phi_srcs
);
450 exec_list_push_tail(&nphi
->srcs
, &nsrc
->node
);
456 static nir_jump_instr
*
457 clone_jump(clone_state
*state
, const nir_jump_instr
*jmp
)
459 nir_jump_instr
*njmp
= nir_jump_instr_create(state
->ns
, jmp
->type
);
464 static nir_call_instr
*
465 clone_call(clone_state
*state
, const nir_call_instr
*call
)
467 nir_function
*ncallee
= remap_global(state
, call
->callee
);
468 nir_call_instr
*ncall
= nir_call_instr_create(state
->ns
, ncallee
);
470 for (unsigned i
= 0; i
< ncall
->num_params
; i
++)
471 ncall
->params
[i
] = clone_deref_var(state
, call
->params
[i
], &ncall
->instr
);
473 ncall
->return_deref
= clone_deref_var(state
, call
->return_deref
,
480 clone_instr(clone_state
*state
, const nir_instr
*instr
)
482 switch (instr
->type
) {
483 case nir_instr_type_alu
:
484 return &clone_alu(state
, nir_instr_as_alu(instr
))->instr
;
485 case nir_instr_type_intrinsic
:
486 return &clone_intrinsic(state
, nir_instr_as_intrinsic(instr
))->instr
;
487 case nir_instr_type_load_const
:
488 return &clone_load_const(state
, nir_instr_as_load_const(instr
))->instr
;
489 case nir_instr_type_ssa_undef
:
490 return &clone_ssa_undef(state
, nir_instr_as_ssa_undef(instr
))->instr
;
491 case nir_instr_type_tex
:
492 return &clone_tex(state
, nir_instr_as_tex(instr
))->instr
;
493 case nir_instr_type_phi
:
494 unreachable("Cannot clone phis with clone_instr");
495 case nir_instr_type_jump
:
496 return &clone_jump(state
, nir_instr_as_jump(instr
))->instr
;
497 case nir_instr_type_call
:
498 return &clone_call(state
, nir_instr_as_call(instr
))->instr
;
499 case nir_instr_type_parallel_copy
:
500 unreachable("Cannot clone parallel copies");
502 unreachable("bad instr type");
508 clone_block(clone_state
*state
, struct exec_list
*cf_list
, const nir_block
*blk
)
510 /* Don't actually create a new block. Just use the one from the tail of
511 * the list. NIR guarantees that the tail of the list is a block and that
512 * no two blocks are side-by-side in the IR; It should be empty.
515 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
516 assert(nblk
->cf_node
.type
== nir_cf_node_block
);
517 assert(exec_list_is_empty(&nblk
->instr_list
));
519 /* We need this for phi sources */
520 add_remap(state
, nblk
, blk
);
522 nir_foreach_instr(blk
, instr
) {
523 if (instr
->type
== nir_instr_type_phi
) {
524 /* Phi instructions are a bit of a special case when cloning because
525 * we don't want inserting the instruction to automatically handle
526 * use/defs for us. Instead, we need to wait until all the
527 * blocks/instructions are in so that we can set their sources up.
529 clone_phi(state
, nir_instr_as_phi(instr
), nblk
);
531 nir_instr
*ninstr
= clone_instr(state
, instr
);
532 nir_instr_insert_after_block(nblk
, ninstr
);
540 clone_cf_list(clone_state
*state
, struct exec_list
*dst
,
541 const struct exec_list
*list
);
544 clone_if(clone_state
*state
, struct exec_list
*cf_list
, const nir_if
*i
)
546 nir_if
*ni
= nir_if_create(state
->ns
);
548 __clone_src(state
, ni
, &ni
->condition
, &i
->condition
);
550 nir_cf_node_insert_end(cf_list
, &ni
->cf_node
);
552 clone_cf_list(state
, &ni
->then_list
, &i
->then_list
);
553 clone_cf_list(state
, &ni
->else_list
, &i
->else_list
);
559 clone_loop(clone_state
*state
, struct exec_list
*cf_list
, const nir_loop
*loop
)
561 nir_loop
*nloop
= nir_loop_create(state
->ns
);
563 nir_cf_node_insert_end(cf_list
, &nloop
->cf_node
);
565 clone_cf_list(state
, &nloop
->body
, &loop
->body
);
570 /* clone list of nir_cf_node: */
572 clone_cf_list(clone_state
*state
, struct exec_list
*dst
,
573 const struct exec_list
*list
)
575 foreach_list_typed(nir_cf_node
, cf
, node
, list
) {
577 case nir_cf_node_block
:
578 clone_block(state
, dst
, nir_cf_node_as_block(cf
));
581 clone_if(state
, dst
, nir_cf_node_as_if(cf
));
583 case nir_cf_node_loop
:
584 clone_loop(state
, dst
, nir_cf_node_as_loop(cf
));
587 unreachable("bad cf type");
592 static nir_function_impl
*
593 clone_function_impl(clone_state
*state
, const nir_function_impl
*fi
)
595 nir_function_impl
*nfi
= nir_function_impl_create_bare(state
->ns
);
597 clone_var_list(state
, &nfi
->locals
, &fi
->locals
);
598 clone_reg_list(state
, &nfi
->registers
, &fi
->registers
);
599 nfi
->reg_alloc
= fi
->reg_alloc
;
601 nfi
->num_params
= fi
->num_params
;
602 nfi
->params
= ralloc_array(state
->ns
, nir_variable
*, fi
->num_params
);
603 for (unsigned i
= 0; i
< fi
->num_params
; i
++) {
604 nfi
->params
[i
] = clone_variable(state
, fi
->params
[i
]);
607 nfi
->return_var
= clone_variable(state
, fi
->return_var
);
609 assert(list_empty(&state
->phi_srcs
));
611 clone_cf_list(state
, &nfi
->body
, &fi
->body
);
613 /* After we've cloned almost everything, we have to walk the list of phi
614 * sources and fix them up. Thanks to loops, the block and SSA value for a
615 * phi source may not be defined when we first encounter it. Instead, we
616 * add it to the phi_srcs list and we fix it up here.
618 list_for_each_entry_safe(nir_phi_src
, src
, &state
->phi_srcs
, src
.use_link
) {
619 src
->pred
= remap_local(state
, src
->pred
);
620 assert(src
->src
.is_ssa
);
621 src
->src
.ssa
= remap_local(state
, src
->src
.ssa
);
623 /* Remove from this list and place in the uses of the SSA def */
624 list_del(&src
->src
.use_link
);
625 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
627 assert(list_empty(&state
->phi_srcs
));
629 /* All metadata is invalidated in the cloning process */
630 nfi
->valid_metadata
= 0;
636 nir_function_impl_clone(const nir_function_impl
*fi
)
639 init_clone_state(&state
, false);
641 /* We use the same shader */
642 state
.ns
= fi
->function
->shader
;
644 nir_function_impl
*nfi
= clone_function_impl(&state
, fi
);
646 free_clone_state(&state
);
651 static nir_function
*
652 clone_function(clone_state
*state
, const nir_function
*fxn
, nir_shader
*ns
)
654 assert(ns
== state
->ns
);
655 nir_function
*nfxn
= nir_function_create(ns
, fxn
->name
);
657 /* Needed for call instructions */
658 add_remap(state
, nfxn
, fxn
);
660 nfxn
->num_params
= fxn
->num_params
;
661 nfxn
->params
= ralloc_array(state
->ns
, nir_parameter
, fxn
->num_params
);
662 memcpy(nfxn
->params
, fxn
->params
, sizeof(nir_parameter
) * fxn
->num_params
);
664 nfxn
->return_type
= fxn
->return_type
;
666 /* At first glance, it looks like we should clone the function_impl here.
667 * However, call instructions need to be able to reference at least the
668 * function and those will get processed as we clone the function_impl's.
669 * We stop here and do function_impls as a second pass.
676 nir_shader_clone(void *mem_ctx
, const nir_shader
*s
)
679 init_clone_state(&state
, true);
681 nir_shader
*ns
= nir_shader_create(mem_ctx
, s
->stage
, s
->options
);
684 clone_var_list(&state
, &ns
->uniforms
, &s
->uniforms
);
685 clone_var_list(&state
, &ns
->inputs
, &s
->inputs
);
686 clone_var_list(&state
, &ns
->outputs
, &s
->outputs
);
687 clone_var_list(&state
, &ns
->shared
, &s
->shared
);
688 clone_var_list(&state
, &ns
->globals
, &s
->globals
);
689 clone_var_list(&state
, &ns
->system_values
, &s
->system_values
);
691 /* Go through and clone functions */
692 foreach_list_typed(nir_function
, fxn
, node
, &s
->functions
)
693 clone_function(&state
, fxn
, ns
);
695 /* Only after all functions are cloned can we clone the actual function
696 * implementations. This is because nir_call_instr's need to reference the
697 * functions of other functions and we don't know what order the functions
698 * will have in the list.
700 nir_foreach_function(s
, fxn
) {
701 nir_function
*nfxn
= remap_global(&state
, fxn
);
702 nfxn
->impl
= clone_function_impl(&state
, fxn
->impl
);
703 nfxn
->impl
->function
= nfxn
;
706 clone_reg_list(&state
, &ns
->registers
, &s
->registers
);
707 ns
->reg_alloc
= s
->reg_alloc
;
710 ns
->info
.name
= ralloc_strdup(ns
, ns
->info
.name
);
712 ns
->info
.label
= ralloc_strdup(ns
, ns
->info
.label
);
714 ns
->num_inputs
= s
->num_inputs
;
715 ns
->num_uniforms
= s
->num_uniforms
;
716 ns
->num_outputs
= s
->num_outputs
;
717 ns
->num_shared
= s
->num_shared
;
719 free_clone_state(&state
);