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

Constants as const #73780

Closed
leonardo-m opened this issue Jun 26, 2020 · 2 comments
Closed

Constants as const #73780

leonardo-m opened this issue Jun 26, 2020 · 2 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

pub fn foo1(x: usize) -> u32 {
    let bar: [u32; 10] = [0,10,20,30,40,50,60,70,80,90];
    bar[x % 10]
}
pub fn foo2(x: usize) -> u32 {
    const BAR: [u32; 10] = [0,10,20,30,40,50,60,70,80,90];
    BAR[x % 10]
}

With good compilation arguments gives (rustc 1.46.0-nightly 50fc24d 2020-06-25):

.LCPI0_0:
	.long   0
	.long   10
	.long   20
	.long   30
	.long   40
	.long   50
	.long   60
	.long   70
example::foo1:
	sub     rsp, 40
	vmovaps ymm0, ymmword ptr [rip + .LCPI0_0]
	vmovups ymmword ptr [rsp], ymm0
	movabs  rcx, -3689348814741910323
	mov     rax, rdi
	mul     rcx
	movabs  rax, 386547056720
	mov     qword ptr [rsp + 32], rax
	shr     rdx, 2
	and     rdx, -2
	lea     rax, [rdx + 4*rdx]
	sub     rdi, rax
	mov     eax, dword ptr [rsp + 4*rdi]
	add     rsp, 40
	vzeroupper
	ret

example::foo2:
	movabs  rcx, -3689348814741910323
	mov     rax, rdi
	mul     rcx
	shr     rdx, 2
	and     rdx, -2
	lea     rax, [rdx + 4*rdx]
	sub     rdi, rax
	lea     rax, [rip + .L__unnamed_1]
	mov     eax, dword ptr [rax + 4*rdi]
	ret

.L__unnamed_1:
        .asciz  "\000\000\000\000\n\000\000\000\024\000\000\000\036\000\000\000(\000\000\0002\000\000\000<\000\000\000F\000\000\000P\000\000\000Z\000\000"

I'd like foo1 to compile as foo2, without those vmovups.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 26, 2020
@nbdd0121
Copy link
Contributor

let bar: [u32; 10] = [0,10,20,30,40,50,60,70,80,90];

Semantically just says: put this on the stack. Rust is doing exactly what you ask it to do; it cannot place foo inside rodata because it is a value.

If you want it to be placed inside rodata, you can use static or take a reference, which Rust can optimise by constant promotion.

pub fn foo3(x: usize) -> u32 {
    static BAR: [u32; 10] = [0,10,20,30,40,50,60,70,80,90];
    BAR[x % 10]
}

pub fn foo4(x: usize) -> u32 {
    let BAR: &[u32; 10] = &[0,10,20,30,40,50,60,70,80,90];
    BAR[x % 10]
}

@leonardo-m
Copy link
Author

I didn't know this nbdd0121, I'm so ignorant. I've never used static arrays defined inside Rust functions like that yet! Thank you! 😁🎉
Then if rustc 'cannot place foo inside rodata because it is a value' then I think we can close this issue down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants