Skip to content

Commit 89b4bc9

Browse files
LU-JOHNsys-ce-bb
authored andcommitted
Use unordered_map for better performance (#2356)
Use unordered_map instead of map for better performance. Signed-off-by: Lu, John <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@56538038eda11b7
1 parent 31826ee commit 89b4bc9

File tree

9 files changed

+30
-25
lines changed

9 files changed

+30
-25
lines changed

llvm-spirv/lib/SPIRV/Mangler/Mangler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
#include "NameMangleAPI.h"
1616
#include "ParameterType.h"
1717
#include <algorithm>
18-
#include <map>
1918
#include <sstream>
2019
#include <string>
20+
#include <unordered_map>
2121

2222
// According to IA64 name mangling spec,
2323
// builtin vector types should not be substituted
@@ -66,7 +66,7 @@ class MangleVisitor : public TypeVisitor {
6666
ThistypeStr << NType;
6767
}
6868
#endif
69-
std::map<std::string, unsigned>::iterator I =
69+
std::unordered_map<std::string, unsigned>::iterator I =
7070
Substitutions.find(ThistypeStr.str());
7171
if (I == Substitutions.end())
7272
return false;
@@ -195,7 +195,7 @@ class MangleVisitor : public TypeVisitor {
195195
// Holds the mangled string representing the prototype of the function.
196196
std::stringstream &Stream;
197197
unsigned SeqId;
198-
std::map<std::string, unsigned> Substitutions;
198+
std::unordered_map<std::string, unsigned> Substitutions;
199199
};
200200

201201
//

llvm-spirv/lib/SPIRV/OCLTypeToSPIRV.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class OCLTypeToSPIRVBase : protected BuiltinCallHelper {
6868
llvm::Module *M;
6969
llvm::LLVMContext *Ctx;
7070
// Map of argument/Function -> adapted type (probably TypedPointerType)
71-
std::map<llvm::Value *, llvm::Type *> AdaptedTy;
71+
std::unordered_map<llvm::Value *, llvm::Type *> AdaptedTy;
7272
std::set<llvm::Function *> WorkSet; // Functions to be adapted
7373

7474
void adaptFunctionArguments(llvm::Function *F);

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ void SPIRVToLLVM::setLLVMLoopMetadata(const LoopInstType *LM,
748748
}
749749
if (LC & LoopControlDependencyArrayINTELMask) {
750750
// Collect pointer variable <-> safelen information
751-
std::map<Value *, unsigned> PointerSflnMap;
751+
std::unordered_map<Value *, unsigned> PointerSflnMap;
752752
unsigned NumOperandPairs = LoopControlParameters[NumParam];
753753
unsigned OperandsEndIndex = NumParam + NumOperandPairs * 2;
754754
assert(OperandsEndIndex <= LoopControlParameters.size() &&
@@ -763,7 +763,7 @@ void SPIRVToLLVM::setLLVMLoopMetadata(const LoopInstType *LM,
763763

764764
// A single run over the loop to retrieve all GetElementPtr instructions
765765
// that access relevant array variables
766-
std::map<Value *, std::vector<GetElementPtrInst *>> ArrayGEPMap;
766+
std::unordered_map<Value *, std::vector<GetElementPtrInst *>> ArrayGEPMap;
767767
for (const auto &BB : LoopObj->blocks()) {
768768
for (Instruction &I : *BB) {
769769
auto *GEP = dyn_cast<GetElementPtrInst>(&I);

llvm-spirv/lib/SPIRV/SPIRVReader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ class SPIRVToLLVM : private BuiltinCallHelper {
150150
// A SPIRV value may be translated to a load instruction of a placeholder
151151
// global variable. This map records load instruction of these placeholders
152152
// which are supposed to be replaced by the real values later.
153-
typedef std::map<SPIRVValue *, LoadInst *> SPIRVToLLVMPlaceholderMap;
153+
typedef std::unordered_map<SPIRVValue *, LoadInst *>
154+
SPIRVToLLVMPlaceholderMap;
154155

155-
typedef std::map<const BasicBlock *, const SPIRVValue *>
156+
typedef std::unordered_map<const BasicBlock *, const SPIRVValue *>
156157
SPIRVToLLVMLoopMetadataMap;
157158

158159
// Store all the allocations to Struct Types that are further

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5441,7 +5441,7 @@ LLVMToSPIRVBase::collectEntryPointInterfaces(SPIRVFunction *SF, Function *F) {
54415441
}
54425442

54435443
void LLVMToSPIRVBase::mutateFuncArgType(
5444-
const std::map<unsigned, Type *> &ChangedType, Function *F) {
5444+
const std::unordered_map<unsigned, Type *> &ChangedType, Function *F) {
54455445
for (auto &I : ChangedType) {
54465446
for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ++UI) {
54475447
auto *Call = dyn_cast<CallInst>(*UI);
@@ -5676,7 +5676,7 @@ bool LLVMToSPIRVBase::translate() {
56765676

56775677
for (auto &F : *M) {
56785678
auto *FT = F.getFunctionType();
5679-
std::map<unsigned, Type *> ChangedType;
5679+
std::unordered_map<unsigned, Type *> ChangedType;
56805680
oclGetMutatedArgumentTypesByBuiltin(FT, ChangedType, &F);
56815681
mutateFuncArgType(ChangedType, &F);
56825682
}
@@ -5716,7 +5716,7 @@ llvm::IntegerType *LLVMToSPIRVBase::getSizetType(unsigned AS) {
57165716
}
57175717

57185718
void LLVMToSPIRVBase::oclGetMutatedArgumentTypesByBuiltin(
5719-
llvm::FunctionType *FT, std::map<unsigned, Type *> &ChangedType,
5719+
llvm::FunctionType *FT, std::unordered_map<unsigned, Type *> &ChangedType,
57205720
Function *F) {
57215721
StringRef Demangled;
57225722
if (!oclIsBuiltin(F->getName(), Demangled))

llvm-spirv/lib/SPIRV/SPIRVWriter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,9 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
228228
bool oclGetExtInstIndex(const std::string &MangledName,
229229
const std::string &DemangledName,
230230
SPIRVWord *EntryPoint);
231-
void
232-
oclGetMutatedArgumentTypesByBuiltin(llvm::FunctionType *FT,
233-
std::map<unsigned, Type *> &ChangedType,
234-
Function *F);
231+
void oclGetMutatedArgumentTypesByBuiltin(
232+
llvm::FunctionType *FT, std::unordered_map<unsigned, Type *> &ChangedType,
233+
Function *F);
235234
bool isBuiltinTransToInst(Function *F);
236235
bool isBuiltinTransToExtInst(Function *F,
237236
SPIRVExtInstSetKind *BuiltinSet = nullptr,
@@ -245,8 +244,9 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
245244
SPIRVValue *transBuiltinToConstant(StringRef DemangledName, CallInst *CI);
246245
SPIRVInstruction *transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
247246
SPIRVBasicBlock *BB);
248-
void mutateFuncArgType(const std::map<unsigned, Type *> &ChangedType,
249-
Function *F);
247+
void
248+
mutateFuncArgType(const std::unordered_map<unsigned, Type *> &ChangedType,
249+
Function *F);
250250

251251
SPIRVValue *transSpcvCast(CallInst *CI, SPIRVBasicBlock *BB);
252252
SPIRVValue *oclTransSpvcCastSampler(CallInst *CI, SPIRVBasicBlock *BB);

llvm-spirv/lib/SPIRV/libSPIRV/SPIRV.debug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ namespace Operation {
760760
enum {
761761
OpCodeIdx = 0
762762
};
763-
static std::map<ExpressionOpCode, unsigned> OpCountMap {
763+
static std::unordered_map<ExpressionOpCode, unsigned> OpCountMap {
764764
{ Deref, 1 },
765765
{ Plus, 1 },
766766
{ Minus, 1 },

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ SPIRVEntry *SPIRVEntry::create(Op OpCode) {
8080
#undef _SPIRV_OP
8181
};
8282

83-
typedef std::map<Op, SPIRVFactoryTy> OpToFactoryMapTy;
83+
typedef std::unordered_map<Op, SPIRVFactoryTy> OpToFactoryMapTy;
8484
static const OpToFactoryMapTy OpToFactoryMap(std::begin(Table),
8585
std::end(Table));
8686

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,16 @@ class SPIRVModuleImpl : public SPIRVModule {
521521
typedef std::vector<SPIRVAsmTargetINTEL *> SPIRVAsmTargetVector;
522522
typedef std::vector<SPIRVAsmINTEL *> SPIRVAsmVector;
523523
typedef std::vector<SPIRVEntryPoint *> SPIRVEntryPointVec;
524-
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToInstructionSetMap;
525-
std::map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds;
526-
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToBuiltinSetMap;
527-
typedef std::map<SPIRVExecutionModelKind, SPIRVIdSet> SPIRVExecModelIdSetMap;
524+
typedef std::unordered_map<SPIRVId, SPIRVExtInstSetKind>
525+
SPIRVIdToInstructionSetMap;
526+
std::unordered_map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds;
527+
typedef std::unordered_map<SPIRVId, SPIRVExtInstSetKind>
528+
SPIRVIdToBuiltinSetMap;
529+
typedef std::unordered_map<SPIRVExecutionModelKind, SPIRVIdSet>
530+
SPIRVExecModelIdSetMap;
528531
typedef std::unordered_map<std::string, SPIRVString *> SPIRVStringMap;
529-
typedef std::map<SPIRVTypeStruct *, std::vector<std::pair<unsigned, SPIRVId>>>
532+
typedef std::unordered_map<SPIRVTypeStruct *,
533+
std::vector<std::pair<unsigned, SPIRVId>>>
530534
SPIRVUnknownStructFieldMap;
531535
typedef std::vector<SPIRVEntry *> SPIRVAliasInstMDVec;
532536
typedef std::unordered_map<llvm::MDNode *, SPIRVEntry *> SPIRVAliasInstMDMap;
@@ -560,7 +564,7 @@ class SPIRVModuleImpl : public SPIRVModule {
560564
SPIRVTypeVoid *VoidTy;
561565
SmallDenseMap<unsigned, SPIRVTypeInt *, 4> IntTypeMap;
562566
SmallDenseMap<unsigned, SPIRVTypeFloat *, 4> FloatTypeMap;
563-
std::map<unsigned, SPIRVConstant *> LiteralMap;
567+
std::unordered_map<unsigned, SPIRVConstant *> LiteralMap;
564568
std::vector<SPIRVExtInst *> DebugInstVec;
565569
std::vector<SPIRVExtInst *> AuxDataInstVec;
566570
std::vector<SPIRVModuleProcessed *> ModuleProcessedVec;

0 commit comments

Comments
 (0)