CIRCT 20.0.0git
Loading...
Searching...
No Matches
HW.cpp
Go to the documentation of this file.
1//===- HW.cpp - C interface for the HW dialect ----------------------------===//
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
15#include "circt/Support/LLVM.h"
16#include "mlir/CAPI/IR.h"
17#include "mlir/CAPI/Registration.h"
18#include "mlir/CAPI/Support.h"
19#include "llvm/ADT/PostOrderIterator.h"
20
21using namespace circt;
22using namespace circt::hw;
23
26
27//===----------------------------------------------------------------------===//
28// Dialect API.
29//===----------------------------------------------------------------------===//
30
33
34//===----------------------------------------------------------------------===//
35// Type API.
36//===----------------------------------------------------------------------===//
37
38int64_t hwGetBitWidth(MlirType type) { return getBitWidth(unwrap(type)); }
39
40bool hwTypeIsAValueType(MlirType type) { return isHWValueType(unwrap(type)); }
41
42bool hwTypeIsAArrayType(MlirType type) { return isa<ArrayType>(unwrap(type)); }
43
44MlirType hwArrayTypeGet(MlirType element, size_t size) {
45 return wrap(ArrayType::get(unwrap(element), size));
46}
47
48MlirType hwArrayTypeGetElementType(MlirType type) {
49 return wrap(cast<ArrayType>(unwrap(type)).getElementType());
50}
51
52intptr_t hwArrayTypeGetSize(MlirType type) {
53 return cast<ArrayType>(unwrap(type)).getNumElements();
54}
55
56bool hwTypeIsAIntType(MlirType type) { return isa<IntType>(unwrap(type)); }
57
58MlirType hwParamIntTypeGet(MlirAttribute parameter) {
59 return wrap(IntType::get(cast<TypedAttr>(unwrap(parameter))));
60}
61
62MlirAttribute hwParamIntTypeGetWidthAttr(MlirType type) {
63 return wrap(cast<IntType>(unwrap(type)).getWidth());
64}
65
66MlirType hwInOutTypeGet(MlirType element) {
67 return wrap(InOutType::get(unwrap(element)));
68}
69
70MlirType hwInOutTypeGetElementType(MlirType type) {
71 return wrap(cast<InOutType>(unwrap(type)).getElementType());
72}
73
74bool hwTypeIsAInOut(MlirType type) { return isa<InOutType>(unwrap(type)); }
75
76bool hwTypeIsAModuleType(MlirType type) {
77 return isa<ModuleType>(unwrap(type));
78}
79
80MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts,
81 HWModulePort const *ports) {
82 SmallVector<ModulePort> modulePorts;
83 for (intptr_t i = 0; i < numPorts; ++i) {
84 HWModulePort port = ports[i];
85
87 switch (port.dir) {
88 case HWModulePortDirection::Input:
89 dir = ModulePort::Direction::Input;
90 break;
91 case HWModulePortDirection::Output:
92 dir = ModulePort::Direction::Output;
93 break;
94 case HWModulePortDirection::InOut:
95 dir = ModulePort::Direction::InOut;
96 break;
97 }
98
99 StringAttr name = cast<StringAttr>(unwrap(port.name));
100 Type type = unwrap(port.type);
101
102 modulePorts.push_back(ModulePort{name, type, dir});
103 }
104
105 return wrap(ModuleType::get(unwrap(ctx), modulePorts));
106}
107
108intptr_t hwModuleTypeGetNumInputs(MlirType type) {
109 return cast<ModuleType>(unwrap(type)).getNumInputs();
110}
111
112MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index) {
113 return wrap(cast<ModuleType>(unwrap(type)).getInputType(index));
114}
115
116MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index) {
117 return wrap(cast<ModuleType>(unwrap(type)).getInputName(index));
118}
119
120intptr_t hwModuleTypeGetNumOutputs(MlirType type) {
121 return cast<ModuleType>(unwrap(type)).getNumOutputs();
122}
123
124MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index) {
125 return wrap(cast<ModuleType>(unwrap(type)).getOutputType(index));
126}
127
128MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index) {
129 return wrap(cast<ModuleType>(unwrap(type)).getOutputName(index));
130}
131
132bool hwTypeIsAStructType(MlirType type) {
133 return isa<StructType>(unwrap(type));
134}
135
136MlirType hwStructTypeGet(MlirContext ctx, intptr_t numElements,
137 HWStructFieldInfo const *elements) {
138 SmallVector<StructType::FieldInfo> fieldInfos;
139 fieldInfos.reserve(numElements);
140 for (intptr_t i = 0; i < numElements; ++i) {
141 fieldInfos.push_back(StructType::FieldInfo{
142 cast<StringAttr>(unwrap(elements[i].name)), unwrap(elements[i].type)});
143 }
144 return wrap(StructType::get(unwrap(ctx), fieldInfos));
145}
146
147MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName) {
148 StructType st = cast<StructType>(unwrap(structType));
149 return wrap(st.getFieldType(unwrap(fieldName)));
150}
151
152MlirAttribute hwStructTypeGetFieldIndex(MlirType structType,
153 MlirStringRef fieldName) {
154 StructType st = cast<StructType>(unwrap(structType));
155 if (auto idx = st.getFieldIndex(unwrap(fieldName)))
156 return wrap(IntegerAttr::get(IntegerType::get(st.getContext(), 32), *idx));
157 return wrap(UnitAttr::get(st.getContext()));
158}
159
160intptr_t hwStructTypeGetNumFields(MlirType structType) {
161 StructType st = cast<StructType>(unwrap(structType));
162 return st.getElements().size();
163}
164
165HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx) {
166 StructType st = cast<StructType>(unwrap(structType));
167 auto cppField = st.getElements()[idx];
169 ret.name = wrap(cppField.name);
170 ret.type = wrap(cppField.type);
171 return ret;
172}
173
174bool hwTypeIsATypeAliasType(MlirType type) {
175 return isa<TypeAliasType>(unwrap(type));
176}
177
178MlirType hwTypeAliasTypeGet(MlirStringRef cScope, MlirStringRef cName,
179 MlirType cInnerType) {
180 StringRef scope = unwrap(cScope);
181 StringRef name = unwrap(cName);
182 Type innerType = unwrap(cInnerType);
183 FlatSymbolRefAttr nameRef =
184 FlatSymbolRefAttr::get(innerType.getContext(), name);
185 SymbolRefAttr ref =
186 SymbolRefAttr::get(innerType.getContext(), scope, {nameRef});
187 return wrap(TypeAliasType::get(ref, innerType));
188}
189
190MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias) {
191 TypeAliasType type = cast<TypeAliasType>(unwrap(typeAlias));
192 return wrap(type.getCanonicalType());
193}
194
195MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias) {
196 TypeAliasType type = cast<TypeAliasType>(unwrap(typeAlias));
197 return wrap(type.getInnerType());
198}
199
200MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias) {
201 TypeAliasType type = cast<TypeAliasType>(unwrap(typeAlias));
202 return wrap(type.getRef().getLeafReference().getValue());
203}
204
205MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias) {
206 TypeAliasType type = cast<TypeAliasType>(unwrap(typeAlias));
207 return wrap(type.getRef().getRootReference().getValue());
208}
209
210//===----------------------------------------------------------------------===//
211// Attribute API.
212//===----------------------------------------------------------------------===//
213
214bool hwAttrIsAInnerSymAttr(MlirAttribute attr) {
215 return isa<InnerSymAttr>(unwrap(attr));
216}
217
218MlirAttribute hwInnerSymAttrGet(MlirAttribute symName) {
219 return wrap(InnerSymAttr::get(cast<StringAttr>(unwrap(symName))));
220}
221
222MlirAttribute hwInnerSymAttrGetEmpty(MlirContext ctx) {
223 return wrap(InnerSymAttr::get(unwrap(ctx)));
224}
225
226MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute innerSymAttr) {
227 return wrap((Attribute)cast<InnerSymAttr>(unwrap(innerSymAttr)).getSymName());
228}
229
230bool hwAttrIsAInnerRefAttr(MlirAttribute attr) {
231 return isa<InnerRefAttr>(unwrap(attr));
232}
233
234MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName,
235 MlirAttribute innerSym) {
236 auto moduleNameAttr = cast<StringAttr>(unwrap(moduleName));
237 auto innerSymAttr = cast<StringAttr>(unwrap(innerSym));
238 return wrap(InnerRefAttr::get(moduleNameAttr, innerSymAttr));
239}
240
241MlirAttribute hwInnerRefAttrGetName(MlirAttribute innerRefAttr) {
242 return wrap((Attribute)cast<InnerRefAttr>(unwrap(innerRefAttr)).getName());
243}
244
245MlirAttribute hwInnerRefAttrGetModule(MlirAttribute innerRefAttr) {
246 return wrap((Attribute)cast<InnerRefAttr>(unwrap(innerRefAttr)).getModule());
247}
248
249MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute attr) {
250 return isa<ParamDeclAttr>(unwrap(attr));
251}
252MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef cName,
253 MlirType cType,
254 MlirAttribute cValue) {
255 auto type = unwrap(cType);
256 auto name = StringAttr::get(type.getContext(), unwrap(cName));
257 return wrap(
258 ParamDeclAttr::get(type.getContext(), name, type, unwrap(cValue)));
259}
260MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl) {
261 return wrap(cast<ParamDeclAttr>(unwrap(decl)).getName().getValue());
262}
263MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl) {
264 return wrap(cast<ParamDeclAttr>(unwrap(decl)).getType());
265}
266MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl) {
267 return wrap(cast<ParamDeclAttr>(unwrap(decl)).getValue());
268}
269
270MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute attr) {
271 return isa<ParamDeclRefAttr>(unwrap(attr));
272}
273
274MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx,
275 MlirStringRef cName) {
276 auto name = StringAttr::get(unwrap(ctx), unwrap(cName));
277 return wrap(ParamDeclRefAttr::get(unwrap(ctx), name,
278 IntegerType::get(unwrap(ctx), 32)));
279}
280
281MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl) {
282 return wrap(cast<ParamDeclRefAttr>(unwrap(decl)).getName().getValue());
283}
284MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl) {
285 return wrap(cast<ParamDeclRefAttr>(unwrap(decl)).getType());
286}
287
288MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute attr) {
289 return isa<ParamVerbatimAttr>(unwrap(attr));
290}
291MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text) {
292 auto textAttr = cast<StringAttr>(unwrap(text));
293 MLIRContext *ctx = textAttr.getContext();
294 auto type = NoneType::get(ctx);
295 return wrap(ParamVerbatimAttr::get(ctx, textAttr, type));
296}
297
298MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute attr) {
299 return isa<OutputFileAttr>(unwrap(attr));
300}
301MLIR_CAPI_EXPORTED MlirAttribute
302hwOutputFileGetFromFileName(MlirAttribute fileName, bool excludeFromFileList,
303 bool includeReplicatedOp) {
304 auto fileNameStrAttr = cast<StringAttr>(unwrap(fileName));
305 return wrap(OutputFileAttr::getFromFilename(
306 fileNameStrAttr.getContext(), fileNameStrAttr.getValue(),
307 excludeFromFileList, includeReplicatedOp));
308}
309
310MLIR_CAPI_EXPORTED HWInstanceGraph hwInstanceGraphGet(MlirOperation operation) {
311 return wrap(new InstanceGraph{unwrap(operation)});
312}
313
314MLIR_CAPI_EXPORTED void hwInstanceGraphDestroy(HWInstanceGraph instanceGraph) {
315 delete unwrap(instanceGraph);
316}
317
318MLIR_CAPI_EXPORTED HWInstanceGraphNode
319hwInstanceGraphGetTopLevelNode(HWInstanceGraph instanceGraph) {
320 return wrap(unwrap(instanceGraph)->getTopLevelNode());
321}
322
323MLIR_CAPI_EXPORTED void
324hwInstanceGraphForEachNode(HWInstanceGraph instanceGraph,
326 void *userData) {
327 InstanceGraph *graph = unwrap(instanceGraph);
328 for (const auto &inst : llvm::post_order(graph)) {
329 callback(wrap(inst), userData);
330 }
331}
332
333MLIR_CAPI_EXPORTED bool hwInstanceGraphNodeEqual(HWInstanceGraphNode lhs,
334 HWInstanceGraphNode rhs) {
335 return unwrap(lhs) == unwrap(rhs);
336}
337
338MLIR_CAPI_EXPORTED MlirModule
339hwInstanceGraphNodeGetModule(HWInstanceGraphNode node) {
340 return wrap(dyn_cast<ModuleOp>(unwrap(node)->getModule().getOperation()));
341}
342
343MLIR_CAPI_EXPORTED MlirOperation
344hwInstanceGraphNodeGetModuleOp(HWInstanceGraphNode node) {
345 return wrap(unwrap(node)->getModule());
346}
DEFINE_C_API_PTR_METHODS(CirctFirtoolFirtoolOptions, circt::firtool::FirtoolOptions) CirctFirtoolFirtoolOptions circtFirtoolOptionsCreateDefault()
Definition Firtool.cpp:18
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
Definition CHIRRTL.cpp:30
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static void registerPasses()
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef cName, MlirType cType, MlirAttribute cValue)
Definition HW.cpp:252
MlirAttribute hwInnerRefAttrGetModule(MlirAttribute innerRefAttr)
Definition HW.cpp:245
MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl)
Definition HW.cpp:263
MlirType hwInOutTypeGet(MlirType element)
Creates an HW inout type in the context associated with element.
Definition HW.cpp:66
MlirType hwArrayTypeGet(MlirType element, size_t size)
Creates a fixed-size HW array type in the context associated with element.
Definition HW.cpp:44
intptr_t hwArrayTypeGetSize(MlirType type)
returns the size of an array type
Definition HW.cpp:52
MlirType hwStructTypeGet(MlirContext ctx, intptr_t numElements, HWStructFieldInfo const *elements)
Creates an HW struct type in the context associated with the elements.
Definition HW.cpp:136
MlirType hwArrayTypeGetElementType(MlirType type)
returns the element type of an array type
Definition HW.cpp:48
MLIR_CAPI_EXPORTED HWInstanceGraphNode hwInstanceGraphGetTopLevelNode(HWInstanceGraph instanceGraph)
Definition HW.cpp:319
intptr_t hwModuleTypeGetNumInputs(MlirType type)
Get an HW module type's number of inputs.
Definition HW.cpp:108
MLIR_CAPI_EXPORTED MlirOperation hwInstanceGraphNodeGetModuleOp(HWInstanceGraphNode node)
Definition HW.cpp:344
intptr_t hwStructTypeGetNumFields(MlirType structType)
Definition HW.cpp:160
bool hwTypeIsAArrayType(MlirType type)
If the type is an HW array.
Definition HW.cpp:42
bool hwTypeIsATypeAliasType(MlirType type)
If the type is an HW type alias.
Definition HW.cpp:174
MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
Definition HW.cpp:195
MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias)
Definition HW.cpp:190
MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
Definition HW.cpp:147
MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName, MlirAttribute innerSym)
Definition HW.cpp:234
MLIR_CAPI_EXPORTED bool hwInstanceGraphNodeEqual(HWInstanceGraphNode lhs, HWInstanceGraphNode rhs)
Definition HW.cpp:333
MLIR_CAPI_EXPORTED MlirModule hwInstanceGraphNodeGetModule(HWInstanceGraphNode node)
Definition HW.cpp:339
MLIR_CAPI_EXPORTED HWInstanceGraph hwInstanceGraphGet(MlirOperation operation)
Definition HW.cpp:310
bool hwTypeIsAIntType(MlirType type)
If the type is an HW int.
Definition HW.cpp:56
MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index)
Get an HW module type's input type at a specific index.
Definition HW.cpp:112
bool hwTypeIsAInOut(MlirType type)
If the type is an HW inout.
Definition HW.cpp:74
MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index)
Get an HW module type's input name at a specific index.
Definition HW.cpp:116
MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts, HWModulePort const *ports)
Creates an HW module type.
Definition HW.cpp:80
MlirAttribute hwStructTypeGetFieldIndex(MlirType structType, MlirStringRef fieldName)
Definition HW.cpp:152
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl)
Definition HW.cpp:266
MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute innerSymAttr)
Definition HW.cpp:226
MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
Definition HW.cpp:218
MLIR_CAPI_EXPORTED void hwInstanceGraphDestroy(HWInstanceGraph instanceGraph)
Definition HW.cpp:314
MLIR_CAPI_EXPORTED void hwInstanceGraphForEachNode(HWInstanceGraph instanceGraph, HWInstanceGraphNodeCallback callback, void *userData)
Definition HW.cpp:324
MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias)
Definition HW.cpp:205
MlirType hwInOutTypeGetElementType(MlirType type)
Returns the element type of an inout type.
Definition HW.cpp:70
MlirType hwTypeAliasTypeGet(MlirStringRef cScope, MlirStringRef cName, MlirType cInnerType)
Definition HW.cpp:178
bool hwTypeIsAValueType(MlirType type)
Return true if the specified type can be used as an HW value type, that is the set of types that can ...
Definition HW.cpp:40
bool hwAttrIsAInnerSymAttr(MlirAttribute attr)
Definition HW.cpp:214
MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute attr)
Definition HW.cpp:288
bool hwTypeIsAStructType(MlirType type)
If the type is an HW struct.
Definition HW.cpp:132
MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute attr)
Definition HW.cpp:298
MlirType hwParamIntTypeGet(MlirAttribute parameter)
Definition HW.cpp:58
MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index)
Get an HW module type's output name at a specific index.
Definition HW.cpp:128
MLIR_CAPI_EXPORTED MlirAttribute hwOutputFileGetFromFileName(MlirAttribute fileName, bool excludeFromFileList, bool includeReplicatedOp)
Definition HW.cpp:302
MlirAttribute hwInnerRefAttrGetName(MlirAttribute innerRefAttr)
Definition HW.cpp:241
MlirAttribute hwInnerSymAttrGetEmpty(MlirContext ctx)
Definition HW.cpp:222
HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx)
Definition HW.cpp:165
bool hwTypeIsAModuleType(MlirType type)
If the type is an HW module type.
Definition HW.cpp:76
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute attr)
Definition HW.cpp:270
int64_t hwGetBitWidth(MlirType type)
Return the hardware bit width of a type.
Definition HW.cpp:38
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl)
Definition HW.cpp:281
MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias)
Definition HW.cpp:200
void registerHWPasses()
Definition HW.cpp:32
bool hwAttrIsAInnerRefAttr(MlirAttribute attr)
Definition HW.cpp:230
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx, MlirStringRef cName)
Definition HW.cpp:274
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute attr)
Definition HW.cpp:249
MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index)
Get an HW module type's output type at a specific index.
Definition HW.cpp:124
MlirAttribute hwParamIntTypeGetWidthAttr(MlirType type)
Definition HW.cpp:62
intptr_t hwModuleTypeGetNumOutputs(MlirType type)
Get an HW module type's number of outputs.
Definition HW.cpp:120
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl)
Definition HW.cpp:260
MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text)
Definition HW.cpp:291
MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl)
Definition HW.cpp:284
void(* HWInstanceGraphNodeCallback)(HWInstanceGraphNode, void *)
Definition HW.h:207
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:113
HW-specific instance graph with a virtual entry node linking to all publicly visible modules.
This is a Node in the InstanceGraph.
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 ...
int64_t getBitWidth(mlir::Type type)
Return the hardware bit width of a type.
Definition HWTypes.cpp:110
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition hw.py:1
HWModulePortDirection dir
Definition HW.h:41
MlirAttribute name
Definition HW.h:39
MlirType type
Definition HW.h:40
MlirIdentifier name
Definition HW.h:30
MlirType type
Definition HW.h:31