Path: blob/21.2-virgl/src/amd/compiler/aco_lower_phis.cpp
4550 views
/*1* Copyright © 2019 Valve 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*22*/2324#include "aco_builder.h"25#include "aco_ir.h"2627#include <algorithm>28#include <map>29#include <vector>3031namespace aco {3233struct ssa_state {34bool checked_preds_for_uniform;35bool all_preds_uniform;3637bool needs_init;38uint64_t cur_undef_operands;3940unsigned phi_block_idx;41unsigned loop_nest_depth;42std::map<unsigned, unsigned> writes;43/* Whether there's a write in any of a block's predecessors. Indexed by the block index. */44std::vector<bool> any_pred_defined;45std::vector<Operand> latest;46std::vector<bool> visited;47};4849Operand50get_ssa(Program* program, unsigned block_idx, ssa_state* state, bool before_write)51{52if (!before_write) {53auto it = state->writes.find(block_idx);54if (it != state->writes.end())55return Operand(Temp(it->second, program->lane_mask));56if (state->visited[block_idx])57return state->latest[block_idx];58}5960state->visited[block_idx] = true;6162Block& block = program->blocks[block_idx];63size_t pred = block.linear_preds.size();64if (pred == 0 || block.loop_nest_depth < state->loop_nest_depth ||65!state->any_pred_defined[block_idx]) {66return Operand(program->lane_mask);67} else if (block.loop_nest_depth > state->loop_nest_depth) {68Operand op = get_ssa(program, block_idx - 1, state, false);69state->latest[block_idx] = op;70return op;71} else if (pred == 1 || block.kind & block_kind_loop_exit) {72Operand op = get_ssa(program, block.linear_preds[0], state, false);73state->latest[block_idx] = op;74return op;75} else if (block.kind & block_kind_loop_header &&76!(program->blocks[state->phi_block_idx].kind & block_kind_loop_exit)) {77return Operand(program->lane_mask);78} else {79Temp res = Temp(program->allocateTmp(program->lane_mask));80state->latest[block_idx] = Operand(res);8182aco_ptr<Pseudo_instruction> phi{83create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, pred, 1)};84for (unsigned i = 0; i < pred; i++)85phi->operands[i] = get_ssa(program, block.linear_preds[i], state, false);86phi->definitions[0] = Definition(res);87block.instructions.emplace(block.instructions.begin(), std::move(phi));8889return Operand(res);90}91}9293void94insert_before_logical_end(Block* block, aco_ptr<Instruction> instr)95{96auto IsLogicalEnd = [](const aco_ptr<Instruction>& inst) -> bool97{ return inst->opcode == aco_opcode::p_logical_end; };98auto it = std::find_if(block->instructions.crbegin(), block->instructions.crend(), IsLogicalEnd);99100if (it == block->instructions.crend()) {101assert(block->instructions.back()->isBranch());102block->instructions.insert(std::prev(block->instructions.end()), std::move(instr));103} else {104block->instructions.insert(std::prev(it.base()), std::move(instr));105}106}107108void109build_merge_code(Program* program, Block* block, Definition dst, Operand prev, Operand cur)110{111Builder bld(program);112113auto IsLogicalEnd = [](const aco_ptr<Instruction>& instr) -> bool114{ return instr->opcode == aco_opcode::p_logical_end; };115auto it = std::find_if(block->instructions.rbegin(), block->instructions.rend(), IsLogicalEnd);116assert(it != block->instructions.rend());117bld.reset(&block->instructions, std::prev(it.base()));118119if (prev.isUndefined()) {120bld.copy(dst, cur);121return;122}123124bool prev_is_constant = prev.isConstant() && prev.constantValue() + 1u < 2u;125bool cur_is_constant = cur.isConstant() && cur.constantValue() + 1u < 2u;126127if (!prev_is_constant) {128if (!cur_is_constant) {129Temp tmp1 = bld.tmp(bld.lm), tmp2 = bld.tmp(bld.lm);130bld.sop2(Builder::s_andn2, Definition(tmp1), bld.def(s1, scc), prev,131Operand(exec, bld.lm));132bld.sop2(Builder::s_and, Definition(tmp2), bld.def(s1, scc), cur, Operand(exec, bld.lm));133bld.sop2(Builder::s_or, dst, bld.def(s1, scc), tmp1, tmp2);134} else if (cur.constantValue()) {135bld.sop2(Builder::s_or, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));136} else {137bld.sop2(Builder::s_andn2, dst, bld.def(s1, scc), prev, Operand(exec, bld.lm));138}139} else if (prev.constantValue()) {140if (!cur_is_constant)141bld.sop2(Builder::s_orn2, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));142else if (cur.constantValue())143bld.copy(dst, Operand::c32_or_c64(UINT32_MAX, bld.lm == s2));144else145bld.sop1(Builder::s_not, dst, bld.def(s1, scc), Operand(exec, bld.lm));146} else {147if (!cur_is_constant)148bld.sop2(Builder::s_and, dst, bld.def(s1, scc), cur, Operand(exec, bld.lm));149else if (cur.constantValue())150bld.copy(dst, Operand(exec, bld.lm));151else152bld.copy(dst, Operand::zero(bld.lm.bytes()));153}154}155156void157init_any_pred_defined(Program* program, ssa_state* state, Block* block, aco_ptr<Instruction>& phi)158{159std::fill(state->any_pred_defined.begin(), state->any_pred_defined.end(), false);160for (unsigned i = 0; i < block->logical_preds.size(); i++) {161if (phi->operands[i].isUndefined())162continue;163for (unsigned succ : program->blocks[block->logical_preds[i]].linear_succs)164state->any_pred_defined[succ] = true;165}166167unsigned start = block->logical_preds[0];168169/* for loop exit phis, start at the loop header */170const bool loop_exit = block->kind & block_kind_loop_exit;171while (loop_exit && program->blocks[start - 1].loop_nest_depth >= state->loop_nest_depth)172start--;173174for (unsigned i = 0; i < 1u + loop_exit; i++) {175for (unsigned j = start; j < block->index; j++) {176if (!state->any_pred_defined[j])177continue;178for (unsigned succ : program->blocks[j].linear_succs)179state->any_pred_defined[succ] = true;180}181}182}183184void185lower_divergent_bool_phi(Program* program, ssa_state* state, Block* block,186aco_ptr<Instruction>& phi)187{188Builder bld(program);189190if (!state->checked_preds_for_uniform) {191state->all_preds_uniform = !(block->kind & block_kind_merge) &&192block->linear_preds.size() == block->logical_preds.size();193for (unsigned pred : block->logical_preds)194state->all_preds_uniform =195state->all_preds_uniform && (program->blocks[pred].kind & block_kind_uniform);196state->checked_preds_for_uniform = true;197}198199if (state->all_preds_uniform) {200phi->opcode = aco_opcode::p_linear_phi;201return;202}203204state->latest.resize(program->blocks.size());205state->visited.resize(program->blocks.size());206state->any_pred_defined.resize(program->blocks.size());207208uint64_t undef_operands = 0;209for (unsigned i = 0; i < phi->operands.size(); i++)210undef_operands |= (uint64_t)phi->operands[i].isUndefined() << i;211212if (state->needs_init || undef_operands != state->cur_undef_operands ||213block->logical_preds.size() > 64) {214/* this only has to be done once per block unless the set of predecessors215* which are undefined changes */216state->cur_undef_operands = undef_operands;217state->phi_block_idx = block->index;218state->loop_nest_depth = block->loop_nest_depth;219if (block->kind & block_kind_loop_exit) {220state->loop_nest_depth += 1;221}222state->writes.clear();223init_any_pred_defined(program, state, block, phi);224state->needs_init = false;225}226std::fill(state->latest.begin(), state->latest.end(), Operand(program->lane_mask));227std::fill(state->visited.begin(), state->visited.end(), false);228229for (unsigned i = 0; i < phi->operands.size(); i++) {230if (phi->operands[i].isUndefined())231continue;232233state->writes[block->logical_preds[i]] = program->allocateId(program->lane_mask);234}235236bool uniform_merge = block->kind & block_kind_loop_header;237238for (unsigned i = 0; i < phi->operands.size(); i++) {239Block* pred = &program->blocks[block->logical_preds[i]];240241bool need_get_ssa = !uniform_merge;242if (block->kind & block_kind_loop_header && !(pred->kind & block_kind_uniform))243uniform_merge = false;244245if (phi->operands[i].isUndefined())246continue;247248Operand cur(bld.lm);249if (need_get_ssa)250cur = get_ssa(program, pred->index, state, true);251assert(cur.regClass() == bld.lm);252253Temp new_cur = {state->writes.at(pred->index), program->lane_mask};254assert(new_cur.regClass() == bld.lm);255256if (i == 1 && (block->kind & block_kind_merge) && phi->operands[0].isConstant())257cur = phi->operands[0];258build_merge_code(program, pred, Definition(new_cur), cur, phi->operands[i]);259}260261unsigned num_preds = block->linear_preds.size();262if (phi->operands.size() != num_preds) {263Pseudo_instruction* new_phi{create_instruction<Pseudo_instruction>(264aco_opcode::p_linear_phi, Format::PSEUDO, num_preds, 1)};265new_phi->definitions[0] = phi->definitions[0];266phi.reset(new_phi);267} else {268phi->opcode = aco_opcode::p_linear_phi;269}270assert(phi->operands.size() == num_preds);271272for (unsigned i = 0; i < num_preds; i++)273phi->operands[i] = get_ssa(program, block->linear_preds[i], state, false);274275return;276}277278void279lower_subdword_phis(Program* program, Block* block, aco_ptr<Instruction>& phi)280{281Builder bld(program);282for (unsigned i = 0; i < phi->operands.size(); i++) {283if (phi->operands[i].isUndefined())284continue;285if (phi->operands[i].regClass() == phi->definitions[0].regClass())286continue;287288assert(phi->operands[i].isTemp());289Block* pred = &program->blocks[block->logical_preds[i]];290Temp phi_src = phi->operands[i].getTemp();291292assert(phi_src.regClass().type() == RegType::sgpr);293Temp tmp = bld.tmp(RegClass(RegType::vgpr, phi_src.size()));294insert_before_logical_end(pred, bld.copy(Definition(tmp), phi_src).get_ptr());295Temp new_phi_src = bld.tmp(phi->definitions[0].regClass());296insert_before_logical_end(pred, bld.pseudo(aco_opcode::p_extract_vector,297Definition(new_phi_src), tmp, Operand::zero())298.get_ptr());299300phi->operands[i].setTemp(new_phi_src);301}302return;303}304305void306lower_phis(Program* program)307{308ssa_state state;309310for (Block& block : program->blocks) {311state.checked_preds_for_uniform = false;312state.needs_init = true;313for (aco_ptr<Instruction>& phi : block.instructions) {314if (phi->opcode == aco_opcode::p_phi) {315assert(program->wave_size == 64 ? phi->definitions[0].regClass() != s1316: phi->definitions[0].regClass() != s2);317if (phi->definitions[0].regClass() == program->lane_mask)318lower_divergent_bool_phi(program, &state, &block, phi);319else if (phi->definitions[0].regClass().is_subdword())320lower_subdword_phis(program, &block, phi);321} else if (!is_phi(phi)) {322break;323}324}325}326}327328} // namespace aco329330331