Skip to content

Commit 5fdf860

Browse files
committed
[DX] Fix PSV resource serialization
When writing this initially I missed including the resource stride. This change adds the resources stride to the serialized value. I've also extended the testing and error reporting around parsing PSV information. This adds tests to verify that the reader produces meaningful error messages for malformed DXContainer files, and a test that verifies the resource stride is respected in the reader even if the stride isn't an expected or known value (as would happen if the format changes in the future). This is part of llvm#59479. Reviewed By: bogner, bob80905 Differential Revision: https://reviews.llvm.org/D155143
1 parent f385abf commit 5fdf860

31 files changed

+336
-11
lines changed

llvm/include/llvm/Object/DXContainer.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,19 @@ class PSVRuntimeInfo {
3535
// swaps it as appropriate.
3636
struct ResourceArray {
3737
StringRef Data;
38-
size_t Stride; // size of each element in the list.
38+
uint32_t Stride; // size of each element in the list.
3939

4040
ResourceArray() = default;
4141
ResourceArray(StringRef D, size_t S) : Data(D), Stride(S) {}
4242

4343
using value_type = dxbc::PSV::v2::ResourceBindInfo;
44+
static constexpr uint32_t MaxStride() {
45+
return static_cast<uint32_t>(sizeof(value_type));
46+
}
4447

4548
struct iterator {
4649
StringRef Data;
47-
size_t Stride; // size of each element in the list.
50+
uint32_t Stride; // size of each element in the list.
4851
const char *Current;
4952

5053
iterator(const ResourceArray &A, const char *C)
@@ -58,7 +61,8 @@ class PSVRuntimeInfo {
5861
value_type Val = {{0, 0, 0, 0}, 0, 0};
5962
if (Current >= Data.end())
6063
return Val;
61-
memcpy(static_cast<void *>(&Val), Current, Stride);
64+
memcpy(static_cast<void *>(&Val), Current,
65+
std::min(Stride, MaxStride()));
6266
if (sys::IsBigEndianHost)
6367
Val.swapBytes();
6468
return Val;
@@ -123,6 +127,8 @@ class PSVRuntimeInfo {
123127
: (Size >= sizeof(dxbc::PSV::v1::RuntimeInfo) ? 1 : 0);
124128
}
125129

130+
uint32_t getResourceStride() const { return Resources.Stride; }
131+
126132
const InfoStruct &getInfo() const { return BasicInfo; }
127133
};
128134

llvm/include/llvm/ObjectYAML/DXContainerYAML.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct PSVInfo {
8080
uint32_t Version;
8181

8282
dxbc::PSV::v2::RuntimeInfo Info;
83+
uint32_t ResourceStride;
8384
std::vector<ResourceBindInfo> Resources;
8485

8586
void mapInfoForVersion(yaml::IO &IO);

llvm/lib/MC/DXContainerPSVInfo.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ using namespace llvm::dxbc::PSV;
1616

1717
void PSVRuntimeInfo::write(raw_ostream &OS, uint32_t Version) const {
1818
uint32_t InfoSize;
19+
uint32_t BindingSize;
1920
switch (Version) {
2021
case 0:
2122
InfoSize = sizeof(dxbc::PSV::v0::RuntimeInfo);
23+
BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
2224
break;
2325
case 1:
2426
InfoSize = sizeof(dxbc::PSV::v1::RuntimeInfo);
27+
BindingSize = sizeof(dxbc::PSV::v0::ResourceBindInfo);
2528
break;
2629
case 2:
2730
default:
2831
InfoSize = sizeof(dxbc::PSV::v2::RuntimeInfo);
32+
BindingSize = sizeof(dxbc::PSV::v2::ResourceBindInfo);
2933
}
3034
uint32_t InfoSizeSwapped = InfoSize;
3135
if (sys::IsBigEndianHost)
@@ -40,8 +44,7 @@ void PSVRuntimeInfo::write(raw_ostream &OS, uint32_t Version) const {
4044
sys::swapByteOrder(ResourceCount);
4145
OS.write(reinterpret_cast<const char *>(&ResourceCount), sizeof(uint32_t));
4246

43-
size_t BindingSize = (Version < 2) ? sizeof(v0::ResourceBindInfo)
44-
: sizeof(v2::ResourceBindInfo);
47+
OS.write(reinterpret_cast<const char *>(&BindingSize), sizeof(uint32_t));
4548
for (const auto &Res : Resources)
4649
OS.write(reinterpret_cast<const char *>(&Res), BindingSize);
4750
}

llvm/lib/Object/DXContainer.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ Error DirectX::PSVRuntimeInfo::parse(uint16_t ShaderKind) {
200200

201201
StringRef PSVInfoData = Data.substr(sizeof(uint32_t), Size);
202202

203+
if (PSVInfoData.size() < Size)
204+
return parseFailed(
205+
"Pipeline state data extends beyond the bounds of the part");
206+
203207
using namespace dxbc::PSV;
204208

205209
const uint32_t PSVVersion = getVersion();
@@ -234,12 +238,20 @@ Error DirectX::PSVRuntimeInfo::parse(uint16_t ShaderKind) {
234238
return Err;
235239
Current += sizeof(uint32_t);
236240

237-
Resources.Stride = (PSVVersion < 2) ? sizeof(v0::ResourceBindInfo)
238-
: sizeof(v2::ResourceBindInfo);
239-
size_t BindingDataSize = Resources.Stride * ResourceCount;
240-
Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
241+
if (ResourceCount > 0) {
242+
if (Error Err = readInteger(Data, Current, Resources.Stride))
243+
return Err;
244+
Current += sizeof(uint32_t);
245+
246+
size_t BindingDataSize = Resources.Stride * ResourceCount;
247+
Resources.Data = Data.substr(Current - Data.begin(), BindingDataSize);
241248

242-
Current += BindingDataSize;
249+
if (Resources.Data.size() < BindingDataSize)
250+
return parseFailed(
251+
"Resource binding data extends beyond the bounds of the part");
252+
253+
Current += BindingDataSize;
254+
}
243255

244256
return Error::success();
245257
}

llvm/lib/ObjectYAML/DXContainerYAML.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ void MappingTraits<DXContainerYAML::PSVInfo>::mapping(
127127
IO.mapRequired("ShaderStage", PSV.Info.ShaderStage);
128128
PSV.mapInfoForVersion(IO);
129129

130+
IO.mapRequired("ResourceStride", PSV.ResourceStride);
130131
IO.mapRequired("Resources", PSV.Resources);
131132

132133
// Restore the YAML context.

llvm/test/ObjectYAML/DXContainer/PSVv0-amplification.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Parts:
1717
PayloadSizeInBytes: 4092
1818
MinimumWaveLaneCount: 0
1919
MaximumWaveLaneCount: 4294967295
20+
ResourceStride: 16
2021
Resources:
2122
- Type: 1
2223
Space: 2
@@ -45,6 +46,7 @@ Parts:
4546
# CHECK-NEXT: PayloadSizeInBytes: 4092
4647
# CHECK-NEXT: MinimumWaveLaneCount: 0
4748
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
49+
# CHECK-NEXT: ResourceStride: 16
4850
# CHECK-NEXT: Resources:
4951
# CHECK-NEXT: - Type: 1
5052
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-compute.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Parts:
1616
ShaderStage: 5
1717
MinimumWaveLaneCount: 0
1818
MaximumWaveLaneCount: 4294967295
19+
ResourceStride: 16
1920
Resources:
2021
- Type: 1
2122
Space: 2
@@ -43,6 +44,7 @@ Parts:
4344
# CHECK-NEXT: ShaderStage: 5
4445
# CHECK-NEXT: MinimumWaveLaneCount: 0
4546
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
47+
# CHECK-NEXT: ResourceStride: 16
4648
# CHECK-NEXT: Resources:
4749
# CHECK-NEXT: - Type: 1
4850
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-domain.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Parts:
1919
TessellatorDomain: 2056
2020
MinimumWaveLaneCount: 0
2121
MaximumWaveLaneCount: 4294967295
22+
ResourceStride: 16
2223
Resources:
2324
- Type: 1
2425
Space: 2
@@ -49,6 +50,7 @@ Parts:
4950
# CHECK-NEXT: TessellatorDomain: 2056
5051
# CHECK-NEXT: MinimumWaveLaneCount: 0
5152
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
53+
# CHECK-NEXT: ResourceStride: 16
5254
# CHECK-NEXT: Resources:
5355
# CHECK-NEXT: - Type: 1
5456
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-geometry.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Parts:
2020
OutputPositionPresent: 1
2121
MinimumWaveLaneCount: 0
2222
MaximumWaveLaneCount: 4294967295
23+
ResourceStride: 16
2324
Resources:
2425
- Type: 1
2526
Space: 2
@@ -51,6 +52,7 @@ Parts:
5152
# CHECK-NEXT: OutputPositionPresent: 1
5253
# CHECK-NEXT: MinimumWaveLaneCount: 0
5354
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
55+
# CHECK-NEXT: ResourceStride: 16
5456
# CHECK-NEXT: Resources:
5557
# CHECK-NEXT: - Type: 1
5658
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-hull.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Parts:
2020
TessellatorOutputPrimitive: 8192
2121
MinimumWaveLaneCount: 0
2222
MaximumWaveLaneCount: 4294967295
23+
ResourceStride: 16
2324
Resources:
2425
- Type: 1
2526
Space: 2
@@ -51,6 +52,7 @@ Parts:
5152
# CHECK-NEXT: TessellatorOutputPrimitive: 8192
5253
# CHECK-NEXT: MinimumWaveLaneCount: 0
5354
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
55+
# CHECK-NEXT: ResourceStride: 16
5456
# CHECK-NEXT: Resources:
5557
# CHECK-NEXT: - Type: 1
5658
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-mesh.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Parts:
2121
MaxOutputPrimitives: 4092
2222
MinimumWaveLaneCount: 0
2323
MaximumWaveLaneCount: 4294967295
24+
ResourceStride: 16
2425
Resources:
2526
- Type: 1
2627
Space: 2
@@ -53,6 +54,7 @@ Parts:
5354
# CHECK-NEXT: MaxOutputPrimitives: 4092
5455
# CHECK-NEXT: MinimumWaveLaneCount: 0
5556
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
57+
# CHECK-NEXT: ResourceStride: 16
5658
# CHECK-NEXT: Resources:
5759
# CHECK-NEXT: - Type: 1
5860
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-pixel.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Parts:
1818
SampleFrequency: 96
1919
MinimumWaveLaneCount: 0
2020
MaximumWaveLaneCount: 4294967295
21+
ResourceStride: 16
2122
Resources:
2223
- Type: 1
2324
Space: 2
@@ -47,6 +48,7 @@ Parts:
4748
# CHECK-NEXT: SampleFrequency: 96
4849
# CHECK-NEXT: MinimumWaveLaneCount: 0
4950
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
51+
# CHECK-NEXT: ResourceStride: 16
5052
# CHECK-NEXT: Resources:
5153
# CHECK-NEXT: - Type: 1
5254
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv0-vertex.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Parts:
1717
OutputPositionPresent: 1
1818
MinimumWaveLaneCount: 0
1919
MaximumWaveLaneCount: 4294967295
20+
ResourceStride: 16
2021
Resources:
2122
- Type: 1
2223
Space: 2
@@ -45,6 +46,7 @@ Parts:
4546
# CHECK-NEXT: OutputPositionPresent: 1
4647
# CHECK-NEXT: MinimumWaveLaneCount: 0
4748
# CHECK-NEXT: MaximumWaveLaneCount: 4294967295
49+
# CHECK-NEXT: ResourceStride: 16
4850
# CHECK-NEXT: Resources:
4951
# CHECK-NEXT: - Type: 1
5052
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-amplification.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Parts:
2323
SigPatchConstOrPrimElements: 32
2424
SigInputVectors: 64
2525
SigOutputVectors: [ 8, 16, 32, 64 ]
26+
ResourceStride: 16
2627
Resources:
2728
- Type: 1
2829
Space: 2
@@ -57,6 +58,7 @@ Parts:
5758
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
5859
# CHECK-NEXT: SigInputVectors: 64
5960
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
61+
# CHECK-NEXT: ResourceStride: 16
6062
# CHECK-NEXT: Resources:
6163
# CHECK-NEXT: - Type: 1
6264
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-compute.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Parts:
2222
SigPatchConstOrPrimElements: 32
2323
SigInputVectors: 64
2424
SigOutputVectors: [ 8, 16, 32, 64 ]
25+
ResourceStride: 16
2526
Resources:
2627
- Type: 1
2728
Space: 2
@@ -55,6 +56,7 @@ Parts:
5556
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
5657
# CHECK-NEXT: SigInputVectors: 64
5758
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
59+
# CHECK-NEXT: ResourceStride: 16
5860
# CHECK-NEXT: Resources:
5961
# CHECK-NEXT: - Type: 1
6062
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-domain.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Parts:
2626
SigPatchConstOrPrimElements: 32
2727
SigInputVectors: 64
2828
SigOutputVectors: [ 8, 16, 32, 64 ]
29+
ResourceStride: 16
2930
Resources:
3031
- Type: 1
3132
Space: 2
@@ -63,6 +64,7 @@ Parts:
6364
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
6465
# CHECK-NEXT: SigInputVectors: 64
6566
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
67+
# CHECK-NEXT: ResourceStride: 16
6668
# CHECK-NEXT: Resources:
6769
# CHECK-NEXT: - Type: 1
6870
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-geometry.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Parts:
2727
SigPatchConstOrPrimElements: 32
2828
SigInputVectors: 64
2929
SigOutputVectors: [ 8, 16, 32, 64 ]
30+
ResourceStride: 16
3031
Resources:
3132
- Type: 1
3233
Space: 2
@@ -65,6 +66,7 @@ Parts:
6566
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
6667
# CHECK-NEXT: SigInputVectors: 64
6768
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
69+
# CHECK-NEXT: ResourceStride: 16
6870
# CHECK-NEXT: Resources:
6971
# CHECK-NEXT: - Type: 1
7072
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-hull.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Parts:
2727
SigPatchConstOrPrimElements: 32
2828
SigInputVectors: 64
2929
SigOutputVectors: [ 8, 16, 32, 64 ]
30+
ResourceStride: 16
3031
Resources:
3132
- Type: 1
3233
Space: 2
@@ -65,6 +66,7 @@ Parts:
6566
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
6667
# CHECK-NEXT: SigInputVectors: 64
6768
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
69+
# CHECK-NEXT: ResourceStride: 16
6870
# CHECK-NEXT: Resources:
6971
# CHECK-NEXT: - Type: 1
7072
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-mesh.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Parts:
2929
SigPatchConstOrPrimElements: 32
3030
SigInputVectors: 64
3131
SigOutputVectors: [ 8, 16, 32, 64 ]
32+
ResourceStride: 16
3233
Resources:
3334
- Type: 1
3435
Space: 2
@@ -69,6 +70,7 @@ Parts:
6970
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
7071
# CHECK-NEXT: SigInputVectors: 64
7172
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
73+
# CHECK-NEXT: ResourceStride: 16
7274
# CHECK-NEXT: Resources:
7375
# CHECK-NEXT: - Type: 1
7476
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-pixel.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Parts:
2424
SigPatchConstOrPrimElements: 32
2525
SigInputVectors: 64
2626
SigOutputVectors: [ 8, 16, 32, 64 ]
27+
ResourceStride: 16
2728
Resources:
2829
- Type: 1
2930
Space: 2
@@ -59,6 +60,7 @@ Parts:
5960
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
6061
# CHECK-NEXT: SigInputVectors: 64
6162
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
63+
# CHECK-NEXT: ResourceStride: 16
6264
# CHECK-NEXT: Resources:
6365
# CHECK-NEXT: - Type: 1
6466
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv1-vertex.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Parts:
2323
SigPatchConstOrPrimElements: 32
2424
SigInputVectors: 64
2525
SigOutputVectors: [ 8, 16, 32, 64 ]
26+
ResourceStride: 16
2627
Resources:
2728
- Type: 1
2829
Space: 2
@@ -57,6 +58,7 @@ Parts:
5758
# CHECK-NEXT: SigPatchConstOrPrimElements: 32
5859
# CHECK-NEXT: SigInputVectors: 64
5960
# CHECK-NEXT: SigOutputVectors: [ 8, 16, 32, 64 ]
61+
# CHECK-NEXT: ResourceStride: 16
6062
# CHECK-NEXT: Resources:
6163
# CHECK-NEXT: - Type: 1
6264
# CHECK-NEXT: Space: 2

llvm/test/ObjectYAML/DXContainer/PSVv2-amplification.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Parts:
2626
NumThreadsX: 512
2727
NumThreadsY: 1024
2828
NumThreadsZ: 2048
29+
ResourceStride: 24
2930
Resources:
3031
- Type: 1
3132
Space: 2
@@ -67,6 +68,7 @@ Parts:
6768
# CHECK-NEXT: NumThreadsX: 512
6869
# CHECK-NEXT: NumThreadsY: 1024
6970
# CHECK-NEXT: NumThreadsZ: 2048
71+
# CHECK-NEXT: ResourceStride: 24
7072
# CHECK-NEXT: Resources:
7173
# CHECK-NEXT: - Type: 1
7274
# CHECK-NEXT: Space: 2

0 commit comments

Comments
 (0)