Prechádzať zdrojové kódy

Avoid iterate on map which use point as key to make sure output is the same. (#104)

Xiang Li 8 rokov pred
rodič
commit
19952dbf73

+ 3 - 3
include/dxc/HLSL/DxilTypeSystem.h

@@ -11,13 +11,13 @@
 
 #pragma once
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/MapVector.h"
 #include "dxc/HLSL/DxilCompType.h"
 #include "dxc/HLSL/DxilInterpolationMode.h"
 
 #include <memory>
 #include <string>
 #include <vector>
-#include <map>
 
 namespace llvm {
 class LLVMContext;
@@ -152,8 +152,8 @@ private:
 /// Use this class to represent structure type annotations in HL and DXIL.
 class DxilTypeSystem {
 public:
-  using StructAnnotationMap = std::map<const llvm::StructType *, std::unique_ptr<DxilStructAnnotation> >;
-  using FunctionAnnotationMap = std::map<const llvm::Function *, std::unique_ptr<DxilFunctionAnnotation> >;
+  using StructAnnotationMap = llvm::MapVector<const llvm::StructType *, std::unique_ptr<DxilStructAnnotation> >;
+  using FunctionAnnotationMap = llvm::MapVector<const llvm::Function *, std::unique_ptr<DxilFunctionAnnotation> >;
 
   DxilTypeSystem(llvm::Module *pModule);
 

+ 8 - 6
lib/HLSL/DxilTypeSystem.cpp

@@ -173,9 +173,10 @@ DxilStructAnnotation *DxilTypeSystem::GetStructAnnotation(const StructType *pStr
 }
 
 void DxilTypeSystem::EraseStructAnnotation(const StructType *pStructType) {
-  auto it = m_StructAnnotations.find(pStructType);
-  DXASSERT_NOMSG(it != m_StructAnnotations.end());
-  m_StructAnnotations.erase(it);
+  DXASSERT_NOMSG(m_StructAnnotations.count(pStructType));
+  m_StructAnnotations.remove_if([pStructType](
+      const std::pair<const StructType *, std::unique_ptr<DxilStructAnnotation>>
+          &I) { return pStructType == I.first; });
 }
 
 DxilTypeSystem::StructAnnotationMap &DxilTypeSystem::GetStructAnnotationMap() {
@@ -201,9 +202,10 @@ DxilFunctionAnnotation *DxilTypeSystem::GetFunctionAnnotation(const Function *pF
 }
 
 void DxilTypeSystem::EraseFunctionAnnotation(const Function *pFunction) {
-  auto it = m_FunctionAnnotations.find(pFunction);
-  DXASSERT_NOMSG(it != m_FunctionAnnotations.end());
-  m_FunctionAnnotations.erase(it);
+  DXASSERT_NOMSG(m_FunctionAnnotations.count(pFunction));
+  m_FunctionAnnotations.remove_if([pFunction](
+      const std::pair<const Function *, std::unique_ptr<DxilFunctionAnnotation>>
+          &I) { return pFunction == I.first; });
 }
 
 DxilTypeSystem::FunctionAnnotationMap &DxilTypeSystem::GetFunctionAnnotationMap() {

+ 6 - 18
tools/clang/lib/CodeGen/CGHLSLMS.cpp

@@ -175,7 +175,7 @@ private:
                                         bool bKeepUndefined);
   hlsl::CompType GetCompType(const BuiltinType *BT);
   // save intrinsic opcode
-  std::unordered_map<Function *, unsigned> m_IntrinsicMap;
+  std::vector<std::pair<Function *, unsigned>> m_IntrinsicMap;
   void AddHLSLIntrinsicOpcodeToFunction(Function *, unsigned opcode);
 
   // Type annotation related.
@@ -342,7 +342,7 @@ bool CGMSHLSLRuntime::IsHlslObjectType(llvm::Type *Ty) {
 
 void CGMSHLSLRuntime::AddHLSLIntrinsicOpcodeToFunction(Function *F,
                                                        unsigned opcode) {
-  m_IntrinsicMap[F] = opcode;
+  m_IntrinsicMap.emplace_back(F,opcode);
 }
 
 void CGMSHLSLRuntime::CheckParameterAnnotation(
@@ -3140,17 +3140,16 @@ static void AddOpcodeParamForIntrinsic(HLModule &HLM, Function *F,
 }
 
 static void AddOpcodeParamForIntrinsics(HLModule &HLM
-    , std::unordered_map<Function *, unsigned> &intrinsicMap) {
-  for (auto mapIter = intrinsicMap.begin(); mapIter != intrinsicMap.end();
-       mapIter++) {
-    Function *F = mapIter->first;
+    , std::vector<std::pair<Function *, unsigned>> &intrinsicMap) {
+  for (auto mapIter : intrinsicMap) {
+    Function *F = mapIter.first;
     if (F->user_empty()) {
       // delete the function
       F->eraseFromParent();
       continue;
     }
 
-    unsigned opcode = mapIter->second;
+    unsigned opcode = mapIter.second;
     AddOpcodeParamForIntrinsic(HLM, F, opcode);
   }
 }
@@ -3741,17 +3740,6 @@ void CGMSHLSLRuntime::FinishCodeGen() {
       if (noUpdate)
         break;
     }
-    // Remove unused external function.
-    for (auto FIt = TheModule.functions().begin(),
-              FE = TheModule.functions().end();
-         FIt != FE;) {
-      Function *F = FIt++;
-      if (F->isDeclaration() && F->user_empty()) {
-        if (m_IntrinsicMap.count(F))
-          m_IntrinsicMap.erase(F);
-        F->eraseFromParent();
-      }
-    }
   }
 
   // Create copy for clip plane.