Skip to content

Commit 992e4be

Browse files
vmaksimosys-ce-bb
authored andcommitted
Translate icmp eq and icmp ne to PtrEqual and PtrNotEqual respectively (#2511)
This fixes #1391 Original commit: KhronosGroup/SPIRV-LLVM-Translator@5e0f14b5a09d961
1 parent d0ca8a7 commit 992e4be

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,9 +1503,14 @@ SPIRVInstruction *LLVMToSPIRVBase::transCmpInst(CmpInst *Cmp,
15031503
auto *Op0 = Cmp->getOperand(0);
15041504
SPIRVValue *TOp0 = transValue(Op0, BB);
15051505
SPIRVValue *TOp1 = transValue(Cmp->getOperand(1), BB);
1506-
// TODO: once the translator supports SPIR-V 1.4, update the condition below:
1507-
// if (/* */->isPointerTy() && /* it is not allowed to use SPIR-V 1.4 */)
15081506
if (Op0->getType()->isPointerTy()) {
1507+
auto P = Cmp->getPredicate();
1508+
if (BM->isAllowedToUseVersion(VersionNumber::SPIRV_1_4) &&
1509+
(P == ICmpInst::ICMP_EQ || P == ICmpInst::ICMP_NE) &&
1510+
Cmp->getOperand(1)->getType()->isPointerTy()) {
1511+
Op OC = P == ICmpInst::ICMP_EQ ? OpPtrEqual : OpPtrNotEqual;
1512+
return BM->addBinaryInst(OC, transType(Cmp->getType()), TOp0, TOp1, BB);
1513+
}
15091514
unsigned AS = cast<PointerType>(Op0->getType())->getAddressSpace();
15101515
SPIRVType *Ty = transType(getSizetType(AS));
15111516
TOp0 = BM->addUnaryInst(OpConvertPtrToU, Ty, TOp0, BB);

llvm-spirv/test/ComparePointers.cl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ kernel void test(int global *in, int global *in2) {
99
return;
1010
}
1111
// RUN: %clang_cc1 -triple spir64 -x cl -cl-std=CL2.0 -O0 -emit-llvm-bc %s -o %t.bc
12-
// RUN: llvm-spirv %t.bc -spirv-text -o %t.spt
12+
// RUN: llvm-spirv %t.bc --spirv-max-version=1.3 -o %t.spv
13+
// RUN: spirv-val %t.spv
14+
// RUN: llvm-spirv %t.bc -spirv-text --spirv-max-version=1.3 -o %t.spt
1315
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
16+
1417
// RUN: llvm-spirv %t.bc -o %t.spv
1518
// RUN: spirv-val %t.spv
19+
// RUN: llvm-spirv %t.bc -spirv-text -o %t.spt
20+
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV-14
1621

1722
// CHECK-SPIRV:ConvertPtrToU
1823
// CHECK-SPIRV:ConvertPtrToU
@@ -26,3 +31,12 @@ kernel void test(int global *in, int global *in2) {
2631
// CHECK-SPIRV:ConvertPtrToU
2732
// CHECK-SPIRV:ConvertPtrToU
2833
// CHECK-SPIRV:ULessThan
34+
35+
// CHECK-SPIRV-14: PtrNotEqual
36+
// CHECK-SPIRV-14: PtrEqual
37+
// CHECK-SPIRV-14:ConvertPtrToU
38+
// CHECK-SPIRV-14:ConvertPtrToU
39+
// CHECK-SPIRV-14:UGreaterThan
40+
// CHECK-SPIRV-14:ConvertPtrToU
41+
// CHECK-SPIRV-14:ConvertPtrToU
42+
// CHECK-SPIRV-14:ULessThan

llvm-spirv/test/complex-constexpr.ll

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
; RUN: llvm-as %s -o %t.bc
2-
; RUN: llvm-spirv %t.bc -o %t.spv
3-
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
4-
; RUN: llvm-spirv -r %t.spv -o %t.bc
5-
; RUN: llvm-dis %t.bc -o %t.ll
2+
; RUN: llvm-spirv %t.bc --spirv-max-version=1.3 -o %t.spv
3+
; RUN: spirv-val %t.spv
4+
; RUN: llvm-spirv %t.spv --spirv-max-version=1.3 -o %t.spt --to-text
5+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
6+
; RUN: llvm-dis %t.rev.bc
67
; RUN: FileCheck %s --input-file %t.spt -check-prefix=CHECK-SPIRV
7-
; RUN: FileCheck %s --input-file %t.ll -check-prefix=CHECK-LLVM
8+
; RUN: FileCheck %s --input-file %t.rev.ll -check-prefix=CHECK-LLVM
9+
10+
; RUN: llvm-spirv %t.bc -o %t.spv
811
; RUN: spirv-val %t.spv
12+
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
13+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
14+
; RUN: llvm-dis %t.rev.bc
15+
; RUN: FileCheck %s --input-file %t.spt -check-prefix=CHECK-SPIRV-14
16+
; RUN: FileCheck %s --input-file %t.rev.ll -check-prefix=CHECK-LLVM-14
17+
918

1019
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"
1120
target triple = "spir64"
@@ -16,6 +25,12 @@ target triple = "spir64"
1625
; CHECK-SPIRV: TypePointer [[Ptr_Ty:[0-9]+]] 8
1726
; CHECK-SPIRV: TypeFunction [[Func_Ty2:[0-9]+]] [[Void_Ty]] [[Ptr_Ty]] [[Ptr_Ty]]
1827

28+
; CHECK-SPIRV-14: TypeInt [[Int_Ty:[0-9]+]] 8 0
29+
; CHECK-SPIRV-14: TypeVoid [[Void_Ty:[0-9]+]]
30+
; CHECK-SPIRV-14: TypeFunction [[Func_Ty1:[0-9]+]] [[Void_Ty]]
31+
; CHECK-SPIRV-14: TypePointer [[Ptr_Ty:[0-9]+]] 8
32+
; CHECK-SPIRV-14: TypeFunction [[Func_Ty2:[0-9]+]] [[Void_Ty]] [[Ptr_Ty]] [[Ptr_Ty]]
33+
1934
@.str.1 = private unnamed_addr addrspace(1) constant [1 x i8] zeroinitializer, align 1
2035

2136
define linkonce_odr hidden spir_func void @foo() {
@@ -27,13 +42,28 @@ entry:
2742
; CHECK-SPIRV: ConvertUToPtr {{[0-9]+}} [[UToPtr:[0-9]+]]
2843
; CHECK-SPIRV: Select {{[0-9]+}} [[Sel:[0-9]+]] [[IEq]] [[UToPtr]] [[Cast]]
2944
; CHECK-SPIRV: FunctionCall [[Void_Ty]] {{[0-9]+}} [[Func:[0-9]+]] [[Cast]] [[Sel]]
45+
46+
; CHECK-SPIRV-14: PtrCastToGeneric {{[0-9]+}} [[Cast:[0-9]+]]
47+
; CHECK-SPIRV-14: PtrEqual {{[0-9]+}} [[PtrEq:[0-9]+]] [[Cast]] [[#]]
48+
; CHECK-SPIRV-14: ConvertUToPtr {{[0-9]+}} [[UToPtr:[0-9]+]]
49+
; CHECK-SPIRV-14: Select {{[0-9]+}} [[Sel:[0-9]+]] [[PtrEq]] [[UToPtr]] [[Cast]]
50+
; CHECK-SPIRV-14: FunctionCall [[Void_Ty]] {{[0-9]+}} [[Func:[0-9]+]] [[Cast]] [[Sel]]
51+
3052
; CHECK-LLVM: %[[Cast:[0-9]+]] = addrspacecast ptr addrspace(1) @.str.1 to ptr addrspace(4)
3153
; CHECK-LLVM: %[[PtrToU1:[0-9]+]] = ptrtoint ptr addrspace(4) %[[Cast]] to i64
3254
; CHECK-LLVM: %[[PtrToU2:[0-9]+]] = ptrtoint ptr addrspace(4) null to i64
3355
; CHECK-LLVM: %[[IEq:[0-9]+]] = icmp eq i64 %[[PtrToU1]], %[[PtrToU2]]
3456
; CHECK-LLVM: %[[UToPtr:[0-9]+]] = inttoptr i64 -1 to ptr addrspace(4)
3557
; CHECK-LLVM: %[[Sel:[0-9]+]] = select i1 %[[IEq]], ptr addrspace(4) %[[UToPtr]], ptr addrspace(4) %[[Cast]]
3658
; CHECK-LLVM: call spir_func void @bar(ptr addrspace(4) %[[Cast]], ptr addrspace(4) %[[Sel]]) #0
59+
60+
; CHECK-LLVM-14: %[[Cast:[0-9]+]] = addrspacecast ptr addrspace(1) @.str.1 to ptr addrspace(4)
61+
; CHECK-LLVM-14: %[[PtrToU1:[0-9]+]] = ptrtoint ptr addrspace(4) %[[Cast]] to i64
62+
; CHECK-LLVM-14: %[[IEq:[0-9]+]] = icmp eq i64 %[[PtrToU1]], 0
63+
; CHECK-LLVM-14: %[[UToPtr:[0-9]+]] = inttoptr i64 -1 to ptr addrspace(4)
64+
; CHECK-LLVM-14: %[[Sel:[0-9]+]] = select i1 %[[IEq]], ptr addrspace(4) %[[UToPtr]], ptr addrspace(4) %[[Cast]]
65+
; CHECK-LLVM-14: call spir_func void @bar(ptr addrspace(4) %[[Cast]], ptr addrspace(4) %[[Sel]]) #0
66+
3767
%0 = select i1 icmp eq (ptr addrspace(4) addrspacecast (ptr addrspace(1) @.str.1 to ptr addrspace(4)), ptr addrspace(4) null), ptr addrspace(4) inttoptr (i64 -1 to ptr addrspace(4)), ptr addrspace(4) addrspacecast (ptr addrspace(1) @.str.1 to ptr addrspace(4))
3868
call spir_func void @bar(ptr addrspace(4) addrspacecast (ptr addrspace(1) @.str.1 to ptr addrspace(4)), ptr addrspace(4) %0)
3969
ret void

llvm-spirv/test/type-scavenger/ptr-abuse.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ define spir_kernel void @foo() {
2222
; CHECK: 4 Bitcast [[FLOATPTR]] [[STOREB:[0-9]+]] [[IPTR]]
2323
; CHECK: Store [[STOREB]] {{[0-9]+}}
2424
; CHECK: 4 Bitcast [[INTPTR]] [[CMPB:[0-9]+]] [[FPTR]]
25-
; CHECK: 4 ConvertPtrToU [[SIZET:[0-9]+]] [[CMPL:[0-9]+]] [[IPTR]]
26-
; CHECK: 4 ConvertPtrToU [[SIZET]] [[CMPR:[0-9]+]] [[CMPB]]
27-
; CHECK: 5 IEqual {{[0-9]+}} {{[0-9]+}} [[CMPL]] [[CMPR]]
25+
; CHECK: 5 PtrEqual [[#]] [[#]] [[IPTR]] [[CMPB]]
2826
entry:
2927
%iptr = alloca i32, align 4
3028
%fptr = alloca float, align 4

0 commit comments

Comments
 (0)