-
Notifications
You must be signed in to change notification settings - Fork 384
[FIRRTL] FoldUnusedBits: minor cleanup #7914
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2679,7 +2679,7 @@ struct FoldUnusedBits : public mlir::RewritePattern { | |
| // ports whose data/rdata field is used only through bit select ops. The | ||
| // bit selects are then used to build a bit-mask. The ops are collected. | ||
| SmallVector<BitsPrimOp> readOps; | ||
| auto findReadUsers = [&](Value port, StringRef field) { | ||
| auto findReadUsers = [&](Value port, StringRef field) -> LogicalResult { | ||
| auto portTy = type_cast<BundleType>(port.getType()); | ||
| auto fieldIndex = portTy.getElementIndex(field); | ||
| assert(fieldIndex && "missing data port"); | ||
|
|
@@ -2691,16 +2691,19 @@ struct FoldUnusedBits : public mlir::RewritePattern { | |
|
|
||
| for (auto *user : op->getUsers()) { | ||
| auto bits = dyn_cast<BitsPrimOp>(user); | ||
| if (!bits) { | ||
| usedBits.set(); | ||
| continue; | ||
| } | ||
| if (!bits) | ||
| return failure(); | ||
|
|
||
| usedBits.set(bits.getLo(), bits.getHi() + 1); | ||
| if (usedBits.all()) | ||
| return failure(); | ||
|
|
||
| mapping[bits.getLo()] = 0; | ||
| readOps.push_back(bits); | ||
| } | ||
| } | ||
|
|
||
| return success(); | ||
| }; | ||
|
|
||
| // Finds the users of write ports. This expects all the data/wdata fields | ||
|
|
@@ -2741,20 +2744,21 @@ struct FoldUnusedBits : public mlir::RewritePattern { | |
| return failure(); | ||
| continue; | ||
| case MemOp::PortKind::Read: | ||
| findReadUsers(port, "data"); | ||
| if (failed(findReadUsers(port, "data"))) | ||
| return failure(); | ||
| continue; | ||
| case MemOp::PortKind::ReadWrite: | ||
| if (failed(findWriteUsers(port, "wdata"))) | ||
| return failure(); | ||
| findReadUsers(port, "rdata"); | ||
| if (failed(findReadUsers(port, "rdata"))) | ||
| return failure(); | ||
| continue; | ||
| } | ||
| llvm_unreachable("unknown port kind"); | ||
| } | ||
|
|
||
| // Perform the transformation is there are some bits missing. Unused | ||
| // memories are handled in a different canonicalizer. | ||
| if (usedBits.all() || usedBits.none()) | ||
| // Unused memories are handled in a different canonicalizer. | ||
| if (usedBits.none()) | ||
| return failure(); | ||
|
|
||
| // Build a mapping of existing indices to compacted ones. | ||
|
|
@@ -2828,9 +2832,13 @@ struct FoldUnusedBits : public mlir::RewritePattern { | |
| rewriter.setInsertionPointAfter(readOp); | ||
| auto it = mapping.find(readOp.getLo()); | ||
| assert(it != mapping.end() && "bit op mapping not found"); | ||
| rewriter.replaceOpWithNewOp<BitsPrimOp>( | ||
| readOp, readOp.getInput(), | ||
| // Create a new bit selection from the compressed memory. The new op may | ||
| // be folded if we are selecting the entire compressed memory. | ||
| auto newReadValue = rewriter.createOrFold<BitsPrimOp>( | ||
| readOp.getLoc(), readOp.getInput(), | ||
| readOp.getHi() - readOp.getLo() + it->second, it->second); | ||
| rewriter.replaceAllUsesWith(readOp, newReadValue); | ||
| rewriter.eraseOp(readOp); | ||
|
Comment on lines
-2831
to
+2841
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me. |
||
| } | ||
|
|
||
| // Rewrite the writes into a concatenation of slices. | ||
|
|
@@ -2840,11 +2848,11 @@ struct FoldUnusedBits : public mlir::RewritePattern { | |
|
|
||
| Value catOfSlices; | ||
| for (auto &[start, end] : ranges) { | ||
| Value slice = | ||
| rewriter.create<BitsPrimOp>(writeOp.getLoc(), source, end, start); | ||
| Value slice = rewriter.createOrFold<BitsPrimOp>(writeOp.getLoc(), | ||
| source, end, start); | ||
| if (catOfSlices) { | ||
| catOfSlices = | ||
| rewriter.create<CatPrimOp>(writeOp.getLoc(), slice, catOfSlices); | ||
| catOfSlices = rewriter.createOrFold<CatPrimOp>(writeOp.getLoc(), | ||
| slice, catOfSlices); | ||
| } else { | ||
| catOfSlices = slice; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This early exit makes sense to me. No reason to wait until all users have been visited before bailing.