1 //===- DialectConversion.cpp - Dialect conversion unit 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/Transforms/DialectConversion.h"
10 #include "gtest/gtest.h"
14 static Operation
*createOp(MLIRContext
*context
) {
15 context
->allowUnregisteredDialects();
16 return Operation::create(UnknownLoc::get(context
),
17 OperationName("foo.bar", context
), llvm::None
,
18 llvm::None
, llvm::None
, llvm::None
, 0);
23 static StringRef
getOperationName() { return "foo.bar"; }
26 TEST(DialectConversionTest
, DynamicallyLegalOpCallbackOrder
) {
28 ConversionTarget
target(context
);
31 int callbackCalled1
= 0;
32 target
.addDynamicallyLegalOp
<DummyOp
>([&](Operation
*) {
33 callbackCalled1
= ++index
;
37 int callbackCalled2
= 0;
38 target
.addDynamicallyLegalOp
<DummyOp
>([&](Operation
*) -> Optional
<bool> {
39 callbackCalled2
= ++index
;
43 auto *op
= createOp(&context
);
44 EXPECT_TRUE(target
.isLegal(op
));
45 EXPECT_EQ(2, callbackCalled1
);
46 EXPECT_EQ(1, callbackCalled2
);
47 EXPECT_FALSE(target
.isIllegal(op
));
48 EXPECT_EQ(4, callbackCalled1
);
49 EXPECT_EQ(3, callbackCalled2
);
53 TEST(DialectConversionTest
, DynamicallyLegalOpCallbackSkip
) {
55 ConversionTarget
target(context
);
58 int callbackCalled
= 0;
59 target
.addDynamicallyLegalOp
<DummyOp
>([&](Operation
*) -> Optional
<bool> {
60 callbackCalled
= ++index
;
64 auto *op
= createOp(&context
);
65 EXPECT_FALSE(target
.isLegal(op
));
66 EXPECT_EQ(1, callbackCalled
);
67 EXPECT_FALSE(target
.isIllegal(op
));
68 EXPECT_EQ(2, callbackCalled
);
72 TEST(DialectConversionTest
, DynamicallyLegalUnknownOpCallbackOrder
) {
74 ConversionTarget
target(context
);
77 int callbackCalled1
= 0;
78 target
.markUnknownOpDynamicallyLegal([&](Operation
*) {
79 callbackCalled1
= ++index
;
83 int callbackCalled2
= 0;
84 target
.markUnknownOpDynamicallyLegal([&](Operation
*) -> Optional
<bool> {
85 callbackCalled2
= ++index
;
89 auto *op
= createOp(&context
);
90 EXPECT_TRUE(target
.isLegal(op
));
91 EXPECT_EQ(2, callbackCalled1
);
92 EXPECT_EQ(1, callbackCalled2
);
93 EXPECT_FALSE(target
.isIllegal(op
));
94 EXPECT_EQ(4, callbackCalled1
);
95 EXPECT_EQ(3, callbackCalled2
);
99 TEST(DialectConversionTest
, DynamicallyLegalReturnNone
) {
101 ConversionTarget
target(context
);
103 target
.addDynamicallyLegalOp
<DummyOp
>(
104 [&](Operation
*) -> Optional
<bool> { return llvm::None
; });
106 auto *op
= createOp(&context
);
107 EXPECT_FALSE(target
.isLegal(op
));
108 EXPECT_FALSE(target
.isIllegal(op
));
110 EXPECT_TRUE(succeeded(applyPartialConversion(op
, target
, {})));
111 EXPECT_TRUE(failed(applyFullConversion(op
, target
, {})));
116 TEST(DialectConversionTest
, DynamicallyLegalUnknownReturnNone
) {
118 ConversionTarget
target(context
);
120 target
.markUnknownOpDynamicallyLegal(
121 [&](Operation
*) -> Optional
<bool> { return llvm::None
; });
123 auto *op
= createOp(&context
);
124 EXPECT_FALSE(target
.isLegal(op
));
125 EXPECT_FALSE(target
.isIllegal(op
));
127 EXPECT_TRUE(succeeded(applyPartialConversion(op
, target
, {})));
128 EXPECT_TRUE(failed(applyFullConversion(op
, target
, {})));