Skip to content

Commit e16f698

Browse files
authored
Add support for llvm.scmp/ucmp.* (#2741)
Signed-off-by: Marcos Maronas <[email protected]>
1 parent 7d7f946 commit e16f698

File tree

3 files changed

+717
-0
lines changed

3 files changed

+717
-0
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,6 +3820,8 @@ bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
38203820
case Intrinsic::minnum:
38213821
case Intrinsic::smin:
38223822
case Intrinsic::umin:
3823+
case Intrinsic::scmp:
3824+
case Intrinsic::ucmp:
38233825
case Intrinsic::nearbyint:
38243826
case Intrinsic::pow:
38253827
case Intrinsic::powi:
@@ -4197,6 +4199,51 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
41974199
BM->addCmpInst(OC, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
41984200
return BM->addSelectInst(Cmp, FirstArgVal, SecondArgVal, BB);
41994201
}
4202+
case Intrinsic::ucmp:
4203+
case Intrinsic::scmp: {
4204+
Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
4205+
SPIRVValue *FirstArgVal = transValue(II->getArgOperand(0), BB);
4206+
SPIRVValue *SecondArgVal = transValue(II->getArgOperand(1), BB);
4207+
SPIRVType *ResTy = transType(II->getType());
4208+
SPIRVValue *One = nullptr;
4209+
SPIRVValue *Zero = nullptr;
4210+
SPIRVValue *MinusOne = nullptr;
4211+
if (auto *VecTy = dyn_cast<VectorType>(II->getType())) {
4212+
auto *ElemTy = transType(VecTy->getElementType());
4213+
APInt MinusOneValue(ElemTy->getIntegerBitWidth(), 0, 1);
4214+
MinusOneValue.setAllBits();
4215+
unsigned VecSize = VecTy->getElementCount().getFixedValue();
4216+
auto ElemCount =
4217+
static_cast<std::vector<SPIRVValue *>::size_type>(VecSize);
4218+
auto *ElemOne = BM->addConstant(ElemTy, 1);
4219+
auto *ElemZero = BM->addConstant(ElemTy, 0);
4220+
auto *ElemMinusOne = BM->addConstant(ElemTy, MinusOneValue);
4221+
std::vector<SPIRVValue *> ElemsOne(ElemCount, ElemOne);
4222+
std::vector<SPIRVValue *> ElemsZero(ElemCount, ElemZero);
4223+
std::vector<SPIRVValue *> ElemsMinusOne(ElemCount, ElemMinusOne);
4224+
One = BM->addCompositeConstant(ResTy, ElemsOne);
4225+
Zero = BM->addCompositeConstant(ResTy, ElemsZero);
4226+
MinusOne = BM->addCompositeConstant(ResTy, ElemsMinusOne);
4227+
} else {
4228+
One = BM->addConstant(ResTy, 1);
4229+
Zero = BM->addConstant(ResTy, 0);
4230+
APInt MinusOneValue(ResTy->getIntegerBitWidth(), 0, 1);
4231+
MinusOneValue.setAllBits();
4232+
MinusOne = BM->addConstant(ResTy, MinusOneValue);
4233+
}
4234+
4235+
Op OC1 = (IID == Intrinsic::scmp) ? OpSLessThanEqual : OpULessThanEqual;
4236+
Op OC2 = (IID == Intrinsic::scmp) ? OpSLessThan : OpULessThan;
4237+
if (auto *VecTy = dyn_cast<VectorType>(II->getArgOperand(0)->getType()))
4238+
BoolTy = VectorType::get(BoolTy, VecTy->getElementCount());
4239+
SPIRVValue *Cmp1 =
4240+
BM->addCmpInst(OC1, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
4241+
SPIRVValue *Cmp2 =
4242+
BM->addCmpInst(OC2, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
4243+
auto *ResCmp2 = BM->addSelectInst(Cmp2, MinusOne, Zero, BB);
4244+
auto *ResCmp1 = BM->addSelectInst(Cmp1, ResCmp2, One, BB);
4245+
return ResCmp1;
4246+
}
42004247
case Intrinsic::fma: {
42014248
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
42024249
break;

0 commit comments

Comments
 (0)