ziglang / zig-bootstrap

take off every zig
391 stars 91 forks source link

loongarch64-linux-musl #164

Closed yxd-ym closed 2 months ago

yxd-ym commented 10 months ago

system:

OS: loongarchlinux GCC:

$ gcc --version
gcc (GCC) 13.2.1 20230906
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[15/20] Running zig1.wasm to produce /home/yxd/playspace/zig-bootstrap/out/build-zig-host/zig2.c
FAILED: zig2.c /home/yxd/playspace/zig-bootstrap/out/build-zig-host/zig2.c
cd /home/yxd/playspace/zig-bootstrap/zig && /home/yxd/playspace/zig-bootstrap/out/build-zig-host/zig1 /home/yxd/playspace/zig-bootstrap/zig/lib build-exe -ofmt=c -lc -OReleaseSmall --name zig2 -femit-bin="/home/yxd/playspace/zig-bootstrap/out/build-zig-host/zig2.c" -target loongarch64-linux --dep build_options --dep aro --mod root src/main.zig --mod build_options /home/yxd/playspace/zig-bootstrap/out/build-zig-host/config.zig --mod aro_options src/stubs/aro_options.zig --mod Builtins/Builtin.def src/stubs/aro_builtins.zig --mod Attribute/names.def src/stubs/aro_names.zig --mod Diagnostics/messages.def src/stubs/aro_messages.zig --dep build_options=aro_options --mod aro_backend deps/aro/backend.zig --dep Builtins/Builtin.def --dep Attribute/names.def --dep Diagnostics/messages.def --dep build_options=aro_options --dep backend=aro_backend --mod aro deps/aro/aro.zig
/lib/std/os/linux.zig:77:28: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'ino_t'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:113:33: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'MAP'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:145:33: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'O'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:83:28: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'off_t'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:48:34: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'syscall0'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:52:34: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'syscall4'
/lib/std/os/linux.zig:46:13: note: struct declared here
/lib/std/os/linux.zig:51:34: error: struct 'os.linux.arch_bits__struct_7806' has no member named 'syscall3'
/lib/std/os/linux.zig:46:13: note: struct declared here
ninja: build stopped: subcommand failed.
nektro commented 10 months ago

musl upstream does not support this target yet https://wiki.musl-libc.org/supported-platforms

yxd-ym commented 10 months ago

There is some patch in the mailing list and I guess the support is coming soon. I'm using a patched version of musl to try this.

https://inbox.vuxu.org/musl/2a8d55b0-add3-4d90-1119-4e5c28b6626e@loongson.cn/

Anyway, maybe trying to make #166 work first is a good idea.

yxd-ym commented 9 months ago

musl support is coming: https://www.openwall.com/lists/musl/2024/01/25/12

yxd-ym commented 9 months ago

https://git.musl-libc.org/cgit/musl/commit/?id=522bd54edaa2fa404fd428f8ad0bcb0f0bec5639

yxd-ym commented 8 months ago

https://musl.libc.org/releases.html

musl 1.2.5 adds loongarch64 support.

andrewrk commented 3 months ago

current status is:

CMake Error at /nix/store/mk7637n6h4d437gl5cplxvd3k2l66awd-cmake-3.29.2/share/cmake-3.29/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/home/andy/dev/zig-bootstrap/out/host/bin/zig"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/andy/dev/zig-bootstrap/out/build-zlib-loongarch64-linux-musl-baseline/CMakeFiles/CMakeScratch/TryCompile-msrcsI'

    Run Build Command(s): /nix/store/p5y20shjad8an1jhcby82sz843lirvwi-ninja-1.11.1/bin/ninja -v cmTC_7f7a4
    [1/2] /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline    -MD -MT CMakeFiles/cmTC_7f7a4.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_7f7a4.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_7f7a4.dir/testCCompiler.c.o -c /home/andy/dev/zig-bootstrap/out/build-zlib-loongarch64-linux-musl-baseline/CMakeFiles/CMakeScratch/TryCompile-msrcsI/testCCompiler.c
    [2/2] : && /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline   CMakeFiles/cmTC_7f7a4.dir/testCCompiler.c.o -o cmTC_7f7a4   && :
    FAILED: cmTC_7f7a4 
    : && /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline   CMakeFiles/cmTC_7f7a4.dir/testCCompiler.c.o -o cmTC_7f7a4   && :
    LLVM ERROR: Cannot select: 0x7fae00a11e70: i64 = fp_to_fp16 0x7fae00a3ec00
      0x7fae00a3ec00: f32,ch = CopyFromReg 0x7fae00328980, Register:f32 %11
        0x7fae00c7cf00: f32 = Register %11
    In function: __fixhfsi
    ninja: build stopped: subcommand failed.

In other words, unable to build compiler_rt from source due to LLVM bug in lowering to fp16.

yxd-ym commented 3 months ago

https://elixir.bootlin.com/llvm/llvmorg-18.1.8/source/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp#L58

I'm trying to handle the code model issue for loongarch64 target. If the code model is not set properly, it will have R_LARCH_B26 overflow problem now.

yxd-ym commented 3 months ago

I managed to bootstrap a loongarch64-linux-musl zig with some patch like this on zig:

diff --git a/lib/libc/glibc/sysdeps/loongarch/start.S b/lib/libc/glibc/sysdeps/loongarch/start.S
index de7f9645cf..976dd92178 100644
--- a/lib/libc/glibc/sysdeps/loongarch/start.S
+++ b/lib/libc/glibc/sysdeps/loongarch/start.S
@@ -56,7 +56,7 @@ ENTRY (ENTRY_POINT)

 /* Terminate call stack by noting ra is undefined.  Use a dummy
    .cfi_label to force starting the FDE.  */
-       .cfi_label .Ldummy
+       /* .cfi_label .Ldummy */
        cfi_undefined (1)
        or              a5, a0, zero /* rtld_fini */

diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index 233cf7e3eb..80e37c1ad3 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -11799,6 +11799,7 @@ fn backendSupportsF16(target: std.Target) bool {
         .mips64,
         .mips64el,
         .s390x,
+        .loongarch64,
         => false,
         .aarch64,
         .aarch64_be,

However, when trying to use this zig to build a simple hello world C file

#include <stdio.h>

int main() {
   printf("Hello, World!\n");
   return 0;
}

with

zig cc hello.c

I got the following error. But this doesn't happen on x86_64 zig.

#0  0x000000000a435b18 in ZigLLVMTargetMachineEmitToFile (targ_machine_ref=0x7fffe7ec9f90,
    module_ref=0x7fffe6ffff90, error_message=0x7fffeef32050,
    options=<error reading variable: Cannot access memory at address 0x0>)
    at /Users/yxd/playspace/zig/src/zig_llvm.cpp:218
#1  0x0000000009860f30 in codegen.llvm.Object.emit (self=<optimized out>, options=...)
    at /Users/yxd/playspace/zig/src/codegen/llvm.zig:1317
#2  0x000000000985f210 in Compilation.emitLlvmObject (comp=0x7ffff7fe6c10, arena=...,
    default_emit=..., bin_emit_loc=..., llvm_object=0x7fffedf0c1d0) at Compilation.zig:2756
#3  0x0000000009938304 in link.File.emitLlvmObject (arena=..., llvm_object=0x0,
    prog_node=...) at link.zig:1032
#4  link.Elf.flushModule (self=0x7fffedf28e70, arena=..., tid=main, prog_node=...)
    at /Users/yxd/playspace/zig/src/link/Elf.zig:992
#5  0x000000000985ccc8 in link.File.flushModule (base=0x7fffedf292d0, tid=main)
    at link.zig:635
#6  link.File.linkAsArchive (base=0x7fffedf292d0, tid=main) at link.zig:762
#7  link.File.flush (base=<optimized out>, arena=..., tid=main, prog_node=...)
    at link.zig:619
#8  0x000000000985b4cc in Compilation.flush (comp=0x7ffff7fe6c10, arena=..., tid=main,
    prog_node=...) at Compilation.zig:2446
#9  0x000000000980acd8 in Compilation.update (comp=0x7ffff7fe6c10, main_progress_node=...)
    at Compilation.zig:2396
#10 0x0000000009ffc598 in Compilation.updateSubCompilation (parent_comp=0x7fffeff43770,
    sub_comp=0x7ffff7fe6c10, misc_task=compiler_rt) at Compilation.zig:6274
#11 0x000000000a009054 in Compilation.buildOutputFromZig (comp=0x7fffeff43770,
    output_mode=<optimized out>, out=0x7fffeff43cf8, misc_task_tag=compiler_rt,
    prog_node=...) at Compilation.zig:6413
#12 0x0000000009fceb84 in Compilation.buildRt (comp=0x7fffeff43770, misc_task=compiler_rt,
    output_mode=Exe, out=0x0,
    prog_node=<error reading variable: Cannot access memory at address 0x0>)
    at Compilation.zig:4612
#13 0x0000000009fcea54 in Thread.WaitGroup.spawnManager__anon_193170.Manager.run (
    wg_inner=0x7ffffffe66d8, args_inner=...)
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/Thread/WaitGroup.zig:62
#14 Thread.callFn__anon_390026 (args=...)
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/Thread.zig:409
#15 Thread.PosixThreadImpl.spawn__anon_319169.Instance.entryFn (raw_arg=0x7ffff7fbb800)
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/std/Thread.zig:678
#16 0x000000000a4d3050 in start (p=0x7fffeef378f0)
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/thread/pthread_create.c:207
#17 0x00000000095775b4 in __clone ()
    at /Users/yxd/playspace/zig-bootstrap/out/host/lib/zig/libc/musl/src/thread/loongarch64/clone.s:27

Looking into this.

yxd-ym commented 2 months ago

Same as https://github.com/ziglang/zig-bootstrap/issues/166#issuecomment-2321565953

andrewrk commented 2 months ago

With LLVM 19.1.0rc4 I get this:

CMake Error at /nix/store/28s979fhz8d6h1rny3n5zh1ybmby6kjz-cmake-3.29.2/share/cmake-3.29/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/home/andy/dev/zig-bootstrap/out/host/bin/zig"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: '/home/andy/dev/zig-bootstrap/out/build-zlib-loongarch64-linux-musl-baseline/CMakeFiles/CMakeScratch/TryCompile-p9HGgz'

    Run Build Command(s): /nix/store/qvbaf1639wmhn95ddmk23n5xldjaj76n-ninja-1.11.1/bin/ninja -v cmTC_31aae
    [1/2] /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline    -MD -MT CMakeFiles/cmTC_31aae.dir/testCCompiler.c.o -MF CMakeFiles/cmTC_31aae.dir/testCCompiler.c.o.d -o CMakeFiles/cmTC_31aae.dir/testCCompiler.c.o -c /home/andy/dev/zig-bootstrap/out/build-zlib-loongarch64-linux-musl-baseline/CMakeFiles/CMakeScratch/TryCompile-p9HGgz/testCCompiler.c
    [2/2] : && /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline   CMakeFiles/cmTC_31aae.dir/testCCompiler.c.o -o cmTC_31aae   && :
    FAILED: cmTC_31aae 
    : && /home/andy/dev/zig-bootstrap/out/host/bin/zig cc -fno-sanitize=all -s -target loongarch64-linux-musl -mcpu=baseline   CMakeFiles/cmTC_31aae.dir/testCCompiler.c.o -o cmTC_31aae   && :
    LLVM ERROR: Do not know how to promote this operator's result!
    ninja: build stopped: subcommand failed.
yxd-ym commented 2 months ago

https://github.com/ziglang/zig-bootstrap/pull/186

Managed to bootstrap loongarch64-linux-musl with this patch. However, I'm not familiar with llvm so I'm not sure whether this fix is reasonable.

yxd-ym commented 2 months ago

@alexrp https://github.com/ziglang/zig-bootstrap/pull/186#issuecomment-2336576463

yxd-ym commented 2 months ago

With https://github.com/ziglang/zig-bootstrap/commit/1ad232c5a2e1ebbdad2cfbb86ad8fe7f73c7e4d4

✅ We are now able to bootstrap a loongarch64-linux-musl zig. ✅ It is possible to compile C and zig source.

But now it has the following error when running zig libc:

yxd@elle ~/tmp/zigtest $ file ~/opt/zig/musl/zig
/home/yxd/opt/zig/musl/zig: ELF 64-bit LSB executable, LoongArch, version 1 (GNU/Linux), statically linked, stripped
yxd@elle ~/tmp/zigtest $ ~/opt/zig/musl/zig version
0.14.0-dev.1622+2ac543388
yxd@elle ~/tmp/zigtest $ ~/opt/zig/musl/zig libc
error: OutOfMemory
alexrp commented 2 months ago

Since we've successfully bootstrapped a Zig compiler for this target, I think any bugs encountered from this point should be filed on ziglang/zig as individual issues.