@@ -417,8 +417,8 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
417417 // A pointer to image or pipe type in LLVM is translated to a SPIRV
418418 // (non-pointer) image or pipe type.
419419 if (T->isPointerTy ()) {
420- auto *ET = Type::getInt8Ty (T->getContext ());
421420 auto AddrSpc = T->getPointerAddressSpace ();
421+ auto *ET = Type::getInt8Ty (T->getContext ());
422422 return transPointerType (ET, AddrSpc);
423423 }
424424
@@ -720,7 +720,6 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(Type *ET, unsigned AddrSpc) {
720720 transType (ET)));
721721 }
722722 } else {
723- SPIRVType *ElementType = transType (ET);
724723 // ET, as a recursive type, may contain exactly the same pointer T, so it
725724 // may happen that after translation of ET we already have translated T,
726725 // added the translated pointer to the SPIR-V module and mapped T to this
@@ -729,7 +728,17 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(Type *ET, unsigned AddrSpc) {
729728 if (Loc != PointeeTypeMap.end ()) {
730729 return Loc->second ;
731730 }
732- SPIRVType *TranslatedTy = transPointerType (ElementType, AddrSpc);
731+
732+ SPIRVType *ElementType = nullptr ;
733+ SPIRVType *TranslatedTy = nullptr ;
734+ if (ET->isPointerTy () &&
735+ BM->isAllowedToUseExtension (ExtensionID::SPV_KHR_untyped_pointers)) {
736+ TranslatedTy = BM->addUntypedPointerKHRType (
737+ SPIRSPIRVAddrSpaceMap::map (static_cast <SPIRAddressSpace>(AddrSpc)));
738+ } else {
739+ ElementType = transType (ET);
740+ TranslatedTy = transPointerType (ElementType, AddrSpc);
741+ }
733742 PointeeTypeMap[TypeKey] = TranslatedTy;
734743 return TranslatedTy;
735744 }
@@ -744,8 +753,16 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(SPIRVType *ET, unsigned AddrSpc) {
744753 if (Loc != PointeeTypeMap.end ())
745754 return Loc->second ;
746755
747- SPIRVType *TranslatedTy = BM->addPointerType (
748- SPIRSPIRVAddrSpaceMap::map (static_cast <SPIRAddressSpace>(AddrSpc)), ET);
756+ SPIRVType *TranslatedTy = nullptr ;
757+ if (BM->isAllowedToUseExtension (ExtensionID::SPV_KHR_untyped_pointers) &&
758+ !(ET->isTypeArray () || ET->isTypeVector () || ET->isTypeImage () ||
759+ ET->isTypeSampler () || ET->isTypePipe ())) {
760+ TranslatedTy = BM->addUntypedPointerKHRType (
761+ SPIRSPIRVAddrSpaceMap::map (static_cast <SPIRAddressSpace>(AddrSpc)));
762+ } else {
763+ TranslatedTy = BM->addPointerType (
764+ SPIRSPIRVAddrSpaceMap::map (static_cast <SPIRAddressSpace>(AddrSpc)), ET);
765+ }
749766 PointeeTypeMap[TypeKey] = TranslatedTy;
750767 return TranslatedTy;
751768}
@@ -2184,8 +2201,13 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
21842201 MemoryAccessNoAliasINTELMaskMask);
21852202 if (MemoryAccess.front () == 0 )
21862203 MemoryAccess.clear ();
2187- return mapValue (V, BM->addLoadInst (transValue (LD->getPointerOperand (), BB),
2188- MemoryAccess, BB));
2204+ return mapValue (
2205+ V,
2206+ BM->addLoadInst (
2207+ transValue (LD->getPointerOperand (), BB), MemoryAccess, BB,
2208+ BM->isAllowedToUseExtension (ExtensionID::SPV_KHR_untyped_pointers)
2209+ ? transType (LD->getType ())
2210+ : nullptr ));
21892211 }
21902212
21912213 if (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
@@ -2395,14 +2417,17 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
23952417
23962418 if (auto *Phi = dyn_cast<PHINode>(V)) {
23972419 std::vector<SPIRVValue *> IncomingPairs;
2420+ SPIRVType *Ty = transScavengedType (Phi);
23982421
23992422 for (size_t I = 0 , E = Phi->getNumIncomingValues (); I != E; ++I) {
2400- IncomingPairs.push_back (transValue (Phi->getIncomingValue (I), BB, true ,
2401- FuncTransMode::Pointer));
2423+ SPIRVValue *Val = transValue (Phi->getIncomingValue (I), BB, true ,
2424+ FuncTransMode::Pointer);
2425+ if (Val->getType () != Ty)
2426+ Val = BM->addUnaryInst (OpBitcast, Ty, Val, BB);
2427+ IncomingPairs.push_back (Val);
24022428 IncomingPairs.push_back (transValue (Phi->getIncomingBlock (I), nullptr ));
24032429 }
2404- return mapValue (V,
2405- BM->addPhiInst (transScavengedType (Phi), IncomingPairs, BB));
2430+ return mapValue (V, BM->addPhiInst (Ty, IncomingPairs, BB));
24062431 }
24072432
24082433 if (auto *Ext = dyn_cast<ExtractValueInst>(V)) {
@@ -6659,9 +6684,12 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
66596684 assert ((Pointee == Args[I] || !isa<Function>(Pointee)) &&
66606685 " Illegal use of a function pointer type" );
66616686 }
6662- SPArgs.push_back (SPI->isOperandLiteral (I)
6663- ? cast<ConstantInt>(Args[I])->getZExtValue ()
6664- : transValue (Args[I], BB)->getId ());
6687+ if (!SPI->isOperandLiteral (I)) {
6688+ SPIRVValue *Val = transValue (Args[I], BB);
6689+ SPArgs.push_back (Val->getId ());
6690+ } else {
6691+ SPArgs.push_back (cast<ConstantInt>(Args[I])->getZExtValue ());
6692+ }
66656693 }
66666694 BM->addInstTemplate (SPI, SPArgs, BB, SPRetTy);
66676695 if (!SPRetTy || !SPRetTy->isTypeStruct ())
0 commit comments