From 9e475f6c4140d1a40b556d0f4612c94806357d93 Mon Sep 17 00:00:00 2001 From: Rex Xu Date: Mon, 11 Dec 2023 17:36:33 +0800 Subject: [PATCH] Port the upstream change to optimize adding decorations This is to port the upstream change: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/d56378e. It will greatly speed up the compilation time when there are lots of decorations in SPIR-V. --- ...onGroup_TestGroupAndGroupMember_lit.spvasm | 12 ++--- .../lib/SPIRV/libSPIRV/SPIRVDecorate.cpp | 47 +------------------ .../lib/SPIRV/libSPIRV/SPIRVDecorate.h | 37 +++------------ .../lib/SPIRV/libSPIRV/SPIRVEntry.cpp | 4 +- .../lib/SPIRV/libSPIRV/SPIRVEntry.h | 4 +- .../lib/SPIRV/libSPIRV/SPIRVModule.cpp | 16 ++----- .../lib/SPIRV/libSPIRV/SPIRVModule.h | 2 +- 7 files changed, 24 insertions(+), 98 deletions(-) diff --git a/llpc/test/shaderdb/core/OpDecorationGroup_TestGroupAndGroupMember_lit.spvasm b/llpc/test/shaderdb/core/OpDecorationGroup_TestGroupAndGroupMember_lit.spvasm index 4571be2480..a65d7377b3 100644 --- a/llpc/test/shaderdb/core/OpDecorationGroup_TestGroupAndGroupMember_lit.spvasm +++ b/llpc/test/shaderdb/core/OpDecorationGroup_TestGroupAndGroupMember_lit.spvasm @@ -1,12 +1,12 @@ ; BEGIN_SHADERTEST ; RUN: amdllpc -v %gfxip %s | FileCheck -check-prefix=SHADERTEST %s ; SHADERTEST-LABEL: {{^// LLPC}} SPIRV-to-LLVM translation results -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} -; SHADERTEST: %{{[0-9]+}} = getelementptr <{ [4294967295 x float] }>, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} +; SHADERTEST: %{{[0-9]+}} = getelementptr { [4294967295 x float] }, ptr addrspace(7) @{{.*}}, i32 0, i32 0, i32 %{{[0-9]+}} ; SHADERTEST: AMDLLPC SUCCESS ; END_SHADERTEST diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.cpp b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.cpp index 5dcbd7bf96..6e20ad8a82 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.cpp +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.cpp @@ -119,7 +119,7 @@ void SPIRVGroupDecorate::decorateTargets() { auto Target = getOrCreate(I); for (auto &Dec : DecorationGroup->getDecorations()) { assert(Dec->isDecorate()); - Target->addDecorate(static_cast(Dec)); + Target->addDecorate(static_cast(Dec)); } } } @@ -164,49 +164,4 @@ void SPIRVDecorateId::decode(std::istream &I) { getOrCreateTarget()->addDecorate(this); } -bool SPIRVDecorateGeneric::Comparator::operator()(const SPIRVDecorateGeneric *A, const SPIRVDecorateGeneric *B) const { - auto Action = [=]() { - if (A->getOpCode() < B->getOpCode()) - return true; - if (A->getOpCode() > B->getOpCode()) - return false; - if (A->getDecorateKind() < B->getDecorateKind()) - return true; - if (A->getDecorateKind() > B->getDecorateKind()) - return false; - if (A->getLiteralCount() < B->getLiteralCount()) - return true; - if (A->getLiteralCount() > B->getLiteralCount()) - return false; - for (size_t I = 0, E = A->getLiteralCount(); I != E; ++I) { - auto EA = A->getLiteral(I); - auto EB = B->getLiteral(I); - if (EA < EB) - return true; - if (EA > EB) - return false; - } - return false; - }; - auto Res = Action(); - return Res; -} - -bool operator==(const SPIRVDecorateGeneric &A, const SPIRVDecorateGeneric &B) { - if (A.getTargetId() != B.getTargetId()) - return false; - if (A.getOpCode() != B.getOpCode()) - return false; - if (A.getDecorateKind() != B.getDecorateKind()) - return false; - if (A.getLiteralCount() != B.getLiteralCount()) - return false; - for (size_t I = 0, E = A.getLiteralCount(); I != E; ++I) { - auto EA = A.getLiteral(I); - auto EB = B.getLiteral(I); - if (EA != EB) - return false; - } - return true; -} } // namespace SPIRV diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.h b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.h index 97325dc5bf..1c9b800e0a 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.h +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVDecorate.h @@ -66,12 +66,6 @@ class SPIRVDecorateGeneric : public SPIRVAnnotationGeneric { } Decoration getDecorateKind() const; size_t getLiteralCount() const; - /// Compare for kind and literal only. - struct Comparator { - bool operator()(const SPIRVDecorateGeneric *A, const SPIRVDecorateGeneric *B) const; - }; - /// Compare kind, literals and target. - friend bool operator==(const SPIRVDecorateGeneric &A, const SPIRVDecorateGeneric &B); SPIRVDecorationGroup *getOwner() const { return Owner; } @@ -96,18 +90,7 @@ class SPIRVDecorateGeneric : public SPIRVAnnotationGeneric { SPIRVDecorationGroup *Owner; // Owning decorate group }; -class SPIRVDecorateSet : public std::multiset { -public: - typedef std::multiset BaseType; - iterator insert(const value_type &Dec) { - auto ER = BaseType::equal_range(Dec); - for (auto I = ER.first, E = ER.second; I != E; ++I) { - if (**I == *Dec) - return I; - } - return BaseType::insert(Dec); - } -}; +typedef std::vector SPIRVDecorateVec; class SPIRVDecorate : public SPIRVDecorateGeneric { public: @@ -205,23 +188,17 @@ class SPIRVDecorationGroup : public SPIRVEntry { SPIRVDecorationGroup() : SPIRVEntry(OC) {} _SPIRV_DCL_DECODE // Move the given decorates to the decoration group - void takeDecorates(SPIRVDecorateSet &Decs) { - for (auto &I : Decs) { - // Insert decorates whose target ID is this decoration group - if (I->getTargetId() == Id) { - const_cast(I)->setOwner(this); - Decorations.insert(I); - } - } - // Remove those inserted decorates from original set + void takeDecorates(SPIRVDecorateVec &Decs) { + Decorations = std::move(Decs); for (auto &I : Decorations) - Decs.erase(I); + const_cast(I)->setOwner(this); + Decs.clear(); } - SPIRVDecorateSet &getDecorations() { return Decorations; } + SPIRVDecorateVec &getDecorations() { return Decorations; } protected: - SPIRVDecorateSet Decorations; + SPIRVDecorateVec Decorations; void validate() const override { assert(OpCode == OC); assert(WordCount == WC); diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.cpp b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.cpp index 91e2674bb1..778db418d0 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.cpp +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.cpp @@ -201,7 +201,7 @@ void SPIRVEntry::validateBuiltin(SPIRVWord TheSet, SPIRVWord Index) const { assert(TheSet != SPIRVWORD_MAX && Index != SPIRVWORD_MAX && "Invalid builtin"); } -void SPIRVEntry::addDecorate(const SPIRVDecorate *Dec) { +void SPIRVEntry::addDecorate(SPIRVDecorate *Dec) { auto Kind = Dec->getDecorateKind(); Decorates.insert(std::make_pair(Dec->getDecorateKind(), Dec)); Module->addDecorate(Dec); @@ -232,7 +232,7 @@ void SPIRVEntry::setLine(const SPIRVLine *L) { Line = L; } -void SPIRVEntry::addMemberDecorate(const SPIRVMemberDecorate *Dec) { +void SPIRVEntry::addMemberDecorate(SPIRVMemberDecorate *Dec) { assert(Dec); assert(canHaveMemberDecorates()); MemberDecorates[Dec->getPair()] = Dec; diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.h b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.h index ffbd0fd03f..6d35557df8 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.h +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVEntry.h @@ -237,11 +237,11 @@ class SPIRVEntry { return false; } - void addDecorate(const SPIRVDecorate *); + void addDecorate(SPIRVDecorate *); void addDecorate(Decoration Kind); void addDecorate(Decoration Kind, SPIRVWord Literal); void eraseDecorate(Decoration); - void addMemberDecorate(const SPIRVMemberDecorate *); + void addMemberDecorate(SPIRVMemberDecorate *); void addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind); void addMemberDecorate(SPIRVWord MemberNumber, Decoration Kind, SPIRVWord Literal); void eraseMemberDecorate(SPIRVWord MemberNumber, Decoration Kind); diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.cpp b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.cpp index 6c3dc8c800..062feca63e 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.cpp +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.cpp @@ -172,7 +172,7 @@ class SPIRVModuleImpl : public SPIRVModule { void setCurrentLine(const SPIRVLine *Line) override; void addCapability(SPIRVCapabilityKind) override; void addCapabilityInternal(SPIRVCapabilityKind) override; - const SPIRVDecorateGeneric *addDecorate(const SPIRVDecorateGeneric *) override; + const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) override; SPIRVDecorationGroup *addDecorationGroup() override; SPIRVDecorationGroup *addDecorationGroup(SPIRVDecorationGroup *Group) override; SPIRVGroupDecorate *addGroupDecorate(SPIRVDecorationGroup *Group, const std::vector &Targets) override; @@ -322,7 +322,7 @@ class SPIRVModuleImpl : public SPIRVModule { SPIRVStringVec StringVec; SPIRVMemberNameVec MemberNameVec; const SPIRVLine *CurrentLine; - SPIRVDecorateSet DecorateSet; + SPIRVDecorateVec DecorateVec; SPIRVDecGroupVec DecGroupVec; SPIRVGroupDecVec GroupDecVec; SPIRVEnetryPointVec EntryPointVec; @@ -702,12 +702,12 @@ SPIRVBasicBlock *SPIRVModuleImpl::addBasicBlock(SPIRVFunction *Func, SPIRVId Id) return Func->addBasicBlock(new SPIRVBasicBlock(getId(Id), Func)); } -const SPIRVDecorateGeneric *SPIRVModuleImpl::addDecorate(const SPIRVDecorateGeneric *Dec) { +const SPIRVDecorateGeneric *SPIRVModuleImpl::addDecorate(SPIRVDecorateGeneric *Dec) { SPIRVEntry *Target = nullptr; assert(exist(Dec->getTargetId(), &Target) && "Decorate target does not exist"); (void)Target; if (!Dec->getOwner()) - DecorateSet.insert(Dec); + DecorateVec.push_back(Dec); addCapabilities(Dec->getRequiredCapability()); return Dec; } @@ -999,12 +999,6 @@ SPIRVInstruction *SPIRVModuleImpl::addVariable(SPIRVType *Type, bool IsConstant, return Variable; } -template spv_ostream &operator<<(spv_ostream &O, const std::multiset &V) { - for (auto &I : V) - O << *I; - return O; -} - template void SPIRVModuleImpl::addTo(std::vector &V, SPIRVEntry *E) { V.push_back(static_cast(E)); } @@ -1018,7 +1012,7 @@ SPIRVDecorationGroup *SPIRVModuleImpl::addDecorationGroup() { SPIRVDecorationGroup *SPIRVModuleImpl::addDecorationGroup(SPIRVDecorationGroup *Group) { add(Group); - Group->takeDecorates(DecorateSet); + Group->takeDecorates(DecorateVec); DecGroupVec.push_back(Group); return Group; } diff --git a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.h b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.h index 56afc972ae..afa46ef3f9 100644 --- a/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.h +++ b/llpc/translator/lib/SPIRV/libSPIRV/SPIRVModule.h @@ -167,7 +167,7 @@ class SPIRVModule { virtual void addUnknownStructField(SPIRVTypeStruct *, unsigned Idx, SPIRVId Id) = 0; virtual const SPIRVLine *getCurrentLine() const = 0; virtual void setCurrentLine(const SPIRVLine *) = 0; - virtual const SPIRVDecorateGeneric *addDecorate(const SPIRVDecorateGeneric *) = 0; + virtual const SPIRVDecorateGeneric *addDecorate(SPIRVDecorateGeneric *) = 0; virtual SPIRVDecorationGroup *addDecorationGroup() = 0; virtual SPIRVDecorationGroup *addDecorationGroup(SPIRVDecorationGroup *Group) = 0; virtual SPIRVGroupDecorate *addGroupDecorate(SPIRVDecorationGroup *Group,