MLIR  17.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 
18 #include "llvm/ADT/MapVector.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/StringMap.h"
21 
22 namespace mlir {
23 class BytecodeDialectInterface;
24 class BytecodeWriterConfig;
25 
26 namespace bytecode {
27 namespace detail {
28 struct DialectNumbering;
29 
30 //===----------------------------------------------------------------------===//
31 // Attribute and Type Numbering
32 //===----------------------------------------------------------------------===//
33 
34 /// This class represents a numbering entry for an Attribute or Type.
37 
38  /// The concrete value.
40 
41  /// The number assigned to this value.
42  unsigned number = 0;
43 
44  /// The number of references to this value.
45  unsigned refCount = 1;
46 
47  /// The dialect of this value.
49 };
52  Attribute getValue() const { return value.get<Attribute>(); }
53 };
56  Type getValue() const { return value.get<Type>(); }
57 };
58 
59 //===----------------------------------------------------------------------===//
60 // OpName Numbering
61 //===----------------------------------------------------------------------===//
62 
63 /// This class represents the numbering entry of an operation name.
66  : dialect(dialect), name(name) {}
67 
68  /// The dialect of this value.
70 
71  /// The concrete name.
73 
74  /// The number assigned to this name.
75  unsigned number = 0;
76 
77  /// The number of references to this name.
78  unsigned refCount = 1;
79 };
80 
81 //===----------------------------------------------------------------------===//
82 // Dialect Resource Numbering
83 //===----------------------------------------------------------------------===//
84 
85 /// This class represents a numbering entry for a dialect resource.
87  DialectResourceNumbering(std::string key) : key(std::move(key)) {}
88 
89  /// The key used to reference this resource.
90  std::string key;
91 
92  /// The number assigned to this resource.
93  unsigned number = 0;
94 
95  /// A flag indicating if this resource is only a declaration, not a full
96  /// definition.
97  bool isDeclaration = true;
98 };
99 
100 //===----------------------------------------------------------------------===//
101 // Dialect Numbering
102 //===----------------------------------------------------------------------===//
103 
104 /// This class represents a numbering entry for an Dialect.
106  DialectNumbering(StringRef name, unsigned number)
107  : name(name), number(number) {}
108 
109  /// The namespace of the dialect.
110  StringRef name;
111 
112  /// The number assigned to the dialect.
113  unsigned number;
114 
115  /// The bytecode dialect interface of the dialect if defined.
116  const BytecodeDialectInterface *interface = nullptr;
117 
118  /// The asm dialect interface of the dialect if defined.
120 
121  /// The referenced resources of this dialect.
123 
124  /// A mapping from resource key to the corresponding resource numbering entry.
125  llvm::MapVector<StringRef, DialectResourceNumbering *> resourceMap;
126 };
127 
128 //===----------------------------------------------------------------------===//
129 // IRNumberingState
130 //===----------------------------------------------------------------------===//
131 
132 /// This class manages numbering IR entities in preparation of bytecode
133 /// emission.
135 public:
137 
138  /// Return the numbered dialects.
139  auto getDialects() {
140  return llvm::make_pointee_range(llvm::make_second_range(dialects));
141  }
142  auto getAttributes() { return llvm::make_pointee_range(orderedAttrs); }
143  auto getOpNames() { return llvm::make_pointee_range(orderedOpNames); }
144  auto getTypes() { return llvm::make_pointee_range(orderedTypes); }
145 
146  /// Return the number for the given IR unit.
147  unsigned getNumber(Attribute attr) {
148  assert(attrs.count(attr) && "attribute not numbered");
149  return attrs[attr]->number;
150  }
151  unsigned getNumber(Block *block) {
152  assert(blockIDs.count(block) && "block not numbered");
153  return blockIDs[block];
154  }
155  unsigned getNumber(OperationName opName) {
156  assert(opNames.count(opName) && "opName not numbered");
157  return opNames[opName]->number;
158  }
159  unsigned getNumber(Type type) {
160  assert(types.count(type) && "type not numbered");
161  return types[type]->number;
162  }
163  unsigned getNumber(Value value) {
164  assert(valueIDs.count(value) && "value not numbered");
165  return valueIDs[value];
166  }
167  unsigned getNumber(const AsmDialectResourceHandle &resource) {
168  assert(dialectResources.count(resource) && "resource not numbered");
169  return dialectResources[resource]->number;
170  }
171 
172  /// Return the block and value counts of the given region.
173  std::pair<unsigned, unsigned> getBlockValueCount(Region *region) {
174  assert(regionBlockValueCounts.count(region) && "value not numbered");
175  return regionBlockValueCounts[region];
176  }
177 
178  /// Return the number of operations in the given block.
179  unsigned getOperationCount(Block *block) {
180  assert(blockOperationCounts.count(block) && "block not numbered");
181  return blockOperationCounts[block];
182  }
183 
184 private:
185  /// This class is used to provide a fake dialect writer for numbering nested
186  /// attributes and types.
187  struct NumberingDialectWriter;
188 
189  /// Number the given IR unit for bytecode emission.
190  void number(Attribute attr);
191  void number(Block &block);
192  DialectNumbering &numberDialect(Dialect *dialect);
193  DialectNumbering &numberDialect(StringRef dialect);
194  void number(Operation &op);
195  void number(OperationName opName);
196  void number(Region &region);
197  void number(Type type);
198 
199  /// Number the given dialect resources.
200  void number(Dialect *dialect, ArrayRef<AsmDialectResourceHandle> resources);
201 
202  /// Finalize the numberings of any dialect resources.
203  void finalizeDialectResourceNumberings(Operation *rootOp);
204 
205  /// Mapping from IR to the respective numbering entries.
209  DenseMap<Dialect *, DialectNumbering *> registeredDialects;
210  llvm::MapVector<StringRef, DialectNumbering *> dialects;
211  std::vector<AttributeNumbering *> orderedAttrs;
212  std::vector<OpNameNumbering *> orderedOpNames;
213  std::vector<TypeNumbering *> orderedTypes;
214 
215  /// A mapping from dialect resource handle to the numbering for the referenced
216  /// resource.
218  dialectResources;
219 
220  /// Allocators used for the various numbering entries.
221  llvm::SpecificBumpPtrAllocator<AttributeNumbering> attrAllocator;
222  llvm::SpecificBumpPtrAllocator<DialectNumbering> dialectAllocator;
223  llvm::SpecificBumpPtrAllocator<OpNameNumbering> opNameAllocator;
224  llvm::SpecificBumpPtrAllocator<DialectResourceNumbering> resourceAllocator;
225  llvm::SpecificBumpPtrAllocator<TypeNumbering> typeAllocator;
226 
227  /// The value ID for each Block and Value.
229  DenseMap<Value, unsigned> valueIDs;
230 
231  /// The number of operations in each block.
232  DenseMap<Block *, unsigned> blockOperationCounts;
233 
234  /// A map from region to the number of blocks and values within that region.
235  DenseMap<Region *, std::pair<unsigned, unsigned>> regionBlockValueCounts;
236 
237  /// The next value ID to assign when numbering.
238  unsigned nextValueID = 0;
239 };
240 } // namespace detail
241 } // namespace bytecode
242 } // namespace mlir
243 
244 #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:30
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:41
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
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:93
This class manages numbering IR entities in preparation of bytecode emission.
Definition: IRNumbering.h:134
unsigned getOperationCount(Block *block)
Return the number of operations in the given block.
Definition: IRNumbering.h:179
unsigned getNumber(OperationName opName)
Definition: IRNumbering.h:155
std::pair< unsigned, unsigned > getBlockValueCount(Region *region)
Return the block and value counts of the given region.
Definition: IRNumbering.h:173
unsigned getNumber(const AsmDialectResourceHandle &resource)
Definition: IRNumbering.h:167
auto getDialects()
Return the numbered dialects.
Definition: IRNumbering.h:139
unsigned getNumber(Attribute attr)
Return the number for the given IR unit.
Definition: IRNumbering.h:147
Include the generated interface declarations.
This class represents a numbering entry for an Attribute or Type.
Definition: IRNumbering.h:35
unsigned refCount
The number of references to this value.
Definition: IRNumbering.h:45
AttrTypeNumbering(PointerUnion< Attribute, Type > value)
Definition: IRNumbering.h:36
unsigned number
The number assigned to this value.
Definition: IRNumbering.h:42
DialectNumbering * dialect
The dialect of this value.
Definition: IRNumbering.h:48
PointerUnion< Attribute, Type > value
The concrete value.
Definition: IRNumbering.h:39
This class represents a numbering entry for an Dialect.
Definition: IRNumbering.h:105
StringRef name
The namespace of the dialect.
Definition: IRNumbering.h:110
DialectNumbering(StringRef name, unsigned number)
Definition: IRNumbering.h:106
unsigned number
The number assigned to the dialect.
Definition: IRNumbering.h:113
llvm::MapVector< StringRef, DialectResourceNumbering * > resourceMap
A mapping from resource key to the corresponding resource numbering entry.
Definition: IRNumbering.h:125
SetVector< AsmDialectResourceHandle > resources
The referenced resources of this dialect.
Definition: IRNumbering.h:122
const OpAsmDialectInterface * asmInterface
The asm dialect interface of the dialect if defined.
Definition: IRNumbering.h:119
This class represents a numbering entry for a dialect resource.
Definition: IRNumbering.h:86
std::string key
The key used to reference this resource.
Definition: IRNumbering.h:90
bool isDeclaration
A flag indicating if this resource is only a declaration, not a full definition.
Definition: IRNumbering.h:97
unsigned number
The number assigned to this resource.
Definition: IRNumbering.h:93
This class represents the numbering entry of an operation name.
Definition: IRNumbering.h:64
unsigned refCount
The number of references to this name.
Definition: IRNumbering.h:78
OpNameNumbering(DialectNumbering *dialect, OperationName name)
Definition: IRNumbering.h:65
DialectNumbering * dialect
The dialect of this value.
Definition: IRNumbering.h:69
unsigned number
The number assigned to this name.
Definition: IRNumbering.h:75
OperationName name
The concrete name.
Definition: IRNumbering.h:72