Skip to content

[DirectX] Moving Root Signature Metadata Parsing in to Shared Root Signature Metadata lib #149221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
133 changes: 133 additions & 0 deletions llvm/include/llvm/Frontend/HLSL/RootSignatureMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H

#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
#include "llvm/IR/Constants.h"
#include "llvm/MC/DXContainerRootSignature.h"

namespace llvm {
class LLVMContext;
Expand All @@ -24,6 +26,96 @@ class Metadata;
namespace hlsl {
namespace rootsig {

inline std::optional<uint32_t> extractMdIntValue(MDNode *Node,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything to prevent this from being static functions in the .cpp file?

I don't think we want to expose this api.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used to extract some metadata in DXILRootSignature

unsigned int OpId) {
if (auto *CI =
mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpId).get()))
return CI->getZExtValue();
return std::nullopt;
}

inline std::optional<float> extractMdFloatValue(MDNode *Node,
unsigned int OpId) {
if (auto *CI = mdconst::dyn_extract<ConstantFP>(Node->getOperand(OpId).get()))
return CI->getValueAPF().convertToFloat();
return std::nullopt;
}

inline std::optional<StringRef> extractMdStringValue(MDNode *Node,
unsigned int OpId) {
MDString *NodeText = dyn_cast<MDString>(Node->getOperand(OpId));
if (NodeText == nullptr)
return std::nullopt;
return NodeText->getString();
}

template <typename T>
class RootSignatureValidationError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all seems like new code?

It might be a little easier to review if we have just an NFC code move and then a separate change to error handling

: public ErrorInfo<RootSignatureValidationError<T>> {
public:
static char ID;
std::string ParamName;
T Value;

RootSignatureValidationError(StringRef ParamName, T Value)
: ParamName(ParamName.str()), Value(Value) {}

void log(raw_ostream &OS) const override {
OS << "Invalid value for " << ParamName << ": " << Value;
}

std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
};

class GenericRSMetadataError : public ErrorInfo<GenericRSMetadataError> {
public:
static char ID;
std::string Message;

GenericRSMetadataError(Twine Message) : Message(Message.str()) {}

void log(raw_ostream &OS) const override { OS << Message; }

std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
};

class InvalidRSMetadataFormat : public ErrorInfo<InvalidRSMetadataFormat> {
public:
static char ID;
std::string ElementName;

InvalidRSMetadataFormat(StringRef ElementName)
: ElementName(ElementName.str()) {}

void log(raw_ostream &OS) const override {
OS << "Invalid format for " << ElementName;
}

std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
};

class InvalidRSMetadataValue : public ErrorInfo<InvalidRSMetadataValue> {
public:
static char ID;
std::string ParamName;

InvalidRSMetadataValue(StringRef ParamName) : ParamName(ParamName.str()) {}

void log(raw_ostream &OS) const override {
OS << "Invalid value for " << ParamName;
}

std::error_code convertToErrorCode() const override {
return llvm::inconvertibleErrorCode();
}
};

class MetadataBuilder {
public:
MetadataBuilder(llvm::LLVMContext &Ctx, ArrayRef<RootElement> Elements)
Expand All @@ -49,6 +141,47 @@ class MetadataBuilder {
SmallVector<Metadata *> GeneratedMetadata;
};

enum class RootSignatureElementKind {
Error = 0,
RootFlags = 1,
RootConstants = 2,
SRV = 3,
UAV = 4,
CBV = 5,
DescriptorTable = 6,
StaticSamplers = 7
};

class MetadataParser {
public:
MetadataParser(MDNode *Root) : Root(Root) {}

/// Iterates through root signature and converts them into MapT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we specify what MapT is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was a previous version, that I tried during development.

LLVM_ABI llvm::Expected<llvm::mcdxbc::RootSignatureDesc>
ParseRootSignature(uint32_t Version);

private:
llvm::Error parseRootFlags(mcdxbc::RootSignatureDesc &RSD,
MDNode *RootFlagNode);
llvm::Error parseRootConstants(mcdxbc::RootSignatureDesc &RSD,
MDNode *RootConstantNode);
llvm::Error parseRootDescriptors(mcdxbc::RootSignatureDesc &RSD,
MDNode *RootDescriptorNode,
RootSignatureElementKind ElementKind);
llvm::Error parseDescriptorRange(mcdxbc::DescriptorTable &Table,
MDNode *RangeDescriptorNode);
llvm::Error parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD,
MDNode *DescriptorTableNode);
llvm::Error parseRootSignatureElement(mcdxbc::RootSignatureDesc &RSD,
MDNode *Element);
llvm::Error parseStaticSampler(mcdxbc::RootSignatureDesc &RSD,
MDNode *StaticSamplerNode);

llvm::Error validateRootSignature(const llvm::mcdxbc::RootSignatureDesc &RSD);

MDNode *Root;
};

} // namespace rootsig
} // namespace hlsl
} // namespace llvm
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/MC/DXContainerRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_MC_DXCONTAINERROOTSIGNATURE_H
#define LLVM_MC_DXCONTAINERROOTSIGNATURE_H

#include "llvm/BinaryFormat/DXContainer.h"
#include <cstdint>
#include <limits>
Expand Down Expand Up @@ -116,3 +119,5 @@ struct RootSignatureDesc {
};
} // namespace mcdxbc
} // namespace llvm

#endif // LLVM_MC_DXCONTAINERROOTSIGNATURE_H
Loading
Loading