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.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 /* 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.
43 bool allow_remap_fallback
;
45 /* maps orig ptr -> cloned ptr: */
46 struct hash_table
*remap_table
;
48 /* List of phi sources. */
49 struct list_head phi_srcs
;
51 /* new shader object, used as memctx for just about everything else: */
56 init_clone_state(clone_state
*state
, struct hash_table
*remap_table
,
57 bool global
, bool allow_remap_fallback
)
59 state
->global_clone
= global
;
60 state
->allow_remap_fallback
= allow_remap_fallback
;
63 state
->remap_table
= remap_table
;
65 state
->remap_table
= _mesa_pointer_hash_table_create(NULL
);
68 list_inithead(&state
->phi_srcs
);
72 free_clone_state(clone_state
*state
)
74 _mesa_hash_table_destroy(state
->remap_table
, NULL
);
78 _lookup_ptr(clone_state
*state
, const void *ptr
, bool global
)
80 struct hash_entry
*entry
;
85 if (!state
->global_clone
&& global
)
88 entry
= _mesa_hash_table_search(state
->remap_table
, ptr
);
90 assert(state
->allow_remap_fallback
);
98 add_remap(clone_state
*state
, void *nptr
, const void *ptr
)
100 _mesa_hash_table_insert(state
->remap_table
, ptr
, nptr
);
104 remap_local(clone_state
*state
, const void *ptr
)
106 return _lookup_ptr(state
, ptr
, false);
110 remap_global(clone_state
*state
, const void *ptr
)
112 return _lookup_ptr(state
, ptr
, true);
115 static nir_register
*
116 remap_reg(clone_state
*state
, const nir_register
*reg
)
118 return _lookup_ptr(state
, reg
, false);
121 static nir_variable
*
122 remap_var(clone_state
*state
, const nir_variable
*var
)
124 return _lookup_ptr(state
, var
, nir_variable_is_global(var
));
128 nir_constant_clone(const nir_constant
*c
, nir_variable
*nvar
)
130 nir_constant
*nc
= ralloc(nvar
, nir_constant
);
132 memcpy(nc
->values
, c
->values
, sizeof(nc
->values
));
133 nc
->num_elements
= c
->num_elements
;
134 nc
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
135 for (unsigned i
= 0; i
< c
->num_elements
; i
++) {
136 nc
->elements
[i
] = nir_constant_clone(c
->elements
[i
], nvar
);
142 /* NOTE: for cloning nir_variables, bypass nir_variable_create to avoid
143 * having to deal with locals and globals separately:
146 nir_variable_clone(const nir_variable
*var
, nir_shader
*shader
)
148 nir_variable
*nvar
= rzalloc(shader
, nir_variable
);
150 nvar
->type
= var
->type
;
151 nvar
->name
= ralloc_strdup(nvar
, var
->name
);
152 nvar
->data
= var
->data
;
153 nvar
->num_state_slots
= var
->num_state_slots
;
154 if (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
));
159 if (var
->constant_initializer
) {
160 nvar
->constant_initializer
=
161 nir_constant_clone(var
->constant_initializer
, nvar
);
163 nvar
->interface_type
= var
->interface_type
;
165 nvar
->num_members
= var
->num_members
;
166 if (var
->num_members
) {
167 nvar
->members
= ralloc_array(nvar
, struct nir_variable_data
,
169 memcpy(nvar
->members
, var
->members
,
170 var
->num_members
* sizeof(*var
->members
));
176 static nir_variable
*
177 clone_variable(clone_state
*state
, const nir_variable
*var
)
179 nir_variable
*nvar
= nir_variable_clone(var
, state
->ns
);
180 add_remap(state
, nvar
, var
);
185 /* clone list of nir_variable: */
187 clone_var_list(clone_state
*state
, struct exec_list
*dst
,
188 const struct exec_list
*list
)
190 exec_list_make_empty(dst
);
191 foreach_list_typed(nir_variable
, var
, node
, list
) {
192 nir_variable
*nvar
= clone_variable(state
, var
);
193 exec_list_push_tail(dst
, &nvar
->node
);
197 /* NOTE: for cloning nir_registers, bypass nir_global/local_reg_create()
198 * to avoid having to deal with locals and globals separately:
200 static nir_register
*
201 clone_register(clone_state
*state
, const nir_register
*reg
)
203 nir_register
*nreg
= rzalloc(state
->ns
, nir_register
);
204 add_remap(state
, nreg
, reg
);
206 nreg
->num_components
= reg
->num_components
;
207 nreg
->bit_size
= reg
->bit_size
;
208 nreg
->num_array_elems
= reg
->num_array_elems
;
209 nreg
->index
= reg
->index
;
210 nreg
->name
= ralloc_strdup(nreg
, reg
->name
);
212 /* reconstructing uses/defs/if_uses handled by nir_instr_insert() */
213 list_inithead(&nreg
->uses
);
214 list_inithead(&nreg
->defs
);
215 list_inithead(&nreg
->if_uses
);
220 /* clone list of nir_register: */
222 clone_reg_list(clone_state
*state
, struct exec_list
*dst
,
223 const struct exec_list
*list
)
225 exec_list_make_empty(dst
);
226 foreach_list_typed(nir_register
, reg
, node
, list
) {
227 nir_register
*nreg
= clone_register(state
, reg
);
228 exec_list_push_tail(dst
, &nreg
->node
);
233 __clone_src(clone_state
*state
, void *ninstr_or_if
,
234 nir_src
*nsrc
, const nir_src
*src
)
236 nsrc
->is_ssa
= src
->is_ssa
;
238 nsrc
->ssa
= remap_local(state
, src
->ssa
);
240 nsrc
->reg
.reg
= remap_reg(state
, src
->reg
.reg
);
241 if (src
->reg
.indirect
) {
242 nsrc
->reg
.indirect
= ralloc(ninstr_or_if
, nir_src
);
243 __clone_src(state
, ninstr_or_if
, nsrc
->reg
.indirect
, src
->reg
.indirect
);
245 nsrc
->reg
.base_offset
= src
->reg
.base_offset
;
250 __clone_dst(clone_state
*state
, nir_instr
*ninstr
,
251 nir_dest
*ndst
, const nir_dest
*dst
)
253 ndst
->is_ssa
= dst
->is_ssa
;
255 nir_ssa_dest_init(ninstr
, ndst
, dst
->ssa
.num_components
,
256 dst
->ssa
.bit_size
, dst
->ssa
.name
);
257 add_remap(state
, &ndst
->ssa
, &dst
->ssa
);
259 ndst
->reg
.reg
= remap_reg(state
, dst
->reg
.reg
);
260 if (dst
->reg
.indirect
) {
261 ndst
->reg
.indirect
= ralloc(ninstr
, nir_src
);
262 __clone_src(state
, ninstr
, ndst
->reg
.indirect
, dst
->reg
.indirect
);
264 ndst
->reg
.base_offset
= dst
->reg
.base_offset
;
269 nir_alu_instr_clone(nir_shader
*shader
, const nir_alu_instr
*orig
)
271 nir_alu_instr
*clone
= nir_alu_instr_create(shader
, orig
->op
);
273 clone
->exact
= orig
->exact
;
275 for (unsigned i
= 0; i
< nir_op_infos
[orig
->op
].num_inputs
; i
++)
276 nir_alu_src_copy(&clone
->src
[i
], &orig
->src
[i
], clone
);
278 nir_ssa_dest_init(&clone
->instr
,
280 orig
->dest
.dest
.ssa
.num_components
,
281 orig
->dest
.dest
.ssa
.bit_size
,
282 orig
->dest
.dest
.ssa
.name
);
283 clone
->dest
.write_mask
= orig
->dest
.write_mask
;
288 static nir_alu_instr
*
289 clone_alu(clone_state
*state
, const nir_alu_instr
*alu
)
291 nir_alu_instr
*nalu
= nir_alu_instr_create(state
->ns
, alu
->op
);
292 nalu
->exact
= alu
->exact
;
293 nalu
->no_signed_wrap
= alu
->no_signed_wrap
;
294 nalu
->no_unsigned_wrap
= alu
->no_unsigned_wrap
;
296 __clone_dst(state
, &nalu
->instr
, &nalu
->dest
.dest
, &alu
->dest
.dest
);
297 nalu
->dest
.saturate
= alu
->dest
.saturate
;
298 nalu
->dest
.write_mask
= alu
->dest
.write_mask
;
300 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
301 __clone_src(state
, &nalu
->instr
, &nalu
->src
[i
].src
, &alu
->src
[i
].src
);
302 nalu
->src
[i
].negate
= alu
->src
[i
].negate
;
303 nalu
->src
[i
].abs
= alu
->src
[i
].abs
;
304 memcpy(nalu
->src
[i
].swizzle
, alu
->src
[i
].swizzle
,
305 sizeof(nalu
->src
[i
].swizzle
));
311 static nir_deref_instr
*
312 clone_deref_instr(clone_state
*state
, const nir_deref_instr
*deref
)
314 nir_deref_instr
*nderef
=
315 nir_deref_instr_create(state
->ns
, deref
->deref_type
);
317 __clone_dst(state
, &nderef
->instr
, &nderef
->dest
, &deref
->dest
);
319 nderef
->mode
= deref
->mode
;
320 nderef
->type
= deref
->type
;
322 if (deref
->deref_type
== nir_deref_type_var
) {
323 nderef
->var
= remap_var(state
, deref
->var
);
327 __clone_src(state
, &nderef
->instr
, &nderef
->parent
, &deref
->parent
);
329 switch (deref
->deref_type
) {
330 case nir_deref_type_struct
:
331 nderef
->strct
.index
= deref
->strct
.index
;
334 case nir_deref_type_array
:
335 case nir_deref_type_ptr_as_array
:
336 __clone_src(state
, &nderef
->instr
,
337 &nderef
->arr
.index
, &deref
->arr
.index
);
340 case nir_deref_type_array_wildcard
:
344 case nir_deref_type_cast
:
345 nderef
->cast
.ptr_stride
= deref
->cast
.ptr_stride
;
349 unreachable("Invalid instruction deref type");
355 static nir_intrinsic_instr
*
356 clone_intrinsic(clone_state
*state
, const nir_intrinsic_instr
*itr
)
358 nir_intrinsic_instr
*nitr
=
359 nir_intrinsic_instr_create(state
->ns
, itr
->intrinsic
);
361 unsigned num_srcs
= nir_intrinsic_infos
[itr
->intrinsic
].num_srcs
;
363 if (nir_intrinsic_infos
[itr
->intrinsic
].has_dest
)
364 __clone_dst(state
, &nitr
->instr
, &nitr
->dest
, &itr
->dest
);
366 nitr
->num_components
= itr
->num_components
;
367 memcpy(nitr
->const_index
, itr
->const_index
, sizeof(nitr
->const_index
));
369 for (unsigned i
= 0; i
< num_srcs
; i
++)
370 __clone_src(state
, &nitr
->instr
, &nitr
->src
[i
], &itr
->src
[i
]);
375 static nir_load_const_instr
*
376 clone_load_const(clone_state
*state
, const nir_load_const_instr
*lc
)
378 nir_load_const_instr
*nlc
=
379 nir_load_const_instr_create(state
->ns
, lc
->def
.num_components
,
382 memcpy(&nlc
->value
, &lc
->value
, sizeof(*nlc
->value
) * lc
->def
.num_components
);
384 add_remap(state
, &nlc
->def
, &lc
->def
);
389 static nir_ssa_undef_instr
*
390 clone_ssa_undef(clone_state
*state
, const nir_ssa_undef_instr
*sa
)
392 nir_ssa_undef_instr
*nsa
=
393 nir_ssa_undef_instr_create(state
->ns
, sa
->def
.num_components
,
396 add_remap(state
, &nsa
->def
, &sa
->def
);
401 static nir_tex_instr
*
402 clone_tex(clone_state
*state
, const nir_tex_instr
*tex
)
404 nir_tex_instr
*ntex
= nir_tex_instr_create(state
->ns
, tex
->num_srcs
);
406 ntex
->sampler_dim
= tex
->sampler_dim
;
407 ntex
->dest_type
= tex
->dest_type
;
409 __clone_dst(state
, &ntex
->instr
, &ntex
->dest
, &tex
->dest
);
410 for (unsigned i
= 0; i
< ntex
->num_srcs
; i
++) {
411 ntex
->src
[i
].src_type
= tex
->src
[i
].src_type
;
412 __clone_src(state
, &ntex
->instr
, &ntex
->src
[i
].src
, &tex
->src
[i
].src
);
414 ntex
->coord_components
= tex
->coord_components
;
415 ntex
->is_array
= tex
->is_array
;
416 ntex
->is_shadow
= tex
->is_shadow
;
417 ntex
->is_new_style_shadow
= tex
->is_new_style_shadow
;
418 ntex
->component
= tex
->component
;
419 memcpy(ntex
->tg4_offsets
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
421 ntex
->texture_index
= tex
->texture_index
;
422 ntex
->texture_array_size
= tex
->texture_array_size
;
423 ntex
->sampler_index
= tex
->sampler_index
;
428 static nir_phi_instr
*
429 clone_phi(clone_state
*state
, const nir_phi_instr
*phi
, nir_block
*nblk
)
431 nir_phi_instr
*nphi
= nir_phi_instr_create(state
->ns
);
433 __clone_dst(state
, &nphi
->instr
, &nphi
->dest
, &phi
->dest
);
435 /* Cloning a phi node is a bit different from other instructions. The
436 * sources of phi instructions are the only time where we can use an SSA
437 * def before it is defined. In order to handle this, we just copy over
438 * the sources from the old phi instruction directly and then fix them up
439 * in a second pass once all the instrutions in the function have been
442 * In order to ensure that the copied sources (which are the same as the
443 * old phi instruction's sources for now) don't get inserted into the old
444 * shader's use-def lists, we have to add the phi instruction *before* we
445 * set up its sources.
447 nir_instr_insert_after_block(nblk
, &nphi
->instr
);
449 foreach_list_typed(nir_phi_src
, src
, node
, &phi
->srcs
) {
450 nir_phi_src
*nsrc
= ralloc(nphi
, nir_phi_src
);
452 /* Just copy the old source for now. */
453 memcpy(nsrc
, src
, sizeof(*src
));
455 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
456 * we have to set the parent_instr manually. It doesn't really matter
457 * when we do it, so we might as well do it here.
459 nsrc
->src
.parent_instr
= &nphi
->instr
;
461 /* Stash it in the list of phi sources. We'll walk this list and fix up
462 * sources at the very end of clone_function_impl.
464 list_add(&nsrc
->src
.use_link
, &state
->phi_srcs
);
466 exec_list_push_tail(&nphi
->srcs
, &nsrc
->node
);
472 static nir_jump_instr
*
473 clone_jump(clone_state
*state
, const nir_jump_instr
*jmp
)
475 nir_jump_instr
*njmp
= nir_jump_instr_create(state
->ns
, jmp
->type
);
480 static nir_call_instr
*
481 clone_call(clone_state
*state
, const nir_call_instr
*call
)
483 nir_function
*ncallee
= remap_global(state
, call
->callee
);
484 nir_call_instr
*ncall
= nir_call_instr_create(state
->ns
, ncallee
);
486 for (unsigned i
= 0; i
< ncall
->num_params
; i
++)
487 __clone_src(state
, ncall
, &ncall
->params
[i
], &call
->params
[i
]);
493 clone_instr(clone_state
*state
, const nir_instr
*instr
)
495 switch (instr
->type
) {
496 case nir_instr_type_alu
:
497 return &clone_alu(state
, nir_instr_as_alu(instr
))->instr
;
498 case nir_instr_type_deref
:
499 return &clone_deref_instr(state
, nir_instr_as_deref(instr
))->instr
;
500 case nir_instr_type_intrinsic
:
501 return &clone_intrinsic(state
, nir_instr_as_intrinsic(instr
))->instr
;
502 case nir_instr_type_load_const
:
503 return &clone_load_const(state
, nir_instr_as_load_const(instr
))->instr
;
504 case nir_instr_type_ssa_undef
:
505 return &clone_ssa_undef(state
, nir_instr_as_ssa_undef(instr
))->instr
;
506 case nir_instr_type_tex
:
507 return &clone_tex(state
, nir_instr_as_tex(instr
))->instr
;
508 case nir_instr_type_phi
:
509 unreachable("Cannot clone phis with clone_instr");
510 case nir_instr_type_jump
:
511 return &clone_jump(state
, nir_instr_as_jump(instr
))->instr
;
512 case nir_instr_type_call
:
513 return &clone_call(state
, nir_instr_as_call(instr
))->instr
;
514 case nir_instr_type_parallel_copy
:
515 unreachable("Cannot clone parallel copies");
517 unreachable("bad instr type");
523 clone_block(clone_state
*state
, struct exec_list
*cf_list
, const nir_block
*blk
)
525 /* Don't actually create a new block. Just use the one from the tail of
526 * the list. NIR guarantees that the tail of the list is a block and that
527 * no two blocks are side-by-side in the IR; It should be empty.
530 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
531 assert(nblk
->cf_node
.type
== nir_cf_node_block
);
532 assert(exec_list_is_empty(&nblk
->instr_list
));
534 /* We need this for phi sources */
535 add_remap(state
, nblk
, blk
);
537 nir_foreach_instr(instr
, blk
) {
538 if (instr
->type
== nir_instr_type_phi
) {
539 /* Phi instructions are a bit of a special case when cloning because
540 * we don't want inserting the instruction to automatically handle
541 * use/defs for us. Instead, we need to wait until all the
542 * blocks/instructions are in so that we can set their sources up.
544 clone_phi(state
, nir_instr_as_phi(instr
), nblk
);
546 nir_instr
*ninstr
= clone_instr(state
, instr
);
547 nir_instr_insert_after_block(nblk
, ninstr
);
555 clone_cf_list(clone_state
*state
, struct exec_list
*dst
,
556 const struct exec_list
*list
);
559 clone_if(clone_state
*state
, struct exec_list
*cf_list
, const nir_if
*i
)
561 nir_if
*ni
= nir_if_create(state
->ns
);
562 ni
->control
= i
->control
;
564 __clone_src(state
, ni
, &ni
->condition
, &i
->condition
);
566 nir_cf_node_insert_end(cf_list
, &ni
->cf_node
);
568 clone_cf_list(state
, &ni
->then_list
, &i
->then_list
);
569 clone_cf_list(state
, &ni
->else_list
, &i
->else_list
);
575 clone_loop(clone_state
*state
, struct exec_list
*cf_list
, const nir_loop
*loop
)
577 nir_loop
*nloop
= nir_loop_create(state
->ns
);
578 nloop
->control
= loop
->control
;
579 nloop
->partially_unrolled
= loop
->partially_unrolled
;
581 nir_cf_node_insert_end(cf_list
, &nloop
->cf_node
);
583 clone_cf_list(state
, &nloop
->body
, &loop
->body
);
588 /* clone list of nir_cf_node: */
590 clone_cf_list(clone_state
*state
, struct exec_list
*dst
,
591 const struct exec_list
*list
)
593 foreach_list_typed(nir_cf_node
, cf
, node
, list
) {
595 case nir_cf_node_block
:
596 clone_block(state
, dst
, nir_cf_node_as_block(cf
));
599 clone_if(state
, dst
, nir_cf_node_as_if(cf
));
601 case nir_cf_node_loop
:
602 clone_loop(state
, dst
, nir_cf_node_as_loop(cf
));
605 unreachable("bad cf type");
610 /* After we've cloned almost everything, we have to walk the list of phi
611 * sources and fix them up. Thanks to loops, the block and SSA value for a
612 * phi source may not be defined when we first encounter it. Instead, we
613 * add it to the phi_srcs list and we fix it up here.
616 fixup_phi_srcs(clone_state
*state
)
618 list_for_each_entry_safe(nir_phi_src
, src
, &state
->phi_srcs
, src
.use_link
) {
619 src
->pred
= remap_local(state
, src
->pred
);
621 /* Remove from this list */
622 list_del(&src
->src
.use_link
);
624 if (src
->src
.is_ssa
) {
625 src
->src
.ssa
= remap_local(state
, src
->src
.ssa
);
626 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
628 src
->src
.reg
.reg
= remap_reg(state
, src
->src
.reg
.reg
);
629 list_addtail(&src
->src
.use_link
, &src
->src
.reg
.reg
->uses
);
632 assert(list_is_empty(&state
->phi_srcs
));
636 nir_cf_list_clone(nir_cf_list
*dst
, nir_cf_list
*src
, nir_cf_node
*parent
,
637 struct hash_table
*remap_table
)
639 exec_list_make_empty(&dst
->list
);
640 dst
->impl
= src
->impl
;
642 if (exec_list_is_empty(&src
->list
))
646 init_clone_state(&state
, remap_table
, false, true);
648 /* We use the same shader */
649 state
.ns
= src
->impl
->function
->shader
;
651 /* The control-flow code assumes that the list of cf_nodes always starts
652 * and ends with a block. We start by adding an empty block.
654 nir_block
*nblk
= nir_block_create(state
.ns
);
655 nblk
->cf_node
.parent
= parent
;
656 exec_list_push_tail(&dst
->list
, &nblk
->cf_node
.node
);
658 clone_cf_list(&state
, &dst
->list
, &src
->list
);
660 fixup_phi_srcs(&state
);
663 static nir_function_impl
*
664 clone_function_impl(clone_state
*state
, const nir_function_impl
*fi
)
666 nir_function_impl
*nfi
= nir_function_impl_create_bare(state
->ns
);
668 clone_var_list(state
, &nfi
->locals
, &fi
->locals
);
669 clone_reg_list(state
, &nfi
->registers
, &fi
->registers
);
670 nfi
->reg_alloc
= fi
->reg_alloc
;
672 assert(list_is_empty(&state
->phi_srcs
));
674 clone_cf_list(state
, &nfi
->body
, &fi
->body
);
676 fixup_phi_srcs(state
);
678 /* All metadata is invalidated in the cloning process */
679 nfi
->valid_metadata
= 0;
685 nir_function_impl_clone(nir_shader
*shader
, const nir_function_impl
*fi
)
688 init_clone_state(&state
, NULL
, false, false);
692 nir_function_impl
*nfi
= clone_function_impl(&state
, fi
);
694 free_clone_state(&state
);
699 static nir_function
*
700 clone_function(clone_state
*state
, const nir_function
*fxn
, nir_shader
*ns
)
702 assert(ns
== state
->ns
);
703 nir_function
*nfxn
= nir_function_create(ns
, fxn
->name
);
705 /* Needed for call instructions */
706 add_remap(state
, nfxn
, fxn
);
708 nfxn
->num_params
= fxn
->num_params
;
709 nfxn
->params
= ralloc_array(state
->ns
, nir_parameter
, fxn
->num_params
);
710 memcpy(nfxn
->params
, fxn
->params
, sizeof(nir_parameter
) * fxn
->num_params
);
711 nfxn
->is_entrypoint
= fxn
->is_entrypoint
;
713 /* At first glance, it looks like we should clone the function_impl here.
714 * However, call instructions need to be able to reference at least the
715 * function and those will get processed as we clone the function_impls.
716 * We stop here and do function_impls as a second pass.
723 nir_shader_clone(void *mem_ctx
, const nir_shader
*s
)
726 init_clone_state(&state
, NULL
, true, false);
728 nir_shader
*ns
= nir_shader_create(mem_ctx
, s
->info
.stage
, s
->options
, NULL
);
731 clone_var_list(&state
, &ns
->uniforms
, &s
->uniforms
);
732 clone_var_list(&state
, &ns
->inputs
, &s
->inputs
);
733 clone_var_list(&state
, &ns
->outputs
, &s
->outputs
);
734 clone_var_list(&state
, &ns
->shared
, &s
->shared
);
735 clone_var_list(&state
, &ns
->globals
, &s
->globals
);
736 clone_var_list(&state
, &ns
->system_values
, &s
->system_values
);
738 /* Go through and clone functions */
739 foreach_list_typed(nir_function
, fxn
, node
, &s
->functions
)
740 clone_function(&state
, fxn
, ns
);
742 /* Only after all functions are cloned can we clone the actual function
743 * implementations. This is because nir_call_instrs need to reference the
744 * functions of other functions and we don't know what order the functions
745 * will have in the list.
747 nir_foreach_function(fxn
, s
) {
748 nir_function
*nfxn
= remap_global(&state
, fxn
);
749 nfxn
->impl
= clone_function_impl(&state
, fxn
->impl
);
750 nfxn
->impl
->function
= nfxn
;
754 ns
->info
.name
= ralloc_strdup(ns
, ns
->info
.name
);
756 ns
->info
.label
= ralloc_strdup(ns
, ns
->info
.label
);
758 ns
->num_inputs
= s
->num_inputs
;
759 ns
->num_uniforms
= s
->num_uniforms
;
760 ns
->num_outputs
= s
->num_outputs
;
761 ns
->num_shared
= s
->num_shared
;
762 ns
->scratch_size
= s
->scratch_size
;
764 ns
->constant_data_size
= s
->constant_data_size
;
765 if (s
->constant_data_size
> 0) {
766 ns
->constant_data
= ralloc_size(ns
, s
->constant_data_size
);
767 memcpy(ns
->constant_data
, s
->constant_data
, s
->constant_data_size
);
770 free_clone_state(&state
);
775 /** Overwrites dst and replaces its contents with src
777 * Everything ralloc parented to dst and src itself (but not its children)
780 * This should only be used by test code which needs to swap out shaders with
781 * a cloned or deserialized version.
784 nir_shader_replace(nir_shader
*dst
, nir_shader
*src
)
786 /* Delete all of dest's ralloc children */
787 void *dead_ctx
= ralloc_context(NULL
);
788 ralloc_adopt(dead_ctx
, dst
);
789 ralloc_free(dead_ctx
);
791 /* Re-parent all of src's ralloc children to dst */
792 ralloc_adopt(dst
, src
);
794 memcpy(dst
, src
, sizeof(*dst
));
796 /* We have to move all the linked lists over separately because we need the
797 * pointers in the list elements to point to the lists in dst and not src.
799 exec_list_move_nodes_to(&src
->uniforms
, &dst
->uniforms
);
800 exec_list_move_nodes_to(&src
->inputs
, &dst
->inputs
);
801 exec_list_move_nodes_to(&src
->outputs
, &dst
->outputs
);
802 exec_list_move_nodes_to(&src
->shared
, &dst
->shared
);
803 exec_list_move_nodes_to(&src
->globals
, &dst
->globals
);
804 exec_list_move_nodes_to(&src
->system_values
, &dst
->system_values
);
806 /* Now move the functions over. This takes a tiny bit more work */
807 exec_list_move_nodes_to(&src
->functions
, &dst
->functions
);
808 nir_foreach_function(function
, dst
)
809 function
->shader
= dst
;