xref: /llvm-project/mlir/lib/Dialect/Transform/PDLExtension/PDLExtension.cpp (revision 84cc1865ef9202af39404ff4524a9b13df80cfc1)
1 //===- PDLExtension.cpp - PDL extension for the Transform dialect ---------===//
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/Transform/PDLExtension/PDLExtension.h"
10 #include "mlir/Dialect/PDL/IR/PDL.h"
11 #include "mlir/Dialect/PDL/IR/PDLTypes.h"
12 #include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
13 #include "mlir/Dialect/Transform/IR/TransformDialect.h"
14 #include "mlir/Dialect/Transform/PDLExtension/PDLExtensionOps.h"
15 #include "mlir/IR/DialectRegistry.h"
16 
17 using namespace mlir;
18 
19 namespace {
20 /// Implementation of the TransformHandleTypeInterface for the PDL
21 /// OperationType. Accepts any payload operation.
22 struct PDLOperationTypeTransformHandleTypeInterfaceImpl
23     : public transform::TransformHandleTypeInterface::ExternalModel<
24           PDLOperationTypeTransformHandleTypeInterfaceImpl,
25           pdl::OperationType> {
26 
27   /// Accept any operation.
28   DiagnosedSilenceableFailure
29   checkPayload(Type type, Location loc, ArrayRef<Operation *> payload) const {
30     return DiagnosedSilenceableFailure::success();
31   }
32 };
33 } // namespace
34 
35 namespace {
36 /// PDL extension of the Transform dialect. This provides transform operations
37 /// that connect to PDL matching as well as interfaces for PDL types to be used
38 /// with Transform dialect operations.
39 class PDLExtension : public transform::TransformDialectExtension<PDLExtension> {
40 public:
41   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(PDLExtension)
42 
43   void init() {
44     registerTransformOps<
45 #define GET_OP_LIST
46 #include "mlir/Dialect/Transform/PDLExtension/PDLExtensionOps.cpp.inc"
47         >();
48 
49     addDialectDataInitializer<transform::PDLMatchHooks>(
50         [](transform::PDLMatchHooks &) {});
51 
52     // Declare PDL as dependent so we can attach an interface to its type in the
53     // later step.
54     declareDependentDialect<pdl::PDLDialect>();
55 
56     // PDLInterp is only relevant if we actually apply the transform IR so
57     // declare it as generated.
58     declareGeneratedDialect<pdl_interp::PDLInterpDialect>();
59 
60     // Make PDL OperationType usable as a transform dialect type.
61     addCustomInitializationStep([](MLIRContext *context) {
62       pdl::OperationType::attachInterface<
63           PDLOperationTypeTransformHandleTypeInterfaceImpl>(*context);
64     });
65   }
66 };
67 } // namespace
68 
69 void mlir::transform::registerPDLExtension(DialectRegistry &dialectRegistry) {
70   dialectRegistry.addExtensions<PDLExtension>();
71 }
72