1//===-- Constraints.td - Constraints definition file ----------------*- 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// This file defines constraints/predicates for verifiers. 10// 11//===----------------------------------------------------------------------===// 12 13#ifndef CONSTRAINTS 14#define CONSTRAINTS 15 16include "mlir/IR/Utils.td" 17 18 19//===----------------------------------------------------------------------===// 20// Predicate definitions 21//===----------------------------------------------------------------------===// 22 23// Base class for logical predicates. 24// 25// Predicates are used to compose constraints (see next section for details). 26// There are two categories of predicates: 27// 28// 1. CPred: the primitive leaf predicate. 29// 2. Compound predicate: a predicate composed from child predicates using 30// predicate combiners ("conjunction", "disjunction", "negation" or 31// "substitution"). 32class Pred; 33 34// A logical predicate wrapping any C expression. 35// 36// This is the basis for composing more complex predicates. It is the "atom" 37// predicate from the perspective of TableGen and the "interface" between 38// TableGen and C++. What is inside is already C++ code, which will be treated 39// as opaque strings with special placeholders to be substituted. 40// 41// ## Special placeholders 42// 43// Special placeholders can be used to refer to entities in the context where 44// this predicate is used. They serve as "hooks" to the enclosing environment. 45// The following special placeholders are supported in constraints for an op: 46// 47// * `$_builder` will be replaced by a mlir::Builder instance. 48// * `$_op` will be replaced by the current operation. 49// * `$_self` will be replaced with the entity this predicate is attached to. 50// E.g., `BoolAttr` is an attribute constraint that wraps a 51// `CPred<"::llvm::isa<BoolAttr>($_self)">` (see the following sections for details). 52// Then for `F32:$attr`,`$_self` will be replaced by `$attr`. 53// For type constraints, it's a little bit special since we want the 54// constraints on each type definition reads naturally and we want to attach 55// type constraints directly to an operand/result, $_self will be replaced 56// by the operand/result's type. E.g., for `F32` in `F32:$operand`, its 57// `$_self` will be expanded as `getOperand(...).getType()`. 58// 59// One thing to be noticed, while using these placeholders in the C expression, 60// the type of placeholder is only guaranteed to be the base type. For example, 61// if you have a predicate in the form `CPred<"CheckType($_self)">, the argument 62// type of the function `CheckType` should be `mlir::Type`. 63class CPred<code pred> : Pred { 64 code predExpr = "(" # pred # ")"; 65} 66 67// Kinds of predicate combiners. These must closely match the predicates 68// implemented by the C++ backend (tblgen::PredCombinerKind). 69class PredCombinerKind; 70def PredCombinerAnd : PredCombinerKind; 71def PredCombinerOr : PredCombinerKind; 72def PredCombinerNot : PredCombinerKind; 73def PredCombinerSubstLeaves : PredCombinerKind; 74def PredCombinerConcat : PredCombinerKind; 75 76// A predicate that combines other predicates as defined by PredCombinerKind. 77// Instantiated below. 78class CombinedPred<PredCombinerKind k, list<Pred> c> : Pred { 79 PredCombinerKind kind = k; 80 list<Pred> children = c; 81} 82 83// Predicate combiners 84 85// A predicate that holds if all of its children hold. Always holds for zero 86// children. 87class And<list<Pred> children> : CombinedPred<PredCombinerAnd, children>; 88 89// A predicate that holds if any of its children hold. Never holds for zero 90// children. 91class Or<list<Pred> children> : CombinedPred<PredCombinerOr, children>; 92 93// A predicate that holds if its child does not. 94class Neg<Pred child> : CombinedPred<PredCombinerNot, [child]>; 95 96// A predicate that is always true. 97defvar TruePred = And<[]>; 98 99// A predicate that is always false. 100defvar False = Or<[]>; 101 102// A predicate that substitutes "pat" with "repl" in predicate calls of the 103// leaves of the predicate tree (i.e., not CombinedPred). 104// 105// This is plain string substitution without regular expressions or captures. 106// New predicates with more complex logical can be introduced should the need 107// arise. 108class SubstLeaves<string pat, string repl, Pred child> 109 : CombinedPred<PredCombinerSubstLeaves, [child]> { 110 string pattern = pat; 111 string replacement = repl; 112} 113 114// A predicate that prepends `pre` and appends `suf` to the final predicate 115// string composed from `child`. This is plain string concatenation and there 116// will be no substitution happening for `pre` and `suf`. 117class Concat<string pre, Pred child, string suf> : 118 CombinedPred<PredCombinerConcat, [child]> { 119 string prefix = pre; 120 string suffix = suf; 121} 122 123//===----------------------------------------------------------------------===// 124// Constraint definitions 125//===----------------------------------------------------------------------===// 126 127// TODO: Merge Constraints into Pred. 128 129// Base class for named constraints. 130// 131// An op's operands/attributes/results can have various requirements, e.g., 132// having certain types, having values inside a certain range, and so on. 133// Besides, for a graph rewrite rule, the source pattern used to match against 134// the existing graph has conditions, like the op's operand must be of a more 135// constrained subtype, the attribute must have a certain value, and so on. 136// 137// These requirements and conditions are modeled using this class. Records of 138// this class are used to generate verification code in op verifier, and 139// matching code in pattern matcher. 140// 141// Constraints are predicates with descriptive names, to facilitate inspection, 142// provide nice error messages, etc. 143class Constraint<Pred pred, string desc = ""> { 144 // The predicates that this constraint requires. 145 Pred predicate = pred; 146 // User-readable one line summary used in error reporting messages. If empty, 147 // a generic message will be used. 148 string summary = desc; 149} 150 151// Subclasses used to differentiate different constraint kinds. These are used 152// as markers for the TableGen backend to handle different constraint kinds 153// differently if needed. Constraints not deriving from the following subclasses 154// are considered as uncategorized constraints. 155 156// Subclass for constraints on a type. 157class TypeConstraint<Pred predicate, string summary = "", 158 string cppTypeParam = "::mlir::Type", 159 string cppFunctionNameParam = ""> : 160 Constraint<predicate, summary> { 161 // The name of the C++ Type class if known, or Type if not. 162 string cppType = cppTypeParam; 163 // The name of the C++ function that is generated for this type constraint. 164 // If empty, no C++ function is generated. 165 string cppFunctionName = cppFunctionNameParam; 166} 167 168// Subclass for constraints on an attribute. 169class AttrConstraint<Pred predicate, string summary = ""> : 170 Constraint<predicate, summary>; 171 172// Subclass for constraints on a region. 173class RegionConstraint<Pred predicate, string summary = ""> : 174 Constraint<predicate, summary>; 175 176// Subclass for constraints on a successor. 177class SuccessorConstraint<Pred predicate, string summary = ""> : 178 Constraint<predicate, summary>; 179 180// How to use these constraint categories: 181// 182// * Use TypeConstraint to specify 183// * Constraints on an op's operand/result definition 184// * Further constraints to match an op's operand/result in source pattern 185// 186// * Use Attr (a subclass for AttrConstraint) for 187// * Constraints on an op's attribute definition 188// * Use AttrConstraint to specify 189// * Further constraints to match an op's attribute in source pattern 190// 191// * Use uncategorized constraint to specify 192// * Multi-entity constraints in rewrite rules 193 194#endif // CONSTRAINTS 195