MLIR  16.0.0git
Trait.h
Go to the documentation of this file.
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 
51  Kind getKind() const { return kind; }
52 
53  // Returns the Tablegen definition this operator was constructed from.
54  const llvm::Record &getDef() const { return *def; }
55 
56 protected:
57  // The TableGen definition of this trait.
58  const llvm::Record *def;
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  static bool classof(const Trait *t) { return t->getKind() == Kind::Native; }
72 };
73 
74 // Trait corresponding to a predicate on the operation.
75 class PredTrait : public Trait {
76 public:
77  // Returns the template for constructing the predicate.
78  std::string getPredTemplate() const;
79 
80  // Returns the description of what the predicate is verifying.
81  StringRef getSummary() const;
82 
83  static bool classof(const Trait *t) { return t->getKind() == Kind::Pred; }
84 };
85 
86 // Trait controlling op definition generator internals.
87 class InternalTrait : public Trait {
88 public:
89  // Returns the trait controlling op definition generator internals.
90  StringRef getFullyQualifiedTraitName() const;
91 
92  static bool classof(const Trait *t) { return t->getKind() == Kind::Internal; }
93 };
94 
95 // Trait corresponding to an OpInterface on the operation.
96 class InterfaceTrait : public Trait {
97 public:
98  // Returns interface corresponding to the trait.
99  Interface getInterface() const;
100 
101  // Returns the trait corresponding to a C++ trait class.
102  std::string getFullyQualifiedTraitName() const;
103 
104  static bool classof(const Trait *t) {
105  return t->getKind() == Kind::Interface;
106  }
107 
108  // Whether the declaration of methods for this trait should be emitted.
109  bool shouldDeclareMethods() const;
110 
111  // Returns the methods that should always be declared if this interface is
112  // emitting declarations.
113  std::vector<StringRef> getAlwaysDeclaredMethods() const;
114 };
115 
116 } // namespace tblgen
117 } // namespace mlir
118 
119 #endif // MLIR_TABLEGEN_TRAIT_H_
Include the generated interface declarations.
static bool classof(const Trait *t)
Definition: Trait.h:83
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition: CallGraph.h:229
static bool classof(const Trait *t)
Definition: Trait.h:104
const llvm::Record * def
Definition: Trait.h:58
static bool classof(const Trait *t)
Definition: Trait.h:92
static bool classof(const Trait *t)
Definition: Trait.h:71
Kind getKind() const
Definition: Trait.h:51
const llvm::Record & getDef() const
Definition: Trait.h:54