Skip to content

Commit 3882d24

Browse files
zuban32AlexeySachkov
authored andcommitted
Support function pointers in cast instructions
1 parent 08849f5 commit 3882d24

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,7 @@ SPIRVInstruction *LLVMToSPIRV::transCmpInst(CmpInst *Cmp, SPIRVBasicBlock *BB) {
696696
SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
697697
SPIRVBasicBlock *BB) {
698698
Op BOC = OpNop;
699+
SPIRVValue *Op = nullptr;
699700
if (auto Cast = dyn_cast<AddrSpaceCastInst>(U)) {
700701
if (Cast->getDestTy()->getPointerAddressSpace() == SPIRAS_Generic) {
701702
assert(Cast->getSrcTy()->getPointerAddressSpace() != SPIRAS_Constant &&
@@ -710,9 +711,21 @@ SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
710711
} else {
711712
auto OpCode = U->getOpcode();
712713
BOC = OpCodeMap::map(OpCode);
714+
715+
if (Function *F = dyn_cast<Function>(U->getOperand(0))) {
716+
if (!BM->checkExtension(ExtensionID::SPV_INTEL_function_pointers,
717+
SPIRVEC_FunctionPointers, toString(U)))
718+
return nullptr;
719+
assert(BOC == OpConvertPtrToU &&
720+
"Illegal unary operation on function pointer");
721+
Op = BM->addFunctionPointerINTELInst(
722+
transType(F->getType()),
723+
static_cast<SPIRVFunction *>(transValue(F, BB)), BB);
724+
}
713725
}
714726

715-
auto Op = transValue(U->getOperand(0), BB);
727+
if (!Op)
728+
Op = transValue(U->getOperand(0), BB);
716729
return BM->addUnaryInst(transBoolOpCode(Op, BOC), transType(U->getType()), Op,
717730
BB);
718731
}

test/constexpr_vector.ll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@
4343
; CHECK-LLVM-NEXT: ret void
4444
; CHECK-LLVM-NEXT: }
4545

46-
; RUN: llvm-as < %s | llvm-spirv -spirv-text | FileCheck %s --check-prefix=CHECK-SPIRV
46+
; RUN: llvm-as < %s | llvm-spirv -spirv-text --spirv-ext=+SPV_INTEL_function_pointers | FileCheck %s --check-prefix=CHECK-SPIRV
4747

48-
; CHECK-SPIRV: 4 Name [[Funcs:[0-9]+]] "Funcs"
48+
; CHECK-SPIRV-DAG: 4 Name [[Funcs:[0-9]+]] "Funcs"
49+
; CHECK-SPIRV-DAG: 6 Name [[F1:[0-9+]]] "_Z2f1u2CMvb32_j"
50+
; CHECK-SPIRV-DAG: 6 Name [[F2:[0-9+]]] "_Z2f2u2CMvb32_j"
4951

5052
; CHECK-SPIRV-DAG: 4 TypeInt [[TypeInt8:[0-9]+]] 8 0
5153
; CHECK-SPIRV-DAG: 4 TypeInt [[TypeInt32:[0-9]+]] 32 0
@@ -55,7 +57,8 @@
5557
; CHECK-SPIRV-DAG: 4 TypePointer [[StorePtr:[0-9]+]] 7 [[TypeVec16]]
5658
; CHECK-SPIRV-DAG: 3 Undef [[TypeVec16]] [[TypeUndef:[0-9]+]]
5759

58-
; CHECK-SPIRV: 4 ConvertPtrToU [[TypeInt64]] [[Ptr1:[0-9]+]] {{[0-9]+}}
60+
; CHECK-SPIRV: 4 FunctionPointerINTEL [[FuncPtrTy:[0-9]+]] [[F1Ptr:[0-9]+]] [[F1]]
61+
; CHECK-SPIRV-NEXT: 4 ConvertPtrToU [[TypeInt64]] [[Ptr1:[0-9]+]] [[F1Ptr]]
5962
; CHECK-SPIRV-NEXT: 4 Bitcast [[TypeVec8]] [[Vec1:[0-9]+]] [[Ptr1]]
6063
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v00:[0-9]+]] [[Vec1]] 0
6164
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v01:[0-9]+]] [[Vec1]] 1
@@ -65,7 +68,8 @@
6568
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v05:[0-9]+]] [[Vec1]] 5
6669
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v06:[0-9]+]] [[Vec1]] 6
6770
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v07:[0-9]+]] [[Vec1]] 7
68-
; CHECK-SPIRV-NEXT: 4 ConvertPtrToU [[TypeInt64]] [[Ptr2:[0-9]+]] {{[0-9]+}}
71+
; CHECK-SPIRV-NEXT: 4 FunctionPointerINTEL [[FuncPtrTy]] [[F2Ptr:[0-9]+]] [[F2]]
72+
; CHECK-SPIRV-NEXT: 4 ConvertPtrToU [[TypeInt64]] [[Ptr2:[0-9]+]] [[F2Ptr]]
6973
; CHECK-SPIRV-NEXT: 4 Bitcast [[TypeVec8]] [[Vec2:[0-9]+]] [[Ptr2]]
7074
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v10:[0-9]+]] [[Vec2]] 0
7175
; CHECK-SPIRV-NEXT: 5 CompositeExtract [[TypeInt8]] [[v11:[0-9]+]] [[Vec2]] 1

0 commit comments

Comments
 (0)