MLIR 24.0.0git
Constraint.cpp
Go to the documentation of this file.
1//===- Constraint.cpp - Constraint class ----------------------------------===//
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
14#include "llvm/TableGen/Record.h"
15
16using namespace mlir;
17using namespace mlir::tblgen;
18
19Constraint::Constraint(const llvm::Record *record)
20 : Constraint(record, CK_Uncategorized) {
21 // Look through OpVariable's to their constraint.
22 if (def->isSubClassOf("OpVariable"))
23 def = def->getValueAsDef("constraint");
24
25 if (def->isSubClassOf("TypeConstraint")) {
26 kind = CK_Type;
27 } else if (def->isSubClassOf("AttrConstraint")) {
28 kind = CK_Attr;
29 } else if (def->isSubClassOf("PropConstraint")) {
30 kind = CK_Prop;
31 } else if (def->isSubClassOf("RegionConstraint")) {
32 kind = CK_Region;
33 } else if (def->isSubClassOf("SuccessorConstraint")) {
34 kind = CK_Successor;
35 } else if (!def->isSubClassOf("Constraint")) {
36 llvm::errs() << "Expected a constraint but got: \n" << *def << "\n";
37 llvm::report_fatal_error("Abort");
38 }
39}
40
42 auto *val = def->getValue("predicate");
43
44 // If no predicate is specified, then return the null predicate (which
45 // corresponds to true).
46 if (!val)
47 return Pred();
48
49 const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
50 return Pred(pred);
51}
52
54 return getPredicate().getCondition();
55}
56
57std::vector<StringRef> Constraint::getCppIncludes() const {
58 // Only type and attribute constraints have the field.
59 if (!def->getValue("cppIncludes"))
60 return {};
61 return def->getValueAsListOfStrings("cppIncludes");
62}
63
64StringRef Constraint::getSummary() const {
65 if (std::optional<StringRef> summary =
66 def->getValueAsOptionalString("summary"))
67 return *summary;
68 return def->getName();
69}
70
71StringRef Constraint::getDescription() const {
72 return def->getValueAsOptionalString("description").value_or("");
73}
74
75StringRef Constraint::getDefName() const {
76 if (std::optional<StringRef> baseDefName = getBaseDefName())
77 return *baseDefName;
78 return def->getName();
79}
80
81std::string Constraint::getUniqueDefName() const {
82 std::string defName = def->getName().str();
83
84 // Non-anonymous classes already have a unique name from the def.
85 if (!def->isAnonymous())
86 return defName;
87
88 // Otherwise, this is an anonymous class. In these cases we still use the def
89 // name, but we also try attach the name of the base def when present to make
90 // the name more obvious.
91 if (std::optional<StringRef> baseDefName = getBaseDefName())
92 return (*baseDefName + "(" + defName + ")").str();
93 return defName;
94}
95
96std::optional<StringRef> Constraint::getBaseDefName() const {
97 // Functor used to check a base def in the case where the current def is
98 // anonymous.
99 auto checkBaseDefFn = [&](StringRef baseName) -> std::optional<StringRef> {
100 if (const auto *defValue = def->getValue(baseName)) {
101 if (const auto *defInit = dyn_cast<llvm::DefInit>(defValue->getValue()))
102 return Constraint(defInit->getDef(), kind).getDefName();
103 }
104 return std::nullopt;
105 };
106
107 switch (kind) {
108 case CK_Attr:
109 if (def->isAnonymous())
110 return checkBaseDefFn("baseAttr");
111 return std::nullopt;
112 case CK_Type:
113 if (def->isAnonymous())
114 return checkBaseDefFn("baseType");
115 return std::nullopt;
116 default:
117 return std::nullopt;
118 }
119}
120
121std::optional<StringRef> Constraint::getCppFunctionName() const {
122 std::optional<StringRef> name =
123 def->getValueAsOptionalString("cppFunctionName");
124 if (!name || *name == "")
125 return std::nullopt;
126 return name;
127}
128
130 llvm::StringRef self,
131 std::vector<std::string> &&entities)
132 : constraint(constraint), self(std::string(self)),
133 entities(std::move(entities)) {}
134
136 return llvm::hash_combine(constraint.getPredicate(), constraint.getSummary());
137}
138
140 if (lhs == rhs)
141 return true;
142 return lhs.getPredicate() == rhs.getPredicate() &&
143 lhs.getSummary() == rhs.getSummary();
144}
if(failed(verifyVectorMemoryOp(getOperation(), memrefType, getVectorType()))) return failure()
StringRef getSummary() const
const llvm::Record * def
Definition Constraint.h:95
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
std::string getCondition() const
Definition Predicate.cpp:38
Include the generated interface declarations.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
Definition LLVM.h:119
std::vector< std::string > entities
Definition Constraint.h:115
AppliedConstraint(Constraint &&constraint, StringRef self, std::vector< std::string > &&entities)