15 #include "mlir/CAPI/IR.h"
16 #include "mlir/CAPI/Registration.h"
17 #include "mlir/CAPI/Support.h"
18 #include "llvm/Support/JSON.h"
20 using namespace circt;
21 using namespace firrtl;
30 circt::firrtl::FIRRTLDialect)
61 auto baseType = cast<FIRRTLBaseType>(
unwrap(element));
62 assert(baseType &&
"element must be base type");
68 return isa<OpenBundleType>(
unwrap(type));
73 bool bundleCompatible =
true;
74 SmallVector<OpenBundleType::BundleElement, 4> bundleFields;
76 bundleFields.reserve(count);
78 for (
size_t i = 0; i < count; i++) {
79 auto field = fields[i];
80 auto type = cast<FIRRTLType>(
unwrap(field.type));
81 bundleFields.emplace_back(
unwrap(field.name), field.isFlip, type);
82 bundleCompatible &= isa<BundleType::ElementType>(type);
86 if (bundleCompatible) {
87 auto bundleFieldsMapped = llvm::map_range(bundleFields, [](
auto field) {
88 return BundleType::BundleElement{
89 field.name, field.isFlip, cast<BundleType::ElementType>(field.type)};
98 std::optional<unsigned> fieldIndex;
99 if (
auto bundleType = dyn_cast<BundleType>(
unwrap(type))) {
100 fieldIndex = bundleType.getElementIndex(
unwrap(fieldName));
101 }
else if (
auto bundleType = dyn_cast<OpenBundleType>(
unwrap(type))) {
102 fieldIndex = bundleType.getElementIndex(
unwrap(fieldName));
104 llvm_unreachable(
"must be a bundle type");
106 assert(fieldIndex.has_value() &&
"unknown field");
107 return fieldIndex.value();
111 auto baseType = dyn_cast<FIRRTLBaseType>(
unwrap(target));
112 assert(baseType &&
"target must be base type");
143 assert(type &&
"element must be property type");
149 size_t numberOfElements,
151 auto nameSymbol = dyn_cast<FlatSymbolRefAttr>(
unwrap(name));
152 assert(nameSymbol &&
"name must be FlatSymbolRefAttr");
154 SmallVector<ClassElement, 4> classElements;
155 classElements.reserve(numberOfElements);
157 for (
size_t i = 0; i < numberOfElements; i++) {
158 auto element = elements[i];
161 classElements.emplace_back(
unwrap(element.name),
unwrap(element.type), dir);
167 auto baseType = type_dyn_cast<FIRRTLBaseType>(
unwrap(type));
168 assert(baseType &&
"unexpected type, must be base type");
169 return wrap(baseType.getMaskType());
180 switch (convention) {
182 value = Convention::Internal;
185 value = Convention::Scalarized;
195 static_cast<std::underlying_type_t<Direction>
>(
Direction::In));
202 unwrap(ctx), ArrayRef(
reinterpret_cast<const Direction *
>(dirs), count)));
206 MlirType type, MlirAttribute value) {
216 value = NameKindEnum::DroppableName;
219 value = NameKindEnum::InterestingName;
231 value = RUWAttr::Undefined;
234 value = RUWAttr::Old;
237 value = RUWAttr::New;
245 bool isBinary,
bool isInline) {
255 value = MemDirAttr::Infer;
258 value = MemDirAttr::Read;
261 value = MemDirAttr::Write;
264 value = MemDirAttr::ReadWrite;
275 switch (eventControl) {
277 value = EventControl::AtPosEdge;
280 value = EventControl::AtNegEdge;
283 value = EventControl::AtEdge;
291 MlirStringRef str, uint8_t radix) {
292 auto value = APInt{numBits,
unwrap(str), radix};
316 switch (flowResult) {
326 llvm_unreachable(
"invalid flow");
330 MlirContext ctx, MlirStringRef annotationsStr,
331 MlirAttribute *importedAnnotationsArray) {
332 auto annotations = json::parse(
unwrap(annotationsStr));
337 auto *ctxUnwrapped =
unwrap(ctx);
339 json::Path::Root root;
340 SmallVector<Attribute> annos;
assert(baseType &&"element must be base type")
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirAttribute firrtlAttrGetMemDir(MlirContext ctx, FIRRTLMemDir dir)
MlirType firrtlTypeGetAsyncReset(MlirContext ctx)
MlirType firrtlTypeGetList(MlirContext ctx, MlirType elementType)
MlirAttribute firrtlAttrGetIntegerFromString(MlirType type, unsigned numBits, MlirStringRef str, uint8_t radix)
MlirType firrtlTypeGetBoolean(MlirContext ctx)
MlirType firrtlTypeGetDouble(MlirContext ctx)
MlirType firrtlTypeGetPath(MlirContext ctx)
MlirType firrtlTypeGetString(MlirContext ctx)
MlirType firrtlTypeGetAnalog(MlirContext ctx, int32_t width)
MlirType firrtlTypeGetInteger(MlirContext ctx)
MlirAttribute firrtlAttrGetConvention(MlirContext ctx, FIRRTLConvention convention)
MlirType firrtlTypeGetClock(MlirContext ctx)
MlirType firrtlTypeGetReset(MlirContext ctx)
MlirType firrtlTypeGetVector(MlirContext ctx, MlirType element, size_t count)
bool firrtlImportAnnotationsFromJSONRaw(MlirContext ctx, MlirStringRef annotationsStr, MlirAttribute *importedAnnotationsArray)
MlirType firrtlTypeGetAnyRef(MlirContext ctx)
MlirAttribute firrtlAttrGetPortDirs(MlirContext ctx, size_t count, const FIRRTLDirection *dirs)
MlirType firrtlTypeGetClass(MlirContext ctx, MlirAttribute name, size_t numberOfElements, const FIRRTLClassElement *elements)
bool firrtlTypeIsAOpenBundle(MlirType type)
MlirType firrtlTypeGetMaskType(MlirType type)
MlirType firrtlTypeGetSInt(MlirContext ctx, int32_t width)
MlirType firrtlTypeGetRef(MlirType target, bool forceable)
MlirType firrtlTypeGetBundle(MlirContext ctx, size_t count, const FIRRTLBundleField *fields)
MlirAttribute firrtlAttrGetEventControl(MlirContext ctx, FIRRTLEventControl eventControl)
MlirAttribute firrtlAttrGetParamDecl(MlirContext ctx, MlirIdentifier name, MlirType type, MlirAttribute value)
MlirAttribute firrtlAttrGetMemInit(MlirContext ctx, MlirIdentifier filename, bool isBinary, bool isInline)
FIRRTLValueFlow firrtlValueFoldFlow(MlirValue value, FIRRTLValueFlow flow)
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(FIRRTL, firrtl, circt::firrtl::FIRRTLDialect) MlirType firrtlTypeGetUInt(MlirContext ctx
MlirAttribute firrtlAttrGetRUW(MlirContext ctx, FIRRTLRUW ruw)
MlirAttribute firrtlAttrGetNameKind(MlirContext ctx, FIRRTLNameKind nameKind)
unsigned firrtlTypeGetBundleFieldIndex(MlirType type, MlirStringRef fieldName)
@ FIRRTL_VALUE_FLOW_SOURCE
@ FIRRTL_VALUE_FLOW_DUPLEX
@ FIRRTL_NAME_KIND_DROPPABLE_NAME
@ FIRRTL_NAME_KIND_INTERESTING_NAME
@ FIRRTL_MEM_DIR_READ_WRITE
MLIR_CAPI_EXPORTED MlirType firrtlTypeGetUInt(MlirContext ctx, int32_t width)
@ FIRRTL_EVENT_CONTROL_AT_POS_EDGE
@ FIRRTL_EVENT_CONTROL_AT_NEG_EDGE
@ FIRRTL_EVENT_CONTROL_AT_EDGE
@ FIRRTL_CONVENTION_SCALARIZED
@ FIRRTL_CONVENTION_INTERNAL
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
mlir::DenseBoolArrayAttr packAttribute(MLIRContext *context, ArrayRef< Direction > directions)
Return a DenseBoolArrayAttr containing the packed representation of an array of directions.
Direction
This represents the direction of a single port.
Flow foldFlow(Value val, Flow accumulatedFlow=Flow::Source)
Compute the flow for a Value, val, as determined by the FIRRTL specification.
bool importAnnotationsFromJSONRaw(llvm::json::Value &value, SmallVectorImpl< Attribute > &annotations, llvm::json::Path path, MLIRContext *context)
Deserialize a JSON value into FIRRTL Annotations.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
FIRRTLDirection direction