Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime/cgo: build fails with GOARCH=arm GOARM=5 CC=clang #65290

Closed
corhere opened this issue Jan 25, 2024 · 11 comments
Closed

runtime/cgo: build fails with GOARCH=arm GOARM=5 CC=clang #65290

corhere opened this issue Jan 25, 2024 · 11 comments
Assignees
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@corhere
Copy link

corhere commented Jan 25, 2024

Go version

go version go1.22rc2 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22rc2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3163621444=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Attempted to (cross-)compile a cgo program for Linux/ARMv5 using clang.

What did you see happen?

$ docker run --rm -it golang:1.22rc2
root@f78e65b632f2:/go# apt update && apt install -y crossbuild-essential-armhf clang
[...snip...]
root@f78e65b632f2:/go# clang --version
Debian clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
root@f78e65b632f2:/go# CC='clang --target=armv5-none-linux-gnu -mfloat-abi=hard --sysroot=/usr/arm-linux-gnueabihf' GOARCH=arm GOARM=5 CGO_ENABLED=1 go build runtime/cgo
# runtime/cgo
gcc_libinit.c:44:8: error: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Werror,-Watomic-alignment]
gcc_libinit.c:47:6: error: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Werror,-Watomic-alignment]
gcc_libinit.c:49:10: error: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Werror,-Watomic-alignment]
gcc_libinit.c:69:9: error: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Werror,-Watomic-alignment]
gcc_libinit.c:71:3: error: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Werror,-Watomic-alignment]

What did you expect to see?

The runtime/cgo package should build without error, like when using gcc.

root@f78e65b632f2:/go# CC=arm-linux-gnueabihf-gcc GOARCH=arm GOARM=5 CGO_ENABLED=1 go build runtime/cgo
root@f78e65b632f2:/go# arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 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.
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jan 25, 2024
@mauri870 mauri870 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 26, 2024
@mauri870
Copy link
Member

mauri870 commented Jan 26, 2024

This seems to have been introduced with CL505455 addition of __atomic_load_n.

This bit exceeds the max lock-free size (0 bytes) looks weird, a size can never be zero, It is probably some sort of clang or arm target bug since you can reproduce it with a C program.

clang-14 --target=armv5-none-linux-gnu -mfloat-abi=hard --sysroot=/usr/arm-linux-gnueabihf test.c
test.c:8:15: warning: large atomic operation may incur significant performance penalty; the access size (4 bytes) exceeds the max lock-free size (0  bytes) [-Watomic-alignment]
        int result = __atomic_load_n(&myVar, __ATOMIC_RELAXED);
                     ^

You could try the compiler flag -Wno-atomic-alignment, it should get rid of the error.

@corhere
Copy link
Author

corhere commented Jan 26, 2024

I think "max size 0" means that the platform does not natively support atomic operations and they have to be emulated. https://bugs.llvm.org/show_bug.cgi?id=38593

@mauri870
Copy link
Member

mauri870 commented Jan 26, 2024

Interesting! That seems to be the real reason, the error message is just poorly worded.

Mauri

@mauri870 mauri870 added the arch-arm Issues solely affecting the 32-bit arm architecture. label Jan 26, 2024
@mauri870
Copy link
Member

I wonder if we can just ignore the warning in runtime/cgo. Since we build with -Werror it ends up failing the build. Depending on the C compiler version -Wno-atomic-alignment might not even be available. The user can also supply this flag manually when it is needed, but given the misleading error message that might not be initially clear, but that is clang to blame.

diff --git a/src/runtime/cgo/cgo.go b/src/runtime/cgo/cgo.go
index 1e3a502918..4bd8e6a53a 100644
--- a/src/runtime/cgo/cgo.go
+++ b/src/runtime/cgo/cgo.go
@@ -29,6 +29,8 @@ package cgo
 
 #cgo solaris CPPFLAGS: -D_POSIX_PTHREAD_SEMANTICS
 
+#cgo arm CFLAGS: -Wno-atomic-alignment
+
 */
 import "C"

cc @golang/runtime @golang/arm

@mknyszek
Copy link
Contributor

In triage, one thing we're wondering is if this error is actually benign and we're OK with unaligned atomics (which IIRC break some atomicity guarantees) or if we need to actually fix this in the code CL 505455 modified.

CC @cuonglm @ianlancetaylor

@mknyszek mknyszek added this to the Backlog milestone Jan 31, 2024
@prattmic
Copy link
Member

I don't think the issue here is alignment. I think clang is just warning that ARMv5 doesn't really have atomics, as @corhere mentioned.

In our own atomic code, we call into a kernel-provided stub to help with atomics on ARM <7: https://cs.opensource.google/go/go/+/master:src/runtime/internal/atomic/sys_linux_arm.s;l=79-83.

In the kernel, that is https://elixir.bootlin.com/linux/latest/source/arch/arm/kernel/entry-armv.S#L749 -> https://elixir.bootlin.com/linux/latest/source/arch/arm/include/asm/assembler.h#L374. For ARMv5 that is ultimately a nop, because Linux simply doesn't support SMP for ARMv5 at all.

I don't know what the clang-generated code looks like (if someone could provide the disassembly, that would be helpful), but I suspect it is just warning that it has to call into a helper.

If this is accurate, I suspect ignoring the warning is fine.

@corhere
Copy link
Author

corhere commented Feb 5, 2024

Compiler Explorer

a:
  push {r11, lr}
  ldr r0, .LCPI0_0
  mov r1, #0
  bl __atomic_load_4
  pop {r11, lr}
  bx lr

The __atomic_load_4 symbol is defined in GCC's libatomic. (When the -latomic flag is provided, Compiler Explorer will link the binary but won't render the disassembly for some reason.) The libatomic function is presumably implemented in terms of the kernel memory-barrier helper.

@ianlancetaylor
Copy link
Contributor

If we only see the warnings for runtime/cgo/gcc_libinit.c, then we can add a #pragma line to that file to disable the warning. Something like

#pragma GCC diagnostic ignored "-Watomic-alignment"

@mauri870
Copy link
Member

mauri870 commented Feb 7, 2024

I just confirmed that this works. It is less intrusive than disabling the warning in a #cgo directive. Thanks, Ian.

@mauri870 mauri870 self-assigned this Feb 7, 2024
@mauri870 mauri870 added NeedsFix The path to resolution is known, but the work has not been done. and removed help wanted NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 7, 2024
@gopherbot
Copy link

Change https://go.dev/cl/562348 mentions this issue: runtime/cgo: ignore -Watomic-alignment in gcc_libinit.c

@dmitshur dmitshur modified the milestones: Backlog, Go1.23 Feb 10, 2024
@dmitshur dmitshur added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label Feb 10, 2024
ezz-no pushed a commit to ezz-no/go-ezzno that referenced this issue Feb 18, 2024
When cross-compiling a cgo program with CC=clang for Linux/ARMv5,
atomic warnings cause build errors, as cgo uses -Werror.

These warnings seem to be harmless and come from the usage of
__atomic_load_n, which is emulated due to the lack of atomic
instructions in armv5.

Fixes golang#65290

Change-Id: Ie72efb77468f06888f81f15850401dc8ce2c78f9
GitHub-Last-Rev: fbad847
GitHub-Pull-Request: golang#65588
Reviewed-on: https://go-review.googlesource.com/c/go/+/562348
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
@gopherbot
Copy link

Change https://go.dev/cl/567556 mentions this issue: runtime/cgo: ignore unknown warning options

gopherbot pushed a commit that referenced this issue Feb 29, 2024
For #65290
Fixes #65971

Change-Id: If15853f287e06b85bb1cb038b3785516d5812f84
Reviewed-on: https://go-review.googlesource.com/c/go/+/567556
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-arm Issues solely affecting the 32-bit arm architecture. compiler/runtime Issues related to the Go compiler and/or runtime. FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants