Skip to content

thirdparty: fix tcc __atomic_thread_fence#26185

Merged
spytheman merged 13 commits into
vlang:masterfrom
kbkpbot:fix-tcc-arm64-atomic-fence
Jan 3, 2026
Merged

thirdparty: fix tcc __atomic_thread_fence#26185
spytheman merged 13 commits into
vlang:masterfrom
kbkpbot:fix-tcc-arm64-atomic-fence

Conversation

@kbkpbot

@kbkpbot kbkpbot commented Dec 28, 2025

Copy link
Copy Markdown
Contributor

Fix issue #25856
Fix issue #26158

This bug is caused by on aarch64, tcc remove the prefix __ from __atomic_thread_fence, so a workaround needed here.
Maybe a tcc fix is needed for upstream.

ping @spytheman would you please help me verify that the test file is written in a correct format ? I just want to check it can be compiled under tcc/arm64. Do it need to set a VFLAGS = -no-retry-compilation?

Comment thread vlib/v/tests/tcc_arm64_atomic_fence_test.v Outdated
@spytheman

Copy link
Copy Markdown
Contributor

@kbkpbot I think it should be fine now; I'll test on macos to be sure and in a VM asap.

@kbkpbot

kbkpbot commented Dec 28, 2025

Copy link
Copy Markdown
Contributor Author

Thank you @spytheman

@tankf33der

Copy link
Copy Markdown
Contributor

Also will fix #25856

@kbkpbot kbkpbot marked this pull request as draft December 29, 2025 04:12
@kbkpbot kbkpbot marked this pull request as ready for review December 29, 2025 11:29

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work 🙇🏻 .
@tankf33der what do you think?

@tankf33der

Copy link
Copy Markdown
Contributor

Since all checks have passed this should be merged.
I will check everywhere and comment here.

@tankf33der

Copy link
Copy Markdown
Contributor

@spytheman - i take my words back. do not merge. i am testing.

@tankf33der

Copy link
Copy Markdown
Contributor

During the pause, I decided to check that the patch works.

@tankf33der

Copy link
Copy Markdown
Contributor

x64 alpine - ok

@tankf33der

Copy link
Copy Markdown
Contributor

some fail on one of the ARM

tankf33der@cfarm103:~/v$ gmake
cd ./vc && git clean -xf && git pull --quiet
cd ./thirdparty/tcc && git clean -xf && git pull --quiet
cc  -std=c99 -w -o v1 ./vc/v.c -lm -lpthread  || cmd/tools/cc_compilation_failed_non_windows.sh
./v1 -no-parallel -o v2 -no-retry-compilation -no-parallel cmd/v
================== C compilation error (from /home/tankf33der/v/thirdparty/tcc/tcc.exe): ==============
cc: /tmp/v_62104/v2.01KDNDDK4KEKCD1649CT8Z8KR5.tmp.c:1223: error: ARM asm not implemented.
=======================================================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
gmake: *** [GNUmakefile:115: all] Error 1

@spytheman

spytheman commented Dec 29, 2025

Copy link
Copy Markdown
Contributor

what is on line /tmp/v_62104/v2.01KDNDDK4KEKCD1649CT8Z8KR5.tmp.c:1223 ?

@tankf33der

Copy link
Copy Markdown
Contributor

what is on line /tmp/v_62104/v2.01KDNDDK4KEKCD1649CT8Z8KR5.tmp.c:1223 ?

@spytheman @kbkpbot

Some line from patch of this PR

image

@kbkpbot

kbkpbot commented Dec 30, 2025

Copy link
Copy Markdown
Contributor Author

Yes, due to tcc does not support arm asm right now, this should not work as inline asm.
Maybe a .s file will work.
But I am not sure we can have a .s in the thirdparty directory.

@kbkpbot kbkpbot marked this pull request as draft December 30, 2025 07:33
@spytheman

Copy link
Copy Markdown
Contributor

Afaik, there is no policy restriction for .s files in thirdparty/ , but I am not sure if there will not be technical ones 🤔 . I have not tried using separate .s files till now.

@spytheman

Copy link
Copy Markdown
Contributor

thirdparty/libatomic_ops/atomic_ops.h:293 has a AO_compiler_barrier() macro.
Can it be used instead?

@kbkpbot

kbkpbot commented Dec 30, 2025

Copy link
Copy Markdown
Contributor Author

thirdparty/libatomic_ops/atomic_ops.h:293 has a AO_compiler_barrier() macro. Can it be used instead?

no.
I just created a c file,
c.c

#include <stdio.h>

void main() {
        __asm__ __volatile__("" : : : "memory")
}
$ ./tcc.exe c.c -o c -I ./include -I ./lib/tcc/include
c.c:4: error: ARM asm not implemented.

spytheman pushed a commit that referenced this pull request Dec 31, 2025
@kbkpbot kbkpbot marked this pull request as ready for review January 1, 2026 00:46
Comment thread vlib/v/builder/cc.v
@tankf33der

Copy link
Copy Markdown
Contributor

@spytheman let me know when i should start manual testing.

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Excellent work.
Thank you @kbkpbot .

@spytheman

Copy link
Copy Markdown
Contributor

@tankf33der I think it is ready to be tested (and merged).

Comment thread thirdparty/stdatomic/nix/atomic.S
@tankf33der

Copy link
Copy Markdown
Contributor

@tankf33der I think it is ready to be tested (and merged).

I will start testing in 90mins.

@tankf33der

Copy link
Copy Markdown
Contributor

@spytheman @kbkpbot

CPU Distro Status
X64 Apine compilation failed
X64 Debian Passed
ARM64 Debian Passed
ARM64 Debian Passed
RISCV64 Alpine compilation failed
RISCV64 Debian Passed
PPC64LE Debian Passed
  1. compilation failed text will be on next comments.
  2. When i say Passed it means - that after patching v, v list and VTEST=ONLY=closure ./v test vlib/ works.

@tankf33der

Copy link
Copy Markdown
Contributor

X64 on Alpine failed this way with patch.
v compiled, v list failed. Note it worked on prev. testing, see above.

lambda:~/v$ v list
cannot compile `/home/mpech/v/cmd/tools/vpm`: 1
================== C compilation error (from tcc): ==============
cc: tcc: error: undefined symbol '__atomic_thread_fence'
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

@tankf33der

Copy link
Copy Markdown
Contributor

RISCV64 on Alpine v compilation failed this way with or without patch:

cfarm94:~/v$ gmake
cd ./vc && git clean -xf && git pull --quiet
cd ./thirdparty/tcc && git clean -xf && git pull --quiet
cc  -std=c99 -w -o v1 ./vc/v.c -lm -lpthread  || cmd/tools/cc_compilation_failed_non_windows.sh
./v1 -no-parallel -o v2  cmd/v
./v2 -nocache -o ./v  cmd/v
rm -rf v1 v2
================== C compilation error (from cc): ==============
cc: /usr/lib/gcc/riscv64-alpine-linux-musl/14.2.0/../../../../riscv64-alpine-linux-musl/bin/ld: cannot find /home/tankf33der/v/thirdparty/tcc/lib/libgc.a: No such file or directory
cc: collect2: error: ld returned 1 exit status
================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
gmake: *** [GNUmakefile:118: all] Error 1

@kbkpbot

kbkpbot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

X64 on Alpine failed this way with patch. v compiled, v list failed. Note it worked on prev. testing, see above.

lambda:~/v$ v list
cannot compile `/home/mpech/v/cmd/tools/vpm`: 1
================== C compilation error (from tcc): ==============
cc: tcc: error: undefined symbol '__atomic_thread_fence'
=================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

I need more info to find out what is really going on.
Do you upgrade master to latest git before this test?

please provide more info about your tcc version.

@kbkpbot

kbkpbot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

RISCV64 on Alpine v compilation failed this way with or without patch:

cfarm94:~/v$ gmake
cd ./vc && git clean -xf && git pull --quiet
cd ./thirdparty/tcc && git clean -xf && git pull --quiet
cc  -std=c99 -w -o v1 ./vc/v.c -lm -lpthread  || cmd/tools/cc_compilation_failed_non_windows.sh
./v1 -no-parallel -o v2  cmd/v
./v2 -nocache -o ./v  cmd/v
rm -rf v1 v2
================== C compilation error (from cc): ==============
cc: /usr/lib/gcc/riscv64-alpine-linux-musl/14.2.0/../../../../riscv64-alpine-linux-musl/bin/ld: cannot find /home/tankf33der/v/thirdparty/tcc/lib/libgc.a: No such file or directory
cc: collect2: error: ld returned 1 exit status
================================================================
Try passing `-g` when compiling, to see a .v file:line information, that correlates more with the C error.
(Alternatively, pass `-show-c-output`, to print the full C error message).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .
gmake: *** [GNUmakefile:118: all] Error 1

It seems you miss a prebuilt libgc.a at that location.

@spytheman

Copy link
Copy Markdown
Contributor

I've checked in a Docker container with Alpine x64 , v -no-retry-compilation -cc tcc cmd/tools/vpm (which v list does invoke, to compile vpm), failed with tcc: error: undefined symbol '__atomic_thread_fence' .

386deb8 fixes it for me, by adding a flag for the .S file, and changing _V_atomic_thread_fence for that case to __atomic_thread_fence .

@spytheman

Copy link
Copy Markdown
Contributor

For the Alpine X64 case, make does:

git clone --filter=blob:none --quiet --branch thirdparty-linuxmusl-amd64 https://github.com/vlang/tccbin ./thirdparty/tcc

and for that clone:

/src # ./thirdparty/tcc/tcc.exe --version
tcc version 0.9.27 mob:d3e940c (x86_64 Linux)

@spytheman

spytheman commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

tcc d3e940c is from 2022-04-28 . We should update it indeed. It is currently 499 commits behind the tip of the mob branch of git://repo.or.cz/tinycc.git .

@spytheman

Copy link
Copy Markdown
Contributor

Can we provide both _V_atomic_thread_fence and __atomic_thread_fence symbols in the same .S file?

@kbkpbot

kbkpbot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

Can we provide both _V_atomic_thread_fence and __atomic_thread_fence symbols in the same .S file?

I am not sure it can pass vtcc test, as vtcc already provide a symbol __atomic_thread_fence
https://github.com/felipensp/vtcc/blob/78da82675cf56e2a4c36e1d006bb19265114bd81/lib/libtcc1/stdatomic.v#L14-L17

@spytheman

Copy link
Copy Markdown
Contributor

hm, if there is a pure V implementation (well with inline assembly), why do we need a separate .S file ?

@spytheman

Copy link
Copy Markdown
Contributor

the vtcc implementation does not use a memory barrier 🤔

@kbkpbot

kbkpbot commented Jan 2, 2026

Copy link
Copy Markdown
Contributor Author

hm, if there is a pure V implementation (well with inline assembly), why do we need a separate .S file ?

That is for vtcc only, not for tcc.
And I think vtcc is compiled by gcc not by tcc, as gcc support inline asm for arm64.

@spytheman

Copy link
Copy Markdown
Contributor

vtcc can be compiled with the PR from here on Alpine, just checked:

/src/vtcc # v run make.vsh /src/vtcc
building bt-log.o (tcc_backtrace symbol): ok
building bcheck.o: : ok
building bt-exe.o: : ok
building dso.o: : ok
buildind libtcc1.o: : ok
building vtcc.o: : ok
building libtcc1.a: : ok
buildind vtcc: : ok
/src/vtcc # ./vtcc --version
tcc version 0.9.28rc (x86_64 Linux)
/src/vtcc # ./vtcc -c hello.c -o hello.o
/src/vtcc # ls -la hello.o
-rw-r--r--    1 root     root           976 Jan  2 14:30 hello.o
/src/vtcc #
/src/vtcc #
/src/vtcc # ./vtcc hello.c
tcc: error: file 'crt1.o' not found
tcc: error: file 'crti.o' not found
tcc: error: file 'crtn.o' not found
/src/vtcc #

The last compilation fails because of unrelated reasons - it is tuned to work on a glibc system, and imho making it work on a musl one, is out of scope for this PR.

@spytheman

Copy link
Copy Markdown
Contributor

If the new docker-alpine-musl-tcc CI job passes here, I intend to merge this as it is. We can improve the RISCV64 Alpine case later.

@spytheman

Copy link
Copy Markdown
Contributor

The sanitized CI jobs failed due to an unrelated problem, that is fixed on master.

@spytheman spytheman merged commit 8018dde into vlang:master Jan 3, 2026
89 of 93 checks passed
@spytheman

Copy link
Copy Markdown
Contributor

Thank you @kbkpbot 🙇🏻 .
Thank you @tankf33der.

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.

3 participants