CIRCT  18.0.0git
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 //
9 // Implements a C Interface for the HW Dialect
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "circt-c/Dialect/HW.h"
15 #include "circt/Dialect/HW/HWOps.h"
17 #include "circt/Support/LLVM.h"
18 #include "mlir/CAPI/IR.h"
19 #include "mlir/CAPI/Registration.h"
20 #include "mlir/CAPI/Support.h"
21 
22 using namespace circt;
23 using namespace circt::hw;
24 
25 //===----------------------------------------------------------------------===//
26 // Dialect API.
27 //===----------------------------------------------------------------------===//
28 
30 
31 //===----------------------------------------------------------------------===//
32 // Type API.
33 //===----------------------------------------------------------------------===//
34 
35 int64_t hwGetBitWidth(MlirType type) { return getBitWidth(unwrap(type)); }
36 
37 bool hwTypeIsAValueType(MlirType type) { return isHWValueType(unwrap(type)); }
38 
39 bool hwTypeIsAArrayType(MlirType type) { return unwrap(type).isa<ArrayType>(); }
40 
41 MlirType hwArrayTypeGet(MlirType element, size_t size) {
42  return wrap(ArrayType::get(unwrap(element), size));
43 }
44 
45 MlirType hwArrayTypeGetElementType(MlirType type) {
46  return wrap(unwrap(type).cast<ArrayType>().getElementType());
47 }
48 
49 intptr_t hwArrayTypeGetSize(MlirType type) {
50  return unwrap(type).cast<ArrayType>().getNumElements();
51 }
52 
53 bool hwTypeIsAIntType(MlirType type) { return unwrap(type).isa<IntType>(); }
54 
55 MlirType hwParamIntTypeGet(MlirAttribute parameter) {
56  return wrap(IntType::get(unwrap(parameter).cast<TypedAttr>()));
57 }
58 
59 MlirAttribute hwParamIntTypeGetWidthAttr(MlirType type) {
60  return wrap(unwrap(type).cast<IntType>().getWidth());
61 }
62 
63 MlirType hwInOutTypeGet(MlirType element) {
64  return wrap(InOutType::get(unwrap(element)));
65 }
66 
67 MlirType hwInOutTypeGetElementType(MlirType type) {
68  return wrap(unwrap(type).cast<InOutType>().getElementType());
69 }
70 
71 bool hwTypeIsAInOut(MlirType type) { return unwrap(type).isa<InOutType>(); }
72 
73 bool hwTypeIsAModuleType(MlirType type) {
74  return isa<ModuleType>(unwrap(type));
75 }
76 
77 MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts,
78  HWModulePort const *ports) {
79  SmallVector<ModulePort> modulePorts;
80  for (intptr_t i = 0; i < numPorts; ++i) {
81  HWModulePort port = ports[i];
82 
84  switch (port.dir) {
87  break;
90  break;
93  break;
94  }
95 
96  StringAttr name = cast<StringAttr>(unwrap(port.name));
97  Type type = unwrap(port.type);
98 
99  modulePorts.push_back(ModulePort{name, type, dir});
100  }
101 
102  return wrap(ModuleType::get(unwrap(ctx), modulePorts));
103 }
104 
105 intptr_t hwModuleTypeGetNumInputs(MlirType type) {
106  return cast<ModuleType>(unwrap(type)).getNumInputs();
107 }
108 
109 MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index) {
110  return wrap(cast<ModuleType>(unwrap(type)).getInputType(index));
111 }
112 
113 MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index) {
114  return wrap(cast<ModuleType>(unwrap(type)).getInputName(index));
115 }
116 
117 intptr_t hwModuleTypeGetNumOutputs(MlirType type) {
118  return cast<ModuleType>(unwrap(type)).getNumOutputs();
119 }
120 
121 MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index) {
122  return wrap(cast<ModuleType>(unwrap(type)).getOutputType(index));
123 }
124 
125 MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index) {
126  return wrap(cast<ModuleType>(unwrap(type)).getOutputName(index));
127 }
128 
129 bool hwTypeIsAStructType(MlirType type) {
130  return unwrap(type).isa<StructType>();
131 }
132 
133 MlirType hwStructTypeGet(MlirContext ctx, intptr_t numElements,
134  HWStructFieldInfo const *elements) {
135  SmallVector<StructType::FieldInfo> fieldInfos;
136  fieldInfos.reserve(numElements);
137  for (intptr_t i = 0; i < numElements; ++i) {
138  fieldInfos.push_back(StructType::FieldInfo{
139  unwrap(elements[i].name).cast<StringAttr>(), unwrap(elements[i].type)});
140  }
141  return wrap(StructType::get(unwrap(ctx), fieldInfos));
142 }
143 
144 MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName) {
145  StructType st = unwrap(structType).cast<StructType>();
146  return wrap(st.getFieldType(unwrap(fieldName)));
147 }
148 
149 MlirAttribute hwStructTypeGetFieldIndex(MlirType structType,
150  MlirStringRef fieldName) {
151  StructType st = unwrap(structType).cast<StructType>();
152  if (auto idx = st.getFieldIndex(unwrap(fieldName)))
153  return wrap(IntegerAttr::get(IntegerType::get(st.getContext(), 32), *idx));
154  return wrap(UnitAttr::get(st.getContext()));
155 }
156 
157 intptr_t hwStructTypeGetNumFields(MlirType structType) {
158  StructType st = unwrap(structType).cast<StructType>();
159  return st.getElements().size();
160 }
161 
162 HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx) {
163  StructType st = unwrap(structType).cast<StructType>();
164  auto cppField = st.getElements()[idx];
165  HWStructFieldInfo ret;
166  ret.name = wrap(cppField.name);
167  ret.type = wrap(cppField.type);
168  return ret;
169 }
170 
171 bool hwTypeIsATypeAliasType(MlirType type) {
172  return unwrap(type).isa<TypeAliasType>();
173 }
174 
175 MlirType hwTypeAliasTypeGet(MlirStringRef cScope, MlirStringRef cName,
176  MlirType cInnerType) {
177  StringRef scope = unwrap(cScope);
178  StringRef name = unwrap(cName);
179  Type innerType = unwrap(cInnerType);
180  FlatSymbolRefAttr nameRef =
181  FlatSymbolRefAttr::get(innerType.getContext(), name);
182  SymbolRefAttr ref =
183  SymbolRefAttr::get(innerType.getContext(), scope, {nameRef});
184  return wrap(TypeAliasType::get(ref, innerType));
185 }
186 
187 MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias) {
188  TypeAliasType type = unwrap(typeAlias).cast<TypeAliasType>();
189  return wrap(type.getCanonicalType());
190 }
191 
192 MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias) {
193  TypeAliasType type = unwrap(typeAlias).cast<TypeAliasType>();
194  return wrap(type.getInnerType());
195 }
196 
197 MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias) {
198  TypeAliasType type = unwrap(typeAlias).cast<TypeAliasType>();
199  return wrap(type.getRef().getLeafReference().getValue());
200 }
201 
202 MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias) {
203  TypeAliasType type = unwrap(typeAlias).cast<TypeAliasType>();
204  return wrap(type.getRef().getRootReference().getValue());
205 }
206 
207 //===----------------------------------------------------------------------===//
208 // Attribute API.
209 //===----------------------------------------------------------------------===//
210 
211 bool hwAttrIsAInnerSymAttr(MlirAttribute attr) {
212  return unwrap(attr).isa<InnerSymAttr>();
213 }
214 
215 MlirAttribute hwInnerSymAttrGet(MlirAttribute symName) {
216  return wrap(InnerSymAttr::get(unwrap(symName).cast<StringAttr>()));
217 }
218 
219 MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute innerSymAttr) {
220  return wrap(
221  (Attribute)unwrap(innerSymAttr).cast<InnerSymAttr>().getSymName());
222 }
223 
224 bool hwAttrIsAInnerRefAttr(MlirAttribute attr) {
225  return unwrap(attr).isa<InnerRefAttr>();
226 }
227 
228 MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName,
229  MlirAttribute innerSym) {
230  auto moduleNameAttr = unwrap(moduleName).cast<StringAttr>();
231  auto innerSymAttr = unwrap(innerSym).cast<StringAttr>();
232  return wrap(InnerRefAttr::get(moduleNameAttr, innerSymAttr));
233 }
234 
235 MlirAttribute hwInnerRefAttrGetName(MlirAttribute innerRefAttr) {
236  return wrap((Attribute)unwrap(innerRefAttr).cast<InnerRefAttr>().getName());
237 }
238 
239 MlirAttribute hwInnerRefAttrGetModule(MlirAttribute innerRefAttr) {
240  return wrap((Attribute)unwrap(innerRefAttr).cast<InnerRefAttr>().getModule());
241 }
242 
243 MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute attr) {
244  return unwrap(attr).isa<ParamDeclAttr>();
245 }
246 MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef cName,
247  MlirType cType,
248  MlirAttribute cValue) {
249  auto type = unwrap(cType);
250  auto name = StringAttr::get(type.getContext(), unwrap(cName));
251  return wrap(
252  ParamDeclAttr::get(type.getContext(), name, type, unwrap(cValue)));
253 }
254 MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl) {
255  return wrap(unwrap(decl).cast<ParamDeclAttr>().getName().getValue());
256 }
257 MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl) {
258  return wrap(unwrap(decl).cast<ParamDeclAttr>().getType());
259 }
260 MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl) {
261  return wrap(unwrap(decl).cast<ParamDeclAttr>().getValue());
262 }
263 
264 MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute attr) {
265  return unwrap(attr).isa<ParamDeclRefAttr>();
266 }
267 
268 MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx,
269  MlirStringRef cName) {
270  auto name = StringAttr::get(unwrap(ctx), unwrap(cName));
271  return wrap(ParamDeclRefAttr::get(unwrap(ctx), name,
272  IntegerType::get(unwrap(ctx), 32)));
273 }
274 
275 MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl) {
276  return wrap(unwrap(decl).cast<ParamDeclRefAttr>().getName().getValue());
277 }
278 MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl) {
279  return wrap(unwrap(decl).cast<ParamDeclRefAttr>().getType());
280 }
281 
282 MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute attr) {
283  return unwrap(attr).isa<ParamVerbatimAttr>();
284 }
285 MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text) {
286  auto textAttr = unwrap(text).cast<StringAttr>();
287  MLIRContext *ctx = textAttr.getContext();
288  auto type = NoneType::get(ctx);
289  return wrap(ParamVerbatimAttr::get(ctx, textAttr, type));
290 }
291 
292 MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute attr) {
293  return unwrap(attr).isa<OutputFileAttr>();
294 }
295 MLIR_CAPI_EXPORTED MlirAttribute
296 hwOutputFileGetFromFileName(MlirAttribute fileName, bool excludeFromFileList,
297  bool includeReplicatedOp) {
298  auto fileNameStrAttr = unwrap(fileName).cast<StringAttr>();
299  return wrap(OutputFileAttr::getFromFilename(
300  fileNameStrAttr.getContext(), fileNameStrAttr.getValue(),
301  excludeFromFileList, includeReplicatedOp));
302 }
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
Definition: CHIRRTL.cpp:26
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGet(MlirStringRef cName, MlirType cType, MlirAttribute cValue)
Definition: HW.cpp:246
MlirAttribute hwInnerRefAttrGetModule(MlirAttribute innerRefAttr)
Definition: HW.cpp:239
MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl)
Definition: HW.cpp:257
MlirType hwInOutTypeGet(MlirType element)
Creates an HW inout type in the context associated with element.
Definition: HW.cpp:63
MlirType hwArrayTypeGet(MlirType element, size_t size)
Creates a fixed-size HW array type in the context associated with element.
Definition: HW.cpp:41
intptr_t hwArrayTypeGetSize(MlirType type)
returns the size of an array type
Definition: HW.cpp:49
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:133
MlirType hwArrayTypeGetElementType(MlirType type)
returns the element type of an array type
Definition: HW.cpp:45
intptr_t hwModuleTypeGetNumInputs(MlirType type)
Get an HW module type's number of inputs.
Definition: HW.cpp:105
intptr_t hwStructTypeGetNumFields(MlirType structType)
Definition: HW.cpp:157
bool hwTypeIsAArrayType(MlirType type)
If the type is an HW array.
Definition: HW.cpp:39
bool hwTypeIsATypeAliasType(MlirType type)
If the type is an HW type alias.
Definition: HW.cpp:171
MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
Definition: HW.cpp:192
MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias)
Definition: HW.cpp:187
MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
Definition: HW.cpp:144
MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName, MlirAttribute innerSym)
Definition: HW.cpp:228
bool hwTypeIsAIntType(MlirType type)
If the type is an HW int.
Definition: HW.cpp:53
MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index)
Get an HW module type's input type at a specific index.
Definition: HW.cpp:109
bool hwTypeIsAInOut(MlirType type)
If the type is an HW inout.
Definition: HW.cpp:71
MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index)
Get an HW module type's input name at a specific index.
Definition: HW.cpp:113
MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts, HWModulePort const *ports)
Creates an HW module type.
Definition: HW.cpp:77
MlirAttribute hwStructTypeGetFieldIndex(MlirType structType, MlirStringRef fieldName)
Definition: HW.cpp:149
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl)
Definition: HW.cpp:260
MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute innerSymAttr)
Definition: HW.cpp:219
MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
Definition: HW.cpp:215
MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias)
Definition: HW.cpp:202
MlirType hwInOutTypeGetElementType(MlirType type)
Returns the element type of an inout type.
Definition: HW.cpp:67
MlirType hwTypeAliasTypeGet(MlirStringRef cScope, MlirStringRef cName, MlirType cInnerType)
Definition: HW.cpp:175
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:37
bool hwAttrIsAInnerSymAttr(MlirAttribute attr)
Definition: HW.cpp:211
MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute attr)
Definition: HW.cpp:282
bool hwTypeIsAStructType(MlirType type)
If the type is an HW struct.
Definition: HW.cpp:129
MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute attr)
Definition: HW.cpp:292
MlirType hwParamIntTypeGet(MlirAttribute parameter)
Definition: HW.cpp:55
MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index)
Get an HW module type's output name at a specific index.
Definition: HW.cpp:125
MLIR_CAPI_EXPORTED MlirAttribute hwOutputFileGetFromFileName(MlirAttribute fileName, bool excludeFromFileList, bool includeReplicatedOp)
Definition: HW.cpp:296
MlirAttribute hwInnerRefAttrGetName(MlirAttribute innerRefAttr)
Definition: HW.cpp:235
HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx)
Definition: HW.cpp:162
bool hwTypeIsAModuleType(MlirType type)
If the type is an HW module type.
Definition: HW.cpp:73
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute attr)
Definition: HW.cpp:264
int64_t hwGetBitWidth(MlirType type)
Return the hardware bit width of a type.
Definition: HW.cpp:35
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl)
Definition: HW.cpp:275
MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias)
Definition: HW.cpp:197
bool hwAttrIsAInnerRefAttr(MlirAttribute attr)
Definition: HW.cpp:224
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx, MlirStringRef cName)
Definition: HW.cpp:268
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute attr)
Definition: HW.cpp:243
MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index)
Get an HW module type's output type at a specific index.
Definition: HW.cpp:121
MlirAttribute hwParamIntTypeGetWidthAttr(MlirType type)
Definition: HW.cpp:59
intptr_t hwModuleTypeGetNumOutputs(MlirType type)
Get an HW module type's number of outputs.
Definition: HW.cpp:117
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl)
Definition: HW.cpp:254
MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text)
Definition: HW.cpp:285
MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl)
Definition: HW.cpp:278
@ Input
Definition: HW.h:32
@ Output
Definition: HW.h:32
@ InOut
Definition: HW.h:32
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition: OM.cpp:100
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
uint64_t getWidth(Type t)
Definition: ESIPasses.cpp:34
mlir::Type innerType(mlir::Type type)
Definition: ESITypes.cpp:184
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
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:102
circt::hw::InOutType InOutType
Definition: SVTypes.h:25
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21
Definition: hw.py:1
HWModulePortDirection dir
Definition: HW.h:38
MlirAttribute name
Definition: HW.h:36
MlirType type
Definition: HW.h:37
MlirIdentifier name
Definition: HW.h:27
MlirType type
Definition: HW.h:28