Path: blob/21.2-virgl/src/amd/compiler/aco_reduce_assign.cpp
4550 views
/*1* Copyright © 2018 Valve Corporation2* Copyright © 2018 Google3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS21* IN THE SOFTWARE.22*23*/2425#include "aco_builder.h"26#include "aco_ir.h"2728#include <vector>2930/*31* Insert p_linear_start instructions right before RA to correctly allocate32* temporaries for reductions that have to disrespect EXEC by executing in33* WWM.34*/3536namespace aco {3738void39setup_reduce_temp(Program* program)40{41unsigned last_top_level_block_idx = 0;42unsigned maxSize = 0;4344std::vector<bool> hasReductions(program->blocks.size());45for (Block& block : program->blocks) {46for (aco_ptr<Instruction>& instr : block.instructions) {47if (instr->format != Format::PSEUDO_REDUCTION)48continue;4950maxSize = MAX2(maxSize, instr->operands[0].size());51hasReductions[block.index] = true;52}53}5455if (maxSize == 0)56return;5758assert(maxSize == 1 || maxSize == 2);59Temp reduceTmp(0, RegClass(RegType::vgpr, maxSize).as_linear());60Temp vtmp(0, RegClass(RegType::vgpr, maxSize).as_linear());61int inserted_at = -1;62int vtmp_inserted_at = -1;63bool reduceTmp_in_loop = false;64bool vtmp_in_loop = false;6566for (Block& block : program->blocks) {6768/* insert p_end_linear_vgpr after the outermost loop */69if (reduceTmp_in_loop && block.loop_nest_depth == 0) {70assert(inserted_at == (int)last_top_level_block_idx);7172aco_ptr<Instruction> end{create_instruction<Instruction>(73aco_opcode::p_end_linear_vgpr, Format::PSEUDO, vtmp_in_loop ? 2 : 1, 0)};74end->operands[0] = Operand(reduceTmp);75if (vtmp_in_loop)76end->operands[1] = Operand(vtmp);77/* insert after the phis of the loop exit block */78std::vector<aco_ptr<Instruction>>::iterator it = block.instructions.begin();79while ((*it)->opcode == aco_opcode::p_linear_phi || (*it)->opcode == aco_opcode::p_phi)80++it;81block.instructions.insert(it, std::move(end));82reduceTmp_in_loop = false;83}8485if (block.kind & block_kind_top_level)86last_top_level_block_idx = block.index;8788if (!hasReductions[block.index])89continue;9091std::vector<aco_ptr<Instruction>>::iterator it;92for (it = block.instructions.begin(); it != block.instructions.end(); ++it) {93Instruction* instr = (*it).get();94if (instr->format != Format::PSEUDO_REDUCTION)95continue;9697ReduceOp op = instr->reduction().reduce_op;98reduceTmp_in_loop |= block.loop_nest_depth > 0;99100if ((int)last_top_level_block_idx != inserted_at) {101reduceTmp = program->allocateTmp(reduceTmp.regClass());102aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(103aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};104create->definitions[0] = Definition(reduceTmp);105/* find the right place to insert this definition */106if (last_top_level_block_idx == block.index) {107/* insert right before the current instruction */108it = block.instructions.insert(it, std::move(create));109it++;110/* inserted_at is intentionally not updated here, so later blocks111* would insert at the end instead of using this one. */112} else {113assert(last_top_level_block_idx < block.index);114/* insert before the branch at last top level block */115std::vector<aco_ptr<Instruction>>& instructions =116program->blocks[last_top_level_block_idx].instructions;117instructions.insert(std::next(instructions.begin(), instructions.size() - 1),118std::move(create));119inserted_at = last_top_level_block_idx;120}121}122123/* same as before, except for the vector temporary instead of the reduce temporary */124unsigned cluster_size = instr->reduction().cluster_size;125bool need_vtmp = op == imul32 || op == fadd64 || op == fmul64 || op == fmin64 ||126op == fmax64 || op == umin64 || op == umax64 || op == imin64 ||127op == imax64 || op == imul64;128bool gfx10_need_vtmp = op == imul8 || op == imax8 || op == imin8 || op == umin8 ||129op == imul16 || op == imax16 || op == imin16 || op == umin16 ||130op == iadd64;131132if (program->chip_class >= GFX10 && cluster_size == 64)133need_vtmp = true;134if (program->chip_class >= GFX10 && gfx10_need_vtmp)135need_vtmp = true;136if (program->chip_class <= GFX7)137need_vtmp = true;138139need_vtmp |= cluster_size == 32;140141vtmp_in_loop |= need_vtmp && block.loop_nest_depth > 0;142if (need_vtmp && (int)last_top_level_block_idx != vtmp_inserted_at) {143vtmp = program->allocateTmp(vtmp.regClass());144aco_ptr<Pseudo_instruction> create{create_instruction<Pseudo_instruction>(145aco_opcode::p_start_linear_vgpr, Format::PSEUDO, 0, 1)};146create->definitions[0] = Definition(vtmp);147if (last_top_level_block_idx == block.index) {148it = block.instructions.insert(it, std::move(create));149it++;150} else {151assert(last_top_level_block_idx < block.index);152std::vector<aco_ptr<Instruction>>& instructions =153program->blocks[last_top_level_block_idx].instructions;154instructions.insert(std::next(instructions.begin(), instructions.size() - 1),155std::move(create));156vtmp_inserted_at = last_top_level_block_idx;157}158}159160instr->operands[1] = Operand(reduceTmp);161if (need_vtmp)162instr->operands[2] = Operand(vtmp);163}164}165}166167}; // namespace aco168169170