Path: blob/21.2-virgl/src/compiler/nir/nir_inline_functions.c
4546 views
/*1* Copyright © 2015 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 OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "nir.h"24#include "nir_builder.h"25#include "nir_control_flow.h"26#include "nir_vla.h"2728void nir_inline_function_impl(struct nir_builder *b,29const nir_function_impl *impl,30nir_ssa_def **params,31struct hash_table *shader_var_remap)32{33nir_function_impl *copy = nir_function_impl_clone(b->shader, impl);3435/* Insert a nop at the cursor so we can keep track of where things are as36* we add/remove stuff from the CFG.37*/38nir_intrinsic_instr *nop = nir_nop(b);3940exec_list_append(&b->impl->locals, ©->locals);41exec_list_append(&b->impl->registers, ©->registers);4243nir_foreach_block(block, copy) {44nir_foreach_instr_safe(instr, block) {45switch (instr->type) {46case nir_instr_type_deref: {47nir_deref_instr *deref = nir_instr_as_deref(instr);48if (deref->deref_type != nir_deref_type_var)49break;5051/* We don't need to remap function variables. We already cloned52* them as part of nir_function_impl_clone and appended them to53* b->impl->locals.54*/55if (deref->var->data.mode == nir_var_function_temp)56break;5758/* If no map is provided, we assume that there are either no59* shader variables or they already live b->shader (this is the60* case for function inlining within a single shader.61*/62if (shader_var_remap == NULL)63break;6465struct hash_entry *entry =66_mesa_hash_table_search(shader_var_remap, deref->var);67if (entry == NULL) {68nir_variable *nvar = nir_variable_clone(deref->var, b->shader);69nir_shader_add_variable(b->shader, nvar);70entry = _mesa_hash_table_insert(shader_var_remap,71deref->var, nvar);72}73deref->var = entry->data;74break;75}7677case nir_instr_type_intrinsic: {78nir_intrinsic_instr *load = nir_instr_as_intrinsic(instr);79if (load->intrinsic != nir_intrinsic_load_param)80break;8182unsigned param_idx = nir_intrinsic_param_idx(load);83assert(param_idx < impl->function->num_params);84assert(load->dest.is_ssa);85nir_ssa_def_rewrite_uses(&load->dest.ssa,86params[param_idx]);8788/* Remove any left-over load_param intrinsics because they're soon89* to be in another function and therefore no longer valid.90*/91nir_instr_remove(&load->instr);92break;93}9495case nir_instr_type_jump:96/* Returns have to be lowered for this to work */97assert(nir_instr_as_jump(instr)->type != nir_jump_return);98break;99100default:101break;102}103}104}105106/* Pluck the body out of the function and place it here */107nir_cf_list body;108nir_cf_list_extract(&body, ©->body);109nir_cf_reinsert(&body, nir_before_instr(&nop->instr));110111b->cursor = nir_instr_remove(&nop->instr);112}113114static bool inline_function_impl(nir_function_impl *impl, struct set *inlined);115116static bool117inline_functions_block(nir_block *block, nir_builder *b,118struct set *inlined)119{120bool progress = false;121/* This is tricky. We're iterating over instructions in a block but, as122* we go, the block and its instruction list are being split into123* pieces. However, this *should* be safe since foreach_safe always124* stashes the next thing in the iteration. That next thing will125* properly get moved to the next block when it gets split, and we126* continue iterating there.127*/128nir_foreach_instr_safe(instr, block) {129if (instr->type != nir_instr_type_call)130continue;131132progress = true;133134nir_call_instr *call = nir_instr_as_call(instr);135assert(call->callee->impl);136137/* Make sure that the function we're calling is already inlined */138inline_function_impl(call->callee->impl, inlined);139140b->cursor = nir_instr_remove(&call->instr);141142/* Rewrite all of the uses of the callee's parameters to use the call143* instructions sources. In order to ensure that the "load" happens144* here and not later (for register sources), we make sure to convert it145* to an SSA value first.146*/147const unsigned num_params = call->num_params;148NIR_VLA(nir_ssa_def *, params, num_params);149for (unsigned i = 0; i < num_params; i++) {150params[i] = nir_ssa_for_src(b, call->params[i],151call->callee->params[i].num_components);152}153154nir_inline_function_impl(b, call->callee->impl, params, NULL);155}156157return progress;158}159160static bool161inline_function_impl(nir_function_impl *impl, struct set *inlined)162{163if (_mesa_set_search(inlined, impl))164return false; /* Already inlined */165166nir_builder b;167nir_builder_init(&b, impl);168169bool progress = false;170nir_foreach_block_safe(block, impl) {171progress |= inline_functions_block(block, &b, inlined);172}173174if (progress) {175/* SSA and register indices are completely messed up now */176nir_index_ssa_defs(impl);177nir_index_local_regs(impl);178179nir_metadata_preserve(impl, nir_metadata_none);180} else {181nir_metadata_preserve(impl, nir_metadata_all);182}183184_mesa_set_add(inlined, impl);185186return progress;187}188189/** A pass to inline all functions in a shader into their callers190*191* For most use-cases, function inlining is a multi-step process. The general192* pattern employed by SPIR-V consumers and others is as follows:193*194* 1. nir_lower_variable_initializers(shader, nir_var_function_temp)195*196* This is needed because local variables from the callee are simply added197* to the locals list for the caller and the information about where the198* constant initializer logically happens is lost. If the callee is199* called in a loop, this can cause the variable to go from being200* initialized once per loop iteration to being initialized once at the201* top of the caller and values to persist from one invocation of the202* callee to the next. The simple solution to this problem is to get rid203* of constant initializers before function inlining.204*205* 2. nir_lower_returns(shader)206*207* nir_inline_functions assumes that all functions end "naturally" by208* execution reaching the end of the function without any return209* instructions causing instant jumps to the end. Thanks to NIR being210* structured, we can't represent arbitrary jumps to various points in the211* program which is what an early return in the callee would have to turn212* into when we inline it into the caller. Instead, we require returns to213* be lowered which lets us just copy+paste the callee directly into the214* caller.215*216* 3. nir_inline_functions(shader)217*218* This does the actual function inlining and the resulting shader will219* contain no call instructions.220*221* 4. nir_opt_deref(shader)222*223* Most functions contain pointer parameters where the result of a deref224* instruction is passed in as a parameter, loaded via a load_param225* intrinsic, and then turned back into a deref via a cast. Function226* inlining will get rid of the load_param but we are still left with a227* cast. Running nir_opt_deref gets rid of the intermediate cast and228* results in a whole deref chain again. This is currently required by a229* number of optimizations and lowering passes at least for certain230* variable modes.231*232* 5. Loop over the functions and delete all but the main entrypoint.233*234* In the Intel Vulkan driver this looks like this:235*236* foreach_list_typed_safe(nir_function, func, node, &nir->functions) {237* if (func != entry_point)238* exec_node_remove(&func->node);239* }240* assert(exec_list_length(&nir->functions) == 1);241*242* While nir_inline_functions does get rid of all call instructions, it243* doesn't get rid of any functions because it doesn't know what the "root244* function" is. Instead, it's up to the individual driver to know how to245* decide on a root function and delete the rest. With SPIR-V,246* spirv_to_nir returns the root function and so we can just use == whereas247* with GL, you may have to look for a function named "main".248*249* 6. nir_lower_variable_initializers(shader, ~nir_var_function_temp)250*251* Lowering constant initializers on inputs, outputs, global variables,252* etc. requires that we know the main entrypoint so that we know where to253* initialize them. Otherwise, we would have to assume that anything254* could be a main entrypoint and initialize them at the start of every255* function but that would clearly be wrong if any of those functions were256* ever called within another function. Simply requiring a single-257* entrypoint function shader is the best way to make it well-defined.258*/259bool260nir_inline_functions(nir_shader *shader)261{262struct set *inlined = _mesa_pointer_set_create(NULL);263bool progress = false;264265nir_foreach_function(function, shader) {266if (function->impl)267progress = inline_function_impl(function->impl, inlined) || progress;268}269270_mesa_set_destroy(inlined, NULL);271272return progress;273}274275276