1 //===- OpTrait.h - OpTrait 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 // OpTrait wrapper to simplify using TableGen Record defining an MLIR OpTrait. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_TABLEGEN_OPTRAIT_H_ 14 #define MLIR_TABLEGEN_OPTRAIT_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 } // end namespace llvm 24 25 namespace mlir { 26 namespace tblgen { 27 28 struct OpInterface; 29 30 // Wrapper class with helper methods for accessing OpTrait constraints defined 31 // in TableGen. 32 class OpTrait { 33 public: 34 // Discriminator for kinds of op traits. 35 enum class Kind { 36 // OpTrait corresponding to C++ class. 37 Native, 38 // OpTrait corresponding to predicate on operation. 39 Pred, 40 // OpTrait controlling op definition generator internals. 41 Internal, 42 // OpTrait corresponding to OpInterface. 43 Interface 44 }; 45 46 explicit OpTrait(Kind kind, const llvm::Record *def); 47 48 // Returns an OpTrait corresponding to the init provided. 49 static OpTrait 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 // OpTrait corresponding to a native C++ OpTrait. 63 class NativeOpTrait : public OpTrait { 64 public: 65 // Returns the trait corresponding to a C++ trait class. 66 StringRef getTrait() const; 67 classof(const OpTrait * t)68 static bool classof(const OpTrait *t) { return t->getKind() == Kind::Native; } 69 }; 70 71 // OpTrait corresponding to a predicate on the operation. 72 class PredOpTrait : public OpTrait { 73 public: 74 // Returns the template for constructing the predicate. 75 std::string getPredTemplate() const; 76 77 // Returns the description of what the predicate is verifying. 78 StringRef getDescription() const; 79 classof(const OpTrait * t)80 static bool classof(const OpTrait *t) { return t->getKind() == Kind::Pred; } 81 }; 82 83 // OpTrait controlling op definition generator internals. 84 class InternalOpTrait : public OpTrait { 85 public: 86 // Returns the trait controlling op definition generator internals. 87 StringRef getTrait() const; 88 classof(const OpTrait * t)89 static bool classof(const OpTrait *t) { 90 return t->getKind() == Kind::Internal; 91 } 92 }; 93 94 // OpTrait corresponding to an OpInterface on the operation. 95 class InterfaceOpTrait : public OpTrait { 96 public: 97 // Returns member function definitions corresponding to the trait, 98 OpInterface getOpInterface() const; 99 100 // Returns the trait corresponding to a C++ trait class. 101 std::string getTrait() const; 102 classof(const OpTrait * t)103 static bool classof(const OpTrait *t) { 104 return t->getKind() == Kind::Interface; 105 } 106 107 // Whether the declaration of methods for this trait should be emitted. 108 bool shouldDeclareMethods() const; 109 110 // Returns the methods that should always be declared if this interface is 111 // emitting declarations. 112 std::vector<StringRef> getAlwaysDeclaredMethods() const; 113 }; 114 115 } // end namespace tblgen 116 } // end namespace mlir 117 118 #endif // MLIR_TABLEGEN_OPTRAIT_H_ 119