16#include "mlir/CAPI/IR.h"
17#include "mlir/CAPI/Registration.h"
18#include "mlir/CAPI/Support.h"
19#include "llvm/ADT/PostOrderIterator.h"
45 return wrap(ArrayType::get(
unwrap(element), size));
49 return wrap(cast<ArrayType>(
unwrap(type)).getElementType());
53 return cast<ArrayType>(
unwrap(type)).getNumElements();
59 return wrap(IntType::get(cast<TypedAttr>(
unwrap(parameter))));
63 return wrap(cast<IntType>(
unwrap(type)).getWidth());
71 return wrap(cast<InOutType>(
unwrap(type)).getElementType());
77 return isa<ModuleType>(
unwrap(type));
82 SmallVector<ModulePort> modulePorts;
83 for (intptr_t i = 0; i < numPorts; ++i) {
88 case HWModulePortDirection::Input:
89 dir = ModulePort::Direction::Input;
91 case HWModulePortDirection::Output:
92 dir = ModulePort::Direction::Output;
94 case HWModulePortDirection::InOut:
95 dir = ModulePort::Direction::InOut;
99 StringAttr name = cast<StringAttr>(
unwrap(port.
name));
102 modulePorts.push_back(
ModulePort{name, type, dir});
105 return wrap(ModuleType::get(
unwrap(ctx), modulePorts));
109 return cast<ModuleType>(
unwrap(type)).getNumInputs();
113 return wrap(cast<ModuleType>(
unwrap(type)).getInputType(index));
117 return wrap(cast<ModuleType>(
unwrap(type)).getInputName(index));
121 return cast<ModuleType>(
unwrap(type)).getNumOutputs();
125 return wrap(cast<ModuleType>(
unwrap(type)).getOutputType(index));
129 return wrap(cast<ModuleType>(
unwrap(type)).getOutputName(index));
133 auto port = cast<ModuleType>(
unwrap(type)).getPorts()[index];
137 case ModulePort::Direction::Input:
138 dir = HWModulePortDirection::Input;
140 case ModulePort::Direction::Output:
141 dir = HWModulePortDirection::Output;
143 case ModulePort::Direction::InOut:
144 dir = HWModulePortDirection::InOut;
148 ret->
name =
wrap(
static_cast<Attribute
>(port.name));
154 return isa<StructType>(
unwrap(type));
159 SmallVector<StructType::FieldInfo> fieldInfos;
162 fieldInfos.push_back(StructType::FieldInfo{
163 cast<StringAttr>(
unwrap(elements[i].name)),
unwrap(elements[i].type)});
165 return wrap(StructType::get(
unwrap(ctx), fieldInfos));
169 StructType st = cast<StructType>(
unwrap(structType));
170 return wrap(st.getFieldType(
unwrap(fieldName)));
174 MlirStringRef fieldName) {
175 StructType st = cast<StructType>(
unwrap(structType));
176 if (
auto idx = st.getFieldIndex(
unwrap(fieldName)))
177 return wrap(IntegerAttr::get(IntegerType::get(st.getContext(), 32), *idx));
178 return wrap(UnitAttr::get(st.getContext()));
182 StructType st = cast<StructType>(
unwrap(structType));
183 return st.getElements().size();
187 StructType st = cast<StructType>(
unwrap(structType));
188 auto cppField = st.getElements()[idx];
196 return isa<TypeAliasType>(
unwrap(type));
200 MlirType cInnerType) {
201 StringRef scope =
unwrap(cScope);
202 StringRef name =
unwrap(cName);
203 Type innerType =
unwrap(cInnerType);
204 FlatSymbolRefAttr nameRef =
205 FlatSymbolRefAttr::get(innerType.getContext(), name);
207 SymbolRefAttr::get(innerType.getContext(), scope, {nameRef});
208 return wrap(TypeAliasType::get(ref, innerType));
212 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
213 return wrap(type.getCanonicalType());
217 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
218 return wrap(type.getInnerType());
222 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
223 return wrap(type.getRef().getLeafReference().getValue());
227 TypeAliasType type = cast<TypeAliasType>(
unwrap(typeAlias));
228 return wrap(type.getRef().getRootReference().getValue());
236 return isa<InnerSymAttr>(
unwrap(attr));
240 return wrap(InnerSymAttr::get(cast<StringAttr>(
unwrap(symName))));
248 return wrap((Attribute)cast<InnerSymAttr>(
unwrap(innerSymAttr)).getSymName());
252 return isa<InnerRefAttr>(
unwrap(attr));
256 MlirAttribute innerSym) {
257 auto moduleNameAttr = cast<StringAttr>(
unwrap(moduleName));
258 auto innerSymAttr = cast<StringAttr>(
unwrap(innerSym));
259 return wrap(InnerRefAttr::get(moduleNameAttr, innerSymAttr));
263 return wrap((Attribute)cast<InnerRefAttr>(
unwrap(innerRefAttr)).getName());
267 return wrap((Attribute)cast<InnerRefAttr>(
unwrap(innerRefAttr)).getModule());
271 return isa<ParamDeclAttr>(
unwrap(attr));
275 MlirAttribute cValue) {
276 auto type =
unwrap(cType);
277 auto name = StringAttr::get(type.getContext(),
unwrap(cName));
279 ParamDeclAttr::get(type.getContext(), name, type,
unwrap(cValue)));
282 return wrap(cast<ParamDeclAttr>(
unwrap(decl)).getName().getValue());
285 return wrap(cast<ParamDeclAttr>(
unwrap(decl)).getType());
288 return wrap(cast<ParamDeclAttr>(
unwrap(decl)).getValue());
292 return isa<ParamDeclRefAttr>(
unwrap(attr));
296 MlirStringRef cName) {
297 auto name = StringAttr::get(
unwrap(ctx),
unwrap(cName));
298 return wrap(ParamDeclRefAttr::get(
unwrap(ctx), name,
299 IntegerType::get(
unwrap(ctx), 32)));
303 return wrap(cast<ParamDeclRefAttr>(
unwrap(decl)).getName().getValue());
306 return wrap(cast<ParamDeclRefAttr>(
unwrap(decl)).getType());
310 return isa<ParamVerbatimAttr>(
unwrap(attr));
313 auto textAttr = cast<StringAttr>(
unwrap(text));
314 MLIRContext *ctx = textAttr.getContext();
315 auto type = NoneType::get(ctx);
316 return wrap(ParamVerbatimAttr::get(ctx, textAttr, type));
320 return isa<OutputFileAttr>(
unwrap(attr));
322MLIR_CAPI_EXPORTED MlirAttribute
324 bool includeReplicatedOp) {
325 auto fileNameStrAttr = cast<StringAttr>(
unwrap(fileName));
326 return wrap(OutputFileAttr::getFromFilename(
327 fileNameStrAttr.getContext(), fileNameStrAttr.getValue(),
328 excludeFromFileList, includeReplicatedOp));
332 auto outputFileAttr = cast<OutputFileAttr>(
unwrap(outputFile));
333 return wrap(outputFileAttr.getFilename().getValue());
341 delete unwrap(instanceGraph);
344MLIR_CAPI_EXPORTED HWInstanceGraphNode
346 return wrap(
unwrap(instanceGraph)->getTopLevelNode());
349MLIR_CAPI_EXPORTED
void
354 for (
const auto &inst : llvm::post_order(graph)) {
355 callback(
wrap(inst), userData);
360 HWInstanceGraphNode rhs) {
364MLIR_CAPI_EXPORTED MlirModule
366 return wrap(dyn_cast<ModuleOp>(
unwrap(node)->getModule().getOperation()));
369MLIR_CAPI_EXPORTED MlirOperation
AIGLongestPathObject wrap(llvm::PointerUnion< Object *, DataflowPath::OutputPort * > object)
DEFINE_C_API_PTR_METHODS(AIGLongestPathHistory, llvm::ImmutableListImpl< DebugPoint >) llvm
MlirType uint64_t numElements
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)
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)
MlirStringRef hwOutputFileGetFileName(MlirAttribute outputFile)
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.
void hwModuleTypeGetPort(MlirType type, intptr_t index, HWModulePort *ret)
Get an HW module type's port info at a specific index.
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)
MlirAttribute hwInnerSymAttrGetEmpty(MlirContext ctx)
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.
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