Skip to content

Commit a236dc6

Browse files
authored
[mlir][NFC] Make Property a subclass of PropConstraint (#140848)
In preparation for allowing non-attribute properties in the declaritive rewrite pattern system, make `Property` a subclass of `PropConstraint` in tablegen and add a CK_Prop to the Constraint class for tablegen. Like `TypeConstraint` but unlike other constraints, a `PropConstraint` has an additional field - the C++ interface type of the property being constraint (if it's known).
1 parent 07ce57f commit a236dc6

File tree

6 files changed

+53
-28
lines changed

6 files changed

+53
-28
lines changed

mlir/docs/DeclarativeRewrites.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ The source pattern is for matching a DAG of operations. Arguments in the `dag`
109109
object are intended to **capture** the op arguments. They can also be used to
110110
**further limit** the match criteria. The capturing is done by specifying a
111111
symbol starting with the `$` sign, while further constraints are introduced by
112-
specifying a `TypeConstraint` (for an operand) or a `AttrConstraint` (for an
113-
attribute).
112+
specifying a `TypeConstraint` (for an operand), an `AttrConstraint` (for an
113+
attribute, or a `PropConstraint` for a property).
114114

115115
#### Binding op arguments and limiting the match
116116

mlir/docs/DefiningDialects/Operations.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ their semantics via a special [TableGen backend][TableGenBackend]:
104104
* The `Property` class hierarchy: They are used to specify non-attribute-backed
105105
properties that are inherent to operations. These properties can have
106106
constraints imposed on them using the `predicate` field or the
107-
`ConfinedProp` class.
107+
`ConfinedProp` class. The `PropConstraint` superclass of `Property` is used
108+
to describe constraints on properties in rewrite patterns.
108109

109110
An operation is defined by specializing the `Op` class with concrete contents
110111
for all the fields it requires. For example, `tf.AvgPool` is defined as
@@ -213,7 +214,7 @@ hierarchy. Similarly, `<attr-constraint>` is a TableGen `def` from the
213214
of `Property` (constraints can be imposed onto it using its `predicate` field
214215
or the `ConfinedProp` subclass).
215216

216-
There is no requirements on the relative order of operands and attributes; they
217+
There are no requirements on the relative order of operands and attributes; they
217218
can mix freely. The relative order of operands themselves matters. From each
218219
named argument a named getter will be generated that returns the argument with
219220
the return type (in the case of attributes the return type will be constructed

mlir/include/mlir/IR/Constraints.td

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ class TypeConstraint<Pred predicate, string summary = "",
169169
class AttrConstraint<Pred predicate, string summary = ""> :
170170
Constraint<predicate, summary>;
171171

172+
// Subclass for constraints on a property.
173+
class PropConstraint<Pred predicate, string summary = "", string interfaceTypeParam = ""> :
174+
Constraint<predicate, summary> {
175+
// The name of the C++ type of $_self, which will be the interface type of the
176+
// property being constrained, or "" if it is to be inferred from context.
177+
// Note, that leaving the interface type unspecified prevents the constraint from
178+
// being uniqued.
179+
code interfaceType = interfaceTypeParam;
180+
}
181+
172182
// Subclass for constraints on a region.
173183
class RegionConstraint<Pred predicate, string summary = ""> :
174184
Constraint<predicate, summary>;
@@ -183,10 +193,15 @@ class SuccessorConstraint<Pred predicate, string summary = ""> :
183193
// * Constraints on an op's operand/result definition
184194
// * Further constraints to match an op's operand/result in source pattern
185195
//
186-
// * Use Attr (a subclass for AttrConstraint) for
187-
// * Constraints on an op's attribute definition
196+
// * Use Attr (a subclass of AttrConstraint) for
197+
// * Constraints on an op's attribute definitions
188198
// * Use AttrConstraint to specify
189-
// * Further constraints to match an op's attribute in source pattern
199+
// * Further constraints to match an op's attribute in rewrite source patterns.
200+
//
201+
// * Use Prop (a subclass of PropConstraint) for
202+
// * Defining an op's properties or constraining them
203+
// * Use Prop constraint to specify
204+
// * Further constraints to match an op's property in rewrite source patterns.
190205
//
191206
// * Use uncategorized constraint to specify
192207
// * Multi-entity constraints in rewrite rules

mlir/include/mlir/IR/Properties.td

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@ include "mlir/IR/Constraints.td"
1717
include "mlir/IR/Utils.td"
1818

1919
// Base class for defining properties.
20-
class Property<string storageTypeParam = "", string desc = ""> {
21-
// User-readable one line summary used in error reporting messages. If empty,
22-
// a generic message will be used.
23-
string summary = desc;
20+
// `desc` is the summary, a user-readable one-line description of the property
21+
// used in error messages. If empty, a generic message will be used.
22+
// `storageTypeParam` is the type which will be used to store the property in
23+
// a property struct, while `interfaceTypeParam` (which defaults to `storageTypeParam`)
24+
// is the type returned from the getter of the property and used as an argument
25+
// to the property's builder.
26+
// Properties have a `predicate` field. It defaults to the true predicate since
27+
// properties always hold their C++ type. Within these predicates, $_self is the
28+
// **interface** type of the property.
29+
class Property<string storageTypeParam = "", string desc = "", string interfaceTypeParam = storageTypeParam>
30+
: PropConstraint<TruePred, desc, interfaceTypeParam> {
2431
// The full description of this property.
2532
string description = "";
2633
code storageType = storageTypeParam;
27-
code interfaceType = storageTypeParam;
34+
// Note: the interface type is a field on PropConstraint.
2835

2936
// The expression to convert from the storage type to the Interface
3037
// type. For example, an enum can be stored as an int but returned as an
@@ -66,13 +73,6 @@ class Property<string storageTypeParam = "", string desc = ""> {
6673
return convertFromAttribute($_storage, $_attr, $_diag);
6774
}];
6875

69-
// The verification predicate for this property. Defaults to the true predicate,
70-
// since properties are always their expected type.
71-
// Within the predicate, `$_self` is an instance of the **interface**
72-
// type of the property. Setting this field to ? will also result in a
73-
// true predicate but is not recommended, as it breaks composability.
74-
Pred predicate = TruePred;
75-
7676
// The call expression to hash the property.
7777
//
7878
// Format:
@@ -240,8 +240,7 @@ def I32Property : IntProp<"int32_t">, Deprecated<"moved to shorter name I32Prop"
240240
def I64Property : IntProp<"int64_t">, Deprecated<"moved to shorter name I64Prop">;
241241

242242
// Note: only a class so we can deprecate the old name
243-
class _cls_StringProp : Property<"std::string", "string"> {
244-
let interfaceType = "::llvm::StringRef";
243+
class _cls_StringProp : Property<"std::string", "string", "::llvm::StringRef"> {
245244
let convertFromStorage = "::llvm::StringRef{$_storage}";
246245
let assignToStorage = "$_storage = $_value.str()";
247246
let optionalParser = [{
@@ -347,7 +346,8 @@ def UnitProperty : _cls_UnitProp, Deprecated<"moved to shorter name UnitProp">;
347346
/// Class for giving a property a default value.
348347
/// This doesn't change anything about the property other than giving it a default
349348
/// which can be used by ODS to elide printing.
350-
class DefaultValuedProp<Property p, string default = "", string storageDefault = ""> : Property<p.storageType, p.summary> {
349+
class DefaultValuedProp<Property p, string default = "", string storageDefault = "">
350+
: Property<p.storageType, p.summary, p.interfaceType> {
351351
let defaultValue = default;
352352
let storageTypeValueOverride = storageDefault;
353353
let baseProperty = p;
@@ -375,7 +375,7 @@ class DefaultValuedProperty<Property p, string default = "", string storageDefau
375375
/// predicates it may already have. If `newSummary` is provided, replace the
376376
/// summary of `p` with `newSummary`.
377377
class ConfinedProp<Property p, Pred pred, string newSummary = "">
378-
: Property<p.storageType, !if(!empty(newSummary), p.summary, newSummary)> {
378+
: Property<p.storageType, !if(!empty(newSummary), p.summary, newSummary), p.interfaceType> {
379379
let predicate = !if(!ne(p.predicate, TruePred), And<[p.predicate, pred]>, pred);
380380
let baseProperty = p;
381381
// Keep up to date with `Property` above.
@@ -441,8 +441,8 @@ class _makeStorageWrapperPred<Property wrappedProp> {
441441
/// subclass `ArrayProp`.
442442
class ArrayProp<Property elem = Property<>, string newSummary = ""> :
443443
Property<"::llvm::SmallVector<" # elem.storageType # ">",
444-
!if(!empty(newSummary), "array of " # elem.summary, newSummary)> {
445-
let interfaceType = "::llvm::ArrayRef<" # elem.storageType # ">";
444+
!if(!empty(newSummary), "array of " # elem.summary, newSummary),
445+
"::llvm::ArrayRef<" # elem.storageType # ">"> {
446446
let convertFromStorage = "::llvm::ArrayRef<" # elem.storageType # ">{$_storage}";
447447
let assignToStorage = "$_storage.assign($_value.begin(), $_value.end())";
448448

@@ -585,7 +585,8 @@ class IntArrayProperty<Property elem, string newSummary="">
585585
/// syntax of the underlying property, or the keyword `none` in the rare cases that
586586
/// it is needed. This behavior can be disabled by setting `canDelegateParsing` to 0.
587587
class OptionalProp<Property p, bit canDelegateParsing = 1>
588-
: Property<"std::optional<" # p.storageType # ">", "optional " # p.summary> {
588+
: Property<"std::optional<" # p.storageType # ">",
589+
"optional " # p.summary, "std::optional<" # p.interfaceType # ">"> {
589590

590591
// In the cases where the underlying attribute is plain old data that's passed by
591592
// value, the conversion code is trivial.
@@ -596,7 +597,6 @@ class OptionalProp<Property p, bit canDelegateParsing = 1>
596597
defvar delegatesParsing = !and(!empty(p.defaultValue),
597598
!not(!empty(p.optionalParser)), canDelegateParsing);
598599

599-
let interfaceType = "std::optional<" # p.interfaceType # ">";
600600
let defaultValue = "std::nullopt";
601601

602602
let convertFromStorage = !if(hasTrivialStorage,

mlir/include/mlir/TableGen/Constraint.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ namespace tblgen {
3030
class Constraint {
3131
public:
3232
// Constraint kind
33-
enum Kind { CK_Attr, CK_Region, CK_Successor, CK_Type, CK_Uncategorized };
33+
enum Kind {
34+
CK_Attr,
35+
CK_Prop,
36+
CK_Region,
37+
CK_Successor,
38+
CK_Type,
39+
CK_Uncategorized
40+
};
3441

3542
// Create a constraint with a TableGen definition and a kind.
3643
Constraint(const llvm::Record *record, Kind kind) : def(record), kind(kind) {}

mlir/lib/TableGen/Constraint.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Constraint::Constraint(const llvm::Record *record)
2626
kind = CK_Type;
2727
} else if (def->isSubClassOf("AttrConstraint")) {
2828
kind = CK_Attr;
29+
} else if (def->isSubClassOf("PropConstraint")) {
30+
kind = CK_Prop;
2931
} else if (def->isSubClassOf("RegionConstraint")) {
3032
kind = CK_Region;
3133
} else if (def->isSubClassOf("SuccessorConstraint")) {

0 commit comments

Comments
 (0)