Skip to content

Commit 93f8086

Browse files
EbinJose2002sys-ce-bb
authored andcommitted
Refactoring Two functions into One (#3124)
- Refactored addUntypedPointerKHRType and addPointerType functions into a single function (It was marked as TODO) - Combined both functions into one with return type SPIRVType * - Also re inserted an assert and removed unnecessary if (Marked as TODO) Original commit: KhronosGroup/SPIRV-LLVM-Translator@19d67bdfc9ebf8e
1 parent 2168dd6 commit 93f8086

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

+11-14
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,9 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(Type *ET, unsigned AddrSpc) {
739739
SPIRVType *TranslatedTy = nullptr;
740740
if (ET->isPointerTy() &&
741741
BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers)) {
742-
TranslatedTy = BM->addUntypedPointerKHRType(
743-
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)));
742+
TranslatedTy = BM->addPointerType(
743+
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)),
744+
nullptr);
744745
} else {
745746
ElementType = transType(ET);
746747
TranslatedTy = transPointerType(ElementType, AddrSpc);
@@ -765,8 +766,9 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(SPIRVType *ET, unsigned AddrSpc) {
765766
return transPointerType(ET, SPIRAS_Private);
766767
if (BM->isAllowedToUseExtension(ExtensionID::SPV_KHR_untyped_pointers) &&
767768
!(ET->isTypeArray() || ET->isTypeVector() || ET->isSPIRVOpaqueType())) {
768-
TranslatedTy = BM->addUntypedPointerKHRType(
769-
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)));
769+
TranslatedTy = BM->addPointerType(
770+
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)),
771+
nullptr);
770772
} else {
771773
TranslatedTy = BM->addPointerType(
772774
SPIRSPIRVAddrSpaceMap::map(static_cast<SPIRAddressSpace>(AddrSpc)), ET);
@@ -2348,10 +2350,8 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
23482350
}
23492351
SPIRVType *VarTy = TranslatedTy;
23502352
if (V->getType()->getPointerAddressSpace() == SPIRAS_Generic) {
2351-
// TODO: refactor addPointerType and addUntypedPointerKHRType in one
2352-
// method if possible.
23532353
if (TranslatedTy->isTypeUntypedPointerKHR())
2354-
VarTy = BM->addUntypedPointerKHRType(StorageClassFunction);
2354+
VarTy = BM->addPointerType(StorageClassFunction, nullptr);
23552355
else
23562356
VarTy = BM->addPointerType(StorageClassFunction,
23572357
TranslatedTy->getPointerElementType());
@@ -2698,11 +2698,8 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
26982698
SPIRVType *LLVMToSPIRVBase::mapType(Type *T, SPIRVType *BT) {
26992699
assert(!T->isPointerTy() && "Pointer types cannot be stored in the type map");
27002700
auto EmplaceStatus = TypeMap.try_emplace(T, BT);
2701-
// TODO: Uncomment the assertion, once the type mapping issue is resolved
2702-
// assert(EmplaceStatus.second && "The type was already added to the map");
2701+
assert(EmplaceStatus.second && "The type was already added to the map");
27032702
SPIRVDBG(dbgs() << "[mapType] " << *T << " => "; spvdbgs() << *BT << '\n');
2704-
if (!EmplaceStatus.second)
2705-
return TypeMap[T];
27062703
return BT;
27072704
}
27082705

@@ -4303,8 +4300,8 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
43034300
SPIRVType *IntegralTy = transType(II->getType()->getStructElementType(1));
43044301
// IntegralTy is the type of the result. We want to create a pointer to this
43054302
// that we can pass to OpenCLLIB::modf to store the integral part.
4306-
SPIRVTypePointer *IntegralPtrTy =
4307-
BM->addPointerType(StorageClassFunction, IntegralTy);
4303+
SPIRVType *GenericPtrTy = BM->addPointerType(StorageClassFunction, IntegralTy);
4304+
auto *IntegralPtrTy = dyn_cast<SPIRVTypePointer>(GenericPtrTy);
43084305
// We need to use the entry BB of the function calling llvm.modf.*, instead
43094306
// of the current BB. For that, we'll find current BB's parent and get its
43104307
// first BB, which is the entry BB of the function.
@@ -4830,7 +4827,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
48304827
auto *SrcTy = PtrOp->getType();
48314828
SPIRVType *DstTy = nullptr;
48324829
if (SrcTy->isTypeUntypedPointerKHR())
4833-
DstTy = BM->addUntypedPointerKHRType(StorageClassFunction);
4830+
DstTy = BM->addPointerType(StorageClassFunction, nullptr);
48344831
else
48354832
DstTy = BM->addPointerType(StorageClassFunction,
48364833
SrcTy->getPointerElementType());

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ class SPIRVModuleImpl : public SPIRVModule {
259259
const std::vector<SPIRVType *> &) override;
260260
SPIRVTypeInt *addIntegerType(unsigned BitWidth) override;
261261
SPIRVTypeOpaque *addOpaqueType(const std::string &) override;
262-
SPIRVTypePointer *addPointerType(SPIRVStorageClassKind, SPIRVType *) override;
263-
SPIRVTypeUntypedPointerKHR *
264-
addUntypedPointerKHRType(SPIRVStorageClassKind) override;
262+
SPIRVType *addPointerType(SPIRVStorageClassKind, SPIRVType *) override;
265263
SPIRVTypeImage *addImageType(SPIRVType *,
266264
const SPIRVTypeImageDescriptor &) override;
267265
SPIRVTypeImage *addImageType(SPIRVType *, const SPIRVTypeImageDescriptor &,
@@ -1020,29 +1018,30 @@ SPIRVTypeFloat *SPIRVModuleImpl::addFloatType(unsigned BitWidth,
10201018
return addType(Ty);
10211019
}
10221020

1023-
SPIRVTypePointer *
1024-
SPIRVModuleImpl::addPointerType(SPIRVStorageClassKind StorageClass,
1025-
SPIRVType *ElementType) {
1021+
SPIRVType *SPIRVModuleImpl::addPointerType(SPIRVStorageClassKind StorageClass,
1022+
SPIRVType *ElementType = nullptr) {
1023+
if (ElementType == nullptr) {
1024+
// Untyped pointer
1025+
auto Loc = UntypedPtrTyMap.find(StorageClass);
1026+
if (Loc != UntypedPtrTyMap.end())
1027+
return Loc->second;
1028+
1029+
auto *Ty = new SPIRVTypeUntypedPointerKHR(this, getId(), StorageClass);
1030+
UntypedPtrTyMap[StorageClass] = Ty;
1031+
return addType(Ty);
1032+
}
1033+
1034+
// Typed pointer
10261035
auto Desc = std::make_pair(StorageClass, ElementType);
10271036
auto Loc = PointerTypeMap.find(Desc);
10281037
if (Loc != PointerTypeMap.end())
10291038
return Loc->second;
1039+
10301040
auto *Ty = new SPIRVTypePointer(this, getId(), StorageClass, ElementType);
10311041
PointerTypeMap[Desc] = Ty;
10321042
return addType(Ty);
10331043
}
10341044

1035-
SPIRVTypeUntypedPointerKHR *
1036-
SPIRVModuleImpl::addUntypedPointerKHRType(SPIRVStorageClassKind StorageClass) {
1037-
auto Loc = UntypedPtrTyMap.find(StorageClass);
1038-
if (Loc != UntypedPtrTyMap.end())
1039-
return Loc->second;
1040-
1041-
auto *Ty = new SPIRVTypeUntypedPointerKHR(this, getId(), StorageClass);
1042-
UntypedPtrTyMap[StorageClass] = Ty;
1043-
return addType(Ty);
1044-
}
1045-
10461045
SPIRVTypeFunction *SPIRVModuleImpl::addFunctionType(
10471046
SPIRVType *ReturnType, const std::vector<SPIRVType *> &ParameterTypes) {
10481047
return addType(

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,7 @@ class SPIRVModule {
257257
virtual SPIRVTypeSampledImage *addSampledImageType(SPIRVTypeImage *T) = 0;
258258
virtual SPIRVTypeInt *addIntegerType(unsigned) = 0;
259259
virtual SPIRVTypeOpaque *addOpaqueType(const std::string &) = 0;
260-
virtual SPIRVTypePointer *addPointerType(SPIRVStorageClassKind,
261-
SPIRVType *) = 0;
262-
virtual SPIRVTypeUntypedPointerKHR *
263-
addUntypedPointerKHRType(SPIRVStorageClassKind) = 0;
260+
virtual SPIRVType *addPointerType(SPIRVStorageClassKind, SPIRVType *) = 0;
264261
virtual SPIRVTypeStruct *openStructType(unsigned, const std::string &) = 0;
265262
virtual SPIRVEntry *addTypeStructContinuedINTEL(unsigned NumMembers) = 0;
266263
virtual void closeStructType(SPIRVTypeStruct *, bool) = 0;

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

+3
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ class SPIRVTypePointer : public SPIRVTypePointerBase<OpTypePointer, 4> {
323323
std::vector<SPIRVEntry *> getNonLiteralOperands() const override {
324324
return std::vector<SPIRVEntry *>(1, getEntry(ElemTypeId));
325325
}
326+
static bool classof(const SPIRVEntry *E) {
327+
return E->getOpCode() == OpTypePointer;
328+
}
326329

327330
protected:
328331
_SPIRV_DEF_ENCDEC3(Id, ElemStorageClass, ElemTypeId)

0 commit comments

Comments
 (0)