[Mlir-commits] [mlir] [mlir] NFC: Abstract NamedTypeSatisfiesPred and reuse TypeIsPred (PR #201814)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Jun 5 04:38:23 PDT 2026


https://github.com/ConvolutedDog created https://github.com/llvm/llvm-project/pull/201814

Abstract NamedTypeSatisfiesPred to encapsulate checking a named
operand/result's type against an arbitrary Pred.

>From bbc236692d4e08b59a39060015efa73ec023fc90 Mon Sep 17 00:00:00 2001
From: ConvolutedDog <yangjianchao16 at nudt.edu.cn>
Date: Fri, 5 Jun 2026 19:33:52 +0800
Subject: [PATCH] [mlir][NFC] Add NamedTypeSatisfiesPred and reuse TypeIsPred

Abstract NamedTypeSatisfiesPred to encapsulate checking a named
operand/result's type against an arbitrary Pred.
---
 .../include/mlir/Dialect/Arith/IR/ArithOps.td |  4 +--
 mlir/include/mlir/IR/OpBase.td                | 27 ++++++++++---------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
index 1f8b07aed3f0d..94c7699fd43da 100644
--- a/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
+++ b/mlir/include/mlir/Dialect/Arith/IR/ArithOps.td
@@ -2008,8 +2008,8 @@ class BooleanConditionOrMatchingShape<string condition, string result> :
     PredOpTrait<
       condition # " is signless i1 or has matching shape",
       Or<[TypeIsPred<condition, I1>,
-          And<[SubstLeaves<"$_self", "$" # condition # ".getType()", IsShapedTypePred>,
-               SubstLeaves<"$_self", "$" # result # ".getType()", IsShapedTypePred>,
+          And<[NamedTypeSatisfiesPred<condition, IsShapedTypePred>,
+               NamedTypeSatisfiesPred<result, IsShapedTypePred>,
                AllShapesMatch<[condition, result]>.predicate]>]>>;
 
 def SelectOp : Arith_Op<"select", [Pure,
diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 0d0669e90c3f7..032242be768ac 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -633,14 +633,25 @@ class RangedTypesMatchWith<string summary, string lhsArg, string rhsArg,
                            string transform>
   : TypesMatchWith<summary, lhsArg, rhsArg, transform, "llvm::equal">;
 
+// Predicate to verify that a named argument or result's type satisfies a given
+// predicate.
+class NamedTypeSatisfiesPred<string name, Pred pred> :
+    SubstLeaves<"$_self", "$" # name # ".getType()", pred>;
+
+// Predicate to verify that a named argument or result's type matches a given
+// type constraint.
+class TypeIsPred<string name, Type type> :
+    NamedTypeSatisfiesPred<name, type.predicate>;
+class TypeIs<string name, Type type> : PredOpTrait<
+  "'" # name # "' is " # type.summary, TypeIsPred<name, type>>;
+
 // Checks that each inputArg has the same type as the corresponding entry
 // in allowedTypes
 class InputMatchesTypes<list<string> inputArgs, list<Type> allowedTypes> :
     PredOpTrait<"operands {" # !interleave(inputArgs, ", ") # "} match expected types",
                 !foldl(TruePred, !range(!size(inputArgs)), acc, i,
                        And<[acc,
-                           SubstLeaves<"$_self", "$" # inputArgs[i] # ".getType()",
-                                      allowedTypes[i].predicate>
+                            TypeIsPred<inputArgs[i], allowedTypes[i]>
                        ]>)> {
     assert !eq(!size(inputArgs), !size(allowedTypes)),
            "inputArgs and allowedTypes lists must have the same length";
@@ -661,8 +672,7 @@ class InputAddressIsCombinationOf<list<string> inputArgs,
                 Or<!foreach(combination, allowedCombinations,
                            !foldl(TruePred, !range(!size(inputArgs)), acc, i,
                                   And<[acc,
-                                      SubstLeaves<"$_self", "$" # inputArgs[i] # ".getType()",
-                                                 combination[i].predicate>
+                                       TypeIsPred<inputArgs[i], combination[i]>
                                   ]>))>> {
     assert !gt(!size(allowedCombinations), 0),
            "allowedCombinations must not be empty";
@@ -686,17 +696,10 @@ class TCopVTEtIs<int idx, Type type> : And<[
    SubstLeaves<"$_self", "getElementTypeOrSelf($_op.getOperand(" # idx # "))",
      type.predicate>]>;
 
-// Predicate to verify that a named argument or result's element type matches a
-// given type.
-class TypeIsPred<string name, Type type> :
-   SubstLeaves<"$_self", "$" # name # ".getType()", type.predicate>;
-class TypeIs<string name, Type type> : PredOpTrait<
-  "'" # name # "' is " # type.summary, TypeIsPred<name, type>>;
-
 // Predicate to verify that a named argument or result's element type matches a
 // given type.
 class ElementTypeIsPred<string name, Type type> : And<[
-   SubstLeaves<"$_self", "$" # name # ".getType()", IsShapedTypePred>,
+   NamedTypeSatisfiesPred<name, IsShapedTypePred>,
    SubstLeaves<"$_self", "getElementTypeOrSelf($" # name # ")",
      type.predicate>]>;
 class ElementTypeIs<string name, Type type> : PredOpTrait<



More information about the Mlir-commits mailing list