pacak / cargo-show-asm

cargo subcommand showing the assembly, LLVM-IR and MIR generated for Rust code
Apache License 2.0
712 stars 35 forks source link

Does not work on default new library #294

Closed ChaiTRex closed 4 months ago

ChaiTRex commented 4 months ago
chaitrex@PC dev % cargo new --lib testing
    Creating library `testing` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

chaitrex@PC dev % cd testing

chaitrex@PC testing % cargo asm --lib 'testing::add'
   Compiling testing v0.1.0 (/Users/chaitrex/dev/testing)
    Finished `release` profile [optimized] target(s) in 0.43s

Can't find any items matching "testing::add"

chaitrex@PC testing % cat src/lib.rs
pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

chaitrex@PC testing % cargo asm --version
Version: 0.2.38

chaitrex@PC testing % uname -a
Darwin PC.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:16:51 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8103 arm64

chaitrex@PC testing % cargo rustc -- --version --verbose
   Compiling testing v0.1.0 (/Users/chaitrex/dev/testing)
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: aarch64-apple-darwin
release: 1.79.0
LLVM version: 18.1.7
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
ChaiTRex commented 4 months ago

It appears that adding #[inline(never)] above the function header fixes the problem.

pacak commented 4 months ago

Yea, since some version rustc treats very simple functions as inline-only and doesn't generate asm for them. #[inline(never)] or in some cases #[no_mangle] fixes the problem.