Skip to content

Commit 6c8c900

Browse files
seldridgerwy7
andcommitted
[FIRRTL] Fix port insertion/deletion domain bugs
Fix bugs in the logic for port insertion and deletion. This was factored out of #9106. I'm trying to fixing bugs in _other_ passes, without having to land the full domain inference pass just yet and this was a prerequisite for some of those other fixes. Co-authored-by: Robert Young <[email protected]> Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent e447283 commit 6c8c900

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

lib/Dialect/FIRRTL/FIRRTLOps.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,15 @@ static void insertPorts(FModuleLike op,
801801
unsigned oldNumArgs = op.getNumPorts();
802802
unsigned newNumArgs = oldNumArgs + ports.size();
803803

804+
// Build a map from old port indices to new indices.
805+
SmallVector<unsigned> indexMap(oldNumArgs);
806+
size_t inserted = 0;
807+
for (size_t i = 0; i < oldNumArgs; ++i) {
808+
while (inserted < ports.size() && ports[inserted].first == i)
809+
++inserted;
810+
indexMap[i] = i + inserted;
811+
}
812+
804813
// Add direction markers and names for new ports.
805814
auto existingDirections = op.getPortDirectionsAttr();
806815
ArrayRef<Attribute> existingNames = op.getPortNames();
@@ -822,12 +831,9 @@ static void insertPorts(FModuleLike op,
822831
newSyms.reserve(newNumArgs);
823832
newLocs.reserve(newNumArgs);
824833

825-
SmallVector<unsigned> indexMap(oldNumArgs);
826-
827834
auto emptyArray = ArrayAttr::get(op.getContext(), {});
828835

829836
unsigned oldIdx = 0;
830-
unsigned inserted = 0;
831837
auto migrateOldPorts = [&](unsigned untilOldIdx) {
832838
while (oldIdx < oldNumArgs && oldIdx < untilOldIdx) {
833839
newDirections.push_back(existingDirections[oldIdx]);
@@ -838,8 +844,6 @@ static void insertPorts(FModuleLike op,
838844
newAnnos.push_back(op.getAnnotationsAttrForPort(oldIdx));
839845
newSyms.push_back(op.getPortSymbolAttr(oldIdx));
840846
newLocs.push_back(existingLocs[oldIdx]);
841-
842-
indexMap[oldIdx] = oldIdx + inserted;
843847
++oldIdx;
844848
}
845849
};
@@ -905,24 +909,13 @@ static ArrayAttr fixDomainInfoDeletions(MLIRContext *context,
905909
if (supportsEmptyAttr && domainInfoAttr.empty())
906910
return domainInfoAttr;
907911

908-
// Compute an array where each entry is the number of ports before that
909-
// index that have been deleted. This is used to update domain information.
910-
SmallVector<unsigned> numDeleted;
911-
numDeleted.resize(portIndices.size());
912-
size_t deletionIndex = portIndices.find_first();
912+
// Build a map from old port indices to new indices.
913+
SmallVector<unsigned> indexMap(portIndices.size());
914+
size_t deleted = 0;
913915
for (size_t i = 0, e = portIndices.size(); i != e; ++i) {
914-
if (i == deletionIndex) {
915-
if (i == 0)
916-
numDeleted[i] = 1;
917-
else
918-
numDeleted[i] = numDeleted[i - 1] + 1;
919-
deletionIndex = portIndices.find_next(i);
920-
continue;
921-
}
922-
if (i == 0)
923-
numDeleted[i] = 0;
924-
else
925-
numDeleted[i] = numDeleted[i - 1];
916+
indexMap[i] = i - deleted;
917+
if (portIndices[i])
918+
++deleted;
926919
}
927920

928921
// Return a cached empty ArrayAttr.
@@ -960,7 +953,7 @@ static ArrayAttr fixDomainInfoDeletions(MLIRContext *context,
960953
if (portIndices.test(oldIdx))
961954
continue;
962955
// If the new index is the same, do nothing.
963-
auto newIdx = oldIdx - numDeleted[oldIdx];
956+
auto newIdx = indexMap[oldIdx];
964957
if (oldIdx == newIdx) {
965958
newDomainInfo.push_back(attr);
966959
continue;

0 commit comments

Comments
 (0)