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