@@ -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->isTypeStruct () ||
759+         ET->isTypeImage () || 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}
@@ -1312,7 +1329,8 @@ SPIRVValue *LLVMToSPIRVBase::transConstantUse(Constant *C,
13121329  if  (Trans->getType () == ExpectedType || Trans->getType ()->isTypePipeStorage ())
13131330    return  Trans;
13141331
1315-   assert (C->getType ()->isPointerTy () &&
1332+   assert ((C->getType ()->isPointerTy () ||
1333+           ExpectedType->isTypeUntypedPointerKHR ()) &&
13161334         " Only pointer type mismatches should be possible"  );
13171335  //  In the common case of strings ([N x i8] GVs), see if we can emit a GEP
13181336  //  instruction.
@@ -2050,8 +2068,12 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
20502068          }
20512069        }
20522070      }
2053-       SPIRVType *TransTy = transType (Ty);
2054-       BVarInit = transConstantUse (Init, TransTy->getPointerElementType ());
2071+       if  (BM->isAllowedToUseExtension (ExtensionID::SPV_KHR_untyped_pointers)) {
2072+         BVarInit = transConstantUse (Init, transType (Init->getType ()));
2073+       } else  {
2074+         SPIRVType *TransTy = transType (Ty);
2075+         BVarInit = transConstantUse (Init, TransTy->getPointerElementType ());
2076+       }
20552077    }
20562078
20572079    SPIRVStorageClassKind StorageClass;
@@ -2084,9 +2106,12 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
20842106    }
20852107
20862108    SPIRVType *TranslatedTy = transType (Ty);
2087-     auto  *BVar = static_cast <SPIRVVariable *>(
2088-         BM->addVariable (TranslatedTy, GV->isConstant (), transLinkageType (GV),
2089-                         BVarInit, GV->getName ().str (), StorageClass, nullptr ));
2109+     auto  *BVar = static_cast <SPIRVVariableBase *>(BM->addVariable (
2110+         TranslatedTy,
2111+         TranslatedTy->isTypeUntypedPointerKHR () ? transType (GV->getValueType ())
2112+                                                 : nullptr ,
2113+         GV->isConstant (), transLinkageType (GV), BVarInit, GV->getName ().str (),
2114+         StorageClass, nullptr ));
20902115
20912116    if  (IsVectorCompute) {
20922117      BVar->addDecorate (DecorationVectorComputeVariableINTEL);
@@ -2196,8 +2221,13 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
21962221                             MemoryAccessNoAliasINTELMaskMask);
21972222    if  (MemoryAccess.front () == 0 )
21982223      MemoryAccess.clear ();
2199-     return  mapValue (V, BM->addLoadInst (transValue (LD->getPointerOperand (), BB),
2200-                                        MemoryAccess, BB));
2224+     return  mapValue (
2225+         V,
2226+         BM->addLoadInst (
2227+             transValue (LD->getPointerOperand (), BB), MemoryAccess, BB,
2228+             BM->isAllowedToUseExtension (ExtensionID::SPV_KHR_untyped_pointers)
2229+                 ? transType (LD->getType ())
2230+                 : nullptr ));
22012231  }
22022232
22032233  if  (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
@@ -2270,8 +2300,9 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
22702300            StorageClassFunction,
22712301            BM->addArrayType (transType (Alc->getAllocatedType ()), Length));
22722302        SPIRVValue *Arr = BM->addVariable (
2273-             AllocationType, false , spv::internal::LinkageTypeInternal, nullptr ,
2274-             Alc->getName ().str () + " _alloca"  , StorageClassFunction, BB);
2303+             AllocationType, nullptr , false , spv::internal::LinkageTypeInternal,
2304+             nullptr , Alc->getName ().str () + " _alloca"  , StorageClassFunction,
2305+             BB);
22752306        //  Manually set alignment. OpBitcast created below will be decorated as
22762307        //  that's the SPIR-V value mapped to the original LLVM one.
22772308        transAlign (Alc, Arr);
@@ -2295,7 +2326,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
22952326                                 TranslatedTy->getPointerElementType ())
22962327            : TranslatedTy;
22972328    SPIRVValue *Var = BM->addVariable (
2298-         VarTy, false , spv::internal::LinkageTypeInternal, nullptr ,
2329+         VarTy,
2330+         VarTy->isTypeUntypedPointerKHR () ? transType (Alc->getAllocatedType ())
2331+                                          : nullptr ,
2332+         false , spv::internal::LinkageTypeInternal, nullptr ,
22992333        Alc->getName ().str (), StorageClassFunction, BB);
23002334    if  (V->getType ()->getPointerAddressSpace () == SPIRAS_Generic) {
23012335      SPIRVValue *Cast =
@@ -2407,14 +2441,17 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
24072441
24082442  if  (auto  *Phi = dyn_cast<PHINode>(V)) {
24092443    std::vector<SPIRVValue *> IncomingPairs;
2444+     SPIRVType *Ty = transScavengedType (Phi);
24102445
24112446    for  (size_t  I = 0 , E = Phi->getNumIncomingValues (); I != E; ++I) {
2412-       IncomingPairs.push_back (transValue (Phi->getIncomingValue (I), BB, true ,
2413-                                          FuncTransMode::Pointer));
2447+       SPIRVValue *Val = transValue (Phi->getIncomingValue (I), BB, true ,
2448+                                    FuncTransMode::Pointer);
2449+       if  (Val->getType () != Ty)
2450+         Val = BM->addUnaryInst (OpBitcast, Ty, Val, BB);
2451+       IncomingPairs.push_back (Val);
24142452      IncomingPairs.push_back (transValue (Phi->getIncomingBlock (I), nullptr ));
24152453    }
2416-     return  mapValue (V,
2417-                     BM->addPhiInst (transScavengedType (Phi), IncomingPairs, BB));
2454+     return  mapValue (V, BM->addPhiInst (Ty, IncomingPairs, BB));
24182455  }
24192456
24202457  if  (auto  *Ext = dyn_cast<ExtractValueInst>(V)) {
@@ -2737,7 +2774,7 @@ void checkIsGlobalVar(SPIRVEntry *E, Decoration Dec) {
27372774  E->getErrorLog ().checkError (E->isVariable (), SPIRVEC_InvalidModule, ErrStr);
27382775
27392776  auto  AddrSpace = SPIRSPIRVAddrSpaceMap::rmap (
2740-       static_cast <SPIRVVariable  *>(E)->getStorageClass ());
2777+       static_cast <SPIRVVariableBase  *>(E)->getStorageClass ());
27412778  ErrStr += "  in a global (module) scope"  ;
27422779  E->getErrorLog ().checkError (AddrSpace == SPIRAS_Global, SPIRVEC_InvalidModule,
27432780                              ErrStr);
@@ -2885,10 +2922,11 @@ static void transMetadataDecorations(Metadata *MD, SPIRVValue *Target) {
28852922    case  spv::internal::DecorationInitModeINTEL:
28862923    case  DecorationInitModeINTEL: {
28872924      checkIsGlobalVar (Target, DecoKind);
2888-       ErrLog.checkError (static_cast <SPIRVVariable *>(Target)->getInitializer (),
2889-                         SPIRVEC_InvalidLlvmModule,
2890-                         " InitModeINTEL only be applied to a global (module " 
2891-                         " scope) variable which has an Initializer operand"  );
2925+       ErrLog.checkError (
2926+           static_cast <SPIRVVariableBase *>(Target)->getInitializer (),
2927+           SPIRVEC_InvalidLlvmModule,
2928+           " InitModeINTEL only be applied to a global (module " 
2929+           " scope) variable which has an Initializer operand"  );
28922930
28932931      ErrLog.checkError (NumOperands == 2 , SPIRVEC_InvalidLlvmModule,
28942932                        " InitModeINTEL requires exactly 1 extra operand"  );
@@ -4130,14 +4168,18 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
41304168    SPIRVType *FTy = transType (II->getType ()->getStructElementType (0 ));
41314169    SPIRVTypePointer *ITy = static_cast <SPIRVTypePointer *>(transPointerType (
41324170        II->getType ()->getStructElementType (1 ), SPIRAS_Private));
4133- 
4134-     unsigned  BitWidth = ITy->getElementType ()->getBitWidth ();
4135-     BM->getErrorLog ().checkError (BitWidth == 32 , SPIRVEC_InvalidBitWidth,
4136-                                  std::to_string (BitWidth));
4137- 
4171+      if  (!ITy-> isTypeUntypedPointerKHR ()) { 
4172+        unsigned  BitWidth = ITy->getElementType ()->getBitWidth ();
4173+        BM->getErrorLog ().checkError (BitWidth == 32 , SPIRVEC_InvalidBitWidth,
4174+                                     std::to_string (BitWidth));
4175+     } 
41384176    SPIRVValue *IntVal =
4139-         BM->addVariable (ITy, false , spv::internal::LinkageTypeInternal, nullptr ,
4140-                         " "  , ITy->getStorageClass (), BB);
4177+         BM->addVariable (ITy,
4178+                         ITy->isTypeUntypedPointerKHR ()
4179+                             ? transType (II->getType ()->getStructElementType (1 ))
4180+                             : nullptr ,
4181+                         false , spv::internal::LinkageTypeInternal, nullptr , " "  ,
4182+                         ITy->getStorageClass (), BB);
41414183
41424184    std::vector<SPIRVValue *> Ops{transValue (II->getArgOperand (0 ), BB), IntVal};
41434185
@@ -4559,7 +4601,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
45594601      Init = BM->addCompositeConstant (CompositeTy, Elts);
45604602    }
45614603    SPIRVType *VarTy = transPointerType (AT, SPIRV::SPIRAS_Constant);
4562-     SPIRVValue *Var = BM->addVariable (VarTy, /* isConstant*/   true ,
4604+     SPIRVValue *Var = BM->addVariable (VarTy, nullptr ,  /* isConstant*/   true ,
45634605                                      spv::internal::LinkageTypeInternal, Init,
45644606                                      " "  , StorageClassUniformConstant, nullptr );
45654607    SPIRVType *SourceTy =
@@ -6671,9 +6713,12 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
66716713          assert ((Pointee == Args[I] || !isa<Function>(Pointee)) &&
66726714                 " Illegal use of a function pointer type"  );
66736715        }
6674-         SPArgs.push_back (SPI->isOperandLiteral (I)
6675-                              ? cast<ConstantInt>(Args[I])->getZExtValue ()
6676-                              : transValue (Args[I], BB)->getId ());
6716+         if  (!SPI->isOperandLiteral (I)) {
6717+           SPIRVValue *Val = transValue (Args[I], BB);
6718+           SPArgs.push_back (Val->getId ());
6719+         } else  {
6720+           SPArgs.push_back (cast<ConstantInt>(Args[I])->getZExtValue ());
6721+         }
66776722      }
66786723      BM->addInstTemplate (SPI, SPArgs, BB, SPRetTy);
66796724      if  (!SPRetTy || !SPRetTy->isTypeStruct ())
0 commit comments