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

[FIRRTL] Add a new pass to detect static asserts #6341

Merged
merged 4 commits into from
Oct 26, 2023

Conversation

prithayan
Copy link
Contributor

This PR adds a new pass called Lint to the FIRRTL pipeline, that checks for asserts that are guaranteed to fail a simulation.
If the assert is enabled and the predicate is either a constant false or a reset, the pass emits an error on the assert.
The intention is that the pass can later be extended to include more checks.

Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

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

This makes sense to me, just some minor thoughts.

test/Dialect/FIRRTL/lint.mlir Outdated Show resolved Hide resolved
lib/Dialect/FIRRTL/Transforms/Lint.cpp Outdated Show resolved Hide resolved
lib/Dialect/FIRRTL/Transforms/Lint.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@dtzSiFive dtzSiFive left a comment

Choose a reason for hiding this comment

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

Neat! Linting pass sounds useful, added some comments!

lib/Dialect/FIRRTL/Transforms/Lint.cpp Outdated Show resolved Hide resolved
auto fModule = getOperation();
(void)failableParallelForEach(
fModule.getContext(), fModule.getOps(),
[&](Operation &op) { return checkAssert(&op); });
Copy link
Contributor

Choose a reason for hiding this comment

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

If emitting an error, this should fail the pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, missed it. Added a failure, but we can discuss if we want this to be a failure or just a warning.

lib/Dialect/FIRRTL/Transforms/Lint.cpp Show resolved Hide resolved
};
LogicalResult checkAssert(Operation *op) {
Value predicate;
if (auto a = dyn_cast<AssertOp>(op)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be an actual analysis that is inspected by the pass, where diagnostics are emitted?
Fine as-is, but consider especially as this grows.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, this was simple enough, but we could consider it if an analysis is required for other uses.

let summary = "An analysis pass to detect static asserts.";
let description = [{
This pass detects assert ops, whose predicate condition can be statically
inferred to be false, that is they will trivially fail any simulation. The
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have a (slightly, for now) more general description?

It seems like presently this already does more than "statically inferred to be false" -- reset signal probably will be false at some point (diagnostic sounds useful) but is it fair to summarize as statically inferred to be false?

Anyway either commit to maintaining this or maybe give you a little wiggle room re:description 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, updated the description.

void runOnOperation() override {
auto fModule = getOperation();
(void)failableParallelForEach(
fModule.getContext(), fModule.getOps(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if parallel over operations is a good idea? Thread-to-work ratio seems a bit rough, but dunno.

Misc potential preconditions/assumptions:

  1. post ExpandWhen's / single-block
  2. IMDCE already executed (in theory if this was run before such, what if the assert is not reachable/dead?).

That sound about right? Just trying to understand this and it's role/purpose/scope.

And pipeline-ordering-wise probably of course want to run this after various optimizations such that if our compiler can/will prove these are statically false/lint-worthy, we'll have done so (e.g., IMCP).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, parallel work seems very small. Removed it.
It is expected that all the optimizations are done, additionally post expand-whens, hence running it late in the pipeline.
I will update the comments to indicate the assumptions.

Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

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

Thanks for the updates, looks good from my perspective.

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

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

Cool! Love the idea of a linting pass. And this catching statically-failing asserts so early in the pipeline is very nice. A lot better than waiting for the full simulation turnaround time.

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

@prithayan
Copy link
Contributor Author

Thanks for the review everyone. It would be nice to keep track of other lint checks we can add.

@prithayan prithayan merged commit cd2d195 into main Oct 26, 2023
5 checks passed
@prithayan prithayan deleted the dev/pbarua/assert_verif branch October 26, 2023 01:39
@fabianschuiki
Copy link
Contributor

Could this be a home for @darthscsi's analysis that checks if uninitialized data leaks into registers?

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.

5 participants