@@ -417,8 +417,8 @@ SPIRVType *LLVMToSPIRVBase::transType(Type *T) {
417
417
// A pointer to image or pipe type in LLVM is translated to a SPIRV
418
418
// (non-pointer) image or pipe type.
419
419
if (T->isPointerTy ()) {
420
- auto *ET = Type::getInt8Ty (T->getContext ());
421
420
auto AddrSpc = T->getPointerAddressSpace ();
421
+ auto *ET = Type::getInt8Ty (T->getContext ());
422
422
return transPointerType (ET, AddrSpc);
423
423
}
424
424
@@ -720,7 +720,6 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(Type *ET, unsigned AddrSpc) {
720
720
transType (ET)));
721
721
}
722
722
} else {
723
- SPIRVType *ElementType = transType (ET);
724
723
// ET, as a recursive type, may contain exactly the same pointer T, so it
725
724
// may happen that after translation of ET we already have translated T,
726
725
// 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) {
729
728
if (Loc != PointeeTypeMap.end ()) {
730
729
return Loc->second ;
731
730
}
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
+ }
733
742
PointeeTypeMap[TypeKey] = TranslatedTy;
734
743
return TranslatedTy;
735
744
}
@@ -744,8 +753,16 @@ SPIRVType *LLVMToSPIRVBase::transPointerType(SPIRVType *ET, unsigned AddrSpc) {
744
753
if (Loc != PointeeTypeMap.end ())
745
754
return Loc->second ;
746
755
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
+ }
749
766
PointeeTypeMap[TypeKey] = TranslatedTy;
750
767
return TranslatedTy;
751
768
}
@@ -1312,7 +1329,8 @@ SPIRVValue *LLVMToSPIRVBase::transConstantUse(Constant *C,
1312
1329
if (Trans->getType () == ExpectedType || Trans->getType ()->isTypePipeStorage ())
1313
1330
return Trans;
1314
1331
1315
- assert (C->getType ()->isPointerTy () &&
1332
+ assert ((C->getType ()->isPointerTy () ||
1333
+ ExpectedType->isTypeUntypedPointerKHR ()) &&
1316
1334
" Only pointer type mismatches should be possible" );
1317
1335
// In the common case of strings ([N x i8] GVs), see if we can emit a GEP
1318
1336
// instruction.
@@ -2050,8 +2068,12 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2050
2068
}
2051
2069
}
2052
2070
}
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
+ }
2055
2077
}
2056
2078
2057
2079
SPIRVStorageClassKind StorageClass;
@@ -2084,9 +2106,12 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2084
2106
}
2085
2107
2086
2108
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 ));
2090
2115
2091
2116
if (IsVectorCompute) {
2092
2117
BVar->addDecorate (DecorationVectorComputeVariableINTEL);
@@ -2196,8 +2221,13 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2196
2221
MemoryAccessNoAliasINTELMaskMask);
2197
2222
if (MemoryAccess.front () == 0 )
2198
2223
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 ));
2201
2231
}
2202
2232
2203
2233
if (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
@@ -2270,8 +2300,9 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2270
2300
StorageClassFunction,
2271
2301
BM->addArrayType (transType (Alc->getAllocatedType ()), Length));
2272
2302
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);
2275
2306
// Manually set alignment. OpBitcast created below will be decorated as
2276
2307
// that's the SPIR-V value mapped to the original LLVM one.
2277
2308
transAlign (Alc, Arr);
@@ -2295,7 +2326,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2295
2326
TranslatedTy->getPointerElementType ())
2296
2327
: TranslatedTy;
2297
2328
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 ,
2299
2333
Alc->getName ().str (), StorageClassFunction, BB);
2300
2334
if (V->getType ()->getPointerAddressSpace () == SPIRAS_Generic) {
2301
2335
SPIRVValue *Cast =
@@ -2407,14 +2441,17 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
2407
2441
2408
2442
if (auto *Phi = dyn_cast<PHINode>(V)) {
2409
2443
std::vector<SPIRVValue *> IncomingPairs;
2444
+ SPIRVType *Ty = transScavengedType (Phi);
2410
2445
2411
2446
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);
2414
2452
IncomingPairs.push_back (transValue (Phi->getIncomingBlock (I), nullptr ));
2415
2453
}
2416
- return mapValue (V,
2417
- BM->addPhiInst (transScavengedType (Phi), IncomingPairs, BB));
2454
+ return mapValue (V, BM->addPhiInst (Ty, IncomingPairs, BB));
2418
2455
}
2419
2456
2420
2457
if (auto *Ext = dyn_cast<ExtractValueInst>(V)) {
@@ -2737,7 +2774,7 @@ void checkIsGlobalVar(SPIRVEntry *E, Decoration Dec) {
2737
2774
E->getErrorLog ().checkError (E->isVariable (), SPIRVEC_InvalidModule, ErrStr);
2738
2775
2739
2776
auto AddrSpace = SPIRSPIRVAddrSpaceMap::rmap (
2740
- static_cast <SPIRVVariable *>(E)->getStorageClass ());
2777
+ static_cast <SPIRVVariableBase *>(E)->getStorageClass ());
2741
2778
ErrStr += " in a global (module) scope" ;
2742
2779
E->getErrorLog ().checkError (AddrSpace == SPIRAS_Global, SPIRVEC_InvalidModule,
2743
2780
ErrStr);
@@ -2885,10 +2922,11 @@ static void transMetadataDecorations(Metadata *MD, SPIRVValue *Target) {
2885
2922
case spv::internal::DecorationInitModeINTEL:
2886
2923
case DecorationInitModeINTEL: {
2887
2924
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" );
2892
2930
2893
2931
ErrLog.checkError (NumOperands == 2 , SPIRVEC_InvalidLlvmModule,
2894
2932
" InitModeINTEL requires exactly 1 extra operand" );
@@ -4130,14 +4168,18 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
4130
4168
SPIRVType *FTy = transType (II->getType ()->getStructElementType (0 ));
4131
4169
SPIRVTypePointer *ITy = static_cast <SPIRVTypePointer *>(transPointerType (
4132
4170
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
+ }
4138
4176
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);
4141
4183
4142
4184
std::vector<SPIRVValue *> Ops{transValue (II->getArgOperand (0 ), BB), IntVal};
4143
4185
@@ -4559,7 +4601,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
4559
4601
Init = BM->addCompositeConstant (CompositeTy, Elts);
4560
4602
}
4561
4603
SPIRVType *VarTy = transPointerType (AT, SPIRV::SPIRAS_Constant);
4562
- SPIRVValue *Var = BM->addVariable (VarTy, /* isConstant*/ true ,
4604
+ SPIRVValue *Var = BM->addVariable (VarTy, nullptr , /* isConstant*/ true ,
4563
4605
spv::internal::LinkageTypeInternal, Init,
4564
4606
" " , StorageClassUniformConstant, nullptr );
4565
4607
SPIRVType *SourceTy =
@@ -6671,9 +6713,12 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
6671
6713
assert ((Pointee == Args[I] || !isa<Function>(Pointee)) &&
6672
6714
" Illegal use of a function pointer type" );
6673
6715
}
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
+ }
6677
6722
}
6678
6723
BM->addInstTemplate (SPI, SPArgs, BB, SPRetTy);
6679
6724
if (!SPRetTy || !SPRetTy->isTypeStruct ())
0 commit comments