Repository navigation
Expand file tree
/
Copy pathResourceTracker.cpp
More file actions
1117 lines (1047 loc) · 48.6 KB
/
Copy pathResourceTracker.cpp
File metadata and controls
1117 lines (1047 loc) · 48.6 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Optimization/ResourceTracker.hpp"
#include "Optimization/SrtWalker.hpp"
#include "Optimization/ResourceMaterializer.hpp"
#include "SpirvBackend/SpirvBufferFormat.hpp"
#include "IntermediateRepresentation/IrBuilder.hpp"
#include "Optimization/SrtWalker/SrtInstructionPredicates.hpp"
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <span>
#include <string>
#include <utility>
#include <vector>
namespace ShaderRecompiler {
namespace {
constexpr std::uint32_t samplerBorderClampMask = (1u << 2u) | (1u << 5u) | (1u << 8u);
constexpr std::uint32_t samplerDword3ReservedMask = 0x3ffff000u;
[[noreturn]] void fail(const std::string& message) {
throw std::runtime_error(message);
}
// Debug aid: APS5_TRACE_BDA=1 names the accesses that make a program address-based (see Collect).
bool bdaTraceEnabled() {
static const bool enabled = std::getenv("APS5_TRACE_BDA") != nullptr;
return enabled;
}
constexpr unsigned bdaTraceLimit = 8;
std::string formatHex32(std::uint32_t value) {
static constexpr char digits[] = "0123456789abcdef";
std::string hex(8u, '0');
for (std::uint32_t index = 0; index < 8u; index++) {
hex[7u - index] = digits[(value >> (index * 4u)) & 0xfu];
}
return hex;
}
std::string describeValueChain(const IrValue* value, std::uint32_t depth) {
value = value->Resolve();
if (value->HasImmediate()) {
return "Immediate";
}
std::string text = std::string(IrOpcodeName(value->Opcode()));
if (value->Opcode() == IrOpcode::LoadAddressU32 || value->Opcode() == IrOpcode::ReadConstBuffer) {
text += "@pc=0x" + formatHex32(value->Flags<MemoryFlags>().pc);
}
if (depth == 0u || value->ArgumentCount() == 0u) {
return text;
}
text += "(";
for (std::uint32_t index = 0; index < value->ArgumentCount(); index++) {
if (index != 0u) {
text += ", ";
}
text += describeValueChain(value->Argument(index), depth - 1u);
}
text += ")";
return text;
}
std::uint32_t possibleU32Bits(const IrValue* value) {
value = value->Resolve();
if (value->HasImmediate()) {
return value->Type() == IrType::U32 ? value->ImmediateU32() : std::numeric_limits<std::uint32_t>::max();
}
switch (value->Opcode()) {
case IrOpcode::BitwiseAnd32:
return possibleU32Bits(value->Argument(0)) & possibleU32Bits(value->Argument(1));
case IrOpcode::BitwiseOr32:
return possibleU32Bits(value->Argument(0)) | possibleU32Bits(value->Argument(1));
case IrOpcode::ShiftLeftLogical32: {
const IrValue* shift = value->Argument(1)->Resolve();
return shift->HasImmediate() && shift->Type() == IrType::U32 ? possibleU32Bits(value->Argument(0)) << (shift->ImmediateU32() & 31u) : std::numeric_limits<std::uint32_t>::max();
}
default:
return std::numeric_limits<std::uint32_t>::max();
}
}
std::uint32_t byteExtent(const MemoryInfo& memory) {
const auto bytes = std::max((memory.dataBits + 7u) / 8u, 1u);
const auto count = std::max(memory.dataDwords, 1u);
const auto end = static_cast<std::uint64_t>(memory.offset) + static_cast<std::uint64_t>(bytes) * count;
return end > std::numeric_limits<std::uint32_t>::max() ? std::numeric_limits<std::uint32_t>::max() : static_cast<std::uint32_t>(end);
}
class Tracker {
public:
explicit Tracker(IrProgram& program) : m_program(program), m_info(program.Resources().info), m_builder(program) {
m_info.buffers.clear();
m_info.images.clear();
m_info.samplers.clear();
m_info.sampledPairs.clear();
m_info.usesDma = false;
m_info.bdaWrites = false;
}
void Run() {
if (m_program.Resources().resourceTrackingComplete) {
fail("resources already tracked");
}
if (!m_program.Resources().srtPlanComplete) {
fail("SRT plan is not ready");
}
SplitDescriptorPhis();
PlanIndirectImages();
for (auto& block : m_program.Blocks()) {
for (IrValue* inst : block->Instructions()) {
Collect(*inst);
}
}
if (m_bdaTraces > bdaTraceLimit) std::fprintf(stderr, "[bda] %u more address accesses in this program not shown\n", m_bdaTraces - bdaTraceLimit);
LinkImageAliases();
for (const auto& patch : m_handlePatches) {
patch.handle->SetFlags<std::uint32_t>(patch.resource);
}
for (const auto& patch : m_memoryPatches) {
auto& memory = m_program.Resources().memoryInfo[patch.index];
memory.resource = patch.resource;
if (patch.hasSampler) {
memory.sampler = patch.sampler;
}
}
for (const auto& plan : m_indirectImages) {
plan.handle->ReplaceArgument(0, plan.key);
for (std::uint32_t dword = 0; dword < 4u; dword++) {
plan.handle->ReplaceArgument(dword + 1u, plan.roots[dword]);
}
for (std::uint32_t dword = 5u; dword < plan.roots.size(); dword++) {
plan.handle->ReplaceArgument(dword, plan.key);
}
for (const auto index : plan.memory) {
m_program.Resources().memoryInfo[index].planningOnly = true;
}
}
std::erase_if(m_program.Metadata().dynamicReads, [&](IrValue* value) {
const IrValue* inst = value->Resolve();
return std::ranges::any_of(m_indirectImages, [&](const IndirectImagePlan& plan) {
return std::ranges::find(plan.reads, inst) != plan.reads.end();
});
});
m_program.Resources().descriptorSources = std::move(m_sources);
m_program.Resources().info = std::move(m_info);
m_program.Resources().resourceTrackingComplete = true;
}
private:
struct HandlePatch {
IrValue* handle = nullptr;
std::uint32_t resource = 0;
};
struct MemoryPatch {
std::uint32_t index = 0;
std::uint32_t resource = 0;
std::uint32_t sampler = 0;
bool hasSampler = false;
};
struct IndirectImagePlan {
IrValue* handle = nullptr;
std::uint32_t source = 0;
IrValue* key = nullptr;
std::array<IrValue*, 8> roots {};
std::array<std::uint32_t, 8> memory {};
std::array<const IrValue*, 8> reads {};
};
void MakeSource(const IrValue& handle, std::uint32_t width, bool sampler, bool sampleAdjust, DescriptorSource& descriptor) {
if (handle.ArgumentCount() != width) {
fail(std::string(IrOpcodeName(handle.Opcode())) + " has " + std::to_string(handle.ArgumentCount()) + " descriptor dwords, expected " + std::to_string(width));
}
descriptor.dwordCount = width;
for (std::uint32_t i = 0; i < width; i++) {
descriptor.dwords[i] = handle.Argument(i)->Resolve();
}
if (sampleAdjust) {
descriptor.dwords[3] = canonicalizeSampleAdjustDword3(descriptor.dwords[3]);
}
const IrValue* dword0 = descriptor.dwords[0]->Resolve();
if (sampler && dword0->HasImmediate() && dword0->Type() == IrType::U32 && (dword0->ImmediateU32() & samplerBorderClampMask) == 0u) {
descriptor.dwords[3] = &m_builder.Constant(0u);
}
}
IrValue* canonicalizeSampleAdjustDword3(IrValue* value) {
for (;;) {
value = value->Resolve();
if (value->Opcode() != IrOpcode::BitwiseOr32) {
return value;
}
IrValue* left = value->Argument(0)->Resolve();
IrValue* right = value->Argument(1)->Resolve();
const bool leftReserved = (possibleU32Bits(left) & ~samplerDword3ReservedMask) == 0u;
const bool rightReserved = (possibleU32Bits(right) & ~samplerDword3ReservedMask) == 0u;
if (leftReserved && rightReserved) {
return &m_builder.Constant(0u);
}
if (leftReserved) {
value = right;
} else if (rightReserved) {
value = left;
} else {
return value;
}
}
}
bool ValidateSource(const DescriptorSource& descriptor, std::uint32_t& badDword) const {
constexpr SrtWalker walker;
for (std::uint32_t i = 0; i < descriptor.dwordCount; i++) {
badDword = i;
if (descriptor.dwords[i]->Resolve()->Type() != IrType::U32) {
return false;
}
if (!walker.ValidateRuntimeValue(m_program.Resources(), descriptor.dwords[i])) {
return false;
}
}
return true;
}
std::uint32_t InternSource(const DescriptorSource& descriptor) {
for (std::uint32_t candidate = 0; candidate < m_sources.size(); candidate++) {
const auto& current = m_sources[candidate];
if (current.dwordCount != descriptor.dwordCount || current.indirectImage != descriptor.indirectImage) {
continue;
}
bool same = true;
for (std::uint32_t i = 0; i < descriptor.dwordCount; i++) {
same = same && EquivalentValue(m_program.Resources(), current.dwords[i], descriptor.dwords[i]);
}
if (same) {
return candidate;
}
}
m_sources.push_back(descriptor);
return static_cast<std::uint32_t>(m_sources.size() - 1);
}
static bool immediateU32(IrValue* value, std::uint32_t& result) {
value = value->Resolve();
if (!value->HasImmediate() || value->Type() != IrType::U32) {
return false;
}
result = value->ImmediateU32();
return true;
}
static bool usesOnly(const IrValue& value, std::span<const IrValue* const> users) {
return !value.Uses().empty() && std::ranges::all_of(value.Uses(), [&](const IrValue* user) {
return std::ranges::find(users, user) != users.end();
});
}
const MemoryInfo* ScalarReadMemory(const IrValue& read, std::uint32_t& index) const {
if (read.Opcode() != IrOpcode::ReadConstBuffer || read.ArgumentCount() != 2u) {
return nullptr;
}
index = read.Flags<MemoryFlags>().index;
if (index >= m_program.Resources().memoryInfo.size()) {
return nullptr;
}
const auto& memory = m_program.Resources().memoryInfo[index];
return memory.kind == ResourceKind::ScalarBuffer && memory.dataBits == 32u && memory.dataDwords == 1u ? &memory : nullptr;
}
bool MemoryIndexBelongsTo(std::uint32_t index, const IrValue& owner) const {
for (const auto& block : m_program.Blocks()) {
for (const IrValue* inst : block->Instructions()) {
const auto op = inst->Opcode();
if ((BufferAccessOf(op) == BufferAccess::None && AddressOpcodeInfoOf(op).access == AddressAccess::None && ImageOpcodeInfoOf(op).access == ImageAccess::None) || inst == &owner) {
continue;
}
if (inst->Flags<MemoryFlags>().index == index) {
return false;
}
}
}
return true;
}
bool MakeRuntimeBufferSource(const IrValue& handle, std::uint32_t& source, DescriptorSource& descriptor) {
if (handle.Opcode() != IrOpcode::GetBufferResource) {
return false;
}
MakeSource(handle, 4u, false, false, descriptor);
std::uint32_t badDword = 0;
if (!ValidateSource(descriptor, badDword)) {
return false;
}
source = InternSource(descriptor);
return true;
}
bool MatchMaterialOffset(IrValue* value, IrValue*& selector, std::uint32_t& stride, std::uint32_t& offset) const {
value = value->Resolve();
offset = 0;
IrValue* candidate = value;
if (candidate->Opcode() == IrOpcode::IAdd32 && candidate->ArgumentCount() == 2u) {
std::uint32_t immediate = 0;
if (immediateU32(candidate->Argument(0), immediate)) {
value = candidate->Argument(1)->Resolve();
} else if (immediateU32(candidate->Argument(1), immediate)) {
value = candidate->Argument(0)->Resolve();
} else {
return false;
}
offset = immediate;
}
IrValue* multiply = value;
if (multiply->Opcode() != IrOpcode::IMul32 || multiply->ArgumentCount() != 2u) {
return false;
}
if (immediateU32(multiply->Argument(0), stride)) {
selector = multiply->Argument(1)->Resolve();
} else if (immediateU32(multiply->Argument(1), stride)) {
selector = multiply->Argument(0)->Resolve();
} else {
return false;
}
return stride != 0u && selector->Opcode() == IrOpcode::ReadFirstLane;
}
// The table entry offset `key * 32` (a shift or a multiply), optionally plus an immediate.
static bool MatchTableOffset(IrValue* value, IrValue*& key, std::uint32_t& entryOffset) {
value = value->Resolve();
entryOffset = 0;
if (value->Opcode() == IrOpcode::IAdd32 && value->ArgumentCount() == 2u) {
std::uint32_t immediate = 0;
if (immediateU32(value->Argument(0), immediate)) {
value = value->Argument(1)->Resolve();
} else if (immediateU32(value->Argument(1), immediate)) {
value = value->Argument(0)->Resolve();
} else {
return false;
}
entryOffset = immediate;
}
if (value->ArgumentCount() != 2u) {
return false;
}
std::uint32_t scale = 0;
if (value->Opcode() == IrOpcode::ShiftLeftLogical32) {
if (!immediateU32(value->Argument(1), scale) || scale != 5u) {
return false;
}
key = value->Argument(0)->Resolve();
return true;
}
if (value->Opcode() != IrOpcode::IMul32) {
return false;
}
if (immediateU32(value->Argument(1), scale)) {
key = value->Argument(0)->Resolve();
} else if (immediateU32(value->Argument(0), scale)) {
key = value->Argument(1)->Resolve();
} else {
return false;
}
return scale == 32u;
}
// A T# whose eight dwords are scalar reads of one V# (the table) at `entryOffset + key * 32`
// with a wave-uniform runtime key: a bindless image table. The key stays an ordinary value
// (the SPIR-V selects the bound slot from it); the eight reads become planning-only.
bool TryMakeTableImage(IrValue& handle, IndirectImagePlan& plan) {
if (handle.Opcode() != IrOpcode::GetImageResource || handle.ArgumentCount() != 8u) {
return false;
}
std::array<IrValue*, 8> heapReads {};
IrValue* heapHandle = nullptr;
IrValue* heapOffset = nullptr;
std::uint32_t immediateOffset = 0;
for (std::uint32_t dword = 0; dword < heapReads.size(); dword++) {
heapReads[dword] = handle.Argument(dword)->Resolve();
std::uint32_t memoryIndex = 0;
const MemoryInfo* memory = ScalarReadMemory(*heapReads[dword], memoryIndex);
if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *heapReads[dword])) {
return false;
}
if (dword == 0u) {
immediateOffset = memory->offset;
} else if (memory->offset != immediateOffset + dword * sizeof(std::uint32_t)) {
return false;
}
IrValue* currentHandle = heapReads[dword]->Argument(0)->Resolve();
if (heapHandle != nullptr && currentHandle != heapHandle) {
return false;
}
heapHandle = currentHandle;
if (dword == 0u) {
heapOffset = heapReads[dword]->Argument(1)->Resolve();
} else if (!EquivalentValue(m_program.Resources(), heapOffset, heapReads[dword]->Argument(1))) {
return false;
}
plan.memory[dword] = memoryIndex;
plan.reads[dword] = heapReads[dword];
}
IrValue* key = nullptr;
std::uint32_t entryOffset = 0;
if (!MatchTableOffset(heapOffset, key, entryOffset) || key->Type() != IrType::U32) {
return false;
}
entryOffset += immediateOffset;
const std::array<const IrValue*, 1> imageUsers {&handle};
for (const auto* read : heapReads) {
if (!usesOnly(*read, imageUsers)) {
return false;
}
}
DescriptorSource heapSource;
std::uint32_t heapSourceIndex = 0;
if (!MakeRuntimeBufferSource(*heapHandle, heapSourceIndex, heapSource)) {
return false;
}
DescriptorSource::IndirectImage table;
table.heapSource = heapSourceIndex;
table.materialSource = heapSourceIndex;
table.entryOffset = entryOffset;
DescriptorSource materialSource = heapSource;
std::uint32_t materialMemoryIndex = 0;
const MemoryInfo* materialMemory = ScalarReadMemory(*key, materialMemoryIndex);
if (materialMemory != nullptr && MemoryIndexBelongsTo(materialMemoryIndex, *key)) {
IrValue* selector = nullptr;
std::uint32_t selectorStride = 0;
std::uint32_t selectorOffset = 0;
DescriptorSource candidateSource;
std::uint32_t candidateIndex = 0;
if (MatchMaterialOffset(key->Argument(1), selector, selectorStride, selectorOffset) && MakeRuntimeBufferSource(*key->Argument(0)->Resolve(), candidateIndex, candidateSource)) {
table.hasMaterial = true;
table.materialSource = candidateIndex;
table.selectorStride = selectorStride;
table.selectorOffset = selectorOffset + materialMemory->offset;
materialSource = candidateSource;
}
}
DescriptorSource imageSource;
imageSource.dwordCount = 8u;
std::copy(heapSource.dwords.begin(), heapSource.dwords.begin() + 4u, imageSource.dwords.begin());
std::copy(materialSource.dwords.begin(), materialSource.dwords.begin() + 4u, imageSource.dwords.begin() + 4u);
imageSource.indirectImage = table;
plan.handle = &handle;
plan.source = InternSource(imageSource);
plan.key = key;
plan.roots = imageSource.dwords;
return true;
}
const IndirectImagePlan* FindIndirectImage(const IrValue& handle) const {
const auto found = std::ranges::find_if(m_indirectImages, [&](const IndirectImagePlan& plan) {
return plan.handle == &handle;
});
return found == m_indirectImages.end() ? nullptr : &*found;
}
bool IsIndirectPlanningMemory(std::uint32_t index) const {
return std::ranges::any_of(m_indirectImages, [&](const IndirectImagePlan& plan) {
return std::ranges::find(plan.memory, index) != plan.memory.end();
});
}
static constexpr std::uint32_t phiSearchDepth = 8u;
static bool SplittableImageRead(const IrValue& inst) {
switch (inst.Opcode()) {
case IrOpcode::ImageSampleRaw:
case IrOpcode::ImageGatherRaw:
case IrOpcode::ImageQueryLod:
case IrOpcode::ImageQueryDimensions:
case IrOpcode::ImageRead: return inst.Type() == IrType::U32x4;
default: return false;
}
}
void CollectDescriptorPhis(IrValue* value, std::vector<IrValue*>& phis, std::uint32_t depth) const {
value = value->Resolve();
if (value->HasImmediate()) {
return;
}
if (value->IsPhi()) {
if (ResolveInvariantPhi(m_program.Resources(), value) == nullptr && std::ranges::find(phis, value) == phis.end()) {
phis.push_back(value);
}
return;
}
if (depth == 0u || !Detail::IsRuntimeUniformOp(value->Opcode())) {
return;
}
for (std::size_t index = 0; index < value->ArgumentCount(); index++) {
CollectDescriptorPhis(value->Argument(index), phis, depth - 1u);
}
}
static IrValue* PhiIncoming(const IrValue& phi, const IrBlock* predecessor) {
for (std::size_t index = 0; index < phi.ArgumentCount(); index++) {
if (phi.PhiBlock(index) == predecessor) {
return phi.Argument(index);
}
}
return nullptr;
}
IrValue* EmitBefore(IrValue& position, IrOpcode op, IrType type, std::span<IrValue* const> arguments, std::uint64_t flags = 0) {
IrValue& value = m_program.CreateValue(op, type, flags);
for (IrValue* argument : arguments) {
value.AddArgument(argument);
}
position.Parent()->InsertInstructionBefore(&position, &value);
return &value;
}
IrValue* EmitCopy(const IrValue& original, std::span<IrValue* const> arguments, IrValue& position) {
return EmitBefore(position, original.Opcode(), original.Type(), arguments, original.Flags<std::uint64_t>());
}
IrValue* Rematerialize(IrValue* value, IrValue& position, bool emit, std::uint32_t depth) {
value = value->Resolve();
if (value->HasImmediate()) {
return value;
}
if (value->IsPhi()) {
IrValue* invariant = ResolveInvariantPhi(m_program.Resources(), value);
return invariant == nullptr || invariant->IsPhi() ? nullptr : Rematerialize(invariant, position, emit, depth);
}
if (depth == 0u) {
return nullptr;
}
const auto op = value->Opcode();
const bool register_ = op == IrOpcode::GetUserData;
const bool argumentless = (op == IrOpcode::GetShaderBase || op == IrOpcode::GetSrtResource) && value->ArgumentCount() == 0u;
const bool srtRead = op == IrOpcode::ReadConst && value->ArgumentCount() == 2u && value->Argument(0)->Resolve()->Opcode() == IrOpcode::GetSrtResource && value->Argument(1)->Resolve()->HasImmediate();
if (!register_ && !argumentless && !srtRead && !Detail::IsRuntimeUniformOp(op)) {
return nullptr;
}
std::vector<IrValue*> arguments;
for (std::size_t index = 0; index < value->ArgumentCount(); index++) {
IrValue* argument = register_ ? value->Argument(index) : Rematerialize(value->Argument(index), position, emit, depth - 1u);
if (argument == nullptr) {
return nullptr;
}
arguments.push_back(argument);
}
return emit ? EmitCopy(*value, arguments, position) : value;
}
IrValue* SubstituteEdge(IrValue* value, const IrBlock* predecessor, IrValue& position, bool emit, std::uint32_t depth) {
value = value->Resolve();
std::vector<IrValue*> phis;
CollectDescriptorPhis(value, phis, depth);
if (phis.empty()) {
return value;
}
if (value->IsPhi()) {
IrValue* incoming = PhiIncoming(*value, predecessor);
return incoming == nullptr ? nullptr : Rematerialize(incoming, position, emit, phiSearchDepth);
}
std::vector<IrValue*> arguments;
for (std::size_t index = 0; index < value->ArgumentCount(); index++) {
IrValue* argument = SubstituteEdge(value->Argument(index), predecessor, position, emit, depth - 1u);
if (argument == nullptr) {
return nullptr;
}
arguments.push_back(argument);
}
return emit ? EmitCopy(*value, arguments, position) : value;
}
IrValue& EdgeSelector(IrBlock& block, const std::vector<bool>& edges) {
for (const auto& selector : m_edgeSelectors) {
if (selector.block == &block && selector.edges == edges) {
return *selector.value;
}
}
IrValue& phi = m_program.CreateValue(IrOpcode::Phi, IrType::Bool);
const auto& predecessors = block.Predecessors();
for (std::size_t index = 0; index < predecessors.size(); index++) {
phi.AddPhiOperand(predecessors[index], &m_builder.ConstantBool(edges[index]));
}
block.InsertInstructionBefore(nullptr, &phi);
m_edgeSelectors.push_back({&block, edges, &phi});
return phi;
}
void SplitDescriptorPhi(IrValue& inst) {
const auto info = ImageOpcodeInfoOf(inst.Opcode());
const std::uint32_t handleCount = info.needsSampler ? 2u : 1u;
if (inst.ArgumentCount() < handleCount) {
return;
}
std::array<IrValue*, 2> handles {};
std::array<bool, 2> split {};
std::vector<IrValue*> phis;
for (std::uint32_t slot = 0; slot < handleCount; slot++) {
IrValue* handle = inst.Argument(slot)->Resolve();
const auto expected = slot == 0u ? IrOpcode::GetImageResource : IrOpcode::GetSamplerResource;
if (handle->Opcode() != expected || handle->ArgumentCount() != (slot == 0u ? 8u : 4u)) {
return;
}
handles[slot] = handle;
std::vector<IrValue*> handlePhis;
for (std::size_t dword = 0; dword < handle->ArgumentCount(); dword++) {
CollectDescriptorPhis(handle->Argument(dword), handlePhis, phiSearchDepth);
}
split[slot] = !handlePhis.empty();
for (IrValue* phi : handlePhis) {
if (std::ranges::find(phis, phi) == phis.end()) {
phis.push_back(phi);
}
}
}
if (phis.empty()) {
return;
}
IrBlock* block = phis.front()->Parent();
if (block == nullptr || inst.Parent() == nullptr) {
return;
}
const auto& predecessors = block->Predecessors();
if (predecessors.size() < 2u) {
return;
}
for (const IrValue* phi : phis) {
if (phi->Parent() != block || phi->PhiBlockCount() != predecessors.size()) {
return;
}
for (const IrBlock* predecessor : predecessors) {
if (PhiIncoming(*phi, predecessor) == nullptr) {
return;
}
}
}
std::vector<std::uint32_t> edgeArm(predecessors.size());
std::vector<const IrBlock*> arms;
for (std::size_t edge = 0; edge < predecessors.size(); edge++) {
std::uint32_t arm = 0;
for (; arm < arms.size(); arm++) {
const bool same = std::ranges::all_of(phis, [&](const IrValue* phi) {
return EquivalentValue(m_program.Resources(), PhiIncoming(*phi, predecessors[edge]), PhiIncoming(*phi, arms[arm]));
});
if (same) {
break;
}
}
if (arm == arms.size()) {
arms.push_back(predecessors[edge]);
}
edgeArm[edge] = arm;
}
for (const IrBlock* arm : arms) {
for (std::uint32_t slot = 0; slot < handleCount; slot++) {
for (std::size_t dword = 0; split[slot] && dword < handles[slot]->ArgumentCount(); dword++) {
if (SubstituteEdge(handles[slot]->Argument(dword), arm, inst, false, phiSearchDepth) == nullptr) {
return;
}
}
}
}
std::vector<IrValue*> copies;
const auto flags = inst.Flags<MemoryFlags>();
const auto memory = m_program.Resources().memoryInfo.at(flags.index);
for (std::uint32_t arm = 0; arm < arms.size(); arm++) {
std::vector<IrValue*> arguments(inst.Arguments().begin(), inst.Arguments().end());
for (std::uint32_t slot = 0; slot < handleCount; slot++) {
if (!split[slot]) {
continue;
}
std::vector<IrValue*> dwords;
for (std::size_t dword = 0; dword < handles[slot]->ArgumentCount(); dword++) {
dwords.push_back(SubstituteEdge(handles[slot]->Argument(dword), arms[arm], inst, true, phiSearchDepth));
}
arguments[slot] = EmitCopy(*handles[slot], dwords, inst);
}
auto copyFlags = flags;
if (arm != 0u) {
copyFlags.index = static_cast<std::uint32_t>(m_program.Resources().memoryInfo.size());
m_program.Resources().memoryInfo.push_back(memory);
}
std::uint64_t rawFlags = 0;
std::memcpy(&rawFlags, ©Flags, sizeof(copyFlags));
copies.push_back(EmitBefore(inst, inst.Opcode(), inst.Type(), arguments, rawFlags));
}
IrValue* result = copies.back();
if (copies.size() > 1u) {
std::array<IrValue*, 4> components {};
for (std::uint32_t component = 0; component < components.size(); component++) {
const auto extract = [&](IrValue* vector) {
const std::array<IrValue*, 2> arguments {vector, &m_builder.Constant(component)};
return EmitBefore(inst, IrOpcode::CompositeExtractU32x4, IrType::U32, arguments);
};
IrValue* selected = extract(copies.back());
for (std::uint32_t arm = static_cast<std::uint32_t>(copies.size()) - 1u; arm-- > 0u;) {
std::vector<bool> edges(edgeArm.size());
for (std::size_t edge = 0; edge < edgeArm.size(); edge++) {
edges[edge] = edgeArm[edge] == arm;
}
const std::array<IrValue*, 3> arguments {&EdgeSelector(*block, edges), extract(copies[arm]), selected};
selected = EmitBefore(inst, IrOpcode::SelectU32, IrType::U32, arguments);
}
components[component] = selected;
}
result = EmitBefore(inst, IrOpcode::CompositeConstructU32x4, IrType::U32x4, components);
}
inst.ReplaceAllUsesWith(result);
inst.Invalidate();
inst.Parent()->RemoveInstruction(&inst);
}
void SplitDescriptorPhis() {
std::vector<IrValue*> candidates;
for (auto& block : m_program.Blocks()) {
for (IrValue* inst : block->Instructions()) {
if (SplittableImageRead(*inst)) {
candidates.push_back(inst);
}
}
}
for (IrValue* inst : candidates) {
SplitDescriptorPhi(*inst);
}
}
void PlanIndirectImages() {
// Kill switch: APS5_NO_BINDLESS_IMAGES=1 leaves table-loaded T#s unplanned (GetHandle then
// rejects them as before).
static const bool enabled = std::getenv("APS5_NO_BINDLESS_IMAGES") == nullptr;
if (!enabled) {
return;
}
for (auto& block : m_program.Blocks()) {
for (IrValue* inst : block->Instructions()) {
const auto imageInfo = ImageOpcodeInfoOf(inst->Opcode());
if (imageInfo.access == ImageAccess::None || inst->ArgumentCount() == 0u) {
continue;
}
IrValue* handle = inst->Argument(0)->Resolve();
const IndirectImagePlan* planned = FindIndirectImage(*handle);
IndirectImagePlan plan;
if (planned == nullptr && !TryMakeTableImage(*handle, plan)) {
continue;
}
// A store through a table would mark every bound slot pending write-back.
if (imageInfo.resourceClass == ImageResourceClass::Storage) {
ResourceMaterializer::CountBindlessRejection(BindlessRejection::Storage);
fail("bindless storage image tables are unsupported");
}
if (planned == nullptr) {
m_indirectImages.push_back(std::move(plan));
}
}
}
}
void GetHandle(IrValue* value, IrOpcode expected, std::uint32_t width, IrValue*& handle, std::uint32_t& source, bool sampler = false, bool sampleAdjust = false) {
handle = value->Resolve();
if (handle->Opcode() != expected) {
fail("memory operation requires " + std::string(IrOpcodeName(expected)));
}
DescriptorSource descriptor;
MakeSource(*handle, width, sampler, sampleAdjust, descriptor);
std::uint32_t badDword = 0;
if (expected == IrOpcode::GetImageResource) {
for (; badDword < descriptor.dwordCount; badDword++) {
const IrValue* value2 = descriptor.dwords[badDword]->Resolve();
if (value2->Opcode() == IrOpcode::ReadConstBuffer) {
fail(std::string(IrOpcodeName(expected)) + " dword " + std::to_string(badDword) + " is not a valid runtime value; chain: " + describeValueChain(descriptor.dwords[badDword], 8u));
}
}
badDword = 0;
}
if (!ValidateSource(descriptor, badDword)) {
fail(std::string(IrOpcodeName(expected)) + " dword " + std::to_string(badDword) + " is not a valid runtime value; chain: " + describeValueChain(descriptor.dwords[badDword], 8u));
}
source = InternSource(descriptor);
}
bool TakeGpuDescriptor(IrValue& inst, std::uint32_t memoryIndex) {
const IrValue* handle = inst.Argument(0)->Resolve();
if (handle->Opcode() != IrOpcode::GetBufferResource || handle->ArgumentCount() != 4u) {
return false;
}
DescriptorSource descriptor;
MakeSource(*handle, 4u, false, false, descriptor);
std::uint32_t badDword = 0;
auto& memory = m_program.Resources().memoryInfo[memoryIndex];
const auto access = BufferAccessOf(inst.Opcode());
if (ValidateSource(descriptor, badDword)) {
const auto source = InternSource(descriptor);
const auto resource = AddBuffer(source, memory, inst.Opcode(), inst.Flags<MemoryFlags>().pc);
if (resource == std::numeric_limits<std::uint32_t>::max()) fail("buffer resource limit exceeded");
AddMemoryPatch(memoryIndex, resource, 0u, false);
memory.gpuDescriptor = false;
m_info.usesDma = m_info.usesDma || access == BufferAccess::Atomic;
return true;
}
memory.gpuDescriptor = true;
m_info.usesDma = true;
m_info.bdaWrites = m_info.bdaWrites || access == BufferAccess::Write || access == BufferAccess::Atomic;
return true;
}
void ValidateAddressHandle(IrValue* value) const {
const IrValue* handle = value->Resolve();
if (handle->Opcode() != IrOpcode::GetAddressResource) {
fail("address operation requires GetAddressResource");
}
if (handle->ArgumentCount() != 2) {
fail("GetAddressResource must have two address dwords");
}
}
std::uint32_t AddBuffer(std::uint32_t source, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) {
for (std::uint32_t i = 0; i < m_info.buffers.size(); i++) {
if (m_info.buffers[i].source == source) {
Merge(m_info.buffers[i], memory, op, pc);
return i;
}
}
BufferResource resource;
resource.source = source;
resource.firstUsePc = pc;
Merge(resource, memory, op, pc);
m_info.buffers.push_back(resource);
return static_cast<std::uint32_t>(m_info.buffers.size() - 1);
}
static void Merge(BufferResource& resource, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) {
const auto access = BufferAccessOf(op);
const bool atomic = access == BufferAccess::Atomic;
const bool write = access == BufferAccess::Write || atomic;
resource.firstUsePc = std::min(resource.firstUsePc, pc);
resource.maxByteExtent = std::max(resource.maxByteExtent, byteExtent(memory));
resource.read = resource.read || !write || atomic;
resource.written = resource.written || write;
resource.atomic = resource.atomic || atomic;
resource.formatted = resource.formatted || memory.formatted;
resource.descriptorFormatted = resource.descriptorFormatted || (memory.formatted && !memory.typed);
if (memory.formatted && memory.typed) {
const auto format = GetFormatInfo(DecodeTBufferFormat(memory.dataFormat, memory.numberFormat));
if (format.byteSize == 0u) fail("typed buffer instruction has an invalid format");
resource.typedAlignment = std::max(resource.typedAlignment, static_cast<std::uint8_t>(std::min(format.byteSize, 4u)));
}
if (memory.formatted && !memory.typed && !write) resource.formattedReadMask |= (1u << std::min(memory.dataDwords, 4u)) - 1u;
resource.scalar = resource.scalar || op == IrOpcode::ReadConstBuffer || memory.kind == ResourceKind::ScalarBuffer;
}
std::uint32_t AddImage(std::uint32_t source, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) {
const auto resourceClass = ImageOpcodeInfoOf(op).resourceClass;
const auto mip = resourceClass == ImageResourceClass::Storage && memory.imageHasMip ? ImageMipMode::DynamicStorage : ImageMipMode::None;
const bool depth = (memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0;
for (std::uint32_t i = 0; i < m_info.images.size(); i++) {
auto& image = m_info.images[i];
if (image.source == source && image.resourceClass == resourceClass && image.dimension == memory.imageDimension && image.mipMode == mip && image.depthCompare == depth && image.r128 == memory.imageR128 && image.packed == memory.imagePacked) {
Merge(image, memory, op, pc);
return i;
}
}
if (m_info.images.size() >= ShaderInfo::MaxImages) {
return std::numeric_limits<std::uint32_t>::max();
}
ImageResource image;
image.source = source;
image.firstUsePc = pc;
image.resourceClass = resourceClass;
image.dimension = memory.imageDimension;
image.mipMode = mip;
image.depthCompare = depth;
image.r128 = memory.imageR128;
image.packed = memory.imagePacked;
Merge(image, memory, op, pc);
m_info.images.push_back(image);
return static_cast<std::uint32_t>(m_info.images.size() - 1);
}
static void Merge(ImageResource& image, const MemoryInfo& memory, IrOpcode op, std::uint32_t pc) {
const auto access = ImageOpcodeInfoOf(op).access;
const bool atomic = access == ImageAccess::Atomic;
const bool write = access == ImageAccess::Write || atomic;
const bool atomic64 = IsImageAtomic64Opcode(op);
if ((image.read || image.written) && image.atomic64 != atomic64) {
throw std::runtime_error("an image accessed by 64-bit atomics is also accessed in another way");
}
image.atomic64 = atomic64;
image.firstUsePc = std::min(image.firstUsePc, pc);
image.read = image.read || !write || atomic;
image.written = image.written || write;
image.atomic = image.atomic || atomic;
image.srgbDecodeCompatible = image.srgbDecodeCompatible && !ImageOpcodeInfoOf(op).needsSampler;
image.fmaskCompatible = image.fmaskCompatible && op == IrOpcode::ImageRead && memory.dataBits == 32u;
image.depthBitsCompatible = image.depthBitsCompatible && memory.dataBits == 32u;
if ((memory.imageSampleFlags & RdnaImageSampleFlagCompare) != 0u) {
constexpr auto unsupported = RdnaImageSampleFlagLod | RdnaImageSampleFlagDerivative;
if (op == IrOpcode::ImageGatherRaw || (memory.imageSampleFlags & unsupported) != 0u) image.emulatedCompare |= EmulatedCompare::Unsupported;
if ((memory.imageSampleFlags & RdnaImageSampleFlagLevelZero) == 0u) image.emulatedCompare |= EmulatedCompare::RequiresSingleLevel;
}
}
std::uint32_t AddSampler(std::uint32_t source, std::uint32_t pc) {
for (std::uint32_t i = 0; i < m_info.samplers.size(); i++) {
if (m_info.samplers[i].source == source) {
m_info.samplers[i].firstUsePc = std::min(m_info.samplers[i].firstUsePc, pc);
return i;
}
}
if (m_info.samplers.size() >= ShaderInfo::MaxSamplers) {
return std::numeric_limits<std::uint32_t>::max();
}
m_info.samplers.push_back({source, pc});
return static_cast<std::uint32_t>(m_info.samplers.size() - 1);
}
void AddSampledPair(std::uint32_t image, std::uint32_t sampler, std::uint32_t pc) {
for (auto& pair : m_info.sampledPairs) {
if (pair.image == image && pair.sampler == sampler) {
pair.firstUsePc = std::min(pair.firstUsePc, pc);
return;
}
}
if (m_info.sampledPairs.size() >= ShaderInfo::MaxSampledPairs) {
fail("sampled image/sampler pair limit exceeded");
}
m_info.sampledPairs.push_back({image, sampler, pc});
}
void AddHandlePatch(IrValue* handle, std::uint32_t resource) {
for (const auto& patch : m_handlePatches) {
if (patch.handle == handle) {
if (patch.resource != resource) {
fail(std::string(IrOpcodeName(handle->Opcode())) + " is reused with incompatible resource classes");
}
return;
}
}
m_handlePatches.push_back({handle, resource});
}
void AddMemoryPatch(std::uint32_t index, std::uint32_t resource, std::uint32_t sampler, bool hasSampler) {
for (auto& patch : m_memoryPatches) {
if (patch.index != index) {
continue;
}
if (patch.resource != resource || (hasSampler && patch.hasSampler && patch.sampler != sampler)) {
fail("memory metadata is reused with incompatible resources");
}
if (hasSampler) {
patch.sampler = sampler;
patch.hasSampler = true;
}
return;
}
m_memoryPatches.push_back({index, resource, sampler, hasSampler});
}
void Collect(IrValue& inst) {
const auto op = inst.Opcode();
if (op == IrOpcode::ImageBvhIntersectRay) {
m_info.usesDma = true;
return;
}
const auto buffer = BufferAccessOf(op);
const auto addressInfo = AddressOpcodeInfoOf(op);
const auto imageInfo = ImageOpcodeInfoOf(op);
if (buffer == BufferAccess::None && addressInfo.access == AddressAccess::None && imageInfo.access == ImageAccess::None) {
return;
}
const auto flags = inst.Flags<MemoryFlags>();
if (flags.index >= m_program.Resources().memoryInfo.size()) {
fail("memory metadata index " + std::to_string(flags.index) + " is out of range");
}
if (inst.ArgumentCount() == 0) {
fail("memory operation has no resource handle");
}
const auto& memory = m_program.Resources().memoryInfo[flags.index];
if (memory.planningOnly || IsIndirectPlanningMemory(flags.index)) {
return;
}
IrValue* handle = nullptr;
std::uint32_t source = 0;
std::uint32_t resource = 0;
if (buffer != BufferAccess::None) {
if (!TakeGpuDescriptor(inst, flags.index)) fail("buffer operation requires a four-dword runtime V#");
return;