Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ExtractInstances] Add the extract instances metadata to OM Classes #7667

Merged
merged 9 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion include/circt/Dialect/FIRRTL/FIRRTLStructure.td
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,23 @@ def ClassOp : FIRRTLModuleLike<"class", [
let builders = [
OpBuilder<(ins
"StringAttr":$name,
"ArrayRef<PortInfo>":$ports)>];
"ArrayRef<PortInfo>":$ports)>,
// This builds a ClassOp, with the specified fieldNames and fieldTypes as
// ports. The output property is set from the input property port.
OpBuilder<(ins
"Twine":$name,
"mlir::ArrayRef<mlir::StringRef>":$fieldNames,
"mlir::ArrayRef<mlir::Type>":$fieldTypes)>
];

let extraModuleClassDeclaration = [{
Block *getBodyBlock() { return &getBody().front(); }

ClassOp static buildSimpleClassOp(
mlir::OpBuilder &odsBuilder, mlir::Location loc, mlir::Twine name,
mlir::ArrayRef<mlir::StringRef> fieldNames,
mlir::ArrayRef<mlir::Type> fieldTypes);

using iterator = Block::iterator;
iterator begin() { return getBodyBlock()->begin(); }
iterator end() { return getBodyBlock()->end(); }
Expand Down
8 changes: 8 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ static ResultTy transformReduce(MLIRContext *context, RangeTy &&r,
/// Truncate `a` to the common prefix of `a` and `b`.
void makeCommonPrefix(SmallString<64> &a, StringRef b);

//===----------------------------------------------------------------------===//
// Object related utilities
//===----------------------------------------------------------------------===//

/// Add the tracker annotation to the op and get a PathOp to the op.
PathOp createPathRef(Operation *op, hw::HierPathOp nla,
mlir::ImplicitLocOpBuilder &builderOM);

} // namespace firrtl
} // namespace circt

Expand Down
24 changes: 24 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,30 @@ void ClassOp::build(OpBuilder &builder, OperationState &result, StringAttr name,
body->addArgument(elt.type, elt.loc);
}

void ClassOp::build(::mlir::OpBuilder &odsBuilder,
::mlir::OperationState &odsState, Twine name,
mlir::ArrayRef<mlir::StringRef> fieldNames,
mlir::ArrayRef<mlir::Type> fieldTypes) {

SmallVector<PortInfo, 10> ports;
for (auto [fieldName, fieldType] : llvm::zip(fieldNames, fieldTypes)) {
ports.emplace_back(odsBuilder.getStringAttr(fieldName + "_in"), fieldType,
Direction::In);
ports.emplace_back(odsBuilder.getStringAttr(fieldName), fieldType,
Direction::Out);
}
build(odsBuilder, odsState, odsBuilder.getStringAttr(name), ports);
// Create a region and a block for the body.
auto &body = odsState.regions[0]->getBlocks().front();
auto prevLoc = odsBuilder.saveInsertionPoint();
odsBuilder.setInsertionPointToEnd(&body);
auto args = body.getArguments();
auto loc = odsState.location;
for (unsigned i = 0, e = ports.size(); i != e; i += 2)
odsBuilder.create<PropAssignOp>(loc, args[i + 1], args[i]);

odsBuilder.restoreInsertionPoint(prevLoc);
}
void ClassOp::print(OpAsmPrinter &p) {
printClassLike(p, cast<ClassLike>(getOperation()));
}
Expand Down
24 changes: 24 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,3 +1075,27 @@ void circt::firrtl::makeCommonPrefix(SmallString<64> &a, StringRef b) {
while (!a.empty() && !a.ends_with(sep))
a.pop_back();
}

PathOp circt::firrtl::createPathRef(Operation *op, hw::HierPathOp nla,
mlir::ImplicitLocOpBuilder &builderOM) {

auto *context = op->getContext();
auto id = DistinctAttr::create(UnitAttr::get(context));
TargetKind kind = TargetKind::Reference;
// If op is null, then create an empty path.
if (op) {
NamedAttrList fields;
fields.append("id", id);
fields.append("class", StringAttr::get(context, "circt.tracker"));
if (nla)
fields.append("circt.nonlocal", mlir::FlatSymbolRefAttr::get(nla));
AnnotationSet annos(op);
annos.addAnnotations(DictionaryAttr::get(context, fields));
annos.applyToOperation(op);
if (isa<InstanceOp, FModuleLike>(op))
kind = TargetKind::Instance;
}

// Create the path operation.
return builderOM.create<PathOp>(kind, id);
}
46 changes: 8 additions & 38 deletions lib/Dialect/FIRRTL/Transforms/CreateSiFiveMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,6 @@ struct ObjectModelIR {
return builderOM.create<PathOp>(kind, id);
}

// Create a ClassOp, with the specified fieldNames and fieldTypes as ports.
// The output property is set from the input property port.
ClassOp buildSimpleClassOp(OpBuilder &odsBuilder, Location loc, Twine name,
ArrayRef<StringRef> fieldNames,
ArrayRef<Type> fieldTypes) {
SmallVector<PortInfo, 10> ports;
for (auto [fieldName, fieldType] : llvm::zip(fieldNames, fieldTypes)) {
ports.emplace_back(odsBuilder.getStringAttr(fieldName + "_in"), fieldType,
Direction::In);
ports.emplace_back(odsBuilder.getStringAttr(fieldName), fieldType,
Direction::Out);
}

ClassOp classOp =
odsBuilder.create<ClassOp>(loc, odsBuilder.getStringAttr(name), ports);
Block *body = classOp.getBodyBlock();
auto prevLoc = odsBuilder.saveInsertionPoint();
odsBuilder.setInsertionPointToEnd(body);
auto args = body->getArguments();
for (unsigned i = 0, e = ports.size(); i != e; i += 2)
odsBuilder.create<PropAssignOp>(loc, args[i + 1], args[i]);

odsBuilder.restoreInsertionPoint(prevLoc);

return classOp;
}

void createMemorySchema() {

auto unknownLoc = mlir::UnknownLoc::get(context);
Expand All @@ -118,9 +91,8 @@ struct ObjectModelIR {
};
StringRef extraPortFields[3] = {"name", "direction", "width"};

extraPortsClass =
buildSimpleClassOp(builderOM, unknownLoc, "ExtraPortsMemorySchema",
extraPortFields, extraPortsType);
extraPortsClass = builderOM.create<ClassOp>(
"ExtraPortsMemorySchema", extraPortFields, extraPortsType);

mlir::Type classFieldTypes[13] = {
StringType::get(context),
Expand All @@ -140,9 +112,8 @@ struct ObjectModelIR {
ListType::get(context, cast<PropertyType>(StringType::get(context))),
};

memorySchemaClass =
buildSimpleClassOp(builderOM, unknownLoc, "MemorySchema",
memoryParamNames, classFieldTypes);
memorySchemaClass = builderOM.create<ClassOp>(
"MemorySchema", memoryParamNames, classFieldTypes);

// Now create the class that will instantiate metadata class with all the
// memories of the circt.
Expand All @@ -156,9 +127,8 @@ struct ObjectModelIR {
auto builderOM = mlir::ImplicitLocOpBuilder::atBlockEnd(
unknownLoc, circtOp.getBodyBlock());
Type classFieldTypes[] = {StringType::get(context)};
retimeModulesSchemaClass =
buildSimpleClassOp(builderOM, unknownLoc, "RetimeModulesSchema",
retimeModulesParamNames, classFieldTypes);
retimeModulesSchemaClass = builderOM.create<ClassOp>(
"RetimeModulesSchema", retimeModulesParamNames, classFieldTypes);

SmallVector<PortInfo> mports;
retimeModulesMetadataClass = builderOM.create<ClassOp>(
Expand Down Expand Up @@ -196,8 +166,8 @@ struct ObjectModelIR {
unknownLoc, circtOp.getBodyBlock());
Type classFieldTypes[] = {StringType::get(context)};
blackBoxModulesSchemaClass =
buildSimpleClassOp(builderOM, unknownLoc, "SitestBlackBoxModulesSchema",
blackBoxModulesParamNames, classFieldTypes);
builderOM.create<ClassOp>("SitestBlackBoxModulesSchema",
blackBoxModulesParamNames, classFieldTypes);
SmallVector<PortInfo> mports;
blackBoxMetadataClass = builderOM.create<ClassOp>(
builderOM.getStringAttr("SitestBlackBoxMetadata"), mports);
Expand Down
Loading
Loading