thepowersgang / mrustc

Alternative rust compiler (re-implementation)
MIT License
2.18k stars 109 forks source link

Support PowerPC: mrustc itself builds fine, rust tries to build for x86 and fails #300

Open barracuda156 opened 1 year ago

barracuda156 commented 1 year ago

What is needed to support building for PowerPC? Currently mrustc builds, but rust tries to build for x86:

test -e bin/mrustc
/usr/bin/make -C tools/minicargo/
make[1]: Nothing to be done for `all'.
test -e bin/minicargo
bin/minicargo rustc-1.54.0-src/library/std --vendor-dir rustc-1.54.0-src/vendor --script-overrides script-overrides/stable-1.54.0-macos/ --output-dir output-1.54.0/  --manifest-overrides rustc-1.54.0-overrides.toml
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/mrustc-11a9c01119988e30ba40bab4ee5f60753c0c4b61/bin/mrustc -Z print-cfgs
(0/13) BUILDING core v0.0.0
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/mrustc-11a9c01119988e30ba40bab4ee5f60753c0c4b61/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/x86/avx2.rs:520: error:0:Type mismatch between [u32; 8] and [u32; 576460752303423488] - sizes differ
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/x86/avx2.rs:520: note: From here
Process was terminated with signal 6
FAILING COMMAND:  /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/mrustc-11a9c01119988e30ba40bab4ee5f60753c0c4b61/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
BUILD FAILED
make: *** [output-1.54.0/libstd.rlib] Error 1
Command failed:  cd "/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/mrustc-11a9c01119988e30ba40bab4ee5f60753c0c4b61" && /usr/bin/make -j2 -w -f minicargo.mk -j2 bin/mrustc bin/minicargo && /usr/bin/make -j2 -C tools/dump_hirfile && /usr/bin/make -j2 -C tools/standalone_miri && /usr/bin/make -j1 -f minicargo.mk LIBS 
Exit code: 2

See also: https://trac.macports.org/ticket/65942

thepowersgang commented 1 year ago

The compiler defaults to compiling for the architecture it's running on... but that error does seem to imply that there's a different architecture involved (and or something even stranger, given the very large array size).

What platform are you running on, and what is the output of ./bin/mrustc -Z print-cfgs

barracuda156 commented 1 year ago

The compiler defaults to compiling for the architecture it's running on... but that error does seem to imply that there's a different architecture involved (and or something even stranger, given the very large array size).

This is natively on PowerPC. I tried building it in emulation via Rosetta, it failed earlier and I gave up on that (no specific value it having it in Rosetta anyway besides extra testing).

What platform are you running on, and what is the output of ./bin/mrustc -Z print-cfgs

I need to build it again, will try that tonight and update you. But it is on macOS 10.6 ppc32.

barracuda156 commented 1 year ago

@thepowersgang This was insightful: while it builds on PPC as a ppc binary, target settings are totally off:

/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc -Z print-cfgs
sergey-fedorovs-power-mac-g5:~ svacchanda$ /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc -Z print-cfgs
Setup: V V V
(0.00 s) Setup: DONE
Target Load: V V V
(0.00 s) Target Load: DONE
>rust_compiler=mrustc
>target_arch=x86_64
>target_endian=little
>target_env=gnu
>target_family=unix
>target_has_atomic=8
>target_has_atomic=16
>target_has_atomic=32
>target_has_atomic=64
>target_has_atomic=ptr
>target_has_atomic=cas
>target_has_atomic_equal_alignment=8
>target_has_atomic_equal_alignment=16
>target_has_atomic_equal_alignment=32
>target_has_atomic_equal_alignment=64
>target_has_atomic_equal_alignment=ptr
>target_has_atomic_load_store=8
>target_has_atomic_load_store=16
>target_has_atomic_load_store=32
>target_has_atomic_load_store=64
>target_has_atomic_load_store=ptr
>target_os=macos
>target_pointer_width=64
>target_vendor=apple
>target_vendor=
>apple
>unix

How to fix this? Obviously, target should be Big-endian and ppc (32-bit).

thepowersgang commented 1 year ago

Ah, just looked at the source - the default for apple is x86. You will need to edit tools/common/target_detect.h to update the detection rules so it can identify a PPC target, and then add the triple to src/trans/target.cpp init_from_spec_name

barracuda156 commented 1 year ago

@thepowersgang Possibly we need to set Bool to 4 bytes on powerpc-apple-darwin (only for ppc32, not ppc64). Could you advise how to do it? Possibly here:

    TU_ARMA(Primitive, te) {
        switch(te)
        {
        case ::HIR::CoreType::Bool:
        case ::HIR::CoreType::U8:
        case ::HIR::CoreType::I8:
            out_size = 1;
            out_align = 1;  // u8 is always 1 aligned
            return true;

I.e. it is the OS default for languages it uses. I am not sure about Rust here.

barracuda156 commented 1 year ago

@thepowersgang Target fixed:

svacchanda$ /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc -Z print-cfgs
Setup: V V V
(0.00 s) Setup: DONE
Target Load: V V V
(0.00 s) Target Load: DONE
>rust_compiler=mrustc
>target_arch=powerpc
>target_endian=big
>target_env=gnu
>target_family=unix
>target_has_atomic=8
>target_has_atomic=32
>target_has_atomic=ptr
>target_has_atomic=cas
>target_has_atomic_equal_alignment=8
>target_has_atomic_equal_alignment=32
>target_has_atomic_equal_alignment=ptr
>target_has_atomic_load_store=8
>target_has_atomic_load_store=32
>target_has_atomic_load_store=ptr
>target_os=macos
>target_pointer_width=32
>target_vendor=apple
>target_vendor=
>apple
>unix
barracuda156 commented 1 year ago

With fixed target, x86 is no longer pulled, but rust fails almost immediately with this:

make[1]: Nothing to be done for `all'.
test -e bin/mrustc
/usr/bin/make -C tools/minicargo/
make[1]: Nothing to be done for `all'.
test -e bin/minicargo
tar -xf rustc-1.54.0-src.tar.gz
cd rustc-1.54.0-src/ && patch -p0 < ../rustc-1.54.0-src.patch;
patching file compiler/rustc_ast/src/ast.rs
patching file compiler/rustc_hir/src/hir.rs
patching file compiler/rustc_middle/src/mir/interpret/error.rs
patching file compiler/rustc_middle/src/mir/mod.rs
patching file compiler/rustc_middle/src/thir.rs
patching file compiler/rustc_mir/src/interpret/place.rs
patching file compiler/rustc_mir/src/interpret/operand.rs
patching file vendor/crc32fast/src/specialized/mod.rs
patching file library/stdarch/crates/std_detect/src/detect/mod.rs
patching file vendor/ppv-lite86/src/lib.rs
patching file vendor/curl/src/lib.rs
touch rustc-1.54.0-src//dl-version
bin/minicargo rustc-1.54.0-src/library/std --vendor-dir rustc-1.54.0-src/vendor --script-overrides script-overrides/stable-1.54.0-macos/ --output-dir output-1.54.0/  --manifest-overrides rustc-1.54.0-overrides.toml
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc -Z print-cfgs
(0/13) BUILDING core v0.0.0
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
Macro:vec_xxpermdi:0: warn:0:Unexpected attribute target_feature on associated item
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/powerpc/vsx.rs:69: note: From here
Macro:vec_xxpermdi:0: warn:0:Unexpected attribute target_feature on associated item
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/powerpc/vsx.rs:70: note: From here
Macro:vec_xxpermdi:0: warn:0:Unexpected attribute target_feature on associated item
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/powerpc/vsx.rs:71: note: From here
Macro:vec_xxpermdi:0: warn:0:Unexpected attribute target_feature on associated item
rustc-1.54.0-src/library/core/src/../../stdarch/crates/core_arch/src/powerpc/vsx.rs:72: note: From here
MIR TODO: ::"core-0_0_0"::core_arch::powerpc::vsx::sealed::#3::IDX BB0/0: Handle big endian in constant evaluate
Process was terminated with signal 6
FAILING COMMAND:  /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
BUILD FAILED
make: *** [output-1.54.0/libstd.rlib] Error 1
Command failed:  cd "/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b" && /usr/bin/make -j2 -w -f minicargo.mk -j2 bin/mrustc bin/minicargo && /usr/bin/make -j2 -C tools/dump_hirfile && /usr/bin/make -j2 -C tools/standalone_miri && /usr/bin/make -j1 -f minicargo.mk LIBS 
Exit code: 2

Given it refers to VSX, that has to be disabled, it is not supported before ISA 2.06 or so.

barracuda156 commented 1 year ago

Yeah, this code in rust is wrong:

    /// Platform-specific intrinsics for the `PowerPC` platform.
    ///
    /// See the [module documentation](../index.html) for more details.
    #[cfg(any(target_arch = "powerpc", doc))]
    #[doc(cfg(target_arch = "powerpc"))]
    #[unstable(feature = "stdsimd", issue = "27731")]
    pub mod powerpc {
        pub use crate::core_arch::powerpc::*;
    }
barracuda156 commented 1 year ago

Altivec code appears broken, so I disabled it together with VSX in rustc-1.54.0-src/library/stdarch/crates/core_arch/src/powerpc/mod.rs.

Build still fails though here:

test -e bin/minicargo
bin/minicargo rustc-1.54.0-src/library/std --vendor-dir rustc-1.54.0-src/vendor --script-overrides script-overrides/stable-1.54.0-macos/ --output-dir output-1.54.0/  --manifest-overrides rustc-1.54.0-overrides.toml
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc -Z print-cfgs
(0/13) BUILDING core v0.0.0
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
MIR TODO: ::"core-0_0_0"::core_arch::macros::#0::FRAGMENTS BB0/0: Handle big endian in constant evaluate
Process was terminated with signal 6
FAILING COMMAND:  /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib --crate-name core --crate-type rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --crate-tag 0_0_0 --cfg debug_assertions -O -L output-1.54.0 --edition 2018
BUILD FAILED
make: *** [output-1.54.0/libstd.rlib] Error 1
Command failed:  cd "/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-1b1416b" && /usr/bin/make -j2 -w -f minicargo.mk -j2 bin/mrustc bin/minicargo && /usr/bin/make -j2 -C tools/dump_hirfile && /usr/bin/make -j2 -C tools/standalone_miri && /usr/bin/make -j1 -f minicargo.mk LIBS 
Exit code: 2

@thepowersgang What to do about this MIR TODO: ::"core-0_0_0"::core_arch::macros::#0::FRAGMENTS BB0/0: Handle big endian in constant evaluate?

thepowersgang commented 1 year ago

Re bool's size: What what rustc does - that's the correct method.

thepowersgang commented 1 year ago

Re Handle big endian in constant evaluate - It looks to be relatively simple to implement, if you feel like it.

barracuda156 commented 1 year ago

Re bool's size: What what rustc does - that's the correct method.

Then leave it as is.

barracuda156 commented 1 year ago

Re Handle big endian in constant evaluate - It looks to be relatively simple to implement, if you feel like it.

@thepowersgang Could you please advise more specifically here, what and where to fix?

thepowersgang commented 1 year ago

If you search for that message, you will find three locations for it - read_uint, write_uint, and write_sint - they will need endian flipping before the read/write

thepowersgang commented 1 year ago

Big-endian support added in b57ddbfcf2d8736e27975762855f70a0da2eedd3

barracuda156 commented 1 year ago

@thepowersgang Awesome, thank you! I should be able to test it in a few days (been away from my PPC hardware for a while, returning now).

P. S. @catap FYI.

barracuda156 commented 1 year ago

@thepowersgang Ok, building from master 38c9db25324351badeaaef91a7755cd5b50e1336 and setting Rust arch to powerpc (and disabling VSX, which is not supported on G5, and expectedly fails), I got this:

[CXX] -o ../../bin/standalone_miri
/usr/bin/make -f Makefile all
make[1]: Nothing to be done for `all'.
test -e bin/mrustc
/usr/bin/make -C tools/minicargo/
make[1]: Nothing to be done for `all'.
test -e bin/minicargo
bin/minicargo --vendor-dir rustc-1.54.0-src/vendor --script-overrides script-overrides/stable-1.54.0-macos/ --output-dir output-1.54.0/  --manifest-overrides rustc-1.54.0-overrides.toml rustc-1.54.0-src/mrustc-stdlib/
--- BUILDING core v0.0.0 (0.0% 1r,0w,19b/20t)
> /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-38c9db2/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --cfg debug_assertions -O -L output-1.54.0 --crate-name core --crate-type rlib --crate-tag 0_0_0 --edition 2018 > output-1.54.0/libcore.rlib_dbg.txt
 (0.0% 1r,0w,19b/20t): core v0.0.0
:0:0 BUG:ASSERT FAIL: src/hir/hir_ops.cpp:1296:ep.m_mir: No HIR (!ep) and no MIR (!ep.m_mir) for ::"core-0_0_0"::panicking::#0::panic_impl<'#local3,>
Process was terminated with signal 6
FAILING COMMAND: /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-38c9db2/bin/mrustc rustc-1.54.0-src/library/core/src/lib.rs -o output-1.54.0/libcore.rlib -C emit-depfile=output-1.54.0/libcore.rlib.d --cfg debug_assertions -O -L output-1.54.0 --crate-name core --crate-type rlib --crate-tag 0_0_0 --edition 2018
Env:  OUT_DIR=/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-38c9db2/output-1.54.0/build_core CARGO_MANIFEST_DIR=/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_lang_mrustc/mrustc/work/thepowersgang-mrustc-38c9db2/rustc-1.54.0-src/library/core CARGO_PKG_NAME=core CARGO_PKG_VERSION=0.0.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=0 CARGO_PKG_VERSION_PATCH=0
 (5.0% 0r,0w,19b/20t):
BUILD FAILED
make: *** [LIBS] Error 1

What should I do about this?

barracuda156 commented 1 year ago

Besides, we will need something like this to avoid VSX on Darwin:

--- a/library/stdarch/crates/core_arch/src/powerpc/mod.rs   2021-07-26 22:43:20.000000000 +0800
+++ b/library/stdarch/crates/core_arch/src/powerpc/mod.rs   2023-10-16 10:00:52.000000000 +0800
@@ -5,7 +5,9 @@
 #[cfg(target_feature = "altivec")]
 pub use self::altivec::*;

+#[cfg(not(target_os = "macos"))]
 mod vsx;
+#[cfg(not(target_os = "macos"))]
 pub use self::vsx::*;

 #[cfg(test)]
thepowersgang commented 1 year ago

Cannot reproduce with MRUSTC_TARGET=powerpc-apple-darwin64 ./build-1.54.0.sh on x86-64 linux (it does fail to build, but on libc not finding c_long) Could you run with MRUSTC_DEBUG=Constant Evaluate (or whatever the last line of the logfile contains) and post the last ~100 lines?

barracuda156 commented 1 year ago

If it expects the target in this exotic way, then I should try using powerpc-apple-darwin32? Normally this would be wrong: digits after darwin stand for its version, not bitness.

thepowersgang commented 1 year ago

uhh... my bad on the command I said I ran. I ran MRUSTC_TARGET=powerpc64-apple-darwin ./build-1.54.0.sh and MRUSTC_TARGET=powerpc-apple-darwin ./build-1.54.0.sh - both of which compiled libcore (well, mrustc part anyway - didn't call the C compiler)

This implies that the error is caused by something about running on PPC (or the compiler you're using)

barracuda156 commented 1 year ago

@thepowersgang Thank you for clarifying. The problem may well be on Macports set-up side: either incompatibility of some of our patches with powerpc-darwin (quite possible, since it was not tested by @catap or anyone else for powerpc, and the portfile as-is does not allow powerpc at the moment) or my additions in order to enable building for powerpc.

Compiler should be good: GCC upstream supports Darwin PowerPC, and gcc13 is pretty well tested, since I use it to build numerous ports.

thepowersgang commented 1 year ago

I'm not saying that it's not a mrustc bug - it very likely is - but it's not happening on the test setups I have, so is probably some bug that is being exposed by being run on ppc.