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

Segfault while compiling rustc_codegen_spirv #29

Open
schell opened this issue Oct 10, 2024 · 10 comments · May be fixed by #170
Open

Segfault while compiling rustc_codegen_spirv #29

schell opened this issue Oct 10, 2024 · 10 comments · May be fixed by #170
Labels
bug Something isn't working fixed by forward porting

Comments

@schell
Copy link
Contributor

schell commented Oct 10, 2024

Expected Behaviour

cargo build should finish without error.

Example & Steps To Reproduce

Run cargo build after a clean clone of the repo.

System Info

  • Rust: rustc 1.79.0-nightly (244da22fa 2024-04-23)
  • OS: macOS 15.0 (24A335)
  • GPU: integrated

Output

error: could not compile `rustc_codegen_spirv` (lib)

Caused by:
  process didn't exit successfully: `/Users/schell/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc --crate-name rustc_codegen_spirv --edition=2021 crates/rustc_codegen_spirv/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=320 --crate-type dylib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --cfg 'feature="default"' --cfg 'feature="use-compiled-tools"' -C metadata=5d6b9f1085555723 --out-dir /Users/schell/code/rust-gpu/target/debug/deps -C incremental=/Users/schell/code/rust-gpu/target/debug/incremental -L dependency=/Users/schell/code/rust-gpu/target/debug/deps --extern ar=/Users/schell/code/rust-gpu/target/debug/deps/libar-e3ad7146ff683a59.rlib --extern either=/Users/schell/code/rust-gpu/target/debug/deps/libeither-b182a76c8248f318.rlib --extern hashbrown=/Users/schell/code/rust-gpu/target/debug/deps/libhashbrown-d177bbe31249a900.rlib --extern indexmap=/Users/schell/code/rust-gpu/target/debug/deps/libindexmap-5721173ffd4b737f.rlib --extern itertools=/Users/schell/code/rust-gpu/target/debug/deps/libitertools-91ee06ba4bd713a5.rlib --extern lazy_static=/Users/schell/code/rust-gpu/target/debug/deps/liblazy_static-4bc53d643ec30e40.rlib --extern libc=/Users/schell/code/rust-gpu/target/debug/deps/liblibc-71b3be213458ada3.rlib --extern num_traits=/Users/schell/code/rust-gpu/target/debug/deps/libnum_traits-893caca608ed540b.rlib --extern once_cell=/Users/schell/code/rust-gpu/target/debug/deps/libonce_cell-e9b1da7b0201a63a.rlib --extern regex=/Users/schell/code/rust-gpu/target/debug/deps/libregex-b32f0d5f03e3e84d.rlib --extern rspirv=/Users/schell/code/rust-gpu/target/debug/deps/librspirv-2abab5c81971212b.rlib --extern rustc_demangle=/Users/schell/code/rust-gpu/target/debug/deps/librustc_demangle-61ad1dd711dcb489.rlib --extern rustc_codegen_spirv_types=/Users/schell/code/rust-gpu/target/debug/deps/librustc_codegen_spirv_types-1b2da75106876b67.rlib --extern sanitize_filename=/Users/schell/code/rust-gpu/target/debug/deps/libsanitize_filename-86f3198c840fea75.rlib --extern smallvec=/Users/schell/code/rust-gpu/target/debug/deps/libsmallvec-814122e6d785094d.rlib --extern spirt=/Users/schell/code/rust-gpu/target/debug/deps/libspirt-3313c9c2493bc97b.rlib --extern spirv_tools=/Users/schell/code/rust-gpu/target/debug/deps/libspirv_tools-ffda6275029880c3.rlib -Dunsafe_code '-Wclippy::all' '-Wclippy::await_holding_lock' '-Wclippy::char_lit_as_u8' '-Wclippy::checked_conversions' '-Wclippy::dbg_macro' '-Wclippy::debug_assert_with_mut_call' '-Wclippy::doc_markdown' '-Wclippy::empty_enum' '-Wclippy::enum_glob_use' '-Wclippy::exit' '-Wclippy::expl_impl_clone_on_copy' '-Wclippy::explicit_deref_methods' '-Wclippy::explicit_into_iter_loop' '-Wclippy::fallible_impl_from' '-Wclippy::filter_map_next' '-Wclippy::flat_map_option' '-Wclippy::float_cmp_const' '-Wclippy::fn_params_excessive_bools' '-Wclippy::from_iter_instead_of_collect' '-Wclippy::if_let_mutex' '-Wclippy::implicit_clone' '-Wclippy::imprecise_flops' '-Wclippy::inefficient_to_string' '-Wclippy::invalid_upcast_comparisons' '-Wclippy::large_digit_groups' '-Wclippy::large_stack_arrays' '-Wclippy::large_types_passed_by_value' '-Wclippy::let_unit_value' '-Wclippy::linkedlist' '-Wclippy::lossy_float_literal' '-Wclippy::macro_use_imports' '-Wclippy::manual_ok_or' '-Wclippy::map_err_ignore' '-Wclippy::map_flatten' '-Wclippy::map_unwrap_or' '-Wclippy::match_on_vec_items' '-Wclippy::match_same_arms' '-Wclippy::match_wild_err_arm' '-Wclippy::match_wildcard_for_single_variants' '-Wclippy::mem_forget' '-Wclippy::mismatched_target_os' '-Wclippy::missing_enforced_import_renames' '-Wclippy::mut_mut' '-Wclippy::mutex_integer' '-Wclippy::needless_borrow' '-Wclippy::needless_continue' '-Wclippy::needless_for_each' '-Wclippy::option_option' '-Wclippy::path_buf_push_overwrite' '-Wclippy::ptr_as_ptr' '-Wclippy::rc_mutex' '-Wclippy::ref_option_ref' '-Wclippy::rest_pat_in_fully_bound_structs' '-Wclippy::same_functions_in_if_condition' '-Wclippy::semicolon_if_nothing_returned' '-Wclippy::single_match_else' '-Wclippy::string_add_assign' '-Wclippy::string_add' '-Wclippy::string_lit_as_bytes' '-Wclippy::string_to_string' '-Wclippy::todo' '-Wclippy::trait_duplication_in_bounds' '-Wclippy::unimplemented' '-Wclippy::unnested_or_patterns' '-Wclippy::unused_self' '-Wclippy::useless_transmute' '-Wclippy::verbose_file_reads' '-Wclippy::zero_sized_map_values' -Wfuture_incompatible -Wnonstandard_style -Wrust_2018_idioms -Aunsafe_code -L native=/Users/schell/code/rust-gpu/target/debug/build/spirv-tools-sys-a44f67dd6078deef/out` (signal: 11, SIGSEGV: invalid memory reference)
@schell schell added the bug Something isn't working label Oct 10, 2024
@schell
Copy link
Contributor Author

schell commented Oct 11, 2024

Here's how far I got with lldb

First I set up a command source file lldb_env_setup.lldb to hold all the env vars and cli options:

settings set target.env-vars CARGO=/Users/schell/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/cargo
settings set target.env-vars CARGO_CRATE_NAME=rustc_codegen_spirv
settings set target.env-vars CARGO_MANIFEST_DIR=/Users/schell/code/rust-gpu/crates/rustc_codegen_spirv
settings set target.env-vars CARGO_PKG_AUTHORS='rust-gpu developers:Embark <[email protected]>'
settings set target.env-vars CARGO_PKG_DESCRIPTION='SPIR-V code generator backend for rustc'
settings set target.env-vars CARGO_PKG_HOMEPAGE=''
settings set target.env-vars CARGO_PKG_LICENSE='MIT OR Apache-2.0'
settings set target.env-vars CARGO_PKG_LICENSE_FILE=''
settings set target.env-vars CARGO_PKG_NAME=rustc_codegen_spirv
settings set target.env-vars CARGO_PKG_README=README.md
settings set target.env-vars CARGO_PKG_REPOSITORY='https://github.com/rust-gpu/rust-gpu'
settings set target.env-vars CARGO_PKG_RUST_VERSION=''
settings set target.env-vars CARGO_PKG_VERSION=0.9.0
settings set target.env-vars CARGO_PKG_VERSION_MAJOR=0
settings set target.env-vars CARGO_PKG_VERSION_MINOR=9
settings set target.env-vars CARGO_PKG_VERSION_PATCH=0
settings set target.env-vars CARGO_PKG_VERSION_PRE=''
settings set target.env-vars CARGO_PRIMARY_PACKAGE=1
settings set target.env-vars CARGO_RUSTC_CURRENT_DIR=/Users/schell/code/rust-gpu
settings set target.env-vars DYLD_FALLBACK_LIBRARY_PATH='/Users/schell/code/rust-gpu/target/debug/deps:/Users/schell/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/lib:/Users/schell/lib:/usr/local/lib:/usr/lib'
settings set target.env-vars OUT_DIR=/Users/schell/code/rust-gpu/target/debug/build/rustc_codegen_spirv-6bf5e31abd8107a7/out

target create /Users/schell/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc

process launch --  --crate-name rustc_codegen_spirv --edition=2021 crates/rustc_codegen_spirv/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=202 --crate-type dylib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --cfg 'feature="default"' --cfg 'feature="use-compiled-tools"' -C metadata=c01b1899eb4284a6 --out-dir /Users/schell/code/rust-gpu/target/debug/deps -C incremental=/Users/schell/code/rust-gpu/target/debug/incremental -L dependency=/Users/schell/code/rust-gpu/target/debug/deps --extern ar=/Users/schell/code/rust-gpu/target/debug/deps/libar-e3ad7146ff683a59.rlib --extern either=/Users/schell/code/rust-gpu/target/debug/deps/libeither-b182a76c8248f318.rlib --extern hashbrown=/Users/schell/code/rust-gpu/target/debug/deps/libhashbrown-6c72dab48672ca38.rlib --extern indexmap=/Users/schell/code/rust-gpu/target/debug/deps/libindexmap-6bda499f2b061353.rlib --extern itertools=/Users/schell/code/rust-gpu/target/debug/deps/libitertools-91ee06ba4bd713a5.rlib --extern lazy_static=/Users/schell/code/rust-gpu/target/debug/deps/liblazy_static-4bc53d643ec30e40.rlib --extern libc=/Users/schell/code/rust-gpu/target/debug/deps/liblibc-e18c03c84dd0c02a.rlib --extern num_traits=/Users/schell/code/rust-gpu/target/debug/deps/libnum_traits-1d980cb5a4875be2.rlib --extern once_cell=/Users/schell/code/rust-gpu/target/debug/deps/libonce_cell-771796d3f5371d07.rlib --extern regex=/Users/schell/code/rust-gpu/target/debug/deps/libregex-9c2651fc8523aa8a.rlib --extern rspirv=/Users/schell/code/rust-gpu/target/debug/deps/librspirv-9e368a3016245c72.rlib --extern rustc_demangle=/Users/schell/code/rust-gpu/target/debug/deps/librustc_demangle-61ad1dd711dcb489.rlib --extern rustc_codegen_spirv_types=/Users/schell/code/rust-gpu/target/debug/deps/librustc_codegen_spirv_types-3d976a4394529b29.rlib --extern sanitize_filename=/Users/schell/code/rust-gpu/target/debug/deps/libsanitize_filename-c0f3b53de5315d3c.rlib --extern smallvec=/Users/schell/code/rust-gpu/target/debug/deps/libsmallvec-d36e0788f4d18c2f.rlib --extern spirt=/Users/schell/code/rust-gpu/target/debug/deps/libspirt-f6c8c35ab9a8f5c2.rlib --extern spirv_tools=/Users/schell/code/rust-gpu/target/debug/deps/libspirv_tools-554835daf8d19504.rlib -Dunsafe_code '-Wclippy::all' '-Wclippy::await_holding_lock' '-Wclippy::char_lit_as_u8' '-Wclippy::checked_conversions' '-Wclippy::dbg_macro' '-Wclippy::debug_assert_with_mut_call' '-Wclippy::doc_markdown' '-Wclippy::empty_enum' '-Wclippy::enum_glob_use' '-Wclippy::exit' '-Wclippy::expl_impl_clone_on_copy' '-Wclippy::explicit_deref_methods' '-Wclippy::explicit_into_iter_loop' '-Wclippy::fallible_impl_from' '-Wclippy::filter_map_next' '-Wclippy::flat_map_option' '-Wclippy::float_cmp_const' '-Wclippy::fn_params_excessive_bools' '-Wclippy::from_iter_instead_of_collect' '-Wclippy::if_let_mutex' '-Wclippy::implicit_clone' '-Wclippy::imprecise_flops' '-Wclippy::inefficient_to_string' '-Wclippy::invalid_upcast_comparisons' '-Wclippy::large_digit_groups' '-Wclippy::large_stack_arrays' '-Wclippy::large_types_passed_by_value' '-Wclippy::let_unit_value' '-Wclippy::linkedlist' '-Wclippy::lossy_float_literal' '-Wclippy::macro_use_imports' '-Wclippy::manual_ok_or' '-Wclippy::map_err_ignore' '-Wclippy::map_flatten' '-Wclippy::map_unwrap_or' '-Wclippy::match_on_vec_items' '-Wclippy::match_same_arms' '-Wclippy::match_wild_err_arm' '-Wclippy::match_wildcard_for_single_variants' '-Wclippy::mem_forget' '-Wclippy::mismatched_target_os' '-Wclippy::missing_enforced_import_renames' '-Wclippy::mut_mut' '-Wclippy::mutex_integer' '-Wclippy::needless_borrow' '-Wclippy::needless_continue' '-Wclippy::needless_for_each' '-Wclippy::option_option' '-Wclippy::path_buf_push_overwrite' '-Wclippy::ptr_as_ptr' '-Wclippy::rc_mutex' '-Wclippy::ref_option_ref' '-Wclippy::rest_pat_in_fully_bound_structs' '-Wclippy::same_functions_in_if_condition' '-Wclippy::semicolon_if_nothing_returned' '-Wclippy::single_match_else' '-Wclippy::string_add_assign' '-Wclippy::string_add' '-Wclippy::string_lit_as_bytes' '-Wclippy::string_to_string' '-Wclippy::todo' '-Wclippy::trait_duplication_in_bounds' '-Wclippy::unimplemented' '-Wclippy::unnested_or_patterns' '-Wclippy::unused_self' '-Wclippy::useless_transmute' '-Wclippy::verbose_file_reads' '-Wclippy::zero_sized_map_values' -Wfuture_incompatible -Wnonstandard_style -Wrust_2018_idioms -Aunsafe_code -L native=/Users/schell/code/rust-gpu/target/debug/build/spirv-tools-sys-d4c0736b4c5521a0/out

Then I invoke lldb and do:

command source lldb_env_setup.lldb

Which then shows:

Process 13821 launched: '/Users/schell/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc' (arm64)
{"$message_type":"artifact","artifact":"/Users/schell/code/rust-gpu/target/debug/deps/rustc_codegen_spirv.d","emit":"dep-info"}
Process 13821 stopped
* thread #8, name = 'opt 4zue6wczm9w1glco', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000010cea5d04 librustc_driver-ca847c7e3a88e281.dylib`llvm::AArch64RegisterBankInfo::isLoadFromFPType(llvm::MachineInstr const&) const + 212
librustc_driver-ca847c7e3a88e281.dylib`llvm::AArch64RegisterBankInfo::isLoadFromFPType:
->  0x10cea5d04 <+212>: ldr    x9, [x8]
    0x10cea5d08 <+216>: ldrb   w8, [x9, #0x8]
    0x10cea5d0c <+220>: cmp    w8, #0x10
    0x10cea5d10 <+224>: ccmp   x9, #0x0, #0x4, eq
Target 3: (rustc) stopped.

@schell
Copy link
Contributor Author

schell commented Oct 11, 2024

It builds in --release! But it won't otherwise.

@utensil
Copy link

utensil commented Oct 11, 2024

I have the same issue, cargo run --release works for example runners/wgpu, but has the same error if not with --release.

@LegNeato
Copy link
Collaborator

LegNeato commented Oct 11, 2024

Interesting, CI only appears to check x86_64 on Mac:

os: macOS-latest
target: x86_64-apple-darwin

Though the docs say latest is arm? 🤔

https://docs.github.com/en/actions/using-github-hosted-runners/using-github-hosted-runners/about-github-hosted-runners#standard-github-hosted-runners-for-public-repositories

And it looks like CI only uses --release 😩

@LegNeato
Copy link
Collaborator

LegNeato commented Oct 13, 2024

I was able to update rust-gpu to nightly-2024-06-20 and this crash went away. It was still there under nightly-2024-05-20.

Many of the tests fail with hacks to get around the alloca changes preventing us from updating (rust-lang/rust@f4426c1) but the compiling works at least which points to an old llvm or rustc issue unrelated to rust-gpu.

@LegNeato
Copy link
Collaborator

LegNeato commented Oct 14, 2024

@eddyb found the cause:

nightly-2024-05-21 has the fix

@eddyb
Copy link
Collaborator

eddyb commented Oct 14, 2024

Likely culprit: llvm/llvm-project@70579c9

This leaves Feb 14th - May 20th as the range with the bug, i.e. between:

Looking at the report (rust-lang/rust#125053), it is weirdly specific. I wonder if it's in code we control.

* thread #8, name = 'opt 4zue6wczm9w1glco'

@schell if you have the time, it might be useful to try to add -C save-temps and hopefully get access to the 4zue6wczm9w1glco codegen-unit before it's optimized.

@schell
Copy link
Contributor Author

schell commented Oct 14, 2024

Nice sleuthing @eddyb and @LegNeato. I'll try it out when I can 🙇 .

@eddyb
Copy link
Collaborator

eddyb commented Oct 14, 2024

Found a relatively low-effort way to inject lldb (doesn't even cause rebuilds!):

$ echo '"$@" || (lldb -b -o "process launch" -- "$@" && exit 99)' > wrapper.sh
$ chmod +x wrapper.sh
$ RUSTC_WRAPPER=./wrapper.sh cargo rustc -p rustc_codegen_spirv --no-default-features --features use-installed-tools -- -C save-temps
   Compiling rustc_codegen_spirv v0.9.0 (/Users/eddyb/Code/rust-gpu/crates/rustc_codegen_spirv)
/Users/eddyb/Code/rust-gpu/./wrapper.sh: line 1: 12630 Segmentation fault: 11  "$@"
(lldb) target create "/Users/eddyb/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc"
Current executable set to '/Users/eddyb/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc' (arm64).
(lldb) settings set -- target.run-args  "--crate-name" "rustc_codegen_spirv" "--edition=2021" "crates/rustc_codegen_spirv/src/lib.rs" "--error-format=json" "--json=diagnostic-rendered-ansi,artifacts,future-incompat" "--diagnostic-width=147" "--crate-type" "dylib" "--emit=dep-info,link" "-C" "embed-bitcode=no" "-C" "debuginfo=2" "-C" "split-debuginfo=unpacked" "-C" "save-temps" "--cfg" "feature=\"use-installed-tools\"" "-C" "metadata=d3f81d8f7c25b8cf" "--out-dir" "/Users/eddyb/Code/rust-gpu/target/debug/deps" "-C" "incremental=/Users/eddyb/Code/rust-gpu/target/debug/incremental" "-L" "dependency=/Users/eddyb/Code/rust-gpu/target/debug/deps" "--extern" "ar=/Users/eddyb/Code/rust-gpu/target/debug/deps/libar-e3ad7146ff683a59.rlib" "--extern" "either=/Users/eddyb/Code/rust-gpu/target/debug/deps/libeither-b182a76c8248f318.rlib" "--extern" "hashbrown=/Users/eddyb/Code/rust-gpu/target/debug/deps/libhashbrown-d17b05ac8765534d.rlib" "--extern" "indexmap=/Users/eddyb/Code/rust-gpu/target/debug/deps/libindexmap-5721173ffd4b737f.rlib" "--extern" "itertools=/Users/eddyb/Code/rust-gpu/target/debug/deps/libitertools-91ee06ba4bd713a5.rlib" "--extern" "lazy_static=/Users/eddyb/Code/rust-gpu/target/debug/deps/liblazy_static-4bc53d643ec30e40.rlib" "--extern" "libc=/Users/eddyb/Code/rust-gpu/target/debug/deps/liblibc-1fb46c3556945141.rlib" "--extern" "num_traits=/Users/eddyb/Code/rust-gpu/target/debug/deps/libnum_traits-893caca608ed540b.rlib" "--extern" "once_cell=/Users/eddyb/Code/rust-gpu/target/debug/deps/libonce_cell-e9b1da7b0201a63a.rlib" "--extern" "regex=/Users/eddyb/Code/rust-gpu/target/debug/deps/libregex-b32f0d5f03e3e84d.rlib" "--extern" "rspirv=/Users/eddyb/Code/rust-gpu/target/debug/deps/librspirv-99fce9813432e2d2.rlib" "--extern" "rustc_demangle=/Users/eddyb/Code/rust-gpu/target/debug/deps/librustc_demangle-61ad1dd711dcb489.rlib" "--extern" "rustc_codegen_spirv_types=/Users/eddyb/Code/rust-gpu/target/debug/deps/librustc_codegen_spirv_types-b835e0468acc55e4.rlib" "--extern" "sanitize_filename=/Users/eddyb/Code/rust-gpu/target/debug/deps/libsanitize_filename-86f3198c840fea75.rlib" "--extern" "smallvec=/Users/eddyb/Code/rust-gpu/target/debug/deps/libsmallvec-503084d070b4a84a.rlib" "--extern" "spirt=/Users/eddyb/Code/rust-gpu/target/debug/deps/libspirt-ce67463698cb406c.rlib" "--extern" "spirv_tools=/Users/eddyb/Code/rust-gpu/target/debug/deps/libspirv_tools-1edd2bdab4924908.rlib" "-Dunsafe_code" "-Wclippy::all" "-Wclippy::await_holding_lock" "-Wclippy::char_lit_as_u8" "-Wclippy::checked_conversions" "-Wclippy::dbg_macro" "-Wclippy::debug_assert_with_mut_call" "-Wclippy::doc_markdown" "-Wclippy::empty_enum" "-Wclippy::enum_glob_use" "-Wclippy::exit" "-Wclippy::expl_impl_clone_on_copy" "-Wclippy::explicit_deref_methods" "-Wclippy::explicit_into_iter_loop" "-Wclippy::fallible_impl_from" "-Wclippy::filter_map_next" "-Wclippy::flat_map_option" "-Wclippy::float_cmp_const" "-Wclippy::fn_params_excessive_bools" "-Wclippy::from_iter_instead_of_collect" "-Wclippy::if_let_mutex" "-Wclippy::implicit_clone" "-Wclippy::imprecise_flops" "-Wclippy::inefficient_to_string" "-Wclippy::invalid_upcast_comparisons" "-Wclippy::large_digit_groups" "-Wclippy::large_stack_arrays" "-Wclippy::large_types_passed_by_value" "-Wclippy::let_unit_value" "-Wclippy::linkedlist" "-Wclippy::lossy_float_literal" "-Wclippy::macro_use_imports" "-Wclippy::manual_ok_or" "-Wclippy::map_err_ignore" "-Wclippy::map_flatten" "-Wclippy::map_unwrap_or" "-Wclippy::match_on_vec_items" "-Wclippy::match_same_arms" "-Wclippy::match_wild_err_arm" "-Wclippy::match_wildcard_for_single_variants" "-Wclippy::mem_forget" "-Wclippy::mismatched_target_os" "-Wclippy::missing_enforced_import_renames" "-Wclippy::mut_mut" "-Wclippy::mutex_integer" "-Wclippy::needless_borrow" "-Wclippy::needless_continue" "-Wclippy::needless_for_each" "-Wclippy::option_option" "-Wclippy::path_buf_push_overwrite" "-Wclippy::ptr_as_ptr" "-Wclippy::rc_mutex" "-Wclippy::ref_option_ref" "-Wclippy::rest_pat_in_fully_bound_structs" "-Wclippy::same_functions_in_if_condition" "-Wclippy::semicolon_if_nothing_returned" "-Wclippy::single_match_else" "-Wclippy::string_add_assign" "-Wclippy::string_add" "-Wclippy::string_lit_as_bytes" "-Wclippy::string_to_string" "-Wclippy::todo" "-Wclippy::trait_duplication_in_bounds" "-Wclippy::unimplemented" "-Wclippy::unnested_or_patterns" "-Wclippy::unused_self" "-Wclippy::useless_transmute" "-Wclippy::verbose_file_reads" "-Wclippy::zero_sized_map_values" "-Wfuture_incompatible" "-Wnonstandard_style" "-Wrust_2018_idioms" "-Aunsafe_code"
(lldb) process launch
{"$message_type":"diagnostic","message":"failed to connect to jobserver from environment variable `CARGO_MAKEFLAGS=\"-j --jobserver-fds=8,9 --jobserver-auth=8,9\"`: cannot open file descriptor 8 from the jobserver environment variable value: Bad file descriptor (os error 9)","code":null,"level":"warning","spans":[],"children":[{"message":"the build environment is likely misconfigured","code":null,"level":"note","spans":[],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: failed to connect to jobserver from environment variable `CARGO_MAKEFLAGS=\"-j --jobserver-fds=8,9 --jobserver-auth=8,9\"`: cannot open file descriptor 8 from the jobserver environment variable value: Bad file descriptor (os error 9)\u001b[0m\n\u001b[0m  \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m  \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: the build environment is likely misconfigured\u001b[0m\n\n"}
{"$message_type":"artifact","artifact":"/Users/eddyb/Code/rust-gpu/target/debug/deps/rustc_codegen_spirv.d","emit":"dep-info"}
Process 12653 stopped
* thread #8, name = 'opt 4q5ivfep7qt5yoow', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x000000010d2edd04 librustc_driver-ca847c7e3a88e281.dylib`llvm::AArch64RegisterBankInfo::isLoadFromFPType(llvm::MachineInstr const&) const + 212
librustc_driver-ca847c7e3a88e281.dylib`llvm::AArch64RegisterBankInfo::isLoadFromFPType:
->  0x10d2edd04 <+212>: ldr    x9, [x8]
    0x10d2edd08 <+216>: ldrb   w8, [x9, #0x8]
    0x10d2edd0c <+220>: cmp    w8, #0x10
    0x10d2edd10 <+224>: ccmp   x9, #0x0, #0x4, eq
Target 0: (rustc) stopped.
Process 12653 launched: '/Users/eddyb/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/bin/rustc' (arm64)
error: could not compile `rustc_codegen_spirv` (lib)

Then:

$ env $(rustc --print=sysroot)/lib/rustlib/aarch64-apple-darwin/bin/llvm-dis \
  target/debug/deps/rustc_codegen_spirv.4q5ivfep7qt5yoow.rcgu.no-opt.bc
$ rg '[^,{]( \{ )+\{\}, ' target/debug/deps/rustc_codegen_spirv.4q5ivfep7qt5yoow.rcgu.no-opt.ll
12:@_RNvNvMsh_NtNtCsbqr1HCq8ilm_12rustc_middle2ty4listINtB7_7RawListupE5empty5EMPTY = external global { {}, i64, [7 x i64], [0 x {}] }
$ cargo install rustfilt
$ rg '[^,{]( \{ )+\{\}, ' target/debug/deps/rustc_codegen_spirv.4q5ivfep7qt5yoow.rcgu.no-opt.ll | rustfilt
@<rustc_middle::ty::list::RawList<(), _>>::empty::EMPTY = external global { {}, i64, [7 x i64], [0 x {}] }

(note the 4q5ivfep7qt5yoow I took from the crashing thread name!)

Now, if it is that symbol, that's bad news because it's in rustc itself, i.e.:

$ nm ~/.rustup/toolchains/nightly-2024-04-24-aarch64-apple-darwin/lib/librustc_driver-ca847c7e3a88e281.dylib \
  | rg _RNvNvMsh_NtNtCsbqr1HCq8ilm_12rustc_middle2ty4listINtB7_7RawListupE5empty5EMPTY
000000000506c680 S __RNvNvMsh_NtNtCsbqr1HCq8ilm_12rustc_middle2ty4listINtB7_7RawListupE5empty5EMPTY

Does this mean nobody on M1 could've contributed to rustc in rust-lang/rust? Or is this "unoptimized" mode so unlikely to be applied to rustc development? (Or am I looking at the wrong plausible cause)

EDIT: the real culprit is a load from that global variable:

$ rg 'load.*@_RNvNvMsh_NtNtCsbqr1HCq8ilm_12rustc_middle2ty4listINtB7_7RawListupE5empty5EMPTY' target/debug/deps/rustc_codegen_spirv.4q5ivfep7qt5yoow.rcgu.ll
17832:  %119 = load i64, ptr @_RNvNvMsh_NtNtCsbqr1HCq8ilm_12rustc_middle2ty4listINtB7_7RawListupE5empty5EMPTY, align 8, !dbg !68816

(not sure yet if we can avoid it, but optimizations simply replace it with 0...)

@eddyb
Copy link
Collaborator

eddyb commented Nov 27, 2024

We have two options to deal with this:

  • detect the combination of aarch64 and debug mode and error explicitly
    • this should work even with just cfgs, (the bug happens while compiling rustc_codegen_spirv, but all the way down in LLVM, so e.g. compile_error! should still prevent it)
    • we could tell the user to use release mode, or if build scripts are involved (not sure if we can automatically detect that), add workspace-level Cargo.toml overrides like the ones we use
  • try to bypass rustc_codegen_ssa's MIR Return terminator codegen under the assumption that it's the only root cause
    • in theory, we could always support PassMode::Indirect, we've just ignored it
      (worst case, the inliner has to inline calls passing "out" pointers illegally)
    • to keep at least some things returned by-value we could use PassMode::Cast, when there's an obvious mapping to the [somewhat strange CastTarget` system](https://github.com/rust-lang/rust/blob/244da22fabd9fa677bbd0ac601a88e5ca6917526/compiler/rustc_target/src/abi/call/mod.rs#L190-L301)
      • if we get to lower multi-element Uniform (with integer/float elements) into vectors instead of structs, that might cover a lot of types, but we don't even get integer signedness
      • alternatively, we could "cheat" by making cast_backend_type(cast_ty) return a special placeholder (ignoring cast_ty's contents), which load+ret special-case to give the original type
    • we control everything else via store_arg/store_fn_arg/fn_decl_backend_type, so the function signatures can remain whatever we want them to be, at the SPIR-V level
    • to avoid PassMode::Cast insanity, we could "just" always use PassMode::Indirect, and pretend the signature has an extra pointer at the start (which is, instead, a local variable, that we can load on ret_void, to do the actual return)
  • vendor a fixed copy of rustc_codegen_ssa and/or build one on the user's machine, from the source code provided by the rustc-dev rustup component

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed by forward porting
Projects
None yet
4 participants