What's Go cmd option 'gcflags' all possible values

Viewed 11610

I'm learning about Go memory optimization through inlining. The following code is my test code.

I use go build -gcflags=-m=2 main.go to get all the results. I follow Dave Cheney's post to do this.

My question is: What're the values of 'gcflags'? Where is the documentation, I couldn't find the doc by Google?

func fn1() int {
    s := 1+2
    return s
}

func fn2() string {
    a := "h"
    a += "e"
    a += "l"
    a += "l"
    a += "o"
    b := " "
    c := "w"
    c += "o"
    c += "r"
    c += "l"
    c += "d"
    d := "d"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    d += "e"
    s := a + b + c
    return s
}

func fn3() {
    p := fn1()

    s := fn2()

    println("p = " + string(p))
    println("s = " + s)
}

output:

go build -gcflags='-m=2' main.go
# command-line-arguments
./main.go:3:6: can inline fn1 as: func() int { s := 1 + 2; return s }
./main.go:16:6: cannot inline fn2: function too complex: cost 81 exceeds budget 80
./main.go:47:6: cannot inline fn3: function too complex: cost 85 exceeds budget 80

I also tried -gcflags=-m=3, -gcflags=-m=4, -gcflags=-m=5, the results is like below, very hard to understand. Maybe somebody can help me point out a direction.

go build -gcflags=-m=3 main.go
# command-line-arguments
./main.go:3:6: can inline fn1 as: func() int { s := 1 + 2; return s }
./main.go:8:6: cannot inline fn2: recursive
./main.go:16:6: can inline fn3 as: func() { p := fn1(); p = fn2(p); println("p = " + string(p)) }
./main.go:17:10: inlining call to fn1 func() int { s := 1 + 2; return s }
./main.go:17:10: Before inlining:
.   CALLFUNC l(17) tc(1) int
.   .   NAME-inlining.fn1 l(3) x(0) class(PFUNC) tc(1) used FUNC-@0
substituting name
.   NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int  ->
.   NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
substituting name
.   NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int  ->
.   NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
substituting name
.   NAME-inlining.s g(2) l(4) x(0) class(PAUTO) tc(1) used int  ->
.   NAME-inlining.s l(4) x(0) class(PAUTO) tc(1) used int
./main.go:17:10: After inlining
.   INLCALL-init
.   .   DCL l(17)
.   .   .   NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int

.   .   AS l(17) tc(1)
.   .   .   NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int

.   .   INLMARK l(+17) x(0)
.   INLCALL l(17) tc(1) int
.   INLCALL-rlist
.   .   NAME-inlining.~r0 l(3) x(0) class(PAUTO) tc(1) assigned used int
..... // too much
2 Answers

-gcflags flag accepts list of flags, that should be passed to go tool compile when it's invoked. So you can see all possible options in its documentation or running command:

go tool compile -help

add -help at the end:

go build -gcflags -help
Related