diff --git a/CMakeLists.txt b/CMakeLists.txt index 278354d49..a66e798db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,7 @@ if(WIN32) src/gfx1013_compute_ir.cpp src/gfx1013_sampled_pixel.cpp src/gfx1013_vertex_id.cpp src/gfx1013_rect_geometry.cpp src/shader_dxil_compiler.cpp) set(native_shader_revision "") foreach(source gfx1013_compute_native.cpp gfx1013_compute_native.h gfx1013_compute_opcodes.inl gfx1013_graphics_wqm.inl - gfx1013_isa_tables.inl gfx1013_isa.cpp gfx1013_isa.h gfx1013_compute_alu.inl gfx1013_direct_compute.inl gfx1013_compute_ir.cpp gfx1013_compute_ir.h + gfx1013_isa_tables.inl gfx1013_isa.cpp gfx1013_isa.h gfx1013_registers.h gfx1013_compute_alu.inl gfx1013_direct_compute.inl gfx1013_compute_ir.cpp gfx1013_compute_ir.h gfx1013_compute_mtbuf.inl gfx1013_direct_graphics.inl gfx1013_direct_mesh.inl gfx1013_pixel_waterfall.inl gfx1013_rect_geometry.cpp guest_shader_container.cpp guest_shader_container.h dxil_module.h mesh_pixel_heap.h direct_compute_cache.cpp direct_compute_cache.h shader_compute_request.cpp shader_compute_request.h shader_translation_error.h startup_stubs/inflate.h) diff --git a/src/gfx1013_compute_native.cpp b/src/gfx1013_compute_native.cpp index cc4aaa51e..0bc71ea27 100644 --- a/src/gfx1013_compute_native.cpp +++ b/src/gfx1013_compute_native.cpp @@ -3,6 +3,7 @@ #include "dxil_module.h" #include "guest_shader_container.h" #include "gfx1013_isa.h" +#include "gfx1013_registers.h" #include #include #include @@ -181,12 +182,12 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.op = (a >> 8) & 255; i.dst = (a >> 16) & 127; i.src[0] = a & 255; - lit = i.src[0] == 255; + lit = i.src[0] == gfx1013::operand::literal; } else if (form == 0xbf000000) { i.enc = Encoding::Sopc; i.op = (a >> 16) & 127; i.src = {a & 255, (a >> 8) & 255, 0}; - lit = i.src[0] == 255 || i.src[1] == 255; + lit = i.src[0] == gfx1013::operand::literal || i.src[1] == gfx1013::operand::literal; } else if ((a >> 28) == 11) { i.enc = Encoding::Sopk; i.op = (a >> 23) & 31; @@ -198,23 +199,23 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.op = (a >> 23) & 127; i.dst = (a >> 16) & 127; i.src = {a & 255, (a >> 8) & 255, 0}; - lit = i.src[0] == 255 || i.src[1] == 255; + lit = i.src[0] == gfx1013::operand::literal || i.src[1] == gfx1013::operand::literal; } else if ((a >> 31) == 0) { auto k = a >> 25; - i.dst = 256 + ((a >> 17) & 255); - i.src = {a & 511, 256 + ((a >> 9) & 255), 106}; + i.dst = gfx1013::operand::vgpr_base + ((a >> 17) & 255); + i.src = {a & 511, gfx1013::operand::vgpr_base + ((a >> 9) & 255), gfx1013::reg::vcc_lo}; if (k == 63) { i.enc = Encoding::Vop1; i.op = (a >> 9) & 255; } else if (k == 62) { i.enc = Encoding::Vopc; i.op = (a >> 17) & 255; - i.dst = 106; + i.dst = gfx1013::reg::vcc_lo; } else { i.enc = Encoding::Vop2; i.op = k; } - lit = i.src[0] == 255 || + lit = i.src[0] == gfx1013::operand::literal || (i.enc == Encoding::Vop2 && (k == 0x20 || k == 0x21 || k == 0x2c || k == 0x2d || k == 0x37 || k == 0x38)); } else if ((a & 0xfc000000u) == 0xf4000000) { i.enc = Encoding::Smem; @@ -225,7 +226,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa const unsigned soffset = i.b >> 25; const unsigned limit = smem_sgpr_limit(direct && !graphics); if ((i.b & 0x01f00000u) || (a & 0x14000) || (i.op & ~8u) > 4 || - (soffset != 125 && ((!direct && i.op < 8 && i.op != 2) || soffset >= limit))) + (soffset != gfx1013::reg::null && ((!direct && i.op < 8 && i.op != 2) || soffset >= limit))) refuse(pc, "unsupported scalar memory addressing"); if (direct && !graphics && i.dst + memory_count(i) > limit) refuse(pc, "unsupported scalar memory destination range"); @@ -256,7 +257,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa if ((a & 0x00038000u) || (i.b & 0x00e00000u)) refuse(pc, "unsupported buffer atomic addressing or cache policy"); if ((a & 0x2000u) && (graphics || (a & 0x1fffu) || - ((i.b >> 24) != 128 && (i.b >> 24) != 125))) + ((i.b >> 24) != gfx1013::operand::zero && (i.b >> 24) != gfx1013::reg::null))) refuse(pc, "indexed compute atomic requires zero byte offsets"); i.name = encoded.name; } else if ((i.op >= 4 && i.op <= 7) || (i.op >= 0x84 && i.op <= 0x87)) { @@ -277,11 +278,11 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "raw buffer load needs a linear byte offset and no modifiers"); // The MUBUF encoding has no trailing literal word. const unsigned soffset = i.b >> 24; - if (soffset > 105 && !(soffset >= 128 && soffset <= 208)) + if (soffset > gfx1013::reg::last_general_sgpr && !(soffset >= gfx1013::operand::zero && soffset <= gfx1013::operand::int_neg_last)) refuse(pc, "raw buffer load scalar offset needs an SGPR or inline integer"); } else if (direct && graphics && !(a & 0x2000u)) { const unsigned soffset = i.b >> 24; - if ((a & 0x02032000u) || (i.b & 0x00a00000u) || (soffset > 105 && soffset != 128)) + if ((a & 0x02032000u) || (i.b & 0x00a00000u) || (soffset > gfx1013::reg::last_general_sgpr && soffset != gfx1013::operand::zero)) refuse(pc, "raw graphics load requires linear addressing without LDS or TFE"); } else if (!(direct && ((a & 0x0203f000u) == 0x2000u || (a & 0x0203f000u) == 0x3000u) && (i.b & 0xffe00000u) == 0x80000000u) && @@ -310,7 +311,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa if (!half_formats[count] || format != half_formats[count]) refuse(pc, "MTBUF D16 store requires an exact-width FLOAT format"); // Keep IDXEN (bit 13). Opcode bit 3 sits in the second word's bit 21. - if ((a & 0xdfffu) || (i.b & 0xc00000u) || (i.b >> 24) != 128) + if ((a & 0xdfffu) || (i.b & 0xc00000u) || (i.b >> 24) != gfx1013::operand::zero) refuse(pc, "MTBUF D16 store requires index or the first element and zero byte offsets"); // The chosen typed UAV implements the explicit FLOAT conversion. // Use the same packed data and element addressing as the MUBUF store. @@ -337,9 +338,9 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "direct flat or scratch memory is not a global root access"); if (a & 0x2000u) refuse(pc, "direct global memory does not lower an LDS result"); - if (saddr == 125) + if (saddr == gfx1013::reg::null) refuse(pc, "direct global memory with a VGPR address pair is not a root buffer offset"); - if (saddr == 127) + if (saddr == gfx1013::reg::exec_hi) refuse(pc, "direct global memory scalar address is not an SGPR pair"); if (!(dword_load || dword_store || subdword_load)) refuse(pc, "direct global memory supports only DWORD loads and stores with a scalar address pair"); @@ -360,7 +361,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.neg = i.b >> 29; i.scale = (i.b >> 27) & 3; i.clamp = (a & 0x8000) != 0; - i.dst = 256 + (a & 255); + i.dst = gfx1013::operand::vgpr_base + (a & 255); if (i.enc == Encoding::Vop3 && i.op < 256) { i.enc = Encoding::Vopc; i.dst = a & 255; @@ -374,10 +375,10 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa if (direct && !graphics && i.enc == Encoding::Vop3 && (i.op == 0x377 || i.op == 0x378)) { // The shared extent decoder already counts the trailing literal word. // S1 and S2 may both reference that word. - if (i.src[0] == 255) + if (i.src[0] == gfx1013::operand::literal) refuse(pc, "permlane S0 cannot be a literal"); } - lit = std::find(i.src.begin(), i.src.end(), 255) != i.src.end(); + lit = std::find(i.src.begin(), i.src.end(), gfx1013::operand::literal) != i.src.end(); } else refuse(pc, "unsupported instruction " + string(encoded.name) + " " + hexword(a)); if (!i.wide && (i.enc == Encoding::Vop1 || i.enc == Encoding::Vop2 || i.enc == Encoding::Vopc)) { @@ -389,7 +390,8 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa const bool sdwa_logic = i.enc == Encoding::Vop2 && i.op == 0x1d; const bool sdwa_class = i.enc == Encoding::Vopc && (i.op == 0x88 || (direct && i.op == 0x8f)); const bool sdwa_compare = i.enc == Encoding::Vopc && !sdwa_class; - const unsigned sdwa_mask_limit = direct && !graphics && !wave64 ? 107 : 106; + // Highest SDWA mask destination: VCC_HI only in direct wave32 compute, which keeps both VCC words numeric. + const unsigned sdwa_mask_limit = direct && !graphics && !wave64 ? gfx1013::reg::vcc_hi : gfx1013::reg::vcc_lo; if (direct && i.src[0] == 249 && (sdwa_compare || i.enc == Encoding::Vop1 || i.enc == Encoding::Vop2)) { // Operand profiles from disasm_rdna2's GFX10 VOP1/VOP2 SDWA // decoder. Admit only profiles with a direct result placement. @@ -428,7 +430,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa if (sdwa_compare) { if ((ext & 0x8000) && (name.substr(0,7) == "v_cmpx_" || ((ext >> 8) & 127) > sdwa_mask_limit)) refuse(pc, "invalid SDWA compare destination"); - i.dst = ext & 0x8000 ? (ext >> 8) & 127 : 106; + i.dst = ext & 0x8000 ? (ext >> 8) & 127 : gfx1013::reg::vcc_lo; } else { i.dst_select = (ext >> 8) & 7; i.dst_unused = (ext >> 11) & 3; @@ -472,9 +474,9 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa i.abs |= ((control >> 5) & 1) << j; i.neg |= ((control >> 4) & 1) << j; const unsigned reg = j ? (a >> 9) & 255 : ext & 255; - if ((control & 128) && reg == 255) + if ((control & 128) && reg == gfx1013::operand::literal) refuse(pc, "literal is unavailable in SDWA"); - i.src[j] = reg + ((control & 128) ? 0 : 256); + i.src[j] = reg + ((control & 128) ? 0 : gfx1013::operand::vgpr_base); } i.subword = true; lit = false; @@ -489,7 +491,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "invalid or unsupported integer SDWA modifiers"); } else { // VOPC has a scalar mask destination in the same bits that VOP2 uses for placement. - i.dst = ext & 0x8000 ? (ext >> 8) & 127 : 106; + i.dst = ext & 0x8000 ? (ext >> 8) & 127 : gfx1013::reg::vcc_lo; if ((ext & 0x70480000u) || i.dst > sdwa_mask_limit) refuse(pc, "invalid SDWA class modifiers or mask destination"); i.neg = (ext >> 20) & 1; @@ -497,9 +499,9 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa } if (i.src_select[0] == 7 || i.src_select[1] == 7) refuse(pc, "invalid or unsupported integer SDWA modifiers"); - i.src[0] = (ext & 255) + (ext & (1u << 23) ? 0 : 256); - i.src[1] = ((a >> 9) & 255) + (ext & (1u << 31) ? 0 : 256); - if (i.src[0] == 255 || i.src[1] == 255) + i.src[0] = (ext & 255) + (ext & (1u << 23) ? 0 : gfx1013::operand::vgpr_base); + i.src[1] = ((a >> 9) & 255) + (ext & (1u << 31) ? 0 : gfx1013::operand::vgpr_base); + if (i.src[0] == gfx1013::operand::literal || i.src[1] == gfx1013::operand::literal) refuse(pc, "literal is unavailable in SDWA"); i.subword = true; lit = false; @@ -509,7 +511,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "SDWA source or destination modifier"); i.subword = true; i.src_select[0] = (ext >> 16) & 7u; - i.src[0] = 256 + (ext & 255); + i.src[0] = gfx1013::operand::vgpr_base + (ext & 255); lit = false; } else if (direct && i.src[0]==250 && (graphics || i.enc!=Encoding::Vopc) && !lit) { auto control=take(); @@ -525,14 +527,14 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc,"unsupported DPP lane selection"); i.dpp_fetch_inactive = (control & (1u << 18)) != 0; i.dpp_bound_zero = (control & (1u << 19)) != 0; - i.src[0]=256+(control&255); + i.src[0]=gfx1013::operand::vgpr_base+(control&255); i.neg=((control>>20)&1)|((control>>21)&2); i.abs=((control>>21)&1)|((control>>22)&2); } else if (direct && !graphics && dpp8 && i.enc != Encoding::Vopc && !lit) { const Word control = take(); i.dpp8_selectors = control >> 8; i.dpp_fetch_inactive = i.src[0] == 234; - i.src[0] = 256 + (control & 255); + i.src[0] = gfx1013::operand::vgpr_base + (control & 255); } else if (graphics && dpp8 && i.enc != Encoding::Vopc && !lit) { // pixel_aot.parse_dpp: both halves of an octet must select the // same local 2x2 permutation to map to desktop quad operations. @@ -546,7 +548,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa } i.dpp_control = perm; i.dpp_fetch_inactive = i.src[0] == 234; - i.src[0] = 256 + (control & 255); + i.src[0] = gfx1013::operand::vgpr_base + (control & 255); } else if (i.src[0] == 249 || i.src[0] == 250 || i.src[0] == 233 || i.src[0] == 234) refuse(pc, "SDWA or DPP vector operands"); } @@ -561,16 +563,16 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa refuse(pc, "unsupported opcode " + hexword(a)); i.name = name; if (i.name.substr(0, 7) == "v_cmpx_") - i.dst = 126; + i.dst = gfx1013::reg::exec_lo; if (i.name == "v_readfirstlane_b32" || i.name == "v_readlane_b32") { - i.dst -= 256; - if (i.dst > (graphics ? 105u : 107u)) + i.dst -= gfx1013::operand::vgpr_base; + if (i.dst > (graphics ? gfx1013::reg::last_general_sgpr : gfx1013::reg::vcc_hi)) refuse(pc, "invalid scalar lane read destination"); } } // Keep permuted VCC compares separate from unpermuted compare/EXEC proofs. if (i.enc == Encoding::Vopc && i.dpp_control) { - if (i.dst != 106 || i.name.substr(0,6) != "v_cmp_" || i.name.substr(i.name.size()-4) != "_f32" || + if (i.dst != gfx1013::reg::vcc_lo || i.name.substr(0,6) != "v_cmp_" || i.name.substr(i.name.size()-4) != "_f32" || i.name.find("_class_") != Name::npos || *i.dpp_control > 255) refuse(pc,"unsupported DPP pixel compare"); i.enc = Encoding::QuadCompare; @@ -582,7 +584,7 @@ Inst decode(const string &bytes, unsigned pc,bool direct=false, bool graphics=fa static std::optional carry_destination(const Inst &i) { if (i.name == "v_add_co_u32" || i.name == "v_sub_co_u32" || i.name == "v_subrev_co_u32" || i.name == "v_add_co_ci_u32" || i.name == "v_sub_co_ci_u32" || i.name == "v_subrev_co_ci_u32") - return i.wide ? (i.a >> 8) & 127 : 106; + return i.wide ? (i.a >> 8) & 127 : gfx1013::reg::vcc_lo; if (i.name == "v_mad_u64_u32") // VOP3B: the carry-out of the 64-bit add (#3000) return (i.a >> 8) & 127; return {}; @@ -600,7 +602,7 @@ struct Symbol { index_mask == r.index_mask && definition == r.definition && projected_numeric == r.projected_numeric; } }; struct State { - std::array scalar{}; + std::array scalar{}; // Encoded VGPR and guest lane. A join can lose the identity while // retaining the fact that some path omitted the runtime lane write. std::map, std::optional> lane_spills; @@ -627,10 +629,10 @@ struct State { static std::optional constant32(const Inst &i, unsigned r) { static constexpr Word floats[]{0x3f000000, 0xbf000000, 0x3f800000, 0xbf800000, 0x40000000, 0xc0000000, 0x40800000, 0xc0800000, 0x3e22f983}; - if (r >= 128 && r <= 192) return r - 128; - if (r >= 193 && r <= 208) return Word(192) - r; - if (r >= 240 && r <= 248) return floats[r - 240]; - if (r == 255) return i.literal; + if (r >= gfx1013::operand::zero && r <= gfx1013::operand::int_pos_last) return r - gfx1013::operand::zero; + if (r >= gfx1013::operand::int_neg_first && r <= gfx1013::operand::int_neg_last) return Word(192) - r; + if (r >= gfx1013::operand::float_first && r <= gfx1013::operand::float_last) return floats[r - gfx1013::operand::float_first]; + if (r == gfx1013::operand::literal) return i.literal; return {}; } static std::optional guest_spill_lane(const Inst &i, bool wave64) { @@ -649,7 +651,7 @@ static void clear_vector_spills(const Inst &i, State &s) { if (s.lane_spills.empty()) return; switch (i.enc) { case Encoding::Vop1: case Encoding::Vop2: case Encoding::Vop3: case Encoding::Mix: - if (i.dst >= 256 && i.name != "v_nop") { + if (i.dst >= gfx1013::operand::vgpr_base && i.name != "v_nop") { const bool pair = i.name == "v_cvt_f64_i32" || i.name == "v_cvt_f64_u32" || i.name == "v_cvt_f64_f32" || i.name == "v_frexp_mant_f64" || i.name == "v_mad_u64_u32" || i.name == "v_mad_i64_i32" || i.name == "v_fract_f64" || @@ -665,9 +667,9 @@ static void clear_vector_spills(const Inst &i, State &s) { case Encoding::Mubuf: if (i.name == "buffer_load_dword") { constexpr unsigned widths[]{1, 2, 4, 3}; - s.clear_spills(256 + i.dst, widths[i.op & 3]); + s.clear_spills(gfx1013::operand::vgpr_base + i.dst, widths[i.op & 3]); } else if (i.name == "buffer_load_format" || i.name.substr(0, 8) == "tbuffer_") - s.clear_spills(256 + i.dst, (i.op & 3) + 1); + s.clear_spills(gfx1013::operand::vgpr_base + i.dst, (i.op & 3) + 1); else if (i.name == "global_load_dword" || i.name == "global_load_ubyte" || i.name == "global_load_sbyte" || i.name == "global_load_ushort" || i.name == "global_load_sshort" || @@ -675,7 +677,7 @@ static void clear_vector_spills(const Inst &i, State &s) { i.name == "global_load_sbyte_d16" || i.name == "global_load_sbyte_d16_hi" || i.name == "global_load_short_d16" || i.name == "global_load_short_d16_hi" || (i.name.substr(0, 14) == "buffer_atomic_" && (i.a & 0x4000))) - s.clear_spills(256 + i.dst); + s.clear_spills(gfx1013::operand::vgpr_base + i.dst); break; case Encoding::Image: if (i.op != 8) { @@ -684,7 +686,7 @@ static void clear_vector_spills(const Inst &i, State &s) { count += mask & 1; if (i.op == 68) count = 4; if (i.b >> 31) count = (count + 1) / 2; - s.clear_spills(256 + i.dst, count); + s.clear_spills(gfx1013::operand::vgpr_base + i.dst, count); } break; default: break; @@ -703,13 +705,13 @@ void bind_root_address(State &state, const ShaderRootInput &input, unsigned limi state[at + half] = {Symbol::Type::root_address, input.root_parameter, half, resource_class}; } Symbol source(const Inst &i, unsigned code, const State &s) { - if (code < 128) + if (code < gfx1013::operand::zero) return s[code]; - if (code <= 192) - return {Symbol::Type::constant, code - 128}; - if (code <= 208) + if (code <= gfx1013::operand::int_pos_last) + return {Symbol::Type::constant, code - gfx1013::operand::zero}; + if (code <= gfx1013::operand::int_neg_last) return {Symbol::Type::constant, 192u - code}; - if (code == 255) + if (code == gfx1013::operand::literal) return {Symbol::Type::constant, i.literal}; return {}; } @@ -717,20 +719,20 @@ Symbol source(const Inst &i, unsigned code, const State &s) { // lane-selected slot: s_add_u32 lo, , , then the carry into the high word. Returns the // operand index of the offset. A constant offset is not matched: the selected slot must come from GPU data. static std::optional heap_offset_add(const Inst &i, const State &s) { - if (i.name != "s_add_u32" || i.dst > 105) return {}; + if (i.name != "s_add_u32" || i.dst > gfx1013::reg::last_general_sgpr) return {}; for (unsigned k = 0; k < 2; ++k) { const auto heap = source(i, i.src[k], s), offset = source(i, i.src[1 - k], s); if (heap.type == Symbol::Type::table && heap.value == compute_resource_heap && !heap.offset && - i.src[1 - k] <= 105 && offset.numeric() && offset.type != Symbol::Type::constant) + i.src[1 - k] <= gfx1013::reg::last_general_sgpr && offset.numeric() && offset.type != Symbol::Type::constant) return 1 - k; } return {}; } // The high word of that address: s_add_u32 hi, 0, src_scc or s_addc_u32 hi, 0, 0. static bool heap_offset_carry(const Inst &i) { - return i.dst <= 105 && ((i.name == "s_add_u32" && ((i.src[0] == 128 && i.src[1] == 253) || - (i.src[0] == 253 && i.src[1] == 128))) || - (i.name == "s_addc_u32" && i.src[0] == 128 && i.src[1] == 128)); + return i.dst <= gfx1013::reg::last_general_sgpr && ((i.name == "s_add_u32" && ((i.src[0] == gfx1013::operand::zero && i.src[1] == gfx1013::operand::scc) || + (i.src[0] == gfx1013::operand::scc && i.src[1] == gfx1013::operand::zero))) || + (i.name == "s_addc_u32" && i.src[0] == gfx1013::operand::zero && i.src[1] == gfx1013::operand::zero)); } // s[base:base+1] holds the resource-heap base plus a byte offset with its own carry in the high word. static bool heap_offset_address(const State &s, unsigned base) { @@ -745,14 +747,14 @@ static std::optional byte_descriptor_field(const Inst &i, const State &s bool direct_compute = false) { using T = Symbol::Type; // s_bitset1_b32 , 16 sets the same bit as s_or_b32 , 0x10000 below. Unlike s_or, it leaves SCC alone. - if (i.name == "s_bitset1_b32" && i.dst <= 105) { + if (i.name == "s_bitset1_b32" && i.dst <= gfx1013::reg::last_general_sgpr) { auto high = s[i.dst]; const auto bit = source(i, i.src[0], s); if (high.type != T::root_address || high.offset != 1 || bit.type != T::constant || bit.value != 16) return {}; high.type = T::root_stride_one_high; return high; } - if (i.enc != Encoding::Sop2 || i.dst > 105) return {}; + if (i.enc != Encoding::Sop2 || i.dst > gfx1013::reg::last_general_sgpr) return {}; auto a = source(i, i.src[0], s), b = source(i, i.src[1], s); auto component = [heap_window](const Symbol &v, unsigned ch) { return v.offset % 32 == ch*4 && ((v.type == T::descriptor && v.value != compute_resource_heap) || @@ -800,7 +802,7 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win if (n >= s.size()) refuse(i.pc, "invalid SGPR"); s[n] = v; - if (n == 127) + if (n == gfx1013::reg::exec_hi) s.exec_hi_zero = v.type == Symbol::Type::constant && v.value == 0; }; const Symbol carry = s.scc; @@ -812,7 +814,7 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win if (auto lane = guest_spill_lane(i, wave64)) { const auto key = std::make_pair(i.dst, *lane); const auto value = source(i, i.src[0], s); - if (i.src[0] <= 107 && identity_spill(value)) + if (i.src[0] <= gfx1013::reg::vcc_hi && identity_spill(value)) s.lane_spills[key] = value; else s.lane_spills.erase(key); @@ -828,17 +830,17 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win clear_vector_spills(i, s); } auto mask = carry_destination(i); - const bool compared = graphics && i.enc == Encoding::Vopc && i.dst < 106 && i.name.substr(0,6) == "v_cmp_"; + const bool compared = graphics && i.enc == Encoding::Vopc && i.dst < gfx1013::reg::vcc_lo && i.name.substr(0,6) == "v_cmp_"; if (mask || compared) { const unsigned dst = mask.value_or(i.dst); - const bool numeric_vcc_hi = dst == 107 && direct && !graphics && !wave64; - if ((dst > 106 && !numeric_vcc_hi) || (wave64 && (dst & 1))) + const bool numeric_vcc_hi = dst == gfx1013::reg::vcc_hi && direct && !graphics && !wave64; + if ((dst > gfx1013::reg::vcc_lo && !numeric_vcc_hi) || (wave64 && (dst & 1))) refuse(i.pc, compared ? "unsupported comparison mask destination" : "unsupported carry mask destination"); - const bool tagged = graphics && dst != 106; + const bool tagged = graphics && dst != gfx1013::reg::vcc_lo; set(dst, tagged ? Symbol{Symbol::Type::lane_mask,i.pc,0} : Symbol{}); if (wave64) set(dst + 1, tagged ? Symbol{Symbol::Type::lane_mask,i.pc,1} : Symbol{}); } else if (graphics && i.name == "s_cselect_b64" && i.dst <= 104 && !(i.dst & 1) && - (i.src[0] == 193 || i.src[0] == 128) && (i.src[1] == 193 || i.src[1] == 128)) { + (i.src[0] == gfx1013::operand::int_neg_first || i.src[0] == gfx1013::operand::zero) && (i.src[1] == gfx1013::operand::int_neg_first || i.src[1] == gfx1013::operand::zero)) { set(i.dst,{Symbol::Type::uniform_mask,i.pc,0}); set(i.dst+1,{Symbol::Type::uniform_mask,i.pc,1}); } else if (heap_add) { @@ -859,19 +861,19 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win const unsigned offset_reg = i.b >> 25; const Symbol index = offset_reg < smem_sgpr_limit(direct && !graphics) ? s[offset_reg] : Symbol{}; unsigned n = memory_count(i); - const bool heap_address = i.op == 2 && i.b >> 25 == 125 && heap_offset_address(s, i.src[0]); + const bool heap_address = i.op == 2 && i.b >> 25 == gfx1013::reg::null && heap_offset_address(s, i.src[0]); for (unsigned j = 0; j < n; ++j) { Symbol v{}; if (heap_address) { v = {Symbol::Type::indexed_descriptor, compute_resource_heap, (i.b & 0xfffff) + 4 * j, base.index_mask, i.pc}; } else if (graphics && i.op == 3 && base.type == Symbol::Type::table && base.value == compute_resource_heap) { - const bool fixed = offset_reg == 125 || index.type == Symbol::Type::constant; - const Word offset = (i.b & 0xfffff) + (offset_reg != 125 && fixed ? index.value : 0); + const bool fixed = offset_reg == gfx1013::reg::null || index.type == Symbol::Type::constant; + const Word offset = (i.b & 0xfffff) + (offset_reg != gfx1013::reg::null && fixed ? index.value : 0); v = {Symbol::Type::indexed_descriptor, base.value, offset + 4*j, fixed ? 0u : index.type == Symbol::Type::masked ? index.value : ~0u, i.pc}; } else if (i.op == 2 && base.type == Symbol::Type::sampler_heap) { v = {Symbol::Type::sampler_word, i.dst, j, 0, i.pc}; - } else if ((i.op == 2 || (graphics && i.op == 3)) && offset_reg != 125 && base.type == Symbol::Type::table) { + } else if ((i.op == 2 || (graphics && i.op == 3)) && offset_reg != gfx1013::reg::null && base.type == Symbol::Type::table) { v = {Symbol::Type::indexed_descriptor, base.value, (i.b & 0xfffff) + 4*j, index.type == Symbol::Type::masked || index.type == Symbol::Type::constant ? index.value : ~0u, i.pc}; } else if (i.op < 8 && base.type == Symbol::Type::table) @@ -881,7 +883,7 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win } else if (i.name == "s_mov_b32" || i.name == "s_mov_b64") { auto v = source(i, i.src[0], s); Symbol high = i.src[0] < 127 ? s[i.src[0] + 1] - : Symbol{Symbol::Type::constant, (i.src[0] >= 193 && i.src[0] <= 208) ? ~0u : 0u}; + : Symbol{Symbol::Type::constant, (i.src[0] >= gfx1013::operand::int_neg_first && i.src[0] <= gfx1013::operand::int_neg_last) ? ~0u : 0u}; set(i.dst, v); if (i.name == "s_mov_b64") set(i.dst + 1, high); @@ -890,15 +892,15 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win i.name == "s_andn1_wrexec_b32" || i.name == "s_andn2_wrexec_b32")) { // Both the saved/written destination and the updated EXEC are numeric mask words. set(i.dst, {}); - set(126, {}); + set(gfx1013::reg::exec_lo, {}); } else if (!graphics && i.enc == Encoding::Sop1 && i.name.size() >= 13 && (i.name.substr(i.name.size() - 13) == "_saveexec_b64" || i.name.substr(i.name.size() - 11) == "_wrexec_b64")) { // These instructions also overwrite both EXEC words implicitly. set(i.dst, {}); set(i.dst + 1, {}); - set(126, {}); - set(127, {}); + set(gfx1013::reg::exec_lo, {}); + set(gfx1013::reg::exec_hi, {}); } else if (i.name == "s_and_b32" || i.name == "s_lshl_b32") { const auto a = source(i, i.src[0], s), b = source(i, i.src[1], s); Symbol out{}; @@ -950,14 +952,14 @@ State transfer(const Inst &i, State s, bool wave64, bool graphics, bool heap_win { set(i.dst, {}); const bool compare_pair = (wave64 && (i.enc == Encoding::Vopc || i.enc == Encoding::QuadCompare)) || - (narrow_wave64 && i.enc == Encoding::Vopc && i.dst == 126); + (narrow_wave64 && i.enc == Encoding::Vopc && i.dst == gfx1013::reg::exec_lo); if (compare_pair || ((i.enc == Encoding::Sop1 || i.enc == Encoding::Sop2) && i.name.size() >= 4 && (i.name.substr(i.name.size() - 4) == "_b64" || i.name.substr(i.name.size() - 4) == "_i64" || i.name.substr(i.name.size() - 4) == "_u64") && i.name.find("_i32_") == Name::npos) || i.name == "s_bitreplicate_b64_b32") set(i.dst + 1, {}); - if (narrow_wave64 && i.enc == Encoding::Vopc && i.dst == 126) + if (narrow_wave64 && i.enc == Encoding::Vopc && i.dst == gfx1013::reg::exec_lo) s.exec_hi_zero = true; // write_ballot() clears the absent upper half. } return s; @@ -980,7 +982,7 @@ std::map fixed_vertex_loads(const string &code) { const auto i = decode(code, pc, true, true); if (!(i.name == "v_nop" || i.name == "s_nop" || i.name == "s_setprio" || i.name == "s_inst_prefetch" || i.name == "s_clause" || - ((i.name == "s_mov_b32" || i.enc == Encoding::Smem) && i.dst >= 3 && i.dst < 106))) + ((i.name == "s_mov_b32" || i.enc == Encoding::Smem) && i.dst >= 3 && i.dst < gfx1013::reg::vcc_lo))) return {}; pc = i.next; } @@ -1006,13 +1008,13 @@ std::map fixed_vertex_loads(const string &code) { setup(8, dst, 4); break; } - if (base == ptr || (b >> 25) != 125 || (dst < ptr + 2 && ptr < dst + 4) || dst < 3) + if (base == ptr || (b >> 25) != gfx1013::reg::null || (dst < ptr + 2 && ptr < dst + 4) || dst < 3) return {}; pc += 8; } if (desc == UINT_MAX || pc + 8 > code.size() || word(code,pc) != 0x4a000002u || word(code,pc+4) != 0xbf8cc07fu) return {}; - setup(4, 256, 1); // v0 is the adjusted fetch index, consumed by the IA. + setup(4, gfx1013::operand::vgpr_base, 1); // v0 is the adjusted fetch index, consumed by the IA. pc += 4; if (pc + 4 <= code.size() && (word(code,pc) & 0xffff0000u) == 0xbfa10000u) pc += 4; bool found = false, overwrote_index = false; @@ -1034,13 +1036,13 @@ std::map fixed_vertex_loads(const string &code) { if (overwrote_index || (b & 0xffff00ffu) != (0x80000000u | ((desc / 4) << 16))) return {}; Inst i{}; i.pc = pc; i.next = pc + 8; i.enc = Encoding::FixedFetch; i.name = "fixed_vertex_fetch"; - i.dst = 256 + ((b >> 8) & 255); + i.dst = gfx1013::operand::vgpr_base + ((b >> 8) & 255); i.op = width; i.a = normalized; i.literal = a & 4095u; if (i.literal % alignment || i.literal >= 2048 || i.dst+i.op > 512) return {}; result.emplace(pc, i); - found = true; overwrote_index = i.dst == 256; pc += 8; + found = true; overwrote_index = i.dst == gfx1013::operand::vgpr_base; pc += 8; } return found ? result : std::map{}; } @@ -1144,11 +1146,11 @@ Program parse(const string &code, unsigned entry, const State &initial, bool gra } return p; } -string reg(unsigned n) { return n >= 256 ? "v" + to_string(n - 256) : "s" + to_string(n); } +string reg(unsigned n) { return n >= gfx1013::operand::vgpr_base ? "v" + to_string(n - gfx1013::operand::vgpr_base) : "s" + to_string(n); } string operand(const Inst &i, unsigned n) { - if ((n <= 105) || n == 106 || n == 126 || n >= 256) + if ((n <= gfx1013::reg::last_general_sgpr) || n == gfx1013::reg::vcc_lo || n == gfx1013::reg::exec_lo || n >= gfx1013::operand::vgpr_base) return reg(n); - if (n == 253) + if (n == gfx1013::operand::scc) return "uint(scc)"; if (auto constant = constant32(i, n)) return hexword(*constant); @@ -1361,7 +1363,7 @@ struct Emit { refuse(i.pc, "unsupported image opcode"); auto tex = binding(i, state, base, 8, half_store ? ComputeViewKind::half_scalar_uav : store ? ComputeViewKind::texture_uav : ComputeViewKind::texture_srv); - string x = reg(256 + addr), y = reg(256 + (((i.a >> 1) & 3) ? (i.image_address_words[0] & 255) : addr + 1)); + string x = reg(gfx1013::operand::vgpr_base + addr), y = reg(gfx1013::operand::vgpr_base + (((i.a >> 1) & 3) ? (i.image_address_words[0] & 255) : addr + 1)); if (i.b & 0x40000000u) { if (lod || gather || (store && !half_store) || (half_store && ((i.a >> 1) & 3))) refuse(i.pc, "packed sample address"); @@ -1371,14 +1373,14 @@ struct Emit { if (store) { if (half_store) { body << "if(active) " << tex << "[uint2(" << x << "," << y << ")]=f16tof32(" - << reg(256+i.dst) << "&65535u);\n"; + << reg(gfx1013::operand::vgpr_base+i.dst) << "&65535u);\n"; return; } if (mask != 15 || i.dst > 252) refuse(i.pc, "unsupported image store mask"); body << "if(active) " << tex << "[uint2(" << x << "," << y << ")]=asfloat(uint4("; for (unsigned k = 0; k < 4; ++k) - body << (k ? "," : "") << reg(256 + i.dst + k); + body << (k ? "," : "") << reg(gfx1013::operand::vgpr_base + i.dst + k); body << "));\n"; return; } @@ -1397,36 +1399,36 @@ struct Emit { if (gather || ((mask >> k) & 1)) { if (dst >= 256) refuse(i.pc, "image destination overflow"); - body << "if(active) " << reg(256 + dst++) << "=asuint(t." << "xyzw"[k] << ");\n"; + body << "if(active) " << reg(gfx1013::operand::vgpr_base + dst++) << "=asuint(t." << "xyzw"[k] << ");\n"; } } - void require_numeric(const Inst &i, unsigned reg, unsigned limit = 106) const { - if ((reg < limit || reg == 124) && !program.before[program.at.at(i.pc)][reg].numeric()) - refuse(i.pc, reg == 124 ? "unbound numeric M0" : "unbound numeric SGPR s" + to_string(reg)); + void require_numeric(const Inst &i, unsigned reg, unsigned limit = gfx1013::reg::vcc_lo) const { + if ((reg < limit || reg == gfx1013::reg::m0) && !program.before[program.at.at(i.pc)][reg].numeric()) + refuse(i.pc, reg == gfx1013::reg::m0 ? "unbound numeric M0" : "unbound numeric SGPR s" + to_string(reg)); } void scalar(const Inst &i) { auto n = i.name; string a = operand(i, i.src[0]), b = operand(i, i.src[1]), d = reg(i.dst), e; if (n == "s_mov_b32" || n == "s_mov_b64") { if (n == "s_mov_b64") { - if (i.dst == 126) + if (i.dst == gfx1013::reg::exec_lo) require_numeric(i, i.src[0]); - if ((i.dst & 1) || i.dst + 1 >= 128 || (i.src[0] < 128 && (i.src[0] & 1))) + if ((i.dst & 1) || i.dst + 1 >= gfx1013::reg::sgpr_count || (i.src[0] < gfx1013::reg::sgpr_count && (i.src[0] & 1))) refuse(i.pc, "scalar pair overflow"); body << reg(i.dst + 1) << "=" << (i.src[0] < 105 ? reg(i.src[0] + 1) - : i.src[0] >= 193 && i.src[0] <= 208 ? "0xffffffffu" + : i.src[0] >= gfx1013::operand::int_neg_first && i.src[0] <= gfx1013::operand::int_neg_last ? "0xffffffffu" : "0u") << ";\n"; } - if (i.dst == 106 || i.dst == 126) + if (i.dst == gfx1013::reg::vcc_lo || i.dst == gfx1013::reg::exec_lo) require_numeric(i, i.src[0]); body << d << "=" << a << ";\n"; - if (i.dst == 126) + if (i.dst == gfx1013::reg::exec_lo) body << "active=(s126&laneBit)!=0;\n"; return; } - if (n == "s_bfe_u32" && i.src[0] <= 105) { + if (n == "s_bfe_u32" && i.src[0] <= gfx1013::reg::last_general_sgpr) { const auto &state = program.before[program.at.at(i.pc)]; const auto word = state[i.src[0]]; const auto bits = source(i, i.src[1], state); @@ -1541,7 +1543,7 @@ struct Emit { } void vector(const Inst &i) { auto n = i.name; - if ((n == "v_readfirstlane_b32" || n == "v_readlane_b32") && i.dst >= 106) + if ((n == "v_readfirstlane_b32" || n == "v_readlane_b32") && i.dst >= gfx1013::reg::vcc_lo) refuse(i.pc, "invalid scalar lane read destination"); if (n == "v_readlane_b32") refuse(i.pc, "unsupported HLSL instruction v_readlane_b32"); @@ -1587,7 +1589,7 @@ struct Emit { return; } if (n == "v_swap_b32") { - if (i.wide || i.src[0] < 256 || i.src[0] > 511) + if (i.wide || i.src[0] < gfx1013::operand::vgpr_base || i.src[0] > 511) refuse(i.pc, "swap requires two VGPRs in a VOP1 encoding"); const auto src = reg(i.src[0]); body << "if(active){uint old=" << d << ";" << d << "=" << src << ";" << src << "=old;}\n"; @@ -1627,7 +1629,7 @@ struct Emit { pred = "(" + a + op + b + ")"; } body << d << "=WaveActiveBallot((uint(active)&uint(" << pred << "))!=0u).x;\n"; - if (i.dst == 126) + if (i.dst == gfx1013::reg::exec_lo) body << "active=(s126&laneBit)!=0;\n"; return; } @@ -1640,11 +1642,11 @@ struct Emit { } if (n == "v_lshrrev_b64") { auto src = i.src[1]; - if (src == 128 && i.dst < 511) { + if (src == gfx1013::operand::zero && i.dst < 511) { body << "if(active){" << d << "=0;" << reg(i.dst + 1) << "=0;}\n"; return; } - if (src < 256 || src >= 511 || i.dst >= 511) + if (src < gfx1013::operand::vgpr_base || src >= 511 || i.dst >= 511) refuse(i.pc, "invalid shift pair"); body << "if(active){uint64_t w=(uint64_t(" << reg(src + 1) << ")<<32)|" << reg(src) << "; w>>=(" << val(0, false) << "&63u);" << d << "=uint(w);" << reg(i.dst + 1) << "=uint(w>>32);}\n"; @@ -1721,10 +1723,10 @@ struct Emit { const auto kind = raw ? ComputeViewKind::raw_buffer_uav : half ? ComputeViewKind::half_buffer_uav : ComputeViewKind::typed_buffer_uav; auto view = binding(i, s, ((i.b >> 16) & 31) * 4, 4, kind, raw ? 4 : count * (half ? 2 : 4)); - auto address = reg(256 + (i.b & 255)); + auto address = reg(gfx1013::operand::vgpr_base + (i.b & 255)); if (raw) { const unsigned scalar = i.b >> 24; - if (scalar > 105 && (scalar < 128 || scalar > 192)) + if (scalar > gfx1013::reg::last_general_sgpr && (scalar < gfx1013::operand::zero || scalar > gfx1013::operand::int_pos_last)) refuse(i.pc, "unsupported raw store scalar offset"); require_numeric(i, scalar); address = "((" + ((i.a & 0x1000) ? address : "0u") + "+" + operand(i, scalar) + @@ -1734,7 +1736,7 @@ struct Emit { string data; for (unsigned j = 0; j < count; ++j) { if (j) data += ","; - auto src = reg(256 + i.dst + (half ? j / 2 : j)); + auto src = reg(gfx1013::operand::vgpr_base + i.dst + (half ? j / 2 : j)); data += half ? "f16tof32((" + src + ">>" + to_string((j%2)*16) + ")&65535u)" : src; } if (count > 1) data = (half ? "float" : "uint") + suffix + "(" + data + ")"; @@ -1750,12 +1752,12 @@ struct Emit { refuse(i.pc, "raw load destination overflow"); auto r = binding(i, s, ((i.b >> 16) & 31) * 4, 4, ComputeViewKind::raw_vector_srv, 4); // Address generation precedes every writeback, including when vaddr is a destination. - body << "if(active){uint address=(" << reg(256 + (i.b & 255)) << "+" << (i.a & 4095) + body << "if(active){uint address=(" << reg(gfx1013::operand::vgpr_base + (i.b & 255)) << "+" << (i.a & 4095) << "u)&~3u;"; const string suffix = count == 1 ? "" : to_string(count); body << "uint" << suffix << " loaded=" << r << ".Load" << suffix << "(address);"; for (unsigned k = 0; k < count; ++k) { - body << reg(256 + i.dst + k) << "=loaded"; + body << reg(gfx1013::operand::vgpr_base + i.dst + k) << "=loaded"; if (count > 1) body << "." << "xyzw"[k]; body << ";"; } @@ -1766,9 +1768,9 @@ struct Emit { binding(i, s, ((i.b >> 16) & 31) * 4, 4, ComputeViewKind::typed_buffer_srv, 4 * (1 + i.op)); if (i.dst + i.op >= 256) refuse(i.pc, "formatted load destination overflow"); - body << "if(active){uint4 loaded=" << buffer << ".Load(" << reg(256 + (i.b & 255)) << ");"; + body << "if(active){uint4 loaded=" << buffer << ".Load(" << reg(gfx1013::operand::vgpr_base + (i.b & 255)) << ");"; for (unsigned j = 0; j <= i.op; ++j) - body << reg(256 + i.dst + j) << "=loaded[" << j << "];"; + body << reg(gfx1013::operand::vgpr_base + i.dst + j) << "=loaded[" << j << "];"; body << "}\n"; return; } @@ -1782,9 +1784,9 @@ struct Emit { refuse(i.pc, "unknown scalar table base"); if (s.at(i.src[0]).value == compute_sampler_heap) refuse(i.pc, "sampler heap reads need the direct DXIL path"); - if (s.at(i.src[0]).value == compute_resource_heap && i.b >> 25 == 125) + if (s.at(i.src[0]).value == compute_resource_heap && i.b >> 25 == gfx1013::reg::null) refuse(i.pc, "unsupported immediate whole-heap descriptor"); - if (i.b >> 25 != 125) { + if (i.b >> 25 != gfx1013::reg::null) { const auto high = s.at(i.src[0]+1); if (high.type != Symbol::Type::constant || high.value || ((i.b & 0xfffff) % 32)) refuse(i.pc,"unproven indexed table address"); @@ -1803,7 +1805,7 @@ struct Emit { } unsigned off = i.b & 0xfffff, count = memory_count(i); const unsigned offset_reg = i.b >> 25; - const bool dynamic = offset_reg != 125; + const bool dynamic = offset_reg != gfx1013::reg::null; if (dynamic) require_numeric(i, offset_reg); auto r = binding(i, s, i.src[0], 4, ComputeViewKind::buffer_srv, dynamic ? 4 : off + count * 4); @@ -1929,9 +1931,9 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi } unsigned next = 4; State initial{}; - for (unsigned k = 0; k <= 105; ++k) + for (unsigned k = 0; k <= gfx1013::reg::last_general_sgpr; ++k) initial[k].type = Symbol::Type::unbound; - initial[124].type = Symbol::Type::unbound; // RDNA2 M0 is a separate 32-bit register. + initial[gfx1013::reg::m0].type = Symbol::Type::unbound; // RDNA2 M0 is a separate 32-bit register. std::vector constants; if (stage) { std::array assigned{}; @@ -2037,12 +2039,12 @@ static NativeComputeShader translate_program(const ComputeRequest &request, unsi const auto& inst = program.code[i]; if (!program.reached[i]) continue; // The narrow wave64 model still requires a guest-aligned carry destination pair. - if (!guest_wave32 && carry_destination(inst) == 107) + if (!guest_wave32 && carry_destination(inst) == gfx1013::reg::vcc_hi) refuse(inst.pc, "unsupported carry mask destination"); // s107 is a numeric wave32 mask only for a guest wave32. - if (!guest_wave32 && inst.name == "v_cndmask_b32" && inst.src[2] == 107) + if (!guest_wave32 && inst.name == "v_cndmask_b32" && inst.src[2] == gfx1013::reg::vcc_hi) refuse(inst.pc, "unsupported wave32 conditional mask source"); - if (!guest_wave32 && inst.name.find("_ci_") != Name::npos && inst.src[2] == 107) + if (!guest_wave32 && inst.name.find("_ci_") != Name::npos && inst.src[2] == gfx1013::reg::vcc_hi) refuse(inst.pc, "unsupported carry-in mask source"); if (inst.enc != Encoding::Mubuf || inst.name != "buffer_store_raw") continue; const auto& symbol = program.before[i].at(((inst.b >> 16) & 31) * 4); @@ -2154,7 +2156,7 @@ uint fpClass(uint v){ h << "groupshared uint waveStarts[" << threads << "];\n"; h << "[WaveSize(32)] [numthreads(" << x << "," << y << "," << z << ")] void main(uint3 g:SV_GroupID,uint idx:SV_GroupIndex){\n"; - for (unsigned j = 0; j < 128; ++j) + for (unsigned j = 0; j < gfx1013::reg::sgpr_count; ++j) h << "uint s" << j << "=0;\n"; for (unsigned j = 0; j < 256; ++j) h << "uint v" << j << "=0;\n"; @@ -2343,17 +2345,18 @@ static void walk_raw_graphics_root(const D3D12_ROOT_SIGNATURE_DESC1 &layout, const bool table=p.ParameterType==D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; const bool constants=p.ParameterType==D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; const unsigned count=table ? 1 : constants ? p.Constants.Num32BitValues : 2; - if (!count || count>106-sgpr || j>=16) refuse(0,"raw graphics user data exceeds register range"); + // User data ends at the last general SGPR: VCC_LO is the first code past it. + if (!count || count>gfx1013::reg::vcc_lo-sgpr || j>=16) refuse(0,"raw graphics user data exceeds register range"); if (visible && (!mesh || table || constants || p.ParameterType==D3D12_ROOT_PARAMETER_TYPE_SRV)) add_input(table ? 3u : constants ? 0u : 1u, count, sgpr, j); sgpr+=count; } if (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) { - if (sgpr >= 106) refuse(0, "resource heap outside graphics user data"); + if (sgpr >= gfx1013::reg::vcc_lo) refuse(0, "resource heap outside graphics user data"); add_input(5, 1, sgpr++, 0); } if (!mesh && (layout.Flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED)) { - if (sgpr >= 106) refuse(0, "sampler heap outside graphics user data"); + if (sgpr >= gfx1013::reg::vcc_lo) refuse(0, "sampler heap outside graphics user data"); add_input(6, 1, sgpr, 0); } } diff --git a/src/gfx1013_registers.h b/src/gfx1013_registers.h new file mode 100644 index 000000000..d4258d277 --- /dev/null +++ b/src/gfx1013_registers.h @@ -0,0 +1,50 @@ +#pragma once +// Named GFX10 (RDNA2) scalar register codes and ISA operand encodings. +// +// Three different number spaces use the same small integers, so keep them apart: +// * reg: a scalar register code 0..127 (SDST/SBASE/SADDR fields and the index +// into a 128-entry SGPR state array). 106/107 are VCC, 124 is M0, +// 126/127 are EXEC; 0..105 are the general SGPRs. +// * operand: an ISA source operand 0..511 (SSRC/SRC0 fields). 0..127 are the same +// register codes, 128..255 are inline constants and special values +// (128 is the constant 0, not a register), 256..511 are VGPRs. +// * slot: a translator-internal index. Some frontends keep SCC in slot 128 of +// their scalar state, which collides with operand 128 (constant 0); +// reg::scc_slot names that slot so it is never read as an operand. +// The values are plain constexpr unsigned so a substitution keeps the generated +// code byte-identical. + +namespace x2w::gfx1013 { + +namespace reg { +inline constexpr unsigned last_general_sgpr = 105; +inline constexpr unsigned vcc_lo = 106; +inline constexpr unsigned vcc_hi = 107; +inline constexpr unsigned m0 = 124; +inline constexpr unsigned null = 125; +inline constexpr unsigned exec_lo = 126; +inline constexpr unsigned exec_hi = 127; +inline constexpr unsigned sgpr_count = 128; +inline constexpr unsigned scc_slot = 128; + +constexpr bool is_general_sgpr(unsigned code) { return code <= last_general_sgpr; } +} // namespace reg + +namespace operand { +inline constexpr unsigned zero = 128; +inline constexpr unsigned int_pos_last = 192; +inline constexpr unsigned int_neg_first = 193; +inline constexpr unsigned int_neg_last = 208; +inline constexpr unsigned float_first = 240; +inline constexpr unsigned float_last = 248; +inline constexpr unsigned vccz = 251; +inline constexpr unsigned execz = 252; +inline constexpr unsigned scc = 253; +inline constexpr unsigned literal = 255; +inline constexpr unsigned vgpr_base = 256; + +constexpr bool is_inline_int(unsigned code) { return code >= zero && code <= int_neg_last; } +constexpr bool is_vgpr(unsigned code) { return code >= vgpr_base && code < 2 * vgpr_base; } +} // namespace operand + +} // namespace x2w::gfx1013 diff --git a/tests/cmake/gfx1013_registers.cmake b/tests/cmake/gfx1013_registers.cmake new file mode 100644 index 000000000..4d0d5649a --- /dev/null +++ b/tests/cmake/gfx1013_registers.cmake @@ -0,0 +1,16 @@ +# Unit tests for the named GFX10 register codes and operand encodings +# (src/gfx1013_registers.h): static_asserts pin every value, and one case +# per predicate checks its boundaries. Header-only, portable C++17, no device. +add_executable(gfx1013_registers_check tests/gfx1013_registers_check.cpp) +target_include_directories(gfx1013_registers_check PRIVATE src) +target_compile_features(gfx1013_registers_check PRIVATE cxx_std_17) +if(MSVC) + target_compile_options(gfx1013_registers_check PRIVATE /W4 /permissive- /WX) +endif() +foreach(registers_case IN ITEMS general_sgpr inline_int vgpr) + add_test(NAME gfx1013_registers_${registers_case} + COMMAND gfx1013_registers_check ${registers_case}) + set_tests_properties(gfx1013_registers_${registers_case} PROPERTIES TIMEOUT 60) +endforeach() +add_test(NAME gfx1013_registers_all COMMAND gfx1013_registers_check) +set_tests_properties(gfx1013_registers_all PROPERTIES TIMEOUT 60) diff --git a/tests/gfx1013_registers_check.cpp b/tests/gfx1013_registers_check.cpp new file mode 100644 index 000000000..d823e682c --- /dev/null +++ b/tests/gfx1013_registers_check.cpp @@ -0,0 +1,111 @@ +// Unit tests for the named GFX10 register codes and operand encodings in +// src/gfx1013_registers.h. The values are pinned at compile time; the +// cases below check the predicates at their boundaries. Header-only, +// portable C++17, no device. +// +// Usage: gfx1013_registers_check [] +// (no case runs every case; --case is accepted too) + +#include +#include + +#include "gfx1013_registers.h" + +namespace reg = x2w::gfx1013::reg; +namespace operand = x2w::gfx1013::operand; + +// GFX10 ISA values. A change here changes every translator that uses them. +static_assert(reg::last_general_sgpr == 105, "s105 is the last general SGPR"); +static_assert(reg::vcc_lo == 106 && reg::vcc_hi == 107, "VCC is s106:s107"); +static_assert(reg::m0 == 124, "M0 is code 124"); +static_assert(reg::null == 125, "NULL is code 125"); +static_assert(reg::exec_lo == 126 && reg::exec_hi == 127, "EXEC is s126:s127"); +static_assert(reg::sgpr_count == 128, "scalar register codes are 0..127"); +static_assert(reg::scc_slot == reg::sgpr_count, "the SCC slot follows the SGPR codes"); +static_assert(operand::zero == 128 && operand::int_pos_last == 192, "inline 0..64 are 128..192"); +static_assert(operand::int_neg_first == 193 && operand::int_neg_last == 208, "inline -1..-16 are 193..208"); +static_assert(operand::float_first == 240 && operand::float_last == 248, "inline floats are 240..248"); +static_assert(operand::vccz == 251 && operand::execz == 252 && operand::scc == 253, "VCCZ, EXECZ, SCC"); +static_assert(operand::literal == 255, "a trailing literal is operand 255"); +static_assert(operand::vgpr_base == 256, "VGPR operands start at 256"); +// The register codes are also operand codes; operand 128 is a constant, not a register. +static_assert(reg::exec_hi < operand::zero, "register codes precede the inline constants"); + +namespace { + +int g_failures = 0; + +void expect(bool ok, const char* what) +{ + if (!ok) { + std::fprintf(stderr, "FAIL: gfx1013_registers: %s\n", what); + ++g_failures; + } +} + +void general_sgpr() +{ + expect(reg::is_general_sgpr(0), "s0 is general"); + expect(reg::is_general_sgpr(105), "s105 is general"); + expect(!reg::is_general_sgpr(reg::vcc_lo), "VCC_LO is not general"); + expect(!reg::is_general_sgpr(reg::m0), "M0 is not general"); + expect(!reg::is_general_sgpr(reg::exec_hi), "EXEC_HI is not general"); +} + +void inline_int() +{ + expect(!operand::is_inline_int(reg::exec_hi), "EXEC_HI is not an inline integer"); + expect(operand::is_inline_int(operand::zero), "128 is inline 0"); + expect(operand::is_inline_int(operand::int_pos_last), "192 is inline 64"); + expect(operand::is_inline_int(operand::int_neg_first), "193 is inline -1"); + expect(operand::is_inline_int(operand::int_neg_last), "208 is inline -16"); + expect(!operand::is_inline_int(209), "209 is reserved"); + expect(!operand::is_inline_int(operand::float_first), "240 is a float constant"); +} + +void vgpr() +{ + expect(!operand::is_vgpr(operand::literal), "255 is the literal"); + expect(operand::is_vgpr(operand::vgpr_base), "256 is v0"); + expect(operand::is_vgpr(511), "511 is v255"); + expect(!operand::is_vgpr(512), "512 is past the VGPR operands"); +} + +struct Case { + const char* name; + void (*run)(); +}; + +const Case kCases[] = { + {"general_sgpr", general_sgpr}, + {"inline_int", inline_int}, + {"vgpr", vgpr}, +}; + +} // namespace + +int main(int argc, char** argv) +{ + const char* wanted = nullptr; + if (argc == 2) { + wanted = argv[1]; + } else if (argc == 3 && std::strcmp(argv[1], "--case") == 0) { + wanted = argv[2]; + } else if (argc != 1) { + std::fprintf(stderr, "FAIL: usage: gfx1013_registers_check [--case] \n"); + return 1; + } + int ran = 0; + for (const Case& c : kCases) { + if (wanted != nullptr && std::strcmp(c.name, wanted) != 0) continue; + const int before = g_failures; + c.run(); + ++ran; + if (g_failures == before) std::printf("PASS: gfx1013_registers %s\n", c.name); + } + if (wanted != nullptr && ran == 0) { + std::fprintf(stderr, "FAIL: unknown case '%s'\n", wanted); + return 1; + } + return g_failures == 0 ? 0 : 1; +}