Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Is there anything like march=native in golang?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
CaptainBlood
Advocate
Advocate


Joined: 24 Jan 2010
Posts: 3657

PostPosted: Mon Feb 06, 2023 12:45 am    Post subject: Is there anything like march=native in golang? Reply with quote

A few packages are using golang.
Is there any way to force generated code to be closer to instructions of a specific CPU?

Thks 4 ur attention, interest & support.
_________________
USE="-* ..." in /etc/portage/make.conf here, i.e. a countermeasure to portage implicit braces, belt & diaper paradigm
LT: "I've been doing a passable imitation of the Fontana di Trevi, except my medium is mucus. Sooo much mucus. "
Back to top
View user's profile Send private message
CooSee
Veteran
Veteran


Joined: 20 Nov 2004
Posts: 1455
Location: Earth

PostPosted: Wed Feb 08, 2023 9:21 pm    Post subject: Re: Is there anything like march=native in golang? Reply with quote

CaptainBlood wrote:
A few packages are using golang.
Is there any way to force generated code to be closer to instructions of a specific CPU?

Thks 4 ur attention, interest & support.

i hope this will answer question :)

go env
Code:

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

snippet from go help environment
Code:
Architecture-specific environment variables:

   GOAMD64
      For GOARCH=amd64, the microarchitecture level for which to compile.
      Valid values are v1 (default), v2, v3, v4.
      See https://golang.org/wiki/MinimumRequirements#amd64

from https://github.com/golang/go/wiki/MinimumRequirements#amd64
Code:
amd64

Until Go 1.17, the Go compiler always generated x86 binaries that could be executed by any 64-bit x86 processor.

Go 1.18 introduced 4 architectural levels for AMD64. Each level differs in the set of x86 instructions that the compiler can include in the generated binaries:

    GOAMD64=v1 (default): The baseline. Exclusively generates instructions that all 64-bit x86 processors can execute.
    GOAMD64=v2: all v1 instructions, plus CMPXCHG16B, LAHF, SAHF, POPCNT, SSE3, SSE4.1, SSE4.2, SSSE3.
    GOAMD64=v3: all v2 instructions, plus AVX, AVX2, BMI1, BMI2, F16C, FMA, LZCNT, MOVBE, OSXSAVE.
    GOAMD64=v4: all v3 instructions, plus AVX512F, AVX512BW, AVX512CD, AVX512DQ, AVX512VL.

Setting, for example, GOAMD64=v3, will allow the Go compiler to use AVX2 instructions in the generated binaries (which may improve performance in some cases); but these binaries will not run on older x86 processors that don't support AVX2.

The Go toolchain may also generate newer instructions, but guarded by dynamic checks to ensure they're only executed on capable processors. For example, with GOAMD64=v1, math/bits.OnesCount will still use the POPCNT instruction if CPUID reports that it's available. Otherwise, it falls back to a generic implementation.

The Go toolchain does not currently generate any AVX512 instructions.

Note that processor is a simplification in this context. In practice, support from the entire system (firmware, hypervisor, kernel) is needed.

See section Microarchitecture support for hints on how to use microarchitecture environment variables like GOAMD64.

8)
_________________
" Die Realität ist eine Illusion, die durch Mangel an ehrlicher Kommunikation entsteht "
---
" Der Mensch ist von Natur aus neugierig, was am Ende übrig bleibt ist die Gier "
Back to top
View user's profile Send private message
Ionen
Developer
Developer


Joined: 06 Dec 2018
Posts: 2732

PostPosted: Wed Feb 08, 2023 9:45 pm    Post subject: Reply with quote

You can confirm that it's getting used in verbose logs, e.g. should see something like
Code:
/usr/lib/go/pkg/tool/linux_amd64/asm -p internal/cpu <snip> -D GOAMD64_v3
No need to set anything else but GOAMD64 and it should work.

Not that Go packages I use are anything resources intensive so it doesn't change much at the end of the day :?
Back to top
View user's profile Send private message
CooSee
Veteran
Veteran


Joined: 20 Nov 2004
Posts: 1455
Location: Earth

PostPosted: Wed Feb 08, 2023 10:01 pm    Post subject: Reply with quote

Quote:
No need to set anything else but GOAMD64 and it should work.

but, on my system go env shows GOAMD64_v1 and also looked in re-emerged go ebuilds - all are compiled with GOAMD64_v1

8)
_________________
" Die Realität ist eine Illusion, die durch Mangel an ehrlicher Kommunikation entsteht "
---
" Der Mensch ist von Natur aus neugierig, was am Ende übrig bleibt ist die Gier "
Back to top
View user's profile Send private message
CaptainBlood
Advocate
Advocate


Joined: 24 Jan 2010
Posts: 3657

PostPosted: Wed Feb 08, 2023 10:30 pm    Post subject: Reply with quote

Coosee & Ionen,
Very Interesting. Nice 8)

Primarily I expect smaller binaries from CPU level accuracy.
Speed as a possibly side effect benefit.

recent net-vpn/ruseup-vpn upgrade shows bigger size (+14%) :
Code:
user@amd64 ~ $ pckllbin riseup-vpn
-rwxr-xr-x 1 root root 24448920 févr.  6 04:11 /usr/bin/riseup-vpn
 * net-vpn/riseup-vpn-0.21.11
         Total files : 53
         Total size  : 23.86 MiB
user@amd64 ~ $ pckllbin riseup-vpn
-rwxr-xr-x 1 root root 27151736 févr.  7 00:53 /usr/bin/riseup-vpn
 * net-vpn/riseup-vpn-0.21.11_p20221113
         Total files : 54
         Total size  : 26.44 MiB
Because it is a daemon of some sort, memory footprint may matter in some case, e.g. low memory resources.
build log are showing
Code:
 -D GOAMD64_v1
:wink:
Will soon give it a try creating a dedicated config file in /etc/portage/env with:
Code:
GOAMD64="v[x]"

lto & GOGCCFLAGS could be another track...

Thks 4 ur attention, interest & support.
_________________
USE="-* ..." in /etc/portage/make.conf here, i.e. a countermeasure to portage implicit braces, belt & diaper paradigm
LT: "I've been doing a passable imitation of the Fontana di Trevi, except my medium is mucus. Sooo much mucus. "


Last edited by CaptainBlood on Thu Feb 09, 2023 2:28 am; edited 1 time in total
Back to top
View user's profile Send private message
CooSee
Veteran
Veteran


Joined: 20 Nov 2004
Posts: 1455
Location: Earth

PostPosted: Wed Feb 08, 2023 10:40 pm    Post subject: Reply with quote

just tried with export GOAMD64=v3 and the compile used it /usr/lib/go/pkg/include -D GOOS_linux -D GOARCH_amd64 -shared -D GOAMD64_v3 -o $WORK/b047/asm_linux_amd64.o ./asm_linux_amd64.s

8)
_________________
" Die Realität ist eine Illusion, die durch Mangel an ehrlicher Kommunikation entsteht "
---
" Der Mensch ist von Natur aus neugierig, was am Ende übrig bleibt ist die Gier "
Back to top
View user's profile Send private message
CaptainBlood
Advocate
Advocate


Joined: 24 Jan 2010
Posts: 3657

PostPosted: Wed Feb 08, 2023 11:01 pm    Post subject: Reply with quote

Code:
pckllbin net-vpn/riseup-vpn
-rwxr-xr-x 1 root root 27134168 févr.  9 02:53 /usr/bin/riseup-vpn
 * net-vpn/riseup-vpn-0.21.11_p20221113
         Total files : 54
         Total size  : 26.42 MiB
tiny 0.1% there. :roll: :lol:
Thks 4 ur attention, interest & support.
_________________
USE="-* ..." in /etc/portage/make.conf here, i.e. a countermeasure to portage implicit braces, belt & diaper paradigm
LT: "I've been doing a passable imitation of the Fontana di Trevi, except my medium is mucus. Sooo much mucus. "
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum