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

asm! macro does not mark variable as used #77915

Closed
Soveu opened this issue Oct 13, 2020 · 1 comment · Fixed by #77976
Closed

asm! macro does not mark variable as used #77915

Soveu opened this issue Oct 13, 2020 · 1 comment · Fixed by #77976
Assignees
Labels
A-inline-assembly Area: inline asm!(..) A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Soveu
Copy link
Contributor

Soveu commented Oct 13, 2020

I tried this code:

unsafe fn rep_movsb(mut dest: *mut u8, mut src: *const u8, mut n: usize) -> *mut u8 {
    while n != 0 {
        asm!(
            "rep movsb",
            inout("rcx") n,
            inout("rsi") src,
            inout("rdi") dest,
        );
    }

    return dest;
}

I expected to see this happen:
Compiler doesn't generate any warnings

Instead, this happened:
Rustc says that src is unused, even though it is declared as inout in asm.
Adding arrows (inout("rsi") src => src) fixes it

Meta

rustc --version --verbose:

rustc 1.49.0-nightly (8dae8cdcc 2020-10-12)
binary: rustc
commit-hash: 8dae8cdcc8fa879cea6a4bbbfa5b32e97be4c306
commit-date: 2020-10-12
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
LLVM version: 11.0
@Soveu Soveu added the C-bug Category: This is a bug. label Oct 13, 2020
@jonas-schievink jonas-schievink added A-inline-assembly Area: inline asm!(..) A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. F-asm `#![feature(asm)]` (not `llvm_asm`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 13, 2020
@oliviacrain
Copy link
Contributor

Interesting bug! I used cargo bisect-rustc to determine that this bug has been around since the commit introducing the asm! syntax.

Looking at the MCVE case from above and doing some debugging, I found that for all live nodes in rep_movsb , src is never marked as being used in the RWU table. It only gets marked as read/written. Later on, this is part of the information used to determine that src is unused. That doesn't really make much sense though- the definition of a use does seem to fit the case where a variable is used as both an input and an output to some arbitrary assembly instruction.

I'll go ahead and claim this. I have a solution in mind, just going to ask around zulip first before putting out a PR.

@rustbot claim

JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 21, 2020
…hewjasper

Mark inout asm! operands as used in liveness pass

Variables used in `inout` operands in inline assembly (that is, they're used as both input and output to some arbitrary assembly instruction) are being marked as read and written, but are not marked as being used in the RWU table during the liveness pass. This can result in such expressions triggering an unused variable lint warning. This is incorrect behavior- reads without uses are currently only used for compound assignments. We conservatively assume that an `inout` operand is being read and used in the context of the assembly instruction.

Closes rust-lang#77915
@bors bors closed this as completed in ae95005 Oct 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: inline asm!(..) A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants