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

Change how we store and sign/zero extend integers. #1557

Merged
merged 1 commit into from
Jan 24, 2025

Conversation

ltratt
Copy link
Contributor

@ltratt ltratt commented Jan 23, 2025

Previously we stored raw u64s and expected the user to remember that they needed to zero/sign extend the underlying integer whenever they wanted to do anything with that. We did not always do this, and we did it incorrectly in a couple of places!

This commit introduces a new struct ArbBitInt which is basically a pair (bit_width: u32, value: u64) which hides the underlying value. To get a raw Rust-level integer you have to call a method, and those methods have names with sign_extend and zero_extend in them. While one can, of course, call the wrong one, it is now impossible not to sign/zero extend. This struct is currently rather simple, but the API is flexible enough to extend to beyond-64-bit ints transparently. The (fairly extensive) test suite is a bit overkill right now, but is intended to help give us confidence if/when we support more than 64 bit ints in the future.

This commit also necessarily requires a full audit of everything to do with ints-in-traces. That means a lot of code churn, but it's absolutely necessary, and (a) makes much clearer where we should sign/zero extend (b) catches some places where we didn't do this but should.

This commit isn't perfect. In particular, I'm not very happy that Const::Int has both a TyIdx that contains a bit width and an ArbBitInt that separately records a bit width. That feels icky, but doing something neater will require at least some ickiness elsewhere. I'll worry about that another day.

@vext01
Copy link
Contributor

vext01 commented Jan 23, 2025

We are talking only about constant integers here, right? I think we store local variables in as many bytes as is required (not always a u64).

@ltratt
Copy link
Contributor Author

ltratt commented Jan 23, 2025

Yes, more-or-less trace-level Const::Ints, though there are some variations on this that make me a bit reluctant to just say "constants".

@vext01
Copy link
Contributor

vext01 commented Jan 24, 2025

LGTM.

This API is basically what LLVM does with .getZExtValue() and .getSExtValue() :)

@vext01
Copy link
Contributor

vext01 commented Jan 24, 2025

Please squash.

Previously we stored raw `u64`s and expected the user to remember
that they needed to zero/sign extend the underlying integer whenever
they wanted to do anything with that. We did not always do this, and we
did it incorrectly in a couple of places!

This commit introduces a new struct `ArbBitInt` which is basically a
pair `(bit_width: u32, value: u64)` which hides the underlying value. To
get a raw Rust-level integer you have to call a method, and those
methods have names with `sign_extend` and `zero_extend` in them. While
one can, of course, call the wrong one, it is now impossible not to
sign/zero extend. This struct is currently rather simple, but the API is
flexible enough to extend to beyond-64-bit ints transparently. The
(fairly extensive) test suite is a bit overkill right now, but is
intended to help give us confidence if/when we support more than 64 bit
ints in the future.

This commit also necessarily requires a full audit of everything to do
with ints-in-traces. That means a lot of code churn, but it's absolutely
necessary, and (a) makes much clearer where we should sign/zero extend
(b) catches some places where we didn't do this but should.

This commit isn't perfect. In particular, I'm not very happy that
`Const::Int` has both a `TyIdx` that contains a bit width *and* an
`ArbBitInt` that separately records a bit width. That feels icky, but
doing something neater will require at least some ickiness elsewhere.
I'll worry about that another day.
@ltratt
Copy link
Contributor Author

ltratt commented Jan 24, 2025

Squashed.

@vext01 vext01 added this pull request to the merge queue Jan 24, 2025
Merged via the queue into ykjit:master with commit fbd7c22 Jan 24, 2025
2 checks passed
@ltratt ltratt deleted the int_stuff branch January 24, 2025 16:16
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