1 //===- OpTrait.cpp - OpTrait 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 // OpTrait wrapper to simplify using TableGen Record defining a MLIR OpTrait. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "mlir/TableGen/OpTrait.h" 14 #include "mlir/TableGen/Interfaces.h" 15 #include "mlir/TableGen/Predicate.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/Support/FormatVariadic.h" 18 #include "llvm/TableGen/Error.h" 19 #include "llvm/TableGen/Record.h" 20 21 using namespace mlir; 22 using namespace mlir::tblgen; 23 create(const llvm::Init * init)24OpTrait OpTrait::create(const llvm::Init *init) { 25 auto def = cast<llvm::DefInit>(init)->getDef(); 26 if (def->isSubClassOf("PredOpTrait")) 27 return OpTrait(Kind::Pred, def); 28 if (def->isSubClassOf("GenInternalOpTrait")) 29 return OpTrait(Kind::Internal, def); 30 if (def->isSubClassOf("OpInterfaceTrait")) 31 return OpTrait(Kind::Interface, def); 32 assert(def->isSubClassOf("NativeOpTrait")); 33 return OpTrait(Kind::Native, def); 34 } 35 OpTrait(Kind kind,const llvm::Record * def)36OpTrait::OpTrait(Kind kind, const llvm::Record *def) : def(def), kind(kind) {} 37 getTrait() const38llvm::StringRef NativeOpTrait::getTrait() const { 39 return def->getValueAsString("trait"); 40 } 41 getTrait() const42llvm::StringRef InternalOpTrait::getTrait() const { 43 return def->getValueAsString("trait"); 44 } 45 getPredTemplate() const46std::string PredOpTrait::getPredTemplate() const { 47 auto pred = Pred(def->getValueInit("predicate")); 48 return pred.getCondition(); 49 } 50 getDescription() const51llvm::StringRef PredOpTrait::getDescription() const { 52 return def->getValueAsString("description"); 53 } 54 getOpInterface() const55OpInterface InterfaceOpTrait::getOpInterface() const { 56 return OpInterface(def); 57 } 58 getTrait() const59std::string InterfaceOpTrait::getTrait() const { 60 llvm::StringRef trait = def->getValueAsString("trait"); 61 llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace"); 62 return cppNamespace.empty() ? trait.str() 63 : (cppNamespace + "::" + trait).str(); 64 } 65 shouldDeclareMethods() const66bool InterfaceOpTrait::shouldDeclareMethods() const { 67 return def->isSubClassOf("DeclareOpInterfaceMethods"); 68 } 69 getAlwaysDeclaredMethods() const70std::vector<StringRef> InterfaceOpTrait::getAlwaysDeclaredMethods() const { 71 return def->getValueAsListOfStrings("alwaysOverriddenMethods"); 72 } 73