Repository navigation
Expand file tree
/
Copy pathBdaResources.cpp
More file actions
337 lines (312 loc) · 17.4 KB
/
Copy pathBdaResources.cpp
File metadata and controls
337 lines (312 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "prx/libSceAgcDriver/Graphics/include/BdaResources.hpp"
#include "ImageTableAbi.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include <algorithm>
#include <atomic>
#include <bit>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <list>
#include <mutex>
#include <iomanip>
#include <sstream>
namespace AgcDriver::Graphics {
namespace {
// The page tables built per device, reused by a build whose ranges (guest ranges, device
// addresses, permissions) equal one of them: consecutive address-based builds map the same registered
// set through the same imports and mirrors, so the ~1200-entry table repeats. The shader only reads
// the table (its lookup keeps its state in function variables), and a batch in flight keeps its
// BdaResources, so sharing the buffer is safe. The cache holds the tables weakly: they never outlive
// the builds that own them (a static owner would destroy the buffer after its device, at exit or on
// device replacement, and would fail the tests' leak check), and with deferred lease release the
// previous builds are still in flight when the next one asks, which is what makes the hit rate.
// One table was enough while every build repeated the last one exactly; now the misaligned
// descriptor sub-ranges the GPU copies out of imports sit in pool staging buffers whose device
// addresses cycle through the few buffers the batches in flight leave free, and the three
// address-based programs of one queue interleave with the lease draws, so a table differs from the
// previous one and comes back a few builds later (69% misses in the wave-8 run, 4% before the GPU
// copies). Hence several recent tables, most recently used first, matched by a word hash before
// the byte compare. APS5_BDA_TABLE_CACHE_ENTRIES sets how many (default 8; 1 is the previous
// behaviour); APS5_NO_BDA_TABLE_CACHE=1 builds every table.
// A table built for (or found equal to) the cached address space's ranges carries the space's
// serial: a build served by that space alone binds it by the serial, with no hash and no compare.
struct TableEntry {
std::uint64_t hash;
std::vector<ShaderRecompiler::BdaAbi::Range> ranges;
std::weak_ptr<Buffer> buffer;
std::uint64_t spaceSerial = 0;
};
struct TableCache {
std::mutex mutex;
VkDevice device = VK_NULL_HANDLE;
std::list<TableEntry> entries;
std::uint64_t hits = 0;
std::uint64_t misses = 0;
BdaResources::TableCacheStats classes;
};
bool sameRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& left, const std::vector<ShaderRecompiler::BdaAbi::Range>& right);
// APS5_PROFILE_DRAW: why the most recently used entry did not serve this build (see
// TableCacheStats). Under the cache mutex.
void classifyFirstEntry(TableCache& cache, std::uint64_t hash, const std::vector<ShaderRecompiler::BdaAbi::Range>& ranges) {
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
if (!profile) return;
auto& classes = cache.classes;
if (cache.entries.empty()) {
++classes.firstEmpty;
return;
}
const auto& first = cache.entries.front();
if (first.hash == hash) {
if (first.buffer.expired()) ++classes.firstExpired;
else if (!sameRanges(first.ranges, ranges)) ++classes.firstSameHash;
return;
}
if (first.buffer.expired()) {
++classes.firstExpired;
return;
}
const auto common = std::min(first.ranges.size(), ranges.size());
std::size_t at = 0;
while (at < common && std::memcmp(&first.ranges[at], &ranges[at], sizeof(ranges[at])) == 0) ++at;
const auto begin = at < ranges.size() ? ranges[at].begin : at < first.ranges.size() ? first.ranges[at].begin : 0;
++(begin < 0x10000000ull ? classes.firstDiffersLow : classes.firstDiffersHeap);
}
TableCache& Tables() {
static TableCache cache;
return cache;
}
bool tableCacheEnabled() {
static const bool disabled = std::getenv("APS5_NO_BDA_TABLE_CACHE") != nullptr;
return !disabled;
}
std::size_t tableCacheEntries() {
static const std::size_t entries = [] {
const char* value = std::getenv("APS5_BDA_TABLE_CACHE_ENTRIES");
const auto parsed = value != nullptr ? std::strtoull(value, nullptr, 10) : 8ull;
return static_cast<std::size_t>(std::clamp<unsigned long long>(parsed, 1, 64));
}();
return entries;
}
bool sameRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& left, const std::vector<ShaderRecompiler::BdaAbi::Range>& right) {
return left.size() == right.size() && (left.empty() || std::memcmp(left.data(), right.data(), left.size() * sizeof(left.front())) == 0);
}
// A 64-bit mix of the table's words: a table is ~1200 ranges of four words, and comparing the bytes
// against every held table would cost as much as building it.
std::uint64_t hashRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& ranges) {
static_assert(sizeof(ShaderRecompiler::BdaAbi::Range) % sizeof(std::uint64_t) == 0);
std::uint64_t hash = 0x9e3779b97f4a7c15ull ^ ranges.size();
for (const auto& range : ranges) {
std::uint64_t words[sizeof(range) / sizeof(std::uint64_t)];
std::memcpy(words, &range, sizeof(range));
for (const auto word : words) {
hash ^= word;
hash *= 0xff51afd7ed558ccdull;
hash ^= hash >> 33u;
}
}
return hash;
}
}
BdaResources::BdaResources(const Context& context) {
static_assert(std::endian::native == std::endian::little);
Require(ShaderRecompiler::BdaAbi::FaultBufferBytes <= context.limits.maxStorageBufferRange, "BDA fault buffer exceeds storage buffer range limit");
fault = std::make_unique<Buffer>(context, ShaderRecompiler::BdaAbi::FaultBufferBytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
std::memset(fault->Bytes().data(), 0, ShaderRecompiler::BdaAbi::WrittenListWord * sizeof(std::uint32_t));
}
BdaResources::BdaResources(const Context& context, const GuestBufferMemory& memory) : BdaResources(context) {
const auto cached = memory.CachedAddressTable();
std::vector<ShaderRecompiler::BdaAbi::Range> built;
if (!cached.has_value()) built = memory.AddressRanges();
const auto& ranges = cached.has_value() ? *cached->ranges : built;
const auto serial = cached.has_value() ? cached->serial : 0;
Require(ranges.size() <= std::numeric_limits<std::uint32_t>::max(), "BDA table range count overflow");
Require(ranges.size() <= (std::numeric_limits<std::size_t>::max() - sizeof(ShaderRecompiler::BdaAbi::Header)) / sizeof(ShaderRecompiler::BdaAbi::Range), "BDA table size overflow");
tableBytes = sizeof(ShaderRecompiler::BdaAbi::Header) + ranges.size() * sizeof(ShaderRecompiler::BdaAbi::Range);
Require(tableBytes <= context.limits.maxStorageBufferRange && ShaderRecompiler::BdaAbi::FaultBufferBytes <= context.limits.maxStorageBufferRange, "BDA descriptors exceed storage buffer range limit");
auto& cache = Tables();
std::uint64_t hash = 0;
if (tableCacheEnabled()) {
std::lock_guard lock(cache.mutex);
// Another device's tables are not this one's (their buffers are dead or foreign).
if (cache.device != context.device) {
cache.entries.clear();
cache.device = context.device;
}
if (serial != 0) {
for (auto it = cache.entries.begin(); it != cache.entries.end(); ++it) {
if (it->spaceSerial != serial) continue;
if (auto shared = it->buffer.lock(); shared != nullptr) {
++cache.hits;
++cache.classes.spaceTables;
table = std::move(shared);
cache.entries.splice(cache.entries.begin(), cache.entries, it);
return;
}
cache.entries.erase(it);
break;
}
}
hash = hashRanges(ranges);
classifyFirstEntry(cache, hash, ranges);
for (auto it = cache.entries.begin(); it != cache.entries.end();) {
// Compare the hash before taking a strong reference: locking a non-matching entry's
// buffer could make this thread its last owner (a concurrent reap releasing it) and
// run the pool release under the cache mutex; expired() takes no reference.
if (it->hash != hash) {
if (it->buffer.expired()) {
it = cache.entries.erase(it);
} else {
++it;
}
continue;
}
auto shared = it->buffer.lock();
if (shared == nullptr) {
// Every build that owned the table is gone, and its buffer with it.
it = cache.entries.erase(it);
continue;
}
if (sameRanges(it->ranges, ranges)) {
++cache.hits;
// An equal table of an earlier space (a rebuild that mapped the same ranges the
// same way): the next build of this space binds it by the serial.
if (serial != 0) it->spaceSerial = serial;
table = std::move(shared);
cache.entries.splice(cache.entries.begin(), cache.entries, it);
return;
}
++it;
}
}
Require(ShaderRecompiler::BdaAbi::RangesOrdered(ranges.data(), ranges.size()), "BDA table ranges do not ascend by begin");
for (const auto& range : ranges) Require((range.permissions & ShaderRecompiler::BdaAbi::Write) == 0 || range.end <= ShaderRecompiler::BdaAbi::WrittenAddressLimit, "a writable BDA range ends past the addresses the written-page record can hold");
table = std::make_shared<Buffer>(context, tableBytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
const ShaderRecompiler::BdaAbi::Header header{ShaderRecompiler::BdaAbi::Version, static_cast<std::uint32_t>(ranges.size()), sizeof(ShaderRecompiler::BdaAbi::Range), 0};
std::memcpy(table->Bytes().data(), &header, sizeof(header));
if (!ranges.empty()) std::memcpy(table->Bytes().data() + sizeof(header), ranges.data(), ranges.size() * sizeof(ranges.front()));
if (tableCacheEnabled()) {
std::lock_guard lock(cache.mutex);
++cache.misses;
if (cache.device != context.device) {
cache.entries.clear();
cache.device = context.device;
}
cache.entries.push_front({hash, cached.has_value() ? ranges : std::move(built), table, serial});
while (cache.entries.size() > tableCacheEntries()) cache.entries.pop_back();
}
}
BdaResources::TableCacheStats BdaResources::TableCacheCounters() {
auto& cache = Tables();
std::lock_guard lock(cache.mutex);
auto stats = cache.classes;
stats.hits = cache.hits;
stats.misses = cache.misses;
stats.held = cache.entries.size();
return stats;
}
VkDescriptorBufferInfo BdaResources::Table() const {
Require(table != nullptr, "BDA page table was not requested");
return {table->Handle(), 0, tableBytes};
}
VkDescriptorBufferInfo BdaResources::Fault() const {
return {fault->Handle(), 0, ShaderRecompiler::BdaAbi::FaultBufferBytes};
}
namespace {
std::atomic<bool> loopGuardTripped{false};
}
bool LoopGuardTripped() {
return loopGuardTripped.load(std::memory_order_relaxed);
}
void BdaResources::CheckFault() const {
markWrittenPages();
ShaderRecompiler::BdaAbi::Fault report{};
std::memcpy(&report, fault->Bytes().data(), sizeof(report));
if (report.state == ShaderRecompiler::BdaAbi::FaultState::Empty) {
Require(static_cast<std::uint32_t>(report.reason) == 0 && report.address == 0 && report.bytes == 0 && report.stage == 0 && report.instruction == 0 && report.otherFaults == 0, "BDA fault record has data without publication");
return;
}
Require(report.state == ShaderRecompiler::BdaAbi::FaultState::Ready && report.otherFaults <= 1, "incomplete or invalid BDA fault record");
Require(report.reason != ShaderRecompiler::BdaAbi::FaultReason::InvalidRectangle, "rect-list requires finite nondegenerate axis-aligned positions with equal positive W");
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::LoopLimit) {
// APS5_LOOP_GUARD: the shader left a loop that ran past the guard; the dispatch result is kept.
loopGuardTripped.store(true, std::memory_order_relaxed);
std::fprintf(stderr, "[gpu] loop guard: the loop exit at pc 0x%x of shader 0x%llx ran past %u evaluations\n", report.instruction, static_cast<unsigned long long>(report.address), report.bytes);
std::memset(fault->Bytes().data(), 0, fault->Bytes().size());
return;
}
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::ImageTable) {
const auto table = std::find_if(imageTables.begin(), imageTables.end(), [&](const ImageTableFaults& candidate) { return candidate.stage == report.stage; });
const auto* tables = table != imageTables.end() ? &*table : imageTables.size() == 1 ? &imageTables.front() : nullptr;
std::ostringstream message;
message << "image table: shader 0x" << std::hex << (tables != nullptr ? tables->shader : 0u) << std::dec << " stage " << report.stage << " pc 0x" << std::hex << report.instruction << " selected the descriptor at 0x" << report.address << std::dec;
if (tables != nullptr && report.bytes < tables->poison.size()) {
const auto& poison = tables->poison[report.bytes];
if (poison.dwordCount != 0) {
message << " (words";
for (std::uint32_t index = 0; index < poison.dwordCount && index < poison.words.size(); ++index) message << ' ' << std::hex << std::setw(8) << std::setfill('0') << poison.words[index];
message << std::dec << ")";
}
message << ": " << ShaderRecompiler::ImageTableAbi::PoisonReasonName(static_cast<ShaderRecompiler::ImageTableAbi::PoisonReason>(poison.reason));
if (report.bytes < tables->messages.size() && !tables->messages[report.bytes].empty()) message << ": " << tables->messages[report.bytes];
} else {
message << ": poison entry " << report.bytes;
}
throw std::runtime_error(message.str());
}
std::ostringstream message;
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::UnlinkedCall) {
message << "shader stage " << report.stage << " called address 0x" << std::hex << report.address << " at program counter 0x" << report.instruction << ", which no linked callee of that call matches";
throw std::runtime_error(message.str());
}
message << "BDA access failed: address=0x" << std::hex << report.address << " instruction=0x" << report.instruction << std::dec << " bytes=" << report.bytes << " stage=" << report.stage << " reason=" << static_cast<std::uint32_t>(report.reason);
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::Permission && table != nullptr) {
const auto bytes = table->Bytes();
ShaderRecompiler::BdaAbi::Header header{};
std::memcpy(&header, bytes.data(), sizeof(header));
for (std::uint32_t index = 0; index < header.count; ++index) {
ShaderRecompiler::BdaAbi::Range range{};
std::memcpy(&range, bytes.data() + sizeof(header) + index * sizeof(range), sizeof(range));
if (report.address < range.begin || report.address >= range.end) continue;
message << std::hex << "; the store hit 0x" << range.begin << "+0x" << range.end - range.begin << ", read-only in the BDA table: stores through GPU-selected descriptors and FLAT/GLOBAL addresses reach only writable ranges imported in place, not ones served by a mirror or a copy (past APS5_HOST_IMPORT_MIB, or refused by the driver)";
break;
}
}
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::Unaligned) {
if (report.otherFaults == 0) throw BdaUnalignedAccess(message.str(), report.instruction);
message << "; a fault of another kind was recorded in the same dispatch";
}
throw std::runtime_error(message.str());
}
}
namespace AgcDriver::Graphics {
void BdaResources::SetImageTables(std::vector<ImageTableFaults> tables, std::vector<std::pair<std::uint64_t, std::uint64_t>> ranges) {
imageTables = std::move(tables);
imageTableRanges = std::move(ranges);
}
void BdaResources::markWrittenPages() const {
namespace Abi = ShaderRecompiler::BdaAbi;
auto* words = reinterpret_cast<std::uint32_t*>(fault->Bytes().data());
Require(words[Abi::WrittenOverflowWord] == 0, "more than " + std::to_string(Abi::WrittenPageSlots) + " chunks of " + std::to_string(Abi::WrittenChunkPages) + " pages stored to through the BDA table in one use are not implemented");
const auto count = words[Abi::WrittenCountWord];
if (count == 0) return;
Require(Abi::ForEachWrittenRange(words, [&](std::uint64_t begin, std::size_t bytes) {
const auto end = begin + bytes;
for (const auto& [base, size] : imageTableRanges) {
if (size != 0 && begin < base + size && base < end) {
std::ostringstream message;
message << "image table at 0x" << std::hex << base << " is written by its own dispatch";
Require(false, message.str());
}
}
GuestMemory::MarkWritten(begin, bytes);
}), "invalid BDA written-page record");
for (std::uint32_t claim = 0; claim < count; ++claim) {
const auto slot = words[Abi::WrittenListWord + claim];
words[Abi::WrittenSlotsWord + 2 * slot] = 0;
words[Abi::WrittenSlotsWord + 2 * slot + 1] = 0;
}
words[Abi::WrittenCountWord] = 0;
}
}