Skip to content

Commit

Permalink
[FIRRTL] Add a pass to convert VoB -> BoV conversion (#4654)
Browse files Browse the repository at this point in the history
This pass converts objects with the shape vector<bundle> to bundle<vector>.
  • Loading branch information
rwy7 authored Mar 1, 2023
1 parent 0e06b7c commit 38b1f39
Show file tree
Hide file tree
Showing 9 changed files with 1,800 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLTypesImpl.td
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def FVectorTypeImpl : FIRRTLImplType<"FVector", [FieldIDTypeInterface]> {
/// return the index of the parent element.
uint64_t getIndexForFieldID(uint64_t fieldID);

/// Find the index of the element that contains the given fieldID.
/// As well, rebase the fieldID to the element.
std::pair<uint64_t, uint64_t> getIndexAndSubfieldID(uint64_t fieldID);

/// Strip off a single layer of this type and return the sub-type and a
/// field ID targeting the same field, but rebased on the sub-type.
std::pair<FIRRTLBaseType, uint64_t> getSubTypeByFieldID(uint64_t fieldID);
Expand Down Expand Up @@ -200,6 +204,10 @@ def BundleImpl : FIRRTLImplType<"Bundle", [FieldIDTypeInterface]> {
/// index of the parent field.
uint64_t getIndexForFieldID(uint64_t fieldID);

/// Find the index of the element that contains the given fieldID.
/// As well, rebase the fieldID to the element.
std::pair<uint64_t, uint64_t> getIndexAndSubfieldID(uint64_t fieldID);

/// Strip off a single layer of this type and return the sub-type and a
/// field ID targeting the same field, but rebased on the sub-type.
std::pair<FIRRTLBaseType, uint64_t> getSubTypeByFieldID(uint64_t fieldID);
Expand Down
3 changes: 2 additions & 1 deletion include/circt/Dialect/FIRRTL/FIRRTLVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ExprVisitor {
// Miscellaneous.
BitsPrimOp, HeadPrimOp, MuxPrimOp, PadPrimOp, ShlPrimOp, ShrPrimOp,
TailPrimOp, VerbatimExprOp, HWStructCastOp, BitCastOp, RefSendOp,
RefResolveOp, mlir::UnrealizedConversionCastOp>(
RefResolveOp, RefSubOp, mlir::UnrealizedConversionCastOp>(
[&](auto expr) -> ResultType {
return thisCast->visitExpr(expr, args...);
})
Expand Down Expand Up @@ -153,6 +153,7 @@ class ExprVisitor {
HANDLE(VerbatimExprOp, Unhandled);
HANDLE(RefSendOp, Unhandled);
HANDLE(RefResolveOp, Unhandled);
HANDLE(RefSubOp, Unhandled);

// Conversions.
HANDLE(HWStructCastOp, Unhandled);
Expand Down
2 changes: 2 additions & 0 deletions include/circt/Dialect/FIRRTL/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ createCreateSiFiveMetadataPass(bool replSeqMem = false,

std::unique_ptr<mlir::Pass> createWireDFTPass();

std::unique_ptr<mlir::Pass> createVBToBVPass();

std::unique_ptr<mlir::Pass> createAddSeqMemPortsPass();

std::unique_ptr<mlir::Pass> createDedupPass();
Expand Down
9 changes: 9 additions & 0 deletions include/circt/Dialect/FIRRTL/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,13 @@ def InnerSymbolDCE : Pass<"firrtl-inner-symbol-dce", "mlir::ModuleOp"> {
];
}

def VBToBV : Pass<"firrtl-vb-to-bv", "firrtl::CircuitOp"> {
let summary = "Transform vector-of-bundles to bundle-of-vectors";
let description = [{
This pass converts vectors containing bundles, into bundles containing
vectors.
}];
let constructor = "circt::firrtl::createVBToBVPass()";
}

#endif // CIRCT_DIALECT_FIRRTL_PASSES_TD
17 changes: 15 additions & 2 deletions lib/Dialect/FIRRTL/FIRRTLTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,13 @@ uint64_t BundleType::getIndexForFieldID(uint64_t fieldID) {
return std::distance(fieldIDs.begin(), it);
}

std::pair<uint64_t, uint64_t>
BundleType::getIndexAndSubfieldID(uint64_t fieldID) {
auto index = getIndexForFieldID(fieldID);
auto elementFieldID = getFieldID(index);
return {index, fieldID - elementFieldID};
}

std::pair<FIRRTLBaseType, uint64_t>
BundleType::getSubTypeByFieldID(uint64_t fieldID) {
if (fieldID == 0)
Expand Down Expand Up @@ -1011,12 +1018,18 @@ uint64_t FVectorType::getIndexForFieldID(uint64_t fieldID) {
return (fieldID - 1) / (getElementType().getMaxFieldID() + 1);
}

std::pair<uint64_t, uint64_t>
FVectorType::getIndexAndSubfieldID(uint64_t fieldID) {
auto index = getIndexForFieldID(fieldID);
auto elementFieldID = getFieldID(index);
return {index, fieldID - elementFieldID};
}

std::pair<FIRRTLBaseType, uint64_t>
FVectorType::getSubTypeByFieldID(uint64_t fieldID) {
if (fieldID == 0)
return {*this, 0};
auto subfieldIndex = getIndexForFieldID(fieldID);
return {getElementType(), fieldID - getFieldID(subfieldIndex)};
return {getElementType(), getIndexForFieldID(fieldID)};
}

uint64_t FVectorType::getMaxFieldID() {
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/FIRRTL/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_circt_dialect_library(CIRCTFIRRTLTransforms
ResolveTraces.cpp
RemoveUnusedPorts.cpp
SFCCompat.cpp
VBToBV.cpp
WireDFT.cpp

DEPENDS
Expand Down
Loading

0 comments on commit 38b1f39

Please sign in to comment.