Building a Better Go Linker
What I would give for the developers of the Go toolchain to have spent the last decade improving GCC or LLVM instead of their own bespoke toolchain.<p>In many ways Go seems like an excuse for Google to fund the continued development of Plan 9. Three of the five most influential people on the Go team (Ken Thompson, Rob Pike, and Russ Cox) were heavily involved in Plan 9. And it shows. Go&#x27;s toolchain is a direct descendant of the Plan 9 toolchain; in fact, the Go language is really just an evolution of the special C dialect that Plan 9 used [1]. Indeed, for a while, the Go compiler was written in this special dialect of C, and so building Go required building a C compiler (!) that could compile this custom dialect of C, and using <i>that</i> to compile the Go compiler [2].<p>By all rights, Plan 9 was an interesting research project, and seems well loved by those familiar with it. (I&#x27;m not personally familiar; it was well before my time.) But it never took off. What we ended up with is Linux, macOS, and Windows.<p>Go very much <i>wants</i> to be Plan 9. Sure, it&#x27;s not a full-fledged operating system. But it&#x27;s a linker, assembler, compiler, binutils, and scheduler. All it asks of the host system is memory management, networking, and filesystem support, and it will happily replace your system&#x27;s DNS resolution with a pure Go version if you ask it to [3]. I wouldn&#x27;t be surprised if Go ships its own TCP&#x2F;IP stack someday [4].<p>This is, in my opinion, craziness. What other language ships its own <i>assembler</i>?! [5] To make matters worse, the assembly syntax is largely undocumented, and what is documented are the strange, unnecessary quirks, like<p>&gt; Instructions, registers, and assembler directives are always in UPPER CASE to remind you that assembly programming is a fraught endeavor. (Exception: the g register renaming on ARM.)<p>&gt; In the general case, the frame size is followed by an argument size, separated by a minus sign. (It&#x27;s not a subtraction, just idiosyncratic syntax.)<p>&gt; In Go object files and binaries, the full name of a symbol is the package path followed by a period and the symbol name: fmt.Printf or math&#x2F;rand.Int. Because the assembler&#x27;s parser treats period and slash as punctuation, those strings cannot be used directly as identifier names. Instead, the assembler allows the middle dot character U+00B7 and the division slash U+2215 in identifiers and rewrites them to plain period and slash.<p>The excuse for the custom toolchain has always been twofold, that a) LLVM is too slow, and fast compiles are one of Go&#x27;s main features, and b) that the core team was too unfamiliar with GCC&#x2F;LLVM, at least in the early days, and attempting to build Go on top of LLVM would have slowed the speed of innovation to a degree that Go might not exist [6].<p>I&#x27;ve always been skeptical of argument (b). After all, one of Go&#x27;s creators literally won a Turing award, as this document not-so-subtly mentions. I&#x27;m quite sure they could have figured out how to build an LLVM frontend, given the desire. Rust, for example, is quite a bit more complicated than Go, and Mozilla&#x27;s developers have had no trouble integrating with LLVM. I suspect the real reason was that hacking on the Plan 9 toolchain was more fun and more familiar—which is a very valid personal reason to work on something! But it doesn&#x27;t mean it was the right strategic decision.<p>I will say that (a) is valid. I recently switched from writing Go to writing Rust, and I miss the compile times of Go desperately.<p>That said—and this is what I can&#x27;t get past—the state of compilers would be much better off if the folks on the Go team had invested more in improving the compile and link times of LLVM or GCC. Every improvement to lld wouldn&#x27;t just speed up compiles for Go; it would speed up compiles for C, C++, Swift, Rust, Fortran, Kotlin, and anything else with an LLVM frontend.<p>In the last year or so, the gollvm project [7] (which is exactly what you&#x27;d expect–a Go frontend for LLVM) has seen some very active development, and I&#x27;m following along excitedly. Unfortunately I still can&#x27;t quite tell whether it&#x27;s Than McIntosh&#x27;s 20% time project or an actual staffed project of Google&#x27;s, albeit a small time one. (There are really only two committers, Than and Cherry Zhang.) There are so many optimizations that will likely never be added to gc, like a register-based calling convention [8] and autovectorization, that you essentially get for &quot;free&quot; (i.e., with a bit of plumbing from the frontend) with a mature toolchain like LLVM.<p>There are not many folks who have the knowledge and expertise to work on compilers and linkers these days, and those that do can command high salaries. Google is in the luxurious position of being able to afford many dozens of these people. I just wish that someone with the power to effect change at Google would realize that the priorities are backwards. gccgo&#x2F;gollvm are where the primary investment should be occurring, and the gc toolchain should be a side project that makes debug builds fast... not the production compiler, where the quality of the object code is the primary objective.<p>[0]: <a href="https:&#x2F;&#x2F;dave.cheney.net&#x2F;2013&#x2F;10&#x2F;15&#x2F;how-does-the-go-build-command-work" rel="nofollow">https:&#x2F;&#x2F;dave.cheney.net&#x2F;2013&#x2F;10&#x2F;15&#x2F;how-does-the-go-build-com...</a><p>[1]: <a href="http:&#x2F;&#x2F;doc.cat-v.org&#x2F;plan_9&#x2F;programming&#x2F;c_programming_in_plan_9" rel="nofollow">http:&#x2F;&#x2F;doc.cat-v.org&#x2F;plan_9&#x2F;programming&#x2F;c_programming_in_pla...</a><p>[2]: <a href="https:&#x2F;&#x2F;docs.google.com&#x2F;document&#x2F;d&#x2F;1P3BLR31VA8cvLJLfMibSuTdwTuF7WWLux71CYD0eeD8&#x2F;edit" rel="nofollow">https:&#x2F;&#x2F;docs.google.com&#x2F;document&#x2F;d&#x2F;1P3BLR31VA8cvLJLfMibSuTdw...</a><p>[3]: <a href="https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;net&#x2F;" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;pkg&#x2F;net&#x2F;</a><p>[4]: <a href="https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;netstack" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;google&#x2F;netstack</a><p>[5]: <a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;asm" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;asm</a><p>[6]: <a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;faq#What_compiler_technology_is_used_to_build_the_compilers" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;faq#What_compiler_technology_is_used_...</a><p>[7]: <a href="https:&#x2F;&#x2F;go.googlesource.com&#x2F;gollvm&#x2F;" rel="nofollow">https:&#x2F;&#x2F;go.googlesource.com&#x2F;gollvm&#x2F;</a><p>[8]: <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;issues&#x2F;18597" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;issues&#x2F;18597</a>