-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathDxilModule.cpp
2364 lines (2092 loc) · 83.7 KB
/
DxilModule.cpp
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
///////////////////////////////////////////////////////////////////////////////
// //
// DxilModule.cpp //
// Copyright (C) Microsoft Corporation. All rights reserved. //
// This file is distributed under the University of Illinois Open Source //
// License. See LICENSE.TXT for details. //
// //
///////////////////////////////////////////////////////////////////////////////
#include "dxc/DXIL/DxilModule.h"
#include "dxc/DXIL/DxilConstants.h"
#include "dxc/DXIL/DxilCounters.h"
#include "dxc/DXIL/DxilEntryProps.h"
#include "dxc/DXIL/DxilFunctionProps.h"
#include "dxc/DXIL/DxilInstructions.h"
#include "dxc/DXIL/DxilOperations.h"
#include "dxc/DXIL/DxilShaderModel.h"
#include "dxc/DXIL/DxilSignatureElement.h"
#include "dxc/DXIL/DxilSubobject.h"
#include "dxc/Support/Global.h"
#include "dxc/WinAdapter.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/raw_ostream.h"
#include <unordered_set>
using std::make_unique;
using namespace llvm;
using std::string;
using std::unique_ptr;
using std::vector;
namespace {
class DxilErrorDiagnosticInfo : public DiagnosticInfo {
private:
const char *m_message;
public:
DxilErrorDiagnosticInfo(const char *str)
: DiagnosticInfo(DK_FirstPluginKind, DiagnosticSeverity::DS_Error),
m_message(str) {}
void print(DiagnosticPrinter &DP) const override { DP << m_message; }
};
} // namespace
namespace hlsl {
namespace DXIL {
// Define constant variables exposed in DxilConstants.h
// TODO: revisit data layout descriptions for the following:
// - x64 pointers?
// - Keep elf manging(m:e)?
// For legacy data layout, everything less than 32 align to 32.
const char *kLegacyLayoutString = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:"
"64-f16:32-f32:32-f64:64-n8:16:32:64";
// New data layout with native low precision types
const char *kNewLayoutString = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-"
"f16:16-f32:32-f64:64-n8:16:32:64";
// Function Attributes
// TODO: consider generating attributes from hctdb
const char *kFP32DenormKindString = "fp32-denorm-mode";
const char *kFP32DenormValueAnyString = "any";
const char *kFP32DenormValuePreserveString = "preserve";
const char *kFP32DenormValueFtzString = "ftz";
const char *kDxBreakFuncName = "dx.break";
const char *kDxBreakCondName = "dx.break.cond";
const char *kDxBreakMDName = "dx.break.br";
const char *kDxIsHelperGlobalName = "dx.ishelper";
const char *kHostLayoutTypePrefix = "hostlayout.";
const char *kWaveOpsIncludeHelperLanesString = "waveops-include-helper-lanes";
} // namespace DXIL
void SetDxilHook(Module &M);
void ClearDxilHook(Module &M);
//------------------------------------------------------------------------------
//
// DxilModule methods.
//
DxilModule::DxilModule(Module *pModule)
: m_Ctx(pModule->getContext()), m_pModule(pModule),
m_pMDHelper(make_unique<DxilMDHelper>(
pModule, make_unique<DxilExtraPropertyHelper>(pModule))),
m_pOP(make_unique<OP>(pModule->getContext(), pModule)),
m_pTypeSystem(make_unique<DxilTypeSystem>(pModule)) {
DXASSERT_NOMSG(m_pModule != nullptr);
SetDxilHook(*m_pModule);
}
DxilModule::~DxilModule() { ClearDxilHook(*m_pModule); }
LLVMContext &DxilModule::GetCtx() const { return m_Ctx; }
Module *DxilModule::GetModule() const { return m_pModule; }
OP *DxilModule::GetOP() const { return m_pOP.get(); }
void DxilModule::SetShaderModel(const ShaderModel *pSM, bool bUseMinPrecision) {
DXASSERT(m_pSM == nullptr || (pSM != nullptr && *m_pSM == *pSM),
"shader model must not change for the module");
DXASSERT(pSM != nullptr && pSM->IsValidForDxil(),
"shader model must be valid");
m_pSM = pSM;
m_pSM->GetDxilVersion(m_DxilMajor, m_DxilMinor);
m_pMDHelper->SetShaderModel(m_pSM);
m_bUseMinPrecision = bUseMinPrecision;
m_pOP->InitWithMinPrecision(m_bUseMinPrecision);
m_pTypeSystem->SetMinPrecision(m_bUseMinPrecision);
if (!m_pSM->IsLib()) {
// Always have valid entry props for non-lib case from this point on.
DxilFunctionProps props;
props.shaderKind = m_pSM->GetKind();
m_DxilEntryPropsMap[nullptr] =
make_unique<DxilEntryProps>(props, m_bUseMinPrecision);
}
m_SerializedRootSignature.clear();
}
const ShaderModel *DxilModule::GetShaderModel() const { return m_pSM; }
void DxilModule::GetDxilVersion(unsigned &DxilMajor,
unsigned &DxilMinor) const {
DxilMajor = m_DxilMajor;
DxilMinor = m_DxilMinor;
}
void DxilModule::SetValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
m_ValMajor = ValMajor;
m_ValMinor = ValMinor;
}
void DxilModule::SetForceZeroStoreLifetimes(bool ForceZeroStoreLifetimes) {
m_ForceZeroStoreLifetimes = ForceZeroStoreLifetimes;
}
bool DxilModule::UpgradeValidatorVersion(unsigned ValMajor, unsigned ValMinor) {
// Don't upgrade if validation was disabled.
if (m_ValMajor == 0 && m_ValMinor == 0) {
return false;
}
if (ValMajor > m_ValMajor ||
(ValMajor == m_ValMajor && ValMinor > m_ValMinor)) {
// Module requires higher validator version than previously set
SetValidatorVersion(ValMajor, ValMinor);
return true;
}
return false;
}
void DxilModule::GetValidatorVersion(unsigned &ValMajor,
unsigned &ValMinor) const {
ValMajor = m_ValMajor;
ValMinor = m_ValMinor;
}
bool DxilModule::GetForceZeroStoreLifetimes() const {
return m_ForceZeroStoreLifetimes;
}
bool DxilModule::GetMinValidatorVersion(unsigned &ValMajor,
unsigned &ValMinor) const {
if (!m_pSM)
return false;
m_pSM->GetMinValidatorVersion(ValMajor, ValMinor);
if (DXIL::CompareVersions(ValMajor, ValMinor, 1, 5) < 0 &&
m_ShaderFlags.GetRaytracingTier1_1())
ValMinor = 5;
else if (DXIL::CompareVersions(ValMajor, ValMinor, 1, 4) < 0 &&
GetSubobjects() && !GetSubobjects()->GetSubobjects().empty())
ValMinor = 4;
else if (DXIL::CompareVersions(ValMajor, ValMinor, 1, 1) < 0 &&
(m_ShaderFlags.GetFeatureInfo() &
hlsl::DXIL::ShaderFeatureInfo_ViewID))
ValMinor = 1;
return true;
}
bool DxilModule::UpgradeToMinValidatorVersion() {
unsigned ValMajor = 1, ValMinor = 0;
if (GetMinValidatorVersion(ValMajor, ValMinor)) {
return UpgradeValidatorVersion(ValMajor, ValMinor);
}
return false;
}
Function *DxilModule::GetEntryFunction() { return m_pEntryFunc; }
const Function *DxilModule::GetEntryFunction() const { return m_pEntryFunc; }
llvm::SmallVector<llvm::Function *, 64> DxilModule::GetExportedFunctions() {
llvm::SmallVector<llvm::Function *, 64> ret;
for (auto const &fn : m_DxilEntryPropsMap) {
if (fn.first != nullptr) {
ret.push_back(const_cast<llvm::Function *>(fn.first));
}
}
if (ret.empty()) {
auto *entryFunction = m_pEntryFunc;
if (entryFunction == nullptr) {
entryFunction = GetPatchConstantFunction();
}
ret.push_back(entryFunction);
}
return ret;
}
void DxilModule::SetEntryFunction(Function *pEntryFunc) {
if (m_pSM->IsLib()) {
DXASSERT(pEntryFunc == nullptr,
"Otherwise, trying to set an entry function on library");
m_pEntryFunc = nullptr;
return;
}
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
m_pEntryFunc = pEntryFunc;
// Move entry props to new function in order to preserve them.
std::unique_ptr<DxilEntryProps> Props =
std::move(m_DxilEntryPropsMap.begin()->second);
m_DxilEntryPropsMap.clear();
m_DxilEntryPropsMap[m_pEntryFunc] = std::move(Props);
}
const string &DxilModule::GetEntryFunctionName() const { return m_EntryName; }
void DxilModule::SetEntryFunctionName(const string &name) {
m_EntryName = name;
}
llvm::Function *DxilModule::GetPatchConstantFunction() {
if (!m_pSM->IsHS())
return nullptr;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.patchConstantFunc;
}
const llvm::Function *DxilModule::GetPatchConstantFunction() const {
if (!m_pSM->IsHS())
return nullptr;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.patchConstantFunc;
}
void DxilModule::SetPatchConstantFunction(llvm::Function *patchConstantFunc) {
if (!m_pSM->IsHS())
return;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
auto &HS = props.ShaderProps.HS;
if (HS.patchConstantFunc != patchConstantFunc) {
if (HS.patchConstantFunc)
m_PatchConstantFunctions.erase(HS.patchConstantFunc);
HS.patchConstantFunc = patchConstantFunc;
if (patchConstantFunc)
m_PatchConstantFunctions.insert(patchConstantFunc);
}
}
bool DxilModule::IsEntryOrPatchConstantFunction(
const llvm::Function *pFunc) const {
return pFunc == GetEntryFunction() || pFunc == GetPatchConstantFunction();
}
unsigned DxilModule::GetGlobalFlags() const {
unsigned Flags = m_ShaderFlags.GetGlobalFlags();
return Flags;
}
void DxilModule::CollectShaderFlagsForModule(ShaderFlags &Flags) {
ComputeShaderCompatInfo();
for (auto &itInfo : m_FuncToShaderCompat)
Flags.CombineShaderFlags(itInfo.second.shaderFlags);
const ShaderModel *SM = GetShaderModel();
// Set DerivativesInMeshAndAmpShaders if necessary for MS/AS.
if (Flags.GetUsesDerivatives()) {
if (SM->IsMS() || SM->IsAS())
Flags.SetDerivativesInMeshAndAmpShaders(true);
}
// Clear function-local flags not intended for the module.
Flags.ClearLocalFlags();
unsigned NumUAVs = 0;
const unsigned kSmallUAVCount = 8;
bool hasRawAndStructuredBuffer = false;
for (auto &UAV : m_UAVs) {
unsigned uavSize = UAV->GetRangeSize();
NumUAVs += uavSize > 8U ? 9U : uavSize; // avoid overflow
if (UAV->IsROV())
Flags.SetROVs(true);
switch (UAV->GetKind()) {
case DXIL::ResourceKind::RawBuffer:
case DXIL::ResourceKind::StructuredBuffer:
hasRawAndStructuredBuffer = true;
break;
default:
// Not raw/structured.
break;
}
}
// Maintain earlier erroneous counting of UAVs for compatibility
if (DXIL::CompareVersions(m_ValMajor, m_ValMinor, 1, 6) < 0)
Flags.Set64UAVs(m_UAVs.size() > kSmallUAVCount);
else
Flags.Set64UAVs(NumUAVs > kSmallUAVCount);
if (DXIL::CompareVersions(m_ValMajor, m_ValMinor, 1, 8) < 0) {
// For 1.7 compatibility, set UAVsAtEveryStage if there are UAVs
// and the shader model is not CS or PS.
if (NumUAVs && !(SM->IsCS() || SM->IsPS()))
Flags.SetUAVsAtEveryStage(true);
} else {
// Starting with 1.8, UAVsAtEveryStage is only set when the shader model is
// a graphics stage where it mattered. It was unnecessary to set it for
// library profiles, or MS/AS profiles.
if (NumUAVs && (SM->IsVS() || SM->IsHS() || SM->IsDS() || SM->IsGS()))
Flags.SetUAVsAtEveryStage(true);
}
for (auto &SRV : m_SRVs) {
switch (SRV->GetKind()) {
case DXIL::ResourceKind::RawBuffer:
case DXIL::ResourceKind::StructuredBuffer:
hasRawAndStructuredBuffer = true;
break;
default:
// Not raw/structured.
break;
}
}
Flags.SetEnableRawAndStructuredBuffers(hasRawAndStructuredBuffer);
bool hasCSRawAndStructuredViaShader4X =
hasRawAndStructuredBuffer && m_pSM->GetMajor() == 4 && m_pSM->IsCS();
Flags.SetCSRawAndStructuredViaShader4X(hasCSRawAndStructuredViaShader4X);
}
void DxilModule::CollectShaderFlagsForModule() {
CollectShaderFlagsForModule(m_ShaderFlags);
// This is also where we record the size of the mesh payload for amplification
// shader output
for (Function &F : GetModule()->functions()) {
if (HasDxilEntryProps(&F)) {
DxilFunctionProps &props = GetDxilFunctionProps(&F);
if (props.shaderKind == DXIL::ShaderKind::Amplification) {
if (props.ShaderProps.AS.payloadSizeInBytes != 0)
continue;
for (const BasicBlock &BB : F.getBasicBlockList()) {
for (const Instruction &I : BB.getInstList()) {
const DxilInst_DispatchMesh dispatch(const_cast<Instruction *>(&I));
if (dispatch) {
Type *payloadTy =
dispatch.get_payload()->getType()->getPointerElementType();
const DataLayout &DL = m_pModule->getDataLayout();
props.ShaderProps.AS.payloadSizeInBytes =
DL.getTypeAllocSize(payloadTy);
}
}
}
}
}
}
}
void DxilModule::SetNumThreads(unsigned x, unsigned y, unsigned z) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 &&
(m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()),
"only works for CS/MS/AS profiles");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT_NOMSG(m_pSM->GetKind() == props.shaderKind);
props.numThreads[0] = x;
props.numThreads[1] = y;
props.numThreads[2] = z;
}
unsigned DxilModule::GetNumThreads(unsigned idx) const {
DXASSERT(m_DxilEntryPropsMap.size() == 1 &&
(m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()),
"only works for CS/MS/AS profiles");
DXASSERT(idx < 3, "Thread dimension index must be 0-2");
assert(idx < 3);
if (!(m_pSM->IsCS() || m_pSM->IsMS() || m_pSM->IsAS()))
return 0;
const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT_NOMSG(m_pSM->GetKind() == props.shaderKind);
return props.numThreads[idx];
}
DxilWaveSize &DxilModule::GetWaveSize() {
return const_cast<DxilWaveSize &>(
static_cast<const DxilModule *>(this)->GetWaveSize());
}
const DxilWaveSize &DxilModule::GetWaveSize() const {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsCS(),
"only works for CS profile");
const DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT_NOMSG(m_pSM->GetKind() == props.shaderKind);
return props.WaveSize;
}
DXIL::InputPrimitive DxilModule::GetInputPrimitive() const {
if (!m_pSM->IsGS())
return DXIL::InputPrimitive::Undefined;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
return props.ShaderProps.GS.inputPrimitive;
}
void DxilModule::SetInputPrimitive(DXIL::InputPrimitive IP) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
"only works for GS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
auto &GS = props.ShaderProps.GS;
DXASSERT_NOMSG(DXIL::InputPrimitive::Undefined < IP &&
IP < DXIL::InputPrimitive::LastEntry);
GS.inputPrimitive = IP;
}
unsigned DxilModule::GetMaxVertexCount() const {
if (!m_pSM->IsGS())
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
auto &GS = props.ShaderProps.GS;
DXASSERT_NOMSG(GS.maxVertexCount != 0);
return GS.maxVertexCount;
}
void DxilModule::SetMaxVertexCount(unsigned Count) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
"only works for GS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
auto &GS = props.ShaderProps.GS;
GS.maxVertexCount = Count;
}
DXIL::PrimitiveTopology DxilModule::GetStreamPrimitiveTopology() const {
return m_StreamPrimitiveTopology;
}
void DxilModule::SetStreamPrimitiveTopology(DXIL::PrimitiveTopology Topology) {
m_StreamPrimitiveTopology = Topology;
SetActiveStreamMask(m_ActiveStreamMask); // Update props
}
bool DxilModule::HasMultipleOutputStreams() const {
if (!m_pSM->IsGS()) {
return false;
} else {
unsigned NumStreams =
(m_ActiveStreamMask & 0x1) + ((m_ActiveStreamMask & 0x2) >> 1) +
((m_ActiveStreamMask & 0x4) >> 2) + ((m_ActiveStreamMask & 0x8) >> 3);
DXASSERT_NOMSG(NumStreams <= DXIL::kNumOutputStreams);
return NumStreams > 1;
}
}
unsigned DxilModule::GetOutputStream() const {
if (!m_pSM->IsGS()) {
return 0;
} else {
DXASSERT_NOMSG(!HasMultipleOutputStreams());
switch (m_ActiveStreamMask) {
case 0x1:
return 0;
case 0x2:
return 1;
case 0x4:
return 2;
case 0x8:
return 3;
default:
DXASSERT_NOMSG(false);
}
return (unsigned)(-1);
}
}
unsigned DxilModule::GetGSInstanceCount() const {
if (!m_pSM->IsGS())
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
return props.ShaderProps.GS.instanceCount;
}
void DxilModule::SetGSInstanceCount(unsigned Count) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
"only works for GS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
props.ShaderProps.GS.instanceCount = Count;
}
bool DxilModule::IsStreamActive(unsigned Stream) const {
return (m_ActiveStreamMask & (1 << Stream)) != 0;
}
void DxilModule::SetStreamActive(unsigned Stream, bool bActive) {
if (bActive) {
m_ActiveStreamMask |= (1 << Stream);
} else {
m_ActiveStreamMask &= ~(1 << Stream);
}
SetActiveStreamMask(m_ActiveStreamMask);
}
void DxilModule::SetActiveStreamMask(unsigned Mask) {
m_ActiveStreamMask = Mask;
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsGS(),
"only works for GS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsGS(), "Must be GS profile");
for (unsigned i = 0; i < 4; i++) {
if (IsStreamActive(i))
props.ShaderProps.GS.streamPrimitiveTopologies[i] =
m_StreamPrimitiveTopology;
else
props.ShaderProps.GS.streamPrimitiveTopologies[i] =
DXIL::PrimitiveTopology::Undefined;
}
}
unsigned DxilModule::GetActiveStreamMask() const { return m_ActiveStreamMask; }
bool DxilModule::GetUseMinPrecision() const { return m_bUseMinPrecision; }
void DxilModule::SetDisableOptimization(bool DisableOptimization) {
m_bDisableOptimizations = DisableOptimization;
}
bool DxilModule::GetDisableOptimization() const {
return m_bDisableOptimizations;
}
void DxilModule::SetAllResourcesBound(bool ResourcesBound) {
m_bAllResourcesBound = ResourcesBound;
}
bool DxilModule::GetAllResourcesBound() const { return m_bAllResourcesBound; }
void DxilModule::SetResMayAlias(bool resMayAlias) {
m_bResMayAlias = resMayAlias;
}
bool DxilModule::GetResMayAlias() const { return m_bResMayAlias; }
void DxilModule::SetLegacyResourceReservation(bool legacyResourceReservation) {
m_IntermediateFlags &= ~LegacyResourceReservation;
if (legacyResourceReservation)
m_IntermediateFlags |= LegacyResourceReservation;
}
bool DxilModule::GetLegacyResourceReservation() const {
return (m_IntermediateFlags & LegacyResourceReservation) != 0;
}
void DxilModule::ClearIntermediateOptions() { m_IntermediateFlags = 0; }
unsigned DxilModule::GetInputControlPointCount() const {
if (!(m_pSM->IsHS() || m_pSM->IsDS()))
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
if (props.IsHS())
return props.ShaderProps.HS.inputControlPoints;
else
return props.ShaderProps.DS.inputControlPoints;
}
void DxilModule::SetInputControlPointCount(unsigned NumICPs) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && (m_pSM->IsHS() || m_pSM->IsDS()),
"only works for non-lib profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
if (props.IsHS())
props.ShaderProps.HS.inputControlPoints = NumICPs;
else
props.ShaderProps.DS.inputControlPoints = NumICPs;
}
DXIL::TessellatorDomain DxilModule::GetTessellatorDomain() const {
if (!(m_pSM->IsHS() || m_pSM->IsDS()))
return DXIL::TessellatorDomain::Undefined;
DXASSERT_NOMSG(m_DxilEntryPropsMap.size() == 1);
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
if (props.IsHS())
return props.ShaderProps.HS.domain;
else
return props.ShaderProps.DS.domain;
}
void DxilModule::SetTessellatorDomain(DXIL::TessellatorDomain TessDomain) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && (m_pSM->IsHS() || m_pSM->IsDS()),
"only works for HS or DS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS() || props.IsDS(), "Must be HS or DS profile");
if (props.IsHS())
props.ShaderProps.HS.domain = TessDomain;
else
props.ShaderProps.DS.domain = TessDomain;
}
unsigned DxilModule::GetOutputControlPointCount() const {
if (!m_pSM->IsHS())
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.outputControlPoints;
}
void DxilModule::SetOutputControlPointCount(unsigned NumOCPs) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
"only works for HS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
props.ShaderProps.HS.outputControlPoints = NumOCPs;
}
DXIL::TessellatorPartitioning DxilModule::GetTessellatorPartitioning() const {
if (!m_pSM->IsHS())
return DXIL::TessellatorPartitioning::Undefined;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.partition;
}
void DxilModule::SetTessellatorPartitioning(
DXIL::TessellatorPartitioning TessPartitioning) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
"only works for HS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
props.ShaderProps.HS.partition = TessPartitioning;
}
DXIL::TessellatorOutputPrimitive
DxilModule::GetTessellatorOutputPrimitive() const {
if (!m_pSM->IsHS())
return DXIL::TessellatorOutputPrimitive::Undefined;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.outputPrimitive;
}
void DxilModule::SetTessellatorOutputPrimitive(
DXIL::TessellatorOutputPrimitive TessOutputPrimitive) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
"only works for HS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
props.ShaderProps.HS.outputPrimitive = TessOutputPrimitive;
}
float DxilModule::GetMaxTessellationFactor() const {
if (!m_pSM->IsHS())
return 0.0F;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
return props.ShaderProps.HS.maxTessFactor;
}
void DxilModule::SetMaxTessellationFactor(float MaxTessellationFactor) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsHS(),
"only works for HS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsHS(), "Must be HS profile");
props.ShaderProps.HS.maxTessFactor = MaxTessellationFactor;
}
unsigned DxilModule::GetMaxOutputVertices() const {
if (!m_pSM->IsMS())
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
return props.ShaderProps.MS.maxVertexCount;
}
void DxilModule::SetMaxOutputVertices(unsigned NumOVs) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
"only works for MS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
props.ShaderProps.MS.maxVertexCount = NumOVs;
}
unsigned DxilModule::GetMaxOutputPrimitives() const {
if (!m_pSM->IsMS())
return 0;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
return props.ShaderProps.MS.maxPrimitiveCount;
}
void DxilModule::SetMaxOutputPrimitives(unsigned NumOPs) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
"only works for MS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
props.ShaderProps.MS.maxPrimitiveCount = NumOPs;
}
DXIL::MeshOutputTopology DxilModule::GetMeshOutputTopology() const {
if (!m_pSM->IsMS())
return DXIL::MeshOutputTopology::Undefined;
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
return props.ShaderProps.MS.outputTopology;
}
void DxilModule::SetMeshOutputTopology(
DXIL::MeshOutputTopology MeshOutputTopology) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && m_pSM->IsMS(),
"only works for MS profile");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
props.ShaderProps.MS.outputTopology = MeshOutputTopology;
}
unsigned DxilModule::GetPayloadSizeInBytes() const {
if (m_pSM->IsMS()) {
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
return props.ShaderProps.MS.payloadSizeInBytes;
} else if (m_pSM->IsAS()) {
DXASSERT(m_DxilEntryPropsMap.size() == 1, "should have one entry prop");
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsAS(), "Must be AS profile");
return props.ShaderProps.AS.payloadSizeInBytes;
} else {
return 0;
}
}
void DxilModule::SetPayloadSizeInBytes(unsigned Size) {
DXASSERT(m_DxilEntryPropsMap.size() == 1 && (m_pSM->IsMS() || m_pSM->IsAS()),
"only works for MS or AS profile");
if (m_pSM->IsMS()) {
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsMS(), "Must be MS profile");
props.ShaderProps.MS.payloadSizeInBytes = Size;
} else if (m_pSM->IsAS()) {
DxilFunctionProps &props = m_DxilEntryPropsMap.begin()->second->props;
DXASSERT(props.IsAS(), "Must be AS profile");
props.ShaderProps.AS.payloadSizeInBytes = Size;
}
}
void DxilModule::SetAutoBindingSpace(uint32_t Space) {
m_AutoBindingSpace = Space;
}
uint32_t DxilModule::GetAutoBindingSpace() const { return m_AutoBindingSpace; }
void DxilModule::SetShaderProperties(DxilFunctionProps *props) {
if (!props)
return;
DxilFunctionProps &ourProps = GetDxilFunctionProps(GetEntryFunction());
if (props != &ourProps) {
ourProps.shaderKind = props->shaderKind;
ourProps.ShaderProps = props->ShaderProps;
}
switch (props->shaderKind) {
case DXIL::ShaderKind::Pixel: {
auto &PS = props->ShaderProps.PS;
m_ShaderFlags.SetForceEarlyDepthStencil(PS.EarlyDepthStencil);
} break;
case DXIL::ShaderKind::Compute:
case DXIL::ShaderKind::Domain:
case DXIL::ShaderKind::Hull:
case DXIL::ShaderKind::Vertex:
case DXIL::ShaderKind::Mesh:
case DXIL::ShaderKind::Amplification:
break;
default: {
DXASSERT(props->shaderKind == DXIL::ShaderKind::Geometry,
"else invalid shader kind");
auto &GS = props->ShaderProps.GS;
m_ActiveStreamMask = 0;
for (size_t i = 0; i < _countof(GS.streamPrimitiveTopologies); ++i) {
if (GS.streamPrimitiveTopologies[i] !=
DXIL::PrimitiveTopology::Undefined) {
m_ActiveStreamMask |= (1 << i);
DXASSERT_NOMSG(
m_StreamPrimitiveTopology == DXIL::PrimitiveTopology::Undefined ||
m_StreamPrimitiveTopology == GS.streamPrimitiveTopologies[i]);
m_StreamPrimitiveTopology = GS.streamPrimitiveTopologies[i];
}
}
// Refresh props:
SetActiveStreamMask(m_ActiveStreamMask);
} break;
}
}
template <typename T>
unsigned DxilModule::AddResource(vector<unique_ptr<T>> &Vec,
unique_ptr<T> pRes) {
DXASSERT_NOMSG((unsigned)Vec.size() < UINT_MAX);
unsigned Id = (unsigned)Vec.size();
Vec.emplace_back(std::move(pRes));
return Id;
}
unsigned DxilModule::AddCBuffer(unique_ptr<DxilCBuffer> pCB) {
return AddResource<DxilCBuffer>(m_CBuffers, std::move(pCB));
}
DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) { return *m_CBuffers[idx]; }
const DxilCBuffer &DxilModule::GetCBuffer(unsigned idx) const {
return *m_CBuffers[idx];
}
const vector<unique_ptr<DxilCBuffer>> &DxilModule::GetCBuffers() const {
return m_CBuffers;
}
unsigned DxilModule::AddSampler(unique_ptr<DxilSampler> pSampler) {
return AddResource<DxilSampler>(m_Samplers, std::move(pSampler));
}
DxilSampler &DxilModule::GetSampler(unsigned idx) { return *m_Samplers[idx]; }
const DxilSampler &DxilModule::GetSampler(unsigned idx) const {
return *m_Samplers[idx];
}
const vector<unique_ptr<DxilSampler>> &DxilModule::GetSamplers() const {
return m_Samplers;
}
unsigned DxilModule::AddSRV(unique_ptr<DxilResource> pSRV) {
return AddResource<DxilResource>(m_SRVs, std::move(pSRV));
}
DxilResource &DxilModule::GetSRV(unsigned idx) { return *m_SRVs[idx]; }
const DxilResource &DxilModule::GetSRV(unsigned idx) const {
return *m_SRVs[idx];
}
const vector<unique_ptr<DxilResource>> &DxilModule::GetSRVs() const {
return m_SRVs;
}
unsigned DxilModule::AddUAV(unique_ptr<DxilResource> pUAV) {
return AddResource<DxilResource>(m_UAVs, std::move(pUAV));
}
DxilResource &DxilModule::GetUAV(unsigned idx) { return *m_UAVs[idx]; }
const DxilResource &DxilModule::GetUAV(unsigned idx) const {
return *m_UAVs[idx];
}
const vector<unique_ptr<DxilResource>> &DxilModule::GetUAVs() const {
return m_UAVs;
}
template <typename TResource>
static void RemoveResources(std::vector<std::unique_ptr<TResource>> &vec,
std::unordered_set<unsigned> &immResID) {
for (auto p = vec.begin(); p != vec.end();) {
auto c = p++;
if (immResID.count((*c)->GetID()) == 0) {
p = vec.erase(c);
}
}
}
static void CollectUsedResource(Value *resID,
std::unordered_set<Value *> &usedResID) {
if (usedResID.count(resID) > 0)
return;
usedResID.insert(resID);
if (dyn_cast<ConstantInt>(resID)) {
// Do nothing
} else if (ZExtInst *ZEI = dyn_cast<ZExtInst>(resID)) {
if (ZEI->getSrcTy()->isIntegerTy()) {
IntegerType *ITy = cast<IntegerType>(ZEI->getSrcTy());
if (ITy->getBitWidth() == 1) {
usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 0));
usedResID.insert(ConstantInt::get(ZEI->getDestTy(), 1));
}
}
} else if (SelectInst *SI = dyn_cast<SelectInst>(resID)) {
CollectUsedResource(SI->getTrueValue(), usedResID);
CollectUsedResource(SI->getFalseValue(), usedResID);
} else if (PHINode *Phi = dyn_cast<PHINode>(resID)) {
for (Use &U : Phi->incoming_values()) {
CollectUsedResource(U.get(), usedResID);
}
}
// TODO: resID could be other types of instructions depending on the compiler
// optimization.
}
static void ConvertUsedResource(std::unordered_set<unsigned> &immResID,
std::unordered_set<Value *> &usedResID) {
for (Value *V : usedResID) {
if (ConstantInt *cResID = dyn_cast<ConstantInt>(V)) {
immResID.insert(cResID->getLimitedValue());
}
}
}
void DxilModule::RemoveFunction(llvm::Function *F) {
DXASSERT_NOMSG(F != nullptr);
m_DxilEntryPropsMap.erase(F);
if (m_pTypeSystem.get()->GetFunctionAnnotation(F))
m_pTypeSystem.get()->EraseFunctionAnnotation(F);
m_pOP->RemoveFunction(F);
}
void DxilModule::RemoveUnusedResources() {
DXASSERT(!m_pSM->IsLib(), "this function does not work on libraries");
hlsl::OP *hlslOP = GetOP();
Function *createHandleFunc =
hlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(GetCtx()));
if (createHandleFunc->user_empty()) {
m_CBuffers.clear();
m_UAVs.clear();
m_SRVs.clear();
m_Samplers.clear();
createHandleFunc->eraseFromParent();
return;
}
std::unordered_set<Value *> usedUAVID;
std::unordered_set<Value *> usedSRVID;
std::unordered_set<Value *> usedSamplerID;
std::unordered_set<Value *> usedCBufID;
// Collect used ID.
for (User *U : createHandleFunc->users()) {
CallInst *CI = cast<CallInst>(U);
Value *vResClass =
CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResClassOpIdx);
ConstantInt *cResClass = cast<ConstantInt>(vResClass);
DXIL::ResourceClass resClass =
static_cast<DXIL::ResourceClass>(cResClass->getLimitedValue());
// Skip unused resource handle.
if (CI->user_empty())
continue;
Value *resID =
CI->getArgOperand(DXIL::OperandIndex::kCreateHandleResIDOpIdx);
switch (resClass) {
case DXIL::ResourceClass::CBuffer:
CollectUsedResource(resID, usedCBufID);
break;
case DXIL::ResourceClass::Sampler:
CollectUsedResource(resID, usedSamplerID);
break;
case DXIL::ResourceClass::SRV:
CollectUsedResource(resID, usedSRVID);
break;
case DXIL::ResourceClass::UAV:
CollectUsedResource(resID, usedUAVID);
break;