Skip to content

Commit 1069a8c

Browse files
bwlodarczsys-ce-bb
authored andcommitted
Fix duplicate type declarations when using addXXXType api (#2216)
Duplicate declarations are non conformant with spec which rendered current implementation of SPIRVModuleImpl::addXXXType useless for some types. This commit is an easy fix for bool, float and void. Integer is changed from std::map to SmallDenseMap with 4 inlined buckets. Most of the time Integer will only have 8, 16, 32 and 64 bitwidth so it's much more efficient to use this specialized map. Original commit: KhronosGroup/SPIRV-LLVM-Translator@4d65d27
1 parent 0d51e97 commit 1069a8c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SPIRVModuleImpl : public SPIRVModule {
7070
SPIRVVersion(static_cast<SPIRVWord>(VersionNumber::SPIRV_1_0)),
7171
GeneratorId(SPIRVGEN_KhronosLLVMSPIRVTranslator), GeneratorVer(0),
7272
InstSchema(SPIRVISCH_Default), SrcLang(SourceLanguageOpenCL_C),
73-
SrcLangVer(102000) {
73+
SrcLangVer(102000), BoolTy(nullptr), VoidTy(nullptr) {
7474
AddrModel = sizeof(size_t) == 32 ? AddressingModelPhysical32
7575
: AddressingModelPhysical64;
7676
// OpenCL memory model requires Kernel capability
@@ -533,7 +533,10 @@ class SPIRVModuleImpl : public SPIRVModule {
533533
SPIRVStringMap StrMap;
534534
SPIRVCapMap CapMap;
535535
SPIRVUnknownStructFieldMap UnknownStructFieldMap;
536-
std::map<unsigned, SPIRVTypeInt *> IntTypeMap;
536+
SPIRVTypeBool *BoolTy;
537+
SPIRVTypeVoid *VoidTy;
538+
SmallDenseMap<unsigned, SPIRVTypeInt *, 4> IntTypeMap;
539+
SmallDenseMap<unsigned, SPIRVTypeFloat *, 4> FloatTypeMap;
537540
std::map<unsigned, SPIRVConstant *> LiteralMap;
538541
std::vector<SPIRVExtInst *> DebugInstVec;
539542
std::vector<SPIRVExtInst *> AuxDataInstVec;
@@ -927,7 +930,12 @@ template <class T> T *SPIRVModuleImpl::addType(T *Ty) {
927930
}
928931

929932
SPIRVTypeVoid *SPIRVModuleImpl::addVoidType() {
930-
return addType(new SPIRVTypeVoid(this, getId()));
933+
SPIRVTypeVoid *V = VoidTy;
934+
if (!V) {
935+
V = addType(new SPIRVTypeVoid(this, getId()));
936+
VoidTy = V;
937+
}
938+
return V;
931939
}
932940

933941
SPIRVTypeArray *SPIRVModuleImpl::addArrayType(SPIRVType *ElementType,
@@ -936,7 +944,12 @@ SPIRVTypeArray *SPIRVModuleImpl::addArrayType(SPIRVType *ElementType,
936944
}
937945

938946
SPIRVTypeBool *SPIRVModuleImpl::addBoolType() {
939-
return addType(new SPIRVTypeBool(this, getId()));
947+
SPIRVTypeBool *B = BoolTy;
948+
if (!B) {
949+
B = addType(new SPIRVTypeBool(this, getId()));
950+
BoolTy = B;
951+
}
952+
return B;
940953
}
941954

942955
SPIRVTypeInt *SPIRVModuleImpl::addIntegerType(unsigned BitWidth) {
@@ -949,8 +962,12 @@ SPIRVTypeInt *SPIRVModuleImpl::addIntegerType(unsigned BitWidth) {
949962
}
950963

951964
SPIRVTypeFloat *SPIRVModuleImpl::addFloatType(unsigned BitWidth) {
952-
SPIRVTypeFloat *T = addType(new SPIRVTypeFloat(this, getId(), BitWidth));
953-
return T;
965+
auto Loc = FloatTypeMap.find(BitWidth);
966+
if (Loc != FloatTypeMap.end())
967+
return Loc->second;
968+
auto *Ty = new SPIRVTypeFloat(this, getId(), BitWidth);
969+
FloatTypeMap[BitWidth] = Ty;
970+
return addType(Ty);
954971
}
955972

956973
SPIRVTypePointer *

0 commit comments

Comments
 (0)