Skip to content

Commit

Permalink
[FIRRTL] Update LowerClasses to handle AnyRef and AnyRef cast. (#6117)
Browse files Browse the repository at this point in the history
The FIRRTL AnyRef type is converted to an OM Any type, and FIRRTL
AnyRef casts are converted to OM Any casts. This is a straightforward
conversion of the same concepts into the OM dialect.
  • Loading branch information
mikeurbach authored Sep 13, 2023
1 parent 46d373b commit bec964d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Dialect/FIRRTL/Transforms/LowerClasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,17 @@ struct WireOpConversion : public OpConversionPattern<WireOp> {
}
};

struct AnyCastOpConversion : public OpConversionPattern<ObjectAnyRefCastOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(ObjectAnyRefCastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<AnyCastOp>(op, adaptor.getInput());
return success();
}
};

struct ObjectSubfieldOpConversion
: public OpConversionPattern<firrtl::ObjectSubfieldOp> {
using OpConversionPattern::OpConversionPattern;
Expand Down Expand Up @@ -1088,6 +1099,12 @@ static void populateTypeConverter(TypeConverter &converter) {
return om::ClassType::get(type.getContext(), type.getNameAttr());
});

// Convert FIRRTL AnyRef type to OM Any type.
converter.addConversion([](om::AnyType type) { return type; });
converter.addConversion([](firrtl::AnyRefType type) {
return om::AnyType::get(type.getContext());
});

// Convert FIRRTL List type to OM List type.
auto convertListType = [&converter](auto type) -> std::optional<mlir::Type> {
auto elementType = converter.convertType(type.getElementType());
Expand Down Expand Up @@ -1162,6 +1179,7 @@ static void populateRewritePatterns(
patterns.add<PathOpConversion>(converter, patterns.getContext(),
pathInfoTable);
patterns.add<WireOpConversion>(converter, patterns.getContext());
patterns.add<AnyCastOpConversion>(converter, patterns.getContext());
patterns.add<ObjectSubfieldOpConversion>(converter, patterns.getContext(),
classTypeTable);
patterns.add<ClassFieldOpConversion>(converter, patterns.getContext());
Expand Down
14 changes: 14 additions & 0 deletions test/Dialect/FIRRTL/lower-classes.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,17 @@ firrtl.circuit "ModuleInstances" {
// CHECK: %[[F1:.+]] = om.object.field %[[O1]], [@outputProp]
// CHECK: om.class.field @outputProp, %[[F1]]
}

// CHECK-LABEL: firrtl.circuit "AnyCast"
firrtl.circuit "AnyCast" {
firrtl.class private @Foo() {}

firrtl.module @AnyCast(out %foo: !firrtl.anyref) {
// CHECK: %[[OBJ:.+]] = om.object @Foo
%fooObject = firrtl.object @Foo()
// CHECK: %[[CAST:.+]] = om.any_cast %[[OBJ]]
%0 = firrtl.object.anyref_cast %fooObject : !firrtl.class<@Foo()>
// CHECK: om.class.field @foo, %[[CAST]]
firrtl.propassign %foo, %0 : !firrtl.anyref
}
}

0 comments on commit bec964d

Please sign in to comment.