1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2010 Intel Corporation 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 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#include "glsl_symbol_table.h" 25b8e80941Smrg#include "glsl_parser_extras.h" 26b8e80941Smrg#include "ir.h" 27b8e80941Smrg#include "program.h" 28b8e80941Smrg#include "util/set.h" 29b8e80941Smrg#include "util/hash_table.h" 30b8e80941Smrg#include "linker.h" 31b8e80941Smrg#include "main/mtypes.h" 32b8e80941Smrg 33b8e80941Smrgstatic ir_function_signature * 34b8e80941Smrgfind_matching_signature(const char *name, const exec_list *actual_parameters, 35b8e80941Smrg glsl_symbol_table *symbols); 36b8e80941Smrg 37b8e80941Smrgnamespace { 38b8e80941Smrg 39b8e80941Smrgclass call_link_visitor : public ir_hierarchical_visitor { 40b8e80941Smrgpublic: 41b8e80941Smrg call_link_visitor(gl_shader_program *prog, gl_linked_shader *linked, 42b8e80941Smrg gl_shader **shader_list, unsigned num_shaders) 43b8e80941Smrg { 44b8e80941Smrg this->prog = prog; 45b8e80941Smrg this->shader_list = shader_list; 46b8e80941Smrg this->num_shaders = num_shaders; 47b8e80941Smrg this->success = true; 48b8e80941Smrg this->linked = linked; 49b8e80941Smrg 50b8e80941Smrg this->locals = _mesa_pointer_set_create(NULL); 51b8e80941Smrg } 52b8e80941Smrg 53b8e80941Smrg ~call_link_visitor() 54b8e80941Smrg { 55b8e80941Smrg _mesa_set_destroy(this->locals, NULL); 56b8e80941Smrg } 57b8e80941Smrg 58b8e80941Smrg virtual ir_visitor_status visit(ir_variable *ir) 59b8e80941Smrg { 60b8e80941Smrg _mesa_set_add(locals, ir); 61b8e80941Smrg return visit_continue; 62b8e80941Smrg } 63b8e80941Smrg 64b8e80941Smrg virtual ir_visitor_status visit_enter(ir_call *ir) 65b8e80941Smrg { 66b8e80941Smrg /* If ir is an ir_call from a function that was imported from another 67b8e80941Smrg * shader callee will point to an ir_function_signature in the original 68b8e80941Smrg * shader. In this case the function signature MUST NOT BE MODIFIED. 69b8e80941Smrg * Doing so will modify the original shader. This may prevent that 70b8e80941Smrg * shader from being linkable in other programs. 71b8e80941Smrg */ 72b8e80941Smrg const ir_function_signature *const callee = ir->callee; 73b8e80941Smrg assert(callee != NULL); 74b8e80941Smrg const char *const name = callee->function_name(); 75b8e80941Smrg 76b8e80941Smrg /* We don't actually need to find intrinsics; they're not real */ 77b8e80941Smrg if (callee->is_intrinsic()) 78b8e80941Smrg return visit_continue; 79b8e80941Smrg 80b8e80941Smrg /* Determine if the requested function signature already exists in the 81b8e80941Smrg * final linked shader. If it does, use it as the target of the call. 82b8e80941Smrg */ 83b8e80941Smrg ir_function_signature *sig = 84b8e80941Smrg find_matching_signature(name, &callee->parameters, linked->symbols); 85b8e80941Smrg if (sig != NULL) { 86b8e80941Smrg ir->callee = sig; 87b8e80941Smrg return visit_continue; 88b8e80941Smrg } 89b8e80941Smrg 90b8e80941Smrg /* Try to find the signature in one of the other shaders that is being 91b8e80941Smrg * linked. If it's not found there, return an error. 92b8e80941Smrg */ 93b8e80941Smrg for (unsigned i = 0; i < num_shaders; i++) { 94b8e80941Smrg sig = find_matching_signature(name, &ir->actual_parameters, 95b8e80941Smrg shader_list[i]->symbols); 96b8e80941Smrg if (sig) 97b8e80941Smrg break; 98b8e80941Smrg } 99b8e80941Smrg 100b8e80941Smrg if (sig == NULL) { 101b8e80941Smrg /* FINISHME: Log the full signature of unresolved function. 102b8e80941Smrg */ 103b8e80941Smrg linker_error(this->prog, "unresolved reference to function `%s'\n", 104b8e80941Smrg name); 105b8e80941Smrg this->success = false; 106b8e80941Smrg return visit_stop; 107b8e80941Smrg } 108b8e80941Smrg 109b8e80941Smrg /* Find the prototype information in the linked shader. Generate any 110b8e80941Smrg * details that may be missing. 111b8e80941Smrg */ 112b8e80941Smrg ir_function *f = linked->symbols->get_function(name); 113b8e80941Smrg if (f == NULL) { 114b8e80941Smrg f = new(linked) ir_function(name); 115b8e80941Smrg 116b8e80941Smrg /* Add the new function to the linked IR. Put it at the end 117b8e80941Smrg * so that it comes after any global variable declarations 118b8e80941Smrg * that it refers to. 119b8e80941Smrg */ 120b8e80941Smrg linked->symbols->add_function(f); 121b8e80941Smrg linked->ir->push_tail(f); 122b8e80941Smrg } 123b8e80941Smrg 124b8e80941Smrg ir_function_signature *linked_sig = 125b8e80941Smrg f->exact_matching_signature(NULL, &callee->parameters); 126b8e80941Smrg if (linked_sig == NULL) { 127b8e80941Smrg linked_sig = new(linked) ir_function_signature(callee->return_type); 128b8e80941Smrg f->add_signature(linked_sig); 129b8e80941Smrg } 130b8e80941Smrg 131b8e80941Smrg /* At this point linked_sig and called may be the same. If ir is an 132b8e80941Smrg * ir_call from linked then linked_sig and callee will be 133b8e80941Smrg * ir_function_signatures that have no definitions (is_defined is false). 134b8e80941Smrg */ 135b8e80941Smrg assert(!linked_sig->is_defined); 136b8e80941Smrg assert(linked_sig->body.is_empty()); 137b8e80941Smrg 138b8e80941Smrg /* Create an in-place clone of the function definition. This multistep 139b8e80941Smrg * process introduces some complexity here, but it has some advantages. 140b8e80941Smrg * The parameter list and the and function body are cloned separately. 141b8e80941Smrg * The clone of the parameter list is used to prime the hashtable used 142b8e80941Smrg * to replace variable references in the cloned body. 143b8e80941Smrg * 144b8e80941Smrg * The big advantage is that the ir_function_signature does not change. 145b8e80941Smrg * This means that we don't have to process the rest of the IR tree to 146b8e80941Smrg * patch ir_call nodes. In addition, there is no way to remove or 147b8e80941Smrg * replace signature stored in a function. One could easily be added, 148b8e80941Smrg * but this avoids the need. 149b8e80941Smrg */ 150b8e80941Smrg struct hash_table *ht = _mesa_pointer_hash_table_create(NULL); 151b8e80941Smrg 152b8e80941Smrg exec_list formal_parameters; 153b8e80941Smrg foreach_in_list(const ir_instruction, original, &sig->parameters) { 154b8e80941Smrg assert(const_cast<ir_instruction *>(original)->as_variable()); 155b8e80941Smrg 156b8e80941Smrg ir_instruction *copy = original->clone(linked, ht); 157b8e80941Smrg formal_parameters.push_tail(copy); 158b8e80941Smrg } 159b8e80941Smrg 160b8e80941Smrg linked_sig->replace_parameters(&formal_parameters); 161b8e80941Smrg 162b8e80941Smrg linked_sig->intrinsic_id = sig->intrinsic_id; 163b8e80941Smrg 164b8e80941Smrg if (sig->is_defined) { 165b8e80941Smrg foreach_in_list(const ir_instruction, original, &sig->body) { 166b8e80941Smrg ir_instruction *copy = original->clone(linked, ht); 167b8e80941Smrg linked_sig->body.push_tail(copy); 168b8e80941Smrg } 169b8e80941Smrg 170b8e80941Smrg linked_sig->is_defined = true; 171b8e80941Smrg } 172b8e80941Smrg 173b8e80941Smrg _mesa_hash_table_destroy(ht, NULL); 174b8e80941Smrg 175b8e80941Smrg /* Patch references inside the function to things outside the function 176b8e80941Smrg * (i.e., function calls and global variables). 177b8e80941Smrg */ 178b8e80941Smrg linked_sig->accept(this); 179b8e80941Smrg 180b8e80941Smrg ir->callee = linked_sig; 181b8e80941Smrg 182b8e80941Smrg return visit_continue; 183b8e80941Smrg } 184b8e80941Smrg 185b8e80941Smrg virtual ir_visitor_status visit_leave(ir_call *ir) 186b8e80941Smrg { 187b8e80941Smrg /* Traverse list of function parameters, and for array parameters 188b8e80941Smrg * propagate max_array_access. Otherwise arrays that are only referenced 189b8e80941Smrg * from inside functions via function parameters will be incorrectly 190b8e80941Smrg * optimized. This will lead to incorrect code being generated (or worse). 191b8e80941Smrg * Do it when leaving the node so the children would propagate their 192b8e80941Smrg * array accesses first. 193b8e80941Smrg */ 194b8e80941Smrg 195b8e80941Smrg const exec_node *formal_param_node = ir->callee->parameters.get_head(); 196b8e80941Smrg if (formal_param_node) { 197b8e80941Smrg const exec_node *actual_param_node = ir->actual_parameters.get_head(); 198b8e80941Smrg while (!actual_param_node->is_tail_sentinel()) { 199b8e80941Smrg ir_variable *formal_param = (ir_variable *) formal_param_node; 200b8e80941Smrg ir_rvalue *actual_param = (ir_rvalue *) actual_param_node; 201b8e80941Smrg 202b8e80941Smrg formal_param_node = formal_param_node->get_next(); 203b8e80941Smrg actual_param_node = actual_param_node->get_next(); 204b8e80941Smrg 205b8e80941Smrg if (formal_param->type->is_array()) { 206b8e80941Smrg ir_dereference_variable *deref = actual_param->as_dereference_variable(); 207b8e80941Smrg if (deref && deref->var && deref->var->type->is_array()) { 208b8e80941Smrg deref->var->data.max_array_access = 209b8e80941Smrg MAX2(formal_param->data.max_array_access, 210b8e80941Smrg deref->var->data.max_array_access); 211b8e80941Smrg } 212b8e80941Smrg } 213b8e80941Smrg } 214b8e80941Smrg } 215b8e80941Smrg return visit_continue; 216b8e80941Smrg } 217b8e80941Smrg 218b8e80941Smrg virtual ir_visitor_status visit(ir_dereference_variable *ir) 219b8e80941Smrg { 220b8e80941Smrg if (_mesa_set_search(locals, ir->var) == NULL) { 221b8e80941Smrg /* The non-function variable must be a global, so try to find the 222b8e80941Smrg * variable in the shader's symbol table. If the variable is not 223b8e80941Smrg * found, then it's a global that *MUST* be defined in the original 224b8e80941Smrg * shader. 225b8e80941Smrg */ 226b8e80941Smrg ir_variable *var = linked->symbols->get_variable(ir->var->name); 227b8e80941Smrg if (var == NULL) { 228b8e80941Smrg /* Clone the ir_variable that the dereference already has and add 229b8e80941Smrg * it to the linked shader. 230b8e80941Smrg */ 231b8e80941Smrg var = ir->var->clone(linked, NULL); 232b8e80941Smrg linked->symbols->add_variable(var); 233b8e80941Smrg linked->ir->push_head(var); 234b8e80941Smrg } else { 235b8e80941Smrg if (var->type->is_array()) { 236b8e80941Smrg /* It is possible to have a global array declared in multiple 237b8e80941Smrg * shaders without a size. The array is implicitly sized by 238b8e80941Smrg * the maximal access to it in *any* shader. Because of this, 239b8e80941Smrg * we need to track the maximal access to the array as linking 240b8e80941Smrg * pulls more functions in that access the array. 241b8e80941Smrg */ 242b8e80941Smrg var->data.max_array_access = 243b8e80941Smrg MAX2(var->data.max_array_access, 244b8e80941Smrg ir->var->data.max_array_access); 245b8e80941Smrg 246b8e80941Smrg if (var->type->length == 0 && ir->var->type->length != 0) 247b8e80941Smrg var->type = ir->var->type; 248b8e80941Smrg } 249b8e80941Smrg if (var->is_interface_instance()) { 250b8e80941Smrg /* Similarly, we need implicit sizes of arrays within interface 251b8e80941Smrg * blocks to be sized by the maximal access in *any* shader. 252b8e80941Smrg */ 253b8e80941Smrg int *const linked_max_ifc_array_access = 254b8e80941Smrg var->get_max_ifc_array_access(); 255b8e80941Smrg int *const ir_max_ifc_array_access = 256b8e80941Smrg ir->var->get_max_ifc_array_access(); 257b8e80941Smrg 258b8e80941Smrg assert(linked_max_ifc_array_access != NULL); 259b8e80941Smrg assert(ir_max_ifc_array_access != NULL); 260b8e80941Smrg 261b8e80941Smrg for (unsigned i = 0; i < var->get_interface_type()->length; 262b8e80941Smrg i++) { 263b8e80941Smrg linked_max_ifc_array_access[i] = 264b8e80941Smrg MAX2(linked_max_ifc_array_access[i], 265b8e80941Smrg ir_max_ifc_array_access[i]); 266b8e80941Smrg } 267b8e80941Smrg } 268b8e80941Smrg } 269b8e80941Smrg 270b8e80941Smrg ir->var = var; 271b8e80941Smrg } 272b8e80941Smrg 273b8e80941Smrg return visit_continue; 274b8e80941Smrg } 275b8e80941Smrg 276b8e80941Smrg /** Was function linking successful? */ 277b8e80941Smrg bool success; 278b8e80941Smrg 279b8e80941Smrgprivate: 280b8e80941Smrg /** 281b8e80941Smrg * Shader program being linked 282b8e80941Smrg * 283b8e80941Smrg * This is only used for logging error messages. 284b8e80941Smrg */ 285b8e80941Smrg gl_shader_program *prog; 286b8e80941Smrg 287b8e80941Smrg /** List of shaders available for linking. */ 288b8e80941Smrg gl_shader **shader_list; 289b8e80941Smrg 290b8e80941Smrg /** Number of shaders available for linking. */ 291b8e80941Smrg unsigned num_shaders; 292b8e80941Smrg 293b8e80941Smrg /** 294b8e80941Smrg * Final linked shader 295b8e80941Smrg * 296b8e80941Smrg * This is used two ways. It is used to find global variables in the 297b8e80941Smrg * linked shader that are accessed by the function. It is also used to add 298b8e80941Smrg * global variables from the shader where the function originated. 299b8e80941Smrg */ 300b8e80941Smrg gl_linked_shader *linked; 301b8e80941Smrg 302b8e80941Smrg /** 303b8e80941Smrg * Table of variables local to the function. 304b8e80941Smrg */ 305b8e80941Smrg set *locals; 306b8e80941Smrg}; 307b8e80941Smrg 308b8e80941Smrg} /* anonymous namespace */ 309b8e80941Smrg 310b8e80941Smrg/** 311b8e80941Smrg * Searches a list of shaders for a particular function definition 312b8e80941Smrg */ 313b8e80941Smrgir_function_signature * 314b8e80941Smrgfind_matching_signature(const char *name, const exec_list *actual_parameters, 315b8e80941Smrg glsl_symbol_table *symbols) 316b8e80941Smrg{ 317b8e80941Smrg ir_function *const f = symbols->get_function(name); 318b8e80941Smrg 319b8e80941Smrg if (f) { 320b8e80941Smrg ir_function_signature *sig = 321b8e80941Smrg f->matching_signature(NULL, actual_parameters, false); 322b8e80941Smrg 323b8e80941Smrg if (sig && (sig->is_defined || sig->is_intrinsic())) 324b8e80941Smrg return sig; 325b8e80941Smrg } 326b8e80941Smrg 327b8e80941Smrg return NULL; 328b8e80941Smrg} 329b8e80941Smrg 330b8e80941Smrg 331b8e80941Smrgbool 332b8e80941Smrglink_function_calls(gl_shader_program *prog, gl_linked_shader *main, 333b8e80941Smrg gl_shader **shader_list, unsigned num_shaders) 334b8e80941Smrg{ 335b8e80941Smrg call_link_visitor v(prog, main, shader_list, num_shaders); 336b8e80941Smrg 337b8e80941Smrg v.run(main->ir); 338b8e80941Smrg return v.success; 339b8e80941Smrg} 340