Skip to content

Conversation

@seldridge
Copy link
Member

Change the Grand Central View lowering pass to now allow for Grand Central
Companions to be multiply instantiated. This should have technically been
possible for a long time [1] as long as there was a guarantee that there
was never going to be a leaf that was outside the companion.

This is a pretty simple change to just loop over all instances and extract
(add a lowerToBind attribute) for the ones which are in the design. The
other, subtle blocker is that without the InstanceInfo, doing the check
of "in the design" was tedious. Now this is pretty straightforward.

@seldridge seldridge requested a review from darthscsi as a code owner June 26, 2025 00:06
builder.create<WireOp>(port.getLoc(), port.getType());
port.replaceAllUsesWith(wire.getResult());
}
instance->erase();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's necessary to erase through InstanceGraph.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this isn't keeping the instance graph up to date with its deletions. Same problem later when the module is deleted when in "drop" mode. This code was like this before. I will still fix it here. Good catch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this in aed1af7. I think that looks right?

Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

return true;
}
// Extract all instances in the design.
for (auto *i : instancePaths->instanceGraph.lookup(op)->uses()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early increment would be necessary

Suggested change
for (auto *i : instancePaths->instanceGraph.lookup(op)->uses()) {
for (auto *i : make_early_inc_range(..)) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change in 7c1a1d7.

I think this, as written, should be fine as the instance graph was never updated. However, now that the instance graph is updated, this can definitely be a problem.

Comment on lines +2102 to +2105
if (!instanceInfo->allInstancesUnderEffectiveDut(
i->getParent()->getModule()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if there is an instance under effective dut and one is not under dut, companion module will be kept under dut. Does that happen practically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that would be very rare. Practically, this should never happen because of the dedup group annotations that Chisel emits. It is expected that the test and the design are kept separate to avoid behavior like this. The behavior here is more a stop gap of needing to do something sane if this ever occurs.

Clarifying the behavior: a parent instantiates the companion. We will not extract the companion if the parent is never instantiated in the effective design. Any instantiation in the effective design will force us to extract this.

There is potentially a better (though surprising) behavior of doing module duplication in this situation. However, we've generally tried to avoid this. The surprising/unpredictable nature has been viewed as unacceptable.

In preparation to remove the exactly-one-instance check of each Grand
Central Companion module, inline the function doing this check and get
this in the right shape to switch it over to allow for multiple
instantiation.

Signed-off-by: Schuyler Eldridge <[email protected]>
Change the Grand Central View lowering pass to now allow for Grand Central
Companions to be multiply instantiated.  This should have technically been
possible for a long time [[1]] as long as there was a guarantee that there
was never going to be a leaf that was _outside_ the companion.

This is a pretty simple change to just loop over all instances and
extract (add a `lowerToBind` attribute) for the ones which are in the
design.  The other, subtle blocker is that without the `InstanceInfo`,
doing the check of "in the design" was tedious.  Now this is pretty
straightforward.

[1]: 2d3a040

Signed-off-by: Schuyler Eldridge <[email protected]>
Fix a bug in the Grand Central pass where it was deleting instances and
modules, but not keeping the instance graph up to date.

Signed-off-by: Schuyler Eldridge <[email protected]>
@seldridge seldridge force-pushed the dev/seldridge/firrtl-allow-grand-central-companions-multi-instantiation branch from 7c1a1d7 to 19a9de6 Compare June 27, 2025 19:41
@seldridge seldridge merged commit 19a9de6 into main Jun 27, 2025
5 checks passed
@seldridge seldridge deleted the dev/seldridge/firrtl-allow-grand-central-companions-multi-instantiation branch June 27, 2025 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants