1//===- PDLBase.td - PDL base definitions -------------------*- tablegen -*-===// 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// Defines base support for MLIR PDL operations. 10// 11//===----------------------------------------------------------------------===// 12 13#ifndef MLIR_DIALECT_PDL_IR_PDLBASE 14#define MLIR_DIALECT_PDL_IR_PDLBASE 15 16include "mlir/IR/OpBase.td" 17 18//===----------------------------------------------------------------------===// 19// PDL Dialect 20//===----------------------------------------------------------------------===// 21 22def PDL_Dialect : Dialect { 23 string summary = "High level pattern definition dialect"; 24 string description = [{ 25 PDL presents a high level abstraction for the rewrite pattern infrastructure 26 available in MLIR. This abstraction allows for representing patterns 27 transforming MLIR, as MLIR. This allows for applying all of the benefits 28 that the general MLIR infrastructure provides, to the infrastructure itself. 29 This means that pattern matching can be more easily verified for 30 correctness, targeted by frontends, and optimized. 31 32 PDL abstracts over various different aspects of patterns and core MLIR data 33 structures. Patterns are specified via a `pdl.pattern` operation. These 34 operations contain a region body for the "matcher" code, and terminate with 35 a `pdl.rewrite` that either dispatches to an external rewriter or contains 36 a region for the rewrite specified via `pdl`. The types of values in `pdl` 37 are handle types to MLIR C++ types, with `!pdl.attribute`, `!pdl.operation`, 38 and `!pdl.type` directly mapping to `mlir::Attribute`, `mlir::Operation*`, 39 and `mlir::Value` respectively. 40 41 An example pattern is shown below: 42 43 ```mlir 44 // pdl.pattern contains metadata similarly to a `RewritePattern`. 45 pdl.pattern : benefit(1) { 46 // External input operand values are specified via `pdl.input` operations. 47 // Result types are constrainted via `pdl.type` operations. 48 49 %resultType = pdl.type 50 %inputOperand = pdl.input 51 %root, %results = pdl.operation "foo.op"(%inputOperand) -> %resultType 52 pdl.rewrite %root { 53 pdl.replace %root with (%inputOperand) 54 } 55 } 56 ``` 57 58 The above pattern simply replaces an operation with its first operand. Note 59 how the input operation is specified structurally, similarly to how it would 60 look in memory. This is a simple example and pdl provides support for many 61 other features such as applying external constraints or external generator 62 methods. These features and more are detailed below. 63 }]; 64 65 let name = "pdl"; 66 let cppNamespace = "::mlir::pdl"; 67} 68 69//===----------------------------------------------------------------------===// 70// PDL Types 71//===----------------------------------------------------------------------===// 72 73class PDL_Handle<string underlying> : 74 DialectType<PDL_Dialect, CPred<"$_self.isa<" # underlying # ">()">, 75 underlying>, 76 BuildableType<"$_builder.getType<" # underlying # ">()">; 77 78// Handle for `mlir::Attribute`. 79def PDL_Attribute : PDL_Handle<"mlir::pdl::AttributeType">; 80 81// Handle for `mlir::Operation*`. 82def PDL_Operation : PDL_Handle<"mlir::pdl::OperationType">; 83 84// Handle for `mlir::Type`. 85def PDL_Type : PDL_Handle<"mlir::pdl::TypeType">; 86 87// Handle for `mlir::Value`. 88def PDL_Value : PDL_Handle<"mlir::pdl::ValueType">; 89 90// A positional value is a location on a pattern DAG, which may be an operation, 91// an attribute, or an operand/result. 92def PDL_PositionalValue : 93 AnyTypeOf<[PDL_Attribute, PDL_Operation, PDL_Type, PDL_Value], 94 "Positional Value">; 95 96#endif // MLIR_DIALECT_PDL_IR_PDLBASE 97