[llvm] 38e671a - [DXIL][Analysis] Use setters for dxil::ResourceInfo initialization. NFC

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 29 14:30:40 PDT 2024


Author: Justin Bogner
Date: 2024-07-29T14:30:37-07:00
New Revision: 38e671ab5221ec3d355fe4e3f70fdcd6f4809532

URL: https://github.com/llvm/llvm-project/commit/38e671ab5221ec3d355fe4e3f70fdcd6f4809532
DIFF: https://github.com/llvm/llvm-project/commit/38e671ab5221ec3d355fe4e3f70fdcd6f4809532.diff

LOG: [DXIL][Analysis] Use setters for dxil::ResourceInfo initialization. NFC

This simplifies making sure we set all of the members of the unions
and adds asserts to help catch if we do something wrong.

Pull Request: https://github.com/llvm/llvm-project/pull/100696

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/DXILResource.h
    llvm/lib/Analysis/DXILResource.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h
index d4006ae10837c..2ca0ad9a75319 100644
--- a/llvm/include/llvm/Analysis/DXILResource.h
+++ b/llvm/include/llvm/Analysis/DXILResource.h
@@ -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,11 +120,45 @@ 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, Align 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);
@@ -164,16 +203,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..1443fef8fc165 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;
 }
 
@@ -83,8 +82,7 @@ ResourceInfo ResourceInfo::StructuredBuffer(Value *Symbol, StringRef Name,
                                             uint32_t Stride, Align 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 +91,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 +102,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,20 +114,15 @@ 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;
 }
 
@@ -141,11 +132,8 @@ ResourceInfo ResourceInfo::RWStructuredBuffer(Value *Symbol, StringRef Name,
                                               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 +143,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 +156,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 +166,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 +176,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;
 }
 


        


More information about the llvm-commits mailing list