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::tblgen; 17 18 Constraint::Constraint(const llvm::Record *record) 19 : def(record), kind(CK_Uncategorized) { 20 // Look through OpVariable's to their constraint. 21 if (def->isSubClassOf("OpVariable")) 22 def = def->getValueAsDef("constraint"); 23 if (def->isSubClassOf("TypeConstraint")) { 24 kind = CK_Type; 25 } else if (def->isSubClassOf("AttrConstraint")) { 26 kind = CK_Attr; 27 } else if (def->isSubClassOf("RegionConstraint")) { 28 kind = CK_Region; 29 } else if (def->isSubClassOf("SuccessorConstraint")) { 30 kind = CK_Successor; 31 } else { 32 assert(def->isSubClassOf("Constraint")); 33 } 34 } 35 36 Constraint::Constraint(Kind kind, const llvm::Record *record) 37 : def(record), kind(kind) { 38 // Look through OpVariable's to their constraint. 39 if (def->isSubClassOf("OpVariable")) 40 def = def->getValueAsDef("constraint"); 41 } 42 43 Pred Constraint::getPredicate() const { 44 auto *val = def->getValue("predicate"); 45 46 // If no predicate is specified, then return the null predicate (which 47 // corresponds to true). 48 if (!val) 49 return Pred(); 50 51 const auto *pred = dyn_cast<llvm::DefInit>(val->getValue()); 52 return Pred(pred); 53 } 54 55 std::string Constraint::getConditionTemplate() const { 56 return getPredicate().getCondition(); 57 } 58 59 llvm::StringRef Constraint::getDescription() const { 60 auto doc = def->getValueAsString("description"); 61 if (doc.empty()) 62 return def->getName(); 63 return doc; 64 } 65 66 AppliedConstraint::AppliedConstraint(Constraint &&constraint, 67 llvm::StringRef self, 68 std::vector<std::string> &&entities) 69 : constraint(constraint), self(std::string(self)), 70 entities(std::move(entities)) {} 71