Skip to content

Commit 43c071f

Browse files
committed
Handle OpSpecConstantOp for integer comparisons
This revealed that OpINotEqual was missing from isSpecConstantOpAllowedOp. Test co-authored by Stuart Brady.
1 parent b6e2bcc commit 43c071f

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,24 +1185,27 @@ Value *SPIRVToLLVM::transShiftLogicalBitwiseInst(SPIRVValue *BV, BasicBlock *BB,
11851185
return NewOp;
11861186
}
11871187

1188-
Instruction *SPIRVToLLVM::transCmpInst(SPIRVValue *BV, BasicBlock *BB,
1189-
Function *F) {
1188+
Value *SPIRVToLLVM::transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F) {
11901189
SPIRVCompare *BC = static_cast<SPIRVCompare *>(BV);
1191-
assert(BB && "Invalid BB");
11921190
SPIRVType *BT = BC->getOperand(0)->getType();
1193-
Instruction *Inst = nullptr;
1191+
Value *Inst = nullptr;
11941192
auto OP = BC->getOpCode();
11951193
if (isLogicalOpCode(OP))
11961194
OP = IntBoolOpMap::rmap(OP);
1195+
1196+
Value *Op0 = transValue(BC->getOperand(0), F, BB);
1197+
Value *Op1 = transValue(BC->getOperand(1), F, BB);
1198+
1199+
IRBuilder<> Builder(*Context);
1200+
if (BB) {
1201+
Builder.SetInsertPoint(BB);
1202+
}
1203+
11971204
if (BT->isTypeVectorOrScalarInt() || BT->isTypeVectorOrScalarBool() ||
11981205
BT->isTypePointer())
1199-
Inst = new ICmpInst(*BB, CmpMap::rmap(OP),
1200-
transValue(BC->getOperand(0), F, BB),
1201-
transValue(BC->getOperand(1), F, BB));
1206+
Inst = Builder.CreateICmp(CmpMap::rmap(OP), Op0, Op1);
12021207
else if (BT->isTypeVectorOrScalarFloat())
1203-
Inst = new FCmpInst(*BB, CmpMap::rmap(OP),
1204-
transValue(BC->getOperand(0), F, BB),
1205-
transValue(BC->getOperand(1), F, BB));
1208+
Inst = Builder.CreateFCmp(CmpMap::rmap(OP), Op0, Op1);
12061209
assert(Inst && "not implemented");
12071210
return Inst;
12081211
}

lib/SPIRV/SPIRVReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class SPIRVToLLVM {
232232
Type *transFPType(SPIRVType *T);
233233
Value *transShiftLogicalBitwiseInst(SPIRVValue *BV, BasicBlock *BB,
234234
Function *F);
235-
Instruction *transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F);
235+
Value *transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F);
236236
void transOCLBuiltinFromInstPreproc(SPIRVInstruction *BI, Type *&RetTy,
237237
std::vector<SPIRVValue *> &Args);
238238
Instruction *transOCLBuiltinPostproc(SPIRVInstruction *BI, CallInst *CI,

lib/SPIRV/libSPIRV/SPIRVInstruction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ bool isSpecConstantOpAllowedOp(Op OC) {
207207
OpLogicalNotEqual,
208208
OpSelect,
209209
OpIEqual,
210+
OpINotEqual,
210211
OpULessThan,
211212
OpSLessThan,
212213
OpUGreaterThan,

test/SpecConstants/specconstantop-init.spvasm

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
; CHECK: @var_logor = addrspace(1) global i1 true
3232
; CHECK: @var_logand = addrspace(1) global i1 false
3333
; CHECK: @var_lognot = addrspace(1) global i1 false
34+
; CHECK: @var_logeq = addrspace(1) global i1 false
35+
; CHECK: @var_logne = addrspace(1) global i1 true
36+
; CHECK: @var_icmpeq = addrspace(1) global i1 false
37+
; CHECK: @var_icmpne = addrspace(1) global i1 true
38+
; CHECK: @var_icmpult = addrspace(1) global i1 true
39+
; CHECK: @var_icmpslt = addrspace(1) global i1 false
40+
; CHECK: @var_icmpugt = addrspace(1) global i1 false
41+
; CHECK: @var_icmpsgt = addrspace(1) global i1 true
42+
; CHECK: @var_icmpule = addrspace(1) global i1 true
43+
; CHECK: @var_icmpsle = addrspace(1) global i1 false
44+
; CHECK: @var_icmpuge = addrspace(1) global i1 false
45+
; CHECK: @var_icmpsge = addrspace(1) global i1 true
3446

3547
OpCapability Addresses
3648
OpCapability Linkage
@@ -59,6 +71,18 @@
5971
OpDecorate %var_logor LinkageAttributes "var_logor" Export
6072
OpDecorate %var_logand LinkageAttributes "var_logand" Export
6173
OpDecorate %var_lognot LinkageAttributes "var_lognot" Export
74+
OpDecorate %var_logeq LinkageAttributes "var_logeq" Export
75+
OpDecorate %var_logne LinkageAttributes "var_logne" Export
76+
OpDecorate %var_icmpeq LinkageAttributes "var_icmpeq" Export
77+
OpDecorate %var_icmpne LinkageAttributes "var_icmpne" Export
78+
OpDecorate %var_icmpult LinkageAttributes "var_icmpult" Export
79+
OpDecorate %var_icmpslt LinkageAttributes "var_icmpslt" Export
80+
OpDecorate %var_icmpugt LinkageAttributes "var_icmpugt" Export
81+
OpDecorate %var_icmpsgt LinkageAttributes "var_icmpsgt" Export
82+
OpDecorate %var_icmpule LinkageAttributes "var_icmpule" Export
83+
OpDecorate %var_icmpsle LinkageAttributes "var_icmpsle" Export
84+
OpDecorate %var_icmpuge LinkageAttributes "var_icmpuge" Export
85+
OpDecorate %var_icmpsge LinkageAttributes "var_icmpsge" Export
6286
%bool = OpTypeBool
6387
%true = OpConstantTrue %bool
6488
%false = OpConstantFalse %bool
@@ -88,6 +112,18 @@
88112
%logor = OpSpecConstantOp %bool LogicalOr %true %false
89113
%logand = OpSpecConstantOp %bool LogicalAnd %true %false
90114
%lognot = OpSpecConstantOp %bool LogicalNot %true
115+
%logeq = OpSpecConstantOp %bool LogicalEqual %true %false
116+
%logne = OpSpecConstantOp %bool LogicalNotEqual %true %false
117+
%icmpeq = OpSpecConstantOp %bool IEqual %uint_53 %uint_min4
118+
%icmpne = OpSpecConstantOp %bool INotEqual %uint_53 %uint_min4
119+
%icmpult = OpSpecConstantOp %bool ULessThan %uint_53 %uint_min4
120+
%icmpslt = OpSpecConstantOp %bool SLessThan %uint_53 %uint_min4
121+
%icmpugt = OpSpecConstantOp %bool UGreaterThan %uint_53 %uint_min4
122+
%icmpsgt = OpSpecConstantOp %bool SGreaterThan %uint_53 %uint_min4
123+
%icmpule = OpSpecConstantOp %bool ULessThanEqual %uint_53 %uint_min4
124+
%icmpsle = OpSpecConstantOp %bool SLessThanEqual %uint_53 %uint_min4
125+
%icmpuge = OpSpecConstantOp %bool UGreaterThanEqual %uint_53 %uint_min4
126+
%icmpsge = OpSpecConstantOp %bool SGreaterThanEqual %uint_53 %uint_min4
91127
%_ptr_uint = OpTypePointer CrossWorkgroup %uint
92128
%_ptr_bool = OpTypePointer CrossWorkgroup %bool
93129
%void = OpTypeVoid
@@ -114,6 +150,18 @@
114150
%var_logor = OpVariable %_ptr_bool CrossWorkgroup %logor
115151
%var_logand = OpVariable %_ptr_bool CrossWorkgroup %logand
116152
%var_lognot = OpVariable %_ptr_bool CrossWorkgroup %lognot
153+
%var_logeq = OpVariable %_ptr_bool CrossWorkgroup %logeq
154+
%var_logne = OpVariable %_ptr_bool CrossWorkgroup %logne
155+
%var_icmpeq = OpVariable %_ptr_bool CrossWorkgroup %icmpeq
156+
%var_icmpne = OpVariable %_ptr_bool CrossWorkgroup %icmpne
157+
%var_icmpult = OpVariable %_ptr_bool CrossWorkgroup %icmpult
158+
%var_icmpslt = OpVariable %_ptr_bool CrossWorkgroup %icmpslt
159+
%var_icmpugt = OpVariable %_ptr_bool CrossWorkgroup %icmpugt
160+
%var_icmpsgt = OpVariable %_ptr_bool CrossWorkgroup %icmpsgt
161+
%var_icmpule = OpVariable %_ptr_bool CrossWorkgroup %icmpule
162+
%var_icmpsle = OpVariable %_ptr_bool CrossWorkgroup %icmpsle
163+
%var_icmpuge = OpVariable %_ptr_bool CrossWorkgroup %icmpuge
164+
%var_icmpsge = OpVariable %_ptr_bool CrossWorkgroup %icmpsge
117165

118166
%15 = OpFunction %void Pure %14
119167
%entry = OpLabel

0 commit comments

Comments
 (0)