From cd343588b3aedb78514e8193b65886c9dc51eee1 Mon Sep 17 00:00:00 2001 From: Ben Ashbaugh Date: Tue, 7 Jan 2025 04:37:45 -0800 Subject: [PATCH] [Backport to 15] handle OpBitcast between pointers and non-pointers (#2948) Adds support for SPIR-V OpBitcast instructions where 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 needs to be handled as a special case because the LLVM bitcast instruction does not support this. Handles bitcasts between pointers and scalar integers, which is supported by all SPIR-V versions, and pointers and vectors of integers, which is supported by SPIR-V 1.5 (though only for vectors of 32-bit integers). (cherry picked from commit aafca810e5cd645282ae5e60a9cadd39e3345ad0) --- lib/SPIRV/SPIRVReader.cpp | 35 ++++++++++++++++++++++++++++++++ test/OpBitcast_ptr_scalar.spvasm | 31 ++++++++++++++++++++++++++++ test/OpBitcast_ptr_vector.spvasm | 33 ++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 test/OpBitcast_ptr_scalar.spvasm create mode 100644 test/OpBitcast_ptr_vector.spvasm diff --git a/lib/SPIRV/SPIRVReader.cpp b/lib/SPIRV/SPIRVReader.cpp index 386ca93b9..58cad9b49 100644 --- a/lib/SPIRV/SPIRVReader.cpp +++ b/lib/SPIRV/SPIRVReader.cpp @@ -1174,6 +1174,41 @@ Value *SPIRVToLLVM::transConvertInst(SPIRVValue *BV, Function *F, case OpFConvert: 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)) { + unsigned TotalBitWidth = + DstVecTy->getElementType()->getIntegerBitWidth() * + DstVecTy->getNumElements(); + auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); + if (BB) { + Src = CastInst::CreatePointerCast(Src, IntTy, "", BB); + } else { + 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())) { + unsigned TotalBitWidth = + SrcVecTy->getElementType()->getIntegerBitWidth() * + SrcVecTy->getNumElements(); + auto *IntTy = Type::getIntNTy(BB->getContext(), TotalBitWidth); + if (BB) { + Src = CastInst::Create(Instruction::BitCast, Src, IntTy, "", BB); + } else { + Src = ConstantExpr::getBitCast(dyn_cast(Src), IntTy); + } + } + CO = Instruction::IntToPtr; + } + 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..164c02836 --- /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 -emit-opaque-pointers -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 diff --git a/test/OpBitcast_ptr_vector.spvasm b/test/OpBitcast_ptr_vector.spvasm new file mode 100644 index 000000000..db4018b78 --- /dev/null +++ b/test/OpBitcast_ptr_vector.spvasm @@ -0,0 +1,33 @@ +; 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 -emit-opaque-pointers -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 + %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