Path: blob/21.2-virgl/src/panfrost/midgard/midgard_helper_invocations.c
4564 views
/*1* Copyright (C) 2019 Collabora, Ltd.2*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, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors (Collabora):23* Alyssa Rosenzweig <[email protected]>24*/2526#include "compiler.h"2728/* Midgard texture/derivative operations have a pair of bits controlling the29* behaviour of helper invocations:30*31* - Should a helper invocation terminate after executing this instruction?32* - Should a helper invocation actually execute this instruction?33*34* The terminate bit should be set on the last instruction requiring helper35* invocations. Without control flow, that's literally the last instruction;36* with control flow, there may be multiple such instructions (with ifs) or no37* such instruction (with loops).38*39* The execute bit should be set if the value of this instruction is required40* by a future instruction requiring helper invocations. Consider:41*42* 0 = texture ...43* 1 = fmul 0, #1044* 2 = dfdx 145* store 246*47* Since the derivative calculation 2 requires helper invocations, the value 148* must be calculated by helper invocations, and since it depends on 0, 0 must49* be calculated by helpers. Hence the texture op has the execute bit set, and50* the derivative op has the terminate bit set.51*52* Calculating the terminate bit occurs by forward dataflow analysis to53* determine which blocks require helper invocations. A block requires54* invocations in if any of its instructions use helper invocations, or if it55* depends on a block that requires invocation. With that analysis, the56* terminate bit is set on the last instruction using invocations within any57* block that does *not* require invocations out.58*59* Likewise, calculating the execute bit requires backward dataflow analysis60* with union as the join operation and the generating set being the union of61* sources of instructions writing executed values.62*/6364/* Does a block use helpers directly */65static bool66mir_block_uses_helpers(gl_shader_stage stage, midgard_block *block)67{68mir_foreach_instr_in_block(block, ins) {69if (ins->type != TAG_TEXTURE_4) continue;70if (mir_op_computes_derivatives(stage, ins->op))71return true;72}7374return false;75}7677static bool78mir_block_terminates_helpers(midgard_block *block)79{80/* Can't terminate if there are no helpers */81if (!block->helpers_in)82return false;8384/* Can't terminate if a successor needs helpers */85pan_foreach_successor((&block->base), succ) {86if (((midgard_block *) succ)->helpers_in)87return false;88}8990/* Otherwise we terminate */91return true;92}9394void95mir_analyze_helper_terminate(compiler_context *ctx)96{97/* Set blocks as directly requiring helpers, and if they do add them to98* the worklist to propagate to their predecessors */99100struct set *worklist = _mesa_set_create(NULL,101_mesa_hash_pointer,102_mesa_key_pointer_equal);103104struct set *visited = _mesa_set_create(NULL,105_mesa_hash_pointer,106_mesa_key_pointer_equal);107108mir_foreach_block(ctx, _block) {109midgard_block *block = (midgard_block *) _block;110block->helpers_in |= mir_block_uses_helpers(ctx->stage, block);111112if (block->helpers_in)113_mesa_set_add(worklist, _block);114}115116/* Next, propagate back. Since there are a finite number of blocks, the117* worklist (a subset of all the blocks) is finite. Since a block can118* only be added to the worklist if it is not on the visited list and119* the visited list - also a subset of the blocks - grows every120* iteration, the algorithm must terminate. */121122struct set_entry *cur;123124while((cur = _mesa_set_next_entry(worklist, NULL)) != NULL) {125/* Pop off a block requiring helpers */126pan_block *blk = (struct pan_block *) cur->key;127_mesa_set_remove(worklist, cur);128129/* Its predecessors also require helpers */130pan_foreach_predecessor(blk, pred) {131if (!_mesa_set_search(visited, pred)) {132((midgard_block *) pred)->helpers_in = true;133_mesa_set_add(worklist, pred);134}135}136137_mesa_set_add(visited, blk);138}139140_mesa_set_destroy(visited, NULL);141_mesa_set_destroy(worklist, NULL);142143/* Finally, set helper_terminate on the last derivative-calculating144* instruction in a block that terminates helpers */145mir_foreach_block(ctx, _block) {146midgard_block *block = (midgard_block *) _block;147148if (!mir_block_terminates_helpers(block))149continue;150151mir_foreach_instr_in_block_rev(block, ins) {152if (ins->type != TAG_TEXTURE_4) continue;153if (!mir_op_computes_derivatives(ctx->stage, ins->op)) continue;154155ins->helper_terminate = true;156break;157}158}159}160161static bool162mir_helper_block_update(BITSET_WORD *deps, pan_block *_block, unsigned temp_count)163{164bool progress = false;165midgard_block *block = (midgard_block *) _block;166167mir_foreach_instr_in_block_rev(block, ins) {168/* Ensure we write to a helper dependency */169if (ins->dest >= temp_count || !BITSET_TEST(deps, ins->dest))170continue;171172/* Then add all of our dependencies */173mir_foreach_src(ins, s) {174if (ins->src[s] >= temp_count)175continue;176177/* Progress if the dependency set changes */178progress |= !BITSET_TEST(deps, ins->src[s]);179BITSET_SET(deps, ins->src[s]);180}181}182183return progress;184}185186void187mir_analyze_helper_requirements(compiler_context *ctx)188{189mir_compute_temp_count(ctx);190unsigned temp_count = ctx->temp_count;191BITSET_WORD *deps = calloc(sizeof(BITSET_WORD), BITSET_WORDS(temp_count));192193/* Initialize with the sources of instructions consuming194* derivatives */195196mir_foreach_instr_global(ctx, ins) {197if (ins->type != TAG_TEXTURE_4) continue;198if (ins->dest >= ctx->temp_count) continue;199if (!mir_op_computes_derivatives(ctx->stage, ins->op)) continue;200201mir_foreach_src(ins, s) {202if (ins->src[s] < temp_count)203BITSET_SET(deps, ins->src[s]);204}205}206207/* Propagate that up */208209struct set *work_list = _mesa_set_create(NULL,210_mesa_hash_pointer,211_mesa_key_pointer_equal);212213struct set *visited = _mesa_set_create(NULL,214_mesa_hash_pointer,215_mesa_key_pointer_equal);216217struct set_entry *cur = _mesa_set_add(work_list, pan_exit_block(&ctx->blocks));218219do {220pan_block *blk = (struct pan_block *) cur->key;221_mesa_set_remove(work_list, cur);222223bool progress = mir_helper_block_update(deps, blk, temp_count);224225if (progress || !_mesa_set_search(visited, blk)) {226pan_foreach_predecessor(blk, pred)227_mesa_set_add(work_list, pred);228}229230_mesa_set_add(visited, blk);231} while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);232233_mesa_set_destroy(visited, NULL);234_mesa_set_destroy(work_list, NULL);235236/* Set the execute bits */237238mir_foreach_instr_global(ctx, ins) {239if (ins->type != TAG_TEXTURE_4) continue;240if (ins->dest >= ctx->temp_count) continue;241242ins->helper_execute = BITSET_TEST(deps, ins->dest);243}244245free(deps);246}247248249