MLIR 24.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
20namespace llvm {
21class Init;
22class Record;
23} // namespace llvm
24
25namespace mlir {
26namespace tblgen {
27
28class Interface;
29
30// Wrapper class with helper methods for accessing Trait constraints defined in
31// TableGen.
32class Trait {
33public:
34 // Discriminator for kinds of traits.
35 enum class Kind {
36 // Trait corresponding to C++ class.
38 // Trait corresponding to a predicate.
40 // Trait controlling definition generator internals.
42 // Trait corresponding to an 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 // Returns the C++ headers declaring this trait.
57 std::vector<StringRef> getCppIncludes() const;
58
59protected:
60 // The TableGen definition of this trait.
61 const llvm::Record *def;
63};
64
65// Trait corresponding to a native C++ Trait.
66class NativeTrait : public Trait {
67public:
68 // Returns the trait corresponding to a C++ trait class.
69 std::string getFullyQualifiedTraitName() const;
70
71 // Returns if this is a structural op trait.
72 bool isStructuralOpTrait() const;
73
74 // Returns extra class declaration code to be added to the concrete instance
75 // when the trait is specified
76 StringRef getExtraConcreteClassDeclaration() const;
77
78 // Returns extra class definition code to be added to the concrete instance
79 // when the trait is specified
80 StringRef getExtraConcreteClassDefinition() const;
81
82 static bool classof(const Trait *t) { return t->getKind() == Kind::Native; }
83};
84
85// Trait corresponding to a predicate on the operation.
86class PredTrait : public Trait {
87public:
88 // Returns the template for constructing the predicate.
89 std::string getPredTemplate() const;
90
91 // Returns the description of what the predicate is verifying.
92 StringRef getSummary() const;
93
94 static bool classof(const Trait *t) { return t->getKind() == Kind::Pred; }
95};
96
97// Trait controlling op definition generator internals.
98class InternalTrait : public Trait {
99public:
100 // Returns the trait controlling op definition generator internals.
101 StringRef getFullyQualifiedTraitName() const;
102
103 static bool classof(const Trait *t) { return t->getKind() == Kind::Internal; }
104};
105
106// Trait corresponding to an OpInterface on the operation.
107class InterfaceTrait : public Trait {
108public:
109 // Returns interface corresponding to the trait.
110 Interface getInterface() const;
111
112 // Returns the trait corresponding to a C++ trait class.
113 std::string getFullyQualifiedTraitName() const;
114
115 static bool classof(const Trait *t) {
116 return t->getKind() == Kind::Interface;
117 }
118
119 // Whether the declaration of methods for this trait should be emitted.
120 bool shouldDeclareMethods() const;
121
122 // Returns the methods that should always be declared if this interface is
123 // emitting declarations.
124 std::vector<StringRef> getAlwaysDeclaredMethods() const;
125};
126
127} // namespace tblgen
128} // namespace mlir
129
130#endif // MLIR_TABLEGEN_TRAIT_H_
static bool classof(const Trait *t)
Definition Trait.h:115
std::vector< StringRef > getAlwaysDeclaredMethods() const
Definition Trait.cpp:105
std::string getFullyQualifiedTraitName() const
Definition Trait.cpp:94
Interface getInterface() const
Definition Trait.cpp:92
bool shouldDeclareMethods() const
Definition Trait.cpp:101
StringRef getFullyQualifiedTraitName() const
Definition Trait.cpp:71
static bool classof(const Trait *t)
Definition Trait.h:103
bool isStructuralOpTrait() const
Definition Trait.cpp:55
std::string getFullyQualifiedTraitName() const
Definition Trait.cpp:48
static bool classof(const Trait *t)
Definition Trait.h:82
StringRef getExtraConcreteClassDeclaration() const
Definition Trait.cpp:59
StringRef getExtraConcreteClassDefinition() const
Definition Trait.cpp:63
StringRef getSummary() const
Definition Trait.cpp:84
static bool classof(const Trait *t)
Definition Trait.h:94
std::string getPredTemplate() const
Definition Trait.cpp:79
std::vector< StringRef > getCppIncludes() const
Definition Trait.cpp:40
const llvm::Record * def
Definition Trait.h:61
Trait(Kind kind, const llvm::Record *def)
Definition Trait.cpp:38
static Trait create(const llvm::Init *init)
Definition Trait.cpp:26
const llvm::Record & getDef() const
Definition Trait.h:54
Kind getKind() const
Definition Trait.h:51
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.