MLIR 23.0.0git
IRNumbering.h
Go to the documentation of this file.
1//===- IRNumbering.h - MLIR bytecode IR numbering ---------------*- 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// This file contains various utilities that number IR structures in preparation
10// for bytecode emission.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LIB_MLIR_BYTECODE_WRITER_IRNUMBERING_H
15#define LIB_MLIR_BYTECODE_WRITER_IRNUMBERING_H
16
17#include "mlir/IR/Location.h"
19#include "llvm/ADT/MapVector.h"
20#include "llvm/ADT/SetVector.h"
21#include "llvm/ADT/StringMap.h"
22#include <cstdint>
23
24namespace mlir {
25class BytecodeDialectInterface;
27
28namespace bytecode {
29namespace detail {
30struct DialectNumbering;
31
32//===----------------------------------------------------------------------===//
33// Attribute and Type Numbering
34//===----------------------------------------------------------------------===//
35
36/// This class represents a numbering entry for an Attribute or Type.
39
40 /// The concrete value.
42
43 /// The number assigned to this value.
44 unsigned number = 0;
45
46 /// The number of references to this value.
47 unsigned refCount = 1;
48
49 /// The dialect of this value.
51};
58 Type getValue() const { return cast<Type>(value); }
59};
60
61//===----------------------------------------------------------------------===//
62// OpName Numbering
63//===----------------------------------------------------------------------===//
64
65/// This class represents the numbering entry of an operation name.
69
70 /// The dialect of this value.
72
73 /// The concrete name.
75
76 /// The number assigned to this name.
77 unsigned number = 0;
78
79 /// The number of references to this name.
80 unsigned refCount = 1;
81};
82
83//===----------------------------------------------------------------------===//
84// Dialect Resource Numbering
85//===----------------------------------------------------------------------===//
86
87/// This class represents a numbering entry for a dialect resource.
89 DialectResourceNumbering(std::string key) : key(std::move(key)) {}
90
91 /// The key used to reference this resource.
92 std::string key;
93
94 /// The number assigned to this resource.
95 unsigned number = 0;
96
97 /// A flag indicating if this resource is only a declaration, not a full
98 /// definition.
99 bool isDeclaration = true;
100};
101
102//===----------------------------------------------------------------------===//
103// Dialect Numbering
104//===----------------------------------------------------------------------===//
105
106/// This class represents a numbering entry for an Dialect.
108 DialectNumbering(StringRef name, unsigned number)
109 : name(name), number(number) {}
110
111 /// The namespace of the dialect.
112 StringRef name;
113
114 /// The number assigned to the dialect.
115 unsigned number;
116
117 /// The bytecode dialect interface of the dialect if defined.
118 const BytecodeDialectInterface *interface = nullptr;
119
120 /// The asm dialect interface of the dialect if defined.
121 const OpAsmDialectInterface *asmInterface = nullptr;
122
123 /// The referenced resources of this dialect.
125
126 /// A mapping from resource key to the corresponding resource numbering entry.
127 llvm::MapVector<StringRef, DialectResourceNumbering *> resourceMap;
128};
129
130//===----------------------------------------------------------------------===//
131// Operation Numbering
132//===----------------------------------------------------------------------===//
133
134/// This class represents the numbering entry of an operation.
137
138 /// The number assigned to this operation.
139 unsigned number;
140
141 /// A flag indicating if this operation's regions are isolated. If unset, the
142 /// operation isn't yet known to be isolated.
143 std::optional<bool> isIsolatedFromAbove;
144};
145
146//===----------------------------------------------------------------------===//
147// IRNumberingState
148//===----------------------------------------------------------------------===//
149
150/// This class manages numbering IR entities in preparation of bytecode
151/// emission.
153public:
155
156 /// Return the numbered dialects.
157 auto getDialects() {
158 return llvm::make_pointee_range(llvm::make_second_range(dialects));
159 }
160 auto getAttributes() { return llvm::make_pointee_range(orderedAttrs); }
161 auto getOpNames() { return llvm::make_pointee_range(orderedOpNames); }
162 auto getTypes() { return llvm::make_pointee_range(orderedTypes); }
163
164 /// Return the number for the given IR unit.
165 unsigned getNumber(Attribute attr) {
166 assert(attrs.count(attr) && "attribute not numbered");
167 return attrs[attr]->number;
168 }
169 unsigned getNumber(Location loc);
170 unsigned getNumber(Block *block) {
171 assert(blockIDs.count(block) && "block not numbered");
172 return blockIDs[block];
173 }
174 unsigned getNumber(Operation *op) {
175 assert(operations.count(op) && "operation not numbered");
176 return operations[op]->number;
177 }
178 unsigned getNumber(OperationName opName) {
179 assert(opNames.count(opName) && "opName not numbered");
180 return opNames[opName]->number;
181 }
182 unsigned getNumber(Type type) {
183 assert(types.count(type) && "type not numbered");
184 return types[type]->number;
185 }
186 unsigned getNumber(Value value) {
187 assert(valueIDs.count(value) && "value not numbered");
188 return valueIDs[value];
189 }
190 unsigned getNumber(const AsmDialectResourceHandle &resource) {
191 assert(dialectResources.count(resource) && "resource not numbered");
192 return dialectResources[resource]->number;
193 }
194
195 /// Return the block and value counts of the given region.
196 std::pair<unsigned, unsigned> getBlockValueCount(Region *region) {
197 assert(regionBlockValueCounts.count(region) && "value not numbered");
198 return regionBlockValueCounts[region];
199 }
200
201 /// Return the number of operations in the given block.
202 unsigned getOperationCount(Block *block) {
203 assert(blockOperationCounts.count(block) && "block not numbered");
204 return blockOperationCounts[block];
205 }
206
207 /// Return if the given operation is isolated from above.
209 assert(operations.count(op) && "operation not numbered");
210 return operations[op]->isIsolatedFromAbove.value_or(false);
211 }
212
213 /// Get the set desired bytecode version to emit.
215
216private:
217 /// This class is used to provide a fake dialect writer for numbering nested
218 /// attributes and types.
219 struct NumberingDialectWriter;
220
221 /// Compute the global numbering state for the given root operation.
222 void computeGlobalNumberingState(Operation *rootOp);
223
224 /// Number the given IR unit for bytecode emission.
225 void number(Attribute attr);
226 void number(Location loc);
227 void number(Block &block);
228 DialectNumbering &numberDialect(Dialect *dialect);
229 DialectNumbering &numberDialect(StringRef dialect);
230 void number(Operation &op);
231 void number(OperationName opName);
232 void number(Region &region);
233 void number(Type type);
234
235 /// Number the given dialect resources.
236 void number(Dialect *dialect, ArrayRef<AsmDialectResourceHandle> resources);
237
238 /// Finalize the numberings of any dialect resources.
239 void finalizeDialectResourceNumberings(Operation *rootOp);
240
241 /// Mapping from IR to the respective numbering entries.
247 llvm::MapVector<StringRef, DialectNumbering *> dialects;
248 std::vector<AttributeNumbering *> orderedAttrs;
249 std::vector<OpNameNumbering *> orderedOpNames;
250 std::vector<TypeNumbering *> orderedTypes;
251
252 /// A mapping from dialect resource handle to the numbering for the referenced
253 /// resource.
255 dialectResources;
256
257 /// Allocators used for the various numbering entries.
258 llvm::SpecificBumpPtrAllocator<AttributeNumbering> attrAllocator;
259 llvm::SpecificBumpPtrAllocator<DialectNumbering> dialectAllocator;
260 llvm::SpecificBumpPtrAllocator<OperationNumbering> opAllocator;
261 llvm::SpecificBumpPtrAllocator<OpNameNumbering> opNameAllocator;
262 llvm::SpecificBumpPtrAllocator<DialectResourceNumbering> resourceAllocator;
263 llvm::SpecificBumpPtrAllocator<TypeNumbering> typeAllocator;
264
265 /// The value ID for each Block and Value.
268
269 /// The number of operations in each block.
270 DenseMap<Block *, unsigned> blockOperationCounts;
271
272 /// A map from region to the number of blocks and values within that region.
274
275 /// The next value ID to assign when numbering.
276 unsigned nextValueID = 0;
277
278 // Configuration: useful to query the required version to emit.
279 const BytecodeWriterConfig &config;
280};
281} // namespace detail
282} // namespace bytecode
283} // namespace mlir
284
285#endif
This class represents an opaque handle to a dialect resource entry.
Attributes are known-constant values of operations.
Definition Attributes.h:25
Block represents an ordered list of Operations.
Definition Block.h:33
This class contains the configuration used for the bytecode writer.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
IRNumberingState(Operation *op, const BytecodeWriterConfig &config)
unsigned getOperationCount(Block *block)
Return the number of operations in the given block.
unsigned getNumber(OperationName opName)
int64_t getDesiredBytecodeVersion() const
Get the set desired bytecode version to emit.
std::pair< unsigned, unsigned > getBlockValueCount(Region *region)
Return the block and value counts of the given region.
bool isIsolatedFromAbove(Operation *op)
Return if the given operation is isolated from above.
unsigned getNumber(const AsmDialectResourceHandle &resource)
auto getDialects()
Return the numbered dialects.
unsigned getNumber(Attribute attr)
Return the number for the given IR unit.
Include the generated interface declarations.
llvm::SetVector< T, Vector, Set, N > SetVector
Definition LLVM.h:125
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:120
unsigned refCount
The number of references to this value.
Definition IRNumbering.h:47
AttrTypeNumbering(PointerUnion< Attribute, Type > value)
Definition IRNumbering.h:38
unsigned number
The number assigned to this value.
Definition IRNumbering.h:44
DialectNumbering * dialect
The dialect of this value.
Definition IRNumbering.h:50
PointerUnion< Attribute, Type > value
The concrete value.
Definition IRNumbering.h:41
This class represents a numbering entry for an Dialect.
StringRef name
The namespace of the dialect.
DialectNumbering(StringRef name, unsigned number)
unsigned number
The number assigned to the dialect.
llvm::MapVector< StringRef, DialectResourceNumbering * > resourceMap
A mapping from resource key to the corresponding resource numbering entry.
SetVector< AsmDialectResourceHandle > resources
The referenced resources of this dialect.
const OpAsmDialectInterface * asmInterface
The asm dialect interface of the dialect if defined.
std::string key
The key used to reference this resource.
Definition IRNumbering.h:92
bool isDeclaration
A flag indicating if this resource is only a declaration, not a full definition.
Definition IRNumbering.h:99
unsigned number
The number assigned to this resource.
Definition IRNumbering.h:95
unsigned refCount
The number of references to this name.
Definition IRNumbering.h:80
OpNameNumbering(DialectNumbering *dialect, OperationName name)
Definition IRNumbering.h:67
DialectNumbering * dialect
The dialect of this value.
Definition IRNumbering.h:71
unsigned number
The number assigned to this name.
Definition IRNumbering.h:77
OperationName name
The concrete name.
Definition IRNumbering.h:74
unsigned number
The number assigned to this operation.
std::optional< bool > isIsolatedFromAbove
A flag indicating if this operation's regions are isolated.