Add a new TestDialect directory in tests/. This directory defines a fake 'TestDia...
authorRiver Riddle <riverriddle@google.com>
Thu, 23 May 2019 22:11:19 +0000 (15:11 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Sun, 2 Jun 2019 02:59:04 +0000 (19:59 -0700)
--

PiperOrigin-RevId: 249724328

mlir/test/CMakeLists.txt
mlir/test/TestDialect/CMakeLists.txt [new file with mode: 0644]
mlir/test/TestDialect/TestDialect.cpp [new file with mode: 0644]
mlir/test/TestDialect/TestDialect.h [new file with mode: 0644]
mlir/test/TestDialect/TestOps.td [new file with mode: 0644]
mlir/test/TestDialect/TestPatterns.cpp [new file with mode: 0644]
mlir/test/TestDialect/lit.local.cfg [new file with mode: 0644]
mlir/test/lit.cfg.py
mlir/test/mlir-tblgen/directive-verifyUnusedValue.td [deleted file]
mlir/test/mlir-tblgen/verify-unused-value.mlir [new file with mode: 0644]

index edd9387..b03c7df 100644 (file)
@@ -1,6 +1,7 @@
 add_subdirectory(EDSC)
 add_subdirectory(mlir-cpu-runner)
 add_subdirectory(SDBM)
+add_subdirectory(TestDialect)
 
 llvm_canonicalize_cmake_booleans(
   LLVM_BUILD_EXAMPLES
@@ -31,6 +32,7 @@ set(MLIR_TEST_DEPENDS
   mlir-opt
   mlir-sdbm-api-test
   mlir-tblgen
+  mlir-test-opt
   mlir-translate
   sdot
   )
diff --git a/mlir/test/TestDialect/CMakeLists.txt b/mlir/test/TestDialect/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b0b5027
--- /dev/null
@@ -0,0 +1,31 @@
+set(LLVM_TARGET_DEFINITIONS TestOps.td)
+mlir_tablegen(TestOps.h.inc -gen-op-decls)
+mlir_tablegen(TestOps.cpp.inc -gen-op-defs)
+mlir_tablegen(TestPatterns.inc -gen-rewriters)
+add_public_tablegen_target(MLIRTestOpsIncGen)
+
+add_llvm_library(MLIRTestDialect
+  TestDialect.cpp
+  TestPatterns.cpp
+)
+add_dependencies(MLIRTestDialect
+  MLIRTestOpsIncGen
+  LLVMSupport
+)
+target_link_libraries(MLIRTestDialect
+  LLVMSupport
+)
+
+add_executable(mlir-test-opt
+  ${MLIR_MAIN_SRC_DIR}/../tools/mlir-opt/mlir-opt.cpp
+)
+llvm_update_compile_flags(mlir-test-opt)
+whole_archive_link(mlir-test-opt
+  MLIRStandardOps
+  MLIRTestDialect
+)
+target_link_libraries(mlir-test-opt
+  MLIRTestDialect
+  MLIRMlirOptLib
+  LLVMSupport
+)
diff --git a/mlir/test/TestDialect/TestDialect.cpp b/mlir/test/TestDialect/TestDialect.cpp
new file mode 100644 (file)
index 0000000..49966df
--- /dev/null
@@ -0,0 +1,39 @@
+//===- TestDialect.cpp - MLIR Dialect for Testing -------------------------===//
+//
+// Copyright 2019 The MLIR Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// =============================================================================
+
+#include "TestDialect.h"
+#include "mlir/IR/PatternMatch.h"
+
+using namespace mlir;
+
+//===----------------------------------------------------------------------===//
+// TestDialect
+//===----------------------------------------------------------------------===//
+
+TestDialect::TestDialect(MLIRContext *context)
+    : Dialect(getDialectName(), context) {
+  addOperations<
+#define GET_OP_LIST
+#include "TestOps.cpp.inc"
+      >();
+}
+
+// Static initialization for Test dialect registration.
+static mlir::DialectRegistration<mlir::TestDialect> testDialect;
+
+#define GET_OP_CLASSES
+#include "TestOps.cpp.inc"
diff --git a/mlir/test/TestDialect/TestDialect.h b/mlir/test/TestDialect/TestDialect.h
new file mode 100644 (file)
index 0000000..4746914
--- /dev/null
@@ -0,0 +1,45 @@
+//===- TestDialect.h - MLIR Dialect for testing -----------------*- C++ -*-===//
+//
+// Copyright 2019 The MLIR Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// =============================================================================
+//
+// This file defines a fake 'test' dialect that can be used for testing things
+// that do not have a respective counterpart in the main source directories.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TESTDIALECT_H
+#define MLIR_TESTDIALECT_H
+
+#include "mlir/IR/Dialect.h"
+#include "mlir/IR/OpDefinition.h"
+
+namespace mlir {
+
+class TestDialect : public Dialect {
+public:
+  /// Create the dialect in the given `context`.
+  TestDialect(MLIRContext *context);
+
+  /// Get the canonical string name of the dialect.
+  static StringRef getDialectName() { return "test"; }
+};
+
+#define GET_OP_CLASSES
+#include "TestOps.h.inc"
+
+} // end namespace mlir
+
+#endif // MLIR_TESTDIALECT_H
diff --git a/mlir/test/TestDialect/TestOps.td b/mlir/test/TestDialect/TestOps.td
new file mode 100644 (file)
index 0000000..c571f12
--- /dev/null
@@ -0,0 +1,49 @@
+//===-- TestOps.td - Test dialect operation definitions ----*- tablegen -*-===//
+//
+// Copyright 2019 The MLIR Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// =============================================================================
+
+#ifdef TEST_OPS
+#else
+#define TEST_OPS
+
+#ifdef OP_BASE
+#else
+include "mlir/IR/OpBase.td"
+#endif // OP_BASE
+
+def TEST_Dialect : Dialect {
+  let name = "test";
+  let cppNamespace = "";
+}
+
+class TEST_Op<string mnemonic, list<OpTrait> traits = []> :
+    Op<TEST_Dialect, mnemonic, traits>;
+
+//===----------------------------------------------------------------------===//
+// 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)
+      ]>;
+
+#endif // TEST_OPS
\ No newline at end of file
diff --git a/mlir/test/TestDialect/TestPatterns.cpp b/mlir/test/TestDialect/TestPatterns.cpp
new file mode 100644 (file)
index 0000000..1d5d398
--- /dev/null
@@ -0,0 +1,36 @@
+//===- TestPatterns.cpp - Test dialect pattern driver ---------------------===//
+//
+// Copyright 2019 The MLIR Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// =============================================================================
+
+#include "TestDialect.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/Pass/Pass.h"
+using namespace mlir;
+
+namespace {
+#include "TestPatterns.inc"
+
+struct TestPatternDriver : public FunctionPass<TestPatternDriver> {
+  void runOnFunction() {
+    mlir::OwningRewritePatternList patterns;
+    populateWithGenerated(&getContext(), &patterns);
+    applyPatternsGreedily(getFunction(), std::move(patterns));
+  }
+};
+} // end anonymous namespace
+
+static mlir::PassRegistration<TestPatternDriver>
+    pass("test-patterns", "Run test dialect patterns");
diff --git a/mlir/test/TestDialect/lit.local.cfg b/mlir/test/TestDialect/lit.local.cfg
new file mode 100644 (file)
index 0000000..edb5b44
--- /dev/null
@@ -0,0 +1 @@
+config.suffixes.remove('.td')
\ No newline at end of file
index 3e6dfc3..d978e9d 100644 (file)
@@ -55,6 +55,7 @@ tool_dirs = [config.mlir_tools_dir, config.llvm_tools_dir]
 tools = [
     'mlir-opt',
     'mlir-tblgen',
+    'mlir-test-opt',
     'mlir-translate',
     'mlir-edsc-builder-api-test',
 ]
diff --git a/mlir/test/mlir-tblgen/directive-verifyUnusedValue.td b/mlir/test/mlir-tblgen/directive-verifyUnusedValue.td
deleted file mode 100644 (file)
index 6d374c2..0000000
+++ /dev/null
@@ -1,36 +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";
-  let cppNamespace = "";
-}
-class NS_Op<string mnemonic, list<OpTrait> traits> :
-    Op<Test_Dialect, mnemonic, traits>;
-
-def ThreeResultOp : NS_Op<"three_result_op", []> {
-  let arguments = (ins I32:$input);
-  let results = (outs I32:$r1, I32:$r2, I32:$r3);
-}
-
-def OneResultOp : NS_Op<"one_result_op", []> {
-  let arguments = (ins I32:$input);
-  let results = (outs I32:$r1);
-}
-
-def : Pattern<(ThreeResultOp $input), [
-        (verifyUnusedValue),
-        (OneResultOp $input),
-        (verifyUnusedValue)
-      ]>;
-
-// CHECK-LABEL: struct GeneratedConvert0
-
-// CHECK: PatternMatchResult match(
-// CHECK:   if (!op0->getResult(0)->use_empty()) return matchFailure();
-// CHECK:   if (!op0->getResult(2)->use_empty()) return matchFailure();
-
-// CHECK: void rewrite(
-// CHECK:   auto vOneResultOp0 = rewriter.create<OneResultOp>(
-// CHECK:   rewriter.replaceOp(op, {nullptr, vOneResultOp0, nullptr});
diff --git a/mlir/test/mlir-tblgen/verify-unused-value.mlir b/mlir/test/mlir-tblgen/verify-unused-value.mlir
new file mode 100644 (file)
index 0000000..36ce869
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: mlir-test-opt -test-patterns %s | FileCheck %s
+
+//===----------------------------------------------------------------------===//
+// Test 'verifyUnusedValue'
+//===----------------------------------------------------------------------===//
+
+// CHECK-LABEL: @match_success_on_unused_first_result
+func @match_success_on_unused_first_result(%arg0 : i32) -> i32 {
+  // CHECK-NEXT: return {{.*}} : i32
+  %result:2 = "test.vuv_two_result_op"(%arg0) : (i32) -> (i32, i32)
+  return %result#1 : i32
+}
+
+// CHECK-LABEL: @match_fail_on_used_first_result
+func @match_fail_on_used_first_result(%arg0 : i32) -> i32 {
+  // CHECK-NEXT: "test.vuv_two_result_op"(%arg0) : (i32) -> (i32, i32)
+  %result:2 = "test.vuv_two_result_op"(%arg0) : (i32) -> (i32, i32)
+  "foo.unknown_op"(%result#0) : (i32) -> ()
+  return %result#1 : i32
+}