1/**
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef GLOW_BASE_TRAITS_H
17#define GLOW_BASE_TRAITS_H
18
19#include "glow/Base/Type.h"
20
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
24
25namespace glow {
26
27/// This add the capability to name subclasses.
28class Named {
29 std::string name_{};
30
31public:
32 explicit Named(llvm::StringRef name) : name_(name) {}
33
34 /// \returns the name of the instruction.
35 llvm::StringRef getName() const { return name_; }
36
37 /// \returns the name of the instruction.
38 bool hasName() const { return !name_.empty(); }
39
40 /// Set the name of the instruction to \p name.
41 void setName(llvm::StringRef name) { name_ = name.str(); }
42
43 /// Compares by names, \returns true if name_ < x.name_.
44 bool compareByName(const Named &x) const {
45 return name_.compare(x.name_) > 0;
46 }
47};
48
49/// Use to sort named classes by their name.
50struct SortNamed {
51 inline bool operator()(const Named *named1, const Named *named2) const {
52 return named1->compareByName(*named2);
53 }
54};
55
56/// Subclasses of this class have a type associated with them.
57class Typed {
58private:
59 TypeRef Ty_{};
60
61public:
62 explicit Typed(TypeRef Ty) : Ty_(Ty) {}
63
64 TypeRef getType() const { return Ty_; }
65
66 void setType(TypeRef Ty) { Ty_ = Ty; }
67
68 llvm::ArrayRef<dim_t> dims() const { return Ty_->dims(); }
69
70 size_t size() const { return Ty_->size(); }
71
72 size_t getSizeInBytes() const { return Ty_->getSizeInBytes(); }
73
74 ElemKind getElementType() const { return Ty_->getElementType(); }
75
76 bool isType(TypeRef T) { return Ty_ == T; }
77};
78
79/// Subclasses of Value have an enum that describe their kind.
80class Kinded {
81public:
82 enum class Kind {
83#define DEF_INSTR(CLASS, NAME) CLASS##Kind,
84#define DEF_BACKEND_SPECIFIC_INSTR(CLASS, NAME) DEF_INSTR(CLASS, NAME)
85#define DEF_VALUE(CLASS, NAME) DEF_INSTR(CLASS, NAME)
86#include "glow/AutoGenInstr.def"
87#define DEF_NODE(CLASS, NAME) CLASS##Kind,
88#include "glow/AutoGenNodes.def"
89 };
90
91 static const char *getKindName(Kind IK) {
92 const char *names[] = {
93#define DEF_INSTR(CLASS, NAME) #NAME,
94#define DEF_BACKEND_SPECIFIC_INSTR(CLASS, NAME) DEF_INSTR(CLASS, NAME)
95#define DEF_VALUE(CLASS, NAME) DEF_INSTR(CLASS, NAME)
96#include "glow/AutoGenInstr.def"
97#define DEF_NODE(CLASS, NAME) #NAME,
98#include "glow/AutoGenNodes.def"
99 nullptr};
100 return names[(int)IK];
101 }
102
103private:
104 /// The kind of the value.
105 Kind kind_;
106
107public:
108 /// Ctor.
109 explicit Kinded(Kind vk) : kind_(vk) {}
110
111 /// Returns the kind of the instruction.
112 Kind getKind() const { return kind_; }
113
114 const char *getKindName() const { return getKindName(kind_); }
115};
116
117using KindSet = llvm::SmallSet<Kinded::Kind, 6>;
118
119/// Kind of the IR.
120enum class IRKind {
121 /// Glow high level graph IR.
122 GlowGraphIRKind,
123 /// Glow low level instruction IR.
124 GlowInstructionIRKind,
125 /// Glow FX IR.
126 GlowFXIRKind,
127};
128
129class Module;
130
131/// Subclasses of this class represent an IR container, e.g. a function or a
132/// module.
133class IRContainer : public Named {
134public:
135 IRContainer(llvm::StringRef name) : Named(name) {}
136 virtual ~IRContainer() = default;
137 virtual IRKind getIRKind() const = 0;
138 virtual Module *getParent() = 0;
139 virtual const Module *getParent() const = 0;
140};
141
142} // namespace glow
143
144#endif // GLOW_BASE_TRAITS_H
145