| 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 | |
| 13 | #include "mlir/TableGen/Constraint.h" |
| 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(Name: "OpVariable" )) |
| 23 | def = def->getValueAsDef(FieldName: "constraint" ); |
| 24 | |
| 25 | if (def->isSubClassOf(Name: "TypeConstraint" )) { |
| 26 | kind = CK_Type; |
| 27 | } else if (def->isSubClassOf(Name: "AttrConstraint" )) { |
| 28 | kind = CK_Attr; |
| 29 | } else if (def->isSubClassOf(Name: "PropConstraint" )) { |
| 30 | kind = CK_Prop; |
| 31 | } else if (def->isSubClassOf(Name: "RegionConstraint" )) { |
| 32 | kind = CK_Region; |
| 33 | } else if (def->isSubClassOf(Name: "SuccessorConstraint" )) { |
| 34 | kind = CK_Successor; |
| 35 | } else if (!def->isSubClassOf(Name: "Constraint" )) { |
| 36 | llvm::errs() << "Expected a constraint but got: \n" << *def << "\n" ; |
| 37 | llvm::report_fatal_error(reason: "Abort" ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | Pred Constraint::getPredicate() const { |
| 42 | auto *val = def->getValue(Name: "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: val->getValue()); |
| 50 | return Pred(pred); |
| 51 | } |
| 52 | |
| 53 | std::string Constraint::getConditionTemplate() const { |
| 54 | return getPredicate().getCondition(); |
| 55 | } |
| 56 | |
| 57 | std::vector<StringRef> Constraint::getCppIncludes() const { |
| 58 | // Only type and attribute constraints have the field. |
| 59 | if (!def->getValue(Name: "cppIncludes" )) |
| 60 | return {}; |
| 61 | return def->getValueAsListOfStrings(FieldName: "cppIncludes" ); |
| 62 | } |
| 63 | |
| 64 | StringRef Constraint::getSummary() const { |
| 65 | if (std::optional<StringRef> summary = |
| 66 | def->getValueAsOptionalString(FieldName: "summary" )) |
| 67 | return *summary; |
| 68 | return def->getName(); |
| 69 | } |
| 70 | |
| 71 | StringRef Constraint::getDescription() const { |
| 72 | return def->getValueAsOptionalString(FieldName: "description" ).value_or(u: "" ); |
| 73 | } |
| 74 | |
| 75 | StringRef Constraint::getDefName() const { |
| 76 | if (std::optional<StringRef> baseDefName = getBaseDefName()) |
| 77 | return *baseDefName; |
| 78 | return def->getName(); |
| 79 | } |
| 80 | |
| 81 | std::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 | |
| 96 | std::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(Name: baseName)) { |
| 101 | if (const auto *defInit = dyn_cast<llvm::DefInit>(Val: 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 | |
| 121 | std::optional<StringRef> Constraint::getCppFunctionName() const { |
| 122 | std::optional<StringRef> name = |
| 123 | def->getValueAsOptionalString(FieldName: "cppFunctionName" ); |
| 124 | if (!name || *name == "" ) |
| 125 | return std::nullopt; |
| 126 | return name; |
| 127 | } |
| 128 | |
| 129 | AppliedConstraint::AppliedConstraint(Constraint &&constraint, |
| 130 | llvm::StringRef self, |
| 131 | std::vector<std::string> &&entities) |
| 132 | : constraint(constraint), self(std::string(self)), |
| 133 | entities(std::move(entities)) {} |
| 134 | |
| 135 | unsigned DenseMapInfo<Constraint>::getHashValue(Constraint constraint) { |
| 136 | return llvm::hash_combine(args: constraint.getPredicate(), args: constraint.getSummary()); |
| 137 | } |
| 138 | |
| 139 | bool DenseMapInfo<Constraint>::isEqual(Constraint lhs, Constraint rhs) { |
| 140 | if (lhs == rhs) |
| 141 | return true; |
| 142 | return lhs.getPredicate() == rhs.getPredicate() && |
| 143 | lhs.getSummary() == rhs.getSummary(); |
| 144 | } |
| 145 | |