1 //===- PassGenTest.cpp - TableGen PassGen Tests ---------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/Pass/Pass.h"
10 #include "llvm/ADT/STLExtras.h"
12 #include "gmock/gmock.h"
14 std::unique_ptr
<mlir::Pass
> createTestPassWithCustomConstructor(int v
= 0);
17 #define GEN_PASS_REGISTRATION
18 #include "PassGenTest.h.inc"
20 #define GEN_PASS_DEF_TESTPASS
21 #define GEN_PASS_DEF_TESTPASSWITHOPTIONS
22 #define GEN_PASS_DEF_TESTPASSWITHCUSTOMCONSTRUCTOR
23 #include "PassGenTest.h.inc"
25 struct TestPass
: public impl::TestPassBase
<TestPass
> {
26 using TestPassBase::TestPassBase
;
28 void runOnOperation() override
{}
30 std::unique_ptr
<mlir::Pass
> clone() const {
31 return TestPassBase
<TestPass
>::clone();
35 TEST(PassGenTest
, defaultGeneratedConstructor
) {
36 std::unique_ptr
<mlir::Pass
> pass
= createTestPass();
37 EXPECT_TRUE(pass
.get() != nullptr);
40 TEST(PassGenTest
, PassClone
) {
41 mlir::MLIRContext context
;
43 const auto unwrap
= [](const std::unique_ptr
<mlir::Pass
> &pass
) {
44 return static_cast<const TestPass
*>(pass
.get());
47 const auto origPass
= createTestPass();
48 const auto clonePass
= unwrap(origPass
)->clone();
50 EXPECT_TRUE(clonePass
.get() != nullptr);
51 EXPECT_TRUE(origPass
.get() != clonePass
.get());
54 struct TestPassWithOptions
55 : public impl::TestPassWithOptionsBase
<TestPassWithOptions
> {
56 using TestPassWithOptionsBase::TestPassWithOptionsBase
;
58 void runOnOperation() override
{}
60 std::unique_ptr
<mlir::Pass
> clone() const {
61 return TestPassWithOptionsBase
<TestPassWithOptions
>::clone();
64 int getTestOption() const { return testOption
; }
66 llvm::ArrayRef
<int64_t> getTestListOption() const { return testListOption
; }
69 TEST(PassGenTest
, PassOptions
) {
70 mlir::MLIRContext context
;
72 TestPassWithOptionsOptions options
;
73 options
.testOption
= 57;
75 options
.testListOption
= {1, 2};
77 const auto unwrap
= [](const std::unique_ptr
<mlir::Pass
> &pass
) {
78 return static_cast<const TestPassWithOptions
*>(pass
.get());
81 const auto pass
= createTestPassWithOptions(options
);
83 EXPECT_EQ(unwrap(pass
)->getTestOption(), 57);
84 EXPECT_EQ(unwrap(pass
)->getTestListOption()[0], 1);
85 EXPECT_EQ(unwrap(pass
)->getTestListOption()[1], 2);
88 struct TestPassWithCustomConstructor
89 : public impl::TestPassWithCustomConstructorBase
<
90 TestPassWithCustomConstructor
> {
91 explicit TestPassWithCustomConstructor(int v
) : extraVal(v
) {}
93 void runOnOperation() override
{}
95 std::unique_ptr
<mlir::Pass
> clone() const {
96 return TestPassWithCustomConstructorBase
<
97 TestPassWithCustomConstructor
>::clone();
100 unsigned int extraVal
= 23;
103 std::unique_ptr
<mlir::Pass
> createTestPassWithCustomConstructor(int v
) {
104 return std::make_unique
<TestPassWithCustomConstructor
>(v
);
107 TEST(PassGenTest
, PassCloneWithCustomConstructor
) {
108 mlir::MLIRContext context
;
110 const auto unwrap
= [](const std::unique_ptr
<mlir::Pass
> &pass
) {
111 return static_cast<const TestPassWithCustomConstructor
*>(pass
.get());
114 const auto origPass
= createTestPassWithCustomConstructor(10);
115 const auto clonePass
= unwrap(origPass
)->clone();
117 EXPECT_EQ(unwrap(origPass
)->extraVal
, unwrap(clonePass
)->extraVal
);