305 points · by pehringer · 2y ago · 110 comments
1. On amd64 those ints are actually 64bit. If you used int32 then they would be be word aligned in the parameter list. However, there is a gotcha with that. The return values will always start at a dword aligned offset on 64bit system.
2. NOSPLIT is defined in "textflag.h" which Go's compiler automatically provides. However, NOSPLIT is, from everything I've read, only respected on runtime.XX functions, so it's not doing anything there, and it's also not necessary. NOSPLIT tells the compiler not to insert code to check if the stack needs to split because it's going to overflow, which is technically unnecessary if the function doesn't need any stack space. It's basically only there on the function that checks for stack splits, to prevent that code from being injected into itself.
For those who are curious: "In the general case, the frame size [the parameter after NOSPLIT] is followed by an argument size, separated by a minus sign. (It's not a subtraction, just idiosyncratic syntax.) The frame size $24-8 states that the function has a 24-byte frame and is called with 8 bytes of argument, which live on the caller's frame. If NOSPLIT is not specified for the TEXT, the argument size must be provided. For assembly functions with Go prototypes, go vet will check that the argument size is correct."
Source: https://go.dev/doc/asm
Is the language actually called that?
But the more I look into it, the more I think this is just a LLM hallucination.
The doc about the 'assembly' format doesn't give a proper name. It just calls 'go assembler'.
And I think the source of this hallucination was this first paragraph:
> The assembler is based on the input style of the Plan 9 assemblers, which is documented in detail elsewhere. If you plan to write assembly language, you should read that document although much of it is Plan 9-specific. The current document provides a summary of the syntax and the differences with what is explained in that document, and describes the peculiarities that apply when writing assembly code to interact with Go.
The link you provided doesn't say the language is called Plan9. Also, nzach quoted from that page.
Rob Pike spoke on the design of Go's assembler at a talk in 2016 [1][2]. I think it basically came down to the observation that most assembly language is roughly the same, so why not build a common assembly language that "lets you talk to the machine at the lowest level and yet not have to learn a new syntax." It also enables them to automatically generate a working assembler given an instruction manual PDF for a new architecture as input.
[1]: https://www.youtube.com/watch?v=KINIAgRpkDA [2]: https://go.dev/talks/2016/asm.slide#1
I've been doing this for 35 years and cross compiling anything nontrivial was always a toolchain nightmare. Discovering a world where all I had to do was set GOARCH=mips64 (and possibly GOOS=darwin if I wanted mac binaries) before invoking the compiler is so magical I was extremely skeptical when I first read about it.
Like previous you, I have to admit I'm skeptical but would be happy to be wrong.
For those interested, here's Thomson's paper about Plan9 C compilers: https://9p.io/sys/doc/compiler.html and https://doc.cat-v.org/bell_labs/new_c_compilers/
How is this actually used though? I searched the whole article, and no where are any operations being performed on slices.
Edit: I found what I was looking for in the linked docs: https://pkg.go.dev/github.com/pehringer/simd#pkg-index
So basically if you have 2 slices you want to add together, instead of a for loop you could do it in parallel with simd using:
simd.AddInt32(slice1, slice2, result)(1) https://go.dev/wiki/MinimumRequirements#amd64
If this is true, i kindly ask the author to not feel embarrassed or "exposed", but be honest, so we can learn from this. I'd like to gain confidence in these type of "LLM exposed" things, but it never seems like people would admit, no matter how obvious. And of course, here it is not obvious, this is of course a wild, very judgemental guess.
This pisses me off so much.
This can’t be the first pass someone has made at something like this, right? There must be literal dozens of SIMD thirsty Gophers around. Would a more common pattern be to use CGO?
https://pkg.go.dev/github.com/grailbio/base/simd has some work I’ve done in this vein.
The most complete library I've seen (though admittedly never used) uses CGO _partially_, with a neat hack to avoid the overhead that it comes with [1].
[0]: https://github.com/slimsag/rand/blob/f1e8d464c0021a391d5cd64...
I recently ported all of the amd64 assembly in Go's crypto libraries over to Avo. Very useful library for this sort of work!
Go's calling convention is using registers, except when writing own assembly functions then it's stack-based; the latter is also how it worked in the past. See https://go.dev/s/regabi and https://go.dev/doc/asm
Build tags have a form "go:build" not "+build" since Go 1.17, that is for couple of years already.
More about build tags: using both build tags and filename suffix for arch-based conditional compilation is redundant. Just use one of them, not both.
But for the particular case of SIMD operations, wouldn't it make more sense to use the GPU instead of the CPU? GPUs excel at parallelism and matrix operations, so the performance difference would be even greater. I suppose the lack of well maintained GPU packages and community around it don't make Go particularly well suited for this.
- SIMD: up to 400% speed boost, most likely on the same VMs you were already using
- GPU: orders of magnitude faster, but now you need to make sure your cloud servers have compatible GPUs attached
If you really do need crazy performance then it's worth it. If you're already stable and right-scaled and SIMD allows you to reduce your VM spend by 25%, then you probably just take the savings and move on.
> The assembler is based on the input style of the Plan 9 assemblers, which is documented in detail elsewhere. If you plan to write assembly language, you should read that document although much of it is Plan 9-specific.
That first sentence links to https://9p.io/sys/doc/asm.html. So yes, definitely not a coincidence.
It would be like calling the AT&T style of assembly used by GNU binutils a "Linux"
Can someone confirm this? Not even for straightforward copies?
I mean, the fact that Go has own Plan9-derived format for assembler, has absoluely nothing to do with the task author aims to solve.
There's a typo on "Performance"
This was to allow for easy cross-compilation.
You should probably clarify that you are talking about 'golang plan9 assembly format', and not about the plan9 OS. This confused me a lot. (https://en.wikipedia.org/wiki/Plan_9_from_Bell_Labs)
I didn't know you could easily hand write 'golang assembly' for your functions. This seems really useful in some cases.
But I do wonder, why the std lib doesn't offer this ?
EDIT: I found the anwser: https://github.com/golang/go/issues/53171
Here is a good explanation provided by Ian Lance Taylor:
> This proposal is focused on code generation rather than language, and the details of how it changes the language are difficult to understand. Go is intended to be a simple language that it is easy to understand. Introducing complex semantics for performance reasons is not a direction that the language is going to take.
That's certainly an ... interesting choice.
https://plan9.io/sys/doc/comp.html
IIRC before Go was self compiling, it was compiled using 9c, and its architecture inherits from 9c.
EDIT: some nearby threads got the details better!
>Go uses its own internal assembly language called Plan9.
Plan9 is the name of the OS. You wouldn't name a programming language "Linux", even if Linus created it and it was super related or not at all related.
Back in those days I actually found that, with a few small tweaks, I could compile the Plan 9 operating system using Go's C suite. We didn't pursue it further but this was one of the options we looked into for cross-compiling Plan 9 from other operating systems.
In this case specifically, SIMD is available, via compiler supported auto-vectorization, and intrisics, no need to go down into Assembly.