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" |
17 | #include "mlir/TableGen/Predicate.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 { CK_Attr, CK_Region, CK_Successor, CK_Type, CK_Uncategorized }; |
34 | |
35 | // Create a constraint with a TableGen definition and a kind. |
36 | Constraint(const llvm::Record *record, Kind kind) : def(record), kind(kind) {} |
37 | // Create a constraint with a TableGen definition, and infer the kind. |
38 | Constraint(const llvm::Record *record); |
39 | |
40 | /// Constraints are pointer-comparable. |
41 | bool operator==(const Constraint &that) { return def == that.def; } |
42 | bool operator!=(const Constraint &that) { return def != that.def; } |
43 | |
44 | // Returns the predicate for this constraint. |
45 | Pred getPredicate() const; |
46 | |
47 | // Returns the condition template that can be used to check if a type or |
48 | // attribute satisfies this constraint. The template may contain "{0}" that |
49 | // must be substituted with an expression returning an mlir::Type or |
50 | // mlir::Attribute. |
51 | std::string getConditionTemplate() const; |
52 | |
53 | // Returns the user-readable summary of this constraint. If the summary is not |
54 | // provided, returns the TableGen def name. |
55 | StringRef getSummary() const; |
56 | |
57 | // Returns the long-form description of this constraint. If the description is |
58 | // not provided, returns an empty string. |
59 | StringRef getDescription() const; |
60 | |
61 | /// Returns the name of the TablGen def of this constraint. In some cases |
62 | /// where the current def is anonymous, the name of the base def is used (e.g. |
63 | /// `std::optional<>`/`Variadic<>` type constraints). |
64 | StringRef getDefName() const; |
65 | |
66 | /// Returns a unique name for the TablGen def of this constraint. This is |
67 | /// generally just the name of the def, but in some cases where the current |
68 | /// def is anonymous, the name of the base def is attached (to provide more |
69 | /// context on the def). |
70 | std::string getUniqueDefName() const; |
71 | |
72 | Kind getKind() const { return kind; } |
73 | |
74 | /// Return the underlying def. |
75 | const llvm::Record &getDef() const { return *def; } |
76 | |
77 | protected: |
78 | // The TableGen definition of this constraint. |
79 | const llvm::Record *def; |
80 | |
81 | private: |
82 | /// Return the name of the base def if there is one, or std::nullopt |
83 | /// otherwise. |
84 | std::optional<StringRef> getBaseDefName() const; |
85 | |
86 | // What kind of constraint this is. |
87 | Kind kind; |
88 | }; |
89 | |
90 | // An constraint and the concrete entities to place the constraint on. |
91 | struct AppliedConstraint { |
92 | AppliedConstraint(Constraint &&constraint, StringRef self, |
93 | std::vector<std::string> &&entities); |
94 | |
95 | Constraint constraint; |
96 | // The symbol to replace `$_self` special placeholder in the constraint. |
97 | std::string self; |
98 | // The symbols to replace `$N` positional placeholders in the constraint. |
99 | std::vector<std::string> entities; |
100 | }; |
101 | |
102 | } // namespace tblgen |
103 | } // namespace mlir |
104 | |
105 | namespace llvm { |
106 | /// Unique constraints by their predicate and summary. Constraints that share |
107 | /// the same predicate may have different descriptions; ensure that the |
108 | /// correct error message is reported when verification fails. |
109 | template <> |
110 | struct DenseMapInfo<mlir::tblgen::Constraint> { |
111 | using RecordDenseMapInfo = llvm::DenseMapInfo<const llvm::Record *>; |
112 | |
113 | static mlir::tblgen::Constraint getEmptyKey(); |
114 | static mlir::tblgen::Constraint getTombstoneKey(); |
115 | static unsigned getHashValue(mlir::tblgen::Constraint constraint); |
116 | static bool isEqual(mlir::tblgen::Constraint lhs, |
117 | mlir::tblgen::Constraint rhs); |
118 | }; |
119 | } // namespace llvm |
120 | |
121 | #endif // MLIR_TABLEGEN_CONSTRAINT_H_ |
122 | |