Skip to content

[HW] Add IMDCE module rewrites#10007

Open
stomfaig wants to merge 3 commits intollvm:mainfrom
stomfaig:add_mod_rewrites
Open

[HW] Add IMDCE module rewrites#10007
stomfaig wants to merge 3 commits intollvm:mainfrom
stomfaig:add_mod_rewrites

Conversation

@stomfaig
Copy link
Copy Markdown
Contributor

Add module rewriting functions and corresponding tests.

This also includes moving the helper removeElementsAtIndices to Utils from FIRRTL specific code.

@stomfaig
Copy link
Copy Markdown
Contributor Author

cc: @uenoku

@uenoku uenoku self-requested a review March 24, 2026 01:25
// Note that post-order traversal on the instance graph never visit
// unreachable modules so it's safe to erase the module even though
// `modules` seems to be capturing module pointers.
module.erase();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a certain situation where this erases modules still used by some symbol. I think until we have a proper analysis it's safer not to erase it (and we can run SymbolDCE anyway).

sv.verbatim "{{0}}" { symbols = [@baz] }
// Not reachable from the top level. 
hw.module private @baz() {}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does this also apply to the erase in eraseEmptyModule?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I think so.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Specifically do you mind if you could check an example in #10084

liveElements.insert(newResult);

use->erase();
instance->erase();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

could you update instance graph?

Copy link
Copy Markdown
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 other than module removal. For now I think we shouldn't remove empty modules as well; this has been a problem of FIRRTL IMDCE as well.

We need liveness analysis like https://github.com/llvm/llvm-project/blob/main/mlir/lib/Transforms/SymbolDCE.cpp). Basically if the module symbol is not referred by anything, it's ok to remove dead or empty modules.

@stomfaig
Copy link
Copy Markdown
Contributor Author

stomfaig commented Apr 2, 2026

Sorry for the delay.

I changed so that the module is kept, but stripped. Or do you think that we should also keep the module signatures for non-executable modules?

@uenoku
Copy link
Copy Markdown
Member

uenoku commented Apr 3, 2026

Thanks! I think it's not necessary to keep signatures (as inner symbol is required to refer to module ports). I think the current code looks good as is.

Comment on lines +670 to +672
instanceGraph->erase(instanceGraphNode);
// module.erase();
++numErasedModules;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove these.

Copy link
Copy Markdown
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.

One nit, otherwise LGTM

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.

2 participants