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"
18#include "mlir/IR/OpImplementation.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;
26class BytecodeWriterConfig;
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.
37struct AttrTypeNumbering {
38 AttrTypeNumbering(PointerUnion<Attribute, Type> value) : value(value) {}
39
40 /// The concrete value.
41 PointerUnion<Attribute, Type> 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.
50 DialectNumbering *dialect = nullptr;
51};
52struct AttributeNumbering : public AttrTypeNumbering {
53 AttributeNumbering(Attribute value) : AttrTypeNumbering(value) {}
54 Attribute getValue() const { return cast<Attribute>(Val: value); }
55};
56struct TypeNumbering : public AttrTypeNumbering {
57 TypeNumbering(Type value) : AttrTypeNumbering(value) {}
58 Type getValue() const { return cast<Type>(Val: value); }
59};
60
61//===----------------------------------------------------------------------===//
62// OpName Numbering
63//===----------------------------------------------------------------------===//
64
65/// This class represents the numbering entry of an operation name.
66struct OpNameNumbering {
67 OpNameNumbering(DialectNumbering *dialect, OperationName name)
68 : dialect(dialect), name(name) {}
69
70 /// The dialect of this value.
71 DialectNumbering *dialect;
72
73 /// The concrete name.
74 OperationName 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.
88struct DialectResourceNumbering {
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.
107struct DialectNumbering {
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.
124 SetVector<AsmDialectResourceHandle> resources;
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.
135struct OperationNumbering {
136 OperationNumbering(unsigned number) : number(number) {}
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.
152class IRNumberingState {
153public:
154 IRNumberingState(Operation *op, const BytecodeWriterConfig &config);
155
156 /// Return the numbered dialects.
157 auto getDialects() {
158 return llvm::make_pointee_range(Range: llvm::make_second_range(c&: dialects));
159 }
160 auto getAttributes() { return llvm::make_pointee_range(Range&: orderedAttrs); }
161 auto getOpNames() { return llvm::make_pointee_range(Range&: orderedOpNames); }
162 auto getTypes() { return llvm::make_pointee_range(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.
208 bool isIsolatedFromAbove(Operation *op) {
209 assert(operations.count(op) && "operation not numbered");
210 return operations[op]->isIsolatedFromAbove.value_or(u: false);
211 }
212
213 /// Get the set desired bytecode version to emit.
214 int64_t getDesiredBytecodeVersion() const;
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.
242 DenseMap<Attribute, AttributeNumbering *> attrs;
243 DenseMap<Operation *, OperationNumbering *> operations;
244 DenseMap<OperationName, OpNameNumbering *> opNames;
245 DenseMap<Type, TypeNumbering *> types;
246 DenseMap<Dialect *, DialectNumbering *> registeredDialects;
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.
254 llvm::DenseMap<AsmDialectResourceHandle, DialectResourceNumbering *>
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.
266 DenseMap<Block *, unsigned> blockIDs;
267 DenseMap<Value, unsigned> valueIDs;
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.
273 DenseMap<Region *, std::pair<unsigned, unsigned>> regionBlockValueCounts;
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
286