Skip to content

Constants as const #73780

Closed
Closed
@leonardo-m

Description

@leonardo-m
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions