1 //===- Trait.h - Trait wrapper class ----------------------------*- C++ -*-===// 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 // Trait wrapper to simplify using TableGen Record defining an MLIR Trait. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_TABLEGEN_TRAIT_H_ 14 #define MLIR_TABLEGEN_TRAIT_H_ 15 16 #include "mlir/Support/LLVM.h" 17 #include "llvm/ADT/StringRef.h" 18 #include <vector> 19 20 namespace llvm { 21 class Init; 22 class Record; 23 } // namespace llvm 24 25 namespace mlir { 26 namespace tblgen { 27 28 class Interface; 29 30 // Wrapper class with helper methods for accessing Trait constraints defined in 31 // TableGen. 32 class Trait { 33 public: 34 // Discriminator for kinds of traits. 35 enum class Kind { 36 // Trait corresponding to C++ class. 37 Native, 38 // Trait corresponding to a predicate. 39 Pred, 40 // Trait controlling definition generator internals. 41 Internal, 42 // Trait corresponding to an Interface. 43 Interface 44 }; 45 46 explicit Trait(Kind kind, const llvm::Record *def); 47 48 // Returns an Trait corresponding to the init provided. 49 static Trait create(const llvm::Init *init); 50 getKind()51 Kind getKind() const { return kind; } 52 53 // Returns the Tablegen definition this operator was constructed from. getDef()54 const llvm::Record &getDef() const { return *def; } 55 56 protected: 57 // The TableGen definition of this trait. 58 const llvm::Record *def; 59 Kind kind; 60 }; 61 62 // Trait corresponding to a native C++ Trait. 63 class NativeTrait : public Trait { 64 public: 65 // Returns the trait corresponding to a C++ trait class. 66 std::string getFullyQualifiedTraitName() const; 67 68 // Returns if this is a structural op trait. 69 bool isStructuralOpTrait() const; 70 71 // Returns extra class declaration code to be added to the concrete instance 72 // when the trait is specified 73 StringRef getExtraConcreteClassDeclaration() const; 74 75 // Returns extra class definition code to be added to the concrete instance 76 // when the trait is specified 77 StringRef getExtraConcreteClassDefinition() const; 78 classof(const Trait * t)79 static bool classof(const Trait *t) { return t->getKind() == Kind::Native; } 80 }; 81 82 // Trait corresponding to a predicate on the operation. 83 class PredTrait : public Trait { 84 public: 85 // Returns the template for constructing the predicate. 86 std::string getPredTemplate() const; 87 88 // Returns the description of what the predicate is verifying. 89 StringRef getSummary() const; 90 classof(const Trait * t)91 static bool classof(const Trait *t) { return t->getKind() == Kind::Pred; } 92 }; 93 94 // Trait controlling op definition generator internals. 95 class InternalTrait : public Trait { 96 public: 97 // Returns the trait controlling op definition generator internals. 98 StringRef getFullyQualifiedTraitName() const; 99 classof(const Trait * t)100 static bool classof(const Trait *t) { return t->getKind() == Kind::Internal; } 101 }; 102 103 // Trait corresponding to an OpInterface on the operation. 104 class InterfaceTrait : public Trait { 105 public: 106 // Returns interface corresponding to the trait. 107 Interface getInterface() const; 108 109 // Returns the trait corresponding to a C++ trait class. 110 std::string getFullyQualifiedTraitName() const; 111 classof(const Trait * t)112 static bool classof(const Trait *t) { 113 return t->getKind() == Kind::Interface; 114 } 115 116 // Whether the declaration of methods for this trait should be emitted. 117 bool shouldDeclareMethods() const; 118 119 // Returns the methods that should always be declared if this interface is 120 // emitting declarations. 121 std::vector<StringRef> getAlwaysDeclaredMethods() const; 122 }; 123 124 } // namespace tblgen 125 } // namespace mlir 126 127 #endif // MLIR_TABLEGEN_TRAIT_H_ 128