[Mlir-commits] [mlir] [mlir][arith] Handle cmpf true/false in CmpFIntToFPConst (PR #232995)
Chang Yong Lik
llvmlistbot at llvm.org
Fri Oct 16 18:13:20 PDT 2026
https://github.com/ahlag created https://github.com/llvm/llvm-project/pull/232995
This PR fixes #231832, where `CmpFIntToFPConst` crashes by reaching `llvm_unreachable` when handling `arith.cmpf` with `true` or `false` predicates, since neither has a corresponding integer comparison. The fix folds both predicates directly to their respective constant results, consistent with the existing handling of `ord` and `uno`. A regression test, `@test8`, has been added to `canonicalize.mlir` to verify the behavior and prevent future regressions.
>From 23b8083969ea23111c045b53946431769e36c003 Mon Sep 17 00:00:00 2001
From: ahlag <theeahlag at gmail.com>
Date: Sat, 17 Oct 2026 10:05:46 +0900
Subject: [PATCH] [mlir][arith] Handle cmpf true/false in CmpFIntToFPConst
CmpFIntToFPConst reached llvm_unreachable("Unexpected predicate!") for
arith.cmpf true and arith.cmpf false, since convertToIntegerPredicate has
no integer equivalent for them. Fold them to constants, like ord and uno.
Fixes #231832
Assisted-by: Claude
---
mlir/lib/Dialect/Arith/IR/ArithOps.cpp | 2 ++
mlir/test/Dialect/Arith/canonicalize.mlir | 12 ++++++++++++
2 files changed, 14 insertions(+)
diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 40fcfa5c45881..fb3b64bf58b00 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -2827,11 +2827,13 @@ class CmpFIntToFPConst final : public OpRewritePattern<CmpFOp> {
// Convert to equivalent cmpi predicate
CmpIPredicate pred;
switch (op.getPredicate()) {
+ case CmpFPredicate::AlwaysTrue:
case CmpFPredicate::ORD:
// Int to fp conversion doesn't create a nan (ord checks neither is a nan)
rewriter.replaceOpWithNewOp<ConstantIntOp>(op, /*value=*/true,
/*width=*/1);
return success();
+ case CmpFPredicate::AlwaysFalse:
case CmpFPredicate::UNO:
// Int to fp conversion doesn't create a nan (uno checks either is a nan)
rewriter.replaceOpWithNewOp<ConstantIntOp>(op, /*value=*/false,
diff --git a/mlir/test/Dialect/Arith/canonicalize.mlir b/mlir/test/Dialect/Arith/canonicalize.mlir
index 0da65a4a0f402..4bba7ac073eae 100644
--- a/mlir/test/Dialect/Arith/canonicalize.mlir
+++ b/mlir/test/Dialect/Arith/canonicalize.mlir
@@ -4019,6 +4019,18 @@ func.func @test7(%arg0: i32) -> i1 {
// CHECK: arith.cmpi ugt, %[[arg0]], %[[c3]] : i32
}
+// CHECK-LABEL: @test8(
+// CHECK: %[[true:.+]] = arith.constant true
+// CHECK: %[[false:.+]] = arith.constant false
+// CHECK: return %[[true]], %[[false]]
+func.func @test8(%arg0: i1) -> (i1, i1) {
+ %cst = arith.constant 0.000000e+00 : bf16
+ %1 = arith.sitofp %arg0 : i1 to bf16
+ %2 = arith.cmpf true, %1, %cst : bf16
+ %3 = arith.cmpf false, %1, %cst : bf16
+ return %2, %3 : i1, i1
+}
+
// -----
// CHECK-LABEL: @foldShl(
More information about the Mlir-commits
mailing list