Skip to content

Commit a94edb6

Browse files
authored
[DXIL][Analysis] Make alignment on StructuredBuffer optional
HLSL allows StructuredBuffer<> to be defined with scalar or up-to-4-element vectors as well as with structs, but when doing so `dxc` doesn't set the alignment. Emulate this. Pull Request: llvm#100697
1 parent 38e671a commit a94edb6

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ResourceInfo {
4747

4848
struct StructInfo {
4949
uint32_t Stride;
50-
Align Alignment;
50+
MaybeAlign Alignment;
5151

5252
bool operator==(const StructInfo &RHS) const {
5353
return std::tie(Stride, Alignment) == std::tie(RHS.Stride, RHS.Alignment);
@@ -138,7 +138,7 @@ class ResourceInfo {
138138
CBufferSize = Size;
139139
}
140140
void setSampler(dxil::SamplerType Ty) { SamplerTy = Ty; }
141-
void setStruct(uint32_t Stride, Align Alignment) {
141+
void setStruct(uint32_t Stride, MaybeAlign Alignment) {
142142
assert(isStruct() && "Not a Struct");
143143
Struct.Stride = Stride;
144144
Struct.Alignment = Alignment;
@@ -164,7 +164,7 @@ class ResourceInfo {
164164
dxil::ResourceKind Kind);
165165
static ResourceInfo RawBuffer(Value *Symbol, StringRef Name);
166166
static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name,
167-
uint32_t Stride, Align Alignment);
167+
uint32_t Stride, MaybeAlign Alignment);
168168
static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name,
169169
dxil::ElementType ElementTy,
170170
uint32_t ElementCount, uint32_t SampleCount);
@@ -180,9 +180,9 @@ class ResourceInfo {
180180
static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name,
181181
bool GloballyCoherent, bool IsROV);
182182
static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name,
183-
uint32_t Stride,
184-
Align Alignment, bool GloballyCoherent,
185-
bool IsROV, bool HasCounter);
183+
uint32_t Stride, MaybeAlign Alignment,
184+
bool GloballyCoherent, bool IsROV,
185+
bool HasCounter);
186186
static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name,
187187
dxil::ElementType ElementTy,
188188
uint32_t ElementCount, uint32_t SampleCount,

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 6 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,8 @@ 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 =
290+
(isStruct() && Struct.Alignment) ? Log2(*Struct.Alignment) : 0;
288291
bool IsUAV = isUAV();
289292
bool IsROV = IsUAV && UAVFlags.IsROV;
290293
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)