Skip to content

Commit 767b7d0

Browse files
author
joaosaffran
committed
first working version
1 parent fe13b61 commit 767b7d0

File tree

9 files changed

+85
-43
lines changed

9 files changed

+85
-43
lines changed

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/BinaryFormat/DXContainer.h"
10-
#include <cstdint>
11-
#include <limits>
10+
#include "llvm/Support/raw_ostream.h"
1211

1312
namespace llvm {
1413

@@ -19,7 +18,7 @@ struct RootSignatureDesc {
1918
dxbc::RootSignatureHeader Header;
2019
SmallVector<dxbc::RootParameter> Parameters;
2120

22-
void write(raw_ostream &OS) const;
21+
Error write(raw_ostream &OS) const;
2322
};
2423
} // namespace mcdxbc
2524
} // namespace llvm

llvm/lib/MC/DXContainerRootSignature.cpp

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,89 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/MC/DXContainerRootSignature.h"
10-
#include "llvm/Support/EndianStream.h"
10+
#include "llvm/Support/BinaryStreamWriter.h"
11+
#include <vector>
1112

1213
using namespace llvm;
1314
using namespace llvm::mcdxbc;
1415

15-
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();
16+
Error RootSignatureDesc::write(raw_ostream &OS) const {
17+
// Header Size + accounting for parameter offset + parameters size
18+
std::vector<uint8_t> Buffer(24 + (Parameters.size() * 4) +
19+
Parameters.size_in_bytes());
20+
BinaryStreamWriter Writer(Buffer, llvm::endianness::little);
21+
22+
SmallVector<uint64_t> OffsetsToReplace;
23+
SmallVector<uint32_t> ValuesToReplaceOffsetsWith;
24+
const uint32_t Dummy = std::numeric_limits<uint32_t>::max();
25+
26+
const uint32_t NumParameters = Parameters.size();
2027
const uint32_t Zero = 0;
2128

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);
29+
if (Error Err = Writer.writeInteger(Header.Version))
30+
return Err;
31+
32+
if (Error Err = Writer.writeInteger(NumParameters))
33+
return Err;
34+
35+
OffsetsToReplace.push_back(Writer.getOffset());
36+
if (Error Err = Writer.writeInteger(Dummy))
37+
return Err;
2638

2739
// 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);
40+
if (Error Err = Writer.writeInteger(Zero))
41+
return Err;
42+
43+
if (Error Err = Writer.writeInteger(Zero))
44+
return Err;
45+
46+
if (Error Err = Writer.writeInteger(Header.Flags))
47+
return Err;
3148

32-
support::endian::write(OS, Header.Flags, llvm::endianness::little);
49+
ValuesToReplaceOffsetsWith.push_back(Writer.getOffset());
3350

34-
uint32_t ParamsOffset =
35-
HeaderSize + (3 * sizeof(uint32_t) * Parameters.size());
3651
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);
52+
if (Error Err = Writer.writeEnum(P.ParameterType))
53+
return Err;
54+
if (Error Err = Writer.writeEnum(P.ShaderVisibility))
55+
return Err;
4056

41-
// Size of root parameter, removing the ParameterType and ShaderVisibility.
42-
ParamsOffset += sizeof(dxbc::RootParameter) - 2 * sizeof(uint32_t);
57+
OffsetsToReplace.push_back(Writer.getOffset());
58+
if (Error Err = Writer.writeInteger(Dummy))
59+
return Err;
4360
}
4461

4562
for (const dxbc::RootParameter &P : Parameters) {
63+
ValuesToReplaceOffsetsWith.push_back(Writer.getOffset());
4664
switch (P.ParameterType) {
4765
case dxbc::RootParameterType::Constants32Bit: {
48-
support::endian::write(OS, P.Constants.ShaderRegister,
49-
llvm::endianness::little);
50-
support::endian::write(OS, P.Constants.RegisterSpace,
51-
llvm::endianness::little);
52-
support::endian::write(OS, P.Constants.Num32BitValues,
53-
llvm::endianness::little);
66+
if (Error Err = Writer.writeInteger(P.Constants.ShaderRegister))
67+
return Err;
68+
if (Error Err = Writer.writeInteger(P.Constants.RegisterSpace))
69+
return Err;
70+
if (Error Err = Writer.writeInteger(P.Constants.Num32BitValues))
71+
return Err;
5472
} break;
5573
case dxbc::RootParameterType::Empty:
5674
llvm_unreachable("Invalid RootParameterType");
5775
}
5876
}
77+
78+
assert(ValuesToReplaceOffsetsWith.size() == OffsetsToReplace.size() &&
79+
"Offset missing value to replace with.");
80+
81+
for (size_t It = 0; It < ValuesToReplaceOffsetsWith.size(); It++) {
82+
uint32_t Position = OffsetsToReplace[It];
83+
uint32_t Value = ValuesToReplaceOffsetsWith[It];
84+
85+
Writer.setOffset(Position);
86+
if (Error Err = Writer.writeInteger(Value))
87+
return Err;
88+
}
89+
90+
llvm::ArrayRef<char> BufferRef(reinterpret_cast<char *>(Buffer.data()),
91+
Buffer.size());
92+
OS.write(BufferRef.data(), BufferRef.size());
93+
94+
return Error::success();
5995
}

llvm/lib/Object/DXContainer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,18 @@ Error DirectX::RootSignature::parse(StringRef Data) {
296296
NewParam.ParameterType =
297297
support::endian::read<dxbc::RootParameterType,
298298
llvm::endianness::little>(Current);
299-
if (!dxbc::RootSignatureValidations::isValidParameterType(NewParam.ParameterType))
299+
if (!dxbc::RootSignatureValidations::isValidParameterType(
300+
NewParam.ParameterType))
300301
return validationFailed("unsupported parameter type value read: " +
301302
llvm::Twine((uint32_t)NewParam.ParameterType));
302303

303304
Current += sizeof(dxbc::RootParameterType);
304305

305306
NewParam.ShaderVisibility =
306-
support::endian::read<dxbc::ShaderVisibility,
307-
llvm::endianness::little>(Current);
308-
if (!dxbc::RootSignatureValidations::isValidShaderVisibility(NewParam.ShaderVisibility))
307+
support::endian::read<dxbc::ShaderVisibility, llvm::endianness::little>(
308+
Current);
309+
if (!dxbc::RootSignatureValidations::isValidShaderVisibility(
310+
NewParam.ShaderVisibility))
309311
return validationFailed("unsupported shader visility flag value read: " +
310312
llvm::Twine((uint32_t)NewParam.ShaderVisibility));
311313

llvm/lib/ObjectYAML/DXContainerEmitter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
271271
RS.Header.Version = P.RootSignature->Version;
272272
RS.Parameters = std::move(P.RootSignature->Parameters);
273273

274-
RS.write(OS);
274+
if (Error Err = RS.write(OS))
275+
handleAllErrors(std::move(Err));
276+
275277
break;
276278
}
277279
uint64_t BytesWritten = OS.tell() - DataStart;

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
#include "llvm/InitializePasses.h"
2626
#include "llvm/MC/DXContainerPSVInfo.h"
2727
#include "llvm/Pass.h"
28+
#include "llvm/Support/Error.h"
2829
#include "llvm/Support/MD5.h"
2930
#include "llvm/TargetParser/Triple.h"
3031
#include "llvm/Transforms/Utils/ModuleUtils.h"
3132
#include <optional>
33+
#include <utility>
3234

3335
using namespace llvm;
3436
using namespace llvm::dxil;
@@ -173,7 +175,8 @@ void DXContainerGlobals::addRootSignature(Module &M,
173175
SmallString<256> Data;
174176
raw_svector_ostream OS(Data);
175177

176-
RS.write(OS);
178+
if (Error Err = RS.write(OS))
179+
handleAllErrors(std::move(Err));
177180

178181
Constant *Constant =
179182
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);

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)