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("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 38 Pred Constraint::getPredicate() const { 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 (Optional<StringRef> summary = def->getValueAsOptionalString("summary")) 56 return *summary; 57 return def->getName(); 58 } 59 60 StringRef Constraint::getDefName() const { 61 // Functor used to check a base def in the case where the current def is 62 // anonymous. 63 auto checkBaseDefFn = [&](StringRef baseName) { 64 if (const auto *init = dyn_cast<llvm::DefInit>(def->getValueInit(baseName))) 65 return Constraint(init->getDef(), kind).getDefName(); 66 return def->getName(); 67 }; 68 69 switch (kind) { 70 case CK_Attr: 71 if (def->isAnonymous()) 72 return checkBaseDefFn("baseAttr"); 73 return def->getName(); 74 case CK_Type: 75 if (def->isAnonymous()) 76 return checkBaseDefFn("baseType"); 77 return def->getName(); 78 default: 79 return def->getName(); 80 } 81 } 82 83 AppliedConstraint::AppliedConstraint(Constraint &&constraint, 84 llvm::StringRef self, 85 std::vector<std::string> &&entities) 86 : constraint(constraint), self(std::string(self)), 87 entities(std::move(entities)) {} 88