2 ms·
Question: does the Go compiler run meaningful optimization passes? I didn't really see it mentioned here, and given that I hear that the compiler is super fast
by saagarjha 7y ago
Question: does the Go compiler run meaningful optimization passes? I didn't really see it mentioned here, and given that I hear that the compiler is super fast I'm not sure if it does…
- Crinus 7y agoFrom a quick look at the repository it looks like there are optimizations performed: this code[1] seems to perform function inlining at the AST level, this code[2] converts the AST to SSA form which is generally meant to be used for optimizations, the SSA directory[3] contains a bunch of optimizations like common subexpression elimination[4], deadcode elimination[5], useless branch elimination[6] and other stuff (just browse around the files, they have comments about what they do). The SSA seems to contain both generic and machine-specific opcodes (basically instructions for the latter) with the former being converted to the latter using a bunch of rule tables[7] which seem to contain both generic-to-native transforms as well as peephole optimization rules (these are converted to go source code, most likely the huge files in the parent directory). Finally there seem to be a few optimizations in the machine-specific code generator (e.g. [8]). Of course it really depends on your definition of "meaningful", but i think that as long as it isn't equal to "as many optimizations known to humankind as possible, everything else be damned", the compiler looks to perform a decent amount of them. At least for me it passes the subjectively vague "meaningful" check :-P. [1] https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/inl.go https://github.com/golang/go/blob/master/src/cmd/compile/int... [2] https://github.com/golang/go/blob/master/src/cmd/compile/internal/gc/ssa.go https://github.com/golang/go/blob/master/src/cmd/compile/int... [3] https://github.com/golang/go/tree/master/src/cmd/compile/internal/ssa https://github.com/golang/go/tree/master/src/cmd/compile/int... [4] https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/cse.go https://github.com/golang/go/blob/master/src/cmd/compile/int... https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/zcse.go https://github.com/golang/go/blob/master/src/cmd/compile/int... [5] https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/deadcode.go https://github.com/golang/go/blob/master/src/cmd/compile/int... [6] https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/fuse.go https://github.com/golang/go/blob/master/src/cmd/compile/int... [7] https://github.com/golang/go/tree/master/src/cmd/compile/internal/ssa/gen https://github.com/golang/go/tree/master/src/cmd/compile/int... [8] https://github.com/golang/go/blob/master/src/cmd/compile/internal/amd64/ssa.go#L413 https://github.com/golang/go/blob/master/src/cmd/compile/int...
- eliben 7y agoCheck out part 2 (http://eli.thegreenplace.net/2019/go-compiler-internals-adding-a-new-statement-to-go-part-2/ http://eli.thegreenplace.net/2019/go-compiler-internals-addi...) which talks about the compiler backend - there are many optimizations done on SSA
- ernst_klim 7y ago>many optimizations Not many, mostly simple peephole optimizations. https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/gen/generic.rules https://github.com/golang/go/blob/master/src/cmd/compile/int...
- dbremner 7y agoThere is a complete list of passes here[0]. It does some optimizations but much less than a mainstream C or C++ compiler. [0] https://github.com/golang/go/blob/master/src/cmd/compile/internal/ssa/compile.go#L397 https://github.com/golang/go/blob/master/src/cmd/compile/int...