From 2722a405e89a878411209620817d97e72e241e46 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Sat, 28 Dec 2024 18:10:43 -0800 Subject: [PATCH 1/6] handle bitcasts between pointers and scalar integers --- lib/SPIRV/SPIRVReader.cpp | 9 +++++++++ test/OpBitcast_ptr_scalar.spvasm | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 test/OpBitcast_ptr_scalar.spvasm diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 0c4f53793..cef1e9213 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1085,6 +1085,15 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, case OpFConvert: CO = IsExt ? Instruction::FPExt : Instruction::FPTrunc; break; + case OpBitcast: + if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { + CO = Instruction::PtrToInt; + } else if (!Src->getType()->isPointerTy() && Dst->isPointerTy()) { + CO = Instruction::IntToPtr; + } else { + CO = Instruction::BitCast; + } + break; default: CO = static_cast(OpCodeMap::rmap(BC->getOpCode())); } diff --git a/test/OpBitcast_ptr_scalar.spvasm b/test/OpBitcast_ptr_scalar.spvasm new file mode 100644 index 000000000..2e57d5d69 --- /dev/null +++ b/test/OpBitcast_ptr_scalar.spvasm @@ -0,0 +1,31 @@ +; Check support of OpBitcast with pointer operands +; Converts to scalar integers, which is supported by all SPIR-V versions + +; REQUIRES: spirv-as +; RUN: spirv-as --target-env spv1.0 -o %t.spv %s +; RUN: spirv-val %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.rev.bc +; RUN: llvm-dis %t.rev.bc +; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM + OpCapability Addresses + OpCapability Kernel + OpCapability Int64 + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %kernel "test" + %uint = OpTypeInt 32 0 + %ulong = OpTypeInt 64 0 + %void = OpTypeVoid + %pptr_int = OpTypePointer Function %uint + %kernel_sig = OpTypeFunction %void + %kernel = OpFunction %void None %kernel_sig + %entry = OpLabel + %srcptr = OpVariable %pptr_int Function + %dstint = OpBitcast %ulong %srcptr + %dstptr = OpBitcast %pptr_int %dstint + OpReturn + OpFunctionEnd + + +; CHECK-LLVM: [[SRCPTR:%[a-z0-9.]+]] = alloca i32, align 4 +; CHECK-LLVM: [[DSTINT:%[a-z0-9.]+]] = ptrtoint ptr [[SRCPTR]] to i64 +; CHECK-LLVM: [[DSTPTR:%[a-z0-9.]+]] = inttoptr i64 [[DSTINT]] to ptr From 2decbf4c3d0bc988269ad39d4083a37476b13ba6 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Sat, 28 Dec 2024 19:09:59 -0800 Subject: [PATCH 2/6] handle bitcasts between pointers and vectors of 32-bit integers --- lib/SPIRV/SPIRVReader.cpp | 28 +++++++++++++++++++++++--- test/OpBitcast_ptr_vector.spvasm | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 test/OpBitcast_ptr_vector.spvasm diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index cef1e9213..6983e567a 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1086,12 +1086,34 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, CO = IsExt ? Instruction::FPExt : Instruction::FPTrunc; break; case OpBitcast: + CO = Instruction::BitCast; if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { - CO = Instruction::PtrToInt; + if (auto* DstVecTy = dyn_cast(Dst)) { + assert(DstVecTy->getElementType()->isIntegerTy(32) && + DstVecTy->getNumElements() == 2 && + "Expected vector of two 32-bit integer components"); + auto *Int64Ty = Type::getInt64Ty(BB->getContext()); + if (BB) { + Src = CastInst::CreatePointerCast(Src, Int64Ty, "", BB); + } else { + Src = ConstantExpr::getPointerCast(dyn_cast(Src), Int64Ty); + } + } else { + CO = Instruction::PtrToInt; + } } else if (!Src->getType()->isPointerTy() && Dst->isPointerTy()) { + if (auto* SrcVecTy = dyn_cast(Src->getType())) { + assert(SrcVecTy->getElementType()->isIntegerTy(32) && + SrcVecTy->getNumElements() == 2 && + "Expected vector of two 32-bit integer components"); + auto *Int64Ty = Type::getInt64Ty(BB->getContext()); + if (BB) { + Src = CastInst::Create(Instruction::BitCast, Src, Int64Ty, "", BB); + } else { + Src = ConstantExpr::getBitCast(dyn_cast(Src), Int64Ty); + } + } CO = Instruction::IntToPtr; - } else { - CO = Instruction::BitCast; } break; default: diff --git a/test/OpBitcast_ptr_vector.spvasm b/test/OpBitcast_ptr_vector.spvasm new file mode 100644 index 000000000..c3c2aaf0a --- /dev/null +++ b/test/OpBitcast_ptr_vector.spvasm @@ -0,0 +1,34 @@ +; Check support of OpBitcast with pointer operands +; Converts to vectors of integers, which is supported by SPIR-V 1.5 + +; REQUIRES: spirv-as +; RUN: spirv-as --target-env spv1.5 -o %t.spv %s +; RUN: spirv-val %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.rev.bc +; RUN: llvm-dis %t.rev.bc +; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM + OpCapability Addresses + OpCapability Kernel + OpCapability Int64 + OpMemoryModel Physical64 OpenCL + OpEntryPoint Kernel %kernel "test" + %uint = OpTypeInt 32 0 + %ulong = OpTypeInt 64 0 + %uint2 = OpTypeVector %uint 2 + %void = OpTypeVoid + %pptr_int = OpTypePointer Function %uint + %kernel_sig = OpTypeFunction %void + %kernel = OpFunction %void None %kernel_sig + %entry = OpLabel + %srcptr = OpVariable %pptr_int Function + %dstint2 = OpBitcast %uint2 %srcptr + %dstptr = OpBitcast %pptr_int %dstint2 + OpReturn + OpFunctionEnd + + +; CHECK-LLVM: [[SRCPTR:%[a-z0-9.]+]] = alloca i32, align 4 +; CHECK-LLVM: [[TMPLONG0:%[a-z0-9.]+]] = ptrtoint ptr [[SRCPTR]] to i64 +; CHECK-LLVM: [[DSTINT2:%[a-z0-9.]+]] = bitcast i64 [[TMPLONG0]] to <2 x i32> +; CHECK-LLVM: [[TMPLONG1:%[a-z0-9.]+]] = bitcast <2 x i32> [[DSTINT2]] to i64 +; CHECK-LLVM: [[DSTPTR:%[a-z0-9.]+]] = inttoptr i64 [[TMPLONG1]] to ptr From 145a964d4c2d115d638d029ba39f40c2508f5553 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Sun, 29 Dec 2024 12:41:50 -0800 Subject: [PATCH 3/6] fix formatting --- lib/SPIRV/SPIRVReader.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 6983e567a..21cbf0fbd 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1088,7 +1088,7 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, case OpBitcast: CO = Instruction::BitCast; if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { - if (auto* DstVecTy = dyn_cast(Dst)) { + if (auto *DstVecTy = dyn_cast(Dst)) { assert(DstVecTy->getElementType()->isIntegerTy(32) && DstVecTy->getNumElements() == 2 && "Expected vector of two 32-bit integer components"); @@ -1102,7 +1102,7 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, CO = Instruction::PtrToInt; } } else if (!Src->getType()->isPointerTy() && Dst->isPointerTy()) { - if (auto* SrcVecTy = dyn_cast(Src->getType())) { + if (auto *SrcVecTy = dyn_cast(Src->getType())) { assert(SrcVecTy->getElementType()->isIntegerTy(32) && SrcVecTy->getNumElements() == 2 && "Expected vector of two 32-bit integer components"); From 2a725d2faa29bc3485be785bb56ec144de3fe2e1 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Sun, 29 Dec 2024 14:43:31 -0800 Subject: [PATCH 4/6] handle more vector types Even though we only expect to OpBitcast a pointer to a vector of two 32-bit integers, it's easy enough to just handle all vector types. --- lib/SPIRV/SPIRVReader.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 21cbf0fbd..b51771fbc 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1089,28 +1089,28 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, CO = Instruction::BitCast; if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { if (auto *DstVecTy = dyn_cast(Dst)) { - assert(DstVecTy->getElementType()->isIntegerTy(32) && - DstVecTy->getNumElements() == 2 && - "Expected vector of two 32-bit integer components"); - auto *Int64Ty = Type::getInt64Ty(BB->getContext()); + unsigned TotalBitWidth = + DstVecTy->getElementType()->getIntegerBitWidth() * + DstVecTy->getNumElements(); + auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); if (BB) { - Src = CastInst::CreatePointerCast(Src, Int64Ty, "", BB); + Src = CastInst::CreatePointerCast(Src, IntTy, "", BB); } else { - Src = ConstantExpr::getPointerCast(dyn_cast(Src), Int64Ty); + Src = ConstantExpr::getPointerCast(dyn_cast(Src), IntTy); } } else { CO = Instruction::PtrToInt; } } else if (!Src->getType()->isPointerTy() && Dst->isPointerTy()) { if (auto *SrcVecTy = dyn_cast(Src->getType())) { - assert(SrcVecTy->getElementType()->isIntegerTy(32) && - SrcVecTy->getNumElements() == 2 && - "Expected vector of two 32-bit integer components"); - auto *Int64Ty = Type::getInt64Ty(BB->getContext()); + unsigned TotalBitWidth = + SrcVecTy->getElementType()->getIntegerBitWidth() * + SrcVecTy->getNumElements(); + auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); if (BB) { - Src = CastInst::Create(Instruction::BitCast, Src, Int64Ty, "", BB); + Src = CastInst::Create(Instruction::BitCast, Src, IntTy, "", BB); } else { - Src = ConstantExpr::getBitCast(dyn_cast(Src), Int64Ty); + Src = ConstantExpr::getBitCast(dyn_cast(Src), IntTy); } } CO = Instruction::IntToPtr; From f88150fc7776e5ef2b3e2953428dadc00d0949c7 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Mon, 6 Jan 2025 11:26:16 -0800 Subject: [PATCH 5/6] remove unused type from OpBitcast_ptr_vector test --- test/OpBitcast_ptr_vector.spvasm | 1 - 1 file changed, 1 deletion(-) diff --git a/test/OpBitcast_ptr_vector.spvasm b/test/OpBitcast_ptr_vector.spvasm index c3c2aaf0a..69b206c84 100644 --- a/test/OpBitcast_ptr_vector.spvasm +++ b/test/OpBitcast_ptr_vector.spvasm @@ -13,7 +13,6 @@ OpMemoryModel Physical64 OpenCL OpEntryPoint Kernel %kernel "test" %uint = OpTypeInt 32 0 - %ulong = OpTypeInt 64 0 %uint2 = OpTypeVector %uint 2 %void = OpTypeVoid %pptr_int = OpTypePointer Function %uint From 91fa4c2e8be6acc1ecd6b856ffa0098e50242c97 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Mon, 6 Jan 2025 11:29:47 -0800 Subject: [PATCH 6/6] describe the difference between the SPIR-V and LLVM bitcast --- lib/SPIRV/SPIRVReader.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index b51771fbc..82c10d1cd 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1086,6 +1086,10 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, CO = IsExt ? Instruction::FPExt : Instruction::FPTrunc; break; case OpBitcast: + // OpBitcast need to be handled as a special-case when the source is a + // pointer and the destination is not a pointer, and where the source is not + // a pointer and the destination is a pointer. This is supported by the + // SPIR-V bitcast, but not by the LLVM bitcast. CO = Instruction::BitCast; if (Src->getType()->isPointerTy() && !Dst->isPointerTy()) { if (auto *DstVecTy = dyn_cast(Dst)) {