CIRCT 24.0.0git
Loading...
Searching...
No Matches
HWTypes.h
Go to the documentation of this file.
1//===- HWTypes.h - Types for the HW dialect ---------------------*- 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// Types for the HW dialect are mostly in tablegen. This file should contain
10// C++ types used in MLIR type parameters.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CIRCT_DIALECT_HW_TYPES_H
15#define CIRCT_DIALECT_HW_TYPES_H
16
19#include "mlir/IR/DialectInterface.h"
20#include "mlir/Interfaces/MemorySlotInterfaces.h"
21
23#include "circt/Support/LLVM.h"
24#include "mlir/IR/AttrTypeSubElements.h"
25#include "mlir/IR/BuiltinTypes.h"
26#include "mlir/IR/Types.h"
27
28namespace circt {
29namespace hw {
30
31struct ModulePort {
33 mlir::StringAttr name;
34 mlir::Type type;
36};
37
39 : public mlir::DialectInterface::Base<HWModulePortTypeInterface> {
40 HWModulePortTypeInterface(mlir::Dialect *dialect) : Base(dialect) {}
41
42 /// Return failure if `type` is not valid for a module port with the given
43 /// direction. Dialects can implement this to restrict non-HW-value handle
44 /// types in HW module signatures.
45 virtual mlir::LogicalResult verifyHWModulePortType(
46 llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
47 ModulePort::Direction direction, mlir::Type type) const {
48 return mlir::success();
49 }
50};
51
52/// Interface for dialects to classify their types as valid probe payloads.
54 : public mlir::DialectInterface::Base<ProbeTypeDialectInterface> {
55 ProbeTypeDialectInterface(mlir::Dialect *dialect) : Base(dialect) {}
56
57 virtual bool isValidProbeElementType(mlir::Type type) const = 0;
58};
59
60/// Return true if `type` is a valid probe payload. Builtin integer types are
61/// always valid; other types are classified by their owning dialect.
62bool isValidProbeElementType(mlir::Type type);
63
64static bool operator==(const ModulePort &a, const ModulePort &b) {
65 return a.dir == b.dir && a.name == b.name && a.type == b.type;
66}
67static llvm::hash_code hash_value(const ModulePort &port) {
68 return llvm::hash_combine(port.dir, port.name, port.type);
69}
70
71namespace detail {
72struct ModuleTypeStorage : public TypeStorage {
73 ModuleTypeStorage(ArrayRef<ModulePort> inPorts);
74
75 using KeyTy = ArrayRef<ModulePort>;
76
77 /// Define the comparison function for the key type.
78 bool operator==(const KeyTy &key) const {
79 return std::equal(key.begin(), key.end(), ports.begin(), ports.end());
80 }
81
82 /// Define a hash function for the key type.
83 static llvm::hash_code hashKey(const KeyTy &key) {
84 return llvm::hash_combine_range(key.begin(), key.end());
85 }
86
87 /// Define a construction method for creating a new instance of this storage.
88 static ModuleTypeStorage *construct(mlir::TypeStorageAllocator &allocator,
89 const KeyTy &key) {
90 return new (allocator.allocate<ModuleTypeStorage>()) ModuleTypeStorage(key);
91 }
92
93 /// Construct an instance of the key from this storage class.
94 KeyTy getAsKey() const { return ports; }
95
96 ArrayRef<ModulePort> getPorts() const { return ports; }
97
98 /// The parametric data held by the storage class.
99 SmallVector<ModulePort> ports;
100 // Cache of common lookups
101 SmallVector<size_t> inputToAbs;
102 SmallVector<size_t> outputToAbs;
103 SmallVector<size_t> absToInput;
104 SmallVector<size_t> absToOutput;
105};
106} // namespace detail
107
108class HWSymbolCache;
109class ParamDeclAttr;
110class TypedeclOp;
111class ModuleType;
112
113namespace detail {
114
115ModuleType fnToMod(Operation *op, ArrayRef<Attribute> inputNames,
116 ArrayRef<Attribute> outputNames);
117ModuleType fnToMod(FunctionType fn, ArrayRef<Attribute> inputNames,
118 ArrayRef<Attribute> outputNames);
119
120/// Struct defining a field. Used in structs.
122
123/// Struct defining a field with an offset. Used in unions.
125 StringAttr name;
126 Type type;
127 size_t offset;
128};
129} // namespace detail
130} // namespace hw
131} // namespace circt
132
133namespace mlir {
134/// Expose the field names and types of union types to the generic attribute and
135/// type walking and replacement infrastructure. This allows walkers to recurse
136/// into the fields of `!hw.union` types, and replacers such as
137/// `mlir::AttrTypeReplacer` to replace types nested within the fields.
138template <>
139struct AttrTypeSubElementHandler<circt::hw::detail::OffsetFieldInfo> {
140 static void walk(const circt::hw::detail::OffsetFieldInfo &param,
141 AttrTypeImmediateSubElementWalker &walker) {
142 walker.walk(param.name);
143 walker.walk(param.type);
144 }
147 AttrSubElementReplacements &attrRepls,
148 TypeSubElementReplacements &typeRepls) {
149 return {cast<StringAttr>(attrRepls.take_front(1)[0]),
150 typeRepls.take_front(1)[0], param.offset};
151 }
152};
153} // namespace mlir
154
155#define GET_TYPEDEF_CLASSES
156#include "circt/Dialect/HW/HWTypes.h.inc"
157
158namespace circt {
159namespace hw {
160
161/// Recursively remove HW type aliases from a type and its subelements.
162mlir::Type getCanonicalType(mlir::Type type);
163
164/// Return true if the specified type is a value HW Integer type. This checks
165/// that it is a signless standard dialect type.
166bool isHWIntegerType(mlir::Type type);
167
168/// Return true if the specified type is a HW Enum type.
169bool isHWEnumType(mlir::Type type);
170
171/// Return true if the specified type can be used as an HW value type, that is
172/// the set of types that can be composed together to represent synthesized,
173/// hardware but not marker types like InOutType or unknown types from other
174/// dialects.
175bool isHWValueType(mlir::Type type);
176
177/// Return the hardware bit width of a type. Does not reflect any encoding,
178/// padding, or storage scheme, just the bit (and wire width) of a
179/// statically-size type. Reflects the number of wires needed to transmit a
180/// value of this type. Returns -1 if the type is not known or cannot be
181/// statically computed.
182int64_t getBitWidth(mlir::Type type);
183
184/// Return true if the specified type contains known marker types like
185/// InOutType. Unlike isHWValueType, this is not conservative, it only returns
186/// false on known InOut types, rather than any unknown types.
187bool hasHWInOutType(mlir::Type type);
188
189/// Convert an APInt value into a nested aggregate attribute matching the given
190/// HWAggregateType. Returns failure() if the type is not an HWAggregateType or
191/// recursively contains a type other than HWAggregateType or IntegerType.
192LogicalResult apIntToAggregateAttr(mlir::Type aggregateType,
193 const APInt &intVal, ArrayAttr &result);
194
195/// Convert an ArrayAttr into an APInt value matching the given type.
196/// The type is used to determine the bit width of the resulting APInt.
197/// Returns failure() if the attribute recursively contains anything other than
198/// ArrayAttr or IntegerAttr.
199LogicalResult aggregateAttrToAPInt(mlir::Type type, ArrayAttr attr,
200 APInt &result);
201
202template <typename... BaseTy>
203bool type_isa(Type type) {
204 // First check if the type is the requested type.
205 if (isa<BaseTy...>(type))
206 return true;
207
208 // Then check if it is a type alias wrapping the requested type.
209 if (auto alias = dyn_cast<TypeAliasType>(type))
210 return type_isa<BaseTy...>(alias.getInnerType());
211
212 return false;
213}
214
215// type_isa for a nullable argument.
216template <typename... BaseTy>
217bool type_isa_and_nonnull(Type type) { // NOLINT(readability-identifier-naming)
218 if (!type)
219 return false;
220 return type_isa<BaseTy...>(type);
221}
222
223template <typename BaseTy>
224BaseTy type_cast(Type type) {
225 assert(type_isa<BaseTy>(type) && "type must convert to requested type");
226
227 // If the type is the requested type, return it.
228 if (isa<BaseTy>(type))
229 return cast<BaseTy>(type);
230
231 // Otherwise, it must be a type alias wrapping the requested type.
232 return type_cast<BaseTy>(cast<TypeAliasType>(type).getInnerType());
233}
234
235template <typename BaseTy>
236BaseTy type_dyn_cast(Type type) {
237 if (!type_isa<BaseTy>(type))
238 return BaseTy();
239
240 return type_cast<BaseTy>(type);
241}
242
243/// Utility type that wraps a type that may be one of several possible Types.
244/// This is similar to std::variant but is implemented for mlir::Type, and it
245/// understands how to handle type aliases.
246template <typename... Types>
248 : public ::mlir::Type::TypeBase<TypeVariant<Types...>, mlir::Type,
249 mlir::TypeStorage> {
250 using mlir::Type::TypeBase<TypeVariant<Types...>, mlir::Type,
251 mlir::TypeStorage>::Base::Base;
252
253public:
254 // Support LLVM isa/cast/dyn_cast to one of the possible types.
255 static bool classof(Type other) { return type_isa<Types...>(other); }
256};
257
258template <typename BaseTy>
260 : public ::mlir::Type::TypeBase<TypeAliasOr<BaseTy>, mlir::Type,
261 mlir::TypeStorage> {
263 mlir::TypeStorage>::Base::Base;
264
265public:
266 // Support LLVM isa/cast/dyn_cast to BaseTy.
267 static bool classof(Type other) { return type_isa<BaseTy>(other); }
268
269 // Support C++ implicit conversions to BaseTy.
270 operator BaseTy() const { return type_cast<BaseTy>(*this); }
271};
272
273} // namespace hw
274} // namespace circt
275
276#endif // CIRCT_DIALECT_HW_TYPES_H
assert(baseType &&"element must be base type")
This stores lookup tables to make manipulating and working with the IR more efficient.
Definition HWSymCache.h:28
static bool classof(Type other)
Definition HWTypes.h:267
Utility type that wraps a type that may be one of several possible Types.
Definition HWTypes.h:249
static bool classof(Type other)
Definition HWTypes.h:255
ModuleType fnToMod(Operation *op, ArrayRef< Attribute > inputNames, ArrayRef< Attribute > outputNames)
Definition HWTypes.cpp:1119
BaseTy type_cast(Type type)
Definition HWTypes.h:224
bool isHWIntegerType(mlir::Type type)
Return true if the specified type is a value HW Integer type.
Definition HWTypes.cpp:59
bool type_isa_and_nonnull(Type type)
Definition HWTypes.h:217
BaseTy type_dyn_cast(Type type)
Definition HWTypes.h:236
bool isHWValueType(mlir::Type type)
Return true if the specified type can be used as an HW value type, that is the set of types that can ...
bool type_isa(Type type)
Definition HWTypes.h:203
static bool operator==(const ModulePort &a, const ModulePort &b)
Definition HWTypes.h:64
static llvm::hash_code hash_value(const ModulePort &port)
Definition HWTypes.h:67
bool isValidProbeElementType(mlir::Type type)
Return true if type is a valid probe payload.
LogicalResult aggregateAttrToAPInt(mlir::Type type, ArrayAttr attr, APInt &result)
Convert an ArrayAttr into an APInt value matching the given type.
int64_t getBitWidth(mlir::Type type)
Return the hardware bit width of a type.
Definition HWTypes.cpp:121
LogicalResult apIntToAggregateAttr(mlir::Type aggregateType, const APInt &intVal, ArrayAttr &result)
Convert an APInt value into a nested aggregate attribute matching the given HWAggregateType.
bool isHWEnumType(mlir::Type type)
Return true if the specified type is a HW Enum type.
Definition HWTypes.cpp:72
mlir::Type getCanonicalType(mlir::Type type)
Recursively remove HW type aliases from a type and its subelements.
Definition HWTypes.cpp:50
bool hasHWInOutType(mlir::Type type)
Return true if the specified type contains known marker types like InOutType.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1
A named and typed field of an aggregate type.
Definition FieldInfo.h:27
virtual mlir::LogicalResult verifyHWModulePortType(llvm::function_ref< mlir::InFlightDiagnostic()> emitError, ModulePort::Direction direction, mlir::Type type) const
Return failure if type is not valid for a module port with the given direction.
Definition HWTypes.h:45
HWModulePortTypeInterface(mlir::Dialect *dialect)
Definition HWTypes.h:40
mlir::Type type
Definition HWTypes.h:34
mlir::StringAttr name
Definition HWTypes.h:33
Interface for dialects to classify their types as valid probe payloads.
Definition HWTypes.h:54
virtual bool isValidProbeElementType(mlir::Type type) const =0
ProbeTypeDialectInterface(mlir::Dialect *dialect)
Definition HWTypes.h:55
SmallVector< ModulePort > ports
The parametric data held by the storage class.
Definition HWTypes.h:99
static llvm::hash_code hashKey(const KeyTy &key)
Define a hash function for the key type.
Definition HWTypes.h:83
SmallVector< size_t > absToInput
Definition HWTypes.h:103
bool operator==(const KeyTy &key) const
Define the comparison function for the key type.
Definition HWTypes.h:78
SmallVector< size_t > outputToAbs
Definition HWTypes.h:102
SmallVector< size_t > inputToAbs
Definition HWTypes.h:101
ArrayRef< ModulePort > KeyTy
Definition HWTypes.h:75
static ModuleTypeStorage * construct(mlir::TypeStorageAllocator &allocator, const KeyTy &key)
Define a construction method for creating a new instance of this storage.
Definition HWTypes.h:88
ArrayRef< ModulePort > getPorts() const
Definition HWTypes.h:96
KeyTy getAsKey() const
Construct an instance of the key from this storage class.
Definition HWTypes.h:94
SmallVector< size_t > absToOutput
Definition HWTypes.h:104
Struct defining a field with an offset. Used in unions.
Definition HWTypes.h:124
static void walk(const circt::hw::detail::OffsetFieldInfo &param, AttrTypeImmediateSubElementWalker &walker)
Definition HWTypes.h:140
static circt::hw::detail::OffsetFieldInfo replace(const circt::hw::detail::OffsetFieldInfo &param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
Definition HWTypes.h:146