From 80381abfd0d99af2041b84b09374ab699fcabb4f Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Fri, 5 Jul 2019 09:23:47 -0700 Subject: [PATCH] Migrate pattern attribute matching tests to use TestDialect This CL also reorders op definitions and tests a bit to make them group more logically. PiperOrigin-RevId: 256682105 --- mlir/test/lib/TestDialect/TestOps.td | 74 +++++++++++++++++++++++------------ mlir/test/mlir-tblgen/pattern-attr.td | 47 ---------------------- mlir/test/mlir-tblgen/pattern.mlir | 51 ++++++++++++++++++++---- 3 files changed, 91 insertions(+), 81 deletions(-) delete mode 100644 mlir/test/mlir-tblgen/pattern-attr.td diff --git a/mlir/test/lib/TestDialect/TestOps.td b/mlir/test/lib/TestDialect/TestOps.td index f5bba8c..890c1c6 100644 --- a/mlir/test/lib/TestDialect/TestOps.td +++ b/mlir/test/lib/TestDialect/TestOps.td @@ -33,20 +33,6 @@ class TEST_Op traits = []> : Op; //===----------------------------------------------------------------------===// -// Test 'verifyUnusedValue' -//===----------------------------------------------------------------------===// - -def VUVTwoResultOp : TEST_Op<"vuv_two_result_op", []> { - let arguments = (ins I32:$input); - let results = (outs I32:$r1, I32:$r2); -} - -def VUVFoldTwoResultOp : Pattern<(VUVTwoResultOp $input), [ - (verifyUnusedValue), - (replaceWithValue $input) - ]>; - -//===----------------------------------------------------------------------===// // Test Types //===----------------------------------------------------------------------===// @@ -138,6 +124,18 @@ def I64EnumAttrOp : TEST_Op<"i64_enum_attr"> { } //===----------------------------------------------------------------------===// +// Test Regions +//===----------------------------------------------------------------------===// + +def TwoRegionOp : TEST_Op<"two_region_op", []> { + let regions = (region AnyRegion, AnyRegion); +} + +def SizedRegionOp : TEST_Op<"sized_region_op", []> { + let regions = (region SizedRegion<2>:$my_region, SizedRegion<1>); +} + +//===----------------------------------------------------------------------===// // Test Traits //===----------------------------------------------------------------------===// @@ -206,10 +204,6 @@ def OpB : TEST_Op<"op_b"> { // Test named pattern. def TestNamedPatternRule : Pat<(OpA $input, $attr), (OpB $input, $attr)>; -// Test with constant attr. -def OpC : TEST_Op<"op_c">, Arguments<(ins I32:$arg)>, Results<(outs I32:$res)>; -def : Pat<(OpC $input), (OpB $input, ConstantAttr:$attr)>; - // Test with fused location. def : Pat<(OpA (OpA $input, $attr), $bttr), (OpB $input, $bttr)>; @@ -225,6 +219,33 @@ def : Pat<(OpD $input), (OpF $input), [], (addBenefit 10)>; def : Pat<(OpG $input), (OpB $input, ConstantAttr:$attr)>; def : Pat<(OpG (OpG $input)), (OpB $input, ConstantAttr:$attr)>; +//===----------------------------------------------------------------------===// +// Test Patterns (Attributes) + +// Test matching against op attributes. +def OpAttrMatch1 : TEST_Op<"match_op_attribute1"> { + let arguments = (ins + I32Attr:$required_attr, + OptionalAttr:$optional_attr, + DefaultValuedAttr:$default_valued_attr, + I32Attr:$more_attr + ); + let results = (outs I32:$output); +} +def OpAttrMatch2 : TEST_Op<"match_op_attribute2"> { + let arguments = OpAttrMatch1.arguments; + let results = (outs I32:$output); +} +def MoreConstraint : AttrConstraint< + CPred<"$_self.cast().getInt() == 4">, "more constraint">; +def : Pat<(OpAttrMatch1 $required, $optional, $default_valued, + MoreConstraint:$more), + (OpAttrMatch2 $required, $optional, $default_valued, $more)>; + +// Test with constant attr. +def OpC : TEST_Op<"op_c">, Arguments<(ins I32:$arg)>, Results<(outs I32:$res)>; +def : Pat<(OpC $input), (OpB $input, ConstantAttr:$attr)>; + // Test string enum attribute in rewrites. def : Pat<(StrEnumAttrOp StrCaseA), (StrEnumAttrOp StrCaseB)>; // Test integer enum attribute in rewrites. @@ -233,7 +254,6 @@ def : Pat<(I64EnumAttrOp I64Case5), (I64EnumAttrOp I64Case10)>; //===----------------------------------------------------------------------===// // Test Patterns (Multi-result Ops) -//===----------------------------------------------------------------------===// def MultiResultOpKind1: I64EnumAttrCase<"kind1", 1>; def MultiResultOpKind2: I64EnumAttrCase<"kind2", 2>; @@ -306,16 +326,18 @@ def : Pattern<(ThreeResultOp MultiResultOpKind4), (TwoResultOp:$res2__1 MultiResultOpKind4)]>; //===----------------------------------------------------------------------===// -// Test op regions +// Test Directives //===----------------------------------------------------------------------===// -def TwoRegionOp : TEST_Op<"two_region_op", []> { - let regions = (region AnyRegion, AnyRegion); -} - -def SizedRegionOp : TEST_Op<"sized_region_op", []> { - let regions = (region SizedRegion<2>:$my_region, SizedRegion<1>); +// Test 'verifyUnusedValue' +def VUVTwoResultOp : TEST_Op<"vuv_two_result_op", []> { + let arguments = (ins I32:$input); + let results = (outs I32:$r1, I32:$r2); } +def VUVFoldTwoResultOp : Pattern<(VUVTwoResultOp $input), [ + (verifyUnusedValue), + (replaceWithValue $input) + ]>; //===----------------------------------------------------------------------===// // Test Legalization diff --git a/mlir/test/mlir-tblgen/pattern-attr.td b/mlir/test/mlir-tblgen/pattern-attr.td deleted file mode 100644 index b767ef8..0000000 --- a/mlir/test/mlir-tblgen/pattern-attr.td +++ /dev/null @@ -1,47 +0,0 @@ -// RUN: mlir-tblgen -gen-rewriters -I %S/../../include %s | FileCheck %s - -include "mlir/IR/OpBase.td" - -def Test_Dialect : Dialect { - let name = "test"; -} -class NS_Op traits> : - Op; - -def MoreConstraint : AttrConstraint, "more constraint">; - -def OpA : NS_Op<"op_a", []> { - let arguments = (ins - I32Attr:$required_attr, - OptionalAttr:$optional_attr, - DefaultValuedAttr:$default_valued_attr, - I32Attr:$more_attr - ); - - let results = (outs I32:$result); -} - -def : Pat<(OpA $required, $optional, $default_valued, MoreConstraint:$more), - (OpA $required, $optional, $default_valued, $more)>; - -// Test attribute capturing -// --- - -// CHECK-LABEL: struct GeneratedConvert0 - -// CHECK: auto attr = op0->getAttrOfType("required_attr"); -// CHECK-NEXT: if (!attr) return matchFailure(); -// CHECK-NEXT: s.required = attr; - -// CHECK: auto attr = op0->getAttrOfType("optional_attr"); -// CHECK-NEXT: s.optional = attr; - -// CHECK: auto attr = op0->getAttrOfType("default_valued_attr"); -// CHECK-NEXT: if (!attr) attr = mlir::Builder(ctx).getIntegerAttr(mlir::Builder(ctx).getIntegerType(32), 42); -// CHECK-NEXT: s.default_valued = attr; - -// CHECK: auto attr = op0->getAttrOfType("more_attr"); -// CHECK-NEXT: if (!attr) return matchFailure(); -// CHECK-NEXT: if (!((MoreConstraint))) return matchFailure(); -// CHECK-NEXT: s.more = attr; - diff --git a/mlir/test/mlir-tblgen/pattern.mlir b/mlir/test/mlir-tblgen/pattern.mlir index 15c0b9a..9dbf24d 100644 --- a/mlir/test/mlir-tblgen/pattern.mlir +++ b/mlir/test/mlir-tblgen/pattern.mlir @@ -1,13 +1,5 @@ // RUN: mlir-opt -test-patterns -mlir-print-debuginfo %s | FileCheck %s -// CHECK-LABEL: verifyConstantAttr -func @verifyConstantAttr(%arg0 : i32) -> i32 { - %0 = "test.op_c"(%arg0) : (i32) -> i32 loc("a") - - // CHECK: "test.op_b"(%arg0) {attr = 17 : i32} : (i32) -> i32 loc("a") - return %0 : i32 -} - // CHECK-LABEL: verifyFusedLocs func @verifyFusedLocs(%arg0 : i32) -> i32 { %0 = "test.op_a"(%arg0) {attr = 10 : i32} : (i32) -> i32 loc("a") @@ -29,6 +21,49 @@ func @verifyBenefit(%arg0 : i32) -> i32 { return %0 : i32 } +//===----------------------------------------------------------------------===// +// Test Attributes +//===----------------------------------------------------------------------===// + +// CHECK-LABEL: succeedMatchOpAttr +func @succeedMatchOpAttr() -> i32 { + // CHECK: "test.match_op_attribute2"() {default_valued_attr = 3 : i32, more_attr = 4 : i32, optional_attr = 2 : i32, required_attr = 1 : i32} + %0 = "test.match_op_attribute1"() {required_attr = 1: i32, optional_attr = 2: i32, default_valued_attr = 3: i32, more_attr = 4: i32} : () -> (i32) + return %0: i32 +} + +// CHECK-LABEL: succeedMatchMissingOptionalAttr +func @succeedMatchMissingOptionalAttr() -> i32 { + // CHECK: "test.match_op_attribute2"() {default_valued_attr = 3 : i32, more_attr = 4 : i32, required_attr = 1 : i32} + %0 = "test.match_op_attribute1"() {required_attr = 1: i32, default_valued_attr = 3: i32, more_attr = 4: i32} : () -> (i32) + return %0: i32 +} + +// CHECK-LABEL: succeedMatchMissingDefaultValuedAttr +func @succeedMatchMissingDefaultValuedAttr() -> i32 { + // CHECK: "test.match_op_attribute2"() {default_valued_attr = 42 : i32, more_attr = 4 : i32, optional_attr = 2 : i32, required_attr = 1 : i32} + %0 = "test.match_op_attribute1"() {required_attr = 1: i32, optional_attr = 2: i32, more_attr = 4: i32} : () -> (i32) + return %0: i32 +} + +// CHECK-LABEL: failedMatchAdditionalConstraintNotSatisfied +func @failedMatchAdditionalConstraintNotSatisfied() -> i32 { + // CHECK: "test.match_op_attribute1"() + %0 = "test.match_op_attribute1"() {required_attr = 1: i32, optional_attr = 2: i32, more_attr = 5: i32} : () -> (i32) + return %0: i32 +} + +// CHECK-LABEL: verifyConstantAttr +func @verifyConstantAttr(%arg0 : i32) -> i32 { + // CHECK: "test.op_b"(%arg0) {attr = 17 : i32} : (i32) -> i32 loc("a") + %0 = "test.op_c"(%arg0) : (i32) -> i32 loc("a") + return %0 : i32 +} + +//===----------------------------------------------------------------------===// +// Test Enum Attributes +//===----------------------------------------------------------------------===// + // CHECK-LABEL: verifyStrEnumAttr func @verifyStrEnumAttr() -> i32 { // CHECK: "test.str_enum_attr"() {attr = "B"} -- 2.7.4