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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 11:52:36 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
----------------
arsenm wrote:

Instcombine does swap these around, and this is the canonical output. I did try all the combinations and they folded to this. This particular ordering of which check comes first came from the libclc and device libs implementations, which did the opposite 

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


More information about the llvm-commits mailing list