19 #include "mlir/CAPI/IR.h"
20 #include "mlir/CAPI/Registration.h"
21 #include "mlir/CAPI/Support.h"
22 #include "llvm/Support/JSON.h"
24 using namespace circt;
25 using namespace firrtl;
34 circt::firrtl::FIRRTLDialect)
76 return isa<AsyncResetType>(
unwrap(type));
84 return isa<AnalogType>(
unwrap(type));
92 return isa<FVectorType>(
unwrap(type));
96 auto baseType = cast<FIRRTLBaseType>(
unwrap(element));
97 assert(baseType &&
"element must be base type");
103 return wrap(cast<FVectorType>(
unwrap(vec)).getElementType());
107 return cast<FVectorType>(
unwrap(vec)).getNumElements();
111 return isa<BundleType>(
unwrap(type));
115 return isa<OpenBundleType>(
unwrap(type));
120 bool bundleCompatible =
true;
121 SmallVector<OpenBundleType::BundleElement, 4> bundleFields;
123 bundleFields.reserve(count);
125 for (
size_t i = 0; i < count; i++) {
126 auto field = fields[i];
127 auto type = cast<FIRRTLType>(
unwrap(field.type));
128 bundleFields.emplace_back(
unwrap(field.name), field.isFlip, type);
129 bundleCompatible &= isa<BundleType::ElementType>(type);
133 if (bundleCompatible) {
134 auto bundleFieldsMapped = llvm::map_range(bundleFields, [](
auto field) {
135 return BundleType::BundleElement{
136 field.name, field.isFlip, cast<BundleType::ElementType>(field.type)};
145 if (
auto bundleType = dyn_cast<BundleType>(
unwrap(bundle))) {
146 return bundleType.getNumElements();
147 }
else if (
auto bundleType = dyn_cast<OpenBundleType>(
unwrap(bundle))) {
148 return bundleType.getNumElements();
150 llvm_unreachable(
"must be a bundle type");
156 auto unwrapped =
unwrap(type);
158 auto cvt = [field](
auto element) {
160 field->
isFlip = element.isFlip;
163 if (
auto bundleType = dyn_cast<BundleType>(unwrapped)) {
164 cvt(bundleType.getElement(index));
166 }
else if (
auto bundleType = dyn_cast<OpenBundleType>(unwrapped)) {
167 cvt(bundleType.getElement(index));
174 std::optional<unsigned> fieldIndex;
175 if (
auto bundleType = dyn_cast<BundleType>(
unwrap(type))) {
176 fieldIndex = bundleType.getElementIndex(
unwrap(fieldName));
177 }
else if (
auto bundleType = dyn_cast<OpenBundleType>(
unwrap(type))) {
178 fieldIndex = bundleType.getElementIndex(
unwrap(fieldName));
180 llvm_unreachable(
"must be a bundle type");
182 assert(fieldIndex.has_value() &&
"unknown field");
183 return fieldIndex.value();
189 auto baseType = dyn_cast<FIRRTLBaseType>(
unwrap(target));
190 assert(baseType &&
"target must be base type");
196 return isa<AnyRefType>(
unwrap(type));
204 return isa<FIntegerType>(
unwrap(type));
212 return isa<DoubleType>(
unwrap(type));
220 return isa<StringType>(
unwrap(type));
243 assert(type &&
"element must be property type");
251 size_t numberOfElements,
253 auto nameSymbol = dyn_cast<FlatSymbolRefAttr>(
unwrap(name));
254 assert(nameSymbol &&
"name must be FlatSymbolRefAttr");
256 SmallVector<ClassElement, 4> classElements;
257 classElements.reserve(numberOfElements);
259 for (
size_t i = 0; i < numberOfElements; i++) {
260 auto element = elements[i];
263 classElements.emplace_back(
unwrap(element.name),
unwrap(element.type), dir);
269 auto baseType = type_dyn_cast<FIRRTLBaseType>(
unwrap(type));
270 assert(baseType &&
"unexpected type, must be base type");
271 return wrap(baseType.getMaskType());
282 switch (convention) {
284 value = Convention::Internal;
287 value = Convention::Scalarized;
297 static_cast<std::underlying_type_t<Direction>
>(
Direction::In));
304 unwrap(ctx), ArrayRef(
reinterpret_cast<const Direction *
>(dirs), count)));
308 MlirType type, MlirAttribute value) {
318 value = NameKindEnum::DroppableName;
321 value = NameKindEnum::InterestingName;
333 value = RUWAttr::Undefined;
336 value = RUWAttr::Old;
339 value = RUWAttr::New;
347 bool isBinary,
bool isInline) {
357 value = MemDirAttr::Infer;
360 value = MemDirAttr::Read;
363 value = MemDirAttr::Write;
366 value = MemDirAttr::ReadWrite;
377 switch (eventControl) {
379 value = EventControl::AtPosEdge;
382 value = EventControl::AtNegEdge;
385 value = EventControl::AtEdge;
393 MlirStringRef str, uint8_t radix) {
394 auto value = APInt{numBits,
unwrap(str), radix};
418 switch (flowResult) {
428 llvm_unreachable(
"invalid flow");
432 MlirContext ctx, MlirStringRef annotationsStr,
433 MlirAttribute *importedAnnotationsArray) {
434 auto annotations = json::parse(
unwrap(annotationsStr));
439 auto *ctxUnwrapped =
unwrap(ctx);
441 json::Path::Root root;
442 SmallVector<Attribute> annos;
assert(baseType &&"element must be base type")
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
bool firrtlTypeIsAUInt(MlirType type)
Checks if this type is a unsigned integer type.
bool firrtlTypeIsASInt(MlirType type)
Checks if this type is a signed integer type.
bool firrtlTypeIsARef(MlirType type)
Checks if this type is a ref type.
bool firrtlTypeIsAString(MlirType type)
Checks if this type is a property string type.
MlirAttribute firrtlAttrGetMemDir(MlirContext ctx, FIRRTLMemDir dir)
Creates a MemDirAttr with the specified memory port direction.
MlirType firrtlTypeGetAsyncReset(MlirContext ctx)
Creates an async reset type.
MlirType firrtlTypeGetList(MlirContext ctx, MlirType elementType)
Creates a property list type with the specified element type.
bool firrtlTypeIsAClass(MlirType type)
Checks if this type is a class type.
bool firrtlTypeGetBundleFieldByIndex(MlirType type, size_t index, FIRRTLBundleField *field)
Returns the field at the specified index in the bundle type.
MlirAttribute firrtlAttrGetIntegerFromString(MlirType type, unsigned numBits, MlirStringRef str, uint8_t radix)
Creates an IntegerAttr from a string representation of integer.
MlirType firrtlTypeGetBoolean(MlirContext ctx)
Creates a property boolean type.
MlirType firrtlTypeGetDouble(MlirContext ctx)
Creates a property double type.
MlirType firrtlTypeGetPath(MlirContext ctx)
Creates a property path type.
MlirType firrtlTypeGetConstType(MlirType type, bool isConst)
Returns a const or non-const version of this type.
MlirType firrtlTypeGetString(MlirContext ctx)
Creates a property string type.
bool firrtlTypeIsAClock(MlirType type)
Checks if this type is a clock type.
MlirType firrtlTypeGetAnalog(MlirContext ctx, int32_t width)
Creates an analog type with the specified width.
MlirType firrtlTypeGetInteger(MlirContext ctx)
Creates a property integer type.
bool firrtlTypeIsAInteger(MlirType type)
Checks if this type is a property integer type.
MlirAttribute firrtlAttrGetConvention(MlirContext ctx, FIRRTLConvention convention)
Creates an ConventionAttr with the specified value.
MlirType firrtlTypeGetClock(MlirContext ctx)
Creates a clock type.
MlirType firrtlTypeGetReset(MlirContext ctx)
Creates a reset type.
bool firrtlTypeIsABundle(MlirType type)
Returns true if the specified type is a bundle type.
MlirType firrtlTypeGetVector(MlirContext ctx, MlirType element, size_t count)
Creates a vector type with the specified element type and count.
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(FIRRTL, firrtl, circt::firrtl::FIRRTLDialect) bool firrtlTypeIsConst(MlirType type)
bool firrtlImportAnnotationsFromJSONRaw(MlirContext ctx, MlirStringRef annotationsStr, MlirAttribute *importedAnnotationsArray)
Deserializes a JSON value into FIRRTL Annotations.
bool firrtlTypeIsAPath(MlirType type)
Checks if this type is a property path type.
MlirType firrtlTypeGetUInt(MlirContext ctx, int32_t width)
Creates a unsigned integer type with the specified width.
MlirType firrtlTypeGetAnyRef(MlirContext ctx)
Creates an anyref type.
MlirAttribute firrtlAttrGetPortDirs(MlirContext ctx, size_t count, const FIRRTLDirection *dirs)
Creates a DenseBoolArrayAttr with the specified port directions.
bool firrtlTypeIsAReset(MlirType type)
Checks if this type is a reset type.
MlirType firrtlTypeGetClass(MlirContext ctx, MlirAttribute name, size_t numberOfElements, const FIRRTLClassElement *elements)
Creates a class type with the specified name and elements.
bool firrtlTypeIsAAnyRef(MlirType type)
Checks if this type is an anyref type.
bool firrtlTypeIsAOpenBundle(MlirType type)
Returns true if the specified type is an open bundle type.
MlirType firrtlTypeGetMaskType(MlirType type)
Returns this type with all ground types replaced with UInt<1>.
MlirType firrtlTypeGetSInt(MlirContext ctx, int32_t width)
Creates a signed integer type with the specified width.
MlirType firrtlTypeGetRef(MlirType target, bool forceable)
Creates a ref type.
MlirType firrtlTypeGetBundle(MlirContext ctx, size_t count, const FIRRTLBundleField *fields)
Creates a bundle type with the specified fields.
bool firrtlTypeIsAVector(MlirType type)
Checks if this type is a vector type.
int64_t firrtlTypeGetBitWidth(MlirType type, bool ignoreFlip)
Gets the bit width for this type, returns -1 if unknown.
MlirType firrtlTypeGetVectorElement(MlirType vec)
Returns the element type of a vector type.
MlirAttribute firrtlAttrGetEventControl(MlirContext ctx, FIRRTLEventControl eventControl)
Creates a EventControlAttr with the specified value.
bool firrtlTypeIsADouble(MlirType type)
Checks if this type is a property double type.
bool firrtlTypeIsAList(MlirType type)
Checks if this type is a property list type.
MlirAttribute firrtlAttrGetParamDecl(MlirContext ctx, MlirIdentifier name, MlirType type, MlirAttribute value)
Creates a ParamDeclAttr with the specified name, type, and value.
MlirAttribute firrtlAttrGetMemInit(MlirContext ctx, MlirIdentifier filename, bool isBinary, bool isInline)
Creates a MemoryInitAttr with the specified memory initialization information.
FIRRTLValueFlow firrtlValueFoldFlow(MlirValue value, FIRRTLValueFlow flow)
Computes the flow for a Value, value, as determined by the FIRRTL specification.
MlirAttribute firrtlAttrGetRUW(MlirContext ctx, FIRRTLRUW ruw)
Creates a RUWAttr with the specified Read-Under-Write Behaviour.
bool firrtlTypeIsAAsyncReset(MlirType type)
Checks if this type is an async reset type.
MlirAttribute firrtlAttrGetNameKind(MlirContext ctx, FIRRTLNameKind nameKind)
Creates a NameKindEnumAttr with the specified name preservation semantic.
bool firrtlTypeIsAAnalog(MlirType type)
Checks if this type is an analog type.
bool firrtlTypeIsABoolean(MlirType type)
Checks if this type is a property boolean type.
size_t firrtlTypeGetVectorNumElements(MlirType vec)
Returns the number of elements in a vector type.
unsigned firrtlTypeGetBundleFieldIndex(MlirType type, MlirStringRef fieldName)
Returns the index of the field with the specified name in the bundle type.
size_t firrtlTypeGetBundleNumFields(MlirType bundle)
Returns the number of fields in the bundle type.
MLIR_CAPI_EXPORTED bool firrtlTypeIsConst(MlirType type)
Returns true if this is a const type whose value is guaranteed to be unchanging at circuit execution ...
FIRRTLValueFlow
Flow of value.
@ FIRRTL_VALUE_FLOW_SOURCE
@ FIRRTL_VALUE_FLOW_DUPLEX
FIRRTLNameKind
Name preservation.
@ FIRRTL_NAME_KIND_DROPPABLE_NAME
@ FIRRTL_NAME_KIND_INTERESTING_NAME
FIRRTLMemDir
Memory port direction.
@ FIRRTL_MEM_DIR_READ_WRITE
FIRRTLEventControl
Edge control trigger.
@ FIRRTL_EVENT_CONTROL_AT_POS_EDGE
@ FIRRTL_EVENT_CONTROL_AT_NEG_EDGE
@ FIRRTL_EVENT_CONTROL_AT_EDGE
FIRRTLDirection
Port direction.
FIRRTLRUW
Read-Under-Write behaviour.
FIRRTLConvention
Module instantiation conventions.
@ 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 isConst(Type type)
Returns true if this is a 'const' type whose value is guaranteed to be unchanging at circuit executio...
bool importAnnotationsFromJSONRaw(llvm::json::Value &value, SmallVectorImpl< Attribute > &annotations, llvm::json::Path path, MLIRContext *context)
Deserialize a JSON value into FIRRTL Annotations.
std::optional< int64_t > getBitWidth(FIRRTLBaseType type, bool ignoreFlip=false)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Describes a field in a bundle type.
Describes an element in a class type.
FIRRTLDirection direction