Skip to content

Commit f50ce99

Browse files
EbinJose2002sys-ce-bb
authored andcommitted
Takes param size and align from literal block in OpEnqueueKernel (#3123)
- Implemented the TODO in the visitCallEnqueueKernel function - Takes param size and align from literal block - If dynamic allocation is used store instructions are processed to obtain size and align - Modified test Original commit: KhronosGroup/SPIRV-LLVM-Translator@ff7db6ef1c71860
1 parent 6a9df73 commit f50ce99

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

llvm-spirv/lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,14 +1501,54 @@ void OCLToSPIRVBase::visitCallEnqueueKernel(CallInst *CI,
15011501
// Param: Pointer to block literal
15021502
Value *BlockLiteral = CI->getArgOperand(BlockFIdx + 1);
15031503
Args.push_back(BlockLiteral);
1504+
BlockLiteral = BlockLiteral->stripPointerCasts();
1505+
if (auto *GV = dyn_cast<GlobalVariable>(BlockLiteral)) {
1506+
assert(GV->hasInitializer() && "Block literal should have an initializer");
1507+
Constant *Init = GV->getInitializer();
1508+
if (auto *Struct = dyn_cast<ConstantStruct>(Init)) {
1509+
Constant *SizeConst = Struct->getOperand(0);
1510+
Constant *AlignConst = Struct->getOperand(1);
1511+
auto *SizeVal = dyn_cast<ConstantInt>(SizeConst);
1512+
auto *AlignVal = dyn_cast<ConstantInt>(AlignConst);
1513+
Args.push_back(SizeVal);
1514+
Args.push_back(AlignVal);
1515+
}
1516+
} else {
15041517

1505-
// Param Size: Size of block literal structure
1506-
// Param Aligment: Aligment of block literal structure
1507-
// TODO: these numbers should be obtained from block literal structure
1508-
Type *ParamType = getBlockStructType(BlockLiteral);
1509-
Args.push_back(getInt32(M, DL.getTypeStoreSize(ParamType)));
1510-
Args.push_back(getInt32(M, DL.getPrefTypeAlign(ParamType).value()));
1518+
Value *Base = getUnderlyingObject(BlockLiteral);
1519+
Value *ParamSizeVal = nullptr;
1520+
Value *ParamAlignVal = nullptr;
1521+
1522+
for (User *U : Base->users()) {
1523+
if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
1524+
if (GEP->getNumIndices() < 2)
1525+
continue;
1526+
auto *CI1 = dyn_cast<ConstantInt>(GEP->getOperand(2));
1527+
if (!CI1)
1528+
continue;
1529+
uint64_t FieldIndex = CI1->getZExtValue();
1530+
for (User *GEPUser : GEP->users()) {
1531+
if (auto *Store = dyn_cast<StoreInst>(GEPUser)) {
1532+
if (FieldIndex == 0)
1533+
ParamSizeVal = dyn_cast<ConstantInt>(Store->getValueOperand());
1534+
else if (FieldIndex == 1)
1535+
ParamAlignVal = dyn_cast<ConstantInt>(Store->getValueOperand());
1536+
}
1537+
}
1538+
}
1539+
if (ParamSizeVal && ParamAlignVal)
1540+
break;
1541+
}
1542+
Type *ParamType = getBlockStructType(BlockLiteral);
1543+
// Fallback to default if not found
1544+
if (!ParamSizeVal)
1545+
ParamSizeVal = getInt32(M, DL.getTypeStoreSize(ParamType));
1546+
if (!ParamAlignVal)
1547+
ParamAlignVal = getInt32(M, DL.getPrefTypeAlign(ParamType).value());
15111548

1549+
Args.push_back(ParamSizeVal);
1550+
Args.push_back(ParamAlignVal);
1551+
}
15121552
// Local sizes arguments: Sizes of block invoke arguments
15131553
// Clang 6.0 and higher generates local size operands as an array,
15141554
// so we need to unpack them

llvm-spirv/test/transcoding/enqueue_kernel.cl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
// CHECK-SPIRV: TypeInt [[Int32Ty:[0-9]+]] 32
3838
// CHECK-SPIRV: TypeInt [[Int8Ty:[0-9]+]] 8
3939
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt12:[0-9]+]] 12
40+
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt4:[0-9]+]] 4
4041
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt0:[0-9]+]] 0
41-
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt17:[0-9]+]] 21
42+
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt21:[0-9]+]] 21
4243
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt2:[0-9]+]] 2
43-
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt8:[0-9]+]] 8
44-
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt20:[0-9]+]] 24
44+
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt24:[0-9]+]] 24
4545

4646
// CHECK-SPIRV-TYPED-PTR: TypePointer [[Int8PtrGenTy:[0-9]+]] 8 [[Int8Ty]]
4747
// CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[Int8PtrGenTy:[0-9]+]] 8
@@ -78,7 +78,7 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
7878
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit1:[0-9]+]]
7979
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
8080
// CHECK-SPIRV-SAME: [[ConstInt0]] [[EventNull]] [[#]]
81-
// CHECK-SPIRV-SAME: [[#InvokeFunc1]] [[BlockLit1]] [[ConstInt17]] [[ConstInt8]]
81+
// CHECK-SPIRV-SAME: [[#InvokeFunc1]] [[BlockLit1]] [[ConstInt21]] [[ConstInt4]]
8282

8383
// CHECK-LLVM: [[Block2:%[0-9]+]] = addrspacecast ptr %block to ptr addrspace(4)
8484
// CHECK-LLVM: [[Block2Ptr:%[0-9]+]] = bitcast ptr addrspace(4) [[Block2]] to ptr addrspace(4)
@@ -98,7 +98,7 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
9898
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit2:[0-9]+]]
9999
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
100100
// CHECK-SPIRV-SAME: [[ConstInt2]] [[Event1]] [[Event2]]
101-
// CHECK-SPIRV-SAME: [[#InvokeFunc2]] [[BlockLit2]] [[ConstInt20]] [[ConstInt8]]
101+
// CHECK-SPIRV-SAME: [[#InvokeFunc2]] [[BlockLit2]] [[ConstInt24]] [[ConstInt4]]
102102

103103
// CHECK-LLVM: [[Block3:%[0-9]+]] = addrspacecast ptr %block4 to ptr addrspace(4)
104104
// CHECK-LLVM: [[Block3Ptr:%[0-9]+]] = bitcast ptr addrspace(4) [[Block3]] to ptr addrspace(4)
@@ -120,7 +120,7 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
120120
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit3:[0-9]+]] [[BlockLit3Tmp]]
121121
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
122122
// CHECK-SPIRV-SAME: [[ConstInt2]] [[Event1]] [[Event2]]
123-
// CHECK-SPIRV-SAME: [[#InvokeFunc3]] [[BlockLit3]] [[ConstInt12]] [[ConstInt8]]
123+
// CHECK-SPIRV-SAME: [[#InvokeFunc3]] [[BlockLit3]] [[ConstInt12]] [[ConstInt4]]
124124
// CHECK-SPIRV-SAME: [[LocalBuf31]]
125125

126126
// CHECK-LLVM: [[Block0Tmp:%[0-9]+]] = addrspacecast ptr addrspace(1) @__block_literal_global to ptr addrspace(4)
@@ -146,7 +146,7 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
146146
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit4:[0-9]+]] [[BlockLit4Tmp]]
147147
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
148148
// CHECK-SPIRV-SAME: [[ConstInt0]] [[#]] [[#]]
149-
// CHECK-SPIRV-SAME: [[#InvokeFunc4]] [[BlockLit4]] [[ConstInt12]] [[ConstInt8]]
149+
// CHECK-SPIRV-SAME: [[#InvokeFunc4]] [[BlockLit4]] [[ConstInt12]] [[ConstInt4]]
150150
// CHECK-SPIRV-SAME: [[LocalBuf41]] [[LocalBuf42]] [[LocalBuf43]]
151151

152152
// CHECK-LLVM: [[Block1Tmp:%[0-9]+]] = addrspacecast ptr addrspace(1) @__block_literal_global.1 to ptr addrspace(4)
@@ -167,7 +167,7 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
167167
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit5:[0-9]+]]
168168
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
169169
// CHECK-SPIRV-SAME: [[ConstInt0]] [[#]] [[Event1]]
170-
// CHECK-SPIRV-SAME: [[#InvokeFunc5]] [[BlockLit5]] [[ConstInt20]] [[ConstInt8]]
170+
// CHECK-SPIRV-SAME: [[#InvokeFunc5]] [[BlockLit5]] [[ConstInt24]] [[ConstInt4]]
171171

172172
// CHECK-LLVM: [[Block5:%[0-9]+]] = addrspacecast ptr %block15 to ptr addrspace(4)
173173
// CHECK-LLVM: [[Block5Ptr:%[0-9]+]] = bitcast ptr addrspace(4) [[Block5]] to ptr addrspace(4)

0 commit comments

Comments
 (0)