Skip to content

Commit 38e671a

Browse files
authored
[DXIL][Analysis] Use setters for dxil::ResourceInfo initialization. NFC
This simplifies making sure we set all of the members of the unions and adds asserts to help catch if we do something wrong. Pull Request: llvm#100696
1 parent 842a332 commit 38e671a

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ class ResourceInfo {
106106

107107
MSInfo MultiSample;
108108

109+
public:
110+
ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol,
111+
StringRef Name)
112+
: Symbol(Symbol), Name(Name), RC(RC), Kind(Kind) {}
113+
109114
// Conditions to check before accessing union members.
110115
bool isUAV() const;
111116
bool isCBuffer() const;
@@ -115,11 +120,45 @@ class ResourceInfo {
115120
bool isFeedback() const;
116121
bool isMultiSample() const;
117122

118-
ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol,
119-
StringRef Name)
120-
: Symbol(Symbol), Name(Name), RC(RC), Kind(Kind) {}
123+
void bind(uint32_t UniqueID, uint32_t Space, uint32_t LowerBound,
124+
uint32_t Size) {
125+
Binding.UniqueID = UniqueID;
126+
Binding.Space = Space;
127+
Binding.LowerBound = LowerBound;
128+
Binding.Size = Size;
129+
}
130+
void setUAV(bool GloballyCoherent, bool HasCounter, bool IsROV) {
131+
assert(isUAV() && "Not a UAV");
132+
UAVFlags.GloballyCoherent = GloballyCoherent;
133+
UAVFlags.HasCounter = HasCounter;
134+
UAVFlags.IsROV = IsROV;
135+
}
136+
void setCBuffer(uint32_t Size) {
137+
assert(isCBuffer() && "Not a CBuffer");
138+
CBufferSize = Size;
139+
}
140+
void setSampler(dxil::SamplerType Ty) { SamplerTy = Ty; }
141+
void setStruct(uint32_t Stride, Align Alignment) {
142+
assert(isStruct() && "Not a Struct");
143+
Struct.Stride = Stride;
144+
Struct.Alignment = Alignment;
145+
}
146+
void setTyped(dxil::ElementType ElementTy, uint32_t ElementCount) {
147+
assert(isTyped() && "Not Typed");
148+
Typed.ElementTy = ElementTy;
149+
Typed.ElementCount = ElementCount;
150+
}
151+
void setFeedback(dxil::SamplerFeedbackType Type) {
152+
assert(isFeedback() && "Not Feedback");
153+
Feedback.Type = Type;
154+
}
155+
void setMultiSample(uint32_t Count) {
156+
assert(isMultiSample() && "Not MultiSampled");
157+
MultiSample.Count = Count;
158+
}
159+
160+
bool operator==(const ResourceInfo &RHS) const;
121161

122-
public:
123162
static ResourceInfo SRV(Value *Symbol, StringRef Name,
124163
dxil::ElementType ElementTy, uint32_t ElementCount,
125164
dxil::ResourceKind Kind);
@@ -164,16 +203,6 @@ class ResourceInfo {
164203
static ResourceInfo Sampler(Value *Symbol, StringRef Name,
165204
dxil::SamplerType SamplerTy);
166205

167-
void bind(uint32_t UniqueID, uint32_t Space, uint32_t LowerBound,
168-
uint32_t Size) {
169-
Binding.UniqueID = UniqueID;
170-
Binding.Space = Space;
171-
Binding.LowerBound = LowerBound;
172-
Binding.Size = Size;
173-
}
174-
175-
bool operator==(const ResourceInfo &RHS) const;
176-
177206
MDTuple *getAsMetadata(LLVMContext &Ctx) const;
178207

179208
ResourceBinding getBinding() const { return Binding; }

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ ResourceInfo ResourceInfo::SRV(Value *Symbol, StringRef Name,
6969
ResourceInfo RI(ResourceClass::SRV, Kind, Symbol, Name);
7070
assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) &&
7171
"Invalid ResourceKind for SRV constructor.");
72-
RI.Typed.ElementTy = ElementTy;
73-
RI.Typed.ElementCount = ElementCount;
72+
RI.setTyped(ElementTy, ElementCount);
7473
return RI;
7574
}
7675

@@ -83,8 +82,7 @@ ResourceInfo ResourceInfo::StructuredBuffer(Value *Symbol, StringRef Name,
8382
uint32_t Stride, Align Alignment) {
8483
ResourceInfo RI(ResourceClass::SRV, ResourceKind::StructuredBuffer, Symbol,
8584
Name);
86-
RI.Struct.Stride = Stride;
87-
RI.Struct.Alignment = Alignment;
85+
RI.setStruct(Stride, Alignment);
8886
return RI;
8987
}
9088

@@ -93,9 +91,8 @@ ResourceInfo ResourceInfo::Texture2DMS(Value *Symbol, StringRef Name,
9391
uint32_t ElementCount,
9492
uint32_t SampleCount) {
9593
ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMS, Symbol, Name);
96-
RI.Typed.ElementTy = ElementTy;
97-
RI.Typed.ElementCount = ElementCount;
98-
RI.MultiSample.Count = SampleCount;
94+
RI.setTyped(ElementTy, ElementCount);
95+
RI.setMultiSample(SampleCount);
9996
return RI;
10097
}
10198

@@ -105,9 +102,8 @@ ResourceInfo ResourceInfo::Texture2DMSArray(Value *Symbol, StringRef Name,
105102
uint32_t SampleCount) {
106103
ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMSArray, Symbol,
107104
Name);
108-
RI.Typed.ElementTy = ElementTy;
109-
RI.Typed.ElementCount = ElementCount;
110-
RI.MultiSample.Count = SampleCount;
105+
RI.setTyped(ElementTy, ElementCount);
106+
RI.setMultiSample(SampleCount);
111107
return RI;
112108
}
113109

@@ -118,20 +114,15 @@ ResourceInfo ResourceInfo::UAV(Value *Symbol, StringRef Name,
118114
ResourceInfo RI(ResourceClass::UAV, Kind, Symbol, Name);
119115
assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) &&
120116
"Invalid ResourceKind for UAV constructor.");
121-
RI.Typed.ElementTy = ElementTy;
122-
RI.Typed.ElementCount = ElementCount;
123-
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
124-
RI.UAVFlags.IsROV = IsROV;
125-
RI.UAVFlags.HasCounter = false;
117+
RI.setTyped(ElementTy, ElementCount);
118+
RI.setUAV(GloballyCoherent, /*HasCounter=*/false, IsROV);
126119
return RI;
127120
}
128121

129122
ResourceInfo ResourceInfo::RWRawBuffer(Value *Symbol, StringRef Name,
130123
bool GloballyCoherent, bool IsROV) {
131124
ResourceInfo RI(ResourceClass::UAV, ResourceKind::RawBuffer, Symbol, Name);
132-
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
133-
RI.UAVFlags.IsROV = IsROV;
134-
RI.UAVFlags.HasCounter = false;
125+
RI.setUAV(GloballyCoherent, /*HasCounter=*/false, IsROV);
135126
return RI;
136127
}
137128

@@ -141,11 +132,8 @@ ResourceInfo ResourceInfo::RWStructuredBuffer(Value *Symbol, StringRef Name,
141132
bool HasCounter) {
142133
ResourceInfo RI(ResourceClass::UAV, ResourceKind::StructuredBuffer, Symbol,
143134
Name);
144-
RI.Struct.Stride = Stride;
145-
RI.Struct.Alignment = Alignment;
146-
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
147-
RI.UAVFlags.IsROV = IsROV;
148-
RI.UAVFlags.HasCounter = HasCounter;
135+
RI.setStruct(Stride, Alignment);
136+
RI.setUAV(GloballyCoherent, HasCounter, IsROV);
149137
return RI;
150138
}
151139

@@ -155,12 +143,9 @@ ResourceInfo ResourceInfo::RWTexture2DMS(Value *Symbol, StringRef Name,
155143
uint32_t SampleCount,
156144
bool GloballyCoherent) {
157145
ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMS, Symbol, Name);
158-
RI.Typed.ElementTy = ElementTy;
159-
RI.Typed.ElementCount = ElementCount;
160-
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
161-
RI.UAVFlags.IsROV = false;
162-
RI.UAVFlags.HasCounter = false;
163-
RI.MultiSample.Count = SampleCount;
146+
RI.setTyped(ElementTy, ElementCount);
147+
RI.setUAV(GloballyCoherent, /*HasCounter=*/false, /*IsROV=*/false);
148+
RI.setMultiSample(SampleCount);
164149
return RI;
165150
}
166151

@@ -171,23 +156,18 @@ ResourceInfo ResourceInfo::RWTexture2DMSArray(Value *Symbol, StringRef Name,
171156
bool GloballyCoherent) {
172157
ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMSArray, Symbol,
173158
Name);
174-
RI.Typed.ElementTy = ElementTy;
175-
RI.Typed.ElementCount = ElementCount;
176-
RI.UAVFlags.GloballyCoherent = GloballyCoherent;
177-
RI.UAVFlags.IsROV = false;
178-
RI.UAVFlags.HasCounter = false;
179-
RI.MultiSample.Count = SampleCount;
159+
RI.setTyped(ElementTy, ElementCount);
160+
RI.setUAV(GloballyCoherent, /*HasCounter=*/false, /*IsROV=*/false);
161+
RI.setMultiSample(SampleCount);
180162
return RI;
181163
}
182164

183165
ResourceInfo ResourceInfo::FeedbackTexture2D(Value *Symbol, StringRef Name,
184166
SamplerFeedbackType FeedbackTy) {
185167
ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2D, Symbol,
186168
Name);
187-
RI.UAVFlags.GloballyCoherent = false;
188-
RI.UAVFlags.IsROV = false;
189-
RI.UAVFlags.HasCounter = false;
190-
RI.Feedback.Type = FeedbackTy;
169+
RI.setUAV(/*GloballyCoherent=*/false, /*HasCounter=*/false, /*IsROV=*/false);
170+
RI.setFeedback(FeedbackTy);
191171
return RI;
192172
}
193173

@@ -196,24 +176,22 @@ ResourceInfo::FeedbackTexture2DArray(Value *Symbol, StringRef Name,
196176
SamplerFeedbackType FeedbackTy) {
197177
ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2DArray,
198178
Symbol, Name);
199-
RI.UAVFlags.GloballyCoherent = false;
200-
RI.UAVFlags.IsROV = false;
201-
RI.UAVFlags.HasCounter = false;
202-
RI.Feedback.Type = FeedbackTy;
179+
RI.setUAV(/*GloballyCoherent=*/false, /*HasCounter=*/false, /*IsROV=*/false);
180+
RI.setFeedback(FeedbackTy);
203181
return RI;
204182
}
205183

206184
ResourceInfo ResourceInfo::CBuffer(Value *Symbol, StringRef Name,
207185
uint32_t Size) {
208186
ResourceInfo RI(ResourceClass::CBuffer, ResourceKind::CBuffer, Symbol, Name);
209-
RI.CBufferSize = Size;
187+
RI.setCBuffer(Size);
210188
return RI;
211189
}
212190

213191
ResourceInfo ResourceInfo::Sampler(Value *Symbol, StringRef Name,
214192
SamplerType SamplerTy) {
215193
ResourceInfo RI(ResourceClass::Sampler, ResourceKind::Sampler, Symbol, Name);
216-
RI.SamplerTy = SamplerTy;
194+
RI.setSampler(SamplerTy);
217195
return RI;
218196
}
219197

0 commit comments

Comments
 (0)