-
Notifications
You must be signed in to change notification settings - Fork 15k
[NFC] Refactoring MCDXBC to support out of order storage of root parameters #137284
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
Changes from 24 commits
0abacfc
8b8c02a
7ac9641
c105458
efe76aa
a928e9d
a38f10b
9a7c359
d6c2b55
93e4cf2
b45b1b6
f804a23
15eb6f5
b9d7f07
46cc8c1
1b3e10a
1f31957
e8fbfce
a31e5a5
a394ad0
ad415a7
8ff4845
d67f7d3
5453ad0
836a8a8
5bd57a6
960cb9c
a60c7a3
2a4c2cb
c29d3f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,12 +15,64 @@ namespace llvm { | |
| class raw_ostream; | ||
| namespace mcdxbc { | ||
|
|
||
| struct RootParameter { | ||
| struct RootParameterInfo { | ||
| dxbc::RootParameterHeader Header; | ||
| union { | ||
| dxbc::RootConstants Constants; | ||
| dxbc::RTS0::v2::RootDescriptor Descriptor; | ||
| }; | ||
| size_t Location; | ||
|
|
||
| RootParameterInfo() = default; | ||
|
|
||
| RootParameterInfo(dxbc::RootParameterHeader H, size_t L) | ||
| : Header(H), Location(L) {} | ||
| }; | ||
|
|
||
| struct RootParametersContainer { | ||
| SmallVector<RootParameterInfo> ParametersInfo; | ||
|
|
||
| SmallVector<dxbc::RootConstants> Constants; | ||
| SmallVector<dxbc::RTS0::v2::RootDescriptor> Descriptors; | ||
|
|
||
| void addInfo(dxbc::RootParameterHeader H, size_t L) { | ||
| ParametersInfo.push_back(RootParameterInfo(H, L)); | ||
| } | ||
|
|
||
| void addParameter(dxbc::RootParameterHeader H, dxbc::RootConstants C) { | ||
| addInfo(H, Constants.size()); | ||
| Constants.push_back(C); | ||
| } | ||
|
|
||
| void addParameter(dxbc::RootParameterHeader H, | ||
| dxbc::RTS0::v2::RootDescriptor D) { | ||
| addInfo(H, Descriptors.size()); | ||
| Descriptors.push_back(D); | ||
| } | ||
|
||
|
|
||
| const std::pair<uint32_t, uint32_t> | ||
| getTypeAndLocForParameter(uint32_t Index) const { | ||
| const RootParameterInfo &Info = ParametersInfo[Index]; | ||
| return {Info.Header.ParameterType, Info.Location}; | ||
| } | ||
|
|
||
| const dxbc::RootParameterHeader &getHeader(size_t Index) const { | ||
| const RootParameterInfo &Info = ParametersInfo[Index]; | ||
| return Info.Header; | ||
| } | ||
|
|
||
| const dxbc::RootConstants &getConstant(size_t Index) const { | ||
| return Constants[Index]; | ||
| } | ||
|
|
||
| const dxbc::RTS0::v2::RootDescriptor &getRootDescriptor(size_t Index) const { | ||
| return Descriptors[Index]; | ||
| } | ||
|
Comment on lines
+65
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (I thought I commented on this already, but I think github ate it...) Seeing these next to |
||
|
|
||
| size_t size() const { return ParametersInfo.size(); } | ||
|
|
||
| SmallVector<RootParameterInfo>::const_iterator begin() const { | ||
| return ParametersInfo.begin(); | ||
| } | ||
| SmallVector<RootParameterInfo>::const_iterator end() const { | ||
| return ParametersInfo.end(); | ||
| } | ||
| }; | ||
| struct RootSignatureDesc { | ||
|
|
||
|
|
@@ -29,7 +81,7 @@ struct RootSignatureDesc { | |
| uint32_t RootParameterOffset = 0U; | ||
| uint32_t StaticSamplersOffset = 0u; | ||
| uint32_t NumStaticSamplers = 0u; | ||
| SmallVector<mcdxbc::RootParameter> Parameters; | ||
| mcdxbc::RootParametersContainer ParametersContainer; | ||
|
|
||
| void write(raw_ostream &OS) const; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,10 +30,10 @@ static void rewriteOffsetToCurrentByte(raw_svector_ostream &Stream, | |||||||
|
|
||||||||
| size_t RootSignatureDesc::getSize() const { | ||||||||
| size_t Size = sizeof(dxbc::RootSignatureHeader) + | ||||||||
| Parameters.size() * sizeof(dxbc::RootParameterHeader); | ||||||||
| ParametersContainer.size() * sizeof(dxbc::RootParameterHeader); | ||||||||
|
|
||||||||
| for (const mcdxbc::RootParameter &P : Parameters) { | ||||||||
| switch (P.Header.ParameterType) { | ||||||||
| for (const auto &I : ParametersContainer) { | ||||||||
| switch (I.Header.ParameterType) { | ||||||||
| case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): | ||||||||
| Size += sizeof(dxbc::RootConstants); | ||||||||
| break; | ||||||||
|
|
@@ -48,6 +48,7 @@ size_t RootSignatureDesc::getSize() const { | |||||||
| break; | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| return Size; | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -56,7 +57,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { | |||||||
| raw_svector_ostream BOS(Storage); | ||||||||
| BOS.reserveExtraSpace(getSize()); | ||||||||
|
|
||||||||
| const uint32_t NumParameters = Parameters.size(); | ||||||||
| const uint32_t NumParameters = ParametersContainer.size(); | ||||||||
|
|
||||||||
| support::endian::write(BOS, Version, llvm::endianness::little); | ||||||||
| support::endian::write(BOS, NumParameters, llvm::endianness::little); | ||||||||
|
|
@@ -66,7 +67,7 @@ void RootSignatureDesc::write(raw_ostream &OS) const { | |||||||
| support::endian::write(BOS, Flags, llvm::endianness::little); | ||||||||
|
|
||||||||
| SmallVector<uint32_t> ParamsOffsets; | ||||||||
| for (const mcdxbc::RootParameter &P : Parameters) { | ||||||||
| for (const RootParameterInfo &P : ParametersContainer) { | ||||||||
| support::endian::write(BOS, P.Header.ParameterType, | ||||||||
| llvm::endianness::little); | ||||||||
| support::endian::write(BOS, P.Header.ShaderVisibility, | ||||||||
|
|
@@ -76,29 +77,34 @@ void RootSignatureDesc::write(raw_ostream &OS) const { | |||||||
| } | ||||||||
|
|
||||||||
| assert(NumParameters == ParamsOffsets.size()); | ||||||||
| for (size_t I = 0; I < NumParameters; ++I) { | ||||||||
| const RootParameterInfo *H = ParametersContainer.begin(); | ||||||||
|
||||||||
| for (size_t I = 0; I < NumParameters; ++I, H++) { | ||||||||
| rewriteOffsetToCurrentByte(BOS, ParamsOffsets[I]); | ||||||||
| const mcdxbc::RootParameter &P = Parameters[I]; | ||||||||
|
|
||||||||
| switch (P.Header.ParameterType) { | ||||||||
| case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): | ||||||||
| support::endian::write(BOS, P.Constants.ShaderRegister, | ||||||||
| const auto &[Type, Loc] = ParametersContainer.getTypeAndLocForParameter(I); | ||||||||
| switch (Type) { | ||||||||
| case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): { | ||||||||
| const dxbc::RootConstants Constants = | ||||||||
| ParametersContainer.getConstant(Loc); | ||||||||
|
||||||||
| support::endian::write(BOS, Constants.ShaderRegister, | ||||||||
| llvm::endianness::little); | ||||||||
| support::endian::write(BOS, P.Constants.RegisterSpace, | ||||||||
| support::endian::write(BOS, Constants.RegisterSpace, | ||||||||
| llvm::endianness::little); | ||||||||
| support::endian::write(BOS, P.Constants.Num32BitValues, | ||||||||
| support::endian::write(BOS, Constants.Num32BitValues, | ||||||||
| llvm::endianness::little); | ||||||||
| break; | ||||||||
| } break; | ||||||||
|
||||||||
| } break; | |
| break; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing break;?
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -274,27 +274,39 @@ void DXContainerWriter::writeParts(raw_ostream &OS) { | |||||||||
| RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset; | ||||||||||
|
|
||||||||||
| for (const auto &Param : P.RootSignature->Parameters) { | ||||||||||
| mcdxbc::RootParameter NewParam; | ||||||||||
| NewParam.Header = dxbc::RootParameterHeader{ | ||||||||||
| Param.Type, Param.Visibility, Param.Offset}; | ||||||||||
| auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility, | ||||||||||
| Param.Offset}; | ||||||||||
|
||||||||||
| auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility, | |
| Param.Offset}; | |
| dxbc::RootParameterHeader Header{Param.Type, Param.Visibility, | |
| Param.Offset}; |
bogner marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems clearer than the implicit cast to from the v1 to the v2 descriptor:
dxbc::RTS0::v2::RootDescriptor Descriptor;
Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
if (RS.Version > 1)
Descriptor.Flags = Param.Descriptor.getEncodedFlags();
RS.ParametersContainer.addParameter(Header, Descriptor);aside: We should probably make the v2::RootDescriptor constructor that takes a v1::RootDescriptor explicit - this implicit cast being legal seems dangerous
spall marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, but do you think it's worth adding a addUnknownParameter or addInvalidParameter method instead of calling addInfo here directly for clarity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks that is a good idea. It can make the code more consistent. And reduce a little of the confusion regarding this edge case.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -75,31 +75,32 @@ static bool parseRootConstants(LLVMContext *Ctx, mcdxbc::RootSignatureDesc &RSD, | |||||||
| if (RootConstantNode->getNumOperands() != 5) | ||||||||
| return reportError(Ctx, "Invalid format for RootConstants Element"); | ||||||||
|
|
||||||||
| mcdxbc::RootParameter NewParameter; | ||||||||
| NewParameter.Header.ParameterType = | ||||||||
| dxbc::RootParameterHeader Header; | ||||||||
| Header.ParameterType = | ||||||||
| llvm::to_underlying(dxbc::RootParameterType::Constants32Bit); | ||||||||
|
|
||||||||
| if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1)) | ||||||||
| NewParameter.Header.ShaderVisibility = *Val; | ||||||||
| Header.ShaderVisibility = *Val; | ||||||||
| else | ||||||||
| return reportError(Ctx, "Invalid value for ShaderVisibility"); | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to initialized There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like a bug when the structure is created here but then only partially initialized. We should probably have a comment here, or even write a zero to it with a comment that it will be overwritten later. |
||||||||
|
|
||||||||
| dxbc::RootConstants Constants; | ||||||||
| if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2)) | ||||||||
| NewParameter.Constants.ShaderRegister = *Val; | ||||||||
| Constants.ShaderRegister = *Val; | ||||||||
| else | ||||||||
| return reportError(Ctx, "Invalid value for ShaderRegister"); | ||||||||
|
|
||||||||
| if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 3)) | ||||||||
| NewParameter.Constants.RegisterSpace = *Val; | ||||||||
| Constants.RegisterSpace = *Val; | ||||||||
| else | ||||||||
| return reportError(Ctx, "Invalid value for RegisterSpace"); | ||||||||
|
|
||||||||
| if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 4)) | ||||||||
| NewParameter.Constants.Num32BitValues = *Val; | ||||||||
| Constants.Num32BitValues = *Val; | ||||||||
| else | ||||||||
| return reportError(Ctx, "Invalid value for Num32BitValues"); | ||||||||
|
|
||||||||
| RSD.Parameters.push_back(NewParameter); | ||||||||
| RSD.ParametersContainer.addParameter(Header, Constants); | ||||||||
|
|
||||||||
| return false; | ||||||||
| } | ||||||||
|
|
@@ -164,12 +165,12 @@ static bool validate(LLVMContext *Ctx, const mcdxbc::RootSignatureDesc &RSD) { | |||||||
| return reportValueError(Ctx, "RootFlags", RSD.Flags); | ||||||||
| } | ||||||||
|
|
||||||||
| for (const mcdxbc::RootParameter &P : RSD.Parameters) { | ||||||||
| if (!dxbc::isValidShaderVisibility(P.Header.ShaderVisibility)) | ||||||||
| for (const llvm::mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) { | ||||||||
| if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility)) | ||||||||
| return reportValueError(Ctx, "ShaderVisibility", | ||||||||
| P.Header.ShaderVisibility); | ||||||||
| Info.Header.ShaderVisibility); | ||||||||
|
|
||||||||
| assert(dxbc::isValidParameterType(P.Header.ParameterType) && | ||||||||
| assert(dxbc::isValidParameterType(Info.Header.ParameterType) && | ||||||||
| "Invalid value for ParameterType"); | ||||||||
| } | ||||||||
|
|
||||||||
|
|
@@ -287,34 +288,40 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, | |||||||
| OS << indent(Space) << "Version: " << RS.Version << "\n"; | ||||||||
| OS << indent(Space) << "RootParametersOffset: " << RS.RootParameterOffset | ||||||||
| << "\n"; | ||||||||
| OS << indent(Space) << "NumParameters: " << RS.Parameters.size() << "\n"; | ||||||||
| OS << indent(Space) << "NumParameters: " << RS.ParametersContainer.size() | ||||||||
| << "\n"; | ||||||||
| Space++; | ||||||||
| for (auto const &P : RS.Parameters) { | ||||||||
| OS << indent(Space) << "- Parameter Type: " << P.Header.ParameterType | ||||||||
| << "\n"; | ||||||||
| for (size_t I = 0; I < RS.ParametersContainer.size(); I++) { | ||||||||
| const auto &[Type, Loc] = | ||||||||
| RS.ParametersContainer.getTypeAndLocForParameter(I); | ||||||||
| const dxbc::RootParameterHeader Header = | ||||||||
| RS.ParametersContainer.getHeader(I); | ||||||||
|
|
||||||||
| OS << indent(Space) << "- Parameter Type: " << Type << "\n"; | ||||||||
| OS << indent(Space + 2) | ||||||||
| << "Shader Visibility: " << P.Header.ShaderVisibility << "\n"; | ||||||||
| switch (P.Header.ParameterType) { | ||||||||
| case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): | ||||||||
| OS << indent(Space + 2) | ||||||||
| << "Register Space: " << P.Constants.RegisterSpace << "\n"; | ||||||||
| << "Shader Visibility: " << Header.ShaderVisibility << "\n"; | ||||||||
|
|
||||||||
| switch (Type) { | ||||||||
| case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit): { | ||||||||
| auto Constants = RS.ParametersContainer.getConstant(Loc); | ||||||||
|
||||||||
| auto Constants = RS.ParametersContainer.getConstant(Loc); | |
| const dxbc::RootConstants &Constants = | |
| RS.ParametersContainer.getConstant(Loc); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clearer to call these
HeaderandLocationrather than abbreviating here