Skip to content

Commit 15d1a8c

Browse files
author
joaosaffran
committed
Merge branch 'refactor/improve-offset-calculation' into users/joaosaffran/127840
2 parents 8434dc2 + d3fafab commit 15d1a8c

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,56 @@
1212
using namespace llvm;
1313
using namespace llvm::mcdxbc;
1414

15+
static uint32_t writePlaceholder(raw_ostream &Stream) {
16+
const uint32_t DummyValue = std::numeric_limits<uint32_t>::max();
17+
uint32_t Offset = Stream.tell();
18+
support::endian::write(Stream, DummyValue, llvm::endianness::little);
19+
return Offset;
20+
}
21+
22+
static void rewriteOffset(buffer_ostream &Stream, uint32_t Offset) {
23+
uint32_t Value =
24+
support::endian::byte_swap<uint32_t, llvm::endianness::little>(
25+
Stream.tell());
26+
Stream.pwrite(reinterpret_cast<const char *>(&Value), sizeof(Value), Offset);
27+
}
28+
1529
void RootSignatureDesc::write(raw_ostream &OS) const {
16-
// Root signature header in dxcontainer has 6 uint_32t values.
17-
const uint32_t HeaderSize = 24;
18-
const uint32_t ParameterByteSize = Parameters.size_in_bytes();
19-
const uint32_t NumParametes = Parameters.size();
30+
buffer_ostream BOS(OS);
31+
const uint32_t NumParameters = Parameters.size();
2032
const uint32_t Zero = 0;
2133

22-
// Writing header information
23-
support::endian::write(OS, Header.Version, llvm::endianness::little);
24-
support::endian::write(OS, NumParametes, llvm::endianness::little);
25-
support::endian::write(OS, HeaderSize, llvm::endianness::little);
34+
support::endian::write(BOS, Header.Version, llvm::endianness::little);
35+
support::endian::write(BOS, NumParameters, llvm::endianness::little);
36+
37+
uint32_t HeaderPoint = writePlaceholder(BOS);
2638

27-
// Static samplers still not implemented
28-
support::endian::write(OS, Zero, llvm::endianness::little);
29-
support::endian::write(OS, ParameterByteSize + HeaderSize,
30-
llvm::endianness::little);
39+
support::endian::write(BOS, Zero, llvm::endianness::little);
40+
support::endian::write(BOS, Zero, llvm::endianness::little);
41+
support::endian::write(BOS, Header.Flags, llvm::endianness::little);
3142

32-
support::endian::write(OS, Header.Flags, llvm::endianness::little);
43+
rewriteOffset(BOS, HeaderPoint);
3344

34-
uint32_t ParamsOffset =
35-
HeaderSize + (3 * sizeof(uint32_t) * Parameters.size());
36-
for (const dxbc::RootParameter &P : Parameters) {
37-
support::endian::write(OS, P.ParameterType, llvm::endianness::little);
38-
support::endian::write(OS, P.ShaderVisibility, llvm::endianness::little);
39-
support::endian::write(OS, ParamsOffset, llvm::endianness::little);
45+
SmallVector<uint32_t> ParamsOffsets;
46+
for (const auto &P : Parameters) {
47+
support::endian::write(BOS, P.ParameterType, llvm::endianness::little);
48+
support::endian::write(BOS, P.ShaderVisibility, llvm::endianness::little);
4049

41-
// Size of root parameter, removing the ParameterType and ShaderVisibility.
42-
ParamsOffset += sizeof(dxbc::RootParameter) - 2 * sizeof(uint32_t);
50+
ParamsOffsets.push_back(writePlaceholder(BOS));
4351
}
4452

45-
for (const dxbc::RootParameter &P : Parameters) {
53+
assert(NumParameters == ParamsOffsets.size());
54+
for (size_t I = 0; I < NumParameters; ++I) {
55+
rewriteOffset(BOS, ParamsOffsets[I]);
56+
const auto &P = Parameters[I];
57+
4658
switch (P.ParameterType) {
4759
case dxbc::RootParameterType::Constants32Bit: {
48-
support::endian::write(OS, P.Constants.ShaderRegister,
60+
support::endian::write(BOS, P.Constants.ShaderRegister,
4961
llvm::endianness::little);
50-
support::endian::write(OS, P.Constants.RegisterSpace,
62+
support::endian::write(BOS, P.Constants.RegisterSpace,
5163
llvm::endianness::little);
52-
support::endian::write(OS, P.Constants.Num32BitValues,
64+
support::endian::write(BOS, P.Constants.Num32BitValues,
5365
llvm::endianness::little);
5466
} break;
5567
case dxbc::RootParameterType::Empty:

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
2323
; DXC-NEXT: RootSignature:
2424
; DXC-NEXT: Version: 2
2525
; DXC-NEXT: NumStaticSamplers: 0
26-
; DXC-NEXT: StaticSamplersOffset: 24
26+
; DXC-NEXT: StaticSamplersOffset: 0
2727
; DXC-NEXT: Parameters: []
2828
; DXC-NEXT: AllowInputAssemblerInputLayout: true

llvm/test/ObjectYAML/DXContainer/RootSignature-Flags.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Parts:
2525
# CHECK-NEXT: RootSignature:
2626
# CHECK-NEXT: Version: 2
2727
# CHECK-NEXT: NumStaticSamplers: 0
28-
# CHECK-NEXT: StaticSamplersOffset: 24
28+
# CHECK-NEXT: StaticSamplersOffset: 0
2929
# CHECK-NEXT: Parameters: []
3030
# CHECK-NEXT: AllowInputAssemblerInputLayout: true
3131
# CHECK-NEXT: DenyGeometryShaderRootAccess: true

llvm/test/ObjectYAML/DXContainer/RootSignature-MultipleParameters.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Parts:
3737
# CHECK-NEXT: RootSignature:
3838
# CHECK-NEXT: Version: 2
3939
# CHECK-NEXT: NumStaticSamplers: 0
40-
# CHECK-NEXT: StaticSamplersOffset: 64
40+
# CHECK-NEXT: StaticSamplersOffset: 0
4141
# CHECK-NEXT: Parameters:
4242
# CHECK-NEXT: - ParameterType: Constants32Bit
4343
# CHECK-NEXT: ShaderVisibility: Hull

llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ TEST(RootSignature, ParseRootFlags) {
134134
)"));
135135

136136
uint8_t Buffer[] = {
137-
0x44, 0x58, 0x42, 0x43, 0x32, 0x9A, 0x53, 0xD8, 0xEC, 0xBE, 0x35, 0x6F,
138-
0x05, 0x39, 0xE1, 0xFE, 0x31, 0x20, 0xF0, 0xC1, 0x01, 0x00, 0x00, 0x00,
137+
0x44, 0x58, 0x42, 0x43, 0x32, 0x9a, 0x53, 0xd8, 0xec, 0xbe, 0x35, 0x6f,
138+
0x05, 0x39, 0xe1, 0xfe, 0x31, 0x20, 0xf0, 0xc1, 0x01, 0x00, 0x00, 0x00,
139139
0x44, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
140140
0x52, 0x54, 0x53, 0x30, 0x18, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
141141
0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142-
0x18, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
142+
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
143143
};
144144

145145
EXPECT_EQ(Storage.size(), 68u);
@@ -184,7 +184,7 @@ TEST(RootSignature, ParseRootConstants) {
184184
0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
185185
0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
186186
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187-
0x2c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
187+
0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
188188
0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
189189
0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
190190
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0 commit comments

Comments
 (0)