xref: /aosp_15_r20/external/tensorflow/tensorflow/core/ir/interfaces_test.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/ir/interfaces.h"
17 
18 #include "llvm/ADT/ScopeExit.h"
19 #include "mlir/IR/DialectInterface.h"  // from @llvm-project
20 #include "mlir/IR/Location.h"  // from @llvm-project
21 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
22 #include "mlir/IR/OperationSupport.h"  // from @llvm-project
23 #include "mlir/IR/Verifier.h"  // from @llvm-project
24 #include "tensorflow/core/ir/dialect.h"
25 #include "tensorflow/core/platform/test.h"
26 
27 namespace mlir {
28 namespace tfg {
29 namespace {
TEST(TensorFlowRegistryInterface,TestDefaultImplementation)30 TEST(TensorFlowRegistryInterface, TestDefaultImplementation) {
31   MLIRContext context(MLIRContext::Threading::DISABLED);
32   auto *dialect = context.getOrLoadDialect<TFGraphDialect>();
33 
34   OperationState state(UnknownLoc::get(&context), "tfg.Foo");
35   state.addTypes(dialect->getControlType());
36 
37   Operation *op = Operation::create(state);
38   auto cleanup = llvm::make_scope_exit([&] { op->destroy(); });
39   ASSERT_TRUE(succeeded(verify(op)));
40   auto iface = dyn_cast<TensorFlowRegistryInterface>(op);
41   EXPECT_FALSE(iface);
42 }
43 
TEST(TensorFlowRegisterInterface,TestCustomImplementation)44 TEST(TensorFlowRegisterInterface, TestCustomImplementation) {
45   MLIRContext context(MLIRContext::Threading::DISABLED);
46   DialectRegistry registry;
47   registry.insert<TFGraphDialect>();
48 
49   struct CustomRegistryInterface : public TensorFlowRegistryInterfaceBase {
50     using TensorFlowRegistryInterfaceBase::TensorFlowRegistryInterfaceBase;
51 
52     bool isStateful(Operation *op) const override {
53       return op->getName().stripDialect() == "Foo";
54     }
55   };
56 
57   registry.addExtension(+[](mlir::MLIRContext *ctx, TFGraphDialect *dialect) {
58     dialect->addInterfaces<CustomRegistryInterface>();
59   });
60   context.appendDialectRegistry(registry);
61 
62   auto *dialect = context.getOrLoadDialect<TFGraphDialect>();
63   SmallVector<StringRef, 2> op_names = {"tfg.Foo", "tfg.Bar"};
64   SmallVector<bool, 2> expected = {true, false};
65   for (auto it : llvm::zip(op_names, expected)) {
66     OperationState state(UnknownLoc::get(&context), std::get<0>(it));
67     state.addTypes(dialect->getControlType());
68     Operation *op = Operation::create(state);
69     auto cleanup = llvm::make_scope_exit([&] { op->destroy(); });
70     auto iface = dyn_cast<TensorFlowRegistryInterface>(op);
71     ASSERT_TRUE(iface);
72     EXPECT_EQ(iface.isStateful(), std::get<1>(it));
73   }
74 }
75 }  // namespace
76 }  // namespace tfg
77 }  // namespace mlir
78