Skip to content

[stdlib] Fix SIMD[..., u128/s128] construction from UInt #4339

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

soraros
Copy link
Contributor

@soraros soraros commented Apr 12, 2025

Close #4338.

@soraros soraros requested a review from a team as a code owner April 12, 2025 04:34
Comment on lines 109 to 112
alias a = rebind[UInt](-1)
alias a_str = String(a)
assert_equal(a_str, String(UInt128(a)))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can't do much better than stringify due to #4049.

self = Self(value.value)
@parameter
if bitwidthof[dtype]() > bitwidthof[DType.index]():
self = Self(rebind[UInt64](value))
Copy link
Contributor Author

@soraros soraros Apr 12, 2025

Choose a reason for hiding this comment

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

Cannot use UInt64(value) here. I think there's a compiler bug(?) where v.cast[index]().cast[int128]() gets simplified to v.cast[int128]() (on the pop level), which lowers to sext yet again. So we use rebind to skip the constructor from index entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, it's probably not really a compiler bug. Suppose the target type is UInt128. If we go through UInt64, we get roughly the following MLIR:

var v: index = ...
var a = pop.cast[int64](v)
var b = pop.cast[int128](a)

Since index is treated as signed (?), the first cast is a no-op, and the second is a sign extension.

Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Aren't you assuming 64 bitwidth machines by using UInt64?

Copy link
Contributor Author

@soraros soraros Apr 13, 2025

Choose a reason for hiding this comment

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

Yea, I'm being lazy here. Will fix it properly when the team confirmes that rebind is an acceptable approach.

Copy link
Contributor Author

@soraros soraros Apr 15, 2025

Choose a reason for hiding this comment

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

Actually, rebind[T] doesn't work when alias T = Scalar[_unsigned_integral_type_of[DType.index]()], so I opted for a different fix.

@soraros soraros force-pushed the index-to-i128 branch 2 times, most recently from fb3be68 to e883e8b Compare April 12, 2025 15:21
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.

[BUG] Mojo doesn't handle UInt to SIMD with bit widths > 64 correctly
2 participants