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

cmd/compile: optimize variables in function calls as equivalent to constant-derived variables #29166

Open
notimesea opened this issue Dec 9, 2018 · 7 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@notimesea
Copy link

notimesea commented Dec 9, 2018

What version of Go are you using (go version)?

$ go version
go version go1.11.2 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/evgeny/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/evgeny/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.2/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6g/mn04rbw57jjb89x_cb95kxp00000gn/T/go-build220591261=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

go tool compile -S opt.go

opt.go

package main

import "math"

const (
    a = 0.1
    b = 0.2
    c = 0.3
)

var logA = math.Log(a)
var logC = math.Log(c)

func foo(x float64) float64 {
    return math.Exp(math.Log(a) * math.Log(b * x) / math.Log(c))
}

func bar(x float64) float64 {
    return math.Exp(logA * math.Log(b * x) / logC)
}

func main() {}

What did you expect to see?

At first I expect one call of math.Log and one call of math.Exp in both functions (due to optimizations of calling functions on constant which can be done in compile time).
Also I expect no difference in asm code in foo and bar functions

What did you see instead?

In foo function there are three calls of math.Log function which creates overhead in computing of constant value every time when foo called.

"".foo STEXT size=185 args=0x10 locals=0x20
	0x0000 00000 (opt.go:14)	TEXT	"".foo(SB), $32-16
	0x0000 00000 (opt.go:14)	MOVQ	(TLS), CX
	0x0009 00009 (opt.go:14)	CMPQ	SP, 16(CX)
	0x000d 00013 (opt.go:14)	JLS	175
	0x0013 00019 (opt.go:14)	SUBQ	$32, SP
	0x0017 00023 (opt.go:14)	MOVQ	BP, 24(SP)
	0x001c 00028 (opt.go:14)	LEAQ	24(SP), BP
	0x0021 00033 (opt.go:14)	FUNCDATA	$0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (opt.go:14)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (opt.go:14)	FUNCDATA	$3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (opt.go:15)	PCDATA	$2, $0
	0x0021 00033 (opt.go:15)	PCDATA	$0, $0
	0x0021 00033 (opt.go:15)	MOVSD	$f64.3fb999999999999a(SB), X0
	0x0029 00041 (opt.go:15)	MOVSD	X0, (SP)
	0x002e 00046 (opt.go:15)	CALL	math.Log(SB)
	0x0033 00051 (opt.go:15)	MOVSD	8(SP), X0
	0x0039 00057 (opt.go:15)	MOVSD	X0, ""..autotmp_6+16(SP)
	0x003f 00063 (opt.go:15)	MOVSD	$f64.3fc999999999999a(SB), X1
	0x0047 00071 (opt.go:15)	MOVSD	"".x+40(SP), X2
	0x004d 00077 (opt.go:15)	MULSD	X2, X1
	0x0051 00081 (opt.go:15)	MOVSD	X1, (SP)
	0x0056 00086 (opt.go:15)	CALL	math.Log(SB)
	0x005b 00091 (opt.go:15)	MOVSD	""..autotmp_6+16(SP), X0
	0x0061 00097 (opt.go:15)	MULSD	8(SP), X0
	0x0067 00103 (opt.go:15)	MOVSD	X0, ""..autotmp_6+16(SP)
	0x006d 00109 (opt.go:15)	MOVSD	$f64.3fd3333333333333(SB), X1
	0x0075 00117 (opt.go:15)	MOVSD	X1, (SP)
	0x007a 00122 (opt.go:15)	CALL	math.Log(SB)
	0x007f 00127 (opt.go:15)	MOVSD	8(SP), X0
	0x0085 00133 (opt.go:15)	MOVSD	""..autotmp_6+16(SP), X1
	0x008b 00139 (opt.go:15)	DIVSD	X0, X1
	0x008f 00143 (opt.go:15)	MOVSD	X1, (SP)
	0x0094 00148 (opt.go:15)	CALL	math.Exp(SB)
	0x0099 00153 (opt.go:15)	MOVSD	8(SP), X0
	0x009f 00159 (opt.go:15)	MOVSD	X0, "".~r1+48(SP)
	0x00a5 00165 (opt.go:15)	MOVQ	24(SP), BP
	0x00aa 00170 (opt.go:15)	ADDQ	$32, SP
	0x00ae 00174 (opt.go:15)	RET
	0x00af 00175 (opt.go:15)	NOP
	0x00af 00175 (opt.go:14)	PCDATA	$0, $-1
	0x00af 00175 (opt.go:14)	PCDATA	$2, $-1
	0x00af 00175 (opt.go:14)	CALL	runtime.morestack_noctxt(SB)
	0x00b4 00180 (opt.go:14)	JMP	0
	0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 9c  eH..%....H;a....
	0x0010 00 00 00 48 83 ec 20 48 89 6c 24 18 48 8d 6c 24  ...H.. H.l$.H.l$
	0x0020 18 f2 0f 10 05 00 00 00 00 f2 0f 11 04 24 e8 00  .............$..
	0x0030 00 00 00 f2 0f 10 44 24 08 f2 0f 11 44 24 10 f2  ......D$....D$..
	0x0040 0f 10 0d 00 00 00 00 f2 0f 10 54 24 28 f2 0f 59  ..........T$(..Y
	0x0050 ca f2 0f 11 0c 24 e8 00 00 00 00 f2 0f 10 44 24  .....$........D$
	0x0060 10 f2 0f 59 44 24 08 f2 0f 11 44 24 10 f2 0f 10  ...YD$....D$....
	0x0070 0d 00 00 00 00 f2 0f 11 0c 24 e8 00 00 00 00 f2  .........$......
	0x0080 0f 10 44 24 08 f2 0f 10 4c 24 10 f2 0f 5e c8 f2  ..D$....L$...^..
	0x0090 0f 11 0c 24 e8 00 00 00 00 f2 0f 10 44 24 08 f2  ...$........D$..
	0x00a0 0f 11 44 24 30 48 8b 6c 24 18 48 83 c4 20 c3 e8  ..D$0H.l$.H.. ..
	0x00b0 00 00 00 00 e9 47 ff ff ff                       .....G...
	rel 5+4 t=16 TLS+0
	rel 37+4 t=15 $f64.3fb999999999999a+0
	rel 47+4 t=8 math.Log+0
	rel 67+4 t=15 $f64.3fc999999999999a+0
	rel 87+4 t=8 math.Log+0
	rel 113+4 t=15 $f64.3fd3333333333333+0
	rel 123+4 t=8 math.Log+0
	rel 149+4 t=8 math.Exp+0
	rel 176+4 t=8 runtime.morestack_noctxt+0
"".bar STEXT size=122 args=0x10 locals=0x18
	0x0000 00000 (opt.go:18)	TEXT	"".bar(SB), $24-16
	0x0000 00000 (opt.go:18)	MOVQ	(TLS), CX
	0x0009 00009 (opt.go:18)	CMPQ	SP, 16(CX)
	0x000d 00013 (opt.go:18)	JLS	115
	0x000f 00015 (opt.go:18)	SUBQ	$24, SP
	0x0013 00019 (opt.go:18)	MOVQ	BP, 16(SP)
	0x0018 00024 (opt.go:18)	LEAQ	16(SP), BP
	0x001d 00029 (opt.go:18)	FUNCDATA	$0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x001d 00029 (opt.go:18)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x001d 00029 (opt.go:18)	FUNCDATA	$3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x001d 00029 (opt.go:19)	PCDATA	$2, $0
	0x001d 00029 (opt.go:19)	PCDATA	$0, $0
	0x001d 00029 (opt.go:19)	MOVSD	$f64.3fc999999999999a(SB), X0
	0x0025 00037 (opt.go:19)	MOVSD	"".x+32(SP), X1
	0x002b 00043 (opt.go:19)	MULSD	X1, X0
	0x002f 00047 (opt.go:19)	MOVSD	X0, (SP)
	0x0034 00052 (opt.go:19)	CALL	math.Log(SB)
	0x0039 00057 (opt.go:19)	MOVSD	"".logA(SB), X0
	0x0041 00065 (opt.go:19)	MULSD	8(SP), X0
	0x0047 00071 (opt.go:19)	MOVSD	"".logC(SB), X1
	0x004f 00079 (opt.go:19)	DIVSD	X1, X0
	0x0053 00083 (opt.go:19)	MOVSD	X0, (SP)
	0x0058 00088 (opt.go:19)	CALL	math.Exp(SB)
	0x005d 00093 (opt.go:19)	MOVSD	8(SP), X0
	0x0063 00099 (opt.go:19)	MOVSD	X0, "".~r1+40(SP)
	0x0069 00105 (opt.go:19)	MOVQ	16(SP), BP
	0x006e 00110 (opt.go:19)	ADDQ	$24, SP
	0x0072 00114 (opt.go:19)	RET
	0x0073 00115 (opt.go:19)	NOP
	0x0073 00115 (opt.go:18)	PCDATA	$0, $-1
	0x0073 00115 (opt.go:18)	PCDATA	$2, $-1
	0x0073 00115 (opt.go:18)	CALL	runtime.morestack_noctxt(SB)
	0x0078 00120 (opt.go:18)	JMP	0
	0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 76 64 48  eH..%....H;a.vdH
	0x0010 83 ec 18 48 89 6c 24 10 48 8d 6c 24 10 f2 0f 10  ...H.l$.H.l$....
	0x0020 05 00 00 00 00 f2 0f 10 4c 24 20 f2 0f 59 c1 f2  ........L$ ..Y..
	0x0030 0f 11 04 24 e8 00 00 00 00 f2 0f 10 05 00 00 00  ...$............
	0x0040 00 f2 0f 59 44 24 08 f2 0f 10 0d 00 00 00 00 f2  ...YD$..........
	0x0050 0f 5e c1 f2 0f 11 04 24 e8 00 00 00 00 f2 0f 10  .^.....$........
	0x0060 44 24 08 f2 0f 11 44 24 28 48 8b 6c 24 10 48 83  D$....D$(H.l$.H.
	0x0070 c4 18 c3 e8 00 00 00 00 eb 86                    ..........
	rel 5+4 t=16 TLS+0
	rel 33+4 t=15 $f64.3fc999999999999a+0
	rel 53+4 t=8 math.Log+0
	rel 61+4 t=15 "".logA+0
	rel 75+4 t=15 "".logC+0
	rel 89+4 t=8 math.Exp+0
	rel 116+4 t=8 runtime.morestack_noctxt+0
"".main STEXT nosplit size=1 args=0x0 locals=0x0
	0x0000 00000 (opt.go:22)	TEXT	"".main(SB), NOSPLIT, $0-0
	0x0000 00000 (opt.go:22)	FUNCDATA	$0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0000 00000 (opt.go:22)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0000 00000 (opt.go:22)	FUNCDATA	$3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0000 00000 (opt.go:22)	RET
	0x0000 c3                                               .
"".init STEXT size=166 args=0x0 locals=0x18
	0x0000 00000 (<autogenerated>:1)	TEXT	"".init(SB), $24-0
	0x0000 00000 (<autogenerated>:1)	MOVQ	(TLS), CX
	0x0009 00009 (<autogenerated>:1)	CMPQ	SP, 16(CX)
	0x000d 00013 (<autogenerated>:1)	JLS	156
	0x0013 00019 (<autogenerated>:1)	SUBQ	$24, SP
	0x0017 00023 (<autogenerated>:1)	MOVQ	BP, 16(SP)
	0x001c 00028 (<autogenerated>:1)	LEAQ	16(SP), BP
	0x0021 00033 (<autogenerated>:1)	FUNCDATA	$0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (<autogenerated>:1)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (<autogenerated>:1)	FUNCDATA	$3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
	0x0021 00033 (<autogenerated>:1)	PCDATA	$2, $0
	0x0021 00033 (<autogenerated>:1)	PCDATA	$0, $0
	0x0021 00033 (<autogenerated>:1)	MOVBLZX	"".initdone·(SB), AX
	0x0028 00040 (<autogenerated>:1)	CMPB	AL, $1
	0x002a 00042 (<autogenerated>:1)	JLS	54
	0x002c 00044 (<autogenerated>:1)	PCDATA	$2, $-2
	0x002c 00044 (<autogenerated>:1)	PCDATA	$0, $-2
	0x002c 00044 (<autogenerated>:1)	MOVQ	16(SP), BP
	0x0031 00049 (<autogenerated>:1)	ADDQ	$24, SP
	0x0035 00053 (<autogenerated>:1)	RET
	0x0036 00054 (<autogenerated>:1)	JNE	63
	0x0038 00056 (<autogenerated>:1)	PCDATA	$2, $0
	0x0038 00056 (<autogenerated>:1)	PCDATA	$0, $0
	0x0038 00056 (<autogenerated>:1)	CALL	runtime.throwinit(SB)
	0x003d 00061 (<autogenerated>:1)	UNDEF
	0x003f 00063 (<autogenerated>:1)	MOVB	$1, "".initdone·(SB)
	0x0046 00070 (<autogenerated>:1)	CALL	math.init(SB)
	0x004b 00075 (opt.go:11)	MOVSD	$f64.3fb999999999999a(SB), X0
	0x0053 00083 (opt.go:11)	MOVSD	X0, (SP)
	0x0058 00088 (opt.go:11)	CALL	math.Log(SB)
	0x005d 00093 (opt.go:11)	MOVSD	8(SP), X0
	0x0063 00099 (opt.go:11)	MOVSD	X0, "".logA(SB)
	0x006b 00107 (opt.go:12)	MOVSD	$f64.3fd3333333333333(SB), X0
	0x0073 00115 (opt.go:12)	MOVSD	X0, (SP)
	0x0078 00120 (opt.go:12)	CALL	math.Log(SB)
	0x007d 00125 (opt.go:12)	MOVSD	8(SP), X0
	0x0083 00131 (opt.go:12)	MOVSD	X0, "".logC(SB)
	0x008b 00139 (<autogenerated>:1)	MOVB	$2, "".initdone·(SB)
	0x0092 00146 (<autogenerated>:1)	MOVQ	16(SP), BP
	0x0097 00151 (<autogenerated>:1)	ADDQ	$24, SP
	0x009b 00155 (<autogenerated>:1)	RET
	0x009c 00156 (<autogenerated>:1)	NOP
	0x009c 00156 (<autogenerated>:1)	PCDATA	$0, $-1
	0x009c 00156 (<autogenerated>:1)	PCDATA	$2, $-1
	0x009c 00156 (<autogenerated>:1)	CALL	runtime.morestack_noctxt(SB)
	0x00a1 00161 (<autogenerated>:1)	JMP	0
	0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 0f 86 89  eH..%....H;a....
	0x0010 00 00 00 48 83 ec 18 48 89 6c 24 10 48 8d 6c 24  ...H...H.l$.H.l$
	0x0020 10 0f b6 05 00 00 00 00 3c 01 76 0a 48 8b 6c 24  ........<.v.H.l$
	0x0030 10 48 83 c4 18 c3 75 07 e8 00 00 00 00 0f 0b c6  .H....u.........
	0x0040 05 00 00 00 00 01 e8 00 00 00 00 f2 0f 10 05 00  ................
	0x0050 00 00 00 f2 0f 11 04 24 e8 00 00 00 00 f2 0f 10  .......$........
	0x0060 44 24 08 f2 0f 11 05 00 00 00 00 f2 0f 10 05 00  D$..............
	0x0070 00 00 00 f2 0f 11 04 24 e8 00 00 00 00 f2 0f 10  .......$........
	0x0080 44 24 08 f2 0f 11 05 00 00 00 00 c6 05 00 00 00  D$..............
	0x0090 00 02 48 8b 6c 24 10 48 83 c4 18 c3 e8 00 00 00  ..H.l$.H........
	0x00a0 00 e9 5a ff ff ff                                ..Z...
	rel 5+4 t=16 TLS+0
	rel 36+4 t=15 "".initdone·+0
	rel 57+4 t=8 runtime.throwinit+0
	rel 65+4 t=15 "".initdone·+-1
	rel 71+4 t=8 math.init+0
	rel 79+4 t=15 $f64.3fb999999999999a+0
	rel 89+4 t=8 math.Log+0
	rel 103+4 t=15 "".logA+0
	rel 111+4 t=15 $f64.3fd3333333333333+0
	rel 121+4 t=8 math.Log+0
	rel 135+4 t=15 "".logC+0
	rel 141+4 t=15 "".initdone·+-1
	rel 157+4 t=8 runtime.morestack_noctxt+0
go.loc."".foo SDWARFLOC size=0
go.info."".foo SDWARFINFO size=54
	0x0000 02 22 22 2e 66 6f 6f 00 00 00 00 00 00 00 00 00  ."".foo.........
	0x0010 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 01 0e  ................
	0x0020 78 00 00 0e 00 00 00 00 00 0e 7e 72 31 00 01 0e  x.........~r1...
	0x0030 00 00 00 00 00 00                                ......
	rel 8+8 t=1 "".foo+0
	rel 16+8 t=1 "".foo+185
	rel 26+4 t=29 gofile../Users/evgeny/exp/opt.go+0
	rel 36+4 t=28 go.info.float64+0
	rel 48+4 t=28 go.info.float64+0
go.range."".foo SDWARFRANGE size=0
go.isstmt."".foo SDWARFMISC size=0
	0x0000 04 13 04 0e 03 08 01 86 01 02 0a 00              ............
go.loc."".bar SDWARFLOC size=0
go.info."".bar SDWARFINFO size=54
	0x0000 02 22 22 2e 62 61 72 00 00 00 00 00 00 00 00 00  ."".bar.........
	0x0010 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 01 0e  ................
	0x0020 78 00 00 12 00 00 00 00 00 0e 7e 72 31 00 01 12  x.........~r1...
	0x0030 00 00 00 00 00 00                                ......
	rel 8+8 t=1 "".bar+0
	rel 16+8 t=1 "".bar+122
	rel 26+4 t=29 gofile../Users/evgeny/exp/opt.go+0
	rel 36+4 t=28 go.info.float64+0
	rel 48+4 t=28 go.info.float64+0
go.range."".bar SDWARFRANGE size=0
go.isstmt."".bar SDWARFMISC size=0
	0x0000 04 0f 04 0e 03 08 01 4e 02 07 00                 .......N...
go.loc."".main SDWARFLOC size=0
go.info."".main SDWARFINFO size=33
	0x0000 02 22 22 2e 6d 61 69 6e 00 00 00 00 00 00 00 00  ."".main........
	0x0010 00 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 01  ................
	0x0020 00                                               .
	rel 9+8 t=1 "".main+0
	rel 17+8 t=1 "".main+1
	rel 27+4 t=29 gofile../Users/evgeny/exp/opt.go+0
go.range."".main SDWARFRANGE size=0
go.isstmt."".main SDWARFMISC size=0
	0x0000 04 01 00                                         ...
go.loc."".init SDWARFLOC size=0
go.info."".init SDWARFINFO size=33
	0x0000 02 22 22 2e 69 6e 69 74 00 00 00 00 00 00 00 00  ."".init........
	0x0010 00 00 00 00 00 00 00 00 00 01 9c 00 00 00 00 01  ................
	0x0020 00                                               .
	rel 9+8 t=1 "".init+0
	rel 17+8 t=1 "".init+166
	rel 27+4 t=29 gofile..<autogenerated>+0
go.range."".init SDWARFRANGE size=0
go.isstmt."".init SDWARFMISC size=0
	0x0000 04 13 04 0e 03 07 01 04 02 0a 01 07 02 02 01 0c  ................
	0x0010 02 08 01 18 02 08 01 18 02 07 01 0a 02 0a 00     ...............
"".logA SNOPTRBSS size=8
"".logC SNOPTRBSS size=8
"".initdone· SNOPTRBSS size=1
type..importpath.math. SRODATA dupok size=7
	0x0000 00 00 04 6d 61 74 68                             ...math
gclocals·33cdeccccebe80329f1fdbee7f5874cb SRODATA dupok size=8
	0x0000 01 00 00 00 00 00 00 00                          ........
@notimesea notimesea changed the title Poor optimization Poor optimization Dec 9, 2018
@odeke-em odeke-em changed the title Poor optimization cmd/compile: optimize variables in function calls as equivalent to constant-derived variables Dec 9, 2018
@odeke-em
Copy link
Member

odeke-em commented Dec 9, 2018

Thank you for filing this issue @notimesea and welcome to the Go project!

I believe this is a tricky one:
a) math.Log(a) vs logA: what happens if math.Log(a) generates a different result undeterministically or has internal state or is not a pure function? logA is a value that's computable at compile time
b) math.Log(a) could be inlinable(currently is not) depending on its content but essentially it might be tough to expect the same performance for a variable that's computed at runtime vs one that can't be proven to the compiler to return deterministically

I'll page some experts who can help answer better or help move the needle forward /cc @josharian @randall77 @TocarIP

@odeke-em odeke-em added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 9, 2018
@notimesea
Copy link
Author

notimesea commented Dec 9, 2018

Thank you for the answer.

Obviously, I expect most math.* functions to be pure.

Comparing to gcc: https://godbolt.org/z/Iryl4h
You can see that gcc compiler finds both functions to be equivalent. It even creates jump when calling bar function.

@randall77
Copy link
Contributor

We've been throwing around the idea of analyzing functions for "pureness" (side effects, etc.). We could use that info to do a better job here.
I can't find the issue at the moment.

I don't think we want to go down the road of evaluating functions at compile time. But we could run them once and cache the result if we knew the function was pure.

@josharian
Copy link
Contributor

I think you're thinking of #22971. Note that we do evaluate math.Sqrt(const) at compile time, although that is a special case because of hardware support for sqrt on some processors.

@bcmills
Copy link
Contributor

bcmills commented Dec 11, 2018

I don't think we want to go down the road of evaluating functions at compile time.

For simple functions that's just inlining + constant-folding, both of which we should arguably do...

@josharian
Copy link
Contributor

Of course. I think Keith meant “special casing package math functions” to be evaluated at compile time. Note also that Exp is implemented in assembly, so it is not a candidate for inlining. And I don’t think math/bits has enough firepower yet for a Go rewrite, but it would be interesting to double-check, and to consider what extra API would be needed.

@randall77
Copy link
Contributor

Sure, if the function is inlineable and constant folds to a constant, then that is fine.
I was thinking more about functions with loops. (I thought math.Log was one of those, but apparently even the generic implementation math.log doesn't loop.)

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
@seankhliao seankhliao added this to the Unplanned milestone Aug 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
Status: Triage Backlog
Development

No branches or pull requests

7 participants