Browse Source

Merged PR 47: Remove duplicate resource usage for a function for arrays of resources

Remove duplicate resource usage for a function for arrays of resources
Young Kim 7 years ago
parent
commit
648b4aebd1

+ 26 - 16
lib/HLSL/DxilContainerAssembler.cpp

@@ -10,6 +10,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/SetVector.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Bitcode/ReaderWriter.h"
@@ -771,15 +772,21 @@ public:
 
 
 struct IndexTable : public RDATPart {
 struct IndexTable : public RDATPart {
 private:
 private:
-  std::vector<std::vector<uint32_t>> m_IndicesList;
+  typedef llvm::SmallVector<uint32_t, 8> Indices;
+  std::vector<Indices> m_IndicesList;
   uint32_t m_curOffset;
   uint32_t m_curOffset;
 
 
 public:
 public:
   IndexTable() : m_IndicesList(), m_curOffset(0) {}
   IndexTable() : m_IndicesList(), m_curOffset(0) {}
-  uint32_t AddIndex(const std::vector<uint32_t> &Indices) {
+  template <class iterator>
+  uint32_t AddIndex(iterator begin, iterator end) {
     uint32_t prevOffset = m_curOffset;
     uint32_t prevOffset = m_curOffset;
-    m_curOffset += Indices.size() + 1;
-    m_IndicesList.emplace_back(std::move(Indices));
+    m_IndicesList.emplace_back(Indices());
+    auto &curIndices = m_IndicesList.back();
+    for (iterator it = begin; it != end; ++it) {
+      curIndices.emplace_back(*it);
+    }
+    m_curOffset += curIndices.size() + 1;
     return prevOffset;
     return prevOffset;
   }
   }
 
 
@@ -797,7 +804,7 @@ public:
     for (auto Indices : m_IndicesList) {
     for (auto Indices : m_IndicesList) {
       uint32_t count = Indices.size();
       uint32_t count = Indices.size();
       memcpy(cur, &count, 4);
       memcpy(cur, &count, 4);
-      std::copy(Indices.data(), Indices.data() + Indices.size(), cur + 1);
+      std::copy(Indices.begin(), Indices.end(), cur + 1);
       cur += sizeof(uint32_t)/sizeof(4) + Indices.size();
       cur += sizeof(uint32_t)/sizeof(4) + Indices.size();
     }
     }
   }
   }
@@ -809,7 +816,8 @@ private:
   SmallVector<char, 1024> m_RDATBuffer;
   SmallVector<char, 1024> m_RDATBuffer;
 
 
   std::vector<std::unique_ptr<RDATPart>> m_tables;
   std::vector<std::unique_ptr<RDATPart>> m_tables;
-  typedef std::unordered_map<llvm::Function *, std::vector<uint32_t>> FunctionIndexMap;
+  typedef llvm::SmallSetVector<uint32_t, 8> Indices;
+  typedef std::unordered_map<llvm::Function *, Indices> FunctionIndexMap;
   FunctionIndexMap m_FuncToResNameOffset; // list of resources used
   FunctionIndexMap m_FuncToResNameOffset; // list of resources used
   FunctionIndexMap m_FuncToDependencies;  // list of unresolved functions used
   FunctionIndexMap m_FuncToDependencies;  // list of unresolved functions used
 
 
@@ -836,12 +844,10 @@ private:
         llvm::Function *F = FindUsingFunction(user);
         llvm::Function *F = FindUsingFunction(user);
         if (!F)
         if (!F)
           continue;
           continue;
-        if (m_FuncToResNameOffset.find(F) != m_FuncToResNameOffset.end()) {
-          m_FuncToResNameOffset[F].emplace_back(offset);
-        }
-        else {
-          m_FuncToResNameOffset[F] = std::vector<uint32_t>({offset});
+        if (m_FuncToResNameOffset.find(F) == m_FuncToResNameOffset.end()) {
+          m_FuncToResNameOffset[F] = Indices();
         }
         }
+        m_FuncToResNameOffset[F].insert(offset);
       }
       }
     }
     }
   }
   }
@@ -915,10 +921,9 @@ private:
       if (m_FuncToDependencies.find(userFunction) ==
       if (m_FuncToDependencies.find(userFunction) ==
           m_FuncToDependencies.end()) {
           m_FuncToDependencies.end()) {
         m_FuncToDependencies[userFunction] =
         m_FuncToDependencies[userFunction] =
-            std::vector<uint32_t>({index});
-      } else {
-        m_FuncToDependencies[userFunction].push_back(index);
+            Indices();
       }
       }
+      m_FuncToDependencies[userFunction].insert(index);
     }
     }
   }
   }
 
 
@@ -949,10 +954,15 @@ private:
         uint32_t payloadSizeInBytes = 0;
         uint32_t payloadSizeInBytes = 0;
         uint32_t attrSizeInBytes = 0;
         uint32_t attrSizeInBytes = 0;
         uint32_t shaderKind = (uint32_t)PSVShaderKind::Library;
         uint32_t shaderKind = (uint32_t)PSVShaderKind::Library;
+
         if (m_FuncToResNameOffset.find(&function) != m_FuncToResNameOffset.end())
         if (m_FuncToResNameOffset.find(&function) != m_FuncToResNameOffset.end())
-          resourceIndex = indexTable.AddIndex(m_FuncToResNameOffset[&function]);
+          resourceIndex =
+              indexTable.AddIndex(m_FuncToResNameOffset[&function].begin(),
+                                  m_FuncToResNameOffset[&function].end());
         if (m_FuncToDependencies.find(&function) != m_FuncToDependencies.end())
         if (m_FuncToDependencies.find(&function) != m_FuncToDependencies.end())
-          functionDependencies = indexTable.AddIndex(m_FuncToDependencies[&function]);
+          functionDependencies =
+              indexTable.AddIndex(m_FuncToDependencies[&function].begin(),
+                                  m_FuncToDependencies[&function].end());
         if (m_Module.HasDxilFunctionProps(&function)) {
         if (m_Module.HasDxilFunctionProps(&function)) {
           auto props = m_Module.GetDxilFunctionProps(&function);
           auto props = m_Module.GetDxilFunctionProps(&function);
           if (props.IsClosestHit() || props.IsAnyHit()) {
           if (props.IsClosestHit() || props.IsAnyHit()) {

+ 2 - 1
tools/clang/unittests/HLSL/DxilContainerTest.cpp

@@ -759,7 +759,8 @@ TEST_F(DxilContainerTest, CompileWhenOkThenCheckRDAT2) {
       "register(u0); Texture2D<float> ThreeTextures[3] : register(t0); "
       "register(u0); Texture2D<float> ThreeTextures[3] : register(t0); "
       "float function1();"
       "float function1();"
       "[shader(\"raygeneration\")] void RayGenMain() { Uav[0] = "
       "[shader(\"raygeneration\")] void RayGenMain() { Uav[0] = "
-      "ThreeTextures[0].Sample(Sampler, float2(0, 0)) + function1(); }";
+      "ThreeTextures[0].Sample(Sampler, float2(0, 0)) + "
+      "ThreeTextures[2].Sample(Sampler, float2(0, 0)) + function1(); }";
   CComPtr<IDxcCompiler> pCompiler;
   CComPtr<IDxcCompiler> pCompiler;
   CComPtr<IDxcBlobEncoding> pSource;
   CComPtr<IDxcBlobEncoding> pSource;
   CComPtr<IDxcBlob> pProgram;
   CComPtr<IDxcBlob> pProgram;