mirror of OpenBSD xenocara tree github.com/openbsd/xenocara
openbsd
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at jcs 865 lines 27 kB view raw
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.h" 26#include "nir_xfb_info.h" 27 28/* Secret Decoder Ring: 29 * clone_foo(): 30 * Allocate and clone a foo. 31 * __clone_foo(): 32 * Clone body of foo (ie. parent class, embedded struct, etc) 33 */ 34 35typedef struct { 36 /* True if we are cloning an entire shader. */ 37 bool global_clone; 38 39 /* If true allows the clone operation to fall back to the original pointer 40 * if no clone pointer is found in the remap table. This allows us to 41 * clone a loop body without having to add srcs from outside the loop to 42 * the remap table. This is useful for loop unrolling. 43 */ 44 bool allow_remap_fallback; 45 46 /* maps orig ptr -> cloned ptr: */ 47 struct hash_table *remap_table; 48 49 /* List of phi sources. */ 50 struct list_head phi_srcs; 51 52 /* new shader object, used as memctx for just about everything else: */ 53 nir_shader *ns; 54} clone_state; 55 56static void 57init_clone_state(clone_state *state, struct hash_table *remap_table, 58 bool global, bool allow_remap_fallback) 59{ 60 state->global_clone = global; 61 state->allow_remap_fallback = allow_remap_fallback; 62 63 if (remap_table) { 64 state->remap_table = remap_table; 65 } else { 66 state->remap_table = _mesa_pointer_hash_table_create(NULL); 67 } 68 69 list_inithead(&state->phi_srcs); 70} 71 72static void 73free_clone_state(clone_state *state) 74{ 75 _mesa_hash_table_destroy(state->remap_table, NULL); 76} 77 78static inline void * 79_lookup_ptr(clone_state *state, const void *ptr, bool global) 80{ 81 struct hash_entry *entry; 82 83 if (!ptr) 84 return NULL; 85 86 if (!state->global_clone && global) 87 return (void *)ptr; 88 89 if (unlikely(!state->remap_table)) { 90 assert(state->allow_remap_fallback); 91 return (void *)ptr; 92 } 93 94 entry = _mesa_hash_table_search(state->remap_table, ptr); 95 if (!entry) { 96 assert(state->allow_remap_fallback); 97 return (void *)ptr; 98 } 99 100 return entry->data; 101} 102 103static void 104add_remap(clone_state *state, void *nptr, const void *ptr) 105{ 106 _mesa_hash_table_insert(state->remap_table, ptr, nptr); 107} 108 109static void * 110remap_local(clone_state *state, const void *ptr) 111{ 112 return _lookup_ptr(state, ptr, false); 113} 114 115static void * 116remap_global(clone_state *state, const void *ptr) 117{ 118 return _lookup_ptr(state, ptr, true); 119} 120 121static nir_variable * 122remap_var(clone_state *state, const nir_variable *var) 123{ 124 return _lookup_ptr(state, var, nir_variable_is_global(var)); 125} 126 127nir_constant * 128nir_constant_clone(const nir_constant *c, nir_variable *nvar) 129{ 130 nir_constant *nc = ralloc(nvar, nir_constant); 131 132 memcpy(nc->values, c->values, sizeof(nc->values)); 133 nc->is_null_constant = c->is_null_constant; 134 nc->num_elements = c->num_elements; 135 nc->elements = ralloc_array(nvar, nir_constant *, c->num_elements); 136 for (unsigned i = 0; i < c->num_elements; i++) { 137 nc->elements[i] = nir_constant_clone(c->elements[i], nvar); 138 } 139 140 return nc; 141} 142 143/* NOTE: for cloning nir_variables, bypass nir_variable_create to avoid 144 * having to deal with locals and globals separately: 145 */ 146nir_variable * 147nir_variable_clone(const nir_variable *var, nir_shader *shader) 148{ 149 nir_variable *nvar = rzalloc(shader, nir_variable); 150 151 nvar->type = var->type; 152 nvar->name = ralloc_strdup(nvar, var->name); 153 nvar->data = var->data; 154 nvar->num_state_slots = var->num_state_slots; 155 if (var->num_state_slots) { 156 nvar->state_slots = ralloc_array(nvar, nir_state_slot, var->num_state_slots); 157 memcpy(nvar->state_slots, var->state_slots, 158 var->num_state_slots * sizeof(nir_state_slot)); 159 } 160 if (var->constant_initializer) { 161 nvar->constant_initializer = 162 nir_constant_clone(var->constant_initializer, nvar); 163 } 164 nvar->interface_type = var->interface_type; 165 166 if (var->max_ifc_array_access) { 167 nvar->max_ifc_array_access = 168 rzalloc_array(nvar, int, var->interface_type->length); 169 memcpy(nvar->max_ifc_array_access, var->max_ifc_array_access, 170 var->interface_type->length * sizeof(unsigned)); 171 } 172 173 nvar->num_members = var->num_members; 174 if (var->num_members) { 175 nvar->members = ralloc_array(nvar, struct nir_variable_data, 176 var->num_members); 177 memcpy(nvar->members, var->members, 178 var->num_members * sizeof(*var->members)); 179 } 180 181 return nvar; 182} 183 184static nir_variable * 185clone_variable(clone_state *state, const nir_variable *var) 186{ 187 nir_variable *nvar = nir_variable_clone(var, state->ns); 188 add_remap(state, nvar, var); 189 190 return nvar; 191} 192 193/* clone list of nir_variable: */ 194static void 195clone_var_list(clone_state *state, struct exec_list *dst, 196 const struct exec_list *list) 197{ 198 exec_list_make_empty(dst); 199 foreach_list_typed(nir_variable, var, node, list) { 200 nir_variable *nvar = clone_variable(state, var); 201 exec_list_push_tail(dst, &nvar->node); 202 } 203} 204 205static void 206__clone_src(clone_state *state, void *ninstr_or_if, 207 nir_src *nsrc, const nir_src *src) 208{ 209 nsrc->ssa = remap_local(state, src->ssa); 210} 211 212static void 213__clone_def(clone_state *state, nir_instr *ninstr, 214 nir_def *ndef, const nir_def *def) 215{ 216 nir_def_init(ninstr, ndef, def->num_components, def->bit_size); 217 if (likely(state->remap_table)) 218 add_remap(state, ndef, def); 219} 220 221static nir_alu_instr * 222clone_alu(clone_state *state, const nir_alu_instr *alu) 223{ 224 nir_alu_instr *nalu = nir_alu_instr_create(state->ns, alu->op); 225 nalu->exact = alu->exact; 226 nalu->fp_fast_math = alu->fp_fast_math; 227 nalu->no_signed_wrap = alu->no_signed_wrap; 228 nalu->no_unsigned_wrap = alu->no_unsigned_wrap; 229 230 __clone_def(state, &nalu->instr, &nalu->def, &alu->def); 231 232 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) { 233 __clone_src(state, &nalu->instr, &nalu->src[i].src, &alu->src[i].src); 234 memcpy(nalu->src[i].swizzle, alu->src[i].swizzle, 235 sizeof(nalu->src[i].swizzle)); 236 } 237 238 return nalu; 239} 240 241nir_alu_instr * 242nir_alu_instr_clone(nir_shader *shader, const nir_alu_instr *orig) 243{ 244 clone_state state = { 245 .allow_remap_fallback = true, 246 .ns = shader, 247 }; 248 return clone_alu(&state, orig); 249} 250 251static nir_deref_instr * 252clone_deref_instr(clone_state *state, const nir_deref_instr *deref) 253{ 254 nir_deref_instr *nderef = 255 nir_deref_instr_create(state->ns, deref->deref_type); 256 257 __clone_def(state, &nderef->instr, &nderef->def, &deref->def); 258 259 nderef->modes = deref->modes; 260 nderef->type = deref->type; 261 262 if (deref->deref_type == nir_deref_type_var) { 263 nderef->var = remap_var(state, deref->var); 264 return nderef; 265 } 266 267 __clone_src(state, &nderef->instr, &nderef->parent, &deref->parent); 268 269 switch (deref->deref_type) { 270 case nir_deref_type_struct: 271 nderef->strct.index = deref->strct.index; 272 break; 273 274 case nir_deref_type_array: 275 case nir_deref_type_ptr_as_array: 276 __clone_src(state, &nderef->instr, 277 &nderef->arr.index, &deref->arr.index); 278 nderef->arr.in_bounds = deref->arr.in_bounds; 279 break; 280 281 case nir_deref_type_array_wildcard: 282 /* Nothing to do */ 283 break; 284 285 case nir_deref_type_cast: 286 nderef->cast.ptr_stride = deref->cast.ptr_stride; 287 nderef->cast.align_mul = deref->cast.align_mul; 288 nderef->cast.align_offset = deref->cast.align_offset; 289 break; 290 291 default: 292 unreachable("Invalid instruction deref type"); 293 } 294 295 return nderef; 296} 297 298static nir_intrinsic_instr * 299clone_intrinsic(clone_state *state, const nir_intrinsic_instr *itr) 300{ 301 nir_intrinsic_instr *nitr = 302 nir_intrinsic_instr_create(state->ns, itr->intrinsic); 303 304 unsigned num_srcs = nir_intrinsic_infos[itr->intrinsic].num_srcs; 305 306 if (nir_intrinsic_infos[itr->intrinsic].has_dest) 307 __clone_def(state, &nitr->instr, &nitr->def, &itr->def); 308 309 nitr->num_components = itr->num_components; 310 memcpy(nitr->const_index, itr->const_index, sizeof(nitr->const_index)); 311 nitr->name = ralloc_strdup(state->ns, itr->name); 312 313 for (unsigned i = 0; i < num_srcs; i++) 314 __clone_src(state, &nitr->instr, &nitr->src[i], &itr->src[i]); 315 316 return nitr; 317} 318 319static nir_load_const_instr * 320clone_load_const(clone_state *state, const nir_load_const_instr *lc) 321{ 322 nir_load_const_instr *nlc = 323 nir_load_const_instr_create(state->ns, lc->def.num_components, 324 lc->def.bit_size); 325 326 memcpy(&nlc->value, &lc->value, sizeof(*nlc->value) * lc->def.num_components); 327 328 if (likely(state->remap_table)) 329 add_remap(state, &nlc->def, &lc->def); 330 331 return nlc; 332} 333 334static nir_undef_instr * 335clone_ssa_undef(clone_state *state, const nir_undef_instr *sa) 336{ 337 nir_undef_instr *nsa = 338 nir_undef_instr_create(state->ns, sa->def.num_components, 339 sa->def.bit_size); 340 341 if (likely(state->remap_table)) 342 add_remap(state, &nsa->def, &sa->def); 343 344 return nsa; 345} 346 347static nir_tex_instr * 348clone_tex(clone_state *state, const nir_tex_instr *tex) 349{ 350 nir_tex_instr *ntex = nir_tex_instr_create(state->ns, tex->num_srcs); 351 352 ntex->sampler_dim = tex->sampler_dim; 353 ntex->dest_type = tex->dest_type; 354 ntex->op = tex->op; 355 __clone_def(state, &ntex->instr, &ntex->def, &tex->def); 356 for (unsigned i = 0; i < ntex->num_srcs; i++) { 357 ntex->src[i].src_type = tex->src[i].src_type; 358 __clone_src(state, &ntex->instr, &ntex->src[i].src, &tex->src[i].src); 359 } 360 ntex->coord_components = tex->coord_components; 361 ntex->is_array = tex->is_array; 362 ntex->array_is_lowered_cube = tex->array_is_lowered_cube; 363 ntex->is_shadow = tex->is_shadow; 364 ntex->is_new_style_shadow = tex->is_new_style_shadow; 365 ntex->is_sparse = tex->is_sparse; 366 ntex->component = tex->component; 367 memcpy(ntex->tg4_offsets, tex->tg4_offsets, sizeof(tex->tg4_offsets)); 368 369 ntex->texture_index = tex->texture_index; 370 ntex->sampler_index = tex->sampler_index; 371 372 ntex->texture_non_uniform = tex->texture_non_uniform; 373 ntex->sampler_non_uniform = tex->sampler_non_uniform; 374 375 ntex->backend_flags = tex->backend_flags; 376 377 return ntex; 378} 379 380static nir_phi_instr * 381clone_phi(clone_state *state, const nir_phi_instr *phi, nir_block *nblk) 382{ 383 nir_phi_instr *nphi = nir_phi_instr_create(state->ns); 384 385 __clone_def(state, &nphi->instr, &nphi->def, &phi->def); 386 387 /* Cloning a phi node is a bit different from other instructions. The 388 * sources of phi instructions are the only time where we can use an SSA 389 * def before it is defined. In order to handle this, we just copy over 390 * the sources from the old phi instruction directly and then fix them up 391 * in a second pass once all the instrutions in the function have been 392 * properly cloned. 393 * 394 * In order to ensure that the copied sources (which are the same as the 395 * old phi instruction's sources for now) don't get inserted into the old 396 * shader's use-def lists, we have to add the phi instruction *before* we 397 * set up its sources. 398 */ 399 nir_instr_insert_after_block(nblk, &nphi->instr); 400 401 nir_foreach_phi_src(src, phi) { 402 nir_phi_src *nsrc = nir_phi_instr_add_src(nphi, src->pred, src->src.ssa); 403 404 /* Stash it in the list of phi sources. We'll walk this list and fix up 405 * sources at the very end of clone_function_impl. 406 */ 407 list_add(&nsrc->src.use_link, &state->phi_srcs); 408 } 409 410 return nphi; 411} 412 413static nir_jump_instr * 414clone_jump(clone_state *state, const nir_jump_instr *jmp) 415{ 416 /* These aren't handled because they require special block linking */ 417 assert(jmp->type != nir_jump_goto && jmp->type != nir_jump_goto_if); 418 419 nir_jump_instr *njmp = nir_jump_instr_create(state->ns, jmp->type); 420 421 return njmp; 422} 423 424static nir_call_instr * 425clone_call(clone_state *state, const nir_call_instr *call) 426{ 427 nir_function *ncallee = remap_global(state, call->callee); 428 nir_call_instr *ncall = nir_call_instr_create(state->ns, ncallee); 429 430 for (unsigned i = 0; i < ncall->num_params; i++) 431 __clone_src(state, ncall, &ncall->params[i], &call->params[i]); 432 433 return ncall; 434} 435 436static nir_debug_info_instr * 437clone_debug_info(clone_state *state, nir_debug_info_instr *di) 438{ 439 nir_debug_info_instr *instr = 440 nir_debug_info_instr_create(state->ns, di->type, di->string_length); 441 442 switch (di->type) { 443 case nir_debug_info_src_loc: 444 if (di->src_loc.line) 445 __clone_src(state, instr, &instr->src_loc.filename, &di->src_loc.filename); 446 instr->src_loc.line = di->src_loc.line; 447 instr->src_loc.column = di->src_loc.column; 448 instr->src_loc.spirv_offset = di->src_loc.spirv_offset; 449 instr->src_loc.source = di->src_loc.source; 450 return instr; 451 case nir_debug_info_string: 452 memcpy(instr->string, di->string, di->string_length); 453 __clone_def(state, &instr->instr, &instr->def, &di->def); 454 return instr; 455 } 456 457 unreachable("Unimplemented nir_debug_info_type"); 458} 459 460static nir_instr * 461clone_instr(clone_state *state, const nir_instr *instr) 462{ 463 switch (instr->type) { 464 case nir_instr_type_alu: 465 return &clone_alu(state, nir_instr_as_alu(instr))->instr; 466 case nir_instr_type_deref: 467 return &clone_deref_instr(state, nir_instr_as_deref(instr))->instr; 468 case nir_instr_type_intrinsic: 469 return &clone_intrinsic(state, nir_instr_as_intrinsic(instr))->instr; 470 case nir_instr_type_load_const: 471 return &clone_load_const(state, nir_instr_as_load_const(instr))->instr; 472 case nir_instr_type_undef: 473 return &clone_ssa_undef(state, nir_instr_as_undef(instr))->instr; 474 case nir_instr_type_tex: 475 return &clone_tex(state, nir_instr_as_tex(instr))->instr; 476 case nir_instr_type_phi: 477 unreachable("Cannot clone phis with clone_instr"); 478 case nir_instr_type_jump: 479 return &clone_jump(state, nir_instr_as_jump(instr))->instr; 480 case nir_instr_type_call: 481 return &clone_call(state, nir_instr_as_call(instr))->instr; 482 case nir_instr_type_debug_info: 483 return &clone_debug_info(state, nir_instr_as_debug_info(instr))->instr; 484 case nir_instr_type_parallel_copy: 485 unreachable("Cannot clone parallel copies"); 486 default: 487 unreachable("bad instr type"); 488 return NULL; 489 } 490} 491 492nir_instr * 493nir_instr_clone(nir_shader *shader, const nir_instr *orig) 494{ 495 clone_state state = { 496 .allow_remap_fallback = true, 497 .ns = shader, 498 }; 499 return clone_instr(&state, orig); 500} 501 502nir_instr * 503nir_instr_clone_deep(nir_shader *shader, const nir_instr *orig, 504 struct hash_table *remap_table) 505{ 506 clone_state state = { 507 .allow_remap_fallback = true, 508 .ns = shader, 509 .remap_table = remap_table, 510 }; 511 return clone_instr(&state, orig); 512} 513 514static nir_block * 515clone_block(clone_state *state, struct exec_list *cf_list, const nir_block *blk) 516{ 517 /* Don't actually create a new block. Just use the one from the tail of 518 * the list. NIR guarantees that the tail of the list is a block and that 519 * no two blocks are side-by-side in the IR; It should be empty. 520 */ 521 nir_block *nblk = 522 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node); 523 assert(nblk->cf_node.type == nir_cf_node_block); 524 assert(exec_list_is_empty(&nblk->instr_list)); 525 526 /* We need this for phi sources */ 527 add_remap(state, nblk, blk); 528 529 nir_foreach_instr(instr, blk) { 530 if (instr->type == nir_instr_type_phi) { 531 /* Phi instructions are a bit of a special case when cloning because 532 * we don't want inserting the instruction to automatically handle 533 * use/defs for us. Instead, we need to wait until all the 534 * blocks/instructions are in so that we can set their sources up. 535 */ 536 clone_phi(state, nir_instr_as_phi(instr), nblk); 537 } else { 538 nir_instr *ninstr = clone_instr(state, instr); 539 nir_instr_insert_after_block(nblk, ninstr); 540 } 541 } 542 543 return nblk; 544} 545 546static void 547clone_cf_list(clone_state *state, struct exec_list *dst, 548 const struct exec_list *list); 549 550static nir_if * 551clone_if(clone_state *state, struct exec_list *cf_list, const nir_if *i) 552{ 553 nir_if *ni = nir_if_create(state->ns); 554 ni->control = i->control; 555 556 __clone_src(state, ni, &ni->condition, &i->condition); 557 558 nir_cf_node_insert_end(cf_list, &ni->cf_node); 559 560 clone_cf_list(state, &ni->then_list, &i->then_list); 561 clone_cf_list(state, &ni->else_list, &i->else_list); 562 563 return ni; 564} 565 566static nir_loop * 567clone_loop(clone_state *state, struct exec_list *cf_list, const nir_loop *loop) 568{ 569 nir_loop *nloop = nir_loop_create(state->ns); 570 nloop->control = loop->control; 571 nloop->partially_unrolled = loop->partially_unrolled; 572 573 nir_cf_node_insert_end(cf_list, &nloop->cf_node); 574 575 clone_cf_list(state, &nloop->body, &loop->body); 576 if (nir_loop_has_continue_construct(loop)) { 577 nir_loop_add_continue_construct(nloop); 578 clone_cf_list(state, &nloop->continue_list, &loop->continue_list); 579 } 580 581 return nloop; 582} 583 584/* clone list of nir_cf_node: */ 585static void 586clone_cf_list(clone_state *state, struct exec_list *dst, 587 const struct exec_list *list) 588{ 589 foreach_list_typed(nir_cf_node, cf, node, list) { 590 switch (cf->type) { 591 case nir_cf_node_block: 592 clone_block(state, dst, nir_cf_node_as_block(cf)); 593 break; 594 case nir_cf_node_if: 595 clone_if(state, dst, nir_cf_node_as_if(cf)); 596 break; 597 case nir_cf_node_loop: 598 clone_loop(state, dst, nir_cf_node_as_loop(cf)); 599 break; 600 default: 601 unreachable("bad cf type"); 602 } 603 } 604} 605 606/* After we've cloned almost everything, we have to walk the list of phi 607 * sources and fix them up. Thanks to loops, the block and SSA value for a 608 * phi source may not be defined when we first encounter it. Instead, we 609 * add it to the phi_srcs list and we fix it up here. 610 */ 611static void 612fixup_phi_srcs(clone_state *state) 613{ 614 list_for_each_entry_safe(nir_phi_src, src, &state->phi_srcs, src.use_link) { 615 src->pred = remap_local(state, src->pred); 616 617 /* Remove from this list */ 618 list_del(&src->src.use_link); 619 620 src->src.ssa = remap_local(state, src->src.ssa); 621 list_addtail(&src->src.use_link, &src->src.ssa->uses); 622 } 623 assert(list_is_empty(&state->phi_srcs)); 624} 625 626void 627nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent, 628 struct hash_table *remap_table) 629{ 630 exec_list_make_empty(&dst->list); 631 dst->impl = src->impl; 632 633 if (exec_list_is_empty(&src->list)) 634 return; 635 636 clone_state state; 637 init_clone_state(&state, remap_table, false, true); 638 639 /* We use the same shader */ 640 state.ns = src->impl->function->shader; 641 642 /* The control-flow code assumes that the list of cf_nodes always starts 643 * and ends with a block. We start by adding an empty block. 644 */ 645 nir_block *nblk = nir_block_create(state.ns); 646 nblk->cf_node.parent = parent; 647 exec_list_push_tail(&dst->list, &nblk->cf_node.node); 648 649 clone_cf_list(&state, &dst->list, &src->list); 650 651 fixup_phi_srcs(&state); 652 653 if (!remap_table) 654 free_clone_state(&state); 655} 656 657static nir_function_impl * 658clone_function_impl(clone_state *state, const nir_function_impl *fi) 659{ 660 nir_function_impl *nfi = nir_function_impl_create_bare(state->ns); 661 662 if (fi->preamble) 663 nfi->preamble = remap_global(state, fi->preamble); 664 665 clone_var_list(state, &nfi->locals, &fi->locals); 666 667 assert(list_is_empty(&state->phi_srcs)); 668 669 clone_cf_list(state, &nfi->body, &fi->body); 670 671 fixup_phi_srcs(state); 672 673 /* All metadata is invalidated in the cloning process */ 674 nfi->valid_metadata = 0; 675 676 return nfi; 677} 678 679nir_function_impl * 680nir_function_impl_clone_remap_globals(nir_shader *shader, 681 const nir_function_impl *fi, 682 struct hash_table *remap_table) 683{ 684 clone_state state; 685 init_clone_state(&state, remap_table, !!remap_table, false); 686 687 state.ns = shader; 688 689 nir_function_impl *nfi = clone_function_impl(&state, fi); 690 691 if (!remap_table) 692 free_clone_state(&state); 693 694 return nfi; 695} 696 697nir_function_impl * 698nir_function_impl_clone(nir_shader *shader, const nir_function_impl *fi) 699{ 700 return nir_function_impl_clone_remap_globals(shader, fi, NULL); 701} 702 703nir_function * 704nir_function_clone(nir_shader *ns, const nir_function *fxn) 705{ 706 nir_function *nfxn = nir_function_create(ns, fxn->name); 707 nfxn->num_params = fxn->num_params; 708 if (fxn->num_params) { 709 nfxn->params = ralloc_array(ns, nir_parameter, fxn->num_params); 710 memcpy(nfxn->params, fxn->params, sizeof(nir_parameter) * fxn->num_params); 711 712 for (unsigned i = 0; i < fxn->num_params; ++i) { 713 if (fxn->params[i].name) 714 nfxn->params[i].name = ralloc_strdup(ns, fxn->params[i].name); 715 } 716 } 717 nfxn->is_entrypoint = fxn->is_entrypoint; 718 nfxn->is_preamble = fxn->is_preamble; 719 nfxn->should_inline = fxn->should_inline; 720 nfxn->dont_inline = fxn->dont_inline; 721 nfxn->is_subroutine = fxn->is_subroutine; 722 nfxn->driver_attributes = fxn->driver_attributes; 723 nfxn->is_tmp_globals_wrapper = fxn->is_tmp_globals_wrapper; 724 nfxn->num_subroutine_types = fxn->num_subroutine_types; 725 nfxn->subroutine_index = fxn->subroutine_index; 726 nfxn->workgroup_size[0] = fxn->workgroup_size[0]; 727 nfxn->workgroup_size[1] = fxn->workgroup_size[1]; 728 nfxn->workgroup_size[2] = fxn->workgroup_size[2]; 729 if (fxn->num_subroutine_types) { 730 nfxn->subroutine_types = ralloc_array(ns, const struct glsl_type *, 731 fxn->num_subroutine_types); 732 for (unsigned i = 0; i < fxn->num_subroutine_types; i++) { 733 nfxn->subroutine_types[i] = fxn->subroutine_types[i]; 734 } 735 } 736 737 /* At first glance, it looks like we should clone the function_impl here. 738 * However, call instructions need to be able to reference at least the 739 * function and those will get processed as we clone the function_impls. 740 * We stop here and do function_impls as a second pass. 741 */ 742 return nfxn; 743} 744 745static nir_function * 746clone_function(clone_state *state, const nir_function *fxn, nir_shader *ns) 747{ 748 assert(ns == state->ns); 749 750 nir_function *nfxn = nir_function_clone(ns, fxn); 751 /* Needed for call instructions */ 752 add_remap(state, nfxn, fxn); 753 return nfxn; 754} 755 756static u_printf_info * 757clone_printf_info(void *mem_ctx, const nir_shader *s) 758{ 759 u_printf_info *infos = ralloc_array(mem_ctx, u_printf_info, s->printf_info_count); 760 761 for (unsigned i = 0; i < s->printf_info_count; i++) { 762 const u_printf_info *src_info = &s->printf_info[i]; 763 764 infos[i].num_args = src_info->num_args; 765 infos[i].arg_sizes = ralloc_memdup(mem_ctx, src_info->arg_sizes, 766 sizeof(infos[i].arg_sizes[0]) * src_info->num_args); 767 768 769 infos[i].string_size = src_info->string_size; 770 infos[i].strings = ralloc_memdup(mem_ctx, src_info->strings, 771 src_info->string_size); 772 } 773 774 return infos; 775} 776 777nir_shader * 778nir_shader_clone(void *mem_ctx, const nir_shader *s) 779{ 780 clone_state state; 781 init_clone_state(&state, NULL, true, false); 782 783 nir_shader *ns = nir_shader_create(mem_ctx, s->info.stage, s->options, NULL); 784 state.ns = ns; 785 786 clone_var_list(&state, &ns->variables, &s->variables); 787 788 /* Go through and clone functions */ 789 foreach_list_typed(nir_function, fxn, node, &s->functions) 790 clone_function(&state, fxn, ns); 791 792 /* Only after all functions are cloned can we clone the actual function 793 * implementations. This is because nir_call_instrs and preambles need to 794 * reference the functions of other functions and we don't know what order 795 * the functions will have in the list. 796 */ 797 nir_foreach_function_with_impl(fxn, impl, s) { 798 nir_function *nfxn = remap_global(&state, fxn); 799 nir_function_set_impl(nfxn, clone_function_impl(&state, impl)); 800 } 801 802 ns->info = s->info; 803 ns->info.name = ralloc_strdup(ns, ns->info.name); 804 if (ns->info.label) 805 ns->info.label = ralloc_strdup(ns, ns->info.label); 806 807 ns->num_inputs = s->num_inputs; 808 ns->num_uniforms = s->num_uniforms; 809 ns->num_outputs = s->num_outputs; 810 ns->scratch_size = s->scratch_size; 811 812 ns->constant_data_size = s->constant_data_size; 813 if (s->constant_data_size > 0) { 814 ns->constant_data = ralloc_memdup(ns, s->constant_data, 815 s->constant_data_size); 816 } 817 818 if (s->xfb_info) { 819 size_t size = nir_xfb_info_size(s->xfb_info->output_count); 820 ns->xfb_info = ralloc_memdup(ns, s->xfb_info, size); 821 } 822 823 if (s->printf_info_count > 0) { 824 ns->printf_info = clone_printf_info(ns, s); 825 ns->printf_info_count = s->printf_info_count; 826 } 827 828 free_clone_state(&state); 829 830 return ns; 831} 832 833/** Overwrites dst and replaces its contents with src 834 * 835 * Everything ralloc parented to dst and src itself (but not its children) 836 * will be freed. 837 * 838 * This should only be used by test code which needs to swap out shaders with 839 * a cloned or deserialized version. 840 */ 841void 842nir_shader_replace(nir_shader *dst, nir_shader *src) 843{ 844 /* Delete all of dest's ralloc children */ 845 void *dead_ctx = ralloc_context(NULL); 846 ralloc_adopt(dead_ctx, dst); 847 ralloc_free(dead_ctx); 848 849 /* Re-parent all of src's ralloc children to dst */ 850 ralloc_adopt(dst, src); 851 852 memcpy(dst, src, sizeof(*dst)); 853 854 /* We have to move all the linked lists over separately because we need the 855 * pointers in the list elements to point to the lists in dst and not src. 856 */ 857 exec_list_move_nodes_to(&src->variables, &dst->variables); 858 859 /* Now move the functions over. This takes a tiny bit more work */ 860 exec_list_move_nodes_to(&src->functions, &dst->functions); 861 nir_foreach_function(function, dst) 862 function->shader = dst; 863 864 ralloc_free(src); 865}