Path: blob/21.2-virgl/src/compiler/glsl/ir_clone.cpp
4545 views
/*1* Copyright © 2010 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*/2223#include <string.h>24#include "util/compiler.h"25#include "ir.h"26#include "compiler/glsl_types.h"27#include "util/hash_table.h"2829ir_rvalue *30ir_rvalue::clone(void *mem_ctx, struct hash_table *) const31{32/* The only possible instantiation is the generic error value. */33return error_value(mem_ctx);34}3536/**37* Duplicate an IR variable38*/39ir_variable *40ir_variable::clone(void *mem_ctx, struct hash_table *ht) const41{42ir_variable *var = new(mem_ctx) ir_variable(this->type, this->name,43(ir_variable_mode) this->data.mode);4445var->data.max_array_access = this->data.max_array_access;46if (this->is_interface_instance()) {47var->u.max_ifc_array_access =48rzalloc_array(var, int, this->interface_type->length);49memcpy(var->u.max_ifc_array_access, this->u.max_ifc_array_access,50this->interface_type->length * sizeof(unsigned));51}5253memcpy(&var->data, &this->data, sizeof(var->data));5455if (this->get_state_slots()) {56ir_state_slot *s = var->allocate_state_slots(this->get_num_state_slots());57memcpy(s, this->get_state_slots(),58sizeof(s[0]) * var->get_num_state_slots());59}6061if (this->constant_value)62var->constant_value = this->constant_value->clone(mem_ctx, ht);6364if (this->constant_initializer)65var->constant_initializer =66this->constant_initializer->clone(mem_ctx, ht);6768var->interface_type = this->interface_type;6970if (ht)71_mesa_hash_table_insert(ht, (void *)const_cast<ir_variable *>(this), var);7273return var;74}7576ir_swizzle *77ir_swizzle::clone(void *mem_ctx, struct hash_table *ht) const78{79return new(mem_ctx) ir_swizzle(this->val->clone(mem_ctx, ht), this->mask);80}8182ir_return *83ir_return::clone(void *mem_ctx, struct hash_table *ht) const84{85ir_rvalue *new_value = NULL;8687if (this->value)88new_value = this->value->clone(mem_ctx, ht);8990return new(mem_ctx) ir_return(new_value);91}9293ir_discard *94ir_discard::clone(void *mem_ctx, struct hash_table *ht) const95{96ir_rvalue *new_condition = NULL;9798if (this->condition != NULL)99new_condition = this->condition->clone(mem_ctx, ht);100101return new(mem_ctx) ir_discard(new_condition);102}103104ir_demote *105ir_demote::clone(void *mem_ctx, struct hash_table *ht) const106{107return new(mem_ctx) ir_demote();108}109110ir_loop_jump *111ir_loop_jump::clone(void *mem_ctx, struct hash_table *ht) const112{113(void)ht;114115return new(mem_ctx) ir_loop_jump(this->mode);116}117118ir_if *119ir_if::clone(void *mem_ctx, struct hash_table *ht) const120{121ir_if *new_if = new(mem_ctx) ir_if(this->condition->clone(mem_ctx, ht));122123foreach_in_list(ir_instruction, ir, &this->then_instructions) {124new_if->then_instructions.push_tail(ir->clone(mem_ctx, ht));125}126127foreach_in_list(ir_instruction, ir, &this->else_instructions) {128new_if->else_instructions.push_tail(ir->clone(mem_ctx, ht));129}130131return new_if;132}133134ir_loop *135ir_loop::clone(void *mem_ctx, struct hash_table *ht) const136{137ir_loop *new_loop = new(mem_ctx) ir_loop();138139foreach_in_list(ir_instruction, ir, &this->body_instructions) {140new_loop->body_instructions.push_tail(ir->clone(mem_ctx, ht));141}142143return new_loop;144}145146ir_call *147ir_call::clone(void *mem_ctx, struct hash_table *ht) const148{149ir_dereference_variable *new_return_ref = NULL;150if (this->return_deref != NULL)151new_return_ref = this->return_deref->clone(mem_ctx, ht);152153exec_list new_parameters;154155foreach_in_list(ir_instruction, ir, &this->actual_parameters) {156new_parameters.push_tail(ir->clone(mem_ctx, ht));157}158159return new(mem_ctx) ir_call(this->callee, new_return_ref, &new_parameters);160}161162ir_expression *163ir_expression::clone(void *mem_ctx, struct hash_table *ht) const164{165ir_rvalue *op[ARRAY_SIZE(this->operands)] = { NULL, };166unsigned int i;167168for (i = 0; i < num_operands; i++) {169op[i] = this->operands[i]->clone(mem_ctx, ht);170}171172return new(mem_ctx) ir_expression(this->operation, this->type,173op[0], op[1], op[2], op[3]);174}175176ir_dereference_variable *177ir_dereference_variable::clone(void *mem_ctx, struct hash_table *ht) const178{179ir_variable *new_var;180181if (ht) {182hash_entry *entry = _mesa_hash_table_search(ht, this->var);183new_var = entry ? (ir_variable *) entry->data : this->var;184} else {185new_var = this->var;186}187188return new(mem_ctx) ir_dereference_variable(new_var);189}190191ir_dereference_array *192ir_dereference_array::clone(void *mem_ctx, struct hash_table *ht) const193{194return new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, ht),195this->array_index->clone(mem_ctx,196ht));197}198199ir_dereference_record *200ir_dereference_record::clone(void *mem_ctx, struct hash_table *ht) const201{202assert(this->field_idx >= 0);203const char *field_name =204this->record->type->fields.structure[this->field_idx].name;205return new(mem_ctx) ir_dereference_record(this->record->clone(mem_ctx, ht),206field_name);207}208209ir_texture *210ir_texture::clone(void *mem_ctx, struct hash_table *ht) const211{212ir_texture *new_tex = new(mem_ctx) ir_texture(this->op);213new_tex->type = this->type;214215new_tex->sampler = this->sampler->clone(mem_ctx, ht);216if (this->coordinate)217new_tex->coordinate = this->coordinate->clone(mem_ctx, ht);218if (this->projector)219new_tex->projector = this->projector->clone(mem_ctx, ht);220if (this->shadow_comparator) {221new_tex->shadow_comparator = this->shadow_comparator->clone(mem_ctx, ht);222}223224if (this->offset != NULL)225new_tex->offset = this->offset->clone(mem_ctx, ht);226227switch (this->op) {228case ir_tex:229case ir_lod:230case ir_query_levels:231case ir_texture_samples:232case ir_samples_identical:233break;234case ir_txb:235new_tex->lod_info.bias = this->lod_info.bias->clone(mem_ctx, ht);236break;237case ir_txl:238case ir_txf:239case ir_txs:240new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);241break;242case ir_txf_ms:243new_tex->lod_info.sample_index = this->lod_info.sample_index->clone(mem_ctx, ht);244break;245case ir_txd:246new_tex->lod_info.grad.dPdx = this->lod_info.grad.dPdx->clone(mem_ctx, ht);247new_tex->lod_info.grad.dPdy = this->lod_info.grad.dPdy->clone(mem_ctx, ht);248break;249case ir_tg4:250new_tex->lod_info.component = this->lod_info.component->clone(mem_ctx, ht);251break;252}253254return new_tex;255}256257ir_assignment *258ir_assignment::clone(void *mem_ctx, struct hash_table *ht) const259{260ir_rvalue *new_condition = NULL;261262if (this->condition)263new_condition = this->condition->clone(mem_ctx, ht);264265ir_assignment *cloned =266new(mem_ctx) ir_assignment(this->lhs->clone(mem_ctx, ht),267this->rhs->clone(mem_ctx, ht),268new_condition);269cloned->write_mask = this->write_mask;270return cloned;271}272273ir_function *274ir_function::clone(void *mem_ctx, struct hash_table *ht) const275{276ir_function *copy = new(mem_ctx) ir_function(this->name);277278copy->is_subroutine = this->is_subroutine;279copy->subroutine_index = this->subroutine_index;280copy->num_subroutine_types = this->num_subroutine_types;281copy->subroutine_types = ralloc_array(mem_ctx, const struct glsl_type *, copy->num_subroutine_types);282for (int i = 0; i < copy->num_subroutine_types; i++)283copy->subroutine_types[i] = this->subroutine_types[i];284285foreach_in_list(const ir_function_signature, sig, &this->signatures) {286ir_function_signature *sig_copy = sig->clone(mem_ctx, ht);287copy->add_signature(sig_copy);288289if (ht != NULL) {290_mesa_hash_table_insert(ht,291(void *)const_cast<ir_function_signature *>(sig), sig_copy);292}293}294295return copy;296}297298ir_function_signature *299ir_function_signature::clone(void *mem_ctx, struct hash_table *ht) const300{301ir_function_signature *copy = this->clone_prototype(mem_ctx, ht);302303copy->is_defined = this->is_defined;304305/* Clone the instruction list.306*/307foreach_in_list(const ir_instruction, inst, &this->body) {308ir_instruction *const inst_copy = inst->clone(mem_ctx, ht);309copy->body.push_tail(inst_copy);310}311312return copy;313}314315ir_function_signature *316ir_function_signature::clone_prototype(void *mem_ctx, struct hash_table *ht) const317{318ir_function_signature *copy =319new(mem_ctx) ir_function_signature(this->return_type);320321copy->is_defined = false;322copy->builtin_avail = this->builtin_avail;323copy->origin = this;324325/* Clone the parameter list, but NOT the body.326*/327foreach_in_list(const ir_variable, param, &this->parameters) {328assert(const_cast<ir_variable *>(param)->as_variable() != NULL);329330ir_variable *const param_copy = param->clone(mem_ctx, ht);331copy->parameters.push_tail(param_copy);332}333334return copy;335}336337ir_constant *338ir_constant::clone(void *mem_ctx, struct hash_table *ht) const339{340(void)ht;341342switch (this->type->base_type) {343case GLSL_TYPE_UINT:344case GLSL_TYPE_INT:345case GLSL_TYPE_FLOAT:346case GLSL_TYPE_FLOAT16:347case GLSL_TYPE_DOUBLE:348case GLSL_TYPE_BOOL:349case GLSL_TYPE_UINT64:350case GLSL_TYPE_INT64:351case GLSL_TYPE_UINT16:352case GLSL_TYPE_INT16:353case GLSL_TYPE_UINT8:354case GLSL_TYPE_INT8:355case GLSL_TYPE_SAMPLER:356case GLSL_TYPE_IMAGE:357return new(mem_ctx) ir_constant(this->type, &this->value);358359case GLSL_TYPE_STRUCT:360case GLSL_TYPE_ARRAY: {361ir_constant *c = new(mem_ctx) ir_constant;362363c->type = this->type;364c->const_elements = ralloc_array(c, ir_constant *, this->type->length);365for (unsigned i = 0; i < this->type->length; i++) {366c->const_elements[i] = this->const_elements[i]->clone(mem_ctx, NULL);367}368return c;369}370371case GLSL_TYPE_ATOMIC_UINT:372case GLSL_TYPE_VOID:373case GLSL_TYPE_ERROR:374case GLSL_TYPE_SUBROUTINE:375case GLSL_TYPE_INTERFACE:376case GLSL_TYPE_FUNCTION:377assert(!"Should not get here.");378break;379}380381return NULL;382}383384385class fixup_ir_call_visitor : public ir_hierarchical_visitor {386public:387fixup_ir_call_visitor(struct hash_table *ht)388{389this->ht = ht;390}391392virtual ir_visitor_status visit_enter(ir_call *ir)393{394/* Try to find the function signature referenced by the ir_call in the395* table. If it is found, replace it with the value from the table.396*/397ir_function_signature *sig;398hash_entry *entry = _mesa_hash_table_search(this->ht, ir->callee);399400if (entry != NULL) {401sig = (ir_function_signature *) entry->data;402ir->callee = sig;403}404405/* Since this may be used before function call parameters are flattened,406* the children also need to be processed.407*/408return visit_continue;409}410411private:412struct hash_table *ht;413};414415416static void417fixup_function_calls(struct hash_table *ht, exec_list *instructions)418{419fixup_ir_call_visitor v(ht);420v.run(instructions);421}422423424void425clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in)426{427struct hash_table *ht = _mesa_pointer_hash_table_create(NULL);428429foreach_in_list(const ir_instruction, original, in) {430ir_instruction *copy = original->clone(mem_ctx, ht);431432out->push_tail(copy);433}434435/* Make a pass over the cloned tree to fix up ir_call nodes to point to the436* cloned ir_function_signature nodes. This cannot be done automatically437* during cloning because the ir_call might be a forward reference (i.e.,438* the function signature that it references may not have been cloned yet).439*/440fixup_function_calls(ht, out);441442_mesa_hash_table_destroy(ht, NULL);443}444445446