CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Software/RasterizerRegCache.cpp
Views: 1401
1
// Copyright (c) 2021- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "GPU/Software/RasterizerRegCache.h"
19
20
#include "Common/Arm64Emitter.h"
21
22
namespace Rasterizer {
23
24
void RegCache::SetupABI(const std::vector<Purpose> &args, bool forceRetain) {
25
#if PPSSPP_ARCH(ARM)
26
_assert_msg_(false, "Not yet implemented");
27
#elif PPSSPP_ARCH(ARM64_NEON)
28
using namespace Arm64Gen;
29
30
// ARM64 has a generous allotment of registers.
31
static const Reg genArgs[] = { X0, X1, X2, X3, X4, X5, X6, X7 };
32
static const Reg vecArgs[] = { Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7 };
33
size_t genIndex = 0;
34
size_t vecIndex = 0;
35
36
for (const Purpose &p : args) {
37
if ((p & FLAG_GEN) != 0) {
38
if (genIndex < ARRAY_SIZE(genArgs)) {
39
Add(genArgs[genIndex++], p);
40
if (forceRetain)
41
ForceRetain(p);
42
}
43
} else {
44
if (vecIndex < ARRAY_SIZE(vecArgs)) {
45
Add(vecArgs[vecIndex++], p);
46
if (forceRetain)
47
ForceRetain(p);
48
}
49
}
50
}
51
52
// Any others are free and purposeless.
53
for (size_t i = genIndex; i < ARRAY_SIZE(genArgs); ++i)
54
Add(genArgs[i], GEN_INVALID);
55
for (size_t i = vecIndex; i < ARRAY_SIZE(vecArgs); ++i)
56
Add(vecArgs[i], VEC_INVALID);
57
58
// Add all other caller saved regs without purposes yet.
59
static const Reg genTemps[] = { X8, X9, X10, X11, X12, X13, X14, X15 };
60
for (Reg r : genTemps)
61
Add(r, GEN_INVALID);
62
static const Reg vecTemps[] = { Q16, Q17, Q18, Q19, Q20, Q21, Q22, Q23 };
63
for (Reg r : vecTemps)
64
Add(r, VEC_INVALID);
65
// We also have X16-17 and Q24-Q31, but leave those for ordered paired instructions.
66
#elif PPSSPP_ARCH(X86)
67
_assert_msg_(false, "Not yet implemented");
68
#elif PPSSPP_ARCH(AMD64)
69
using namespace Gen;
70
71
#if PPSSPP_PLATFORM(WINDOWS)
72
// The Windows convention is annoying, as it wastes registers and keeps to "positions."
73
Reg genArgs[] = { RCX, RDX, R8, R9 };
74
Reg vecArgs[] = { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5 };
75
76
for (size_t i = 0; i < args.size(); ++i) {
77
const Purpose &p = args[i];
78
if ((p & FLAG_GEN) != 0) {
79
if (i < ARRAY_SIZE(genArgs)) {
80
Add(genArgs[i], p);
81
genArgs[i] = INVALID_REG;
82
if (forceRetain)
83
ForceRetain(p);
84
}
85
} else {
86
if (i < ARRAY_SIZE(vecArgs)) {
87
Add(vecArgs[i], p);
88
vecArgs[i] = INVALID_REG;
89
if (forceRetain)
90
ForceRetain(p);
91
}
92
}
93
}
94
95
// Any unused regs can be used freely as temps.
96
for (Reg r : genArgs) {
97
if (r != INVALID_REG)
98
Add(r, GEN_INVALID);
99
}
100
for (Reg r : vecArgs) {
101
if (r != INVALID_REG)
102
Add(r, VEC_INVALID);
103
}
104
105
// Additionally, these three are volatile.
106
// Must save: RBX, RSP, RBP, RDI, RSI, R12-R15, XMM6-15
107
static const Reg genTemps[] = { RAX, R10, R11 };
108
for (Reg r : genTemps)
109
Add(r, GEN_INVALID);
110
#else
111
// Okay, first, allocate args. SystemV gives to the first of each usable pool.
112
static const Reg genArgs[] = { RDI, RSI, RDX, RCX, R8, R9 };
113
static const Reg vecArgs[] = { XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7 };
114
size_t genIndex = 0;
115
size_t vecIndex = 0;
116
117
for (const Purpose &p : args) {
118
if ((p & FLAG_GEN) != 0) {
119
if (genIndex < ARRAY_SIZE(genArgs)) {
120
Add(genArgs[genIndex++], p);
121
if (forceRetain)
122
ForceRetain(p);
123
}
124
} else {
125
if (vecIndex < ARRAY_SIZE(vecArgs)) {
126
Add(vecArgs[vecIndex++], p);
127
if (forceRetain)
128
ForceRetain(p);
129
}
130
}
131
}
132
133
// Any others are free and purposeless.
134
for (size_t i = genIndex; i < ARRAY_SIZE(genArgs); ++i)
135
Add(genArgs[i], GEN_INVALID);
136
for (size_t i = vecIndex; i < ARRAY_SIZE(vecArgs); ++i)
137
Add(vecArgs[i], VEC_INVALID);
138
139
// Add all other caller saved regs without purposes yet.
140
// Must save: RBX, RSP, RBP, R12-R15
141
static const Reg genTemps[] = { RAX, R10, R11 };
142
for (Reg r : genTemps)
143
Add(r, GEN_INVALID);
144
static const Reg vecTemps[] = { XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15 };
145
for (Reg r : vecTemps)
146
Add(r, VEC_INVALID);
147
#endif
148
#elif PPSSPP_ARCH(RISCV64)
149
_assert_msg_(false, "Not yet implemented (no vector calling standard yet)");
150
#elif PPSSPP_ARCH(MIPS)
151
_assert_msg_(false, "Not yet implemented");
152
#else
153
_assert_msg_(false, "Not yet implemented");
154
#endif
155
}
156
157
void RegCache::Reset(bool validate) {
158
if (validate) {
159
for (auto &reg : regs) {
160
_assert_msg_(reg.locked == 0, "softjit: Reset() with reg still locked (%04X)", reg.purpose);
161
_assert_msg_(!reg.forceRetained, "softjit: Reset() with reg force retained (%04X)", reg.purpose);
162
}
163
}
164
regs.clear();
165
}
166
167
void RegCache::Add(Reg r, Purpose p) {
168
for (auto &reg : regs) {
169
if (reg.reg == r && (reg.purpose & FLAG_GEN) == (p & FLAG_GEN)) {
170
_assert_msg_(false, "softjit Add() reg duplicate (%04X)", p);
171
}
172
}
173
_assert_msg_(r != REG_INVALID_VALUE, "softjit Add() invalid reg (%04X)", p);
174
175
RegStatus newStatus;
176
newStatus.reg = r;
177
newStatus.purpose = p;
178
regs.push_back(newStatus);
179
}
180
181
void RegCache::Change(Purpose history, Purpose destiny) {
182
for (auto &reg : regs) {
183
if (reg.purpose == history) {
184
reg.purpose = destiny;
185
return;
186
}
187
}
188
189
_assert_msg_(false, "softjit Change() reg that isn't there (%04X)", history);
190
}
191
192
void RegCache::Release(Reg &r, Purpose p) {
193
RegStatus *status = FindReg(r, p);
194
_assert_msg_(status != nullptr, "softjit Release() reg that isn't there (%04X)", p);
195
_assert_msg_(status->locked > 0, "softjit Release() reg that isn't locked (%04X)", p);
196
_assert_msg_(!status->forceRetained, "softjit Release() reg that is force retained (%04X)", p);
197
198
status->locked--;
199
if (status->locked == 0) {
200
if ((status->purpose & FLAG_GEN) != 0)
201
status->purpose = GEN_INVALID;
202
else
203
status->purpose = VEC_INVALID;
204
}
205
206
r = REG_INVALID_VALUE;
207
}
208
209
void RegCache::Unlock(Reg &r, Purpose p) {
210
_assert_msg_((p & FLAG_TEMP) == 0, "softjit Unlock() temp reg (%04X)", p);
211
RegStatus *status = FindReg(r, p);
212
if (status) {
213
_assert_msg_(status->locked > 0, "softjit Unlock() reg that isn't locked (%04X)", p);
214
status->locked--;
215
r = REG_INVALID_VALUE;
216
return;
217
}
218
219
_assert_msg_(false, "softjit Unlock() reg that isn't there (%04X)", p);
220
}
221
222
bool RegCache::Has(Purpose p) {
223
for (auto &reg : regs) {
224
if (reg.purpose == p) {
225
return true;
226
}
227
}
228
return false;
229
}
230
231
RegCache::Reg RegCache::Find(Purpose p) {
232
for (auto &reg : regs) {
233
if (reg.purpose == p) {
234
_assert_msg_(reg.locked <= 255, "softjit Find() reg has lots of locks (%04X)", p);
235
reg.locked++;
236
reg.everLocked = true;
237
return reg.reg;
238
}
239
}
240
_assert_msg_(false, "softjit Find() reg that isn't there (%04X)", p);
241
return REG_INVALID_VALUE;
242
}
243
244
RegCache::Reg RegCache::Alloc(Purpose p) {
245
_assert_msg_(!Has(p), "softjit Alloc() reg duplicate (%04X)", p);
246
RegStatus *best = nullptr;
247
for (auto &reg : regs) {
248
if (reg.locked != 0 || reg.forceRetained)
249
continue;
250
// Needs to be the same type.
251
if ((reg.purpose & FLAG_GEN) != (p & FLAG_GEN))
252
continue;
253
254
if (best == nullptr)
255
best = &reg;
256
// Prefer a free/purposeless reg (includes INVALID.)
257
if ((reg.purpose & FLAG_TEMP) != 0) {
258
best = &reg;
259
break;
260
}
261
// But also prefer a lower priority reg.
262
if (reg.purpose < best->purpose)
263
best = &reg;
264
}
265
266
if (best) {
267
best->locked = 1;
268
best->everLocked = true;
269
best->purpose = p;
270
return best->reg;
271
}
272
273
_assert_msg_(false, "softjit Alloc() reg with none free (%04X)", p);
274
return REG_INVALID_VALUE;
275
}
276
277
void RegCache::ForceRetain(Purpose p) {
278
for (auto &reg : regs) {
279
if (reg.purpose == p) {
280
reg.forceRetained = true;
281
return;
282
}
283
}
284
285
_assert_msg_(false, "softjit ForceRetain() reg that isn't there (%04X)", p);
286
}
287
288
void RegCache::ForceRelease(Purpose p) {
289
for (auto &reg : regs) {
290
if (reg.purpose == p) {
291
_assert_msg_(reg.locked == 0, "softjit ForceRelease() while locked (%04X)", p);
292
reg.forceRetained = false;
293
if ((reg.purpose & FLAG_GEN) != 0)
294
reg.purpose = GEN_INVALID;
295
else
296
reg.purpose = VEC_INVALID;
297
return;
298
}
299
}
300
301
_assert_msg_(false, "softjit ForceRelease() reg that isn't there (%04X)", p);
302
}
303
304
void RegCache::GrabReg(Reg r, Purpose p, bool &needsSwap, Reg swapReg, Purpose swapPurpose) {
305
for (auto &reg : regs) {
306
if (reg.reg != r)
307
continue;
308
if ((reg.purpose & FLAG_GEN) != (p & FLAG_GEN))
309
continue;
310
311
// Easy version, it's free.
312
if (reg.locked == 0 && !reg.forceRetained) {
313
needsSwap = false;
314
reg.purpose = p;
315
reg.locked = 1;
316
reg.everLocked = true;
317
return;
318
}
319
320
// Okay, we need to swap. Find that reg.
321
needsSwap = true;
322
RegStatus *swap = FindReg(swapReg, swapPurpose);
323
if (swap) {
324
swap->purpose = reg.purpose;
325
swap->forceRetained = reg.forceRetained;
326
swap->locked = reg.locked;
327
swap->everLocked = true;
328
} else {
329
_assert_msg_(!Has(swapPurpose), "softjit GrabReg() wrong purpose (%04X)", swapPurpose);
330
RegStatus newStatus = reg;
331
newStatus.reg = swapReg;
332
newStatus.everLocked = true;
333
regs.push_back(newStatus);
334
}
335
336
reg.purpose = p;
337
reg.locked = 1;
338
reg.everLocked = true;
339
reg.forceRetained = false;
340
return;
341
}
342
343
_assert_msg_(false, "softjit GrabReg() reg that isn't there");
344
}
345
346
bool RegCache::ChangeReg(Reg r, Purpose p) {
347
for (auto &reg : regs) {
348
if (reg.reg != r)
349
continue;
350
if ((reg.purpose & FLAG_GEN) != (p & FLAG_GEN))
351
continue;
352
353
if (reg.purpose == p)
354
return true;
355
_assert_msg_(!Has(p), "softjit ChangeReg() duplicate purpose (%04X)", p);
356
357
if (reg.locked != 0 || reg.forceRetained)
358
return false;
359
360
reg.purpose = p;
361
// Since we're setting it's purpose, we must've used it.
362
reg.everLocked = true;
363
return true;
364
}
365
366
_assert_msg_(false, "softjit ChangeReg() reg that isn't there");
367
return false;
368
}
369
370
bool RegCache::UsedReg(Reg r, Purpose flag) {
371
for (auto &reg : regs) {
372
if (reg.reg != r)
373
continue;
374
if ((reg.purpose & FLAG_GEN) != (flag & FLAG_GEN))
375
continue;
376
return reg.everLocked;
377
}
378
379
_assert_msg_(false, "softjit UsedReg() reg that isn't there");
380
return false;
381
}
382
383
RegCache::RegStatus *RegCache::FindReg(Reg r, Purpose p) {
384
for (auto &reg : regs) {
385
if (reg.reg == r && reg.purpose == p) {
386
return &reg;
387
}
388
}
389
390
return nullptr;
391
}
392
393
CodeBlock::CodeBlock(int size)
394
#if PPSSPP_ARCH(ARM64_NEON)
395
: fp(this)
396
#endif
397
{
398
AllocCodeSpace(size);
399
ClearCodeSpace(0);
400
401
// Add some random code to "help" MSVC's buggy disassembler :(
402
#if defined(_WIN32) && (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !PPSSPP_PLATFORM(UWP)
403
using namespace Gen;
404
for (int i = 0; i < 100; i++) {
405
MOV(32, R(EAX), R(EBX));
406
RET();
407
}
408
#elif PPSSPP_ARCH(ARM)
409
BKPT(0);
410
BKPT(0);
411
#endif
412
}
413
414
int CodeBlock::WriteProlog(int extraStack, const std::vector<RegCache::Reg> &vec, const std::vector<RegCache::Reg> &gen) {
415
savedStack_ = 0;
416
firstVecStack_ = extraStack;
417
prologVec_ = vec;
418
prologGen_ = gen;
419
420
int totalStack = 0;
421
422
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
423
using namespace Gen;
424
425
BeginWrite(32768);
426
AlignCode16();
427
lastPrologStart_ = GetWritableCodePtr();
428
429
for (X64Reg r : gen) {
430
PUSH(r);
431
regCache_.Add(r, RegCache::GEN_INVALID);
432
totalStack += 8;
433
}
434
435
savedStack_ = 16 * (int)vec.size() + extraStack;
436
// We want to align if possible. It starts out unaligned.
437
if ((totalStack & 8) == 0)
438
savedStack_ += 8;
439
totalStack += savedStack_;
440
if (savedStack_ != 0)
441
SUB(64, R(RSP), Imm32(savedStack_));
442
443
int nextOffset = extraStack;
444
for (X64Reg r : vec) {
445
MOVUPS(MDisp(RSP, nextOffset), r);
446
regCache_.Add(r, RegCache::VEC_INVALID);
447
nextOffset += 16;
448
}
449
450
lastPrologEnd_ = GetWritableCodePtr();
451
#else
452
_assert_msg_(false, "Not yet implemented");
453
#endif
454
455
return totalStack;
456
}
457
458
const u8 *CodeBlock::WriteFinalizedEpilog() {
459
u8 *prologPtr = lastPrologStart_;
460
ptrdiff_t prologMaxSize = lastPrologEnd_ - lastPrologStart_;
461
lastPrologStart_ = nullptr;
462
lastPrologEnd_ = nullptr;
463
464
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
465
using namespace Gen;
466
467
bool prologChange = false;
468
int nextOffset = firstVecStack_;
469
for (X64Reg r : prologVec_) {
470
if (regCache_.UsedReg(r, RegCache::VEC_INVALID)) {
471
MOVUPS(r, MDisp(RSP, nextOffset));
472
nextOffset += 16;
473
} else {
474
prologChange = true;
475
}
476
}
477
478
// We use the stack offset in generated code, so maintain any difference.
479
int unusedGenSpace = 0;
480
for (X64Reg r : prologGen_) {
481
if (!regCache_.UsedReg(r, RegCache::GEN_INVALID))
482
unusedGenSpace += 8;
483
}
484
if (unusedGenSpace != 0)
485
prologChange = true;
486
487
if (savedStack_ + unusedGenSpace != 0)
488
ADD(64, R(RSP), Imm32(savedStack_ + unusedGenSpace));
489
for (int i = (int)prologGen_.size(); i > 0; --i) {
490
X64Reg r = prologGen_[i - 1];
491
if (regCache_.UsedReg(r, RegCache::GEN_INVALID))
492
POP(r);
493
}
494
495
RET();
496
EndWrite();
497
498
if (prologChange) {
499
// Okay, now let's rewrite the prolog since we didn't need all those regs.
500
XEmitter prolog(prologPtr);
501
if (PlatformIsWXExclusive()) {
502
ProtectMemoryPages(prologPtr, 128, MEM_PROT_READ | MEM_PROT_WRITE);
503
}
504
505
// First, write the new prolog at the original position.
506
for (X64Reg r : prologGen_) {
507
if (regCache_.UsedReg(r, RegCache::GEN_INVALID))
508
prolog.PUSH(r);
509
}
510
511
// Even if less of the stack is actually used, we want the number to match to references.
512
if (savedStack_ + unusedGenSpace != 0)
513
prolog.SUB(64, R(RSP), Imm32(savedStack_ + unusedGenSpace));
514
515
nextOffset = firstVecStack_;
516
for (X64Reg r : prologVec_) {
517
if (regCache_.UsedReg(r, RegCache::VEC_INVALID)) {
518
prolog.MOVUPS(MDisp(RSP, nextOffset), r);
519
nextOffset += 16;
520
}
521
}
522
523
ptrdiff_t prologLen = prolog.GetWritableCodePtr() - prologPtr;
524
if (prologLen < prologMaxSize) {
525
// We wrote it at the start, but we actually want it at the end.
526
u8 *oldPrologPtr = prologPtr;
527
prologPtr += prologMaxSize - prologLen;
528
memmove(prologPtr, oldPrologPtr, prologLen);
529
// Set INT3s before the new start to be safe.
530
memset(oldPrologPtr, 0xCC, prologMaxSize - prologLen);
531
}
532
533
if (PlatformIsWXExclusive()) {
534
ProtectMemoryPages(prologPtr, 128, MEM_PROT_READ | MEM_PROT_EXEC);
535
}
536
}
537
#else
538
_assert_msg_(false, "Not yet implemented");
539
#endif
540
541
return prologPtr;
542
}
543
544
RegCache::Reg CodeBlock::GetZeroVec() {
545
if (!regCache_.Has(RegCache::VEC_ZERO)) {
546
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
547
using namespace Gen;
548
X64Reg r = regCache_.Alloc(RegCache::VEC_ZERO);
549
PXOR(r, R(r));
550
return r;
551
#else
552
return RegCache::REG_INVALID_VALUE;
553
#endif
554
}
555
return regCache_.Find(RegCache::VEC_ZERO);
556
}
557
558
void CodeBlock::Describe(const std::string &message) {
559
descriptions_[GetCodePointer()] = message;
560
}
561
562
std::string CodeBlock::DescribeCodePtr(const u8 *ptr) {
563
ptrdiff_t dist = 0x7FFFFFFF;
564
std::string found;
565
for (const auto &it : descriptions_) {
566
ptrdiff_t it_dist = ptr - it.first;
567
if (it_dist >= 0 && it_dist < dist) {
568
found = it.second;
569
dist = it_dist;
570
}
571
}
572
return found;
573
}
574
575
void CodeBlock::Clear() {
576
ClearCodeSpace(0);
577
descriptions_.clear();
578
}
579
580
void CodeBlock::WriteSimpleConst16x8(const u8 *&ptr, uint8_t value) {
581
if (ptr == nullptr)
582
WriteDynamicConst16x8(ptr, value);
583
}
584
585
void CodeBlock::WriteSimpleConst8x16(const u8 *&ptr, uint16_t value) {
586
if (ptr == nullptr)
587
WriteDynamicConst8x16(ptr, value);
588
}
589
590
void CodeBlock::WriteSimpleConst4x32(const u8 *&ptr, uint32_t value) {
591
if (ptr == nullptr)
592
WriteDynamicConst4x32(ptr, value);
593
}
594
595
void CodeBlock::WriteDynamicConst16x8(const u8 *&ptr, uint8_t value) {
596
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
597
ptr = AlignCode16();
598
for (int i = 0; i < 16; ++i)
599
Write8(value);
600
#else
601
_assert_msg_(false, "Not yet implemented");
602
#endif
603
}
604
605
void CodeBlock::WriteDynamicConst8x16(const u8 *&ptr, uint16_t value) {
606
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
607
ptr = AlignCode16();
608
for (int i = 0; i < 8; ++i)
609
Write16(value);
610
#else
611
_assert_msg_(false, "Not yet implemented");
612
#endif
613
}
614
615
void CodeBlock::WriteDynamicConst4x32(const u8 *&ptr, uint32_t value) {
616
#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)
617
ptr = AlignCode16();
618
for (int i = 0; i < 4; ++i)
619
Write32(value);
620
#else
621
_assert_msg_(false, "Not yet implemented");
622
#endif
623
}
624
625
};
626
627