[llvm] AMDGPU: Match fract pattern with swapped edge case check (PR #189081)

Steffen Larsen via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 02:02:10 PDT 2026


================
@@ -1608,18 +1608,44 @@ bool AMDGPUCodeGenPrepareImpl::visitSelectInst(SelectInst &I) {
   IRBuilder<> Builder(&I);
   Builder.setFastMathFlags(FPOp->getFastMathFlags());
 
-  auto *IITrue = dyn_cast<IntrinsicInst>(TrueVal);
-  auto *IIFalse = dyn_cast<IntrinsicInst>(FalseVal);
-
   Value *Fract = nullptr;
-  if (Pred == FCmpInst::FCMP_UNO && TrueVal == CmpVal && IIFalse &&
-      CmpVal == matchFractPat(*IIFalse)) {
+  if (IsNanPred == FCmpInst::FCMP_UNO && TrueVal == CmpVal &&
+      CmpVal == matchFractPat(*FalseVal)) {
     // isnan(x) ? x : fract(x)
     Fract = applyFractPat(Builder, CmpVal);
-  } else if (Pred == FCmpInst::FCMP_ORD && FalseVal == CmpVal && IITrue &&
-             CmpVal == matchFractPat(*IITrue)) {
-    // !isnan(x) ? fract(x) : x
-    Fract = applyFractPat(Builder, CmpVal);
+  } else if (IsNanPred == FCmpInst::FCMP_ORD && FalseVal == CmpVal) {
+    if (CmpVal == matchFractPat(*TrueVal)) {
+      // !isnan(x) ? fract(x) : x
+      Fract = applyFractPat(Builder, CmpVal);
+    } else {
+      // Match an intermediate clamp infinity to 0 pattern. i.e.
+      // !isnan(x) ? (!isinf(x) ? fract(x) : 0.0) : x
----------------
steffenlarsen wrote:

Is there a particular reason this is the pattern we look for? Are we somehow guaranteed that this is the structure it will have and not one of

```
!isnan(x) ? (isinf(x) ? 0.0 : fract(x)) : x
isnan(x) ? x : (!isinf(x) ? fract(x) : 0.0)
isnan(x) ? x : (isinf(x) ? 0.0 : fract(x))
```

https://github.com/llvm/llvm-project/pull/189081


More information about the llvm-commits mailing list