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

[BUG] struct with multiple SIMD fields rounds sizeof up to nearest power of 2 #2693

Open
mikowals opened this issue May 17, 2024 · 1 comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label

Comments

@mikowals
Copy link
Contributor

mikowals commented May 17, 2024

Bug description

I expected a struct's sizeof to be the total of the sizeof each of its fields individually. This usually is true but does not seem to be the case for SIMD fields when the sizes don't total to a power of 2. I am not sure if this is really a bug or just something that needs better documentation. But it was unexpected and can be a lot of extra memory use.

Steps to reproduce

Gist from playground or code below:

from sys import sizeof
struct Foo:
  var a: SIMD[DType.uint8, 8]
  var b: SIMD[DType.uint8, 4]
 
def main():
    print("sizeof[Foo](): ", sizeof[Foo]())
    print("sizeof[SIMD[DType.uint8, 8]]():", sizeof[SIMD[DType.uint8, 8]]())
    print("sizeof[SIMD[DType.uint8, 4]]():", sizeof[SIMD[DType.uint8, 4]]())

output:

sizeof[Foo]():  16
sizeof[SIMD[DType.uint8, 8]](): 8
sizeof[SIMD[DType.uint8, 4]](): 4

System information

In playground on 17-May-2024 and locally on:
- macOS Sonoma 14.4.1 (23E224)
- mojo 2024.5.1607 (bd1f55d0)
- modular 0.7.2 (d0adc668)
@mikowals mikowals added bug Something isn't working mojo-repo Tag all issues with this label labels May 17, 2024
@JoeLoser JoeLoser added the mojo-lang Tag for all issues related to language. label May 17, 2024
@bgreni
Copy link
Contributor

bgreni commented May 17, 2024

This doesn't seem to be exclusive to SIMD so there could be a reason for this behaviour?

struct Bar:
    var d: Int
    var e: Bool
    

fn main():
    print(sizeof[Int]()) # 8
    print(sizeof[Bool]()) # 1
    print(sizeof[Bar]()) # 16
``

@ematejska ematejska removed the mojo-lang Tag for all issues related to language. label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo-repo Tag all issues with this label
Projects
None yet
Development

No branches or pull requests

4 participants