Skip to content

Commit

Permalink
Add support for llvm.scmp/ucmp.* (#2741)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Maronas <marcos.maronas@intel.com>
  • Loading branch information
maarquitos14 authored Oct 7, 2024
1 parent 7d7f946 commit e16f698
Show file tree
Hide file tree
Showing 3 changed files with 717 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3820,6 +3820,8 @@ bool LLVMToSPIRVBase::isKnownIntrinsic(Intrinsic::ID Id) {
case Intrinsic::minnum:
case Intrinsic::smin:
case Intrinsic::umin:
case Intrinsic::scmp:
case Intrinsic::ucmp:
case Intrinsic::nearbyint:
case Intrinsic::pow:
case Intrinsic::powi:
Expand Down Expand Up @@ -4197,6 +4199,51 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
BM->addCmpInst(OC, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
return BM->addSelectInst(Cmp, FirstArgVal, SecondArgVal, BB);
}
case Intrinsic::ucmp:
case Intrinsic::scmp: {
Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
SPIRVValue *FirstArgVal = transValue(II->getArgOperand(0), BB);
SPIRVValue *SecondArgVal = transValue(II->getArgOperand(1), BB);
SPIRVType *ResTy = transType(II->getType());
SPIRVValue *One = nullptr;
SPIRVValue *Zero = nullptr;
SPIRVValue *MinusOne = nullptr;
if (auto *VecTy = dyn_cast<VectorType>(II->getType())) {
auto *ElemTy = transType(VecTy->getElementType());
APInt MinusOneValue(ElemTy->getIntegerBitWidth(), 0, 1);
MinusOneValue.setAllBits();
unsigned VecSize = VecTy->getElementCount().getFixedValue();
auto ElemCount =
static_cast<std::vector<SPIRVValue *>::size_type>(VecSize);
auto *ElemOne = BM->addConstant(ElemTy, 1);
auto *ElemZero = BM->addConstant(ElemTy, 0);
auto *ElemMinusOne = BM->addConstant(ElemTy, MinusOneValue);
std::vector<SPIRVValue *> ElemsOne(ElemCount, ElemOne);
std::vector<SPIRVValue *> ElemsZero(ElemCount, ElemZero);
std::vector<SPIRVValue *> ElemsMinusOne(ElemCount, ElemMinusOne);
One = BM->addCompositeConstant(ResTy, ElemsOne);
Zero = BM->addCompositeConstant(ResTy, ElemsZero);
MinusOne = BM->addCompositeConstant(ResTy, ElemsMinusOne);
} else {
One = BM->addConstant(ResTy, 1);
Zero = BM->addConstant(ResTy, 0);
APInt MinusOneValue(ResTy->getIntegerBitWidth(), 0, 1);
MinusOneValue.setAllBits();
MinusOne = BM->addConstant(ResTy, MinusOneValue);
}

Op OC1 = (IID == Intrinsic::scmp) ? OpSLessThanEqual : OpULessThanEqual;
Op OC2 = (IID == Intrinsic::scmp) ? OpSLessThan : OpULessThan;
if (auto *VecTy = dyn_cast<VectorType>(II->getArgOperand(0)->getType()))
BoolTy = VectorType::get(BoolTy, VecTy->getElementCount());
SPIRVValue *Cmp1 =
BM->addCmpInst(OC1, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
SPIRVValue *Cmp2 =
BM->addCmpInst(OC2, transType(BoolTy), FirstArgVal, SecondArgVal, BB);
auto *ResCmp2 = BM->addSelectInst(Cmp2, MinusOne, Zero, BB);
auto *ResCmp1 = BM->addSelectInst(Cmp1, ResCmp2, One, BB);
return ResCmp1;
}
case Intrinsic::fma: {
if (!checkTypeForSPIRVExtendedInstLowering(II, BM))
break;
Expand Down
Loading

0 comments on commit e16f698

Please sign in to comment.