-
Notifications
You must be signed in to change notification settings - Fork 299
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] Canonicalize multibit_mux with narrow index #7373
Conversation
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.
LGTM
While, this looks right to me, it is worthwhile to verify (locally test) that the behavior is doing to right thing for edge cases, e.g., zero width.
lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Outdated
// elements. | ||
auto indexWidth = op.getIndex().getType().getBitWidthOrSentinel(); | ||
uint64_t inputSize = op.getInputs().size(); | ||
if (indexWidth < 64 && 1ull << indexWidth < inputSize) { |
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.
Is 1ull
portable across windows and Linux?
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.
I think it's fine as there are several existing use cases in LLVM and CIRCT repo.
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.
long long is at least 64bits since c++11, according to https://en.cppreference.com/w/cpp/language/types .
That said (only looking at the code added here):
indexWidth is signed (int32_t), and in particular can be negative (!). Shift by a negative amount is UB! Please add a check for that, as even without UB it's logically not what we want either.
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.
Great work identifying this and putting together the canonicalizer!
On my "TODO" is to bolster our knownbits-like capabilities, and with that hat on I can't help but wonder about generalizing this beyond index being narrow directly. Future work, but since you're pushing on this just thought I'd mention it 👍 .
Anyway, generally looks good but there's an important issue that needs to be addressed before landing this, see my comment. Thanks!
lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Outdated
// elements. | ||
auto indexWidth = op.getIndex().getType().getBitWidthOrSentinel(); | ||
uint64_t inputSize = op.getInputs().size(); | ||
if (indexWidth < 64 && 1ull << indexWidth < inputSize) { |
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.
long long is at least 64bits since c++11, according to https://en.cppreference.com/w/cpp/language/types .
That said (only looking at the code added here):
indexWidth is signed (int32_t), and in particular can be negative (!). Shift by a negative amount is UB! Please add a check for that, as even without UB it's logically not what we want either.
Yeah that sounds great idea. I think naively computing knownbits could be expensive (as shown in circt/lib/Dialect/Comb/CombFolds.cpp Lines 618 to 627 in e8033f1
|
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.
Friendly upgrade to "request changes" since this is reachable UB. I don't have the details paged in but the expected change (dropping widths, I think?) to one of the tests was all it took 👍 .
Just making sure this gets addressed, thanks!
76dca79
to
32e5f71
Compare
32e5f71
to
ec3c3ef
Compare
Thanks! I added a condition and test to ensure that the width is not negative number. I checked the canonicalization on internal cores and it was ok. |
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.
LGTM! Thanks!
(sorry for delay, missed the update 👍 )
Np, thank you for review! |
This adds a canonicalization to optimize multibit_mux whose index has narrow width. Close #7361