Rust-GPU / Rust-CUDA

Ecosystem of libraries and tools for writing and executing fast GPU code fully in Rust.
Apache License 2.0
3.16k stars 120 forks source link

Incorrect arithmetics in large function #38

Open andll opened 2 years ago

andll commented 2 years ago

Hi

I am trying more examples with using crypto, and this time problem that I encounter is with integer arithmetic mismatch between GPU and CPU.

There is an example of the code: CPU and GPU

They both have same code of fiat_25519_to_bytes function, however result is slightly different for CPU and GPU

CPU OUT: [192, 72, 16, 54, 192, 98, 172, 116, 44, 128, 112, 112, 150, 42, 195, 95, 129, 14, 47, 50, 18, 198, 117, 255, 32, 79, 57, 78, 137, 92, 244, 98]
GPU OUT: [192, 72, 16, 54, 192, 98, 172, 124, 44, 128, 112, 112, 86, 106, 195, 95, 129, 14, 47, 48, 20, 198, 117, 255, 32, 63, 57, 78, 137, 92, 244, 98]

(note element 7 for example)

Understandably function is fairly large, however I was not able to reduce example. For instance, when trying to narrow down what happen I can see that basically result on this line is incorrect:

fiat_25519_addcarryx_u51(&mut x14, &mut x15, x13, x3, (x11 & 0x7ffffffffffff));

However, when taking this line out of context of large function it works perfectly fine on GPU, so the problem is likely with some kind of optimization that breaks integer arithmetics logic.

RDambrosio016 commented 2 years ago

Does it work if you put release(false) for the cuda builder?

andll commented 2 years ago

With release(false) it fails to compile like this:

Caused by:
  process didn't exit successfully: `/home/ubuntu/Rust-CUDA/target/debug/build/add-3a14f1dbd1150ab2/build-script-build` (exit status: 101)
  --- stdout
  cargo:rerun-if-changed=../../gpu/add_gpu

  --- stderr
     Compiling compiler_builtins v0.1.55
     Compiling core v0.0.0 (/home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
     Compiling autocfg v1.0.1
     Compiling libm v0.2.1
     Compiling semver-parser v0.7.0
     Compiling proc-macro2 v1.0.36
     Compiling unicode-xid v0.2.2
     Compiling syn v1.0.85
     Compiling paste v1.0.6
     Compiling semver v0.9.0
     Compiling num-traits v0.2.14
     Compiling num-integer v0.1.44
     Compiling rustc_version v0.2.3
     Compiling vek v0.15.4
     Compiling quote v1.0.14
     Compiling cuda_std_macros v0.2.0 (/home/ubuntu/Rust-CUDA/crates/cuda_std_macros)
     Compiling rustc-std-workspace-core v1.99.0 (/home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x4d4be3)[0x7f8b1213bbe3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7f8b116eb980]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x1549ec0)[0x7f8b096f0ec0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9f9)[0x7f8b096269f9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9c1)[0x7f8b096269c1]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13fdc06)[0x7f8b095a4c06]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13f9e12)[0x7f8b095a0e12]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x51f082)[0x7f8b086c6082]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4061e4)[0x7f8b085ad1e4]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4bbbed)[0x7f8b08662bed]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43ecf0)[0x7f8b085e5cf0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4a48fd)[0x7f8b0864b8fd]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43e926)[0x7f8b085e5926]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x504bb9)[0x7f8b086abbb9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(_ZN109_$LT$rustc_codegen_nvvm..NvvmCodegenBackend$u20$as$u20$rustc_codegen_ssa..traits..backend..CodegenBackend$GT$13codegen_crate17h75549d57ae9a149eE+0x1d2)[0x7f8b086bc012]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23a97a7)[0x7f8b140107a7]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(_RNvMs0_NtCsbyxJdrs97mU_15rustc_interface7queriesNtB5_7Queries15ongoing_codegen+0x6fe)[0x7f8b13ffd9ee]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2366add)[0x7f8b13fcdadd]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23632cd)[0x7f8b13fca2cd]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2367a2f)[0x7f8b13fcea2f]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2365c55)[0x7f8b13fccc55]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2385d9a)[0x7f8b13fecd9a]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/libstd-13e2ae73269b4206.so(rust_metadata_std_f24903a91e569aa2+0xa99b3)[0x7f8b119a19b3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x76db)[0x7f8b116e06db]
  /lib/x86_64-linux-gnu/libc.so.6(clone+0x3f)[0x7f8b10ffd71f]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x4d4be3)[0x7f30e109abe3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7f30e064a980]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x1549ec0)[0x7f30d86f0ec0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9f9)[0x7f30d86269f9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9c1)[0x7f30d86269c1]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13fdc06)[0x7f30d85a4c06]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13f9e12)[0x7f30d85a0e12]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x51f082)[0x7f30d76c6082]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4061e4)[0x7f30d75ad1e4]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4bbbed)[0x7f30d7662bed]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43ecf0)[0x7f30d75e5cf0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4a48fd)[0x7f30d764b8fd]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43e926)[0x7f30d75e5926]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x504bb9)[0x7f30d76abbb9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(_ZN109_$LT$rustc_codegen_nvvm..NvvmCodegenBackend$u20$as$u20$rustc_codegen_ssa..traits..backend..CodegenBackend$GT$13codegen_crate17h75549d57ae9a149eE+0x1d2)[0x7f30d76bc012]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23a97a7)[0x7f30e2f6f7a7]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(_RNvMs0_NtCsbyxJdrs97mU_15rustc_interface7queriesNtB5_7Queries15ongoing_codegen+0x6fe)[0x7f30e2f5c9ee]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2366add)[0x7f30e2f2cadd]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23632cd)[0x7f30e2f292cd]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2367a2f)[0x7f30e2f2da2f]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2365c55)[0x7f30e2f2bc55]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2385d9a)[0x7f30e2f4bd9a]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/libstd-13e2ae73269b4206.so(rust_metadata_std_f24903a91e569aa2+0xa99b3)[0x7f30e09009b3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x76db)[0x7f30e063f6db]
  /lib/x86_64-linux-gnu/libc.so.6(clone+0x3f)[0x7f30dff5c71f]
  error: could not compile `core`

  Caused by:
    process didn't exit successfully: `rustc --crate-name core --edition=2018 /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="panic_immediate_abort"' -C metadata=a1a56f98b0fb9ff3 -C extra-filename=-a1a56f98b0fb9ff3 --out-dir /home/ubuntu/Rust-CUDA/target/cuda-builder/nvptx64-nvidia-cuda/debug/deps --target nvptx64-nvidia-cuda -Z force-unstable-if-unmarked -L dependency=/home/ubuntu/Rust-CUDA/target/cuda-builder/nvptx64-nvidia-cuda/debug/deps -L dependency=/home/ubuntu/Rust-CUDA/target/cuda-builder/debug/deps --cap-lints allow -Zcodegen-backend=/home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so '-Cllvm-args=-arch=compute_61 -opt=0 --override-libm'` (signal: 11, SIGSEGV: invalid memory reference)
  warning: build failed, waiting for other jobs to finish...
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x4d4be3)[0x7f78a2af3be3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x12980)[0x7f78a20a3980]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x1549ec0)[0x7f789a0f0ec0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9f9)[0x7f789a0269f9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x147f9c1)[0x7f789a0269c1]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13fdc06)[0x7f7899fa4c06]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x13f9e12)[0x7f7899fa0e12]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x51f082)[0x7f78990c6082]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4061e4)[0x7f7898fad1e4]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4bbbed)[0x7f7899062bed]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43ecf0)[0x7f7898fe5cf0]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x4a48fd)[0x7f789904b8fd]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x43e926)[0x7f7898fe5926]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(+0x504bb9)[0x7f78990abbb9]
  /home/ubuntu/Rust-CUDA/target/debug/deps/librustc_codegen_nvvm.so(_ZN109_$LT$rustc_codegen_nvvm..NvvmCodegenBackend$u20$as$u20$rustc_codegen_ssa..traits..backend..CodegenBackend$GT$13codegen_crate17h75549d57ae9a149eE+0x1d2)[0x7f78990bc012]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23a97a7)[0x7f78a49c87a7]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(_RNvMs0_NtCsbyxJdrs97mU_15rustc_interface7queriesNtB5_7Queries15ongoing_codegen+0x6fe)[0x7f78a49b59ee]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2366add)[0x7f78a4985add]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x23632cd)[0x7f78a49822cd]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2367a2f)[0x7f78a4986a2f]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2365c55)[0x7f78a4984c55]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-713ba8e674184066.so(+0x2385d9a)[0x7f78a49a4d9a]
  /home/ubuntu/.rustup/toolchains/nightly-2021-12-04-x86_64-unknown-linux-gnu/bin/../lib/libstd-13e2ae73269b4206.so(rust_metadata_std_f24903a91e569aa2+0xa99b3)[0x7f78a23599b3]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x76db)[0x7f78a20986db]
  /lib/x86_64-linux-gnu/libc.so.6(clone+0x3f)[0x7f78a19b571f]
  error: build failed
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: BuildFailed', examples/cuda/cpu/add/build.rs:8:10
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

(upd: tried with clean target dir but got same error)

RDambrosio016 commented 2 years ago

huh, ill investigate soon, its probably something broken in how something is handled WRT LLVM. Tho i dont expect that failure to be related to the miscompile