Skip to content

Commit

Permalink
[NFC] Use unordered_map for better performance (#2356)
Browse files Browse the repository at this point in the history
Use unordered_map instead of map for better performance.

Signed-off-by: Lu, John <john.lu@intel.com>
  • Loading branch information
LU-JOHN authored Mar 4, 2024
1 parent 08d9396 commit 5653803
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/SPIRV/Mangler/Mangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
#include "NameMangleAPI.h"
#include "ParameterType.h"
#include <algorithm>
#include <map>
#include <sstream>
#include <string>
#include <unordered_map>

// According to IA64 name mangling spec,
// builtin vector types should not be substituted
Expand Down Expand Up @@ -66,7 +66,7 @@ class MangleVisitor : public TypeVisitor {
ThistypeStr << NType;
}
#endif
std::map<std::string, unsigned>::iterator I =
std::unordered_map<std::string, unsigned>::iterator I =
Substitutions.find(ThistypeStr.str());
if (I == Substitutions.end())
return false;
Expand Down Expand Up @@ -195,7 +195,7 @@ class MangleVisitor : public TypeVisitor {
// Holds the mangled string representing the prototype of the function.
std::stringstream &Stream;
unsigned SeqId;
std::map<std::string, unsigned> Substitutions;
std::unordered_map<std::string, unsigned> Substitutions;
};

//
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/OCLTypeToSPIRV.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class OCLTypeToSPIRVBase : protected BuiltinCallHelper {
llvm::Module *M;
llvm::LLVMContext *Ctx;
// Map of argument/Function -> adapted type (probably TypedPointerType)
std::map<llvm::Value *, llvm::Type *> AdaptedTy;
std::unordered_map<llvm::Value *, llvm::Type *> AdaptedTy;
std::set<llvm::Function *> WorkSet; // Functions to be adapted

void adaptFunctionArguments(llvm::Function *F);
Expand Down
4 changes: 2 additions & 2 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ void SPIRVToLLVM::setLLVMLoopMetadata(const LoopInstType *LM,
}
if (LC & LoopControlDependencyArrayINTELMask) {
// Collect pointer variable <-> safelen information
std::map<Value *, unsigned> PointerSflnMap;
std::unordered_map<Value *, unsigned> PointerSflnMap;
unsigned NumOperandPairs = LoopControlParameters[NumParam];
unsigned OperandsEndIndex = NumParam + NumOperandPairs * 2;
assert(OperandsEndIndex <= LoopControlParameters.size() &&
Expand All @@ -763,7 +763,7 @@ void SPIRVToLLVM::setLLVMLoopMetadata(const LoopInstType *LM,

// A single run over the loop to retrieve all GetElementPtr instructions
// that access relevant array variables
std::map<Value *, std::vector<GetElementPtrInst *>> ArrayGEPMap;
std::unordered_map<Value *, std::vector<GetElementPtrInst *>> ArrayGEPMap;
for (const auto &BB : LoopObj->blocks()) {
for (Instruction &I : *BB) {
auto *GEP = dyn_cast<GetElementPtrInst>(&I);
Expand Down
5 changes: 3 additions & 2 deletions lib/SPIRV/SPIRVReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ class SPIRVToLLVM : private BuiltinCallHelper {
// A SPIRV value may be translated to a load instruction of a placeholder
// global variable. This map records load instruction of these placeholders
// which are supposed to be replaced by the real values later.
typedef std::map<SPIRVValue *, LoadInst *> SPIRVToLLVMPlaceholderMap;
typedef std::unordered_map<SPIRVValue *, LoadInst *>
SPIRVToLLVMPlaceholderMap;

typedef std::map<const BasicBlock *, const SPIRVValue *>
typedef std::unordered_map<const BasicBlock *, const SPIRVValue *>
SPIRVToLLVMLoopMetadataMap;

// Store all the allocations to Struct Types that are further
Expand Down
6 changes: 3 additions & 3 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5377,7 +5377,7 @@ LLVMToSPIRVBase::collectEntryPointInterfaces(SPIRVFunction *SF, Function *F) {
}

void LLVMToSPIRVBase::mutateFuncArgType(
const std::map<unsigned, Type *> &ChangedType, Function *F) {
const std::unordered_map<unsigned, Type *> &ChangedType, Function *F) {
for (auto &I : ChangedType) {
for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ++UI) {
auto *Call = dyn_cast<CallInst>(*UI);
Expand Down Expand Up @@ -5612,7 +5612,7 @@ bool LLVMToSPIRVBase::translate() {

for (auto &F : *M) {
auto *FT = F.getFunctionType();
std::map<unsigned, Type *> ChangedType;
std::unordered_map<unsigned, Type *> ChangedType;
oclGetMutatedArgumentTypesByBuiltin(FT, ChangedType, &F);
mutateFuncArgType(ChangedType, &F);
}
Expand Down Expand Up @@ -5652,7 +5652,7 @@ llvm::IntegerType *LLVMToSPIRVBase::getSizetType(unsigned AS) {
}

void LLVMToSPIRVBase::oclGetMutatedArgumentTypesByBuiltin(
llvm::FunctionType *FT, std::map<unsigned, Type *> &ChangedType,
llvm::FunctionType *FT, std::unordered_map<unsigned, Type *> &ChangedType,
Function *F) {
StringRef Demangled;
if (!oclIsBuiltin(F->getName(), Demangled))
Expand Down
12 changes: 6 additions & 6 deletions lib/SPIRV/SPIRVWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
bool oclGetExtInstIndex(const std::string &MangledName,
const std::string &DemangledName,
SPIRVWord *EntryPoint);
void
oclGetMutatedArgumentTypesByBuiltin(llvm::FunctionType *FT,
std::map<unsigned, Type *> &ChangedType,
Function *F);
void oclGetMutatedArgumentTypesByBuiltin(
llvm::FunctionType *FT, std::unordered_map<unsigned, Type *> &ChangedType,
Function *F);
bool isBuiltinTransToInst(Function *F);
bool isBuiltinTransToExtInst(Function *F,
SPIRVExtInstSetKind *BuiltinSet = nullptr,
Expand All @@ -245,8 +244,9 @@ class LLVMToSPIRVBase : protected BuiltinCallHelper {
SPIRVValue *transBuiltinToConstant(StringRef DemangledName, CallInst *CI);
SPIRVInstruction *transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
SPIRVBasicBlock *BB);
void mutateFuncArgType(const std::map<unsigned, Type *> &ChangedType,
Function *F);
void
mutateFuncArgType(const std::unordered_map<unsigned, Type *> &ChangedType,
Function *F);

SPIRVValue *transSpcvCast(CallInst *CI, SPIRVBasicBlock *BB);
SPIRVValue *oclTransSpvcCastSampler(CallInst *CI, SPIRVBasicBlock *BB);
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/libSPIRV/SPIRV.debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ namespace Operation {
enum {
OpCodeIdx = 0
};
static std::map<ExpressionOpCode, unsigned> OpCountMap {
static std::unordered_map<ExpressionOpCode, unsigned> OpCountMap {
{ Deref, 1 },
{ Plus, 1 },
{ Minus, 1 },
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/libSPIRV/SPIRVEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ SPIRVEntry *SPIRVEntry::create(Op OpCode) {
#undef _SPIRV_OP
};

typedef std::map<Op, SPIRVFactoryTy> OpToFactoryMapTy;
typedef std::unordered_map<Op, SPIRVFactoryTy> OpToFactoryMapTy;
static const OpToFactoryMapTy OpToFactoryMap(std::begin(Table),
std::end(Table));

Expand Down
16 changes: 10 additions & 6 deletions lib/SPIRV/libSPIRV/SPIRVModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,16 @@ class SPIRVModuleImpl : public SPIRVModule {
typedef std::vector<SPIRVAsmTargetINTEL *> SPIRVAsmTargetVector;
typedef std::vector<SPIRVAsmINTEL *> SPIRVAsmVector;
typedef std::vector<SPIRVEntryPoint *> SPIRVEntryPointVec;
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToInstructionSetMap;
std::map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds;
typedef std::map<SPIRVId, SPIRVExtInstSetKind> SPIRVIdToBuiltinSetMap;
typedef std::map<SPIRVExecutionModelKind, SPIRVIdSet> SPIRVExecModelIdSetMap;
typedef std::unordered_map<SPIRVId, SPIRVExtInstSetKind>
SPIRVIdToInstructionSetMap;
std::unordered_map<SPIRVExtInstSetKind, SPIRVId> ExtInstSetIds;
typedef std::unordered_map<SPIRVId, SPIRVExtInstSetKind>
SPIRVIdToBuiltinSetMap;
typedef std::unordered_map<SPIRVExecutionModelKind, SPIRVIdSet>
SPIRVExecModelIdSetMap;
typedef std::unordered_map<std::string, SPIRVString *> SPIRVStringMap;
typedef std::map<SPIRVTypeStruct *, std::vector<std::pair<unsigned, SPIRVId>>>
typedef std::unordered_map<SPIRVTypeStruct *,
std::vector<std::pair<unsigned, SPIRVId>>>
SPIRVUnknownStructFieldMap;
typedef std::vector<SPIRVEntry *> SPIRVAliasInstMDVec;
typedef std::unordered_map<llvm::MDNode *, SPIRVEntry *> SPIRVAliasInstMDMap;
Expand Down Expand Up @@ -560,7 +564,7 @@ class SPIRVModuleImpl : public SPIRVModule {
SPIRVTypeVoid *VoidTy;
SmallDenseMap<unsigned, SPIRVTypeInt *, 4> IntTypeMap;
SmallDenseMap<unsigned, SPIRVTypeFloat *, 4> FloatTypeMap;
std::map<unsigned, SPIRVConstant *> LiteralMap;
std::unordered_map<unsigned, SPIRVConstant *> LiteralMap;
std::vector<SPIRVExtInst *> DebugInstVec;
std::vector<SPIRVExtInst *> AuxDataInstVec;
std::vector<SPIRVModuleProcessed *> ModuleProcessedVec;
Expand Down

0 comments on commit 5653803

Please sign in to comment.