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