Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor handling of untyped pointers in builtins reverse translation #2924

Merged
merged 7 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3548,40 +3548,65 @@ Instruction *SPIRVToLLVM::transBuiltinFromInst(const std::string &FuncName,
transType(AI->getSemanticType()),
SPIRSPIRVAddrSpaceMap::rmap(
BI->getValueType(Ops[Ptr]->getId())->getPointerStorageClass()));
} else if (OC == spv::OpCooperativeMatrixStoreKHR ||
OC == spv::internal::OpJointMatrixStoreINTEL ||
OC == spv::internal::OpCooperativeMatrixStoreCheckedINTEL ||
OC == spv::internal::OpJointMatrixLoadINTEL ||
OC == spv::OpCompositeConstruct ||
OC == spv::internal::OpCooperativeMatrixApplyFunctionINTEL) {
auto *Val = transValue(Ops[Ptr], BB->getParent(), BB);
Val = Val->stripPointerCasts();
if (auto *GEP = dyn_cast<GetElementPtrInst>(Val))
ArgTys[Ptr] = TypedPointerType::get(
GEP->getSourceElementType(),
SPIRSPIRVAddrSpaceMap::rmap(
BI->getValueType(Ops[Ptr]->getId())->getPointerStorageClass()));
else if (auto *AI = dyn_cast<AllocaInst>(Val))
ArgTys[Ptr] = TypedPointerType::get(
AI->getAllocatedType(),
SPIRSPIRVAddrSpaceMap::rmap(
BI->getValueType(Ops[Ptr]->getId())->getPointerStorageClass()));
else if (isa<Argument>(Val) && !RetTy->isVoidTy()) {
// Pointer could be a function parameter. Assume that the type of the
// pointer is the same as the return type.
Type *Ty = nullptr;
// it return type is array type, assign its element type to Ty
if (RetTy->isArrayTy())
Ty = RetTy->getArrayElementType();
else if (RetTy->isVectorTy())
Ty = cast<VectorType>(RetTy)->getElementType();
else
Ty = RetTy;
}
}

ArgTys[Ptr] = TypedPointerType::get(
Ty,
SPIRSPIRVAddrSpaceMap::rmap(
BI->getValueType(Ops[Ptr]->getId())->getPointerStorageClass()));
for (unsigned I = 0; I < ArgTys.size(); I++) {
if (isa<PointerType>(ArgTys[I])) {
SPIRVType *OpTy = BI->getValueType(Ops[I]->getId());
// `Param` must be a pointer to an 8-bit integer type scalar.
// Avoid demangling for this argument if it's a pointer to get `Pc`
// mangling.
if (OC == OpEnqueueKernel && I == 7) {
if (ArgTys[I]->isPointerTy())
continue;
}
if (OpTy->isTypeUntypedPointerKHR()) {
auto *Val = transValue(Ops[I], BB->getParent(), BB);
Val = Val->stripPointerCasts();
if (isUntypedAccessChainOpCode(Ops[I]->getOpCode())) {
SPIRVType *BaseTy =
reinterpret_cast<SPIRVAccessChainBase *>(Ops[I])->getBaseType();

Type *Ty = nullptr;
if (BaseTy->isTypeArray())
Ty = transType(BaseTy->getArrayElementType());
else if (BaseTy->isTypeVector())
Ty = transType(BaseTy->getVectorComponentType());
else
Ty = transType(BaseTy);
ArgTys[I] = TypedPointerType::get(
Ty, SPIRSPIRVAddrSpaceMap::rmap(OpTy->getPointerStorageClass()));
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(Val)) {
ArgTys[I] = TypedPointerType::get(
GEP->getSourceElementType(),
SPIRSPIRVAddrSpaceMap::rmap(OpTy->getPointerStorageClass()));
} else if (Ops[I]->getOpCode() == OpUntypedVariableKHR) {
SPIRVUntypedVariableKHR *UV =
static_cast<SPIRVUntypedVariableKHR *>(Ops[I]);
Type *Ty = transType(UV->getDataType());
ArgTys[I] = TypedPointerType::get(
Ty, SPIRSPIRVAddrSpaceMap::rmap(OpTy->getPointerStorageClass()));
} else if (auto *AI = dyn_cast<AllocaInst>(Val)) {
ArgTys[I] = TypedPointerType::get(
AI->getAllocatedType(),
SPIRSPIRVAddrSpaceMap::rmap(OpTy->getPointerStorageClass()));
} else if (Ops[I]->getOpCode() == OpFunctionParameter &&
!RetTy->isVoidTy()) {
// Pointer could be a function parameter. Assume that the type of
// the pointer is the same as the return type.
Type *Ty = nullptr;
// it return type is array type, assign its element type to Ty
if (RetTy->isArrayTy())
Ty = RetTy->getArrayElementType();
else if (RetTy->isVectorTy())
Ty = cast<VectorType>(RetTy)->getElementType();
else
Ty = RetTy;

ArgTys[I] = TypedPointerType::get(
Ty, SPIRSPIRVAddrSpaceMap::rmap(OpTy->getPointerStorageClass()));
}
}
}
}
Expand Down
31 changes: 24 additions & 7 deletions test/transcoding/enqueue_kernel.cl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_cc1 -triple spir-unknown-unknown -O0 -cl-std=CL2.0 -emit-llvm-bc %s -o %t.bc
// RUN: llvm-spirv %t.bc -spirv-text -o %t.spv.txt
// RUN: FileCheck < %t.spv.txt %s --check-prefix=CHECK-SPIRV
// RUN: FileCheck < %t.spv.txt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR
// RUN: llvm-spirv %t.bc -o %t.spv
// RUN: spirv-val %t.spv
// RUN: llvm-spirv -r %t.spv --spirv-target-env CL2.0 -o %t.rev.bc
Expand All @@ -10,6 +10,17 @@
// RUN: llvm-dis %t.rev.bc
// RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-SPV-IR

// RUN: llvm-spirv %t.bc -spirv-text -o %t.spv.txt --spirv-ext=+SPV_KHR_untyped_pointers
// RUN: FileCheck < %t.spv.txt %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR
// RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
// RUN: spirv-val %t.spv
// RUN: llvm-spirv -r %t.spv --spirv-target-env CL2.0 -o %t.rev.bc
// RUN: llvm-dis %t.rev.bc
// RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
// RUN: llvm-spirv -r %t.spv --spirv-target-env SPV-IR -o %t.rev.bc
// RUN: llvm-dis %t.rev.bc
// RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-SPV-IR

// CHECK-SPIRV: EntryPoint {{[0-9]+}} [[BlockKer1:[0-9]+]] "__device_side_enqueue_block_invoke_kernel"
// CHECK-SPIRV: EntryPoint {{[0-9]+}} [[BlockKer2:[0-9]+]] "__device_side_enqueue_block_invoke_2_kernel"
// CHECK-SPIRV: EntryPoint {{[0-9]+}} [[BlockKer3:[0-9]+]] "__device_side_enqueue_block_invoke_3_kernel"
Expand All @@ -32,9 +43,11 @@
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt8:[0-9]+]] 8
// CHECK-SPIRV: Constant [[Int32Ty]] [[ConstInt20:[0-9]+]] 24

// CHECK-SPIRV: TypePointer [[Int8PtrGenTy:[0-9]+]] 8 [[Int8Ty]]
// CHECK-SPIRV-TYPED-PTR: TypePointer [[Int8PtrGenTy:[0-9]+]] 8 [[Int8Ty]]
// CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[Int8PtrGenTy:[0-9]+]] 8
// CHECK-SPIRV: TypeVoid [[VoidTy:[0-9]+]]
// CHECK-SPIRV: TypePointer [[Int32LocPtrTy:[0-9]+]] 7 [[Int32Ty]]
// CHECK-SPIRV-TYPED-PTR: TypePointer [[Int32LocPtrTy:[0-9]+]] 7 [[Int32Ty]]
// CHECK-SPIRV-UNTYPED-PTR: TypeUntypedPointerKHR [[Int32LocPtrTy:[0-9]+]] 7
// CHECK-SPIRV: TypeDeviceEvent [[EventTy:[0-9]+]]
// CHECK-SPIRV: TypePointer [[EventPtrTy:[0-9]+]] 8 [[EventTy]]
// CHECK-SPIRV: TypeFunction [[BlockTy1:[0-9]+]] [[VoidTy]] [[Int8PtrGenTy]]
Expand Down Expand Up @@ -101,7 +114,8 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {
// Emits global block literal and block kernel.
// CHECK-SPIRV: PtrCastToGeneric [[EventPtrTy]] [[Event1:[0-9]+]]
// CHECK-SPIRV: PtrCastToGeneric [[EventPtrTy]] [[Event2:[0-9]+]]
// CHECK-SPIRV-COUNT-2: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf31:[0-9]+]]
// CHECK-SPIRV-TYPED-PTR-COUNT-2: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf31:[0-9]+]]
// CHECK-SPIRV-UNTYPED-PTR-COUNT-2: UntypedPtrAccessChainKHR [[Int32LocPtrTy]] [[LocalBuf31:[0-9]+]]
// CHECK-SPIRV: PtrCastToGeneric {{[0-9]+}} [[BlockLit3Tmp:[0-9]+]] [[BlockGlb1:[0-9]+]]
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit3:[0-9]+]] [[BlockLit3Tmp]]
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
Expand All @@ -122,9 +136,12 @@ kernel void device_side_enqueue(global int *a, global int *b, int i, char c0) {

// Emits global block literal and block kernel.

// CHECK-SPIRV-COUNT-4: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf41:[0-9]+]]
// CHECK-SPIRV: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf42:[0-9]+]]
// CHECK-SPIRV: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf43:[0-9]+]]
// CHECK-SPIRV-TYPED-PTR-COUNT-4: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf41:[0-9]+]]
// CHECK-SPIRV-TYPED-PTR: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf42:[0-9]+]]
// CHECK-SPIRV-TYPED-PTR: PtrAccessChain [[Int32LocPtrTy]] [[LocalBuf43:[0-9]+]]
// CHECK-SPIRV-UNTYPED-PTR-COUNT-4: UntypedPtrAccessChainKHR [[Int32LocPtrTy]] [[LocalBuf41:[0-9]+]]
// CHECK-SPIRV-UNTYPED-PTR: UntypedPtrAccessChainKHR [[Int32LocPtrTy]] [[LocalBuf42:[0-9]+]]
// CHECK-SPIRV-UNTYPED-PTR: UntypedPtrAccessChainKHR [[Int32LocPtrTy]] [[LocalBuf43:[0-9]+]]
// CHECK-SPIRV: PtrCastToGeneric {{[0-9]+}} [[BlockLit4Tmp:[0-9]+]] [[BlockGlb2:[0-9]+]]
// CHECK-SPIRV: Bitcast [[Int8PtrGenTy]] [[BlockLit4:[0-9]+]] [[BlockLit4Tmp]]
// CHECK-SPIRV: EnqueueKernel [[Int32Ty]] [[#]] [[#]] [[#]] [[#]]
Expand Down
Loading