Skip to content

Commit 64f3301

Browse files
maarquitos14jsji
authored andcommitted
Add support for llvm.scmp/ucmp.* (#2741)
Signed-off-by: Marcos Maronas <[email protected]> Original commit: KhronosGroup/SPIRV-LLVM-Translator@e16f698506710fd
1 parent c1dc946 commit 64f3301

File tree

3 files changed

+717
-0
lines changed

3 files changed

+717
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,6 +3828,8 @@ bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
38283828
case Intrinsic::minnum:
38293829
case Intrinsic::smin:
38303830
case Intrinsic::umin:
3831+
case Intrinsic::scmp:
3832+
case Intrinsic::ucmp:
38313833
case Intrinsic::nearbyint:
38323834
case Intrinsic::pow:
38333835
case Intrinsic::powi:
@@ -4205,6 +4207,51 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
42054207
BM->addCmpInst(OC, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
42064208
return BM->addSelectInst(Cmp, FirstArgVal, SecondArgVal, BB);
42074209
}
4210+
case Intrinsic::ucmp:
4211+
case Intrinsic::scmp: {
4212+
Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
4213+
SPIRVValue *FirstArgVal = transValue(II->getArgOperand(0), BB);
4214+
SPIRVValue *SecondArgVal = transValue(II->getArgOperand(1), BB);
4215+
SPIRVType *ResTy = transType(II->getType());
4216+
SPIRVValue *One = nullptr;
4217+
SPIRVValue *Zero = nullptr;
4218+
SPIRVValue *MinusOne = nullptr;
4219+
if (auto *VecTy = dyn_cast<VectorType>(II->getType())) {
4220+
auto *ElemTy = transType(VecTy->getElementType());
4221+
APInt MinusOneValue(ElemTy->getIntegerBitWidth(), 0, 1);
4222+
MinusOneValue.setAllBits();
4223+
unsigned VecSize = VecTy->getElementCount().getFixedValue();
4224+
auto ElemCount =
4225+
static_cast<std::vector<SPIRVValue *>::size_type>(VecSize);
4226+
auto *ElemOne = BM->addConstant(ElemTy, 1);
4227+
auto *ElemZero = BM->addConstant(ElemTy, 0);
4228+
auto *ElemMinusOne = BM->addConstant(ElemTy, MinusOneValue);
4229+
std::vector<SPIRVValue *> ElemsOne(ElemCount, ElemOne);
4230+
std::vector<SPIRVValue *> ElemsZero(ElemCount, ElemZero);
4231+
std::vector<SPIRVValue *> ElemsMinusOne(ElemCount, ElemMinusOne);
4232+
One = BM->addCompositeConstant(ResTy, ElemsOne);
4233+
Zero = BM->addCompositeConstant(ResTy, ElemsZero);
4234+
MinusOne = BM->addCompositeConstant(ResTy, ElemsMinusOne);
4235+
} else {
4236+
One = BM->addConstant(ResTy, 1);
4237+
Zero = BM->addConstant(ResTy, 0);
4238+
APInt MinusOneValue(ResTy->getIntegerBitWidth(), 0, 1);
4239+
MinusOneValue.setAllBits();
4240+
MinusOne = BM->addConstant(ResTy, MinusOneValue);
4241+
}
4242+
4243+
Op OC1 = (IID == Intrinsic::scmp) ? OpSLessThanEqual : OpULessThanEqual;
4244+
Op OC2 = (IID == Intrinsic::scmp) ? OpSLessThan : OpULessThan;
4245+
if (auto *VecTy = dyn_cast<VectorType>(II->getArgOperand(0)->getType()))
4246+
BoolTy = VectorType::get(BoolTy, VecTy->getElementCount());
4247+
SPIRVValue *Cmp1 =
4248+
BM->addCmpInst(OC1, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
4249+
SPIRVValue *Cmp2 =
4250+
BM->addCmpInst(OC2, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
4251+
auto *ResCmp2 = BM->addSelectInst(Cmp2, MinusOne, Zero, BB);
4252+
auto *ResCmp1 = BM->addSelectInst(Cmp1, ResCmp2, One, BB);
4253+
return ResCmp1;
4254+
}
42084255
case Intrinsic::fma: {
42094256
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
42104257
break;

0 commit comments

Comments
 (0)