Skip to content

Commit

Permalink
Translate icmp eq and icmp ne to PtrEqual and PtrNotEqual respectively
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaksimo authored and MrSidims committed May 29, 2024
1 parent 8a1b55c commit d18c16e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
9 changes: 7 additions & 2 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,9 +1333,14 @@ SPIRVInstruction *LLVMToSPIRVBase::transCmpInst(CmpInst *Cmp,
auto *Op0 = Cmp->getOperand(0);
SPIRVValue *TOp0 = transValue(Op0, BB);
SPIRVValue *TOp1 = transValue(Cmp->getOperand(1), BB);
// TODO: once the translator supports SPIR-V 1.4, update the condition below:
// if (/* */->isPointerTy() && /* it is not allowed to use SPIR-V 1.4 */)
if (Op0->getType()->isPointerTy()) {
auto P = Cmp->getPredicate();
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4) &&
(P == ICmpInst::ICMP_EQ || P == ICmpInst::ICMP_NE) &&
Cmp->getOperand(1)->getType()->isPointerTy()) {
Op OC = P == ICmpInst::ICMP_EQ ? OpPtrEqual : OpPtrNotEqual;
return BM->addBinaryInst(OC, transType(Cmp->getType()), TOp0, TOp1, BB);
}
unsigned AS = cast<PointerType>(Op0->getType())->getAddressSpace();
SPIRVType *Ty = transType(getSizetType(AS));
TOp0 = BM->addUnaryInst(OpConvertPtrToU, Ty, TOp0, BB);
Expand Down
16 changes: 15 additions & 1 deletion test/ComparePointers.cl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ kernel void test(int global *in, int global *in2) {
return;
}
// RUN: %clang_cc1 -triple spir64 -x cl -cl-std=CL2.0 -O0 -emit-llvm-bc %s -o %t.bc -no-opaque-pointers
// RUN: llvm-spirv %t.bc -spirv-text -o %t.spt
// RUN: llvm-spirv %t.bc --spirv-max-version=1.3 -o %t.spv
// RUN: spirv-val %t.spv
// RUN: llvm-spirv %t.bc -spirv-text --spirv-max-version=1.3 -o %t.spt
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV

// RUN: llvm-spirv %t.bc -o %t.spv
// RUN: spirv-val %t.spv
// RUN: llvm-spirv %t.bc -spirv-text -o %t.spt
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV-14

// CHECK-SPIRV:ConvertPtrToU
// CHECK-SPIRV:ConvertPtrToU
Expand All @@ -26,3 +31,12 @@ kernel void test(int global *in, int global *in2) {
// CHECK-SPIRV:ConvertPtrToU
// CHECK-SPIRV:ConvertPtrToU
// CHECK-SPIRV:ULessThan

// CHECK-SPIRV-14: PtrNotEqual
// CHECK-SPIRV-14: PtrEqual
// CHECK-SPIRV-14:ConvertPtrToU
// CHECK-SPIRV-14:ConvertPtrToU
// CHECK-SPIRV-14:UGreaterThan
// CHECK-SPIRV-14:ConvertPtrToU
// CHECK-SPIRV-14:ConvertPtrToU
// CHECK-SPIRV-14:ULessThan
18 changes: 12 additions & 6 deletions test/complex-constexpr.ll
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -o %t.spv
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
; RUN: llvm-spirv -r %t.spv -o %t.bc
; RUN: llvm-dis %t.bc -o %t.ll
; RUN: FileCheck %s --input-file %t.spt -check-prefix=CHECK-SPIRV
; RUN: FileCheck %s --input-file %t.ll -check-prefix=CHECK-LLVM
; RUN: llvm-spirv %t.bc --spirv-max-version=1.3 -o %t.spv
; RUN: spirv-val %t.spv
; RUN: llvm-spirv %t.spv --spirv-max-version=1.3 -o %t.spt --to-text
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck %s --input-file %t.spt -check-prefix=CHECK-SPIRV
; RUN: FileCheck %s --input-file %t.rev.ll -check-prefix=CHECK-LLVM

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
target triple = "spir64"
Expand All @@ -16,6 +16,12 @@ target triple = "spir64"
; CHECK-SPIRV: TypePointer [[Ptr_Ty:[0-9]+]] 8 [[Int_Ty]]
; CHECK-SPIRV: TypeFunction [[Func_Ty2:[0-9]+]] [[Void_Ty]] [[Ptr_Ty]] [[Ptr_Ty]]

; CHECK-SPIRV-14: TypeInt [[Int_Ty:[0-9]+]] 8 0
; CHECK-SPIRV-14: TypeVoid [[Void_Ty:[0-9]+]]
; CHECK-SPIRV-14: TypeFunction [[Func_Ty1:[0-9]+]] [[Void_Ty]]
; CHECK-SPIRV-14: TypePointer [[Ptr_Ty:[0-9]+]] 8
; CHECK-SPIRV-14: TypeFunction [[Func_Ty2:[0-9]+]] [[Void_Ty]] [[Ptr_Ty]] [[Ptr_Ty]]

@.str.1 = private unnamed_addr addrspace(1) constant [1 x i8] zeroinitializer, align 1

define linkonce_odr hidden spir_func void @foo() {
Expand Down
3 changes: 1 addition & 2 deletions test/type-scavenger/ptr-abuse.ll
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ define spir_kernel void @foo() {
; CHECK: 4 Bitcast [[FLOATPTR]] [[STOREB:[0-9]+]] [[IPTR]]
; CHECK: Store [[STOREB]] {{[0-9]+}}
; CHECK: 4 Bitcast [[INTPTR]] [[CMPB:[0-9]+]] [[FPTR]]
; CHECK: 4 ConvertPtrToU [[SIZET:[0-9]+]] [[CMPL:[0-9]+]] [[IPTR]]
; CHECK: 4 ConvertPtrToU [[SIZET]] [[CMPR:[0-9]+]] [[CMPB]]
; CHECK: 5 PtrEqual [[#]] [[#]] [[IPTR]] [[CMPB]]
; CHECK: 5 IEqual {{[0-9]+}} {{[0-9]+}} [[CMPL]] [[CMPR]]
entry:
%iptr = alloca i32, align 4
Expand Down

0 comments on commit d18c16e

Please sign in to comment.