[llvm] [DirectX] Implement `llvm.is.fpclass` lowering for the fcNegZero FPClassTest and the `IsNaN`, `IsInf`, `IsFinite`, `IsNormal` DXIL ops (PR #138048)
via llvm-commits
llvm-commits at lists.llvm.org
Wed May 7 15:08:26 PDT 2025
================
@@ -273,6 +274,59 @@ static Value *expandExpIntrinsic(CallInst *Orig) {
return Exp2Call;
}
+static Value *expandIsFPClass(CallInst *Orig) {
+ Value *T = Orig->getArgOperand(1);
+ auto *TCI = dyn_cast<ConstantInt>(T);
+
+ // These FPClassTest cases have DXIL opcodes, so they will be handled in
+ // DXIL Op Lowering instead.
+ switch (TCI->getZExtValue()) {
+ case FPClassTest::fcInf:
+ case FPClassTest::fcNan:
+ case FPClassTest::fcNormal:
+ case FPClassTest::fcFinite:
+ return nullptr;
+ }
+
+ IRBuilder<> Builder(Orig);
+
+ Value *F = Orig->getArgOperand(0);
+ Type *FTy = F->getType();
+ unsigned FNumElem = 0; // 0 => F is not a vector
+
+ unsigned BitWidth; // Bit width of F or the ElemTy of F
+ Type *BitCastTy; // An IntNTy of the same bitwidth as F or ElemTy of F
+
+ if (auto *FVecTy = dyn_cast<FixedVectorType>(FTy)) {
+ Type *ElemTy = FVecTy->getElementType();
+ FNumElem = FVecTy->getNumElements();
+ BitWidth = ElemTy->getPrimitiveSizeInBits();
+ BitCastTy = FixedVectorType::get(Builder.getIntNTy(BitWidth), FNumElem);
+ } else {
+ BitWidth = FTy->getPrimitiveSizeInBits();
+ BitCastTy = Builder.getIntNTy(BitWidth);
+ }
+
+ Value *FBitCast = Builder.CreateBitCast(F, BitCastTy);
+ switch (TCI->getZExtValue()) {
+ case FPClassTest::fcNegZero: {
+ Value *NegZero =
+ ConstantInt::get(Builder.getIntNTy(BitWidth), 1 << (BitWidth - 1));
+ Value *RetVal;
+ if (FNumElem) {
+ Value *NegZeroSplat = Builder.CreateVectorSplat(FNumElem, NegZero);
+ RetVal =
+ Builder.CreateICmpEQ(FBitCast, NegZeroSplat, "is.fpclass.negzero");
+ } else
+ RetVal = Builder.CreateICmpEQ(FBitCast, NegZero, "is.fpclass.negzero");
+ return RetVal;
+ }
+ default:
----------------
joaosaffran wrote:
If this switch is fully covered we don't need the default here, we can move it to after the switch like:
```cpp
switch (TCI->getZExtValue()) {
case FPClassTest::fcNegZero: {
...
return RetVal;
}
}
report_fatal_error(Twine("Unsupported FPClassTest"),
/* gen_crash_diag=*/false);
```
https://github.com/llvm/llvm-project/pull/138048
More information about the llvm-commits
mailing list