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