Skip to content

Commit ce0dc32

Browse files
authored
[SPIR-V] Reland SPV_KHR_untyped_pointers commits (#15507)
They were reverted in pulldown PR #15464 because of the regressions. Don't use this extension in the SYCL tests as the extension is not fully implemented yet. Commits: KhronosGroup/SPIRV-LLVM-Translator@7dacb7cdedc9c01 KhronosGroup/SPIRV-LLVM-Translator@484e4070adb0a95
1 parent e58e17b commit ce0dc32

29 files changed

+696
-124
lines changed

llvm-spirv/include/LLVMSPIRVExtensions.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ EXT(SPV_KHR_subgroup_rotate)
1717
EXT(SPV_KHR_non_semantic_info)
1818
EXT(SPV_KHR_shader_clock)
1919
EXT(SPV_KHR_cooperative_matrix)
20+
EXT(SPV_KHR_untyped_pointers)
2021
EXT(SPV_INTEL_subgroups)
2122
EXT(SPV_INTEL_media_block_io)
2223
EXT(SPV_INTEL_device_side_avc_motion_estimation)

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,11 @@ Type *SPIRVToLLVM::transType(SPIRVType *T, bool UseTPT) {
357357
return TypedPointerType::get(ElementTy, AS);
358358
return mapType(T, PointerType::get(ElementTy, AS));
359359
}
360+
case OpTypeUntypedPointerKHR: {
361+
const unsigned AS =
362+
SPIRSPIRVAddrSpaceMap::rmap(T->getPointerStorageClass());
363+
return mapType(T, PointerType::get(*Context, AS));
364+
}
360365
case OpTypeVector:
361366
return mapType(T,
362367
FixedVectorType::get(transType(T->getVectorComponentType()),
@@ -560,6 +565,8 @@ std::string SPIRVToLLVM::transTypeToOCLTypeName(SPIRVType *T, bool IsSigned) {
560565
}
561566
return transTypeToOCLTypeName(ET) + "*";
562567
}
568+
case OpTypeUntypedPointerKHR:
569+
return "int*";
563570
case OpTypeVector:
564571
return transTypeToOCLTypeName(T->getVectorComponentType()) +
565572
T->getVectorComponentCount();
@@ -1522,9 +1529,15 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
15221529
case OpUndef:
15231530
return mapValue(BV, UndefValue::get(transType(BV->getType())));
15241531

1525-
case OpVariable: {
1526-
auto *BVar = static_cast<SPIRVVariable *>(BV);
1527-
auto *PreTransTy = BVar->getType()->getPointerElementType();
1532+
case OpVariable:
1533+
case OpUntypedVariableKHR: {
1534+
auto *BVar = static_cast<SPIRVVariableBase *>(BV);
1535+
SPIRVType *PreTransTy = BVar->getType()->getPointerElementType();
1536+
if (BVar->getType()->isTypeUntypedPointerKHR()) {
1537+
auto *UntypedVar = static_cast<SPIRVUntypedVariableKHR *>(BVar);
1538+
if (SPIRVType *DT = UntypedVar->getDataType())
1539+
PreTransTy = DT;
1540+
}
15281541
auto *Ty = transType(PreTransTy);
15291542
bool IsConst = BVar->isConstant();
15301543
llvm::GlobalValue::LinkageTypes LinkageTy = transLinkageType(BVar);
@@ -4055,7 +4068,7 @@ bool SPIRVToLLVM::transDecoration(SPIRVValue *BV, Value *V) {
40554068
return true;
40564069
}
40574070

4058-
void SPIRVToLLVM::transGlobalCtorDtors(SPIRVVariable *BV) {
4071+
void SPIRVToLLVM::transGlobalCtorDtors(SPIRVVariableBase *BV) {
40594072
if (BV->getName() != "llvm.global_ctors" &&
40604073
BV->getName() != "llvm.global_dtors")
40614074
return;
@@ -4900,15 +4913,17 @@ SPIRVToLLVM::transLinkageType(const SPIRVValue *V) {
49004913
return GlobalValue::ExternalLinkage;
49014914
}
49024915
// Variable declaration
4903-
if (V->getOpCode() == OpVariable) {
4904-
if (static_cast<const SPIRVVariable *>(V)->getInitializer() == 0)
4916+
if (V->getOpCode() == OpVariable ||
4917+
V->getOpCode() == OpUntypedVariableKHR) {
4918+
if (static_cast<const SPIRVVariableBase *>(V)->getInitializer() == 0)
49054919
return GlobalValue::ExternalLinkage;
49064920
}
49074921
// Definition
49084922
return GlobalValue::AvailableExternallyLinkage;
49094923
case LinkageTypeExport:
4910-
if (V->getOpCode() == OpVariable) {
4911-
if (static_cast<const SPIRVVariable *>(V)->getInitializer() == 0)
4924+
if (V->getOpCode() == OpVariable ||
4925+
V->getOpCode() == OpUntypedVariableKHR) {
4926+
if (static_cast<const SPIRVVariableBase *>(V)->getInitializer() == 0)
49124927
// Tentative definition
49134928
return GlobalValue::CommonLinkage;
49144929
}

llvm-spirv/lib/SPIRV/SPIRVReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class SPIRVToLLVM : private BuiltinCallHelper {
251251

252252
void transUserSemantic(SPIRV::SPIRVFunction *Fun);
253253
void transGlobalAnnotations();
254-
void transGlobalCtorDtors(SPIRVVariable *BV);
254+
void transGlobalCtorDtors(SPIRVVariableBase *BV);
255255
void createCXXStructor(const char *ListName,
256256
SmallVectorImpl<Function *> &Funcs);
257257
void transIntelFPGADecorations(SPIRVValue *BV, Value *V);

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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())

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ SPIRVInstruction *SPIRVBasicBlock::getVariableInsertionPoint() const {
9494
isa<OpNoLine>(Inst) ||
9595
// Note: OpVariable and OpPhi instructions do not belong to the
9696
// same block in a valid SPIR-V module.
97-
isa<OpPhi>(Inst));
97+
isa<OpPhi>(Inst) || isa<OpUntypedVariableKHR>(Inst));
9898
});
9999
if (IP == InstVec.end())
100100
return nullptr;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ class SPIRVBasicBlock : public SPIRVValue {
7777
const SPIRVInstruction *getTerminateInstr() const {
7878
return InstVec.empty() ? nullptr : InstVec.back();
7979
}
80-
// OpVariable instructions must be the first instructions in the block,
80+
// Variables must be the first instructions in the block,
8181
// intermixed with OpLine and OpNoLine instructions. Return first instruction
82-
// not being an OpVariable, OpLine or OpNoLine.
82+
// not being an OpVariable, OpUntypedVariableKHR, OpLine or OpNoLine.
8383
SPIRVInstruction *getVariableInsertionPoint() const;
8484

8585
void setScope(SPIRVEntry *Scope) override;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ SPIRVEntry::getDecorationIds(Decoration Kind) const {
559559
}
560560

561561
bool SPIRVEntry::hasLinkageType() const {
562-
return OpCode == OpFunction || OpCode == OpVariable;
562+
return OpCode == OpFunction || OpCode == OpVariable ||
563+
OpCode == OpUntypedVariableKHR;
563564
}
564565

565566
bool SPIRVEntry::isExtInst(const SPIRVExtInstSetKind InstSet) const {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ class SPIRVEntry {
339339
bool isUndef() const { return OpCode == OpUndef; }
340340
bool isControlBarrier() const { return OpCode == OpControlBarrier; }
341341
bool isMemoryBarrier() const { return OpCode == OpMemoryBarrier; }
342-
bool isVariable() const { return OpCode == OpVariable; }
342+
bool isVariable() const {
343+
return OpCode == OpVariable || OpCode == OpUntypedVariableKHR;
344+
}
343345
bool isEndOfBlock() const;
344346
virtual bool isInst() const { return false; }
345347
virtual bool isOperandLiteral(unsigned Index) const {

0 commit comments

Comments
 (0)