1 //===- BuildOnlyExtensionTest.cpp - unit test for transform extensions ----===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Dialect/Func/IR/FuncOps.h"
10 #include "mlir/Dialect/Transform/IR/TransformDialect.h"
11 #include "mlir/IR/DialectRegistry.h"
12 #include "mlir/IR/MLIRContext.h"
13 #include "gtest/gtest.h"
14 
15 using namespace mlir;
16 using namespace mlir::transform;
17 
18 namespace {
19 class Extension : public TransformDialectExtension<Extension> {
20 public:
21   using Base::Base;
init()22   void init() { declareGeneratedDialect<func::FuncDialect>(); }
23 };
24 } // end namespace
25 
TEST(BuildOnlyExtensionTest,buildOnlyExtension)26 TEST(BuildOnlyExtensionTest, buildOnlyExtension) {
27   // Register the build-only version of the transform dialect extension. The
28   // func dialect is declared as generated so it should not be loaded along with
29   // the transform dialect.
30   DialectRegistry registry;
31   registry.addExtensions<BuildOnly<Extension>>();
32   MLIRContext ctx(registry);
33   ctx.getOrLoadDialect<TransformDialect>();
34   ASSERT_FALSE(ctx.getLoadedDialect<func::FuncDialect>());
35 }
36 
TEST(BuildOnlyExtensionTest,buildAndApplyExtension)37 TEST(BuildOnlyExtensionTest, buildAndApplyExtension) {
38   // Register the full version of the transform dialect extension. The func
39   // dialect should be loaded along with the transform dialect.
40   DialectRegistry registry;
41   registry.addExtensions<Extension>();
42   MLIRContext ctx(registry);
43   ctx.getOrLoadDialect<TransformDialect>();
44   ASSERT_TRUE(ctx.getLoadedDialect<func::FuncDialect>());
45 }
46