1//===- PDLDialect.td - PDL dialect definition --------------*- 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 the MLIR PDL dialect. 10// 11//===----------------------------------------------------------------------===// 12 13#ifndef MLIR_DIALECT_PDL_IR_PDLDIALECT 14#define MLIR_DIALECT_PDL_IR_PDLDIALECT 15 16include "mlir/IR/OpBase.td" 17 18//===----------------------------------------------------------------------===// 19// PDL Dialect 20//===----------------------------------------------------------------------===// 21 22def PDL_Dialect : Dialect { 23 let summary = "High level pattern definition dialect"; 24 let 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 `!pdl.value`, and `!pdl.type` directly mapping to `mlir::Attribute`, 39 `mlir::Operation*`, `mlir::Value`, and `mlir::Type` 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.operand` operations. 47 // Result types are constrainted via `pdl.type` operations. 48 49 %resultType = pdl.type 50 %inputOperand = pdl.operand 51 %root = 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 let useDefaultTypePrinterParser = 1; 69 let extraClassDeclaration = [{ 70 void registerTypes(); 71 }]; 72} 73 74#endif // MLIR_DIALECT_PDL_IR_PDLDIALECT 75