Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions llvm/include/llvm/MC/DXContainerRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/IndexedMap.h"
#include "llvm/BinaryFormat/DXContainer.h"
#include <cstdint>
#include <limits>
#include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Support/raw_ostream.h"

namespace llvm {

class raw_ostream;

namespace mcdxbc {

struct RootSignatureDesc {
dxbc::RootSignatureHeader Header;
SmallVector<dxbc::RootParameter> Parameters;

void write(raw_ostream &OS) const;
Error write(raw_ostream &OS) const;

uint32_t getSizeInBytes() const {
// Header Size + accounting for parameter offset + parameters size
return 24 + (Parameters.size() * 4) + Parameters.size_in_bytes();
}
};
} // namespace mcdxbc
} // namespace llvm
114 changes: 85 additions & 29 deletions llvm/lib/MC/DXContainerRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,109 @@
//===----------------------------------------------------------------------===//

#include "llvm/MC/DXContainerRootSignature.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/BinaryStreamWriter.h"
#include <cstdint>
#include <sys/types.h>

using namespace llvm;
using namespace llvm::mcdxbc;

void RootSignatureDesc::write(raw_ostream &OS) const {
// Root signature header in dxcontainer has 6 uint_32t values.
const uint32_t HeaderSize = 24;
const uint32_t ParameterByteSize = Parameters.size_in_bytes();
const uint32_t NumParametes = Parameters.size();
Error setRewrite(BinaryStreamWriter &Stream, uint32_t &Offset) {
const uint32_t DummyValue = std::numeric_limits<uint32_t>::max();

Offset = Stream.getOffset();

if (Error Err = Stream.writeInteger(DummyValue))
return Err;

return Error::success();
}

Error rewriteOffset(BinaryStreamWriter &Stream, uint32_t Offset) {
uint64_t Value = Stream.getOffset();
Stream.setOffset(Offset);
if (Error Err = Stream.writeInteger((uint32_t)Value))
return Err;

Stream.setOffset(Value);

return Error::success();
}

Error RootSignatureDesc::write(raw_ostream &OS) const {
std::vector<uint8_t> Buffer(getSizeInBytes());
BinaryStreamWriter Writer(Buffer, llvm::endianness::little);

const uint32_t NumParameters = Parameters.size();
const uint32_t Zero = 0;

// Writing header information
support::endian::write(OS, Header.Version, llvm::endianness::little);
support::endian::write(OS, NumParametes, llvm::endianness::little);
support::endian::write(OS, HeaderSize, llvm::endianness::little);
if (Error Err = Writer.writeInteger(Header.Version))
return Err;

if (Error Err = Writer.writeInteger(NumParameters))
return Err;

uint32_t HeaderPoint;
if (Error Err = setRewrite(Writer, HeaderPoint))
return Err;

// Static samplers still not implemented
support::endian::write(OS, Zero, llvm::endianness::little);
support::endian::write(OS, ParameterByteSize + HeaderSize,
llvm::endianness::little);
if (Error Err = Writer.writeInteger(Zero))
return Err;

if (Error Err = Writer.writeInteger(Zero))
return Err;

if (Error Err = Writer.writeInteger(Header.Flags))
return Err;

if (Error Err = rewriteOffset(Writer, HeaderPoint))
return Err;

SmallVector<uint32_t> ParamsOffset;
for (const auto &P : Parameters) {

support::endian::write(OS, Header.Flags, llvm::endianness::little);
if (Error Err = Writer.writeEnum(P.ParameterType))
return Err;

uint32_t ParamsOffset =
HeaderSize + (3 * sizeof(uint32_t) * Parameters.size());
for (const dxbc::RootParameter &P : Parameters) {
support::endian::write(OS, P.ParameterType, llvm::endianness::little);
support::endian::write(OS, P.ShaderVisibility, llvm::endianness::little);
support::endian::write(OS, ParamsOffset, llvm::endianness::little);
if (Error Err = Writer.writeEnum(P.ShaderVisibility))
return Err;

// Size of root parameter, removing the ParameterType and ShaderVisibility.
ParamsOffset += sizeof(dxbc::RootParameter) - 2 * sizeof(uint32_t);
uint32_t Offset;
if (Error Err = setRewrite(Writer, Offset))
return Err;
ParamsOffset.push_back(Offset);
}

for (const dxbc::RootParameter &P : Parameters) {
size_t It = 0;
for (const auto &P : Parameters) {

auto Offset = ParamsOffset[It];
if (Error Err = rewriteOffset(Writer, Offset))
return Err;
It++;

switch (P.ParameterType) {
case dxbc::RootParameterType::Constants32Bit: {
support::endian::write(OS, P.Constants.ShaderRegister,
llvm::endianness::little);
support::endian::write(OS, P.Constants.RegisterSpace,
llvm::endianness::little);
support::endian::write(OS, P.Constants.Num32BitValues,
llvm::endianness::little);
if (Error Err = Writer.writeInteger(P.Constants.ShaderRegister))
return Err;
if (Error Err = Writer.writeInteger(P.Constants.RegisterSpace))
return Err;
if (Error Err = Writer.writeInteger(P.Constants.Num32BitValues))
return Err;
} break;
case dxbc::RootParameterType::Empty:
llvm_unreachable("Invalid RootParameterType");
}
}

assert(It == NumParameters);

llvm::ArrayRef<char> BufferRef(reinterpret_cast<char *>(Buffer.data()),
Buffer.size());
OS.write(BufferRef.data(), BufferRef.size());

return Error::success();
}
10 changes: 6 additions & 4 deletions llvm/lib/Object/DXContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,16 +296,18 @@ Error DirectX::RootSignature::parse(StringRef Data) {
NewParam.ParameterType =
support::endian::read<dxbc::RootParameterType,
llvm::endianness::little>(Current);
if (!dxbc::RootSignatureValidations::isValidParameterType(NewParam.ParameterType))
if (!dxbc::RootSignatureValidations::isValidParameterType(
NewParam.ParameterType))
return validationFailed("unsupported parameter type value read: " +
llvm::Twine((uint32_t)NewParam.ParameterType));

Current += sizeof(dxbc::RootParameterType);

NewParam.ShaderVisibility =
support::endian::read<dxbc::ShaderVisibility,
llvm::endianness::little>(Current);
if (!dxbc::RootSignatureValidations::isValidShaderVisibility(NewParam.ShaderVisibility))
support::endian::read<dxbc::ShaderVisibility, llvm::endianness::little>(
Current);
if (!dxbc::RootSignatureValidations::isValidShaderVisibility(
NewParam.ShaderVisibility))
return validationFailed("unsupported shader visility flag value read: " +
llvm::Twine((uint32_t)NewParam.ShaderVisibility));

Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/ObjectYAML/DXContainerEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
RS.Header.Version = P.RootSignature->Version;
RS.Parameters = std::move(P.RootSignature->Parameters);

RS.write(OS);
if (Error Err = RS.write(OS))
handleAllErrors(std::move(Err));

break;
}
uint64_t BytesWritten = OS.tell() - DataStart;
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#include "llvm/InitializePasses.h"
#include "llvm/MC/DXContainerPSVInfo.h"
#include "llvm/Pass.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MD5.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <optional>
#include <utility>

using namespace llvm;
using namespace llvm::dxil;
Expand Down Expand Up @@ -173,7 +175,8 @@ void DXContainerGlobals::addRootSignature(Module &M,
SmallString<256> Data;
raw_svector_ostream OS(Data);

RS.write(OS);
if (Error Err = RS.write(OS))
handleAllErrors(std::move(Err));

Constant *Constant =
ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
; DXC-NEXT: RootSignature:
; DXC-NEXT: Version: 2
; DXC-NEXT: NumStaticSamplers: 0
; DXC-NEXT: StaticSamplersOffset: 24
; DXC-NEXT: StaticSamplersOffset: 0
; DXC-NEXT: Parameters: []
; DXC-NEXT: AllowInputAssemblerInputLayout: true
2 changes: 1 addition & 1 deletion llvm/test/ObjectYAML/DXContainer/RootSignature-Flags.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Parts:
# CHECK-NEXT: RootSignature:
# CHECK-NEXT: Version: 2
# CHECK-NEXT: NumStaticSamplers: 0
# CHECK-NEXT: StaticSamplersOffset: 24
# CHECK-NEXT: StaticSamplersOffset: 0
# CHECK-NEXT: Parameters: []
# CHECK-NEXT: AllowInputAssemblerInputLayout: true
# CHECK-NEXT: DenyGeometryShaderRootAccess: true
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Parts:
# CHECK-NEXT: RootSignature:
# CHECK-NEXT: Version: 2
# CHECK-NEXT: NumStaticSamplers: 0
# CHECK-NEXT: StaticSamplersOffset: 64
# CHECK-NEXT: StaticSamplersOffset: 0
# CHECK-NEXT: Parameters:
# CHECK-NEXT: - ParameterType: Constants32Bit
# CHECK-NEXT: ShaderVisibility: Hull
Expand Down
8 changes: 4 additions & 4 deletions llvm/unittests/ObjectYAML/DXContainerYAMLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ TEST(RootSignature, ParseRootFlags) {
)"));

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

EXPECT_EQ(Storage.size(), 68u);
Expand Down Expand Up @@ -184,7 +184,7 @@ TEST(RootSignature, ParseRootConstants) {
0x85, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
0x52, 0x54, 0x53, 0x30, 0x59, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x2c, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand Down