Skip to content

[DirectX] Error handling improve in root signature metadata Parser #149232

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
Show file tree
Hide file tree
Changes from 15 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
116 changes: 95 additions & 21 deletions llvm/include/llvm/Frontend/HLSL/RootSignatureMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#ifndef LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H
#define LLVM_FRONTEND_HLSL_ROOTSIGNATUREMETADATA_H

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
#include "llvm/ADT/Twine.h"

#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
#include "llvm/IR/Constants.h"
#include "llvm/MC/DXContainerRootSignature.h"
Expand All @@ -26,6 +28,80 @@ class Metadata;
namespace hlsl {
namespace rootsig {

template <typename T>
class RootSignatureValidationError
: public ErrorInfo<RootSignatureValidationError<T>> {
public:
static char ID;
StringRef ParamName;
T Value;

RootSignatureValidationError(StringRef ParamName, T Value)
: ParamName(ParamName), 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;
StringRef Message;
MDNode *MD;

GenericRSMetadataError(StringRef Message, MDNode *MD)
: Message(Message), MD(MD) {}

void log(raw_ostream &OS) const override {
OS << Message;
if (MD) {
OS << "\n";
MD->printTree(OS);
}
}

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

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

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

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;
StringRef ParamName;

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

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 Down Expand Up @@ -66,29 +142,27 @@ class MetadataParser {
public:
MetadataParser(MDNode *Root) : Root(Root) {}

LLVM_ABI bool ParseRootSignature(LLVMContext *Ctx,
mcdxbc::RootSignatureDesc &RSD);
LLVM_ABI llvm::Expected<llvm::mcdxbc::RootSignatureDesc>
ParseRootSignature(uint32_t Version);

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

bool validateRootSignature(LLVMContext *Ctx,
const llvm::mcdxbc::RootSignatureDesc &RSD);
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;
};
Expand Down
Loading
Loading