Skip to content

Commit 6992ebc

Browse files
authored
Reapply "[DXIL][Analysis] Make alignment on StructuredBuffer optional" (#101113)
Unfortunately storing a `MaybeAlign` in ResourceInfo deletes our move constructor in compilers that haven't implemented [P0602R4], like GCC 7. Since we only ever use the alignment in ways where alignment 1 and unset are ambiguous anyway, we'll just store the integer AlignLog2 value that we'll eventually use directly. [P0602R4]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0602r4.html This reverts commit c22171f, reapplying a94edb6.
1 parent 5af7086 commit 6992ebc

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ class ResourceInfo {
4747

4848
struct StructInfo {
4949
uint32_t Stride;
50-
Align Alignment;
50+
// Note: we store an integer here rather than using `MaybeAlign` because in
51+
// GCC 7 MaybeAlign isn't trivial so having one in this union would delete
52+
// our move constructor.
53+
// See https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0602r4.html
54+
uint32_t AlignLog2;
5155

5256
bool operator==(const StructInfo &RHS) const {
53-
return std::tie(Stride, Alignment) == std::tie(RHS.Stride, RHS.Alignment);
57+
return std::tie(Stride, AlignLog2) == std::tie(RHS.Stride, RHS.AlignLog2);
5458
}
5559
bool operator!=(const StructInfo &RHS) const { return !(*this == RHS); }
5660
};
@@ -138,10 +142,10 @@ class ResourceInfo {
138142
CBufferSize = Size;
139143
}
140144
void setSampler(dxil::SamplerType Ty) { SamplerTy = Ty; }
141-
void setStruct(uint32_t Stride, Align Alignment) {
145+
void setStruct(uint32_t Stride, MaybeAlign Alignment) {
142146
assert(isStruct() && "Not a Struct");
143147
Struct.Stride = Stride;
144-
Struct.Alignment = Alignment;
148+
Struct.AlignLog2 = Alignment ? Log2(*Alignment) : 0;
145149
}
146150
void setTyped(dxil::ElementType ElementTy, uint32_t ElementCount) {
147151
assert(isTyped() && "Not Typed");
@@ -164,7 +168,7 @@ class ResourceInfo {
164168
dxil::ResourceKind Kind);
165169
static ResourceInfo RawBuffer(Value *Symbol, StringRef Name);
166170
static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name,
167-
uint32_t Stride, Align Alignment);
171+
uint32_t Stride, MaybeAlign Alignment);
168172
static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name,
169173
dxil::ElementType ElementTy,
170174
uint32_t ElementCount, uint32_t SampleCount);
@@ -180,9 +184,9 @@ class ResourceInfo {
180184
static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name,
181185
bool GloballyCoherent, bool IsROV);
182186
static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name,
183-
uint32_t Stride,
184-
Align Alignment, bool GloballyCoherent,
185-
bool IsROV, bool HasCounter);
187+
uint32_t Stride, MaybeAlign Alignment,
188+
bool GloballyCoherent, bool IsROV,
189+
bool HasCounter);
186190
static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name,
187191
dxil::ElementType ElementTy,
188192
uint32_t ElementCount, uint32_t SampleCount,

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ ResourceInfo ResourceInfo::RawBuffer(Value *Symbol, StringRef Name) {
7979
}
8080

8181
ResourceInfo ResourceInfo::StructuredBuffer(Value *Symbol, StringRef Name,
82-
uint32_t Stride, Align Alignment) {
82+
uint32_t Stride,
83+
MaybeAlign Alignment) {
8384
ResourceInfo RI(ResourceClass::SRV, ResourceKind::StructuredBuffer, Symbol,
8485
Name);
8586
RI.setStruct(Stride, Alignment);
@@ -127,7 +128,8 @@ ResourceInfo ResourceInfo::RWRawBuffer(Value *Symbol, StringRef Name,
127128
}
128129

129130
ResourceInfo ResourceInfo::RWStructuredBuffer(Value *Symbol, StringRef Name,
130-
uint32_t Stride, Align Alignment,
131+
uint32_t Stride,
132+
MaybeAlign Alignment,
131133
bool GloballyCoherent, bool IsROV,
132134
bool HasCounter) {
133135
ResourceInfo RI(ResourceClass::UAV, ResourceKind::StructuredBuffer, Symbol,
@@ -284,7 +286,7 @@ MDTuple *ResourceInfo::getAsMetadata(LLVMContext &Ctx) const {
284286

285287
std::pair<uint32_t, uint32_t> ResourceInfo::getAnnotateProps() const {
286288
uint32_t ResourceKind = llvm::to_underlying(Kind);
287-
uint32_t AlignLog2 = isStruct() ? Log2(Struct.Alignment) : 0;
289+
uint32_t AlignLog2 = isStruct() ? Struct.AlignLog2 : 0;
288290
bool IsUAV = isUAV();
289291
bool IsROV = IsUAV && UAVFlags.IsROV;
290292
bool IsGloballyCoherent = IsUAV && UAVFlags.GloballyCoherent;

llvm/unittests/Analysis/DXILResourceTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ TEST(DXILResource, AnnotationsAndMetadata) {
151151
EXPECT_MDEQ(
152152
MD, TestMD.get(0, Symbol, "Buffer0", 0, 0, 1, 12, 0, TestMD.get(1, 16)));
153153

154+
// StructuredBuffer<float3> Buffer1 : register(t1);
155+
Symbol = UndefValue::get(StructType::create(
156+
Context, {Floatx3Ty}, "class.StructuredBuffer<vector<float, 3> >"));
157+
Resource = ResourceInfo::StructuredBuffer(Symbol, "Buffer1",
158+
/*Stride=*/12, {});
159+
Resource.bind(1, 0, 1, 1);
160+
Props = Resource.getAnnotateProps();
161+
EXPECT_EQ(Props.first, 0x0000000cU);
162+
EXPECT_EQ(Props.second, 0x0000000cU);
163+
MD = Resource.getAsMetadata(Context);
164+
EXPECT_MDEQ(
165+
MD, TestMD.get(1, Symbol, "Buffer1", 0, 1, 1, 12, 0, TestMD.get(1, 12)));
166+
154167
// Texture2D<float4> ColorMapTexture : register(t2);
155168
Symbol = UndefValue::get(StructType::create(
156169
Context, {Floatx4Ty}, "class.Texture2D<vector<float, 4> >"));

0 commit comments

Comments
 (0)