MLIR  21.0.0git
Constraint.h
Go to the documentation of this file.
1 //===- Constraint.h - Constraint class --------------------------*- 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 // Constraint wrapper to simplify using TableGen Record for constraints.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_TABLEGEN_CONSTRAINT_H_
14 #define MLIR_TABLEGEN_CONSTRAINT_H_
15 
16 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace llvm {
22 class Record;
23 } // namespace llvm
24 
25 namespace mlir {
26 namespace tblgen {
27 
28 // Wrapper class with helper methods for accessing Constraint defined in
29 // TableGen.
30 class Constraint {
31 public:
32  // Constraint kind
33  enum Kind {
40  };
41 
42  // Create a constraint with a TableGen definition and a kind.
43  Constraint(const llvm::Record *record, Kind kind) : def(record), kind(kind) {}
44  // Create a constraint with a TableGen definition, and infer the kind.
45  Constraint(const llvm::Record *record);
46 
47  /// Constraints are pointer-comparable.
48  bool operator==(const Constraint &that) { return def == that.def; }
49  bool operator!=(const Constraint &that) { return def != that.def; }
50 
51  // Returns the predicate for this constraint.
52  Pred getPredicate() const;
53 
54  // Returns the condition template that can be used to check if a type or
55  // attribute satisfies this constraint. The template may contain "{0}" that
56  // must be substituted with an expression returning an mlir::Type or
57  // mlir::Attribute.
58  std::string getConditionTemplate() const;
59 
60  // Returns the user-readable summary of this constraint. If the summary is not
61  // provided, returns the TableGen def name.
62  StringRef getSummary() const;
63 
64  // Returns the long-form description of this constraint. If the description is
65  // not provided, returns an empty string.
66  StringRef getDescription() const;
67 
68  /// Returns the name of the TablGen def of this constraint. In some cases
69  /// where the current def is anonymous, the name of the base def is used (e.g.
70  /// `std::optional<>`/`Variadic<>` type constraints).
71  StringRef getDefName() const;
72 
73  /// Returns a unique name for the TablGen def of this constraint. This is
74  /// generally just the name of the def, but in some cases where the current
75  /// def is anonymous, the name of the base def is attached (to provide more
76  /// context on the def).
77  std::string getUniqueDefName() const;
78 
79  /// Returns the name of the C++ function that should be generated for this
80  /// constraint, or std::nullopt if no C++ function should be generated.
81  std::optional<StringRef> getCppFunctionName() const;
82 
83  Kind getKind() const { return kind; }
84 
85  /// Return the underlying def.
86  const llvm::Record &getDef() const { return *def; }
87 
88 protected:
89  // The TableGen definition of this constraint.
90  const llvm::Record *def;
91 
92 private:
93  /// Return the name of the base def if there is one, or std::nullopt
94  /// otherwise.
95  std::optional<StringRef> getBaseDefName() const;
96 
97  // What kind of constraint this is.
98  Kind kind;
99 };
100 
101 // An constraint and the concrete entities to place the constraint on.
103  AppliedConstraint(Constraint &&constraint, StringRef self,
104  std::vector<std::string> &&entities);
105 
107  // The symbol to replace `$_self` special placeholder in the constraint.
108  std::string self;
109  // The symbols to replace `$N` positional placeholders in the constraint.
110  std::vector<std::string> entities;
111 };
112 
113 } // namespace tblgen
114 } // namespace mlir
115 
116 namespace llvm {
117 /// Unique constraints by their predicate and summary. Constraints that share
118 /// the same predicate may have different descriptions; ensure that the
119 /// correct error message is reported when verification fails.
120 template <>
121 struct DenseMapInfo<mlir::tblgen::Constraint> {
123 
126  static unsigned getHashValue(mlir::tblgen::Constraint constraint);
129 };
130 } // namespace llvm
131 
132 #endif // MLIR_TABLEGEN_CONSTRAINT_H_
union mlir::linalg::@1203::ArityGroupAndKind::Kind kind
bool operator==(const Constraint &that)
Constraints are pointer-comparable.
Definition: Constraint.h:48
Pred getPredicate() const
Definition: Constraint.cpp:41
Kind getKind() const
Definition: Constraint.h:83
StringRef getSummary() const
Definition: Constraint.cpp:57
const llvm::Record * def
Definition: Constraint.h:90
const llvm::Record & getDef() const
Return the underlying def.
Definition: Constraint.h:86
bool operator!=(const Constraint &that)
Definition: Constraint.h:49
std::string getUniqueDefName() const
Returns a unique name for the TablGen def of this constraint.
Definition: Constraint.cpp:74
StringRef getDescription() const
Definition: Constraint.cpp:64
StringRef getDefName() const
Returns the name of the TablGen def of this constraint.
Definition: Constraint.cpp:68
std::optional< StringRef > getCppFunctionName() const
Returns the name of the C++ function that should be generated for this constraint,...
Definition: Constraint.cpp:114
Constraint(const llvm::Record *record, Kind kind)
Definition: Constraint.h:43
std::string getConditionTemplate() const
Definition: Constraint.cpp:53
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
Include the generated interface declarations.
static mlir::tblgen::Constraint getEmptyKey()
static mlir::tblgen::Constraint getTombstoneKey()
static bool isEqual(mlir::tblgen::Constraint lhs, mlir::tblgen::Constraint rhs)
static unsigned getHashValue(mlir::tblgen::Constraint constraint)
std::vector< std::string > entities
Definition: Constraint.h:110
AppliedConstraint(Constraint &&constraint, StringRef self, std::vector< std::string > &&entities)
Definition: Constraint.cpp:122