OSDN Git Service

[mlir][PDL] Use ODS for defining PDL types
authorRiver Riddle <riddleriver@gmail.com>
Fri, 8 Jan 2021 20:29:47 +0000 (12:29 -0800)
committerRiver Riddle <riddleriver@gmail.com>
Fri, 8 Jan 2021 20:32:28 +0000 (12:32 -0800)
This removes the need to define these classes and their parser/printers in C++.

Differential Revision: https://reviews.llvm.org/D94135

mlir/include/mlir/Dialect/PDL/IR/PDL.h
mlir/include/mlir/Dialect/PDL/IR/PDLDialect.td [moved from mlir/include/mlir/Dialect/PDL/IR/PDLBase.td with 69% similarity]
mlir/include/mlir/Dialect/PDL/IR/PDLOps.h [new file with mode: 0644]
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
mlir/include/mlir/Dialect/PDL/IR/PDLTypes.h
mlir/include/mlir/Dialect/PDL/IR/PDLTypes.td [new file with mode: 0644]
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterp.h
mlir/include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td
mlir/lib/Conversion/PDLToPDLInterp/PredicateTree.h
mlir/lib/Dialect/PDL/IR/PDL.cpp
mlir/lib/Rewrite/FrozenRewritePatternList.cpp

index 1413602..bbc5230 100644 (file)
 #ifndef MLIR_DIALECT_PDL_IR_PDL_H_
 #define MLIR_DIALECT_PDL_IR_PDL_H_
 
-#include "mlir/IR/Builders.h"
 #include "mlir/IR/Dialect.h"
-#include "mlir/IR/OpImplementation.h"
-#include "mlir/IR/SymbolTable.h"
-#include "mlir/Interfaces/SideEffectInterfaces.h"
 
 //===----------------------------------------------------------------------===//
 // PDL Dialect
 
 #include "mlir/Dialect/PDL/IR/PDLOpsDialect.h.inc"
 
-//===----------------------------------------------------------------------===//
-// PDL Dialect Operations
-//===----------------------------------------------------------------------===//
-
-#define GET_OP_CLASSES
-#include "mlir/Dialect/PDL/IR/PDLOps.h.inc"
-
-
 #endif // MLIR_DIALECT_PDL_IR_PDL_H_
similarity index 69%
rename from mlir/include/mlir/Dialect/PDL/IR/PDLBase.td
rename to mlir/include/mlir/Dialect/PDL/IR/PDLDialect.td
index b372e59..14a8400 100644 (file)
@@ -1,4 +1,4 @@
-//===- PDLBase.td - PDL base definitions -------------------*- tablegen -*-===//
+//===- PDLDialect.td - PDL dialect definition --------------*- tablegen -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// Defines base support for MLIR PDL operations.
+// Defines the MLIR PDL dialect.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef MLIR_DIALECT_PDL_IR_PDLBASE
-#define MLIR_DIALECT_PDL_IR_PDLBASE
+#ifndef MLIR_DIALECT_PDL_IR_PDLDIALECT
+#define MLIR_DIALECT_PDL_IR_PDLDIALECT
 
 include "mlir/IR/OpBase.td"
 
@@ -66,31 +66,4 @@ def PDL_Dialect : Dialect {
   let cppNamespace = "::mlir::pdl";
 }
 
-//===----------------------------------------------------------------------===//
-// PDL Types
-//===----------------------------------------------------------------------===//
-
-class PDL_Handle<string underlying> :
-    DialectType<PDL_Dialect, CPred<"$_self.isa<" # underlying # ">()">,
-                underlying>,
-    BuildableType<"$_builder.getType<" # underlying # ">()">;
-
-// Handle for `mlir::Attribute`.
-def PDL_Attribute : PDL_Handle<"mlir::pdl::AttributeType">;
-
-// Handle for `mlir::Operation*`.
-def PDL_Operation : PDL_Handle<"mlir::pdl::OperationType">;
-
-// Handle for `mlir::Type`.
-def PDL_Type : PDL_Handle<"mlir::pdl::TypeType">;
-
-// Handle for `mlir::Value`.
-def PDL_Value : PDL_Handle<"mlir::pdl::ValueType">;
-
-// A positional value is a location on a pattern DAG, which may be an operation,
-// an attribute, or an operand/result.
-def PDL_PositionalValue :
-    AnyTypeOf<[PDL_Attribute, PDL_Operation, PDL_Type, PDL_Value],
-              "Positional Value">;
-
-#endif // MLIR_DIALECT_PDL_IR_PDLBASE
+#endif // MLIR_DIALECT_PDL_IR_PDLDIALECT
diff --git a/mlir/include/mlir/Dialect/PDL/IR/PDLOps.h b/mlir/include/mlir/Dialect/PDL/IR/PDLOps.h
new file mode 100644 (file)
index 0000000..8e560ae
--- /dev/null
@@ -0,0 +1,29 @@
+//===- PDLOps.h - Pattern Descriptor Language Operations --------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the operations for the Pattern Descriptor Language dialect.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_PDL_IR_PDLOPS_H_
+#define MLIR_DIALECT_PDL_IR_PDLOPS_H_
+
+#include "mlir/Dialect/PDL/IR/PDLTypes.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/OpImplementation.h"
+#include "mlir/IR/SymbolTable.h"
+#include "mlir/Interfaces/SideEffectInterfaces.h"
+
+//===----------------------------------------------------------------------===//
+// PDL Dialect Operations
+//===----------------------------------------------------------------------===//
+
+#define GET_OP_CLASSES
+#include "mlir/Dialect/PDL/IR/PDLOps.h.inc"
+
+#endif // MLIR_DIALECT_PDL_IR_PDLOPS_H_
index 1cbc044..e4a9ebe 100644 (file)
@@ -13,7 +13,7 @@
 #ifndef MLIR_DIALECT_PDL_IR_PDLOPS
 #define MLIR_DIALECT_PDL_IR_PDLOPS
 
-include "mlir/Dialect/PDL/IR/PDLBase.td"
+include "mlir/Dialect/PDL/IR/PDLTypes.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
 include "mlir/IR/SymbolInterfaces.td"
 
index b3cfcc8..a9028f3 100644 (file)
@@ -1,4 +1,4 @@
-//===- PDL.h - Pattern Descriptor Language Types ----------------*- C++ -*-===//
+//===- PDLTypes.h - Pattern Descriptor Language Types -----------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 
 #include "mlir/IR/Types.h"
 
-namespace mlir {
-namespace pdl {
 //===----------------------------------------------------------------------===//
 // PDL Dialect Types
 //===----------------------------------------------------------------------===//
 
-/// This type represents a handle to an `mlir::Attribute`.
-struct AttributeType : public Type::TypeBase<AttributeType, Type, TypeStorage> {
-  using Base::Base;
-};
-
-/// This type represents a handle to an `mlir::Operation*`.
-struct OperationType : public Type::TypeBase<OperationType, Type, TypeStorage> {
-  using Base::Base;
-};
-
-/// This type represents a handle to an `mlir::Type`.
-struct TypeType : public Type::TypeBase<TypeType, Type, TypeStorage> {
-  using Base::Base;
-};
-
-/// This type represents a handle to an `mlir::Value`.
-struct ValueType : public Type::TypeBase<ValueType, Type, TypeStorage> {
-  using Base::Base;
-};
-
-} // end namespace pdl
-} // end namespace mlir
+#define GET_TYPEDEF_CLASSES
+#include "mlir/Dialect/PDL/IR/PDLOpsTypes.h.inc"
 
 #endif // MLIR_DIALECT_PDL_IR_PDLTYPES_H_
diff --git a/mlir/include/mlir/Dialect/PDL/IR/PDLTypes.td b/mlir/include/mlir/Dialect/PDL/IR/PDLTypes.td
new file mode 100644 (file)
index 0000000..1cf0b1e
--- /dev/null
@@ -0,0 +1,84 @@
+//===- PDLTypes.td - Pattern descriptor types --------------*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the Pattern Descriptor Language dialect types.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_DIALECT_PDL_IR_PDLTYPES
+#define MLIR_DIALECT_PDL_IR_PDLTYPES
+
+include "mlir/Dialect/PDL/IR/PDLDialect.td"
+
+//===----------------------------------------------------------------------===//
+// PDL Types
+//===----------------------------------------------------------------------===//
+
+class PDL_Type<string name, string typeMnemonic> : TypeDef<PDL_Dialect, name> {
+  let mnemonic = typeMnemonic;
+}
+
+//===----------------------------------------------------------------------===//
+// pdl::AttributeType
+//===----------------------------------------------------------------------===//
+
+def PDL_Attribute : PDL_Type<"Attribute", "attribute"> {
+  let summary = "PDL handle to an `mlir::Attribute`";
+  let description = [{
+    This type represents a handle to an instance of an `mlir::Attribute`, bound
+    to a value that is usable within a PDL pattern or rewrite.
+  }];
+}
+
+//===----------------------------------------------------------------------===//
+// pdl::OperationType
+//===----------------------------------------------------------------------===//
+
+def PDL_Operation : PDL_Type<"Operation", "operation"> {
+  let summary = "PDL handle to an `mlir::Operation *`";
+  let description = [{
+    This type represents a handle to an instance of an `mlir::Operation *`,
+    bound to a value that is usable within a PDL pattern or rewrite.
+  }];
+}
+
+//===----------------------------------------------------------------------===//
+// pdl::TypeType
+//===----------------------------------------------------------------------===//
+
+def PDL_Type : PDL_Type<"Type", "type"> {
+  let summary = "PDL handle to an `mlir::Type`";
+  let description = [{
+    This type represents a handle to an instance of an `mlir::Type`, bound to a
+    value that is usable within a PDL pattern or rewrite.
+  }];
+}
+
+//===----------------------------------------------------------------------===//
+// pdl::ValueType
+//===----------------------------------------------------------------------===//
+
+def PDL_Value : PDL_Type<"Value", "value"> {
+  let summary = "PDL handle for an `mlir::Value`";
+  let description = [{
+    This type represents a handle to an instance of an `mlir::Value`, bound to a
+    value that is usable within a PDL pattern or rewrite.
+  }];
+}
+
+//===----------------------------------------------------------------------===//
+// Additional Type Constraints
+//===----------------------------------------------------------------------===//
+
+// A positional value is a location on a pattern DAG, which may be an attribute,
+// operation, or operand/result.
+def PDL_PositionalValue :
+    AnyTypeOf<[PDL_Attribute, PDL_Operation, PDL_Type, PDL_Value],
+              "Positional Value">;
+
+#endif // MLIR_DIALECT_PDL_IR_PDLTYPES
index 07c7f84..a7e1d03 100644 (file)
@@ -15,6 +15,7 @@
 #define MLIR_DIALECT_PDLINTERP_IR_PDLINTERP_H_
 
 #include "mlir/Dialect/PDL/IR/PDL.h"
+#include "mlir/Dialect/PDL/IR/PDLTypes.h"
 #include "mlir/Interfaces/InferTypeOpInterface.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 
index 6b11c0d..5720a62 100644 (file)
@@ -13,7 +13,7 @@
 #ifndef MLIR_DIALECT_PDLINTERP_IR_PDLINTERPOPS
 #define MLIR_DIALECT_PDLINTERP_IR_PDLINTERPOPS
 
-include "mlir/Dialect/PDL/IR/PDLBase.td"
+include "mlir/Dialect/PDL/IR/PDLTypes.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
 
 //===----------------------------------------------------------------------===//
index 15ba8cc..1621fa9 100644 (file)
@@ -15,7 +15,7 @@
 #define MLIR_LIB_CONVERSION_PDLTOPDLINTERP_PREDICATETREE_H_
 
 #include "Predicate.h"
-#include "mlir/Dialect/PDL/IR/PDL.h"
+#include "mlir/Dialect/PDL/IR/PDLOps.h"
 #include "llvm/ADT/MapVector.h"
 
 namespace mlir {
index 7791632..0268e6e 100644 (file)
@@ -7,11 +7,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Dialect/PDL/IR/PDL.h"
+#include "mlir/Dialect/PDL/IR/PDLOps.h"
 #include "mlir/Dialect/PDL/IR/PDLTypes.h"
 #include "mlir/IR/BuiltinTypes.h"
 #include "mlir/IR/DialectImplementation.h"
 #include "mlir/Interfaces/InferTypeOpInterface.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/TypeSwitch.h"
 
 using namespace mlir;
 using namespace mlir::pdl;
@@ -25,38 +27,10 @@ void PDLDialect::initialize() {
 #define GET_OP_LIST
 #include "mlir/Dialect/PDL/IR/PDLOps.cpp.inc"
       >();
-  addTypes<AttributeType, OperationType, TypeType, ValueType>();
-}
-
-Type PDLDialect::parseType(DialectAsmParser &parser) const {
-  StringRef keyword;
-  if (parser.parseKeyword(&keyword))
-    return Type();
-
-  Builder &builder = parser.getBuilder();
-  Type result = StringSwitch<Type>(keyword)
-                    .Case("attribute", builder.getType<AttributeType>())
-                    .Case("operation", builder.getType<OperationType>())
-                    .Case("type", builder.getType<TypeType>())
-                    .Case("value", builder.getType<ValueType>())
-                    .Default(Type());
-  if (!result)
-    parser.emitError(parser.getNameLoc(), "invalid 'pdl' type: `")
-        << keyword << "'";
-  return result;
-}
-
-void PDLDialect::printType(Type type, DialectAsmPrinter &printer) const {
-  if (type.isa<AttributeType>())
-    printer << "attribute";
-  else if (type.isa<OperationType>())
-    printer << "operation";
-  else if (type.isa<TypeType>())
-    printer << "type";
-  else if (type.isa<ValueType>())
-    printer << "value";
-  else
-    llvm_unreachable("unknown 'pdl' type");
+  addTypes<
+#define GET_TYPEDEF_LIST
+#include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
+      >();
 }
 
 /// Returns true if the given operation is used by a "binding" pdl operation
@@ -456,3 +430,27 @@ static LogicalResult verify(TypeOp op) {
 
 #define GET_OP_CLASSES
 #include "mlir/Dialect/PDL/IR/PDLOps.cpp.inc"
+
+//===----------------------------------------------------------------------===//
+// TableGen'd type method definitions
+//===----------------------------------------------------------------------===//
+
+#define GET_TYPEDEF_CLASSES
+#include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
+
+Type PDLDialect::parseType(DialectAsmParser &parser) const {
+  StringRef keyword;
+  if (parser.parseKeyword(&keyword))
+    return Type();
+  if (Type type = generatedTypeParser(getContext(), parser, keyword))
+    return type;
+
+  parser.emitError(parser.getNameLoc(), "invalid 'pdl' type: `")
+      << keyword << "'";
+  return Type();
+}
+
+void PDLDialect::printType(Type type, DialectAsmPrinter &printer) const {
+  if (failed(generatedTypePrinter(type, printer)))
+    llvm_unreachable("unknown 'pdl' type");
+}
index 40d7fcd..40f7aec 100644 (file)
@@ -9,7 +9,7 @@
 #include "mlir/Rewrite/FrozenRewritePatternList.h"
 #include "ByteCode.h"
 #include "mlir/Conversion/PDLToPDLInterp/PDLToPDLInterp.h"
-#include "mlir/Dialect/PDL/IR/PDL.h"
+#include "mlir/Dialect/PDL/IR/PDLOps.h"
 #include "mlir/Interfaces/SideEffectInterfaces.h"
 #include "mlir/Pass/Pass.h"
 #include "mlir/Pass/PassManager.h"