MLIR  18.0.0git
Predicate.h
Go to the documentation of this file.
1 //===- Predicate.h - Pattern predicates -------------------------*- C++ -*-===//
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 contains definitions for "predicates" used when converting PDL into
10 // a matcher tree. Predicates are composed of three different parts:
11 //
12 // * Positions
13 // - A position refers to a specific location on the input DAG, i.e. an
14 // existing MLIR entity being matched. These can be attributes, operands,
15 // operations, results, and types. Each position also defines a relation to
16 // its parent. For example, the operand `[0] -> 1` has a parent operation
17 // position `[0]`. The attribute `[0, 1] -> "myAttr"` has parent operation
18 // position of `[0, 1]`. The operation `[0, 1]` has a parent operand edge
19 // `[0] -> 1` (i.e. it is the defining op of operand 1). The only position
20 // without a parent is `[0]`, which refers to the root operation.
21 // * Questions
22 // - A question refers to a query on a specific positional value. For
23 // example, an operation name question checks the name of an operation
24 // position.
25 // * Answers
26 // - An answer is the expected result of a question. For example, when
27 // matching an operation with the name "foo.op". The question would be an
28 // operation name question, with an expected answer of "foo.op".
29 //
30 //===----------------------------------------------------------------------===//
31 
32 #ifndef MLIR_LIB_CONVERSION_PDLTOPDLINTERP_PREDICATE_H_
33 #define MLIR_LIB_CONVERSION_PDLTOPDLINTERP_PREDICATE_H_
34 
35 #include "mlir/IR/MLIRContext.h"
37 #include "mlir/IR/PatternMatch.h"
38 #include "mlir/IR/Types.h"
39 
40 namespace mlir {
41 namespace pdl_to_pdl_interp {
42 namespace Predicates {
43 /// An enumeration of the kinds of predicates.
44 enum Kind : unsigned {
45  /// Positions, ordered by decreasing priority.
57 
58  // Questions, ordered by dependency and decreasing priority.
69 
70  // Answers.
77 };
78 } // namespace Predicates
79 
80 /// Base class for all predicates, used to allow efficient pointer comparison.
81 template <typename ConcreteT, typename BaseT, typename Key,
83 class PredicateBase : public BaseT {
84 public:
85  using KeyTy = Key;
87 
88  template <typename KeyT>
89  explicit PredicateBase(KeyT &&key)
90  : BaseT(Kind), key(std::forward<KeyT>(key)) {}
91 
92  /// Get an instance of this position.
93  template <typename... Args>
94  static ConcreteT *get(StorageUniquer &uniquer, Args &&...args) {
95  return uniquer.get<ConcreteT>(/*initFn=*/{}, std::forward<Args>(args)...);
96  }
97 
98  /// Construct an instance with the given storage allocator.
99  template <typename KeyT>
100  static ConcreteT *construct(StorageUniquer::StorageAllocator &alloc,
101  KeyT &&key) {
102  return new (alloc.allocate<ConcreteT>()) ConcreteT(std::forward<KeyT>(key));
103  }
104 
105  /// Utility methods required by the storage allocator.
106  bool operator==(const KeyTy &key) const { return this->key == key; }
107  static bool classof(const BaseT *pred) { return pred->getKind() == Kind; }
108 
109  /// Return the key value of this predicate.
110  const KeyTy &getValue() const { return key; }
111 
112 protected:
114 };
115 
116 /// Base storage for simple predicates that only unique with the kind.
117 template <typename ConcreteT, typename BaseT, Predicates::Kind Kind>
118 class PredicateBase<ConcreteT, BaseT, void, Kind> : public BaseT {
119 public:
121 
122  explicit PredicateBase() : BaseT(Kind) {}
123 
124  static ConcreteT *get(StorageUniquer &uniquer) {
125  return uniquer.get<ConcreteT>();
126  }
127  static bool classof(const BaseT *pred) { return pred->getKind() == Kind; }
128 };
129 
130 //===----------------------------------------------------------------------===//
131 // Positions
132 //===----------------------------------------------------------------------===//
133 
134 struct OperationPosition;
135 
136 /// A position describes a value on the input IR on which a predicate may be
137 /// applied, such as an operation or attribute. This enables re-use between
138 /// predicates, and assists generating bytecode and memory management.
139 ///
140 /// Operation positions form the base of other positions, which are formed
141 /// relative to a parent operation. Operations are anchored at Operand nodes,
142 /// except for the root operation which is parentless.
144 public:
145  explicit Position(Predicates::Kind kind) : kind(kind) {}
146  virtual ~Position();
147 
148  /// Returns the depth of the first ancestor operation position.
149  unsigned getOperationDepth() const;
150 
151  /// Returns the parent position. The root operation position has no parent.
152  Position *getParent() const { return parent; }
153 
154  /// Returns the kind of this position.
155  Predicates::Kind getKind() const { return kind; }
156 
157 protected:
158  /// Link to the parent position.
159  Position *parent = nullptr;
160 
161 private:
162  /// The kind of this position.
163  Predicates::Kind kind;
164 };
165 
166 //===----------------------------------------------------------------------===//
167 // AttributePosition
168 
169 /// A position describing an attribute of an operation.
171  : public PredicateBase<AttributePosition, Position,
172  std::pair<OperationPosition *, StringAttr>,
173  Predicates::AttributePos> {
174  explicit AttributePosition(const KeyTy &key);
175 
176  /// Returns the attribute name of this position.
177  StringAttr getName() const { return key.second; }
178 };
179 
180 //===----------------------------------------------------------------------===//
181 // AttributeLiteralPosition
182 
183 /// A position describing a literal attribute.
185  : public PredicateBase<AttributeLiteralPosition, Position, Attribute,
186  Predicates::AttributeLiteralPos> {
188 };
189 
190 //===----------------------------------------------------------------------===//
191 // ForEachPosition
192 
193 /// A position describing an iterative choice of an operation.
194 struct ForEachPosition : public PredicateBase<ForEachPosition, Position,
195  std::pair<Position *, unsigned>,
196  Predicates::ForEachPos> {
197  explicit ForEachPosition(const KeyTy &key) : Base(key) { parent = key.first; }
198 
199  /// Returns the ID, for differentiating various loops.
200  /// For upward traversals, this is the index of the root.
201  unsigned getID() const { return key.second; }
202 };
203 
204 //===----------------------------------------------------------------------===//
205 // OperandPosition
206 
207 /// A position describing an operand of an operation.
209  : public PredicateBase<OperandPosition, Position,
210  std::pair<OperationPosition *, unsigned>,
211  Predicates::OperandPos> {
212  explicit OperandPosition(const KeyTy &key);
213 
214  /// Returns the operand number of this position.
215  unsigned getOperandNumber() const { return key.second; }
216 };
217 
218 //===----------------------------------------------------------------------===//
219 // OperandGroupPosition
220 
221 /// A position describing an operand group of an operation.
223  : public PredicateBase<
224  OperandGroupPosition, Position,
225  std::tuple<OperationPosition *, std::optional<unsigned>, bool>,
226  Predicates::OperandGroupPos> {
227  explicit OperandGroupPosition(const KeyTy &key);
228 
229  /// Returns a hash suitable for the given keytype.
230  static llvm::hash_code hashKey(const KeyTy &key) {
231  return llvm::hash_value(key);
232  }
233 
234  /// Returns the group number of this position. If std::nullopt, this group
235  /// refers to all operands.
236  std::optional<unsigned> getOperandGroupNumber() const {
237  return std::get<1>(key);
238  }
239 
240  /// Returns if the operand group has unknown size. If false, the operand group
241  /// has at max one element.
242  bool isVariadic() const { return std::get<2>(key); }
243 };
244 
245 //===----------------------------------------------------------------------===//
246 // OperationPosition
247 
248 /// An operation position describes an operation node in the IR. Other position
249 /// kinds are formed with respect to an operation position.
250 struct OperationPosition : public PredicateBase<OperationPosition, Position,
251  std::pair<Position *, unsigned>,
252  Predicates::OperationPos> {
253  explicit OperationPosition(const KeyTy &key) : Base(key) {
254  parent = key.first;
255  }
256 
257  /// Returns a hash suitable for the given keytype.
258  static llvm::hash_code hashKey(const KeyTy &key) {
259  return llvm::hash_value(key);
260  }
261 
262  /// Gets the root position.
264  return Base::get(uniquer, nullptr, 0);
265  }
266 
267  /// Gets an operation position with the given parent.
269  return Base::get(uniquer, parent, parent->getOperationDepth() + 1);
270  }
271 
272  /// Returns the depth of this position.
273  unsigned getDepth() const { return key.second; }
274 
275  /// Returns if this operation position corresponds to the root.
276  bool isRoot() const { return getDepth() == 0; }
277 
278  /// Returns if this operation represents an operand defining op.
279  bool isOperandDefiningOp() const;
280 };
281 
282 //===----------------------------------------------------------------------===//
283 // ResultPosition
284 
285 /// A position describing a result of an operation.
287  : public PredicateBase<ResultPosition, Position,
288  std::pair<OperationPosition *, unsigned>,
289  Predicates::ResultPos> {
290  explicit ResultPosition(const KeyTy &key) : Base(key) { parent = key.first; }
291 
292  /// Returns the result number of this position.
293  unsigned getResultNumber() const { return key.second; }
294 };
295 
296 //===----------------------------------------------------------------------===//
297 // ResultGroupPosition
298 
299 /// A position describing a result group of an operation.
301  : public PredicateBase<
302  ResultGroupPosition, Position,
303  std::tuple<OperationPosition *, std::optional<unsigned>, bool>,
304  Predicates::ResultGroupPos> {
305  explicit ResultGroupPosition(const KeyTy &key) : Base(key) {
306  parent = std::get<0>(key);
307  }
308 
309  /// Returns a hash suitable for the given keytype.
310  static llvm::hash_code hashKey(const KeyTy &key) {
311  return llvm::hash_value(key);
312  }
313 
314  /// Returns the group number of this position. If std::nullopt, this group
315  /// refers to all results.
316  std::optional<unsigned> getResultGroupNumber() const {
317  return std::get<1>(key);
318  }
319 
320  /// Returns if the result group has unknown size. If false, the result group
321  /// has at max one element.
322  bool isVariadic() const { return std::get<2>(key); }
323 };
324 
325 //===----------------------------------------------------------------------===//
326 // TypePosition
327 
328 /// A position describing the result type of an entity, i.e. an Attribute,
329 /// Operand, Result, etc.
330 struct TypePosition : public PredicateBase<TypePosition, Position, Position *,
331  Predicates::TypePos> {
332  explicit TypePosition(const KeyTy &key) : Base(key) {
335  "expected parent to be an attribute, operand, or result");
336  parent = key;
337  }
338 };
339 
340 //===----------------------------------------------------------------------===//
341 // TypeLiteralPosition
342 
343 /// A position describing a literal type or type range. The value is stored as
344 /// either a TypeAttr, or an ArrayAttr of TypeAttr.
346  : public PredicateBase<TypeLiteralPosition, Position, Attribute,
347  Predicates::TypeLiteralPos> {
349 };
350 
351 //===----------------------------------------------------------------------===//
352 // UsersPosition
353 
354 /// A position describing the users of a value or a range of values. The second
355 /// value in the key indicates whether we choose users of a representative for
356 /// a range (this is true, e.g., in the upward traversals).
358  : public PredicateBase<UsersPosition, Position, std::pair<Position *, bool>,
359  Predicates::UsersPos> {
360  explicit UsersPosition(const KeyTy &key) : Base(key) { parent = key.first; }
361 
362  /// Returns a hash suitable for the given keytype.
363  static llvm::hash_code hashKey(const KeyTy &key) {
364  return llvm::hash_value(key);
365  }
366 
367  /// Indicates whether to compute a range of a representative.
368  bool useRepresentative() const { return key.second; }
369 };
370 
371 //===----------------------------------------------------------------------===//
372 // Qualifiers
373 //===----------------------------------------------------------------------===//
374 
375 /// An ordinal predicate consists of a "Question" and a set of acceptable
376 /// "Answers" (later converted to ordinal values). A predicate will query some
377 /// property of a positional value and decide what to do based on the result.
378 ///
379 /// This makes top-level predicate representations ordinal (SwitchOp). Later,
380 /// predicates that end up with only one acceptable answer (including all
381 /// boolean kinds) will be converted to boolean predicates (PredicateOp) in the
382 /// matcher.
383 ///
384 /// For simplicity, both are represented as "qualifiers", with a base kind and
385 /// perhaps additional properties. For example, all OperationName predicates ask
386 /// the same question, but GenericConstraint predicates may ask different ones.
388 public:
389  explicit Qualifier(Predicates::Kind kind) : kind(kind) {}
390 
391  /// Returns the kind of this qualifier.
392  Predicates::Kind getKind() const { return kind; }
393 
394 private:
395  /// The kind of this position.
396  Predicates::Kind kind;
397 };
398 
399 //===----------------------------------------------------------------------===//
400 // Answers
401 
402 /// An Answer representing an `Attribute` value.
404  : public PredicateBase<AttributeAnswer, Qualifier, Attribute,
405  Predicates::AttributeAnswer> {
406  using Base::Base;
407 };
408 
409 /// An Answer representing an `OperationName` value.
411  : public PredicateBase<OperationNameAnswer, Qualifier, OperationName,
412  Predicates::OperationNameAnswer> {
413  using Base::Base;
414 };
415 
416 /// An Answer representing a boolean `true` value.
418  : PredicateBase<TrueAnswer, Qualifier, void, Predicates::TrueAnswer> {
419  using Base::Base;
420 };
421 
422 /// An Answer representing a boolean 'false' value.
424  : PredicateBase<FalseAnswer, Qualifier, void, Predicates::FalseAnswer> {
425  using Base::Base;
426 };
427 
428 /// An Answer representing a `Type` value. The value is stored as either a
429 /// TypeAttr, or an ArrayAttr of TypeAttr.
430 struct TypeAnswer : public PredicateBase<TypeAnswer, Qualifier, Attribute,
431  Predicates::TypeAnswer> {
432  using Base::Base;
433 };
434 
435 /// An Answer representing an unsigned value.
437  : public PredicateBase<UnsignedAnswer, Qualifier, unsigned,
438  Predicates::UnsignedAnswer> {
439  using Base::Base;
440 };
441 
442 //===----------------------------------------------------------------------===//
443 // Questions
444 
445 /// Compare an `Attribute` to a constant value.
447  : public PredicateBase<AttributeQuestion, Qualifier, void,
448  Predicates::AttributeQuestion> {};
449 
450 /// Apply a parameterized constraint to multiple position values.
452  : public PredicateBase<ConstraintQuestion, Qualifier,
453  std::tuple<StringRef, ArrayRef<Position *>, bool>,
454  Predicates::ConstraintQuestion> {
455  using Base::Base;
456 
457  /// Return the name of the constraint.
458  StringRef getName() const { return std::get<0>(key); }
459 
460  /// Return the arguments of the constraint.
461  ArrayRef<Position *> getArgs() const { return std::get<1>(key); }
462 
463  /// Return the negation status of the constraint.
464  bool getIsNegated() const { return std::get<2>(key); }
465 
466  /// Construct an instance with the given storage allocator.
468  KeyTy key) {
469  return Base::construct(alloc, KeyTy{alloc.copyInto(std::get<0>(key)),
470  alloc.copyInto(std::get<1>(key)),
471  std::get<2>(key)});
472  }
473 
474  /// Returns a hash suitable for the given keytype.
475  static llvm::hash_code hashKey(const KeyTy &key) {
476  return llvm::hash_value(key);
477  }
478 };
479 
480 /// Compare the equality of two values.
482  : public PredicateBase<EqualToQuestion, Qualifier, Position *,
483  Predicates::EqualToQuestion> {
484  using Base::Base;
485 };
486 
487 /// Compare a positional value with null, i.e. check if it exists.
489  : public PredicateBase<IsNotNullQuestion, Qualifier, void,
490  Predicates::IsNotNullQuestion> {};
491 
492 /// Compare the number of operands of an operation with a known value.
494  : public PredicateBase<OperandCountQuestion, Qualifier, void,
495  Predicates::OperandCountQuestion> {};
497  : public PredicateBase<OperandCountAtLeastQuestion, Qualifier, void,
498  Predicates::OperandCountAtLeastQuestion> {};
499 
500 /// Compare the name of an operation with a known value.
502  : public PredicateBase<OperationNameQuestion, Qualifier, void,
503  Predicates::OperationNameQuestion> {};
504 
505 /// Compare the number of results of an operation with a known value.
507  : public PredicateBase<ResultCountQuestion, Qualifier, void,
508  Predicates::ResultCountQuestion> {};
510  : public PredicateBase<ResultCountAtLeastQuestion, Qualifier, void,
511  Predicates::ResultCountAtLeastQuestion> {};
512 
513 /// Compare the type of an attribute or value with a known type.
514 struct TypeQuestion : public PredicateBase<TypeQuestion, Qualifier, void,
515  Predicates::TypeQuestion> {};
516 
517 //===----------------------------------------------------------------------===//
518 // PredicateUniquer
519 //===----------------------------------------------------------------------===//
520 
521 /// This class provides a storage uniquer that is used to allocate predicate
522 /// instances.
524 public:
526  // Register the types of Positions with the uniquer.
527  registerParametricStorageType<AttributePosition>();
528  registerParametricStorageType<AttributeLiteralPosition>();
529  registerParametricStorageType<ForEachPosition>();
530  registerParametricStorageType<OperandPosition>();
531  registerParametricStorageType<OperandGroupPosition>();
532  registerParametricStorageType<OperationPosition>();
533  registerParametricStorageType<ResultPosition>();
534  registerParametricStorageType<ResultGroupPosition>();
535  registerParametricStorageType<TypePosition>();
536  registerParametricStorageType<TypeLiteralPosition>();
537  registerParametricStorageType<UsersPosition>();
538 
539  // Register the types of Questions with the uniquer.
540  registerParametricStorageType<AttributeAnswer>();
541  registerParametricStorageType<OperationNameAnswer>();
542  registerParametricStorageType<TypeAnswer>();
543  registerParametricStorageType<UnsignedAnswer>();
544  registerSingletonStorageType<FalseAnswer>();
545  registerSingletonStorageType<TrueAnswer>();
546 
547  // Register the types of Answers with the uniquer.
548  registerParametricStorageType<ConstraintQuestion>();
549  registerParametricStorageType<EqualToQuestion>();
550  registerSingletonStorageType<AttributeQuestion>();
551  registerSingletonStorageType<IsNotNullQuestion>();
552  registerSingletonStorageType<OperandCountQuestion>();
553  registerSingletonStorageType<OperandCountAtLeastQuestion>();
554  registerSingletonStorageType<OperationNameQuestion>();
555  registerSingletonStorageType<ResultCountQuestion>();
556  registerSingletonStorageType<ResultCountAtLeastQuestion>();
557  registerSingletonStorageType<TypeQuestion>();
558  }
559 };
560 
561 //===----------------------------------------------------------------------===//
562 // PredicateBuilder
563 //===----------------------------------------------------------------------===//
564 
565 /// This class provides utilities for constructing predicates.
567 public:
569  : uniquer(uniquer), ctx(ctx) {}
570 
571  //===--------------------------------------------------------------------===//
572  // Positions
573  //===--------------------------------------------------------------------===//
574 
575  /// Returns the root operation position.
577 
578  /// Returns the parent position defining the value held by the given operand.
580  assert((isa<OperandPosition, OperandGroupPosition>(p)) &&
581  "expected operand position");
582  return OperationPosition::get(uniquer, p);
583  }
584 
585  /// Returns the operation position equivalent to the given position.
587  assert((isa<ForEachPosition>(p)) && "expected users position");
588  return OperationPosition::get(uniquer, p);
589  }
590 
591  /// Returns an attribute position for an attribute of the given operation.
592  Position *getAttribute(OperationPosition *p, StringRef name) {
593  return AttributePosition::get(uniquer, p, StringAttr::get(ctx, name));
594  }
595 
596  /// Returns an attribute position for the given attribute.
598  return AttributeLiteralPosition::get(uniquer, attr);
599  }
600 
601  Position *getForEach(Position *p, unsigned id) {
602  return ForEachPosition::get(uniquer, p, id);
603  }
604 
605  /// Returns an operand position for an operand of the given operation.
606  Position *getOperand(OperationPosition *p, unsigned operand) {
607  return OperandPosition::get(uniquer, p, operand);
608  }
609 
610  /// Returns a position for a group of operands of the given operation.
611  Position *getOperandGroup(OperationPosition *p, std::optional<unsigned> group,
612  bool isVariadic) {
613  return OperandGroupPosition::get(uniquer, p, group, isVariadic);
614  }
616  return getOperandGroup(p, /*group=*/std::nullopt, /*isVariadic=*/true);
617  }
618 
619  /// Returns a result position for a result of the given operation.
620  Position *getResult(OperationPosition *p, unsigned result) {
621  return ResultPosition::get(uniquer, p, result);
622  }
623 
624  /// Returns a position for a group of results of the given operation.
625  Position *getResultGroup(OperationPosition *p, std::optional<unsigned> group,
626  bool isVariadic) {
627  return ResultGroupPosition::get(uniquer, p, group, isVariadic);
628  }
630  return getResultGroup(p, /*group=*/std::nullopt, /*isVariadic=*/true);
631  }
632 
633  /// Returns a type position for the given entity.
634  Position *getType(Position *p) { return TypePosition::get(uniquer, p); }
635 
636  /// Returns a type position for the given type value. The value is stored
637  /// as either a TypeAttr, or an ArrayAttr of TypeAttr.
639  return TypeLiteralPosition::get(uniquer, attr);
640  }
641 
642  /// Returns the users of a position using the value at the given operand.
643  UsersPosition *getUsers(Position *p, bool useRepresentative) {
645  ResultGroupPosition>(p)) &&
646  "expected result position");
647  return UsersPosition::get(uniquer, p, useRepresentative);
648  }
649 
650  //===--------------------------------------------------------------------===//
651  // Qualifiers
652  //===--------------------------------------------------------------------===//
653 
654  /// An ordinal predicate consists of a "Question" and a set of acceptable
655  /// "Answers" (later converted to ordinal values). A predicate will query some
656  /// property of a positional value and decide what to do based on the result.
657  using Predicate = std::pair<Qualifier *, Qualifier *>;
658 
659  /// Create a predicate comparing an attribute to a known value.
661  return {AttributeQuestion::get(uniquer),
662  AttributeAnswer::get(uniquer, attr)};
663  }
664 
665  /// Create a predicate checking if two values are equal.
667  return {EqualToQuestion::get(uniquer, pos), TrueAnswer::get(uniquer)};
668  }
669 
670  /// Create a predicate checking if two values are not equal.
672  return {EqualToQuestion::get(uniquer, pos), FalseAnswer::get(uniquer)};
673  }
674 
675  /// Create a predicate that applies a generic constraint.
677  bool isNegated) {
678  return {
679  ConstraintQuestion::get(uniquer, std::make_tuple(name, pos, isNegated)),
680  TrueAnswer::get(uniquer)};
681  }
682 
683  /// Create a predicate comparing a value with null.
685  return {IsNotNullQuestion::get(uniquer), TrueAnswer::get(uniquer)};
686  }
687 
688  /// Create a predicate comparing the number of operands of an operation to a
689  /// known value.
690  Predicate getOperandCount(unsigned count) {
691  return {OperandCountQuestion::get(uniquer),
692  UnsignedAnswer::get(uniquer, count)};
693  }
695  return {OperandCountAtLeastQuestion::get(uniquer),
696  UnsignedAnswer::get(uniquer, count)};
697  }
698 
699  /// Create a predicate comparing the name of an operation to a known value.
700  Predicate getOperationName(StringRef name) {
701  return {OperationNameQuestion::get(uniquer),
702  OperationNameAnswer::get(uniquer, OperationName(name, ctx))};
703  }
704 
705  /// Create a predicate comparing the number of results of an operation to a
706  /// known value.
707  Predicate getResultCount(unsigned count) {
708  return {ResultCountQuestion::get(uniquer),
709  UnsignedAnswer::get(uniquer, count)};
710  }
712  return {ResultCountAtLeastQuestion::get(uniquer),
713  UnsignedAnswer::get(uniquer, count)};
714  }
715 
716  /// Create a predicate comparing the type of an attribute or value to a known
717  /// type. The value is stored as either a TypeAttr, or an ArrayAttr of
718  /// TypeAttr.
720  return {TypeQuestion::get(uniquer), TypeAnswer::get(uniquer, type)};
721  }
722 
723 private:
724  /// The uniquer used when allocating predicate nodes.
725  PredicateUniquer &uniquer;
726 
727  /// The current MLIR context.
728  MLIRContext *ctx;
729 };
730 
731 } // namespace pdl_to_pdl_interp
732 } // namespace mlir
733 
734 #endif // MLIR_CONVERSION_PDLTOPDLINTERP_PREDICATE_H_
Attributes are known-constant values of operations.
Definition: Attributes.h:25
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class acts as the base storage that all storage classes must derived from.
This is a utility allocator used to allocate memory for instances of derived types.
ArrayRef< T > copyInto(ArrayRef< T > elements)
Copy the specified array of elements into memory managed by our bump pointer allocator.
T * allocate()
Allocate an instance of the provided type.
A utility class to get or create instances of "storage classes".
Storage * get(function_ref< void(Storage *)> initFn, TypeID id, Args &&...args)
Gets a uniqued instance of 'Storage'.
A position describes a value on the input IR on which a predicate may be applied, such as an operatio...
Definition: Predicate.h:143
Position(Predicates::Kind kind)
Definition: Predicate.h:145
unsigned getOperationDepth() const
Returns the depth of the first ancestor operation position.
Definition: Predicate.cpp:21
Position * getParent() const
Returns the parent position. The root operation position has no parent.
Definition: Predicate.h:152
Predicates::Kind getKind() const
Returns the kind of this position.
Definition: Predicate.h:155
Position * parent
Link to the parent position.
Definition: Predicate.h:159
Base storage for simple predicates that only unique with the kind.
Definition: Predicate.h:118
Base class for all predicates, used to allow efficient pointer comparison.
Definition: Predicate.h:83
bool operator==(const KeyTy &key) const
Utility methods required by the storage allocator.
Definition: Predicate.h:106
static ConcreteT * construct(StorageUniquer::StorageAllocator &alloc, KeyT &&key)
Construct an instance with the given storage allocator.
Definition: Predicate.h:100
static ConcreteT * get(StorageUniquer &uniquer, Args &&...args)
Get an instance of this position.
Definition: Predicate.h:94
static bool classof(const BaseT *pred)
Definition: Predicate.h:107
const KeyTy & getValue() const
Return the key value of this predicate.
Definition: Predicate.h:110
PredicateBase< ConcreteT, BaseT, Key, Kind > Base
Definition: Predicate.h:86
This class provides utilities for constructing predicates.
Definition: Predicate.h:566
Position * getTypeLiteral(Attribute attr)
Returns a type position for the given type value.
Definition: Predicate.h:638
Predicate getOperandCount(unsigned count)
Create a predicate comparing the number of operands of an operation to a known value.
Definition: Predicate.h:690
OperationPosition * getPassthroughOp(Position *p)
Returns the operation position equivalent to the given position.
Definition: Predicate.h:586
Predicate getIsNotNull()
Create a predicate comparing a value with null.
Definition: Predicate.h:684
Predicate getOperandCountAtLeast(unsigned count)
Definition: Predicate.h:694
Predicate getResultCountAtLeast(unsigned count)
Definition: Predicate.h:711
Position * getType(Position *p)
Returns a type position for the given entity.
Definition: Predicate.h:634
Position * getAttribute(OperationPosition *p, StringRef name)
Returns an attribute position for an attribute of the given operation.
Definition: Predicate.h:592
Position * getOperandGroup(OperationPosition *p, std::optional< unsigned > group, bool isVariadic)
Returns a position for a group of operands of the given operation.
Definition: Predicate.h:611
Position * getForEach(Position *p, unsigned id)
Definition: Predicate.h:601
Position * getOperand(OperationPosition *p, unsigned operand)
Returns an operand position for an operand of the given operation.
Definition: Predicate.h:606
Position * getResult(OperationPosition *p, unsigned result)
Returns a result position for a result of the given operation.
Definition: Predicate.h:620
Position * getRoot()
Returns the root operation position.
Definition: Predicate.h:576
Predicate getAttributeConstraint(Attribute attr)
Create a predicate comparing an attribute to a known value.
Definition: Predicate.h:660
Position * getResultGroup(OperationPosition *p, std::optional< unsigned > group, bool isVariadic)
Returns a position for a group of results of the given operation.
Definition: Predicate.h:625
Position * getAllResults(OperationPosition *p)
Definition: Predicate.h:629
UsersPosition * getUsers(Position *p, bool useRepresentative)
Returns the users of a position using the value at the given operand.
Definition: Predicate.h:643
Predicate getTypeConstraint(Attribute type)
Create a predicate comparing the type of an attribute or value to a known type.
Definition: Predicate.h:719
OperationPosition * getOperandDefiningOp(Position *p)
Returns the parent position defining the value held by the given operand.
Definition: Predicate.h:579
Predicate getResultCount(unsigned count)
Create a predicate comparing the number of results of an operation to a known value.
Definition: Predicate.h:707
std::pair< Qualifier *, Qualifier * > Predicate
An ordinal predicate consists of a "Question" and a set of acceptable "Answers" (later converted to o...
Definition: Predicate.h:657
Predicate getEqualTo(Position *pos)
Create a predicate checking if two values are equal.
Definition: Predicate.h:666
Position * getAllOperands(OperationPosition *p)
Definition: Predicate.h:615
PredicateBuilder(PredicateUniquer &uniquer, MLIRContext *ctx)
Definition: Predicate.h:568
Position * getAttributeLiteral(Attribute attr)
Returns an attribute position for the given attribute.
Definition: Predicate.h:597
Predicate getNotEqualTo(Position *pos)
Create a predicate checking if two values are not equal.
Definition: Predicate.h:671
Predicate getOperationName(StringRef name)
Create a predicate comparing the name of an operation to a known value.
Definition: Predicate.h:700
Predicate getConstraint(StringRef name, ArrayRef< Position * > pos, bool isNegated)
Create a predicate that applies a generic constraint.
Definition: Predicate.h:676
This class provides a storage uniquer that is used to allocate predicate instances.
Definition: Predicate.h:523
An ordinal predicate consists of a "Question" and a set of acceptable "Answers" (later converted to o...
Definition: Predicate.h:387
Predicates::Kind getKind() const
Returns the kind of this qualifier.
Definition: Predicate.h:392
Qualifier(Predicates::Kind kind)
Definition: Predicate.h:389
Kind
An enumeration of the kinds of predicates.
Definition: Predicate.h:44
@ OperationPos
Positions, ordered by decreasing priority.
Definition: Predicate.h:46
llvm::hash_code hash_value(const MPInt &x)
Redeclarations of friend declaration above to make it discoverable by lookups.
Definition: MPInt.cpp:15
This header declares functions that assist transformations in the MemRef dialect.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
An Answer representing an Attribute value.
Definition: Predicate.h:405
A position describing a literal attribute.
Definition: Predicate.h:186
A position describing an attribute of an operation.
Definition: Predicate.h:173
StringAttr getName() const
Returns the attribute name of this position.
Definition: Predicate.h:177
Compare an Attribute to a constant value.
Definition: Predicate.h:448
Apply a parameterized constraint to multiple position values.
Definition: Predicate.h:454
StringRef getName() const
Return the name of the constraint.
Definition: Predicate.h:458
ArrayRef< Position * > getArgs() const
Return the arguments of the constraint.
Definition: Predicate.h:461
static ConstraintQuestion * construct(StorageUniquer::StorageAllocator &alloc, KeyTy key)
Construct an instance with the given storage allocator.
Definition: Predicate.h:467
static llvm::hash_code hashKey(const KeyTy &key)
Returns a hash suitable for the given keytype.
Definition: Predicate.h:475
bool getIsNegated() const
Return the negation status of the constraint.
Definition: Predicate.h:464
Compare the equality of two values.
Definition: Predicate.h:483
An Answer representing a boolean 'false' value.
Definition: Predicate.h:424
A position describing an iterative choice of an operation.
Definition: Predicate.h:196
unsigned getID() const
Returns the ID, for differentiating various loops.
Definition: Predicate.h:201
Compare a positional value with null, i.e. check if it exists.
Definition: Predicate.h:490
Compare the number of operands of an operation with a known value.
Definition: Predicate.h:495
A position describing an operand group of an operation.
Definition: Predicate.h:226
bool isVariadic() const
Returns if the operand group has unknown size.
Definition: Predicate.h:242
std::optional< unsigned > getOperandGroupNumber() const
Returns the group number of this position.
Definition: Predicate.h:236
static llvm::hash_code hashKey(const KeyTy &key)
Returns a hash suitable for the given keytype.
Definition: Predicate.h:230
A position describing an operand of an operation.
Definition: Predicate.h:211
unsigned getOperandNumber() const
Returns the operand number of this position.
Definition: Predicate.h:215
An Answer representing an OperationName value.
Definition: Predicate.h:412
Compare the name of an operation with a known value.
Definition: Predicate.h:503
An operation position describes an operation node in the IR.
Definition: Predicate.h:252
static OperationPosition * getRoot(StorageUniquer &uniquer)
Gets the root position.
Definition: Predicate.h:263
bool isRoot() const
Returns if this operation position corresponds to the root.
Definition: Predicate.h:276
unsigned getDepth() const
Returns the depth of this position.
Definition: Predicate.h:273
static llvm::hash_code hashKey(const KeyTy &key)
Returns a hash suitable for the given keytype.
Definition: Predicate.h:258
bool isOperandDefiningOp() const
Returns if this operation represents an operand defining op.
Definition: Predicate.cpp:51
static OperationPosition * get(StorageUniquer &uniquer, Position *parent)
Gets an operation position with the given parent.
Definition: Predicate.h:268
Compare the number of results of an operation with a known value.
Definition: Predicate.h:508
A position describing a result group of an operation.
Definition: Predicate.h:304
bool isVariadic() const
Returns if the result group has unknown size.
Definition: Predicate.h:322
std::optional< unsigned > getResultGroupNumber() const
Returns the group number of this position.
Definition: Predicate.h:316
static llvm::hash_code hashKey(const KeyTy &key)
Returns a hash suitable for the given keytype.
Definition: Predicate.h:310
A position describing a result of an operation.
Definition: Predicate.h:289
unsigned getResultNumber() const
Returns the result number of this position.
Definition: Predicate.h:293
An Answer representing a boolean true value.
Definition: Predicate.h:418
An Answer representing a Type value.
Definition: Predicate.h:431
A position describing a literal type or type range.
Definition: Predicate.h:347
A position describing the result type of an entity, i.e.
Definition: Predicate.h:331
Compare the type of an attribute or value with a known type.
Definition: Predicate.h:515
An Answer representing an unsigned value.
Definition: Predicate.h:438
A position describing the users of a value or a range of values.
Definition: Predicate.h:359
bool useRepresentative() const
Indicates whether to compute a range of a representative.
Definition: Predicate.h:368
static llvm::hash_code hashKey(const KeyTy &key)
Returns a hash suitable for the given keytype.
Definition: Predicate.h:363