Add TabelGen support to logically AND a list of attribute constraints.
authorAshwin Murthy <ashwinm@google.com>
Mon, 8 Apr 2019 21:32:39 +0000 (14:32 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Tue, 9 Apr 2019 02:17:56 +0000 (19:17 -0700)
    This is the AllOf version for AttrConstraint and allows specifying the combined constraint during pattern match.

--

PiperOrigin-RevId: 242540243

mlir/include/mlir/IR/OpBase.td
mlir/test/mlir-tblgen/pattern-allof-attr.td [new file with mode: 0644]

index baaaa27..52a4a1e 100644 (file)
@@ -637,6 +637,15 @@ class Confined<Attr attr, list<AttrConstraint> constraints> : Attr<
   let isOptional = attr.isOptional;
 }
 
+// An AttrConstraint that holds if all attr constraints specified in
+// 'constraints' hold.
+class AllAttrConstraintsOf<list<AttrConstraint> constraints> : AttrConstraint<
+    AllOf<!listconcat([!head(constraints).predicate],
+                      !foreach(pred, !tail(constraints), pred.predicate))>,
+    !foldl(/*init*/!head(constraints).description, /*list*/!tail(constraints),
+           prev, cur, prev # " and " # cur.description)> {
+}
+
 class IntMinValue<int n> : AttrConstraint<
     CPred<"{0}.cast<IntegerAttr>().getInt() >= " # n>,
     "whose minimal value is " # n>;
diff --git a/mlir/test/mlir-tblgen/pattern-allof-attr.td b/mlir/test/mlir-tblgen/pattern-allof-attr.td
new file mode 100644 (file)
index 0000000..e6f3799
--- /dev/null
@@ -0,0 +1,35 @@
+// RUN: mlir-tblgen -gen-rewriters -I %S/../../include %s | FileCheck %s
+
+include "mlir/IR/OpBase.td"
+
+def FirstConstraint : AttrConstraint<CPred<"FirstConstraint">,
+  "first constraint">;
+def SecondConstraint : AttrConstraint<CPred<"SecondConstraint">,
+  "second constraint">;
+def ThirdConstraint : AttrConstraint<CPred<"ThirdConstraint">,
+  "third constraint">;
+
+def OpA : Op<"op_a", []> {
+  let arguments = (ins
+    I32Attr:$attr
+  );
+
+  let results = (outs I32:$result);
+}
+
+def : Pat<
+  (OpA AllAttrConstraintsOf<
+    [FirstConstraint,
+     SecondConstraint,
+     ThirdConstraint]>:$more),
+  (OpA $more)>;
+
+// Test combining AttrConstraint during pattern match.
+// ---
+
+// CHECK-LABEL: struct GeneratedConvert0
+
+// CHECK:      auto attr = op0->getAttrOfType<IntegerAttr>("attr");
+// CHECK: if (!(((FirstConstraint)) && ((SecondConstraint)) && ((ThirdConstraint)))) return matchFailure();
+// CHECK-NEXT: s.more = attr;
+