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

Doesn't add elements to distributed slice from external crate on macOS #61

Open
vikulikov opened this issue Dec 28, 2022 · 6 comments
Open

Comments

@vikulikov
Copy link

vikulikov commented Dec 28, 2022

use linkme::distributed_slice;
use other_crate::BENCHMARKS;

#[distributed_slice(BENCHMARKS)]
static BENCH_DESERIALIZE: fn(&mut Bencher) = bench_deserialize;

fn bench_deserialize(b: &mut Bencher) {
    /* ... */
}

If BENCHMARKS is initialized in external crate the call to #[distributed_slice(BENCHMARKS)] doesn't add bench_deserialize function to the array. But if I turn on lto=true in Cargo.toml of the builded project (where BENCHMARKS is placed) lib works as expected.
I also tested this on ubuntu and it worked without lto

@vikulikov vikulikov changed the title Lib is not working for external crates on macos Linkme doesn't add elements to the distributed_slice from external crate on macos Dec 28, 2022
@vikulikov
Copy link
Author

vikulikov commented Dec 29, 2022

May be the compiler is optimizing out the crate where #[distributed_slice(BENCHMARKS)] is called? Because I don't have explicit use of the crate (only through linkme::distributed_slice). This crate is added to dependecies in the other_crate by the way.

@ncpenke
Copy link

ncpenke commented Apr 11, 2023

I ran into the issue in macOS, where BENCHMARKS is initialized in an external crate and the call to#[distributed_slice(BENCHMARKS)] isn't appending the array.

I tried the lto=true mentioned by @vikulikov above and that worked. lto=thin also worked.

@ncpenke
Copy link

ncpenke commented Apr 11, 2023

This seems to be related to the embed-bitcode flag, which seems to be set by cargo when lto is not "off" on macOS. There's no direct way to enable this flag via cargo, so this seems to be the only workaround for now. I added this for dev builds

[profile.dev]
# Need this for linkme crate to work
lto = "thin"

@dtolnay dtolnay changed the title Linkme doesn't add elements to the distributed_slice from external crate on macos Linkme doesn't add elements to the distributed_slice from external crate on macOS Apr 20, 2023
@dtolnay dtolnay changed the title Linkme doesn't add elements to the distributed_slice from external crate on macOS Doesn't add elements to distributed slice from external crate on macOS Apr 20, 2023
@coravacav
Copy link

Neither of the above solutions worked for me using Linux. Adding so that it's apparent that that's not a guaranteed fix.

@pingiun
Copy link

pingiun commented Jan 6, 2024

The workaround of setting lto = true or lto = "thin" only works for me if I also add extern crate ... to the crate using the distributed_slice

@madsmtm
Copy link

madsmtm commented Dec 14, 2024

This is an upstream issue in rustc: rust-lang/rust#133491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants