Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,14 @@ if(BUILD_TESTING)
set_tests_properties(pixel_ancillary PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(scalar_halt_ops_tests ${LIBS_INCLUDE_DIR}/tests/ScalarHaltOps.cpp)
target_link_libraries(scalar_halt_ops_tests PRIVATE shader_recompiler_agc_driver libc)
configure_windows_unwind(scalar_halt_ops_tests)
add_test(NAME scalar_halt_ops COMMAND scalar_halt_ops_tests)
if(WIN32)
set_tests_properties(scalar_halt_ops PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_indexed_draw_tests ${LIBS_INCLUDE_DIR}/tests/GuestIndexedDraw.cpp)
target_include_directories(guest_indexed_draw_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_indexed_draw_tests PRIVATE libSceAgc libSceAgcDriver libc)
Expand Down
121 changes: 121 additions & 0 deletions core/libs/tests/ScalarHaltOps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include "Translation/TranslationContext.hpp"
#include "Translation/InstructionTranslator.hpp"
#include "RdnaDecoder/RdnaScalarOpDecoder.hpp"
#include "RdnaDecoder/RdnaInstructionDecoder.hpp"
#include "ControlFlow/GraphBuilder.hpp"
#include <array>
#include <stdexcept>
#include <string>
#include <vector>

using namespace ShaderRecompiler;
static void Require(bool value) { if (!value) throw std::runtime_error("scalar halt ops regression"); }

static void CheckRfeDecode() {
const std::array<std::uint32_t, 1> code{0xBEFD220Au};
const RdnaInstruction instruction = DecodeRdnaSop1(0u, code, 0u);
Require(instruction.op == RdnaOpcode::SRfeB64);
Require(instruction.family == RdnaInstructionFamily::SOP1);
Require(instruction.destination.kind == RdnaOperandKind::Null);
Require(instruction.source0.kind == RdnaOperandKind::ScalarRegister);
Require(instruction.source0.reg == 10u);
Require(instruction.sourceCount == 1u);
Require(instruction.dataDwordCount == 1u);
}

static void CheckSethaltDecode() {
const std::array<std::uint32_t, 1> code{0xBF8D0001u};
const RdnaInstruction instruction = DecodeRdnaSopp(0u, code, 0u);
Require(instruction.op == RdnaOpcode::SSethalt);
Require(instruction.family == RdnaInstructionFamily::SOPP);
Require(instruction.source0.kind == RdnaOperandKind::LiteralConstant);
Require(instruction.source0.value == 1u);
Require(instruction.sourceCount == 0u);
}

static void CheckSendmsghaltDecode() {
const std::array<std::uint32_t, 1> code{0xBF910002u};
const RdnaInstruction instruction = DecodeRdnaSopp(4u, code, 0u);
Require(instruction.op == RdnaOpcode::SSendmsghalt);
Require(instruction.source0.value == 2u);
}

static void CheckCodeEndDecode() {
const std::array<std::uint32_t, 1> code{0xBF9F0000u};
const RdnaInstruction instruction = DecodeRdnaSopp(8u, code, 0u);
Require(instruction.op == RdnaOpcode::SCodeEnd);
}

static void TranslateOne(std::uint32_t word) {
const std::array<std::uint32_t, 1> code{word};
const RdnaInstruction instruction = DecodeRdnaInstruction(0u, code, 0u);
IrProgram program;
auto& block = program.CreateBlock();
program.SetEntryBlock(block);
TranslationContext context(program, block, 256);
context.TranslateInstruction(instruction);
}

static bool ThrowsWith(std::uint32_t word, const char* name) {
try {
TranslateOne(word);
} catch (const std::runtime_error& err) {
return std::string(err.what()).find(name) != std::string::npos;
}
return false;
}

static void CheckTranslationBehavior() {
Require(ThrowsWith(0xBF8D0001u, "s_sethalt at pc 0 with immediate 1 is not implemented"));
Require(ThrowsWith(0xBF910002u, "s_sendmsghalt at pc 0 is not implemented"));
Require(ThrowsWith(0xBF9F0000u, "s_code_end at pc 0 is not implemented"));
TranslateOne(0xBEFD220Au);
TranslateOne(0xBF8D0000u);
}

static void CheckProgramTranslationThrows(const std::vector<std::uint32_t>& code, const char* name) {
const auto decoded = RdnaInstructionDecoder{}.Decode(code);
const auto cfg = GraphBuilder{}.Build(decoded);
ShaderComputeInputInfo computeInfo{};
TranslateOptions options{};
options.stage = ShaderStageKind::Compute;
options.inputInfo.compute = &computeInfo;
bool threw = false;
try {
static_cast<void>(InstructionTranslator{}.Translate(decoded, cfg, options));
} catch (const std::runtime_error& err) {
threw = std::string(err.what()).find(name) != std::string::npos;
}
Require(threw);
}

static void CheckRfeControlFlow() {
const std::vector<std::uint32_t> resolved{0xBE801F00u, 0x80009000u, 0x82018001u, 0xBE802200u, 0xBF810000u, 0x7E1402FFu, 0x00001234u, 0xDC708000u, 0x00060A02u};
const auto decoded = RdnaInstructionDecoder{}.Decode(resolved);
static_cast<void>(GraphBuilder{}.Build(decoded));

const std::vector<std::uint32_t> shortForm{0xBE801F00u, 0x80008C00u, 0xBE802200u, 0xBF810000u, 0x7E1402FFu, 0x00001234u, 0xDC708000u, 0x00060A02u};
const auto decodedShort = RdnaInstructionDecoder{}.Decode(shortForm);
static_cast<void>(GraphBuilder{}.Build(decodedShort));

const std::vector<std::uint32_t> dynamic{0xBE8003FFu, 0x00001000u, 0xBE810380u, 0xBE802200u, 0xBF810000u};
const auto decodedDynamic = RdnaInstructionDecoder{}.Decode(dynamic);
bool threw = false;
try {
static_cast<void>(GraphBuilder{}.Build(decodedDynamic));
} catch (const std::invalid_argument& err) {
threw = std::string(err.what()).find("unsupported dynamic s_rfe_b64") != std::string::npos;
}
Require(threw);
}

int main() {
CheckRfeDecode();
CheckSethaltDecode();
CheckSendmsghaltDecode();
CheckCodeEndDecode();
CheckTranslationBehavior();
CheckProgramTranslationThrows({0xBF8D0001u, 0xBF810000u}, "s_sethalt at pc 0 with immediate 1 is not implemented");
CheckRfeControlFlow();
return 0;
}
24 changes: 16 additions & 8 deletions core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ bool isImmediate(const RdnaOperand& operand, std::uint32_t& value) {

constexpr std::uint32_t NoScalarRegister = UINT32_MAX;

bool isSetpcLike(RdnaOpcode opcode) {
return opcode == RdnaOpcode::SSetpcB64 || opcode == RdnaOpcode::SRfeB64;
}

const char* setpcLikeName(RdnaOpcode opcode) {
return opcode == RdnaOpcode::SRfeB64 ? "s_rfe_b64" : "s_setpc_b64";
}

std::uint32_t scalarIndex(const RdnaOperand& operand) {
switch (operand.kind) {
case RdnaOperandKind::ScalarRegister: return operand.reg;
Expand Down Expand Up @@ -146,7 +154,7 @@ bool resolveLongSetpcTarget(const RdnaProgram& program, std::uint32_t setpcIndex
const bool adds = low.op == RdnaOpcode::SAddU32 && high.op == RdnaOpcode::SAddcU32;
const bool subtracts = low.op == RdnaOpcode::SSubU32 && high.op == RdnaOpcode::SSubbU32;
std::uint32_t lowImmediate = 0, highImmediate = 0;
if (setpc.op != RdnaOpcode::SSetpcB64 || pcRegister == NoScalarRegister || pcRegister % 2u != 0u || pc.op != RdnaOpcode::SGetpcB64 || !isScalar(pc.destination, pcRegister)) return false;
if (!isSetpcLike(setpc.op) || pcRegister == NoScalarRegister || pcRegister % 2u != 0u || pc.op != RdnaOpcode::SGetpcB64 || !isScalar(pc.destination, pcRegister)) return false;
if (adds ? !addsImmediateTo(low, pcRegister, lowImmediate) || !addsImmediateTo(high, pcRegister + 1u, highImmediate)
: !subtracts || !subtractsImmediateFrom(low, pcRegister, lowImmediate) || !subtractsImmediateFrom(high, pcRegister + 1u, highImmediate)) return false;
const auto offset = (static_cast<std::uint64_t>(highImmediate) << 32u) | lowImmediate;
Expand All @@ -166,7 +174,7 @@ bool resolveSetpcTarget(const RdnaProgram& program, std::uint32_t setpcIndex, st
}

const auto& setpc = program.instructions[setpcIndex];
if (setpc.op != RdnaOpcode::SSetpcB64 || setpc.source0.kind != RdnaOperandKind::ScalarRegister) {
if (!isSetpcLike(setpc.op) || setpc.source0.kind != RdnaOperandKind::ScalarRegister) {
return false;
}

Expand Down Expand Up @@ -322,7 +330,7 @@ bool writesScalar(const RdnaInstruction& instruction, std::uint32_t index) {
bool findLastWriter(const RdnaProgram& program, std::uint32_t end, std::initializer_list<std::uint32_t> indices, std::uint32_t& writer) {
for (auto position = end; position-- > 0u;) {
const auto& instruction = program.instructions[position];
if (IsDirectBranchOpcode(instruction.op) || instruction.op == RdnaOpcode::SSetpcB64 || instruction.op == RdnaOpcode::SEndpgm) return false;
if (IsDirectBranchOpcode(instruction.op) || isSetpcLike(instruction.op) || instruction.op == RdnaOpcode::SEndpgm) return false;
if (std::ranges::any_of(indices, [&](std::uint32_t index) { return writesScalar(instruction, index); })) {
writer = position;
return true;
Expand Down Expand Up @@ -501,11 +509,11 @@ std::vector<BasicBlock> GraphBuilder::splitIntoBlocks(const RdnaProgram& program
if (nextProgramCounter <= endProgramCounter) {
labels.insert(nextProgramCounter);
}
} else if (instruction.op == RdnaOpcode::SSetpcB64) {
} else if (isSetpcLike(instruction.op)) {
std::uint32_t target = 0;
if (!resolveSetpcTarget(program, index, target)) {
BoundedJumpTable table;
if (!resolveBoundedJumpTable(program, index, table)) throw std::invalid_argument("unsupported dynamic s_setpc_b64 at program counter " + toHexString(instruction.programCounter));
if (!resolveBoundedJumpTable(program, index, table)) throw std::invalid_argument(std::string("unsupported dynamic ") + setpcLikeName(instruction.op) + " at program counter " + toHexString(instruction.programCounter));
for (const auto tableTarget : table.targets) {
if (!instructionProgramCounters.contains(tableTarget)) throw std::invalid_argument("jump table targets invalid instruction boundary " + toHexString(tableTarget));
labels.insert(tableTarget);
Expand All @@ -514,7 +522,7 @@ std::vector<BasicBlock> GraphBuilder::splitIntoBlocks(const RdnaProgram& program
continue;
}
if (!isValidTarget(target, instructionProgramCounters, firstProgramCounter, endProgramCounter)) {
throw std::invalid_argument("s_setpc_b64 at program counter " + toHexString(instruction.programCounter) + " targets invalid program counter " + toHexString(target));
throw std::invalid_argument(std::string(setpcLikeName(instruction.op)) + " at program counter " + toHexString(instruction.programCounter) + " targets invalid program counter " + toHexString(target));
}
labels.insert(target);
if (nextProgramCounter <= endProgramCounter) {
Expand Down Expand Up @@ -568,11 +576,11 @@ void GraphBuilder::linkBlocks(std::vector<BasicBlock>& blocks, const RdnaProgram

if (last.op == RdnaOpcode::SEndpgm) {
block.terminator.kind = TerminatorKind::Return;
} else if (last.op == RdnaOpcode::SSetpcB64) {
} else if (isSetpcLike(last.op)) {
std::uint32_t target = 0;
if (!resolveSetpcTarget(program, block.instructionEnd - 1u, target)) {
BoundedJumpTable table;
if (!resolveBoundedJumpTable(program, block.instructionEnd - 1u, table)) throw std::invalid_argument("unsupported dynamic s_setpc_b64 at program counter " + toHexString(last.programCounter));
if (!resolveBoundedJumpTable(program, block.instructionEnd - 1u, table)) throw std::invalid_argument(std::string("unsupported dynamic ") + setpcLikeName(last.op) + " at program counter " + toHexString(last.programCounter));
block.terminator.kind = TerminatorKind::IndirectBranch;
block.terminator.indirectPcSgpr = last.source0.reg;
sortUnique(table.targets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,10 @@ enum class RdnaOpcode : std::uint16_t {
Exp,
VMulHiI32I24,
VMulHiU32U24,
SRfeB64,
SSethalt,
SSendmsghalt,
SCodeEnd,
Count
};

Expand Down
11 changes: 11 additions & 0 deletions core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RdnaOpcode decodeSop1Opcode(std::uint32_t opcode) {
case 0x1eu: return RdnaOpcode::SBitset1B64;
case 0x1fu: return RdnaOpcode::SGetpcB64;
case 0x20u: return RdnaOpcode::SSetpcB64;
case 0x22u: return RdnaOpcode::SRfeB64;
case 0x24u: return RdnaOpcode::SAndSaveexecB64;
case 0x25u: return RdnaOpcode::SOrSaveexecB64;
case 0x26u: return RdnaOpcode::SXorSaveexecB64;
Expand Down Expand Up @@ -204,9 +205,11 @@ RdnaOpcode decodeSoppOpcode(std::uint32_t opcode) {
case 0x0au: return RdnaOpcode::SBarrier;
case 0x0bu: return RdnaOpcode::SEndpgm;
case 0x0cu: return RdnaOpcode::SWaitcnt;
case 0x0du: return RdnaOpcode::SSethalt;
case 0x0eu: return RdnaOpcode::SSleep;
case 0x0fu: return RdnaOpcode::SSetprio;
case 0x10u: return RdnaOpcode::SSendmsg;
case 0x11u: return RdnaOpcode::SSendmsghalt;
case 0x12u: return RdnaOpcode::STrap;
case 0x13u: return RdnaOpcode::SIcacheInv;
case 0x14u: return RdnaOpcode::SIncperflevel;
Expand All @@ -218,6 +221,7 @@ RdnaOpcode decodeSoppOpcode(std::uint32_t opcode) {
case 0x1au: return RdnaOpcode::SCbranchCdbg;
case 0x1bu:
case 0x1eu: return RdnaOpcode::SEndpgm;
case 0x1fu: return RdnaOpcode::SCodeEnd;
case 0x20u: return RdnaOpcode::SInstPrefetch;
case 0x21u: return RdnaOpcode::SClause;
case 0x22u: return RdnaOpcode::SWaitIdle;
Expand Down Expand Up @@ -347,6 +351,13 @@ RdnaInstruction DecodeRdnaSop1(std::uint32_t programCounter, std::span<const std
ReadRdnaLiteralOperands(code, wordIndex, instruction);
return instruction;
}
if (instruction.op == RdnaOpcode::SRfeB64) {
instruction.sourceCount = 1;
instruction.destination.kind = RdnaOperandKind::Null;
instruction.source0 = DecodeRdnaScalarSource(scalarSource0, programCounter);
ReadRdnaLiteralOperands(code, wordIndex, instruction);
return instruction;
}

instruction.source0 = DecodeRdnaScalarSource(scalarSource0, programCounter);
instruction.destination = DecodeRdnaScalarDestination(scalarDestination, programCounter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ EmbeddedFetchPlan EmbeddedVertexFetchAnalyzer::Analyze(const RdnaProgram& progra
std::array<bool, kVectorSlotCount> vgprIsIndex{};
VectorLaneMap vectorLanes;
const bool trackVectorLanes = std::none_of(program.instructions.begin(), program.instructions.end(), [](const RdnaInstruction& inst) {
return IsDirectBranchOpcode(inst.op) || inst.op == RdnaOpcode::SSetpcB64;
return IsDirectBranchOpcode(inst.op) || inst.op == RdnaOpcode::SSetpcB64 || inst.op == RdnaOpcode::SRfeB64;
});

sgprs[attribSlot].kind = SgprValueKind::AttributeTable;
Expand Down
11 changes: 11 additions & 0 deletions core/shader/recompiler/Translation/src/ScalarInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
return true;
case RdnaOpcode::SSetpcB64:
return true;
case RdnaOpcode::SRfeB64:
return true;
case RdnaOpcode::SSethalt: {
const std::uint32_t value = inst.source0.value;
if (value == 0u || value == 2u || value == 4u) return true;
throw std::runtime_error("s_sethalt at pc " + std::to_string(inst.programCounter) + " with immediate " + std::to_string(value) + " is not implemented");
}
case RdnaOpcode::SSendmsghalt:
throw std::runtime_error("s_sendmsghalt at pc " + std::to_string(inst.programCounter) + " is not implemented");
case RdnaOpcode::SCodeEnd:
throw std::runtime_error("s_code_end at pc " + std::to_string(inst.programCounter) + " is not implemented");
case RdnaOpcode::SSubvectorLoopBegin:
sSubvectorLoop(inst, true);
return true;
Expand Down
Loading