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

Guest fails to build crossbeam due to atomics #444

Open
SchmErik opened this issue Mar 16, 2023 · 2 comments
Open

Guest fails to build crossbeam due to atomics #444

SchmErik opened this issue Mar 16, 2023 · 2 comments
Labels
rust guest workarounds A rust crate is not working on the guest

Comments

@SchmErik
Copy link
Contributor

SchmErik commented Mar 16, 2023

Crate Name: crossbeam https://crates.io/crates/crossbeam

Description of Error

Rust's crossbeam crate is used for concurrent programming. Because the zkvm is single threaded, it's likely that users do not depend on crossbeam directly. However, the are crates such as bevy-ecs depend on the crossbeam-utils or crossbeam-channels. When trying to build these crates you may see errors like this:

methods-guest: error[E0432]: unresolved imports `core::sync::atomic::AtomicI64`, `core::sync::atomic::AtomicU64`   
methods-guest:   --> /Users/erik/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.14/src/lib.rs:79:49   
methods-guest:    |   
methods-guest: 79 |             pub(crate) use core::sync::atomic::{AtomicI64, AtomicU64};   
methods-guest:    |                                                 ^^^^^^^^^  ^^^^^^^^^ no `AtomicU64` in `sync::atomic`   
methods-guest:    |                                                 |   
methods-guest:    |                                                 no `AtomicI64` in `sync::atomic`   
methods-guest:    |   
methods-guest: help: a similar name exists in the module   
methods-guest:    |   
methods-guest: 79 |             pub(crate) use core::sync::atomic::{AtomicI8, AtomicU64};   
methods-guest:    |                                                 ~~~~~~~~   
methods-guest: help: a similar name exists in the module   
methods-guest:    |   
methods-guest: 79 |             pub(crate) use core::sync::atomic::{AtomicI64, AtomicU8};   
methods-guest:    |                                                            ~~~~~~~~   
methods-guest:    
methods-guest: error[E0412]: cannot find type `AtomicU64` in module `core::sync::atomic`   
methods-guest:     --> /Users/erik/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.14/src/atomic/consume.rs:78:14   
methods-guest:      |   
methods-guest: 78   |   impl_atomic!(AtomicU64, u64);   
methods-guest:      |                ^^^^^^^^^ help: a struct with a similar name exists: `AtomicU16`   
methods-guest:      |   
methods-guest:     ::: /Users/erik/risc0-playground/target/debug/build/methods-1063287a65fb6768/out/rust-std_c329f97eff90458a28d6e09fcda804c9f781624a8e83b697b7d5f3c283a12bd6/library/core/src/sync/atomic.rs:2761:1   
methods-guest:      |   
methods-guest: 2761 | / atomic_int! {   
methods-guest: 2762 | |     cfg(target_has_atomic = "16"),   
methods-guest: 2763 | |     cfg(target_has_atomic_equal_alignment = "16"),   
methods-guest: 2764 | |     stable(feature = "integer_atomics_stable", since = "1.34.0"),   
methods-guest: ...    |   
methods-guest: 2778 | |     u16 AtomicU16 ATOMIC_U16_INIT   
methods-guest: 2779 | | }   
methods-guest:      | |_- similarly named struct `AtomicU16` defined here   
methods-guest:    
methods-guest: error[E0412]: cannot find type `AtomicI64` in module `core::sync::atomic`   
methods-guest:     --> /Users/erik/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.14/src/atomic/consume.rs:80:14   
methods-guest:      |   
methods-guest: 80   |   impl_atomic!(AtomicI64, i64);   
methods-guest:      |                ^^^^^^^^^ help: a struct with a similar name exists: `AtomicI16`   
methods-guest:      |   
methods-guest:     ::: /Users/erik/risc0-playground/target/debug/build/methods-1063287a65fb6768/out/rust-std_c329f97eff90458a28d6e09fcda804c9f781624a8e83b697b7d5f3c283a12bd6/library/core/src/sync/atomic.rs:2741:1   
methods-guest:      |   
methods-guest: 2741 | / atomic_int! {   
methods-guest: 2742 | |     cfg(target_has_atomic = "16"),   
methods-guest: 2743 | |     cfg(target_has_atomic_equal_alignment = "16"),   
methods-guest: 2744 | |     stable(feature = "integer_atomics_stable", since = "1.34.0"),   
methods-guest: ...    |   
methods-guest: 2758 | |     i16 AtomicI16 ATOMIC_I16_INIT   
methods-guest: 2759 | | }   
methods-guest:      | |_- similarly named struct `AtomicI16` defined here   
methods-guest:    
methods-guest:    Compiling bit-vec v0.6.3   
methods-guest:    Compiling erased-serde v0.3.25   
methods-guest: Some errors have detailed explanations: E0412, E0432.   
methods-guest: For more information about an error, try `rustc --explain E0412`.   
methods-guest: error: could not compile `crossbeam-utils` due to 3 previous errors

This occurs because crossbeam makes an assumption that risc0 zkvm supports 64 bit atomics (which it does not).

Workarounds

As a temporary workaround, when using crates that depend on crossbeam-utils or crossbeam-channels, use risc0's fork of crossbeam by adding the following line under the [patch.crates-io] section in your Cargo.toml file.

[patch.crates-io]
crossbeam = { git = "https://github.com/risc0/crossbeam", rev = "b25eb50f8c193f36dacb6739692261ea96827bb7" }
crossbeam-utils = { git = "https://github.com/risc0/crossbeam", rev = "b25eb50f8c193f36dacb6739692261ea96827bb7" }
crossbeam-channel = { git = "https://github.com/risc0/crossbeam", rev = "b25eb50f8c193f36dacb6739692261ea96827bb7" }

After adding this patch, run cargo update inside the guest directory to update the dependencies!

Long term solution(s)

Add the risc0 zkvm target triple to upstream rustc. crossbeam uses rustc to dump all supported targets and their configurations to determine the max atomic support. Note: risc0's zkvm is single-threaded, non-preemptive, and does not reorder instructions so we don't need atomics instructions or fence instructions.

@SchmErik SchmErik added the rust guest workarounds A rust crate is not working on the guest label Mar 16, 2023
@mnshah
Copy link

mnshah commented May 4, 2023

Running into a similar issue from the Radium 0.7.0 crate:

zero-raf-methods-guest: error[E0432]: unresolved imports `core::sync::atomic::AtomicI64`, `core::sync::atomic::AtomicU64`
zero-raf-methods-guest:   --> /Users/mnshah/.cargo/registry/src/github.com-1ecc6299db9ec823/radium-0.7.0/src/lib.rs:53:34
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI64, AtomicU64};
zero-raf-methods-guest:    |                                  ^^^^^^^^^  ^^^^^^^^^ no `AtomicU64` in `sync::atomic`
zero-raf-methods-guest:    |                                  |
zero-raf-methods-guest:    |                                  no `AtomicI64` in `sync::atomic`
zero-raf-methods-guest:    |
zero-raf-methods-guest: help: a similar name exists in the module
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI8, AtomicU64};
zero-raf-methods-guest:    |                                  ~~~~~~~~
zero-raf-methods-guest: help: a similar name exists in the module
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI64, AtomicU8};
zero-raf-methods-guest:    |                                             ~~~~~~~~

@SchmErik
Copy link
Contributor Author

SchmErik commented May 5, 2023

Running into a similar issue from the Radium 0.7.0 crate:

zero-raf-methods-guest: error[E0432]: unresolved imports `core::sync::atomic::AtomicI64`, `core::sync::atomic::AtomicU64`
zero-raf-methods-guest:   --> /Users/mnshah/.cargo/registry/src/github.com-1ecc6299db9ec823/radium-0.7.0/src/lib.rs:53:34
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI64, AtomicU64};
zero-raf-methods-guest:    |                                  ^^^^^^^^^  ^^^^^^^^^ no `AtomicU64` in `sync::atomic`
zero-raf-methods-guest:    |                                  |
zero-raf-methods-guest:    |                                  no `AtomicI64` in `sync::atomic`
zero-raf-methods-guest:    |
zero-raf-methods-guest: help: a similar name exists in the module
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI8, AtomicU64};
zero-raf-methods-guest:    |                                  ~~~~~~~~
zero-raf-methods-guest: help: a similar name exists in the module
zero-raf-methods-guest:    |
zero-raf-methods-guest: 53 |         use core::sync::atomic::{AtomicI64, AtomicU8};
zero-raf-methods-guest:    |                                             ~~~~~~~~

This is a separate issue and I wrote a work around here #549

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust guest workarounds A rust crate is not working on the guest
Projects
None yet
Development

No branches or pull requests

2 participants