Skip to content

Optimizer believes vector's length field may alias with its own buffer #80921

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

Closed
meithecatte opened this issue Jan 11, 2021 · 3 comments
Closed
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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.

Comments

@meithecatte
Copy link
Contributor

Consider the following code:

pub fn treat_vec(v: &mut Vec<usize>) {
    if v.len() == 1 && v[0] == 42 {
        assert!(v.pop() == Some(42))
    }
}

(Playground)

I'd expect the assertion to get optimized-out. However, the following assembly is generated:

	pushq	%rax
	cmpq	$1, 16(%rdi)
	jne	.LBB0_4
# %bb.1:
	movq	(%rdi), %rax
	cmpq	$42, (%rax)
	jne	.LBB0_4
# %bb.2:
	movq	$0, 16(%rdi)
	cmpq	$42, (%rax)
	jne	.LBB0_3

.LBB0_4:
	popq	%rax
	retq

.LBB0_3:
	leaq	.L__unnamed_1(%rip), %rdi
	leaq	.L__unnamed_2(%rip), %rdx
	movl	$37, %esi
	callq	*core::panicking::panic@GOTPCREL(%rip)
	ud2

The only instruction between the two cmpq $42, (%rax) comparisons is a write to 16(%rdi). Thus, LLVM seems to believe that these two locations may alias.

@jonas-schievink jonas-schievink added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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. labels Jan 11, 2021
@nagisa
Copy link
Member

nagisa commented Jan 11, 2021

Duplicate of #54878. Thanks!

@nagisa nagisa closed this as completed Jan 11, 2021
@nagisa
Copy link
Member

nagisa commented Jan 11, 2021

And/or #71354

@jrmuizel
Copy link
Contributor

Yep, this is fixed in Nightly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. 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.
Projects
None yet
Development

No branches or pull requests

4 participants