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

One missed array bound test elision #72462

Closed
leonardo-m opened this issue May 22, 2020 · 2 comments
Closed

One missed array bound test elision #72462

leonardo-m opened this issue May 22, 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

Perhaps I'm missing something, but I think in this code rustc should not add a bound test:

#![feature(core_intrinsics)]
const N: usize = 1000;

pub fn foo(arr: &[usize; N]) -> usize {
    let mut total = 0;
    for i in 0 .. N {
        let k = i % 10;
        let index = i >> k;
        // unsafe { std::intrinsics::assume(index < N) };
        total += arr[index];
    }
    total
}

Currently it generates:

example::foo:
	push    rax
	xor     r8d, r8d
	movabs  r9, -3689348814741910323
	xor     esi, esi
.LBB0_1:
	mov     rax, rsi
	mul     r9
	shr     rdx, 2
	and     rdx, -2
	lea     rax, [rdx + 4*rdx]
	mov     rcx, rsi
	sub     rcx, rax
	mov     rax, rsi
	shr     rax, cl
	cmp     rax, 999
	ja      .LBB0_4
	add     rsi, 1
	add     cl, 1
	mov     rdx, rsi
	shr     rdx, cl
	cmp     rdx, 1000
	jae     .LBB0_3
	add     r8, qword ptr [rdi + 8*rax]
	add     r8, qword ptr [rdi + 8*rdx]
	add     rsi, 1
	cmp     rsi, 1000
	jne     .LBB0_1
	mov     rax, r8
	pop     rcx
	ret
.LBB0_3:
	mov     rax, rdx
.LBB0_4:
	lea     rdx, [rip + .L__unnamed_1]
	mov     esi, 1000
	mov     rdi, rax
	call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
	ud2

Using rustc 1.45.0-nightly 9310e3bd4 2020-05-21.

@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 May 22, 2020
@istankovic
Copy link
Contributor

I think this has been solved. At least with 1.75 nightly in release mode I get no bound checks:

playground::foo: # @playground::foo
# %bb.0:
	xorl	%esi, %esi
	movabsq	$-3689348814741910323, %r9      # imm = 0xCCCCCCCCCCCCCCCD
	xorl	%r8d, %r8d

.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	movq	%r8, %rax
	mulq	%r9
	shrl	$2, %edx
	andl	$-2, %edx
	leal	(%rdx,%rdx,4), %eax
	movl	%r8d, %ecx
	subl	%eax, %ecx
	movq	%r8, %rax
	shrq	%cl, %rax
	addq	(%rdi,%rax,8), %rsi
	leaq	1(%r8), %rax
	incb	%cl
                                        # kill: def $cl killed $cl killed $ecx
	shrq	%cl, %rax
	addq	(%rdi,%rax,8), %rsi
	addq	$2, %r8
	cmpq	$1000, %r8                      # imm = 0x3E8
	jne	.LBB0_1
# %bb.2:
	movq	%rsi, %rax
	retq
                                        # -- End function

@leonardo-m
Copy link
Author

Verified, and good.

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