diff options
Diffstat (limited to 'llvm/lib/Frontend/HLSL')
-rw-r--r-- | llvm/lib/Frontend/HLSL/HLSLBinding.cpp | 8 | ||||
-rw-r--r-- | llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp | 55 | ||||
-rw-r--r-- | llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp | 122 | ||||
-rw-r--r-- | llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp | 136 |
4 files changed, 72 insertions, 249 deletions
diff --git a/llvm/lib/Frontend/HLSL/HLSLBinding.cpp b/llvm/lib/Frontend/HLSL/HLSLBinding.cpp index d581311..401402f 100644 --- a/llvm/lib/Frontend/HLSL/HLSLBinding.cpp +++ b/llvm/lib/Frontend/HLSL/HLSLBinding.cpp @@ -76,7 +76,7 @@ BindingInfo BindingInfoBuilder::calculateBindingInfo( // remove duplicates Binding *NewEnd = llvm::unique(Bindings); if (NewEnd != Bindings.end()) - Bindings.erase(NewEnd); + Bindings.erase(NewEnd, Bindings.end()); BindingInfo Info; @@ -131,9 +131,9 @@ BindingInfo BindingInfoBuilder::calculateBindingInfo( return Info; } -const BindingInfoBuilder::Binding &BindingInfoBuilder::findOverlapping( - const BindingInfoBuilder::Binding &ReportedBinding) const { - for (const BindingInfoBuilder::Binding &Other : Bindings) +const Binding & +BindingInfoBuilder::findOverlapping(const Binding &ReportedBinding) const { + for (const Binding &Other : Bindings) if (ReportedBinding.LowerBound <= Other.UpperBound && Other.LowerBound <= ReportedBinding.UpperBound) return Other; diff --git a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp index 78c20a6..92c62b8 100644 --- a/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp +++ b/llvm/lib/Frontend/HLSL/HLSLRootSignature.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Frontend/HLSL/HLSLRootSignature.h" +#include "llvm/Support/DXILABI.h" #include "llvm/Support/ScopedPrinter.h" namespace llvm { @@ -18,24 +19,6 @@ namespace hlsl { namespace rootsig { template <typename T> -static std::optional<StringRef> getEnumName(const T Value, - ArrayRef<EnumEntry<T>> Enums) { - for (const auto &EnumItem : Enums) - if (EnumItem.Value == Value) - return EnumItem.Name; - return std::nullopt; -} - -template <typename T> -static raw_ostream &printEnum(raw_ostream &OS, const T Value, - ArrayRef<EnumEntry<T>> Enums) { - auto MaybeName = getEnumName(Value, Enums); - if (MaybeName) - OS << *MaybeName; - return OS; -} - -template <typename T> static raw_ostream &printFlags(raw_ostream &OS, const T Value, ArrayRef<EnumEntry<T>> Flags) { bool FlagSet = false; @@ -46,9 +29,9 @@ static raw_ostream &printFlags(raw_ostream &OS, const T Value, if (FlagSet) OS << " | "; - auto MaybeFlag = getEnumName(T(Bit), Flags); - if (MaybeFlag) - OS << *MaybeFlag; + StringRef MaybeFlag = enumToStringRef(T(Bit), Flags); + if (!MaybeFlag.empty()) + OS << MaybeFlag; else OS << "invalid: " << Bit; @@ -70,58 +53,49 @@ static const EnumEntry<RegisterType> RegisterNames[] = { }; static raw_ostream &operator<<(raw_ostream &OS, const Register &Reg) { - printEnum(OS, Reg.ViewType, ArrayRef(RegisterNames)); - OS << Reg.Number; + OS << enumToStringRef(Reg.ViewType, ArrayRef(RegisterNames)) << Reg.Number; return OS; } static raw_ostream &operator<<(raw_ostream &OS, const llvm::dxbc::ShaderVisibility &Visibility) { - printEnum(OS, Visibility, dxbc::getShaderVisibility()); + OS << enumToStringRef(Visibility, dxbc::getShaderVisibility()); return OS; } static raw_ostream &operator<<(raw_ostream &OS, const llvm::dxbc::SamplerFilter &Filter) { - printEnum(OS, Filter, dxbc::getSamplerFilters()); + OS << enumToStringRef(Filter, dxbc::getSamplerFilters()); return OS; } static raw_ostream &operator<<(raw_ostream &OS, const dxbc::TextureAddressMode &Address) { - printEnum(OS, Address, dxbc::getTextureAddressModes()); + OS << enumToStringRef(Address, dxbc::getTextureAddressModes()); return OS; } static raw_ostream &operator<<(raw_ostream &OS, const dxbc::ComparisonFunc &CompFunc) { - printEnum(OS, CompFunc, dxbc::getComparisonFuncs()); + OS << enumToStringRef(CompFunc, dxbc::getComparisonFuncs()); return OS; } static raw_ostream &operator<<(raw_ostream &OS, const dxbc::StaticBorderColor &BorderColor) { - printEnum(OS, BorderColor, dxbc::getStaticBorderColors()); + OS << enumToStringRef(BorderColor, dxbc::getStaticBorderColors()); return OS; } -static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = { - {"CBV", dxil::ResourceClass::CBuffer}, - {"SRV", dxil::ResourceClass::SRV}, - {"UAV", dxil::ResourceClass::UAV}, - {"Sampler", dxil::ResourceClass::Sampler}, -}; - -static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) { - printEnum(OS, dxil::ResourceClass(llvm::to_underlying(Type)), - ArrayRef(ResourceClassNames)); - +static raw_ostream &operator<<(raw_ostream &OS, + const dxil::ResourceClass &Type) { + OS << dxil::getResourceClassName(Type); return OS; } @@ -179,8 +153,7 @@ raw_ostream &operator<<(raw_ostream &OS, const DescriptorTableClause &Clause) { } raw_ostream &operator<<(raw_ostream &OS, const RootDescriptor &Descriptor) { - ClauseType Type = ClauseType(llvm::to_underlying(Descriptor.Type)); - OS << "Root" << Type << "(" << Descriptor.Reg + OS << "Root" << Descriptor.Type << "(" << Descriptor.Reg << ", space = " << Descriptor.Space << ", visibility = " << Descriptor.Visibility << ", flags = " << Descriptor.Flags << ")"; diff --git a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp index 48ff1ca..a5a92cb 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp @@ -15,6 +15,7 @@ #include "llvm/Frontend/HLSL/RootSignatureValidations.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Metadata.h" +#include "llvm/Support/DXILABI.h" #include "llvm/Support/ScopedPrinter.h" using namespace llvm; @@ -51,18 +52,15 @@ static std::optional<StringRef> extractMdStringValue(MDNode *Node, return NodeText->getString(); } -static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = { - {"CBV", dxil::ResourceClass::CBuffer}, - {"SRV", dxil::ResourceClass::SRV}, - {"UAV", dxil::ResourceClass::UAV}, - {"Sampler", dxil::ResourceClass::Sampler}, -}; - -static std::optional<StringRef> getResourceName(dxil::ResourceClass Class) { - for (const auto &ClassEnum : ResourceClassNames) - if (ClassEnum.Value == Class) - return ClassEnum.Name; - return std::nullopt; +static Expected<dxbc::ShaderVisibility> +extractShaderVisibility(MDNode *Node, unsigned int OpId) { + if (std::optional<uint32_t> Val = extractMdIntValue(Node, OpId)) { + if (!dxbc::isValidShaderVisibility(*Val)) + return make_error<RootSignatureValidationError<uint32_t>>( + "ShaderVisibility", *Val); + return dxbc::ShaderVisibility(*Val); + } + return make_error<InvalidRSMetadataValue>("ShaderVisibility"); } namespace { @@ -133,10 +131,9 @@ MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) { MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) { IRBuilder<> Builder(Ctx); - std::optional<StringRef> ResName = - getResourceName(dxil::ResourceClass(to_underlying(Descriptor.Type))); - assert(ResName && "Provided an invalid Resource Class"); - SmallString<7> Name({"Root", *ResName}); + StringRef ResName = dxil::getResourceClassName(Descriptor.Type); + assert(!ResName.empty() && "Provided an invalid Resource Class"); + SmallString<7> Name({"Root", ResName}); Metadata *Operands[] = { MDString::get(Ctx, Name), ConstantAsMetadata::get( @@ -174,11 +171,10 @@ MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) { MDNode *MetadataBuilder::BuildDescriptorTableClause( const DescriptorTableClause &Clause) { IRBuilder<> Builder(Ctx); - std::optional<StringRef> ResName = - getResourceName(dxil::ResourceClass(to_underlying(Clause.Type))); - assert(ResName && "Provided an invalid Resource Class"); + StringRef ResName = dxil::getResourceClassName(Clause.Type); + assert(!ResName.empty() && "Provided an invalid Resource Class"); Metadata *Operands[] = { - MDString::get(Ctx, *ResName), + MDString::get(Ctx, ResName), ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)), ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)), ConstantAsMetadata::get(Builder.getInt32(Clause.Space)), @@ -236,17 +232,12 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, if (RootConstantNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("RootConstants Element"); - dxbc::RTS0::v1::RootParameterHeader Header; - // The parameter offset doesn't matter here - we recalculate it during - // serialization Header.ParameterOffset = 0; - Header.ParameterType = to_underlying(dxbc::RootParameterType::Constants32Bit); + Expected<dxbc::ShaderVisibility> Visibility = + extractShaderVisibility(RootConstantNode, 1); + if (auto E = Visibility.takeError()) + return Error(std::move(E)); - if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); - - dxbc::RTS0::v1::RootConstants Constants; + mcdxbc::RootConstants Constants; if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2)) Constants.ShaderRegister = *Val; else @@ -262,7 +253,8 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, else return make_error<InvalidRSMetadataValue>("Num32BitValues"); - RSD.ParametersContainer.addParameter(Header, Constants); + RSD.ParametersContainer.addParameter(dxbc::RootParameterType::Constants32Bit, + *Visibility, Constants); return Error::success(); } @@ -270,36 +262,36 @@ Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD, Error MetadataParser::parseRootDescriptors( mcdxbc::RootSignatureDesc &RSD, MDNode *RootDescriptorNode, RootSignatureElementKind ElementKind) { - assert(ElementKind == RootSignatureElementKind::SRV || - ElementKind == RootSignatureElementKind::UAV || - ElementKind == RootSignatureElementKind::CBV && - "parseRootDescriptors should only be called with RootDescriptor " - "element kind."); + assert((ElementKind == RootSignatureElementKind::SRV || + ElementKind == RootSignatureElementKind::UAV || + ElementKind == RootSignatureElementKind::CBV) && + "parseRootDescriptors should only be called with RootDescriptor " + "element kind."); if (RootDescriptorNode->getNumOperands() != 5) return make_error<InvalidRSMetadataFormat>("Root Descriptor Element"); - dxbc::RTS0::v1::RootParameterHeader Header; + dxbc::RootParameterType Type; switch (ElementKind) { case RootSignatureElementKind::SRV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::SRV); + Type = dxbc::RootParameterType::SRV; break; case RootSignatureElementKind::UAV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::UAV); + Type = dxbc::RootParameterType::UAV; break; case RootSignatureElementKind::CBV: - Header.ParameterType = to_underlying(dxbc::RootParameterType::CBV); + Type = dxbc::RootParameterType::CBV; break; default: llvm_unreachable("invalid Root Descriptor kind"); break; } - if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); + Expected<dxbc::ShaderVisibility> Visibility = + extractShaderVisibility(RootDescriptorNode, 1); + if (auto E = Visibility.takeError()) + return Error(std::move(E)); - dxbc::RTS0::v2::RootDescriptor Descriptor; + mcdxbc::RootDescriptor Descriptor; if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2)) Descriptor.ShaderRegister = *Val; else @@ -311,7 +303,7 @@ Error MetadataParser::parseRootDescriptors( return make_error<InvalidRSMetadataValue>("RegisterSpace"); if (RSD.Version == 1) { - RSD.ParametersContainer.addParameter(Header, Descriptor); + RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor); return Error::success(); } assert(RSD.Version > 1); @@ -321,7 +313,7 @@ Error MetadataParser::parseRootDescriptors( else return make_error<InvalidRSMetadataValue>("Root Descriptor Flags"); - RSD.ParametersContainer.addParameter(Header, Descriptor); + RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor); return Error::success(); } @@ -387,15 +379,12 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, if (NumOperands < 2) return make_error<InvalidRSMetadataFormat>("Descriptor Table"); - dxbc::RTS0::v1::RootParameterHeader Header; - if (std::optional<uint32_t> Val = extractMdIntValue(DescriptorTableNode, 1)) - Header.ShaderVisibility = *Val; - else - return make_error<InvalidRSMetadataValue>("ShaderVisibility"); + Expected<dxbc::ShaderVisibility> Visibility = + extractShaderVisibility(DescriptorTableNode, 1); + if (auto E = Visibility.takeError()) + return Error(std::move(E)); mcdxbc::DescriptorTable Table; - Header.ParameterType = - to_underlying(dxbc::RootParameterType::DescriptorTable); for (unsigned int I = 2; I < NumOperands; I++) { MDNode *Element = dyn_cast<MDNode>(DescriptorTableNode->getOperand(I)); @@ -407,7 +396,8 @@ Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD, return Err; } - RSD.ParametersContainer.addParameter(Header, Table); + RSD.ParametersContainer.addParameter(dxbc::RootParameterType::DescriptorTable, + *Visibility, Table); return Error::success(); } @@ -543,21 +533,15 @@ Error MetadataParser::validateRootSignature( } for (const mcdxbc::RootParameterInfo &Info : RSD.ParametersContainer) { - if (!dxbc::isValidShaderVisibility(Info.Header.ShaderVisibility)) - DeferredErrs = - joinErrors(std::move(DeferredErrs), - make_error<RootSignatureValidationError<uint32_t>>( - "ShaderVisibility", Info.Header.ShaderVisibility)); - assert(dxbc::isValidParameterType(Info.Header.ParameterType) && - "Invalid value for ParameterType"); - - switch (Info.Header.ParameterType) { + switch (Info.Type) { + case dxbc::RootParameterType::Constants32Bit: + break; - case to_underlying(dxbc::RootParameterType::CBV): - case to_underlying(dxbc::RootParameterType::UAV): - case to_underlying(dxbc::RootParameterType::SRV): { - const dxbc::RTS0::v2::RootDescriptor &Descriptor = + case dxbc::RootParameterType::CBV: + case dxbc::RootParameterType::UAV: + case dxbc::RootParameterType::SRV: { + const mcdxbc::RootDescriptor &Descriptor = RSD.ParametersContainer.getRootDescriptor(Info.Location); if (!hlsl::rootsig::verifyRegisterValue(Descriptor.ShaderRegister)) DeferredErrs = @@ -581,7 +565,7 @@ Error MetadataParser::validateRootSignature( } break; } - case to_underlying(dxbc::RootParameterType::DescriptorTable): { + case dxbc::RootParameterType::DescriptorTable: { const mcdxbc::DescriptorTable &Table = RSD.ParametersContainer.getDescriptorTable(Info.Location); for (const dxbc::RTS0::v2::DescriptorRange &Range : Table) { diff --git a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp index f11c7d2..72308a3d 100644 --- a/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp +++ b/llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp @@ -29,7 +29,7 @@ bool verifyRegisterValue(uint32_t RegisterValue) { // This Range is reserverved, therefore invalid, according to the spec // https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#all-the-values-should-be-legal bool verifyRegisterSpace(uint32_t RegisterSpace) { - return !(RegisterSpace >= 0xFFFFFFF0 && RegisterSpace <= 0xFFFFFFFF); + return !(RegisterSpace >= 0xFFFFFFF0); } bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal) { @@ -180,140 +180,6 @@ bool verifyBorderColor(uint32_t BorderColor) { bool verifyLOD(float LOD) { return !std::isnan(LOD); } -std::optional<const RangeInfo *> -ResourceRange::getOverlapping(const RangeInfo &Info) const { - MapT::const_iterator Interval = Intervals.find(Info.LowerBound); - if (!Interval.valid() || Info.UpperBound < Interval.start()) - return std::nullopt; - return Interval.value(); -} - -const RangeInfo *ResourceRange::lookup(uint32_t X) const { - return Intervals.lookup(X, nullptr); -} - -void ResourceRange::clear() { return Intervals.clear(); } - -std::optional<const RangeInfo *> ResourceRange::insert(const RangeInfo &Info) { - uint32_t LowerBound = Info.LowerBound; - uint32_t UpperBound = Info.UpperBound; - - std::optional<const RangeInfo *> Res = std::nullopt; - MapT::iterator Interval = Intervals.begin(); - - while (true) { - if (UpperBound < LowerBound) - break; - - Interval.advanceTo(LowerBound); - if (!Interval.valid()) // No interval found - break; - - // Let Interval = [x;y] and [LowerBound;UpperBound] = [a;b] and note that - // a <= y implicitly from Intervals.find(LowerBound) - if (UpperBound < Interval.start()) - break; // found interval does not overlap with inserted one - - if (!Res.has_value()) // Update to be the first found intersection - Res = Interval.value(); - - if (Interval.start() <= LowerBound && UpperBound <= Interval.stop()) { - // x <= a <= b <= y implies that [a;b] is covered by [x;y] - // -> so we don't need to insert this, report an overlap - return Res; - } else if (LowerBound <= Interval.start() && - Interval.stop() <= UpperBound) { - // a <= x <= y <= b implies that [x;y] is covered by [a;b] - // -> so remove the existing interval that we will cover with the - // overwrite - Interval.erase(); - } else if (LowerBound < Interval.start() && UpperBound <= Interval.stop()) { - // a < x <= b <= y implies that [a; x] is not covered but [x;b] is - // -> so set b = x - 1 such that [a;x-1] is now the interval to insert - UpperBound = Interval.start() - 1; - } else if (Interval.start() <= LowerBound && Interval.stop() < UpperBound) { - // a < x <= b <= y implies that [y; b] is not covered but [a;y] is - // -> so set a = y + 1 such that [y+1;b] is now the interval to insert - LowerBound = Interval.stop() + 1; - } - } - - assert(LowerBound <= UpperBound && "Attempting to insert an empty interval"); - Intervals.insert(LowerBound, UpperBound, &Info); - return Res; -} - -llvm::SmallVector<OverlappingRanges> -findOverlappingRanges(ArrayRef<RangeInfo> Infos) { - // It is expected that Infos is filled with valid RangeInfos and that - // they are sorted with respect to the RangeInfo <operator - assert(llvm::is_sorted(Infos) && "Ranges must be sorted"); - - llvm::SmallVector<OverlappingRanges> Overlaps; - using GroupT = std::pair<dxil::ResourceClass, /*Space*/ uint32_t>; - - // First we will init our state to track: - if (Infos.size() == 0) - return Overlaps; // No ranges to overlap - GroupT CurGroup = {Infos[0].Class, Infos[0].Space}; - - // Create a ResourceRange for each Visibility - ResourceRange::MapT::Allocator Allocator; - std::array<ResourceRange, 8> Ranges = { - ResourceRange(Allocator), // All - ResourceRange(Allocator), // Vertex - ResourceRange(Allocator), // Hull - ResourceRange(Allocator), // Domain - ResourceRange(Allocator), // Geometry - ResourceRange(Allocator), // Pixel - ResourceRange(Allocator), // Amplification - ResourceRange(Allocator), // Mesh - }; - - // Reset the ResourceRanges for when we iterate through a new group - auto ClearRanges = [&Ranges]() { - for (ResourceRange &Range : Ranges) - Range.clear(); - }; - - // Iterate through collected RangeInfos - for (const RangeInfo &Info : Infos) { - GroupT InfoGroup = {Info.Class, Info.Space}; - // Reset our ResourceRanges when we enter a new group - if (CurGroup != InfoGroup) { - ClearRanges(); - CurGroup = InfoGroup; - } - - // Insert range info into corresponding Visibility ResourceRange - ResourceRange &VisRange = Ranges[llvm::to_underlying(Info.Visibility)]; - if (std::optional<const RangeInfo *> Overlapping = VisRange.insert(Info)) - Overlaps.push_back(OverlappingRanges(&Info, Overlapping.value())); - - // Check for overlap in all overlapping Visibility ResourceRanges - // - // If the range that we are inserting has ShaderVisiblity::All it needs to - // check for an overlap in all other visibility types as well. - // Otherwise, the range that is inserted needs to check that it does not - // overlap with ShaderVisibility::All. - // - // OverlapRanges will be an ArrayRef to all non-all visibility - // ResourceRanges in the former case and it will be an ArrayRef to just the - // all visiblity ResourceRange in the latter case. - ArrayRef<ResourceRange> OverlapRanges = - Info.Visibility == llvm::dxbc::ShaderVisibility::All - ? ArrayRef<ResourceRange>{Ranges}.drop_front() - : ArrayRef<ResourceRange>{Ranges}.take_front(); - - for (const ResourceRange &Range : OverlapRanges) - if (std::optional<const RangeInfo *> Overlapping = - Range.getOverlapping(Info)) - Overlaps.push_back(OverlappingRanges(&Info, Overlapping.value())); - } - - return Overlaps; -} - } // namespace rootsig } // namespace hlsl } // namespace llvm |