MLIR  18.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 
16 using namespace mlir;
17 using namespace mlir::tblgen;
18 
19 Constraint::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("RegionConstraint")) {
30  kind = CK_Region;
31  } else if (def->isSubClassOf("SuccessorConstraint")) {
32  kind = CK_Successor;
33  } else {
34  assert(def->isSubClassOf("Constraint"));
35  }
36 }
37 
39  auto *val = def->getValue("predicate");
40 
41  // If no predicate is specified, then return the null predicate (which
42  // corresponds to true).
43  if (!val)
44  return Pred();
45 
46  const auto *pred = dyn_cast<llvm::DefInit>(val->getValue());
47  return Pred(pred);
48 }
49 
50 std::string Constraint::getConditionTemplate() const {
51  return getPredicate().getCondition();
52 }
53 
54 StringRef Constraint::getSummary() const {
55  if (std::optional<StringRef> summary =
56  def->getValueAsOptionalString("summary"))
57  return *summary;
58  return def->getName();
59 }
60 
61 StringRef Constraint::getDescription() const {
62  return def->getValueAsOptionalString("description").value_or("");
63 }
64 
65 StringRef Constraint::getDefName() const {
66  if (std::optional<StringRef> baseDefName = getBaseDefName())
67  return *baseDefName;
68  return def->getName();
69 }
70 
71 std::string Constraint::getUniqueDefName() const {
72  std::string defName = def->getName().str();
73 
74  // Non-anonymous classes already have a unique name from the def.
75  if (!def->isAnonymous())
76  return defName;
77 
78  // Otherwise, this is an anonymous class. In these cases we still use the def
79  // name, but we also try attach the name of the base def when present to make
80  // the name more obvious.
81  if (std::optional<StringRef> baseDefName = getBaseDefName())
82  return (*baseDefName + "(" + defName + ")").str();
83  return defName;
84 }
85 
86 std::optional<StringRef> Constraint::getBaseDefName() const {
87  // Functor used to check a base def in the case where the current def is
88  // anonymous.
89  auto checkBaseDefFn = [&](StringRef baseName) -> std::optional<StringRef> {
90  if (const auto *defValue = def->getValue(baseName)) {
91  if (const auto *defInit = dyn_cast<llvm::DefInit>(defValue->getValue()))
92  return Constraint(defInit->getDef(), kind).getDefName();
93  }
94  return std::nullopt;
95  };
96 
97  switch (kind) {
98  case CK_Attr:
99  if (def->isAnonymous())
100  return checkBaseDefFn("baseAttr");
101  return std::nullopt;
102  case CK_Type:
103  if (def->isAnonymous())
104  return checkBaseDefFn("baseType");
105  return std::nullopt;
106  default:
107  return std::nullopt;
108  }
109 }
110 
112  llvm::StringRef self,
113  std::vector<std::string> &&entities)
114  : constraint(constraint), self(std::string(self)),
115  entities(std::move(entities)) {}
116 
118  return Constraint(RecordDenseMapInfo::getEmptyKey(),
120 }
121 
123  return Constraint(RecordDenseMapInfo::getTombstoneKey(),
125 }
126 
128  if (constraint == getEmptyKey())
129  return RecordDenseMapInfo::getHashValue(RecordDenseMapInfo::getEmptyKey());
130  if (constraint == getTombstoneKey()) {
131  return RecordDenseMapInfo::getHashValue(
132  RecordDenseMapInfo::getTombstoneKey());
133  }
134  return llvm::hash_combine(constraint.getPredicate(), constraint.getSummary());
135 }
136 
138  if (lhs == rhs)
139  return true;
140  if (lhs == getEmptyKey() || lhs == getTombstoneKey())
141  return false;
142  if (rhs == getEmptyKey() || rhs == getTombstoneKey())
143  return false;
144  return lhs.getPredicate() == rhs.getPredicate() &&
145  lhs.getSummary() == rhs.getSummary();
146 }
Pred getPredicate() const
Definition: Constraint.cpp:38
StringRef getSummary() const
Definition: Constraint.cpp:54
const llvm::Record * def
Definition: Constraint.h:79
std::string getUniqueDefName() const
Returns a unique name for the TablGen def of this constraint.
Definition: Constraint.cpp:71
StringRef getDescription() const
Definition: Constraint.cpp:61
StringRef getDefName() const
Returns the name of the TablGen def of this constraint.
Definition: Constraint.cpp:65
Constraint(const llvm::Record *record, Kind kind)
Definition: Constraint.h:36
std::string getConditionTemplate() const
Definition: Constraint.cpp:50
std::string getCondition() const
Definition: Predicate.cpp:36
This header declares functions that assist transformations in the MemRef dialect.
AppliedConstraint(Constraint &&constraint, StringRef self, std::vector< std::string > &&entities)
Definition: Constraint.cpp:111