From 5bfe37691ce5a1950f1a551c500bf164b440f301 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Thu, 23 May 2019 15:11:19 -0700 Subject: [PATCH] Add a new TestDialect directory in tests/. This directory defines a fake 'TestDialect' that allows for the use of FileCheck to test things that aren't currently used anywhere else in tree. As a first order, this should simplify the tests used for tablegen components revolving around operation constraints/patterns. -- PiperOrigin-RevId: 249724328 --- mlir/test/CMakeLists.txt | 2 + mlir/test/TestDialect/CMakeLists.txt | 31 ++++++++++++++ mlir/test/TestDialect/TestDialect.cpp | 39 +++++++++++++++++ mlir/test/TestDialect/TestDialect.h | 45 ++++++++++++++++++++ mlir/test/TestDialect/TestOps.td | 49 ++++++++++++++++++++++ mlir/test/TestDialect/TestPatterns.cpp | 36 ++++++++++++++++ mlir/test/TestDialect/lit.local.cfg | 1 + mlir/test/lit.cfg.py | 1 + .../mlir-tblgen/directive-verifyUnusedValue.td | 36 ---------------- mlir/test/mlir-tblgen/verify-unused-value.mlir | 20 +++++++++ 10 files changed, 224 insertions(+), 36 deletions(-) create mode 100644 mlir/test/TestDialect/CMakeLists.txt create mode 100644 mlir/test/TestDialect/TestDialect.cpp create mode 100644 mlir/test/TestDialect/TestDialect.h create mode 100644 mlir/test/TestDialect/TestOps.td create mode 100644 mlir/test/TestDialect/TestPatterns.cpp create mode 100644 mlir/test/TestDialect/lit.local.cfg delete mode 100644 mlir/test/mlir-tblgen/directive-verifyUnusedValue.td create mode 100644 mlir/test/mlir-tblgen/verify-unused-value.mlir diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt index edd9387..b03c7df 100644 --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -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 index 0000000..b0b5027 --- /dev/null +++ b/mlir/test/TestDialect/CMakeLists.txt @@ -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 index 0000000..49966df --- /dev/null +++ b/mlir/test/TestDialect/TestDialect.cpp @@ -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 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 index 0000000..4746914 --- /dev/null +++ b/mlir/test/TestDialect/TestDialect.h @@ -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 index 0000000..c571f12 --- /dev/null +++ b/mlir/test/TestDialect/TestOps.td @@ -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 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) + ]>; + +#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 index 0000000..1d5d398 --- /dev/null +++ b/mlir/test/TestDialect/TestPatterns.cpp @@ -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 { + void runOnFunction() { + mlir::OwningRewritePatternList patterns; + populateWithGenerated(&getContext(), &patterns); + applyPatternsGreedily(getFunction(), std::move(patterns)); + } +}; +} // end anonymous namespace + +static mlir::PassRegistration + 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 index 0000000..edb5b44 --- /dev/null +++ b/mlir/test/TestDialect/lit.local.cfg @@ -0,0 +1 @@ +config.suffixes.remove('.td') \ No newline at end of file diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py index 3e6dfc3..d978e9d 100644 --- a/mlir/test/lit.cfg.py +++ b/mlir/test/lit.cfg.py @@ -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 index 6d374c2..0000000 --- a/mlir/test/mlir-tblgen/directive-verifyUnusedValue.td +++ /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 traits> : - Op; - -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( -// 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 index 0000000..36ce869 --- /dev/null +++ b/mlir/test/mlir-tblgen/verify-unused-value.mlir @@ -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 +} -- 2.7.4