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

v: implement aligned attr for struct #19915

Merged
merged 9 commits into from
Nov 21, 2023
Merged

Conversation

felipensp
Copy link
Member

@felipensp felipensp commented Nov 17, 2023

Fix #19775

@[aligned]
struct Test {
// ...
}

@[aligned:8]
struct Test2 {
// ...
}

It also works for MSVC.

🤖[deprecated] Generated by Copilot at a9b32f2

Add support for the aligned attribute for structs in C code generation. This allows specifying the memory alignment of structs and their fields for different compilers.

🤖[deprecated] Generated by Copilot at a9b32f2

  • Add support for the aligned attribute for structs (link,

@felipensp felipensp marked this pull request as ready for review November 17, 2023 13:40
@spytheman
Copy link
Member

It needs a few lines to describe the attribute after https://github.com/vlang/v/blob/master/doc/docs.md#packed too.

@spytheman
Copy link
Member

Also a pair of .vv and .c.must_have files in vlib/v/gen/c/testdata/ , to prevent potential future silent regressions.

@JalonSolov
Copy link
Contributor

In your example, you show using @[aligned], while also showing @[aligned:8]. My question is... what is the default alignment if you don't specify :<some number>? Can I make that number any arbitrary integer? How about floats?

In other words... there needs to be more documentation for this feature. If you're going to allow the attr without a number, the default needs to be documented. Possible values for the number after the : also need to be documented (as in, must be an integer power of 2(right?), minimum value, maximum value, etc.).

@JalonSolov
Copy link
Contributor

Something else to document... does this force the alignment of the whole struct, or the fields within the struct?

@felipensp
Copy link
Member Author

felipensp commented Nov 19, 2023

In your example, you show using @[aligned], while also showing @[aligned:8]. My question is... what is the default alignment if you don't specify :<some number>? Can I make that number any arbitrary integer? How about floats?

In other words... there needs to be more documentation for this feature. If you're going to allow the attr without a number, the default needs to be documented. Possible values for the number after the : also need to be documented (as in, must be an integer power of 2(right?), minimum value, maximum value, etc.).

About the alignment factor (the parameter value), the GCC docs says about:

Alternatively, you can leave out the alignment factor and just ask the compiler to align a type to the maximum useful alignment for the target machine you are compiling for.
Whenever you leave out the alignment factor in an aligned attribute specification, the compiler automatically sets the alignment for the type to the largest alignment which is ever used for any data type on the target machine you are compiling for. Doing this can often make copy operations more efficient, because the compiler can use whatever instructions copy the biggest chunks of memory when performing copies to or from the variables which have types that you have aligned this way.

@felipensp
Copy link
Member Author

Something else to document... does this force the alignment of the whole struct, or the fields within the struct?

It is about the final struct sizing.

@JalonSolov
Copy link
Contributor

About the alignment factor (the parameter value), the GCC docs says about:... I would like to see at least minimal documentation about it in the V docs, or, if nothing else, a link to the GCC docs.

And on that note: Have you verified that it works exactly the same in clang, msvc, zig, etc.? Yes, zig, as there is a way to use zig as the backend compiler for V.

@felipensp
Copy link
Member Author

felipensp commented Nov 20, 2023

And on that note: Have you verified that it works exactly the same in clang, msvc, zig, etc.? Yes, zig, as there is a way to use zig as the backend compiler for V.

Yes. This pr changes works in gcc, clang, msvc.

@spytheman spytheman merged commit 8cc8216 into vlang:master Nov 21, 2023
54 checks passed
@felipensp felipensp deleted the aligned_attr branch January 9, 2024 12:04
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.

Add support of aligned attribute
3 participants