Path: blob/21.2-virgl/src/amd/compiler/aco_insert_exec_mask.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 "util/u_math.h"2829#include <set>30#include <vector>3132namespace aco {3334namespace {3536enum WQMState : uint8_t {37Unspecified = 0,38Exact = 1 << 0,39WQM = 1 << 1, /* with control flow applied */40Preserve_WQM = 1 << 2,41Exact_Branch = 1 << 3,42};4344enum mask_type : uint8_t {45mask_type_global = 1 << 0,46mask_type_exact = 1 << 1,47mask_type_wqm = 1 << 2,48mask_type_loop = 1 << 3, /* active lanes of a loop */49};5051struct wqm_ctx {52Program* program;53/* state for WQM propagation */54std::set<unsigned> worklist;55std::vector<uint16_t> defined_in;56std::vector<bool> needs_wqm;57std::vector<bool> branch_wqm; /* true if the branch condition in this block should be in wqm */58wqm_ctx(Program* program_)59: program(program_), defined_in(program->peekAllocationId(), 0xFFFF),60needs_wqm(program->peekAllocationId()), branch_wqm(program->blocks.size())61{62for (unsigned i = 0; i < program->blocks.size(); i++)63worklist.insert(i);64}65};6667struct loop_info {68Block* loop_header;69uint16_t num_exec_masks;70uint8_t needs;71bool has_divergent_break;72bool has_divergent_continue;73bool has_discard; /* has a discard or demote */74loop_info(Block* b, uint16_t num, uint8_t needs_, bool breaks, bool cont, bool discard)75: loop_header(b), num_exec_masks(num), needs(needs_), has_divergent_break(breaks),76has_divergent_continue(cont), has_discard(discard)77{}78};7980struct block_info {81std::vector<std::pair<Operand, uint8_t>>82exec; /* Vector of exec masks. Either a temporary or const -1. */83std::vector<WQMState> instr_needs;84uint8_t block_needs;85uint8_t ever_again_needs;86bool logical_end_wqm;87/* more... */88};8990struct exec_ctx {91Program* program;92std::vector<block_info> info;93std::vector<loop_info> loop;94bool handle_wqm = false;95exec_ctx(Program* program_) : program(program_), info(program->blocks.size()) {}96};9798bool99needs_exact(aco_ptr<Instruction>& instr)100{101if (instr->isMUBUF()) {102return instr->mubuf().disable_wqm;103} else if (instr->isMTBUF()) {104return instr->mtbuf().disable_wqm;105} else if (instr->isMIMG()) {106return instr->mimg().disable_wqm;107} else if (instr->isFlatLike()) {108return instr->flatlike().disable_wqm;109} else {110return instr->isEXP();111}112}113114void115set_needs_wqm(wqm_ctx& ctx, Temp tmp)116{117if (!ctx.needs_wqm[tmp.id()]) {118ctx.needs_wqm[tmp.id()] = true;119if (ctx.defined_in[tmp.id()] != 0xFFFF)120ctx.worklist.insert(ctx.defined_in[tmp.id()]);121}122}123124void125mark_block_wqm(wqm_ctx& ctx, unsigned block_idx)126{127if (ctx.branch_wqm[block_idx])128return;129130ctx.branch_wqm[block_idx] = true;131ctx.worklist.insert(block_idx);132133Block& block = ctx.program->blocks[block_idx];134135/* TODO: this sets more branch conditions to WQM than it needs to136* it should be enough to stop at the "exec mask top level" */137if (block.kind & block_kind_top_level)138return;139140for (unsigned pred_idx : block.logical_preds)141mark_block_wqm(ctx, pred_idx);142}143144void145get_block_needs(wqm_ctx& ctx, exec_ctx& exec_ctx, Block* block)146{147block_info& info = exec_ctx.info[block->index];148149std::vector<WQMState> instr_needs(block->instructions.size());150151for (int i = block->instructions.size() - 1; i >= 0; --i) {152aco_ptr<Instruction>& instr = block->instructions[i];153154WQMState needs = needs_exact(instr) ? Exact : Unspecified;155bool propagate_wqm =156instr->opcode == aco_opcode::p_wqm || instr->opcode == aco_opcode::p_as_uniform;157bool preserve_wqm = instr->opcode == aco_opcode::p_discard_if;158bool pred_by_exec = needs_exec_mask(instr.get());159for (const Definition& definition : instr->definitions) {160if (!definition.isTemp())161continue;162const unsigned def = definition.tempId();163ctx.defined_in[def] = block->index;164if (needs == Unspecified && ctx.needs_wqm[def]) {165needs = pred_by_exec ? WQM : Unspecified;166propagate_wqm = true;167}168}169170if (instr->isBranch() && ctx.branch_wqm[block->index]) {171assert(!(info.block_needs & Exact_Branch));172needs = WQM;173propagate_wqm = true;174}175176if (propagate_wqm) {177for (const Operand& op : instr->operands) {178if (op.isTemp()) {179set_needs_wqm(ctx, op.getTemp());180}181}182} else if (preserve_wqm && info.block_needs & WQM) {183needs = Preserve_WQM;184}185186/* ensure the condition controlling the control flow for this phi is in WQM */187if (needs == WQM && instr->opcode == aco_opcode::p_phi) {188for (unsigned pred_idx : block->logical_preds) {189mark_block_wqm(ctx, pred_idx);190exec_ctx.info[pred_idx].logical_end_wqm = true;191ctx.worklist.insert(pred_idx);192}193}194195if ((instr->opcode == aco_opcode::p_logical_end && info.logical_end_wqm) ||196instr->opcode == aco_opcode::p_wqm) {197assert(needs != Exact);198needs = WQM;199}200201instr_needs[i] = needs;202info.block_needs |= needs;203}204205info.instr_needs = instr_needs;206207/* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",208* <cond> should be computed in WQM */209if (info.block_needs & WQM && !(block->kind & block_kind_top_level)) {210for (unsigned pred_idx : block->logical_preds)211mark_block_wqm(ctx, pred_idx);212}213}214215/* If an outer loop needs WQM but a nested loop does not, we have to ensure that216* the nested loop is done in WQM so that the exec is not empty upon entering217* the nested loop.218*219* TODO: This could be fixed with slightly better code (for loops with divergent220* breaks, which might benefit from being in exact) by adding Exact_Branch to a221* divergent branch surrounding the nested loop, if such a branch exists.222*/223void224handle_wqm_loops(wqm_ctx& ctx, exec_ctx& exec_ctx, unsigned preheader)225{226for (unsigned idx = preheader + 1; idx < exec_ctx.program->blocks.size(); idx++) {227Block& block = exec_ctx.program->blocks[idx];228if (block.kind & block_kind_break)229mark_block_wqm(ctx, idx);230231if ((block.kind & block_kind_loop_exit) && block.loop_nest_depth == 0)232break;233}234}235236/* If an outer loop and it's nested loops does not need WQM,237* add_branch_code() will ensure that it enters in Exact. We have to238* ensure that the exact exec mask is not empty by adding Exact_Branch to239* the outer divergent branch.240*/241void242handle_exact_loops(wqm_ctx& ctx, exec_ctx& exec_ctx, unsigned preheader)243{244assert(exec_ctx.program->blocks[preheader + 1].kind & block_kind_loop_header);245246int parent_branch = preheader;247unsigned rel_branch_depth = 0;248for (; parent_branch >= 0; parent_branch--) {249Block& branch = exec_ctx.program->blocks[parent_branch];250if (branch.kind & block_kind_branch) {251if (rel_branch_depth == 0)252break;253rel_branch_depth--;254}255256/* top-level blocks should never have empty exact exec masks */257if (branch.kind & block_kind_top_level)258return;259260if (branch.kind & block_kind_merge)261rel_branch_depth++;262}263assert(parent_branch >= 0);264265ASSERTED Block& branch = exec_ctx.program->blocks[parent_branch];266assert(branch.kind & block_kind_branch);267if (ctx.branch_wqm[parent_branch]) {268/* The branch can't be done in Exact because some other blocks in it269* are in WQM. So instead, ensure that the loop is done in WQM. */270handle_wqm_loops(ctx, exec_ctx, preheader);271} else {272exec_ctx.info[parent_branch].block_needs |= Exact_Branch;273}274}275276void277calculate_wqm_needs(exec_ctx& exec_ctx)278{279wqm_ctx ctx(exec_ctx.program);280281while (!ctx.worklist.empty()) {282unsigned block_index = *std::prev(ctx.worklist.end());283ctx.worklist.erase(std::prev(ctx.worklist.end()));284285Block& block = exec_ctx.program->blocks[block_index];286get_block_needs(ctx, exec_ctx, &block);287288/* handle_exact_loops() needs information on outer branches, so don't289* handle loops until a top-level block.290*/291if (block.kind & block_kind_top_level && block.index != exec_ctx.program->blocks.size() - 1) {292unsigned preheader = block.index;293do {294Block& preheader_block = exec_ctx.program->blocks[preheader];295if ((preheader_block.kind & block_kind_loop_preheader) &&296preheader_block.loop_nest_depth == 0) {297/* If the loop or a nested loop needs WQM, branch_wqm will be true for the298* preheader.299*/300if (ctx.branch_wqm[preheader])301handle_wqm_loops(ctx, exec_ctx, preheader);302else303handle_exact_loops(ctx, exec_ctx, preheader);304}305preheader++;306} while (!(exec_ctx.program->blocks[preheader].kind & block_kind_top_level));307}308}309310uint8_t ever_again_needs = 0;311for (int i = exec_ctx.program->blocks.size() - 1; i >= 0; i--) {312exec_ctx.info[i].ever_again_needs = ever_again_needs;313Block& block = exec_ctx.program->blocks[i];314315if (block.kind & block_kind_needs_lowering)316exec_ctx.info[i].block_needs |= Exact;317318/* if discard is used somewhere in nested CF, we need to preserve the WQM mask */319if ((block.kind & block_kind_discard || block.kind & block_kind_uses_discard_if) &&320ever_again_needs & WQM)321exec_ctx.info[i].block_needs |= Preserve_WQM;322323ever_again_needs |= exec_ctx.info[i].block_needs & ~Exact_Branch;324if (block.kind & block_kind_discard || block.kind & block_kind_uses_discard_if ||325block.kind & block_kind_uses_demote)326ever_again_needs |= Exact;327328/* don't propagate WQM preservation further than the next top_level block */329if (block.kind & block_kind_top_level)330ever_again_needs &= ~Preserve_WQM;331else332exec_ctx.info[i].block_needs &= ~Preserve_WQM;333}334exec_ctx.handle_wqm = true;335}336337Operand338get_exec_op(Operand t)339{340if (t.isUndefined())341return Operand(exec, t.regClass());342else343return t;344}345346void347transition_to_WQM(exec_ctx& ctx, Builder bld, unsigned idx)348{349if (ctx.info[idx].exec.back().second & mask_type_wqm)350return;351if (ctx.info[idx].exec.back().second & mask_type_global) {352Operand exec_mask = ctx.info[idx].exec.back().first;353if (exec_mask.isUndefined()) {354exec_mask = bld.pseudo(aco_opcode::p_parallelcopy, bld.def(bld.lm), Operand(exec, bld.lm));355ctx.info[idx].exec.back().first = exec_mask;356}357358exec_mask = bld.sop1(Builder::s_wqm, Definition(exec, bld.lm), bld.def(s1, scc),359get_exec_op(exec_mask));360ctx.info[idx].exec.emplace_back(exec_mask, mask_type_global | mask_type_wqm);361return;362}363/* otherwise, the WQM mask should be one below the current mask */364ctx.info[idx].exec.pop_back();365assert(ctx.info[idx].exec.back().second & mask_type_wqm);366assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());367assert(ctx.info[idx].exec.back().first.isTemp());368ctx.info[idx].exec.back().first = bld.pseudo(369aco_opcode::p_parallelcopy, Definition(exec, bld.lm), ctx.info[idx].exec.back().first);370}371372void373transition_to_Exact(exec_ctx& ctx, Builder bld, unsigned idx)374{375if (ctx.info[idx].exec.back().second & mask_type_exact)376return;377/* We can't remove the loop exec mask, because that can cause exec.size() to378* be less than num_exec_masks. The loop exec mask also needs to be kept379* around for various uses. */380if ((ctx.info[idx].exec.back().second & mask_type_global) &&381!(ctx.info[idx].exec.back().second & mask_type_loop)) {382ctx.info[idx].exec.pop_back();383assert(ctx.info[idx].exec.back().second & mask_type_exact);384assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());385assert(ctx.info[idx].exec.back().first.isTemp());386ctx.info[idx].exec.back().first = bld.pseudo(387aco_opcode::p_parallelcopy, Definition(exec, bld.lm), ctx.info[idx].exec.back().first);388return;389}390/* otherwise, we create an exact mask and push to the stack */391Operand wqm = ctx.info[idx].exec.back().first;392if (wqm.isUndefined()) {393wqm = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),394Definition(exec, bld.lm), ctx.info[idx].exec[0].first, Operand(exec, bld.lm));395} else {396bld.sop2(Builder::s_and, Definition(exec, bld.lm), bld.def(s1, scc),397ctx.info[idx].exec[0].first, wqm);398}399ctx.info[idx].exec.back().first = Operand(wqm);400ctx.info[idx].exec.emplace_back(Operand(bld.lm), mask_type_exact);401}402403unsigned404add_coupling_code(exec_ctx& ctx, Block* block, std::vector<aco_ptr<Instruction>>& instructions)405{406unsigned idx = block->index;407Builder bld(ctx.program, &instructions);408std::vector<unsigned>& preds = block->linear_preds;409410/* start block */411if (idx == 0) {412aco_ptr<Instruction>& startpgm = block->instructions[0];413assert(startpgm->opcode == aco_opcode::p_startpgm);414bld.insert(std::move(startpgm));415416Operand start_exec(bld.lm);417418/* exec seems to need to be manually initialized with combined shaders */419if (ctx.program->stage.num_sw_stages() > 1 || ctx.program->stage.hw == HWStage::NGG) {420start_exec = Operand::c32_or_c64(-1u, bld.lm == s2);421bld.copy(Definition(exec, bld.lm), start_exec);422}423424if (ctx.handle_wqm) {425ctx.info[0].exec.emplace_back(start_exec, mask_type_global | mask_type_exact);426/* if this block only needs WQM, initialize already */427if (ctx.info[0].block_needs == WQM)428transition_to_WQM(ctx, bld, 0);429} else {430uint8_t mask = mask_type_global;431if (ctx.program->needs_wqm) {432bld.sop1(Builder::s_wqm, Definition(exec, bld.lm), bld.def(s1, scc),433Operand(exec, bld.lm));434mask |= mask_type_wqm;435} else {436mask |= mask_type_exact;437}438ctx.info[0].exec.emplace_back(start_exec, mask);439}440441return 1;442}443444/* loop entry block */445if (block->kind & block_kind_loop_header) {446assert(preds[0] == idx - 1);447ctx.info[idx].exec = ctx.info[idx - 1].exec;448loop_info& info = ctx.loop.back();449while (ctx.info[idx].exec.size() > info.num_exec_masks)450ctx.info[idx].exec.pop_back();451452/* create ssa names for outer exec masks */453if (info.has_discard) {454aco_ptr<Pseudo_instruction> phi;455for (int i = 0; i < info.num_exec_masks - 1; i++) {456phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi,457Format::PSEUDO, preds.size(), 1));458phi->definitions[0] = bld.def(bld.lm);459phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec[i].first);460ctx.info[idx].exec[i].first = bld.insert(std::move(phi));461}462}463464/* create ssa name for restore mask */465if (info.has_divergent_break) {466/* this phi might be trivial but ensures a parallelcopy on the loop header */467aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(468aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};469phi->definitions[0] = bld.def(bld.lm);470phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec[info.num_exec_masks - 1].first);471ctx.info[idx].exec.back().first = bld.insert(std::move(phi));472}473474/* create ssa name for loop active mask */475aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(476aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};477if (info.has_divergent_continue)478phi->definitions[0] = bld.def(bld.lm);479else480phi->definitions[0] = Definition(exec, bld.lm);481phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec.back().first);482Temp loop_active = bld.insert(std::move(phi));483484if (info.has_divergent_break) {485uint8_t mask_type =486(ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact)) | mask_type_loop;487ctx.info[idx].exec.emplace_back(loop_active, mask_type);488} else {489ctx.info[idx].exec.back().first = Operand(loop_active);490ctx.info[idx].exec.back().second |= mask_type_loop;491}492493/* create a parallelcopy to move the active mask to exec */494unsigned i = 0;495if (info.has_divergent_continue) {496while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {497bld.insert(std::move(block->instructions[i]));498i++;499}500uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);501assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());502ctx.info[idx].exec.emplace_back(503bld.pseudo(aco_opcode::p_parallelcopy, Definition(exec, bld.lm),504ctx.info[idx].exec.back().first),505mask_type);506}507508return i;509}510511/* loop exit block */512if (block->kind & block_kind_loop_exit) {513Block* header = ctx.loop.back().loop_header;514loop_info& info = ctx.loop.back();515516for (ASSERTED unsigned pred : preds)517assert(ctx.info[pred].exec.size() >= info.num_exec_masks);518519/* fill the loop header phis */520std::vector<unsigned>& header_preds = header->linear_preds;521int instr_idx = 0;522if (info.has_discard) {523while (instr_idx < info.num_exec_masks - 1) {524aco_ptr<Instruction>& phi = header->instructions[instr_idx];525assert(phi->opcode == aco_opcode::p_linear_phi);526for (unsigned i = 1; i < phi->operands.size(); i++)527phi->operands[i] = get_exec_op(ctx.info[header_preds[i]].exec[instr_idx].first);528instr_idx++;529}530}531532{533aco_ptr<Instruction>& phi = header->instructions[instr_idx++];534assert(phi->opcode == aco_opcode::p_linear_phi);535for (unsigned i = 1; i < phi->operands.size(); i++)536phi->operands[i] =537get_exec_op(ctx.info[header_preds[i]].exec[info.num_exec_masks - 1].first);538}539540if (info.has_divergent_break) {541aco_ptr<Instruction>& phi = header->instructions[instr_idx];542assert(phi->opcode == aco_opcode::p_linear_phi);543for (unsigned i = 1; i < phi->operands.size(); i++)544phi->operands[i] =545get_exec_op(ctx.info[header_preds[i]].exec[info.num_exec_masks].first);546}547548assert(!(block->kind & block_kind_top_level) || info.num_exec_masks <= 2);549550/* create the loop exit phis if not trivial */551for (unsigned exec_idx = 0; exec_idx < info.num_exec_masks; exec_idx++) {552Operand same = ctx.info[preds[0]].exec[exec_idx].first;553uint8_t type = ctx.info[header_preds[0]].exec[exec_idx].second;554bool trivial = true;555556for (unsigned i = 1; i < preds.size() && trivial; i++) {557if (ctx.info[preds[i]].exec[exec_idx].first != same)558trivial = false;559}560561if (trivial) {562ctx.info[idx].exec.emplace_back(same, type);563} else {564/* create phi for loop footer */565aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(566aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};567phi->definitions[0] = bld.def(bld.lm);568if (exec_idx == info.num_exec_masks - 1u) {569phi->definitions[0] = Definition(exec, bld.lm);570}571for (unsigned i = 0; i < phi->operands.size(); i++)572phi->operands[i] = get_exec_op(ctx.info[preds[i]].exec[exec_idx].first);573ctx.info[idx].exec.emplace_back(bld.insert(std::move(phi)), type);574}575}576assert(ctx.info[idx].exec.size() == info.num_exec_masks);577578/* create a parallelcopy to move the live mask to exec */579unsigned i = 0;580while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {581bld.insert(std::move(block->instructions[i]));582i++;583}584585if (ctx.handle_wqm) {586if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {587if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||588(ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {589ctx.info[idx].exec.back().second |= mask_type_global;590transition_to_Exact(ctx, bld, idx);591ctx.handle_wqm = false;592}593}594if (ctx.info[idx].block_needs == WQM)595transition_to_WQM(ctx, bld, idx);596else if (ctx.info[idx].block_needs == Exact)597transition_to_Exact(ctx, bld, idx);598}599600assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());601if (get_exec_op(ctx.info[idx].exec.back().first).isTemp()) {602/* move current exec mask into exec register */603ctx.info[idx].exec.back().first = bld.pseudo(604aco_opcode::p_parallelcopy, Definition(exec, bld.lm), ctx.info[idx].exec.back().first);605}606607ctx.loop.pop_back();608return i;609}610611if (preds.size() == 1) {612ctx.info[idx].exec = ctx.info[preds[0]].exec;613} else {614assert(preds.size() == 2);615/* if one of the predecessors ends in exact mask, we pop it from stack */616unsigned num_exec_masks =617std::min(ctx.info[preds[0]].exec.size(), ctx.info[preds[1]].exec.size());618619if (block->kind & block_kind_merge)620num_exec_masks--;621if (block->kind & block_kind_top_level)622num_exec_masks = std::min(num_exec_masks, 2u);623624/* create phis for diverged exec masks */625for (unsigned i = 0; i < num_exec_masks; i++) {626/* skip trivial phis */627if (ctx.info[preds[0]].exec[i].first == ctx.info[preds[1]].exec[i].first) {628Operand t = ctx.info[preds[0]].exec[i].first;629/* discard/demote can change the state of the current exec mask */630assert(!t.isTemp() ||631ctx.info[preds[0]].exec[i].second == ctx.info[preds[1]].exec[i].second);632uint8_t mask = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;633ctx.info[idx].exec.emplace_back(t, mask);634continue;635}636637bool in_exec = i == num_exec_masks - 1 && !(block->kind & block_kind_merge);638Temp phi = bld.pseudo(aco_opcode::p_linear_phi,639in_exec ? Definition(exec, bld.lm) : bld.def(bld.lm),640get_exec_op(ctx.info[preds[0]].exec[i].first),641get_exec_op(ctx.info[preds[1]].exec[i].first));642uint8_t mask_type = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;643ctx.info[idx].exec.emplace_back(phi, mask_type);644}645}646647unsigned i = 0;648while (block->instructions[i]->opcode == aco_opcode::p_phi ||649block->instructions[i]->opcode == aco_opcode::p_linear_phi) {650bld.insert(std::move(block->instructions[i]));651i++;652}653654/* try to satisfy the block's needs */655if (ctx.handle_wqm) {656if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {657if ((ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == 0 ||658(ctx.info[idx].block_needs | ctx.info[idx].ever_again_needs) == Exact) {659ctx.info[idx].exec.back().second |= mask_type_global;660transition_to_Exact(ctx, bld, idx);661ctx.handle_wqm = false;662}663}664if (ctx.info[idx].block_needs == WQM)665transition_to_WQM(ctx, bld, idx);666else if (ctx.info[idx].block_needs == Exact)667transition_to_Exact(ctx, bld, idx);668}669670if (block->kind & block_kind_merge && !ctx.info[idx].exec.back().first.isUndefined()) {671Operand restore = ctx.info[idx].exec.back().first;672assert(restore.size() == bld.lm.size());673bld.pseudo(aco_opcode::p_parallelcopy, Definition(exec, bld.lm), restore);674if (!restore.isConstant())675ctx.info[idx].exec.back().first = Operand(bld.lm);676}677678return i;679}680681void682process_instructions(exec_ctx& ctx, Block* block, std::vector<aco_ptr<Instruction>>& instructions,683unsigned idx)684{685WQMState state;686if (ctx.info[block->index].exec.back().second & mask_type_wqm)687state = WQM;688else {689assert(!ctx.handle_wqm || ctx.info[block->index].exec.back().second & mask_type_exact);690state = Exact;691}692693/* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */694bool process = (ctx.handle_wqm && (ctx.info[block->index].block_needs & state) !=695(ctx.info[block->index].block_needs & (WQM | Exact))) ||696block->kind & block_kind_uses_discard_if ||697block->kind & block_kind_uses_demote || block->kind & block_kind_needs_lowering;698if (!process) {699std::vector<aco_ptr<Instruction>>::iterator it = std::next(block->instructions.begin(), idx);700instructions.insert(instructions.end(),701std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(it),702std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(703block->instructions.end()));704return;705}706707Builder bld(ctx.program, &instructions);708709for (; idx < block->instructions.size(); idx++) {710aco_ptr<Instruction> instr = std::move(block->instructions[idx]);711712WQMState needs = ctx.handle_wqm ? ctx.info[block->index].instr_needs[idx] : Unspecified;713714if (instr->opcode == aco_opcode::p_discard_if) {715if (ctx.info[block->index].block_needs & Preserve_WQM) {716assert(block->kind & block_kind_top_level);717transition_to_WQM(ctx, bld, block->index);718ctx.info[block->index].exec.back().second &= ~mask_type_global;719}720int num = ctx.info[block->index].exec.size();721assert(num);722723/* discard from current exec */724const Operand cond = instr->operands[0];725Temp exit_cond = bld.sop2(Builder::s_andn2, Definition(exec, bld.lm), bld.def(s1, scc),726Operand(exec, bld.lm), cond)727.def(1)728.getTemp();729730/* discard from inner to outer exec mask on stack */731for (int i = num - 2; i >= 0; i--) {732Instruction* andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),733ctx.info[block->index].exec[i].first, cond);734ctx.info[block->index].exec[i].first = Operand(andn2->definitions[0].getTemp());735exit_cond = andn2->definitions[1].getTemp();736}737738instr->opcode = aco_opcode::p_exit_early_if;739instr->operands[0] = bld.scc(exit_cond);740assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);741742} else if (needs == WQM && state != WQM) {743transition_to_WQM(ctx, bld, block->index);744state = WQM;745} else if (needs == Exact && state != Exact) {746transition_to_Exact(ctx, bld, block->index);747state = Exact;748}749750if (instr->opcode == aco_opcode::p_is_helper) {751Definition dst = instr->definitions[0];752assert(dst.size() == bld.lm.size());753if (state == Exact) {754instr.reset(create_instruction<SOP1_instruction>(bld.w64or32(Builder::s_mov),755Format::SOP1, 1, 1));756instr->operands[0] = Operand::zero();757instr->definitions[0] = dst;758} else {759std::pair<Operand, uint8_t>& exact_mask = ctx.info[block->index].exec[0];760assert(exact_mask.second & mask_type_exact);761762instr.reset(create_instruction<SOP2_instruction>(bld.w64or32(Builder::s_andn2),763Format::SOP2, 2, 2));764instr->operands[0] = Operand(exec, bld.lm); /* current exec */765instr->operands[1] = Operand(exact_mask.first);766instr->definitions[0] = dst;767instr->definitions[1] = bld.def(s1, scc);768}769} else if (instr->opcode == aco_opcode::p_demote_to_helper) {770/* turn demote into discard_if with only exact masks */771assert((ctx.info[block->index].exec[0].second & (mask_type_exact | mask_type_global)) ==772(mask_type_exact | mask_type_global));773774int num;775Temp cond, exit_cond;776if (instr->operands[0].isConstant()) {777assert(instr->operands[0].constantValue() == -1u);778/* transition to exact and set exec to zero */779exit_cond = bld.tmp(s1);780cond =781bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.scc(Definition(exit_cond)),782Definition(exec, bld.lm), Operand::zero(), Operand(exec, bld.lm));783784num = ctx.info[block->index].exec.size() - 2;785if (!(ctx.info[block->index].exec.back().second & mask_type_exact)) {786ctx.info[block->index].exec.back().first = Operand(cond);787ctx.info[block->index].exec.emplace_back(Operand(bld.lm), mask_type_exact);788}789} else {790/* demote_if: transition to exact */791transition_to_Exact(ctx, bld, block->index);792assert(instr->operands[0].isTemp());793cond = instr->operands[0].getTemp();794num = ctx.info[block->index].exec.size() - 1;795}796797for (int i = num; i >= 0; i--) {798if (ctx.info[block->index].exec[i].second & mask_type_exact) {799Instruction* andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),800ctx.info[block->index].exec[i].first, cond);801if (i == (int)ctx.info[block->index].exec.size() - 1) {802andn2->operands[0] = Operand(exec, bld.lm);803andn2->definitions[0] = Definition(exec, bld.lm);804}805806ctx.info[block->index].exec[i].first = Operand(andn2->definitions[0].getTemp());807exit_cond = andn2->definitions[1].getTemp();808} else {809assert(i != 0);810}811}812instr->opcode = aco_opcode::p_exit_early_if;813instr->operands[0] = bld.scc(exit_cond);814state = Exact;815}816817bld.insert(std::move(instr));818}819}820821void822add_branch_code(exec_ctx& ctx, Block* block)823{824unsigned idx = block->index;825Builder bld(ctx.program, block);826827if (idx == ctx.program->blocks.size() - 1)828return;829830/* try to disable wqm handling */831if (ctx.handle_wqm && block->kind & block_kind_top_level) {832if (ctx.info[idx].exec.size() == 3) {833assert(ctx.info[idx].exec[1].second == mask_type_wqm);834ctx.info[idx].exec.pop_back();835}836assert(ctx.info[idx].exec.size() <= 2);837838if (ctx.info[idx].ever_again_needs == 0 || ctx.info[idx].ever_again_needs == Exact) {839/* transition to Exact */840aco_ptr<Instruction> branch = std::move(block->instructions.back());841block->instructions.pop_back();842ctx.info[idx].exec.back().second |= mask_type_global;843transition_to_Exact(ctx, bld, idx);844bld.insert(std::move(branch));845ctx.handle_wqm = false;846847} else if (ctx.info[idx].block_needs & Preserve_WQM) {848/* transition to WQM and remove global flag */849aco_ptr<Instruction> branch = std::move(block->instructions.back());850block->instructions.pop_back();851transition_to_WQM(ctx, bld, idx);852ctx.info[idx].exec.back().second &= ~mask_type_global;853bld.insert(std::move(branch));854}855}856857if (block->kind & block_kind_loop_preheader) {858/* collect information about the succeeding loop */859bool has_divergent_break = false;860bool has_divergent_continue = false;861bool has_discard = false;862uint8_t needs = 0;863unsigned loop_nest_depth = ctx.program->blocks[idx + 1].loop_nest_depth;864865for (unsigned i = idx + 1; ctx.program->blocks[i].loop_nest_depth >= loop_nest_depth; i++) {866Block& loop_block = ctx.program->blocks[i];867needs |= ctx.info[i].block_needs;868869if (loop_block.kind & block_kind_uses_discard_if || loop_block.kind & block_kind_discard ||870loop_block.kind & block_kind_uses_demote)871has_discard = true;872if (loop_block.loop_nest_depth != loop_nest_depth)873continue;874875if (loop_block.kind & block_kind_uniform)876continue;877else if (loop_block.kind & block_kind_break)878has_divergent_break = true;879else if (loop_block.kind & block_kind_continue)880has_divergent_continue = true;881}882883if (ctx.handle_wqm) {884if (needs & WQM) {885aco_ptr<Instruction> branch = std::move(block->instructions.back());886block->instructions.pop_back();887transition_to_WQM(ctx, bld, idx);888bld.insert(std::move(branch));889} else {890aco_ptr<Instruction> branch = std::move(block->instructions.back());891block->instructions.pop_back();892transition_to_Exact(ctx, bld, idx);893bld.insert(std::move(branch));894}895}896897unsigned num_exec_masks = ctx.info[idx].exec.size();898if (block->kind & block_kind_top_level)899num_exec_masks = std::min(num_exec_masks, 2u);900901ctx.loop.emplace_back(&ctx.program->blocks[block->linear_succs[0]], num_exec_masks, needs,902has_divergent_break, has_divergent_continue, has_discard);903}904905/* For normal breaks, this is the exec mask. For discard+break, it's the906* old exec mask before it was zero'd.907*/908Operand break_cond = Operand(exec, bld.lm);909910if (block->kind & block_kind_discard) {911912assert(block->instructions.back()->isBranch());913aco_ptr<Instruction> branch = std::move(block->instructions.back());914block->instructions.pop_back();915916/* create a discard_if() instruction with the exec mask as condition */917unsigned num = 0;918if (ctx.loop.size()) {919/* if we're in a loop, only discard from the outer exec masks */920num = ctx.loop.back().num_exec_masks;921} else {922num = ctx.info[idx].exec.size() - 1;923}924925Temp cond = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),926Definition(exec, bld.lm), Operand::zero(), Operand(exec, bld.lm));927928for (int i = num - 1; i >= 0; i--) {929Instruction* andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),930get_exec_op(ctx.info[block->index].exec[i].first), cond);931if (i == (int)ctx.info[idx].exec.size() - 1)932andn2->definitions[0] = Definition(exec, bld.lm);933if (i == 0)934bld.pseudo(aco_opcode::p_exit_early_if, bld.scc(andn2->definitions[1].getTemp()));935ctx.info[block->index].exec[i].first = Operand(andn2->definitions[0].getTemp());936}937assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);938939break_cond = Operand(cond);940bld.insert(std::move(branch));941/* no return here as it can be followed by a divergent break */942}943944if (block->kind & block_kind_continue_or_break) {945assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[1]].linear_succs[0]].kind &946block_kind_loop_header);947assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[0]].linear_succs[0]].kind &948block_kind_loop_exit);949assert(block->instructions.back()->opcode == aco_opcode::p_branch);950block->instructions.pop_back();951952bool need_parallelcopy = false;953while (!(ctx.info[idx].exec.back().second & mask_type_loop)) {954ctx.info[idx].exec.pop_back();955need_parallelcopy = true;956}957958if (need_parallelcopy)959ctx.info[idx].exec.back().first = bld.pseudo(960aco_opcode::p_parallelcopy, Definition(exec, bld.lm), ctx.info[idx].exec.back().first);961bld.branch(aco_opcode::p_cbranch_nz, bld.hint_vcc(bld.def(s2)), Operand(exec, bld.lm),962block->linear_succs[1], block->linear_succs[0]);963return;964}965966if (block->kind & block_kind_uniform) {967Pseudo_branch_instruction& branch = block->instructions.back()->branch();968if (branch.opcode == aco_opcode::p_branch) {969branch.target[0] = block->linear_succs[0];970} else {971branch.target[0] = block->linear_succs[1];972branch.target[1] = block->linear_succs[0];973}974return;975}976977if (block->kind & block_kind_branch) {978979if (ctx.handle_wqm && ctx.info[idx].exec.size() >= 2 &&980ctx.info[idx].exec.back().second == mask_type_exact &&981!(ctx.info[idx].block_needs & Exact_Branch) &&982ctx.info[idx].exec[ctx.info[idx].exec.size() - 2].second & mask_type_wqm) {983/* return to wqm before branching */984ctx.info[idx].exec.pop_back();985}986987// orig = s_and_saveexec_b64988assert(block->linear_succs.size() == 2);989assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_z);990Temp cond = block->instructions.back()->operands[0].getTemp();991block->instructions.pop_back();992993if (ctx.info[idx].block_needs & Exact_Branch)994transition_to_Exact(ctx, bld, idx);995996uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);997if (ctx.info[idx].exec.back().first.constantEquals(-1u)) {998bld.pseudo(aco_opcode::p_parallelcopy, Definition(exec, bld.lm), cond);999} else {1000Temp old_exec = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),1001Definition(exec, bld.lm), cond, Operand(exec, bld.lm));10021003ctx.info[idx].exec.back().first = Operand(old_exec);1004}10051006/* add next current exec to the stack */1007ctx.info[idx].exec.emplace_back(Operand(bld.lm), mask_type);10081009bld.branch(aco_opcode::p_cbranch_z, bld.hint_vcc(bld.def(s2)), Operand(exec, bld.lm),1010block->linear_succs[1], block->linear_succs[0]);1011return;1012}10131014if (block->kind & block_kind_invert) {1015// exec = s_andn2_b64 (original_exec, exec)1016assert(block->instructions.back()->opcode == aco_opcode::p_branch);1017block->instructions.pop_back();1018assert(ctx.info[idx].exec.size() >= 2);1019Operand orig_exec = ctx.info[idx].exec[ctx.info[idx].exec.size() - 2].first;1020bld.sop2(Builder::s_andn2, Definition(exec, bld.lm), bld.def(s1, scc), orig_exec,1021Operand(exec, bld.lm));10221023bld.branch(aco_opcode::p_cbranch_z, bld.hint_vcc(bld.def(s2)), Operand(exec, bld.lm),1024block->linear_succs[1], block->linear_succs[0]);1025return;1026}10271028if (block->kind & block_kind_break) {1029// loop_mask = s_andn2_b64 (loop_mask, exec)1030assert(block->instructions.back()->opcode == aco_opcode::p_branch);1031block->instructions.pop_back();10321033Temp cond = Temp();1034for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {1035cond = bld.tmp(s1);1036Operand exec_mask = ctx.info[idx].exec[exec_idx].first;1037exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),1038exec_mask, break_cond);1039ctx.info[idx].exec[exec_idx].first = exec_mask;1040if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)1041break;1042}10431044/* check if the successor is the merge block, otherwise set exec to 0 */1045// TODO: this could be done better by directly branching to the merge block1046unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];1047Block& succ = ctx.program->blocks[succ_idx];1048if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {1049bld.copy(Definition(exec, bld.lm), Operand::zero(bld.lm.bytes()));1050}10511052bld.branch(aco_opcode::p_cbranch_nz, bld.hint_vcc(bld.def(s2)), bld.scc(cond),1053block->linear_succs[1], block->linear_succs[0]);1054return;1055}10561057if (block->kind & block_kind_continue) {1058assert(block->instructions.back()->opcode == aco_opcode::p_branch);1059block->instructions.pop_back();10601061Temp cond = Temp();1062for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {1063if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)1064break;1065cond = bld.tmp(s1);1066Operand exec_mask = ctx.info[idx].exec[exec_idx].first;1067exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),1068exec_mask, Operand(exec, bld.lm));1069ctx.info[idx].exec[exec_idx].first = exec_mask;1070}1071assert(cond != Temp());10721073/* check if the successor is the merge block, otherwise set exec to 0 */1074// TODO: this could be done better by directly branching to the merge block1075unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];1076Block& succ = ctx.program->blocks[succ_idx];1077if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {1078bld.copy(Definition(exec, bld.lm), Operand::zero(bld.lm.bytes()));1079}10801081bld.branch(aco_opcode::p_cbranch_nz, bld.hint_vcc(bld.def(s2)), bld.scc(cond),1082block->linear_succs[1], block->linear_succs[0]);1083return;1084}1085}10861087void1088process_block(exec_ctx& ctx, Block* block)1089{1090std::vector<aco_ptr<Instruction>> instructions;1091instructions.reserve(block->instructions.size());10921093unsigned idx = add_coupling_code(ctx, block, instructions);10941095assert(block->index != ctx.program->blocks.size() - 1 ||1096ctx.info[block->index].exec.size() <= 2);10971098process_instructions(ctx, block, instructions, idx);10991100block->instructions = std::move(instructions);11011102add_branch_code(ctx, block);1103}11041105} /* end namespace */11061107void1108insert_exec_mask(Program* program)1109{1110exec_ctx ctx(program);11111112if (program->needs_wqm && program->needs_exact)1113calculate_wqm_needs(ctx);11141115for (Block& block : program->blocks)1116process_block(ctx, &block);1117}11181119} // namespace aco112011211122