rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
907 stars 61 forks source link

Targeting a specific MIPS arch: internal compiler error on building `compiler_builtins` stemming from ABI troubles. #546

Open lowercase-g opened 1 month ago

lowercase-g commented 1 month ago

target.json in question:

{
  "arch": "mips64r5",
  "data-layout": "e-m:m-p:64:32-i8:8:32-i16:16:32-i64:64-n32-S64",
  "dynamic-linking": false,
  "executables": true,
  "linker": "mipsel-none-elf-ld",
  "linker-flavor": "ld",
  "llvm-target": "mipsel-none-elf",
  "llvm-args": "-mxgot",
  "max-atomic-width": 32,
  "features": "+soft_float",
  "os": "none",
  "panic-strategy": "abort",
  "position-independent-executables": false,
  "relro-level": "full",
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-pointer-width": "64",
  "vendor": "unknown"
}

which, after some wrangling, can build core, but not compiler_builtins:


error: internal compiler error: src/context.rs:605:21: `fn_abi_of_instance(conv::__floatunsisf, [])` failed: AdjustForForeignAbi(Unsupported { arch: "mips64r5", abi: C { unwind: false } })
   --> /home/gabca/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.109/src/macros.rs:491:9
    |
491 |           pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
   ::: /home/gabca/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.109/src/float/conv.rs:74:1
    |
74  | / intrinsics! {
75  | |     #[arm_aeabi_alias = __aeabi_ui2f]
76  | |     pub extern "C" fn __floatunsisf(i: u32) -> f32 {
77  | |         f32::from_bits(int_to_float::u32_to_f32_bits(i))
...   |
103 | |     }
104 | | }
    | |_- in this macro invocation
    |
    = note: this error: internal compiler error originates in the macro `intrinsics` (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at src/context.rs:605:21:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_errors::DiagCtxtHandle::span_bug
   3: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   4: rustc_middle::ty::context::tls::with_opt::{{closure}}
   5: rustc_middle::ty::context::tls::with_context_opt
   6: rustc_middle::util::bug::span_bug_fmt
   7: <rustc_codegen_gcc::context::CodegenCx as rustc_middle::ty::layout::FnAbiOfHelpers>::handle_fn_abi_err
   8: rustc_middle::ty::layout::FnAbiOf::fn_abi_of_instance::{{closure}}
   9: rustc_middle::ty::layout::FnAbiOf::fn_abi_of_instance
  10: rustc_codegen_gcc::mono_item::<impl rustc_codegen_ssa::traits::declare::PreDefineMethods for rustc_codegen_gcc::context::CodegenCx>::predefine_fn
  11: rustc_codegen_gcc::base::compile_codegen_unit::module_codegen
  12: rustc_codegen_gcc::base::compile_codegen_unit
  13: rustc_codegen_ssa::base::codegen_crate
  14: <rustc_codegen_gcc::GccCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  15: <rustc_interface::queries::Queries>::codegen_and_build_linker
  16: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

note: please attach the file at `/home/gabca/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.109/rustc-ice-2024-07-28T15_04_39-617670.txt` to your bug report

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -Z force-unstable-if-unmarked -Z codegen-backend=/home/gabca/rustc_codegen_gcc/target/release/librustc_codegen_gcc.so

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
warning: `compiler_builtins` (lib) generated 1 warning
error: could not compile `compiler_builtins` (lib); 1 warning emitted
warning: build failed, waiting for other jobs to finish...
error: internal compiler error: src/context.rs:605:21: `fn_abi_of_instance(CString::from_raw::strlen, [])` failed: AdjustForForeignAbi(Unsupported { arch: "mips64r5", abi: C { unwind: false } })
   --> /home/gabca/.rustup/toolchains/nightly-2024-07-02-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/ffi/c_str.rs:409:17
    |
409 |                 fn strlen(s: *const c_char) -> usize;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

thread 'rustc' panicked at src/context.rs:605:21:
Box<dyn Any>
stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: <rustc_errors::diagnostic::BugAbort as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
   2: rustc_errors::DiagCtxtHandle::span_bug
   3: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
   4: rustc_middle::ty::context::tls::with_opt::{{closure}}
   5: rustc_middle::ty::context::tls::with_context_opt
   6: rustc_middle::util::bug::span_bug_fmt
   7: <rustc_codegen_gcc::context::CodegenCx as rustc_middle::ty::layout::FnAbiOfHelpers>::handle_fn_abi_err
   8: <rustc_codegen_gcc::builder::Builder as rustc_middle::ty::layout::FnAbiOfHelpers>::handle_fn_abi_err
   9: rustc_middle::ty::layout::FnAbiOf::fn_abi_of_instance::{{closure}}
  10: rustc_codegen_ssa::mir::block::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_terminator
  11: rustc_codegen_ssa::mir::codegen_mir
  12: rustc_codegen_ssa::base::codegen_instance
  13: <rustc_middle::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::define
  14: rustc_codegen_gcc::base::compile_codegen_unit::module_codegen
  15: rustc_codegen_gcc::base::compile_codegen_unit
  16: rustc_codegen_ssa::base::codegen_crate
  17: <rustc_codegen_gcc::GccCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
  18: <rustc_interface::queries::Queries>::codegen_and_build_linker
  19: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}

https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html documents a from-abi value that could fix this hopefully. No idea how to go about getting rustc_codegen_gcc to emit that though.

antoyo commented 1 month ago

If you want to add a GCC flag, you can do it as such:

./y.sh cargo rustc --bin project-name -- -Cllvm-args=-mips64r5

where -Cllvm-args contains the arguments for GCC.

However, this seems to be an error on the Rust side, so, we would need to fix it there, presumably by first adding a way to add targets not supported by LLVM.

If you want to test before this is supported on the Rust side, please follow this doc, which says you will need to select an arch supported by the Rust compiler (pick one that is close and it might work).

lowercase-g commented 1 month ago

If you want to add a GCC flag, you can do it as such:

./y.sh cargo rustc --bin project-name -- -Cllvm-args=-mips64r5

where -Cllvm-args contains the arguments for GCC.

However, this seems to be an error on the Rust side, so, we would need to fix it there, presumably by first adding a way to add targets not supported by LLVM.

If you want to test before this is supported on the Rust side, please follow this doc, which says you will need to select an arch supported by the Rust compiler (pick one that is close and it might work).

What are the current roadblocks to getting rustc_codegen_gcc to support targets not supported by LLVM? Is it just the need to use the same target JSON spec as normal rustc or does it go deeper than that?

GuillaumeGomez commented 1 month ago

It needs to have a new parameter in the list of supported platforms. Like codegens: ["gcc", "llvm"] but for each target. That way, we could add any target we'd like to for GCC without breaking things for LLVM backend.

antoyo commented 1 month ago

What are the current roadblocks to getting rustc_codegen_gcc to support targets not supported by LLVM? Is it just the need to use the same target JSON spec as normal rustc or does it go deeper than that?

What do you mean by "support targets not supported by LLVM" in rustc_codegen_gcc? Do you mean some official support from Rust or easy installation via rustup? As it stands, we can already target unsupported architectures like SuperH for instance, but the setup is not that easy. While rustc_codegen_gcc can generate code for unsupported targets, this doesn't mean it will work 100% and this means using workarounds like the one I suggested above like using the wrong arch in the target spec.

Also, there's a chicken-and-egg problem because it seems like you cannot add a target to libc before it's added to rustc, but the latter won't accept adding all targets for maintenance burden. See these for more details: