Skip to content

Add support for LLVM 13#2365

Merged
deadprogram merged 2 commits intodevfrom
llvm13
Jan 9, 2022
Merged

Add support for LLVM 13#2365
deadprogram merged 2 commits intodevfrom
llvm13

Conversation

@aykevl
Copy link
Copy Markdown
Member

@aykevl aykevl commented Dec 9, 2021

This adds support for building with -tags=llvm13 and switches to LLVM 13 for tinygo binaries that are statically linked against LLVM.

Some notes on this PR:

  • Added -mfloat-abi=soft to all Cortex-M targets because otherwise nrfx would complain that floating point was enabled on Cortex-M0. That's not the case, but with -mfloat-abi=soft the __SOFTFP__ macro is defined which silences this warning.
    See: https://reviews.llvm.org/D100372
  • Changed from --sysroot=<root> to -nostdlib -isystem <root>/include for musl because with Clang 13, even with --sysroot some system libraries are used which we don't want.
  • Changed all -Xclang -internal-isystem -Xclang to simply -isystem, for consistency with the above change. It appears to have the same effect.
  • Moved WebAssembly function declarations to the top of the file in task_asyncify_wasm.S because (apparently) the assembler has become more strict.

@QuLogic pinging you in case you hadn't seen this yet

@aykevl aykevl force-pushed the llvm13 branch 3 times, most recently from 8b4af53 to 8e8c0eb Compare January 2, 2022 19:12
@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 2, 2022

Huh, for some reason the goroutines test starts to fail on AVR with LLVM 13.

@aykevl aykevl force-pushed the llvm13 branch 2 times, most recently from c01b2b7 to bdfd323 Compare January 2, 2022 23:29
@aykevl aykevl mentioned this pull request Jan 3, 2022
@aykevl aykevl force-pushed the llvm13 branch 2 times, most recently from 56b23e9 to 4b9ace6 Compare January 3, 2022 00:36
@QuLogic
Copy link
Copy Markdown
Contributor

QuLogic commented Jan 4, 2022

Backporting the first commit to v0.21.0 fixes most of the tests except for TestCompiler/WebAssembly/channel.go, which errors with the output:

    main_test.go:381: failed to run: signal: interrupt
    main_test.go:395: stdout: len, cap of channel: 1 2 false
    main_test.go:395: stdout: len, cap of channel: 0 0 false
    main_test.go:395: stdout: recv from open channel: 1 true
    main_test.go:395: stdout: received num: 2
    main_test.go:395: stdout: received num: 3
    main_test.go:395: stdout: slept
    main_test.go:395: stdout: received num: 4
    main_test.go:395: stdout: received num: 5
    main_test.go:395: stdout: received num: 6
    main_test.go:395: stdout: received num: 7
    main_test.go:395: stdout: received num: 8
    main_test.go:395: stdout: recv from closed channel: 0 false
    main_test.go:395: stdout: complex128: (+7.000000e+000+1.050000e+001i)
    main_test.go:395: stdout: sum of n: 149
    main_test.go:395: stdout: --- test ran too long, terminating...

I did not bother with the second commit, as AVR tests are not enabled in 0.21.0. I suppose I should try out dev as well some time, but that requires forward-porting the patches.

@QuLogic
Copy link
Copy Markdown
Contributor

QuLogic commented Jan 4, 2022

Running this against dev does appear to get TestBuild/WebAssembly/channel.go passing, but other new tests are failing in TestTest. I haven't determined if that's related to this PR or dev directly.

@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 5, 2022

Maybe it works now? Let's see what CI thinks.

@QuLogic I hope to get a release out soon, with this PR in it. We need to catch up on LLVM versions!

@aykevl aykevl force-pushed the llvm13 branch 2 times, most recently from 547eb1a to 0c2931b Compare January 5, 2022 02:14
@deadprogram
Copy link
Copy Markdown
Member

This PR has passed all CI including TinyHCI!

@aykevl aykevl marked this pull request as ready for review January 5, 2022 12:44
@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Jan 5, 2022

Ok, ready for review now!
@niaow pinging you because the first commit modifies AVR tests a bit.

@deadprogram deadprogram requested review from QuLogic and niaow January 5, 2022 14:05
Failed = Parser->Run(Opts.NoInitialTextSection);
}

// Close Streamer first.
Copy link
Copy Markdown
Member

@niaow niaow Jan 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this no longer an issue? I don't quite understand what is going on here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is basically a copy of https://github.com/llvm/llvm-project/blob/main/clang/tools/driver/cc1as_main.cpp with some small modifications. So I'm just syncing up the changes to LLVM 13.

@@ -1,5 +1,5 @@
// +build !byollvm
// +build !llvm12
// +build !llvm12,!llvm13
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these build tags still work correctly with LLVM 12 as the current default?

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 5, 2022

I just tried go test -v -tags llvm13 right now on my system (Manjaro/Arch uses LLVM 13.0.0 by default now) and it seems that:

  1. All Linux tests currently segfault (host and emulated)
  2. wasi-libc cannot be imported due to missing (u)int*_t types

All other targets seem to work fine, not quite sure what is happening yet.

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 5, 2022

It currently appears to be crashing when starting the libc:

(gdb) bt
#0  0x0000000000206b0a in __init_libc (envp=0x7fffffffde28, pn=0x7fffffffe19f "/tmp/tinygo3925493316/main") at lib/musl/src/env/__libc_start_main.c:24
#1  0x0000000000206cdc in __libc_start_main (main=0x2041d0 <main>, argc=<optimized out>, argv=0x7fffffffde18) at lib/musl/src/env/__libc_start_main.c:79
#2  0x0000000000206ad6 in _start ()

The debug location unfortunately does not seem to be very accurate - it points at the opening bracket.

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 5, 2022

The crashing instruction (on host) is:

mov %fs:0x28,%rax

Which is doing something with TLS before TLS is initialized if I understand correctly. I don't quite see why it is there yet.

At that point in the program, GDB reports that the fs segment register is 0 (because it has not been set yet).

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 5, 2022

This appears to be a stack check

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 5, 2022

Adding -fno-stack-protector to musl's CFLAGS seems to fix all of the segfaults, so the only remaining issue is wasi-libc

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 7, 2022

The wasi-libc problem seems to be an issue with getClangHeaderPath on Arch. The clang binary is stored at /usr/bin/clang-13 (with a symlink at /usr/bin/clang), but the headers are stored at /usr/lib/clang/13.0.0/include. It looks like we were previously pulling in headers from both the main clang header path and the header path from my locally compiled assert copy of clang 12, but after deleting the assert copy it just cannot find some headers.

@niaow
Copy link
Copy Markdown
Member

niaow commented Jan 8, 2022

With #2498, this works for me

The goroutine tests are failing with the default 256 byte stack size.
This adds support for building with `-tags=llvm13` and switches to LLVM
13 for tinygo binaries that are statically linked against LLVM.

Some notes on this commit:

  * Added `-mfloat-abi=soft` to all Cortex-M targets because otherwise
    nrfx would complain that floating point was enabled on Cortex-M0.
    That's not the case, but with `-mfloat-abi=soft` the `__SOFTFP__`
    macro is defined which silences this warning.
    See: https://reviews.llvm.org/D100372
  * Changed from `--sysroot=<root>` to `-nostdlib -isystem <root>` for
    musl because with Clang 13, even with `--sysroot` some system
    libraries are used which we don't want.
  * Changed all `-Xclang -internal-isystem -Xclang` to simply
    `-isystem`, for consistency with the above change. It appears to
    have the same effect.
  * Moved WebAssembly function declarations to the top of the file in
    task_asyncify_wasm.S because (apparently) the assembler has become
    more strict.
@QuLogic
Copy link
Copy Markdown
Contributor

QuLogic commented Jan 9, 2022

I haven't tested the latest version of this, but I did test dev and I see the same failure there with LLVM 12 as I do with LLVM13 + this PR. I'm bisecting that now to see what introduced the problem.

@QuLogic
Copy link
Copy Markdown
Contributor

QuLogic commented Jan 9, 2022

Ah, the problem with dev is that some recent PRs pulled in internal/itoa and the backport is not visible when running tests (but seems fine for build), so I probably have some messed up GOPATH setting or something. I guess previously tests didn't import anything that needed backports in the Go version I was using, so this didn't fail, but was probably still wrong.

@deadprogram
Copy link
Copy Markdown
Member

Thanks for looking at this @QuLogic sounds like if there are any further issues we can resolve them individually.

Thanks also @niaow for your testing. Please see my comment on PR #2498 I am doing to merge this PR so you should both change the target branch for your PR and also rebase.

Great work on this @aykevl it is very important for us to be able to use the latest LLVM, not to mention all the upstream fixes you have been contributing.

Now merging!

@deadprogram deadprogram merged commit ebd4969 into dev Jan 9, 2022
@deadprogram deadprogram deleted the llvm13 branch January 9, 2022 10:04
@QuLogic
Copy link
Copy Markdown
Contributor

QuLogic commented Jan 11, 2022

OK, so backporting this + 1d2c177 to v0.21.0 works on LLVM 12. For LLVM 13, I also needed to backport e4de7b4.

For dev directly, I figured out the problem is that I'm running the equivalent of go test ./..., and that picks up tests in src, but those should not be run by go, but by tinygo test. I'm not totally sure why this didn't happen in 0.21 or older, but I've excluded that directory, and everything is good on both LLVM 12 & 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants