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

Allow specifying function alignment #75072

Closed
repnop opened this issue Aug 2, 2020 · 8 comments · Fixed by #81234
Closed

Allow specifying function alignment #75072

repnop opened this issue Aug 2, 2020 · 8 comments · Fixed by #81234
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@repnop
Copy link
Contributor

repnop commented Aug 2, 2020

I've been recently writing a kernel for the RISC-V architecture, using strictly as much Rust as I can get away with and during which I've run into the need to have certain functions aligned to specific byte alignments (ex. any function placed in mtvec MUST be 4 byte aligned), and while I can seemingly accomplish this using asm!(".align <alignment>") I think it would make sense for that to be an attribute of the function itself, whether it be limited to extern "C" fns or valid on all ABIs. Example of what I am asking for:

#[no_mangle]
#[repr(align(4))]
extern "C" fn gets_put_in_mtvec() {
    // ... content here ...
}
@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Aug 2, 2020
@hanna-kruppe
Copy link
Contributor

For context, how is this requirement addressed by C toolchains?

@repnop
Copy link
Contributor Author

repnop commented Aug 3, 2020

Using https://c.godbolt.org/z/E7ETx8 to verify, it looks like both GCC and Clang support using __attribute__((aligned(<alignment>))) to specify function alignment on a per-function basis

@nbdd0121
Copy link
Contributor

nbdd0121 commented Aug 3, 2020

I believe you would need to use assembly for interrupt handlers because naked functions don't work as intended anyway.

@Skallwar
Copy link

I'm on the exact same situation as @repnop, with the exact same context.
asm!(".align <alignment>") inside the function worked for you?

@Skallwar
Copy link

Skallwar commented Nov 19, 2020

If this is something that should be part of the language, I would gladly try to implement it

@sampersand
Copy link

I'd also find this quite helpful for some low-level bit twiddling I'm doing.

@repnop
Copy link
Contributor Author

repnop commented Jan 21, 2021

@Skallwar oh, somehow I missed the notification from this issue. yeah, currently I'm (ab)using that in a #[naked] fn (I have no idea if it works for non-naked fns).

Opened a PR so we'll see where this goes :)

@memoryruins
Copy link
Contributor

The tracking issue for #![feature(fn_align)] is now #82232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants