Skip to content
Merged
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
80 changes: 66 additions & 14 deletions core/libs/tests/ScalarCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "RdnaDecoder/RdnaMemoryOpDecoder.hpp"
#include "ControlFlow/GraphBuilder.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <initializer_list>
Expand Down Expand Up @@ -44,6 +45,11 @@ static RdnaInstruction Sop1(std::uint32_t pc, std::uint32_t destination, std::ui
return DecodeRdnaSop1(pc, code, 0u);
}

static RdnaInstruction Sop2(std::uint32_t pc, std::uint32_t destination, std::uint32_t op, std::uint32_t source0, std::uint32_t source1) {
const std::array<std::uint32_t, 1> code{0x80000000u | (op << 23u) | (destination << 16u) | (source1 << 8u) | source0};
return DecodeRdnaSop2(pc, code, 0u);
}

static RdnaInstruction Sopp(std::uint32_t pc, std::uint32_t op, std::uint32_t immediate = 0u) {
const std::array<std::uint32_t, 1> code{0xbf800000u | (op << 16u) | immediate};
return DecodeRdnaSopp(pc, code, 0u);
Expand Down Expand Up @@ -271,27 +277,69 @@ static void CheckTableCalls() {
const std::vector<std::vector<TableCallee>> callees{{{0x1122334455ull, first}, {0x66778899aaull, second}}};
const auto linkedCalls = LinkCalls(program, {}, {}, calls, callees);
const auto& linked = linkedCalls.code;
Require(linkedCalls.fallbacks == std::vector<std::uint32_t>{50u * 4u});
Require(linkedCalls.fallbacks == std::vector<std::uint32_t>{53u * 4u});
ExpectThrow("is shared with another call", [&] { static_cast<void>(BuildCode(linked, swappc)); });
SwappcInfo linkedSwappc = swappc;
linkedSwappc.linkedCallFallbacks = linkedCalls.fallbacks;
Require(linked.size() == 59u && linked[9] == 0xbf820001u && linked[11] == 0xbf850010u && linked[58] == 0xbf9f0000u);
Require(linked[12] == 0xbf07ff0eu && linked[13] == 0x22334455u && linked[14] == 0xbf850004u && linked[15] == 0xbf07ff0fu && linked[16] == 0x11u && linked[17] == 0xbf850001u && linked[18] == 0xbf82001bu);
Require(linked[20] == 0x778899aau && linked[26] == 0xbf078080u && linked[27] == 0xbf820016u && linked[34] == 0xbf068080u && linked[35] == 0xbf82000au && linked[44] == 0xbf068080u && linked[45] == 0xbf820004u);
Require(linked[46] == 0xbb0e0005u && linked[47] == 0xbf82ffdau && linked[48] == 0xbb0e0006u && linked[49] == 0xbf82ffd8u && linked[50] == 0xbe8e210eu && linked[51] == 0xbf82ffd6u);
Require(std::equal(first.begin(), first.end(), linked.begin() + 52) && std::equal(second.begin(), second.end(), linked.begin() + 55));
Require(linked.size() == 56u && linked[9] == 0xbf820001u && linked[11] == 0xbf850007u && linked[55] == 0xbf9f0000u);
Require(linked[12] == 0xbf07ff0eu && linked[13] == 0x22334455u && linked[14] == 0xbf850010u && linked[15] == 0xbf07ff0fu && linked[16] == 0x11u && linked[17] == 0xbf85000du && linked[18] == 0xbf820007u);
Require(linked[19] == 0xbf07ff0eu && linked[21] == 0xbf850010u && linked[24] == 0xbf85000du && linked[25] == 0xbf068080u && linked[26] == 0xbb0e0001u && linked[27] == 0xbf82ffeeu);
Require(linked[32] == 0x778899aau && linked[33] == 0xbf850010u && linked[37] == 0xbf820007u && linked[40] == 0xbf85000bu && linked[43] == 0xbf850008u && linked[45] == 0xbb0e0001u && linked[46] == 0xbf82ffecu);
Require(linked[50] == 0xbf078080u && linked[51] == 0xbf820001u && linked[52] == 0xbf068080u && linked[53] == 0xbe8e210eu && linked[54] == 0xbf82fff7u);
Require(std::equal(first.begin(), first.end(), linked.begin() + 28) && std::equal(second.begin(), second.end(), linked.begin() + 47));
const auto cfg = BuildCode(linked, linkedSwappc);
const auto block = [&](std::uint32_t word) { return cfg.FindBlockByProgramCounter(word * 4u).id; };
Require(cfg.FindBlockByProgramCounter(46u * 4u).terminator.trueBlock == block(52u));
Require(cfg.FindBlockByProgramCounter(52u * 4u).terminator.trueBlock == block(47u));
Require(cfg.FindBlockByProgramCounter(48u * 4u).terminator.trueBlock == block(55u));
Require(cfg.FindBlockByProgramCounter(55u * 4u).terminator.trueBlock == block(49u));
Require(cfg.unlinkedCallProgramCounters == std::vector<std::uint32_t>{50u * 4u});
Require(cfg.FindBlockByProgramCounter(26u * 4u).terminator.trueBlock == block(28u));
Require(cfg.FindBlockByProgramCounter(28u * 4u).terminator.trueBlock == block(27u));
Require(cfg.FindBlockByProgramCounter(45u * 4u).terminator.trueBlock == block(47u));
Require(cfg.FindBlockByProgramCounter(47u * 4u).terminator.trueBlock == block(46u));
Require(cfg.unlinkedCallProgramCounters == std::vector<std::uint32_t>{53u * 4u});
static_cast<void>(TranslateCode(linked, linkedSwappc));

std::vector<std::uint32_t> large(0x8000u, 0xbf800000u);
const std::vector<std::vector<TableCallee>> far{{{0x10ull, large}, {0x20ull, first}}};
ExpectThrow("out of s_call_b64 range", [&] { static_cast<void>(LinkCalls(program, {}, {}, calls, far)); });
std::vector<std::uint32_t> large(0x3000u, 0xbf800000u);
large.push_back(0xbe80200eu);
const std::vector<std::vector<TableCallee>> far{{{0x10ull, large}, {0x20ull, large}, {0x30ull, large}, {0x40ull, first}}};
const auto farLinked = LinkCalls(program, {}, {}, calls, far);
const auto farCall = 12u + 3u * (16u + 0x3001u) + 14u;
Require(farLinked.code[farCall] == 0xbb0e0001u && std::equal(first.begin(), first.end(), farLinked.code.begin() + farCall + 2u));
SwappcInfo farSwappc = swappc;
farSwappc.linkedCallFallbacks = farLinked.fallbacks;
const auto farCfg = BuildCode(farLinked.code, farSwappc);
Require(farCfg.FindBlockByProgramCounter(farCall * 4u).terminator.trueBlock == farCfg.FindBlockByProgramCounter((farCall + 2u) * 4u).id);
Require(farCfg.FindBlockByProgramCounter((farCall + 2u) * 4u).terminator.trueBlock == farCfg.FindBlockByProgramCounter((farCall + 1u) * 4u).id);
auto twoCallers = caller;
twoCallers.pop_back();
twoCallers.insert(twoCallers.end(), caller.begin(), caller.end());
const auto twoProgram = RdnaInstructionDecoder{}.Decode(twoCallers);
auto secondCall = calls[0];
secondCall.programCounter += 40u;
const std::array<TableCall, 2> twoCalls{calls[0], secondCall};
const std::vector<std::vector<TableCallee>> twoCallees{far[0], {{0x50ull, first}, {0x60ull, second}}};
const auto twoLinked = LinkCalls(twoProgram, {}, {}, twoCalls, twoCallees);
const auto& two = twoLinked.code;
const auto follow = [&](std::size_t word, std::size_t& hops) {
hops = 0;
while (two[word] >> 16u == 0xbf82u) {
word = static_cast<std::size_t>(static_cast<std::int64_t>(word) + 1 + static_cast<std::int16_t>(two[word] & 0xffffu));
++hops;
}
return word;
};
std::size_t hops = 0;
Require(follow(9u, hops) == 23u && hops == 1u && two[21] >> 16u == 0xbf82u && two[22] >> 16u == 0xbf82u);
Require(follow(39u, hops) == 10u && hops == 1u);
const auto secondChain = follow(19u, hops);
Require(hops == 6u && two[secondChain] == 0xbf850007u && two[secondChain + 1u] == 0xbf07ff0eu && two[secondChain + 2u] == 0x50u);
Require(two[secondChain + 15u] == 0xbb0e0001u && std::equal(first.begin(), first.end(), two.begin() + static_cast<std::ptrdiff_t>(secondChain + 17u)));
Require(follow(secondChain + 16u, hops) == 20u && hops == 6u);
SwappcInfo twoSwappc = swappc;
twoSwappc.linkedCallFallbacks = twoLinked.fallbacks;
const auto twoCfg = BuildCode(two, twoSwappc);
Require(twoCfg.FindBlockByProgramCounter(static_cast<std::uint32_t>(secondChain + 15u) * 4u).terminator.trueBlock == twoCfg.FindBlockByProgramCounter(static_cast<std::uint32_t>(secondChain + 17u) * 4u).id);
Require(twoCfg.unlinkedCallProgramCounters.size() == 2u);
std::vector<std::uint32_t> oversized(0x8000u, 0xbf800000u);
const std::vector<std::vector<TableCallee>> overflows{{{0x10ull, oversized}, {0x20ull, first}}};
ExpectThrow("out of the 16-bit branch range", [&] { static_cast<void>(LinkCalls(program, {}, {}, calls, overflows)); });

const std::vector<std::uint32_t> jumpTable{
0xbe8c1f00u, 0x800cff0cu, 0x00000058u, 0x820d800du, 0x83928202u, 0xd4c40016u, 0x02000500u, 0x8f128212u, 0xf4000386u, 0x24000000u,
Expand All @@ -306,6 +354,10 @@ static void CheckTableCalls() {
ExpectThrow("escapes the constant-offset call/return model", [] {
Build({Sop1(0u, 0u, 3u, 8u), Call(4u, 8u, 2u), Sopp(8u, 2u, 0xfffdu), Sopp(12u, 1u), Sop1(16u, 0u, 0x20u, 8u)});
});
static_cast<void>(Build({Call(0u, 8u, 3u), Sop2(4u, 8u, 0x15u, 0u, 2u), Sop2(8u, 10u, 0x1bu, 8u, 2u), Sopp(12u, 1u), Sop1(16u, 0u, 0x20u, 8u)}));
ExpectThrow("escapes the constant-offset call/return model", [] {
Build({Call(0u, 8u, 3u), Sop2(4u, 8u, 0x0eu, 0u, 2u), Sop2(8u, 10u, 0x1bu, 8u, 2u), Sopp(12u, 1u), Sop1(16u, 0u, 0x20u, 8u)});
});
}

enum class HoistedShape { InLoop, Hoisted, SecondWriterOnArm, LoadOnArmOnly, WriterAfterCall, WriterAfterLoop, CallOnArm, ConstantPointer, SkipsNullTarget, LongBranchWriter };
Expand Down
66 changes: 37 additions & 29 deletions core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,9 @@ bool overwritesScalar(const RdnaInstruction& instruction, std::uint32_t reg, std
case RdnaOpcode::SBitset1B64:
case RdnaOpcode::SSwappcB64:
case RdnaOpcode::SSetpcB64: return false;
case RdnaOpcode::SMovB64:
case RdnaOpcode::SGetpcB64: return first != NoScalarRegister && reg >= first && reg - first < 2u;
default: return first != NoScalarRegister && reg == first;
default: return first != NoScalarRegister && reg >= first && reg - first < std::max(instruction.dataDwordCount, 1u);
}
case RdnaInstructionFamily::SOP2: return first != NoScalarRegister && reg == first;
case RdnaInstructionFamily::SOP2: return first != NoScalarRegister && reg >= first && reg - first < std::max(instruction.dataDwordCount, 1u);
case RdnaInstructionFamily::SOPK: return instruction.op == RdnaOpcode::SMovkI32 && reg == first;
case RdnaInstructionFamily::VOP1:
case RdnaInstructionFamily::VOP2:
Expand Down Expand Up @@ -1584,48 +1582,58 @@ LinkedCalls LinkCalls(const RdnaProgram& program, std::span<const UserDataCall>
linked[word] = 0xbb000000u | (userDataCalls[call].linkRegister << 16u) | static_cast<std::uint32_t>(offset);
linked.insert(linked.end(), userDataCallees[call].begin(), userDataCallees[call].end());
}
std::vector<std::pair<std::size_t, std::size_t>> stations;
for (std::size_t call = 0; call < tableCalls.size(); ++call) {
const auto& table = tableCalls[call];
const auto& callees = tableCallees[call];
const auto word = callWord(table.programCounter, "table");
const auto at = "the table callees of the call at program counter " + toHexString(table.programCounter);
const auto original = linked[word];
const auto count = callees.size();
const auto dispatch = linked.size();
const auto zero = dispatch + 1u;
const auto one = zero + count * 7u + 2u;
const auto calls = one + count * 8u + 2u;
const auto fault = calls + count * 2u;
const auto relays = 2u * (tableCalls.size() - call - 1u);
std::vector<std::size_t> path;
for (const auto& [position, block] : stations) path.push_back(position + 2u * (call - block - 1u));
const auto station = [&] {
if (relays == 0u) return;
stations.push_back({linked.size(), call});
linked.insert(linked.end(), relays, 0u);
};
station();
const auto start = linked.size();
std::vector<std::size_t> segments(count + 1u);
segments[0] = start + 1u;
for (std::size_t callee = 0; callee < count; ++callee) segments[callee + 1u] = segments[callee] + 16u + callees[callee].code.size() + relays;
const auto one = [&](std::size_t callee) { return segments[callee] + (callee < count ? 7u : 2u); };
const auto back = [&](std::size_t callee) { return callee != 0u ? segments[callee - 1u] + 15u : path.empty() ? word + 1u : path.back() + 1u; };
const auto fault = segments[count] + 3u;
const auto branch = [&](std::uint32_t opcode, std::size_t target) { linked.push_back(branchWord(opcode, linked.size(), target, at.c_str())); };
linked[word] = branchWord(0xbf820000u, word, dispatch, at.c_str());
branch(0xbf850000u, one);
for (const bool sccOne : {false, true}) {
for (std::size_t callee = 0; callee < count; ++callee) {
const auto next = linked.size() + (sccOne ? 8u : 7u);
for (std::size_t hop = 0; hop < path.size(); ++hop) {
linked[path[hop]] = branchWord(0xbf820000u, path[hop], hop + 1u < path.size() ? path[hop + 1u] : start, at.c_str());
linked[path[hop] + 1u] = branchWord(0xbf820000u, path[hop] + 1u, hop == 0u ? word + 1u : path[hop - 1u] + 1u, at.c_str());
}
linked[word] = branchWord(0xbf820000u, word, path.empty() ? start : path.front(), at.c_str());
branch(0xbf850000u, one(0u));
for (std::size_t callee = 0; callee < count; ++callee) {
for (const bool sccOne : {false, true}) {
const auto next = sccOne ? one(callee + 1u) : segments[callee + 1u];
linked.insert(linked.end(), {0xbf07ff00u | table.targetRegister, static_cast<std::uint32_t>(callees[callee].target)});
branch(0xbf850000u, next);
linked.insert(linked.end(), {0xbf07ff00u | (table.targetRegister + 1u), static_cast<std::uint32_t>(callees[callee].target >> 32u)});
branch(0xbf850000u, next);
if (sccOne) linked.push_back(0xbf068080u);
branch(0xbf820000u, calls + callee * 2u);
else branch(0xbf820000u, segments[callee] + 14u);
}
linked.push_back(sccOne ? 0xbf068080u : 0xbf078080u);
branch(0xbf820000u, fault);
}
for (std::size_t callee = 0; callee < count; ++callee) {
linked.push_back(0u);
branch(0xbf820000u, word + 1u);
linked.push_back(0xbb000001u | (table.linkRegister << 16u));
branch(0xbf820000u, back(callee));
linked.insert(linked.end(), callees[callee].code.begin(), callees[callee].code.end());
station();
}
linked.push_back(0xbf078080u);
branch(0xbf820000u, fault);
linked.push_back(0xbf068080u);
fallbacks.push_back(static_cast<std::uint32_t>(linked.size() * 4u));
linked.push_back(original);
branch(0xbf820000u, word + 1u);
for (std::size_t callee = 0; callee < count; ++callee) {
const auto site = calls + callee * 2u;
const auto offset = static_cast<std::int64_t>(linked.size()) - static_cast<std::int64_t>(site) - 1;
if (offset > 0x7fff) throw std::invalid_argument(at + " land out of s_call_b64 range");
linked[site] = 0xbb000000u | (table.linkRegister << 16u) | static_cast<std::uint32_t>(offset);
linked.insert(linked.end(), callees[callee].code.begin(), callees[callee].code.end());
}
branch(0xbf820000u, back(count));
}
linked.push_back(0xbf9f0000u);
return {std::move(linked), std::move(fallbacks)};
Expand Down
Loading
Loading