15 #include "mlir/CAPI/IR.h"
16 #include "mlir/CAPI/Registration.h"
17 #include "mlir/CAPI/Support.h"
18 #include "llvm/ADT/PostOrderIterator.h"
20 using namespace circt;
47 return wrap(cast<ArrayType>(
unwrap(type)).getElementType());
51 return cast<ArrayType>(
unwrap(type)).getNumElements();
69 return wrap(cast<InOutType>(
unwrap(type)).getElementType());
75 return isa<ModuleType>(
unwrap(type));
80 SmallVector<ModulePort> modulePorts;
81 for (intptr_t i = 0; i < numPorts; ++i) {
97 StringAttr name = cast<StringAttr>(
unwrap(port.
name));
100 modulePorts.push_back(
ModulePort{name, type, dir});
107 return cast<ModuleType>(
unwrap(type)).getNumInputs();
111 return wrap(cast<ModuleType>(
unwrap(type)).getInputType(index));
115 return wrap(cast<ModuleType>(
unwrap(type)).getInputName(index));
119 return cast<ModuleType>(
unwrap(type)).getNumOutputs();
123 return wrap(cast<ModuleType>(
unwrap(type)).getOutputType(index));
127 return wrap(cast<ModuleType>(
unwrap(type)).getOutputName(index));
131 return isa<StructType>(
unwrap(type));
136 SmallVector<StructType::FieldInfo> fieldInfos;
139 fieldInfos.push_back(StructType::FieldInfo{
140 cast<StringAttr>(
unwrap(elements[i].name)),
unwrap(elements[i].type)});
146 StructType st = cast<StructType>(
unwrap(structType));
147 return wrap(st.getFieldType(
unwrap(fieldName)));
151 MlirStringRef fieldName) {
152 StructType st = cast<StructType>(
unwrap(structType));
153 if (
auto idx = st.getFieldIndex(
unwrap(fieldName)))
159 StructType st = cast<StructType>(
unwrap(structType));
160 return st.getElements().size();
164 StructType st = cast<StructType>(
unwrap(structType));
165 auto cppField = st.getElements()[idx];
173 return isa<TypeAliasType>(
unwrap(type));
177 MlirType cInnerType) {
178 StringRef scope =
unwrap(cScope);
179 StringRef name =
unwrap(cName);
181 FlatSymbolRefAttr nameRef =
189 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
190 return wrap(type.getCanonicalType());
194 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
195 return wrap(type.getInnerType());
199 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
200 return wrap(type.getRef().getLeafReference().getValue());
204 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
205 return wrap(type.getRef().getRootReference().getValue());
213 return isa<InnerSymAttr>(
unwrap(attr));
221 return wrap((Attribute)cast<InnerSymAttr>(
unwrap(innerSymAttr)).getSymName());
225 return isa<InnerRefAttr>(
unwrap(attr));
229 MlirAttribute innerSym) {
230 auto moduleNameAttr = cast<StringAttr>(
unwrap(moduleName));
231 auto innerSymAttr = cast<StringAttr>(
unwrap(innerSym));
240 return wrap((Attribute)cast<InnerRefAttr>(
unwrap(innerRefAttr)).getModule());
244 return isa<ParamDeclAttr>(
unwrap(attr));
248 MlirAttribute cValue) {
249 auto type =
unwrap(cType);
258 return wrap(cast<ParamDeclAttr>(
unwrap(decl)).getType());
261 return wrap(cast<ParamDeclAttr>(
unwrap(decl)).getValue());
265 return isa<ParamDeclRefAttr>(
unwrap(attr));
269 MlirStringRef cName) {
279 return wrap(cast<ParamDeclRefAttr>(
unwrap(decl)).getType());
283 return isa<ParamVerbatimAttr>(
unwrap(attr));
286 auto textAttr = cast<StringAttr>(
unwrap(text));
287 MLIRContext *ctx = textAttr.getContext();
293 return isa<OutputFileAttr>(
unwrap(attr));
295 MLIR_CAPI_EXPORTED MlirAttribute
297 bool includeReplicatedOp) {
298 auto fileNameStrAttr = cast<StringAttr>(
unwrap(fileName));
299 return wrap(OutputFileAttr::getFromFilename(
300 fileNameStrAttr.getContext(), fileNameStrAttr.getValue(),
301 excludeFromFileList, includeReplicatedOp));
309 delete unwrap(instanceGraph);
312 MLIR_CAPI_EXPORTED HWInstanceGraphNode
314 return wrap(
unwrap(instanceGraph)->getTopLevelNode());
317 MLIR_CAPI_EXPORTED
void
322 for (
const auto &inst : llvm::post_order(graph)) {
323 callback(
wrap(inst), userData);
328 HWInstanceGraphNode rhs) {
332 MLIR_CAPI_EXPORTED MlirModule
334 return wrap(dyn_cast<ModuleOp>(
unwrap(node)->getModule().getOperation()));
337 MLIR_CAPI_EXPORTED MlirOperation
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
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)
MlirAttribute hwInnerRefAttrGetModule(MlirAttribute innerRefAttr)
MLIR_CAPI_EXPORTED MlirType hwParamDeclAttrGetType(MlirAttribute decl)
MlirType hwInOutTypeGet(MlirType element)
Creates an HW inout type in the context associated with element.
MlirType hwArrayTypeGet(MlirType element, size_t size)
Creates a fixed-size HW array type in the context associated with element.
intptr_t hwArrayTypeGetSize(MlirType type)
returns the size of an array type
MlirType hwStructTypeGet(MlirContext ctx, intptr_t numElements, HWStructFieldInfo const *elements)
Creates an HW struct type in the context associated with the elements.
MlirType hwArrayTypeGetElementType(MlirType type)
returns the element type of an array type
MLIR_CAPI_EXPORTED HWInstanceGraphNode hwInstanceGraphGetTopLevelNode(HWInstanceGraph instanceGraph)
intptr_t hwModuleTypeGetNumInputs(MlirType type)
Get an HW module type's number of inputs.
MLIR_CAPI_EXPORTED MlirOperation hwInstanceGraphNodeGetModuleOp(HWInstanceGraphNode node)
intptr_t hwStructTypeGetNumFields(MlirType structType)
bool hwTypeIsAArrayType(MlirType type)
If the type is an HW array.
bool hwTypeIsATypeAliasType(MlirType type)
If the type is an HW type alias.
MlirType hwTypeAliasTypeGetInnerType(MlirType typeAlias)
MlirType hwTypeAliasTypeGetCanonicalType(MlirType typeAlias)
MlirType hwStructTypeGetField(MlirType structType, MlirStringRef fieldName)
MlirAttribute hwInnerRefAttrGet(MlirAttribute moduleName, MlirAttribute innerSym)
MLIR_CAPI_EXPORTED bool hwInstanceGraphNodeEqual(HWInstanceGraphNode lhs, HWInstanceGraphNode rhs)
MLIR_CAPI_EXPORTED MlirModule hwInstanceGraphNodeGetModule(HWInstanceGraphNode node)
MLIR_CAPI_EXPORTED HWInstanceGraph hwInstanceGraphGet(MlirOperation operation)
bool hwTypeIsAIntType(MlirType type)
If the type is an HW int.
MlirType hwModuleTypeGetInputType(MlirType type, intptr_t index)
Get an HW module type's input type at a specific index.
bool hwTypeIsAInOut(MlirType type)
If the type is an HW inout.
MlirStringRef hwModuleTypeGetInputName(MlirType type, intptr_t index)
Get an HW module type's input name at a specific index.
MlirType hwModuleTypeGet(MlirContext ctx, intptr_t numPorts, HWModulePort const *ports)
Creates an HW module type.
MlirAttribute hwStructTypeGetFieldIndex(MlirType structType, MlirStringRef fieldName)
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclAttrGetValue(MlirAttribute decl)
MlirAttribute hwInnerSymAttrGetSymName(MlirAttribute innerSymAttr)
MlirAttribute hwInnerSymAttrGet(MlirAttribute symName)
MLIR_CAPI_EXPORTED void hwInstanceGraphDestroy(HWInstanceGraph instanceGraph)
MLIR_CAPI_EXPORTED void hwInstanceGraphForEachNode(HWInstanceGraph instanceGraph, HWInstanceGraphNodeCallback callback, void *userData)
MlirStringRef hwTypeAliasTypeGetScope(MlirType typeAlias)
MlirType hwInOutTypeGetElementType(MlirType type)
Returns the element type of an inout type.
MlirType hwTypeAliasTypeGet(MlirStringRef cScope, MlirStringRef cName, MlirType cInnerType)
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 ...
bool hwAttrIsAInnerSymAttr(MlirAttribute attr)
MLIR_CAPI_EXPORTED bool hwAttrIsAParamVerbatimAttr(MlirAttribute attr)
bool hwTypeIsAStructType(MlirType type)
If the type is an HW struct.
MLIR_CAPI_EXPORTED bool hwAttrIsAOutputFileAttr(MlirAttribute attr)
MlirType hwParamIntTypeGet(MlirAttribute parameter)
MlirStringRef hwModuleTypeGetOutputName(MlirType type, intptr_t index)
Get an HW module type's output name at a specific index.
MLIR_CAPI_EXPORTED MlirAttribute hwOutputFileGetFromFileName(MlirAttribute fileName, bool excludeFromFileList, bool includeReplicatedOp)
MlirAttribute hwInnerRefAttrGetName(MlirAttribute innerRefAttr)
HWStructFieldInfo hwStructTypeGetFieldNum(MlirType structType, unsigned idx)
bool hwTypeIsAModuleType(MlirType type)
If the type is an HW module type.
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclRefAttr(MlirAttribute attr)
int64_t hwGetBitWidth(MlirType type)
Return the hardware bit width of a type.
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclRefAttrGetName(MlirAttribute decl)
MlirStringRef hwTypeAliasTypeGetName(MlirType typeAlias)
bool hwAttrIsAInnerRefAttr(MlirAttribute attr)
MLIR_CAPI_EXPORTED MlirAttribute hwParamDeclRefAttrGet(MlirContext ctx, MlirStringRef cName)
MLIR_CAPI_EXPORTED bool hwAttrIsAParamDeclAttr(MlirAttribute attr)
MlirType hwModuleTypeGetOutputType(MlirType type, intptr_t index)
Get an HW module type's output type at a specific index.
MlirAttribute hwParamIntTypeGetWidthAttr(MlirType type)
intptr_t hwModuleTypeGetNumOutputs(MlirType type)
Get an HW module type's number of outputs.
MLIR_CAPI_EXPORTED MlirStringRef hwParamDeclAttrGetName(MlirAttribute decl)
MLIR_CAPI_EXPORTED MlirAttribute hwParamVerbatimAttrGet(MlirAttribute text)
MLIR_CAPI_EXPORTED MlirType hwParamDeclRefAttrGetType(MlirAttribute decl)
void(* HWInstanceGraphNodeCallback)(HWInstanceGraphNode, void *)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
HW-specific instance graph with a virtual entry node linking to all publicly visible modules.
This is a Node in the InstanceGraph.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
uint64_t getWidth(Type t)
mlir::Type innerType(mlir::Type type)
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.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
HWModulePortDirection dir