From d4af2a966b6fbe88b486df84504812c26fcafdd0 Mon Sep 17 00:00:00 2001 From: Justin Bogner Date: Thu, 25 Jul 2024 22:54:12 -0700 Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20change?= =?UTF-8?q?s=20to=20main=20this=20commit=20is=20based=20on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5-bogner [skip ci] --- llvm/include/llvm/Analysis/DXILResource.h | 69 ++++++++++++----- llvm/lib/Analysis/DXILResource.cpp | 77 +++++++------------ .../Target/DirectX/DXILResourceAnalysis.cpp | 16 ---- .../lib/Target/DirectX/DXILResourceAnalysis.h | 20 ----- .../Target/DirectX/DirectXPassRegistry.def | 2 - llvm/unittests/Analysis/DXILResourceTest.cpp | 13 ++++ 6 files changed, 92 insertions(+), 105 deletions(-) diff --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h index d4006ae10837c..0fad598d416ec 100644 --- a/llvm/include/llvm/Analysis/DXILResource.h +++ b/llvm/include/llvm/Analysis/DXILResource.h @@ -47,7 +47,7 @@ class ResourceInfo { struct StructInfo { uint32_t Stride; - Align Alignment; + MaybeAlign Alignment; bool operator==(const StructInfo &RHS) const { return std::tie(Stride, Alignment) == std::tie(RHS.Stride, RHS.Alignment); @@ -106,6 +106,11 @@ class ResourceInfo { MSInfo MultiSample; +public: + ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol, + StringRef Name) + : Symbol(Symbol), Name(Name), RC(RC), Kind(Kind) {} + // Conditions to check before accessing union members. bool isUAV() const; bool isCBuffer() const; @@ -115,17 +120,53 @@ class ResourceInfo { bool isFeedback() const; bool isMultiSample() const; - ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol, - StringRef Name) - : Symbol(Symbol), Name(Name), RC(RC), Kind(Kind) {} + void bind(uint32_t UniqueID, uint32_t Space, uint32_t LowerBound, + uint32_t Size) { + Binding.UniqueID = UniqueID; + Binding.Space = Space; + Binding.LowerBound = LowerBound; + Binding.Size = Size; + } + void setUAV(bool GloballyCoherent, bool HasCounter, bool IsROV) { + assert(isUAV() && "Not a UAV"); + UAVFlags.GloballyCoherent = GloballyCoherent; + UAVFlags.HasCounter = HasCounter; + UAVFlags.IsROV = IsROV; + } + void setCBuffer(uint32_t Size) { + assert(isCBuffer() && "Not a CBuffer"); + CBufferSize = Size; + } + void setSampler(dxil::SamplerType Ty) { + SamplerTy = Ty; + } + void setStruct(uint32_t Stride, MaybeAlign Alignment) { + assert(isStruct() && "Not a Struct"); + Struct.Stride = Stride; + Struct.Alignment = Alignment; + } + void setTyped(dxil::ElementType ElementTy, uint32_t ElementCount) { + assert(isTyped() && "Not Typed"); + Typed.ElementTy = ElementTy; + Typed.ElementCount = ElementCount; + } + void setFeedback(dxil::SamplerFeedbackType Type) { + assert(isFeedback() && "Not Feedback"); + Feedback.Type = Type; + } + void setMultiSample(uint32_t Count) { + assert(isMultiSample() && "Not MultiSampled"); + MultiSample.Count = Count; + } + + bool operator==(const ResourceInfo &RHS) const; -public: static ResourceInfo SRV(Value *Symbol, StringRef Name, dxil::ElementType ElementTy, uint32_t ElementCount, dxil::ResourceKind Kind); static ResourceInfo RawBuffer(Value *Symbol, StringRef Name); static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name, - uint32_t Stride, Align Alignment); + uint32_t Stride, MaybeAlign Alignment); static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name, dxil::ElementType ElementTy, uint32_t ElementCount, uint32_t SampleCount); @@ -141,9 +182,9 @@ class ResourceInfo { static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name, bool GloballyCoherent, bool IsROV); static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name, - uint32_t Stride, - Align Alignment, bool GloballyCoherent, - bool IsROV, bool HasCounter); + uint32_t Stride, MaybeAlign Alignment, + bool GloballyCoherent, bool IsROV, + bool HasCounter); static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name, dxil::ElementType ElementTy, uint32_t ElementCount, uint32_t SampleCount, @@ -164,16 +205,6 @@ class ResourceInfo { static ResourceInfo Sampler(Value *Symbol, StringRef Name, dxil::SamplerType SamplerTy); - void bind(uint32_t UniqueID, uint32_t Space, uint32_t LowerBound, - uint32_t Size) { - Binding.UniqueID = UniqueID; - Binding.Space = Space; - Binding.LowerBound = LowerBound; - Binding.Size = Size; - } - - bool operator==(const ResourceInfo &RHS) const; - MDTuple *getAsMetadata(LLVMContext &Ctx) const; ResourceBinding getBinding() const { return Binding; } diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp index 72cba9d4373bb..d49fed7f72465 100644 --- a/llvm/lib/Analysis/DXILResource.cpp +++ b/llvm/lib/Analysis/DXILResource.cpp @@ -69,8 +69,7 @@ ResourceInfo ResourceInfo::SRV(Value *Symbol, StringRef Name, ResourceInfo RI(ResourceClass::SRV, Kind, Symbol, Name); assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) && "Invalid ResourceKind for SRV constructor."); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; + RI.setTyped(ElementTy, ElementCount); return RI; } @@ -80,11 +79,11 @@ ResourceInfo ResourceInfo::RawBuffer(Value *Symbol, StringRef Name) { } ResourceInfo ResourceInfo::StructuredBuffer(Value *Symbol, StringRef Name, - uint32_t Stride, Align Alignment) { + uint32_t Stride, + MaybeAlign Alignment) { ResourceInfo RI(ResourceClass::SRV, ResourceKind::StructuredBuffer, Symbol, Name); - RI.Struct.Stride = Stride; - RI.Struct.Alignment = Alignment; + RI.setStruct(Stride, Alignment); return RI; } @@ -93,9 +92,8 @@ ResourceInfo ResourceInfo::Texture2DMS(Value *Symbol, StringRef Name, uint32_t ElementCount, uint32_t SampleCount) { ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMS, Symbol, Name); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; - RI.MultiSample.Count = SampleCount; + RI.setTyped(ElementTy, ElementCount); + RI.setMultiSample(SampleCount); return RI; } @@ -105,9 +103,8 @@ ResourceInfo ResourceInfo::Texture2DMSArray(Value *Symbol, StringRef Name, uint32_t SampleCount) { ResourceInfo RI(ResourceClass::SRV, ResourceKind::Texture2DMSArray, Symbol, Name); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; - RI.MultiSample.Count = SampleCount; + RI.setTyped(ElementTy, ElementCount); + RI.setMultiSample(SampleCount); return RI; } @@ -118,34 +115,27 @@ ResourceInfo ResourceInfo::UAV(Value *Symbol, StringRef Name, ResourceInfo RI(ResourceClass::UAV, Kind, Symbol, Name); assert(RI.isTyped() && !(RI.isStruct() || RI.isMultiSample()) && "Invalid ResourceKind for UAV constructor."); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; - RI.UAVFlags.GloballyCoherent = GloballyCoherent; - RI.UAVFlags.IsROV = IsROV; - RI.UAVFlags.HasCounter = false; + RI.setTyped(ElementTy, ElementCount); + RI.setUAV(GloballyCoherent, /*HasCounter=*/false, IsROV); return RI; } ResourceInfo ResourceInfo::RWRawBuffer(Value *Symbol, StringRef Name, bool GloballyCoherent, bool IsROV) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::RawBuffer, Symbol, Name); - RI.UAVFlags.GloballyCoherent = GloballyCoherent; - RI.UAVFlags.IsROV = IsROV; - RI.UAVFlags.HasCounter = false; + RI.setUAV(GloballyCoherent, /*HasCounter=*/false, IsROV); return RI; } ResourceInfo ResourceInfo::RWStructuredBuffer(Value *Symbol, StringRef Name, - uint32_t Stride, Align Alignment, + uint32_t Stride, + MaybeAlign Alignment, bool GloballyCoherent, bool IsROV, bool HasCounter) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::StructuredBuffer, Symbol, Name); - RI.Struct.Stride = Stride; - RI.Struct.Alignment = Alignment; - RI.UAVFlags.GloballyCoherent = GloballyCoherent; - RI.UAVFlags.IsROV = IsROV; - RI.UAVFlags.HasCounter = HasCounter; + RI.setStruct(Stride, Alignment); + RI.setUAV(GloballyCoherent, HasCounter, IsROV); return RI; } @@ -155,12 +145,9 @@ ResourceInfo ResourceInfo::RWTexture2DMS(Value *Symbol, StringRef Name, uint32_t SampleCount, bool GloballyCoherent) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMS, Symbol, Name); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; - RI.UAVFlags.GloballyCoherent = GloballyCoherent; - RI.UAVFlags.IsROV = false; - RI.UAVFlags.HasCounter = false; - RI.MultiSample.Count = SampleCount; + RI.setTyped(ElementTy, ElementCount); + RI.setUAV(GloballyCoherent, /*HasCounter=*/false, /*IsROV=*/false); + RI.setMultiSample(SampleCount); return RI; } @@ -171,12 +158,9 @@ ResourceInfo ResourceInfo::RWTexture2DMSArray(Value *Symbol, StringRef Name, bool GloballyCoherent) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::Texture2DMSArray, Symbol, Name); - RI.Typed.ElementTy = ElementTy; - RI.Typed.ElementCount = ElementCount; - RI.UAVFlags.GloballyCoherent = GloballyCoherent; - RI.UAVFlags.IsROV = false; - RI.UAVFlags.HasCounter = false; - RI.MultiSample.Count = SampleCount; + RI.setTyped(ElementTy, ElementCount); + RI.setUAV(GloballyCoherent, /*HasCounter=*/false, /*IsROV=*/false); + RI.setMultiSample(SampleCount); return RI; } @@ -184,10 +168,8 @@ ResourceInfo ResourceInfo::FeedbackTexture2D(Value *Symbol, StringRef Name, SamplerFeedbackType FeedbackTy) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2D, Symbol, Name); - RI.UAVFlags.GloballyCoherent = false; - RI.UAVFlags.IsROV = false; - RI.UAVFlags.HasCounter = false; - RI.Feedback.Type = FeedbackTy; + RI.setUAV(/*GloballyCoherent=*/false, /*HasCounter=*/false, /*IsROV=*/false); + RI.setFeedback(FeedbackTy); return RI; } @@ -196,24 +178,22 @@ ResourceInfo::FeedbackTexture2DArray(Value *Symbol, StringRef Name, SamplerFeedbackType FeedbackTy) { ResourceInfo RI(ResourceClass::UAV, ResourceKind::FeedbackTexture2DArray, Symbol, Name); - RI.UAVFlags.GloballyCoherent = false; - RI.UAVFlags.IsROV = false; - RI.UAVFlags.HasCounter = false; - RI.Feedback.Type = FeedbackTy; + RI.setUAV(/*GloballyCoherent=*/false, /*HasCounter=*/false, /*IsROV=*/false); + RI.setFeedback(FeedbackTy); return RI; } ResourceInfo ResourceInfo::CBuffer(Value *Symbol, StringRef Name, uint32_t Size) { ResourceInfo RI(ResourceClass::CBuffer, ResourceKind::CBuffer, Symbol, Name); - RI.CBufferSize = Size; + RI.setCBuffer(Size); return RI; } ResourceInfo ResourceInfo::Sampler(Value *Symbol, StringRef Name, SamplerType SamplerTy) { ResourceInfo RI(ResourceClass::Sampler, ResourceKind::Sampler, Symbol, Name); - RI.SamplerTy = SamplerTy; + RI.setSampler(SamplerTy); return RI; } @@ -306,7 +286,8 @@ MDTuple *ResourceInfo::getAsMetadata(LLVMContext &Ctx) const { std::pair ResourceInfo::getAnnotateProps() const { uint32_t ResourceKind = llvm::to_underlying(Kind); - uint32_t AlignLog2 = isStruct() ? Log2(Struct.Alignment) : 0; + uint32_t AlignLog2 = + isStruct() && Struct.Alignment ? Log2(*Struct.Alignment) : 0; bool IsUAV = isUAV(); bool IsROV = IsUAV && UAVFlags.IsROV; bool IsGloballyCoherent = IsUAV && UAVFlags.GloballyCoherent; diff --git a/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp b/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp index 0b2f0d827ebbc..35d750311ffd4 100644 --- a/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp +++ b/llvm/lib/Target/DirectX/DXILResourceAnalysis.cpp @@ -18,22 +18,6 @@ using namespace llvm; #define DEBUG_TYPE "dxil-resource-analysis" -dxil::Resources DXILResourceAnalysis::run(Module &M, - ModuleAnalysisManager &AM) { - dxil::Resources R; - R.collect(M); - return R; -} - -AnalysisKey DXILResourceAnalysis::Key; - -PreservedAnalyses DXILResourcePrinterPass::run(Module &M, - ModuleAnalysisManager &AM) { - dxil::Resources Res = AM.getResult(M); - Res.print(OS); - return PreservedAnalyses::all(); -} - char DXILResourceWrapper::ID = 0; INITIALIZE_PASS_BEGIN(DXILResourceWrapper, DEBUG_TYPE, "DXIL resource Information", true, true) diff --git a/llvm/lib/Target/DirectX/DXILResourceAnalysis.h b/llvm/lib/Target/DirectX/DXILResourceAnalysis.h index bce41160b95ec..aa66180b560be 100644 --- a/llvm/lib/Target/DirectX/DXILResourceAnalysis.h +++ b/llvm/lib/Target/DirectX/DXILResourceAnalysis.h @@ -19,26 +19,6 @@ #include namespace llvm { -/// Analysis pass that exposes the \c DXILResource for a module. -class DXILResourceAnalysis : public AnalysisInfoMixin { - friend AnalysisInfoMixin; - static AnalysisKey Key; - -public: - typedef dxil::Resources Result; - dxil::Resources run(Module &M, ModuleAnalysisManager &AM); -}; - -/// Printer pass for the \c DXILResourceAnalysis results. -class DXILResourcePrinterPass : public PassInfoMixin { - raw_ostream &OS; - -public: - explicit DXILResourcePrinterPass(raw_ostream &OS) : OS(OS) {} - PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); - static bool isRequired() { return true; } -}; - /// The legacy pass manager's analysis pass to compute DXIL resource /// information. class DXILResourceWrapper : public ModulePass { diff --git a/llvm/lib/Target/DirectX/DirectXPassRegistry.def b/llvm/lib/Target/DirectX/DirectXPassRegistry.def index 1b326d020d511..754f174b96052 100644 --- a/llvm/lib/Target/DirectX/DirectXPassRegistry.def +++ b/llvm/lib/Target/DirectX/DirectXPassRegistry.def @@ -17,7 +17,6 @@ #define MODULE_ANALYSIS(NAME, CREATE_PASS) #endif MODULE_ANALYSIS("dx-shader-flags", dxil::ShaderFlagsAnalysis()) -MODULE_ANALYSIS("dxil-resource", DXILResourceAnalysis()) #undef MODULE_ANALYSIS #ifndef MODULE_PASS @@ -25,5 +24,4 @@ MODULE_ANALYSIS("dxil-resource", DXILResourceAnalysis()) #endif // TODO: rename to print after NPM switch MODULE_PASS("print-dx-shader-flags", dxil::ShaderFlagsAnalysisPrinter(dbgs())) -MODULE_PASS("print-dxil-resource", DXILResourcePrinterPass(dbgs())) #undef MODULE_PASS diff --git a/llvm/unittests/Analysis/DXILResourceTest.cpp b/llvm/unittests/Analysis/DXILResourceTest.cpp index 554cbd0d8ded7..7bbb417097882 100644 --- a/llvm/unittests/Analysis/DXILResourceTest.cpp +++ b/llvm/unittests/Analysis/DXILResourceTest.cpp @@ -151,6 +151,19 @@ TEST(DXILResource, AnnotationsAndMetadata) { EXPECT_MDEQ( MD, TestMD.get(0, Symbol, "Buffer0", 0, 0, 1, 12, 0, TestMD.get(1, 16))); + // StructuredBuffer Buffer1 : register(t1); + Symbol = UndefValue::get(StructType::create( + Context, {Floatx3Ty}, "class.StructuredBuffer >")); + Resource = ResourceInfo::StructuredBuffer(Symbol, "Buffer1", + /*Stride=*/12, {}); + Resource.bind(1, 0, 1, 1); + Props = Resource.getAnnotateProps(); + EXPECT_EQ(Props.first, 0x0000000cU); + EXPECT_EQ(Props.second, 0x0000000cU); + MD = Resource.getAsMetadata(Context); + EXPECT_MDEQ( + MD, TestMD.get(1, Symbol, "Buffer1", 0, 1, 1, 12, 0, TestMD.get(1, 12))); + // Texture2D ColorMapTexture : register(t2); Symbol = UndefValue::get(StructType::create( Context, {Floatx4Ty}, "class.Texture2D >"));