Path: blob/21.2-virgl/src/amd/compiler/aco_ir.cpp
4550 views
/*1* Copyright © 2020 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_ir.h"2526#include "util/debug.h"2728#include "c11/threads.h"2930namespace aco {3132uint64_t debug_flags = 0;3334static const struct debug_control aco_debug_options[] = {{"validateir", DEBUG_VALIDATE_IR},35{"validatera", DEBUG_VALIDATE_RA},36{"perfwarn", DEBUG_PERFWARN},37{"force-waitcnt", DEBUG_FORCE_WAITCNT},38{"novn", DEBUG_NO_VN},39{"noopt", DEBUG_NO_OPT},40{"nosched", DEBUG_NO_SCHED},41{"perfinfo", DEBUG_PERF_INFO},42{"liveinfo", DEBUG_LIVE_INFO},43{NULL, 0}};4445static once_flag init_once_flag = ONCE_FLAG_INIT;4647static void48init_once()49{50debug_flags = parse_debug_string(getenv("ACO_DEBUG"), aco_debug_options);5152#ifndef NDEBUG53/* enable some flags by default on debug builds */54debug_flags |= aco::DEBUG_VALIDATE_IR;55#endif56}5758void59init()60{61call_once(&init_once_flag, init_once);62}6364void65init_program(Program* program, Stage stage, struct radv_shader_info* info,66enum chip_class chip_class, enum radeon_family family, bool wgp_mode,67ac_shader_config* config)68{69program->stage = stage;70program->config = config;71program->info = info;72program->chip_class = chip_class;73if (family == CHIP_UNKNOWN) {74switch (chip_class) {75case GFX6: program->family = CHIP_TAHITI; break;76case GFX7: program->family = CHIP_BONAIRE; break;77case GFX8: program->family = CHIP_POLARIS10; break;78case GFX9: program->family = CHIP_VEGA10; break;79case GFX10: program->family = CHIP_NAVI10; break;80default: program->family = CHIP_UNKNOWN; break;81}82} else {83program->family = family;84}85program->wave_size = info->wave_size;86program->lane_mask = program->wave_size == 32 ? s1 : s2;8788program->dev.lds_encoding_granule = chip_class >= GFX7 ? 512 : 256;89program->dev.lds_alloc_granule =90chip_class >= GFX10_3 ? 1024 : program->dev.lds_encoding_granule;91program->dev.lds_limit = chip_class >= GFX7 ? 65536 : 32768;92/* apparently gfx702 also has 16-bank LDS but I can't find a family for that */93program->dev.has_16bank_lds = family == CHIP_KABINI || family == CHIP_STONEY;9495program->dev.vgpr_limit = 256;96program->dev.physical_vgprs = 256;97program->dev.vgpr_alloc_granule = 4;9899if (chip_class >= GFX10) {100program->dev.physical_sgprs = 5120; /* doesn't matter as long as it's at least 128 * 40 */101program->dev.physical_vgprs = program->wave_size == 32 ? 1024 : 512;102program->dev.sgpr_alloc_granule = 128;103program->dev.sgpr_limit =104108; /* includes VCC, which can be treated as s[106-107] on GFX10+ */105if (chip_class >= GFX10_3)106program->dev.vgpr_alloc_granule = program->wave_size == 32 ? 16 : 8;107else108program->dev.vgpr_alloc_granule = program->wave_size == 32 ? 8 : 4;109} else if (program->chip_class >= GFX8) {110program->dev.physical_sgprs = 800;111program->dev.sgpr_alloc_granule = 16;112program->dev.sgpr_limit = 102;113if (family == CHIP_TONGA || family == CHIP_ICELAND)114program->dev.sgpr_alloc_granule = 96; /* workaround hardware bug */115} else {116program->dev.physical_sgprs = 512;117program->dev.sgpr_alloc_granule = 8;118program->dev.sgpr_limit = 104;119}120121program->dev.max_wave64_per_simd = 10;122if (program->chip_class >= GFX10_3)123program->dev.max_wave64_per_simd = 16;124else if (program->chip_class == GFX10)125program->dev.max_wave64_per_simd = 20;126else if (program->family >= CHIP_POLARIS10 && program->family <= CHIP_VEGAM)127program->dev.max_wave64_per_simd = 8;128129program->dev.simd_per_cu = program->chip_class >= GFX10 ? 2 : 4;130131switch (program->family) {132/* GFX8 APUs */133case CHIP_CARRIZO:134case CHIP_STONEY:135/* GFX9 APUS */136case CHIP_RAVEN:137case CHIP_RAVEN2:138case CHIP_RENOIR: program->dev.xnack_enabled = true; break;139default: break;140}141142program->dev.sram_ecc_enabled = program->family == CHIP_ARCTURUS;143/* apparently gfx702 also has fast v_fma_f32 but I can't find a family for that */144program->dev.has_fast_fma32 = program->chip_class >= GFX9;145if (program->family == CHIP_TAHITI || program->family == CHIP_CARRIZO ||146program->family == CHIP_HAWAII)147program->dev.has_fast_fma32 = true;148149program->wgp_mode = wgp_mode;150151program->progress = CompilationProgress::after_isel;152153program->next_fp_mode.preserve_signed_zero_inf_nan32 = false;154program->next_fp_mode.preserve_signed_zero_inf_nan16_64 = false;155program->next_fp_mode.must_flush_denorms32 = false;156program->next_fp_mode.must_flush_denorms16_64 = false;157program->next_fp_mode.care_about_round32 = false;158program->next_fp_mode.care_about_round16_64 = false;159program->next_fp_mode.denorm16_64 = fp_denorm_keep;160program->next_fp_mode.denorm32 = 0;161program->next_fp_mode.round16_64 = fp_round_ne;162program->next_fp_mode.round32 = fp_round_ne;163}164165memory_sync_info166get_sync_info(const Instruction* instr)167{168switch (instr->format) {169case Format::SMEM: return instr->smem().sync;170case Format::MUBUF: return instr->mubuf().sync;171case Format::MIMG: return instr->mimg().sync;172case Format::MTBUF: return instr->mtbuf().sync;173case Format::FLAT:174case Format::GLOBAL:175case Format::SCRATCH: return instr->flatlike().sync;176case Format::DS: return instr->ds().sync;177default: return memory_sync_info();178}179}180181bool182can_use_SDWA(chip_class chip, const aco_ptr<Instruction>& instr, bool pre_ra)183{184if (!instr->isVALU())185return false;186187if (chip < GFX8 || instr->isDPP())188return false;189190if (instr->isSDWA())191return true;192193if (instr->isVOP3()) {194VOP3_instruction& vop3 = instr->vop3();195if (instr->format == Format::VOP3)196return false;197if (vop3.clamp && instr->format == asVOP3(Format::VOPC) && chip != GFX8)198return false;199if (vop3.omod && chip < GFX9)200return false;201202// TODO: return true if we know we will use vcc203if (!pre_ra && instr->definitions.size() >= 2)204return false;205206for (unsigned i = 1; i < instr->operands.size(); i++) {207if (instr->operands[i].isLiteral())208return false;209if (chip < GFX9 && !instr->operands[i].isOfType(RegType::vgpr))210return false;211}212}213214if (!instr->definitions.empty() && instr->definitions[0].bytes() > 4)215return false;216217if (!instr->operands.empty()) {218if (instr->operands[0].isLiteral())219return false;220if (chip < GFX9 && !instr->operands[0].isOfType(RegType::vgpr))221return false;222if (instr->operands[0].bytes() > 4)223return false;224if (instr->operands.size() > 1 && instr->operands[1].bytes() > 4)225return false;226}227228bool is_mac = instr->opcode == aco_opcode::v_mac_f32 || instr->opcode == aco_opcode::v_mac_f16 ||229instr->opcode == aco_opcode::v_fmac_f32 || instr->opcode == aco_opcode::v_fmac_f16;230231if (chip != GFX8 && is_mac)232return false;233234// TODO: return true if we know we will use vcc235if (!pre_ra && instr->isVOPC())236return false;237if (!pre_ra && instr->operands.size() >= 3 && !is_mac)238return false;239240return instr->opcode != aco_opcode::v_madmk_f32 && instr->opcode != aco_opcode::v_madak_f32 &&241instr->opcode != aco_opcode::v_madmk_f16 && instr->opcode != aco_opcode::v_madak_f16 &&242instr->opcode != aco_opcode::v_readfirstlane_b32 &&243instr->opcode != aco_opcode::v_clrexcp && instr->opcode != aco_opcode::v_swap_b32;244}245246/* updates "instr" and returns the old instruction (or NULL if no update was needed) */247aco_ptr<Instruction>248convert_to_SDWA(chip_class chip, aco_ptr<Instruction>& instr)249{250if (instr->isSDWA())251return NULL;252253aco_ptr<Instruction> tmp = std::move(instr);254Format format =255(Format)(((uint16_t)tmp->format & ~(uint16_t)Format::VOP3) | (uint16_t)Format::SDWA);256instr.reset(create_instruction<SDWA_instruction>(tmp->opcode, format, tmp->operands.size(),257tmp->definitions.size()));258std::copy(tmp->operands.cbegin(), tmp->operands.cend(), instr->operands.begin());259std::copy(tmp->definitions.cbegin(), tmp->definitions.cend(), instr->definitions.begin());260261SDWA_instruction& sdwa = instr->sdwa();262263if (tmp->isVOP3()) {264VOP3_instruction& vop3 = tmp->vop3();265memcpy(sdwa.neg, vop3.neg, sizeof(sdwa.neg));266memcpy(sdwa.abs, vop3.abs, sizeof(sdwa.abs));267sdwa.omod = vop3.omod;268sdwa.clamp = vop3.clamp;269}270271for (unsigned i = 0; i < instr->operands.size(); i++) {272/* SDWA only uses operands 0 and 1. */273if (i >= 2)274break;275276switch (instr->operands[i].bytes()) {277case 1: sdwa.sel[i] = sdwa_ubyte; break;278case 2: sdwa.sel[i] = sdwa_uword; break;279case 4: sdwa.sel[i] = sdwa_udword; break;280}281}282switch (instr->definitions[0].bytes()) {283case 1:284sdwa.dst_sel = sdwa_ubyte;285sdwa.dst_preserve = true;286break;287case 2:288sdwa.dst_sel = sdwa_uword;289sdwa.dst_preserve = true;290break;291case 4: sdwa.dst_sel = sdwa_udword; break;292}293294if (instr->definitions[0].getTemp().type() == RegType::sgpr && chip == GFX8)295instr->definitions[0].setFixed(vcc);296if (instr->definitions.size() >= 2)297instr->definitions[1].setFixed(vcc);298if (instr->operands.size() >= 3)299instr->operands[2].setFixed(vcc);300301return tmp;302}303304bool305can_use_opsel(chip_class chip, aco_opcode op, int idx, bool high)306{307/* opsel is only GFX9+ */308if ((high || idx == -1) && chip < GFX9)309return false;310311switch (op) {312case aco_opcode::v_div_fixup_f16:313case aco_opcode::v_fma_f16:314case aco_opcode::v_mad_f16:315case aco_opcode::v_mad_u16:316case aco_opcode::v_mad_i16:317case aco_opcode::v_med3_f16:318case aco_opcode::v_med3_i16:319case aco_opcode::v_med3_u16:320case aco_opcode::v_min3_f16:321case aco_opcode::v_min3_i16:322case aco_opcode::v_min3_u16:323case aco_opcode::v_max3_f16:324case aco_opcode::v_max3_i16:325case aco_opcode::v_max3_u16:326case aco_opcode::v_max_u16_e64:327case aco_opcode::v_max_i16_e64:328case aco_opcode::v_min_u16_e64:329case aco_opcode::v_min_i16_e64:330case aco_opcode::v_add_i16:331case aco_opcode::v_sub_i16:332case aco_opcode::v_add_u16_e64:333case aco_opcode::v_sub_u16_e64:334case aco_opcode::v_lshlrev_b16_e64:335case aco_opcode::v_lshrrev_b16_e64:336case aco_opcode::v_ashrrev_i16_e64:337case aco_opcode::v_mul_lo_u16_e64: return true;338case aco_opcode::v_pack_b32_f16:339case aco_opcode::v_cvt_pknorm_i16_f16:340case aco_opcode::v_cvt_pknorm_u16_f16: return idx != -1;341case aco_opcode::v_mad_u32_u16:342case aco_opcode::v_mad_i32_i16: return idx >= 0 && idx < 2;343default: return false;344}345}346347uint32_t348get_reduction_identity(ReduceOp op, unsigned idx)349{350switch (op) {351case iadd8:352case iadd16:353case iadd32:354case iadd64:355case fadd16:356case fadd32:357case fadd64:358case ior8:359case ior16:360case ior32:361case ior64:362case ixor8:363case ixor16:364case ixor32:365case ixor64:366case umax8:367case umax16:368case umax32:369case umax64: return 0;370case imul8:371case imul16:372case imul32:373case imul64: return idx ? 0 : 1;374case fmul16: return 0x3c00u; /* 1.0 */375case fmul32: return 0x3f800000u; /* 1.0 */376case fmul64: return idx ? 0x3ff00000u : 0u; /* 1.0 */377case imin8: return INT8_MAX;378case imin16: return INT16_MAX;379case imin32: return INT32_MAX;380case imin64: return idx ? 0x7fffffffu : 0xffffffffu;381case imax8: return INT8_MIN;382case imax16: return INT16_MIN;383case imax32: return INT32_MIN;384case imax64: return idx ? 0x80000000u : 0;385case umin8:386case umin16:387case iand8:388case iand16: return 0xffffffffu;389case umin32:390case umin64:391case iand32:392case iand64: return 0xffffffffu;393case fmin16: return 0x7c00u; /* infinity */394case fmin32: return 0x7f800000u; /* infinity */395case fmin64: return idx ? 0x7ff00000u : 0u; /* infinity */396case fmax16: return 0xfc00u; /* negative infinity */397case fmax32: return 0xff800000u; /* negative infinity */398case fmax64: return idx ? 0xfff00000u : 0u; /* negative infinity */399default: unreachable("Invalid reduction operation"); break;400}401return 0;402}403404bool405needs_exec_mask(const Instruction* instr)406{407if (instr->isSALU() || instr->isBranch())408return instr->reads_exec();409if (instr->isSMEM())410return false;411if (instr->isBarrier())412return false;413414if (instr->isPseudo()) {415switch (instr->opcode) {416case aco_opcode::p_create_vector:417case aco_opcode::p_extract_vector:418case aco_opcode::p_split_vector:419case aco_opcode::p_phi:420case aco_opcode::p_parallelcopy:421for (Definition def : instr->definitions) {422if (def.getTemp().type() == RegType::vgpr)423return true;424}425return false;426case aco_opcode::p_spill:427case aco_opcode::p_reload:428case aco_opcode::p_logical_start:429case aco_opcode::p_logical_end:430case aco_opcode::p_startpgm: return false;431default: break;432}433}434435if (instr->opcode == aco_opcode::v_readlane_b32 ||436instr->opcode == aco_opcode::v_readlane_b32_e64 ||437instr->opcode == aco_opcode::v_writelane_b32 ||438instr->opcode == aco_opcode::v_writelane_b32_e64)439return false;440441return true;442}443444wait_imm::wait_imm() : vm(unset_counter), exp(unset_counter), lgkm(unset_counter), vs(unset_counter)445{}446wait_imm::wait_imm(uint16_t vm_, uint16_t exp_, uint16_t lgkm_, uint16_t vs_)447: vm(vm_), exp(exp_), lgkm(lgkm_), vs(vs_)448{}449450wait_imm::wait_imm(enum chip_class chip, uint16_t packed) : vs(unset_counter)451{452vm = packed & 0xf;453if (chip >= GFX9)454vm |= (packed >> 10) & 0x30;455456exp = (packed >> 4) & 0x7;457458lgkm = (packed >> 8) & 0xf;459if (chip >= GFX10)460lgkm |= (packed >> 8) & 0x30;461}462463uint16_t464wait_imm::pack(enum chip_class chip) const465{466uint16_t imm = 0;467assert(exp == unset_counter || exp <= 0x7);468switch (chip) {469case GFX10:470case GFX10_3:471assert(lgkm == unset_counter || lgkm <= 0x3f);472assert(vm == unset_counter || vm <= 0x3f);473imm = ((vm & 0x30) << 10) | ((lgkm & 0x3f) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);474break;475case GFX9:476assert(lgkm == unset_counter || lgkm <= 0xf);477assert(vm == unset_counter || vm <= 0x3f);478imm = ((vm & 0x30) << 10) | ((lgkm & 0xf) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);479break;480default:481assert(lgkm == unset_counter || lgkm <= 0xf);482assert(vm == unset_counter || vm <= 0xf);483imm = ((lgkm & 0xf) << 8) | ((exp & 0x7) << 4) | (vm & 0xf);484break;485}486if (chip < GFX9 && vm == wait_imm::unset_counter)487imm |= 0xc000; /* should have no effect on pre-GFX9 and now we won't have to worry about the488architecture when interpreting the immediate */489if (chip < GFX10 && lgkm == wait_imm::unset_counter)490imm |= 0x3000; /* should have no effect on pre-GFX10 and now we won't have to worry about the491architecture when interpreting the immediate */492return imm;493}494495bool496wait_imm::combine(const wait_imm& other)497{498bool changed = other.vm < vm || other.exp < exp || other.lgkm < lgkm || other.vs < vs;499vm = std::min(vm, other.vm);500exp = std::min(exp, other.exp);501lgkm = std::min(lgkm, other.lgkm);502vs = std::min(vs, other.vs);503return changed;504}505506bool507wait_imm::empty() const508{509return vm == unset_counter && exp == unset_counter && lgkm == unset_counter &&510vs == unset_counter;511}512513bool514should_form_clause(const Instruction* a, const Instruction* b)515{516/* Vertex attribute loads from the same binding likely load from similar addresses */517unsigned a_vtx_binding =518a->isMUBUF() ? a->mubuf().vtx_binding : (a->isMTBUF() ? a->mtbuf().vtx_binding : 0);519unsigned b_vtx_binding =520b->isMUBUF() ? b->mubuf().vtx_binding : (b->isMTBUF() ? b->mtbuf().vtx_binding : 0);521if (a_vtx_binding && a_vtx_binding == b_vtx_binding)522return true;523524if (a->format != b->format)525return false;526527/* Assume loads which don't use descriptors might load from similar addresses. */528if (a->isFlatLike())529return true;530if (a->isSMEM() && a->operands[0].bytes() == 8 && b->operands[0].bytes() == 8)531return true;532533/* If they load from the same descriptor, assume they might load from similar534* addresses.535*/536if (a->isVMEM() || a->isSMEM())537return a->operands[0].tempId() == b->operands[0].tempId();538539return false;540}541542} // namespace aco543544545