golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.72k stars 17.62k forks source link

cmd/compile: switch to a register-based calling convention for Go functions #40724

Open aclements opened 4 years ago

aclements commented 4 years ago

I propose that we switch the Go internal ABI (used between Go functions) from stack-based to register-based argument and result passing for Go ~1.16~ 1.17.

I lay out the details of our proposal and the work required in this document.

The ABI specification can be found here.

This was previously proposed in #18597, where @dr2chase did some excellent prototyping work that our new proposal builds on. With multiple ABI support, we’re now in a much better position to execute on this without breaking compatibility with the existing body of Go assembly code. I’ve opened this new issue to focus on discussion of our new proposal.

/cc @dr2chase @danscales @thanm @cherrymui @mknyszek @prattmic @randall77

An incomplete and evolving list of tasks:

High-priority non-critical path

Enabling steps

Testing

Post-MVP

Cleanup (can be done later)

aclements commented 3 years ago

I've submitted the ABI spec. It may continue to evolve in small ways, but reflects what all of the code is converging around.

zx2c4 commented 3 years ago

I've submitted the ABI spec. It may continue to evolve in small ways, but reflects what all of the code is converging around.

@aclements - In case you didn't see this, https://groups.google.com/g/golang-dev/c/2oHi8t1s0Z8 might be worth considering.

gopherbot commented 3 years ago

Change https://golang.org/cl/292709 mentions this issue: all: merge branch dev.regabi (d3cd4830ad) into master

gopherbot commented 3 years ago

Change https://golang.org/cl/260539 mentions this issue: cmd/compile: add AMD64 parameter register defs, Arg ops, plumb to ssa.Config

gopherbot commented 3 years ago

Change https://golang.org/cl/295789 mentions this issue: cmd/compile/internal-abi: use x87 mode, not MMX mode

gopherbot commented 3 years ago

Change https://golang.org/cl/295792 mentions this issue: cmd/compile/internal-abi: fix ABI0-equivalence for zero-sized values

gopherbot commented 3 years ago

Change https://golang.org/cl/295791 mentions this issue: cmd/compile/internal-abi: fix ABI0-equivalence for zero-sized values

gopherbot commented 3 years ago

Change https://golang.org/cl/295790 mentions this issue: cmd/compile/internal-abi: update internal ABI spec for g register

aclements commented 3 years ago

@aclements - In case you didn't see this, https://groups.google.com/g/golang-dev/c/2oHi8t1s0Z8 might be worth considering.

Thanks @zx2c4 , I just sent a fix. (I'm still catching up from being away for a few weeks.)

gopherbot commented 3 years ago

Change https://golang.org/cl/293390 mentions this issue: cmd/compile: plumb abi info into expandCalls

gopherbot commented 3 years ago

Change https://golang.org/cl/293391 mentions this issue: cmd/compile: change StaticCall to return a "Results"

gopherbot commented 3 years ago

Change https://golang.org/cl/293389 mentions this issue: cmd/compile: plumb abi info into ssagen/ssa

gopherbot commented 3 years ago

Change https://golang.org/cl/293396 mentions this issue: cmd/compile: handle aggregate OpArg in registers

gopherbot commented 3 years ago

Change https://golang.org/cl/293393 mentions this issue: cmd/compile: use abiutils for all rcvr/in/out frame offsets.

gopherbot commented 3 years ago

Change https://golang.org/cl/293394 mentions this issue: cmd/compile: retrieve Args from registers

gopherbot commented 3 years ago

Change https://golang.org/cl/293397 mentions this issue: cmd/compile: refactor out an almost-superfluous arg

gopherbot commented 3 years ago

Change https://golang.org/cl/293392 mentions this issue: cmd/compile: check frame offsets against abi

gopherbot commented 3 years ago

Change https://golang.org/cl/294429 mentions this issue: cmd/compile: pointer maps seem to be right for morestack

gopherbot commented 3 years ago

Change https://golang.org/cl/295309 mentions this issue: cmd/compile: register results seem to work

gopherbot commented 3 years ago

Change https://golang.org/cl/294410 mentions this issue: cmd/compile: register abi, morestack work and mole whacking

gopherbot commented 3 years ago

Change https://golang.org/cl/293889 mentions this issue: cmd/compile: pass register parameters to called function

gopherbot commented 3 years ago

Change https://golang.org/cl/293398 mentions this issue: cmd/compile: pass arguments as register parameters to StaticCall.

gopherbot commented 3 years ago

Change https://golang.org/cl/297637 mentions this issue: cmd/compile: better version of check frame offsets against abi

gopherbot commented 3 years ago

Change https://golang.org/cl/297911 mentions this issue: cmd/compile: make modified Aux type for OpArgXXXX pass ssa/check

gopherbot commented 3 years ago

Change https://golang.org/cl/299269 mentions this issue: reflect: add tests for reflect.Value.Call for the new ABI

gopherbot commented 3 years ago

Change https://golang.org/cl/300113 mentions this issue: runtime: support register ABI Go functions from Windows callbacks

gopherbot commented 3 years ago

Change https://golang.org/cl/298670 mentions this issue: reflect: add register ABI support for makeFuncStub and methodValueCall

gopherbot commented 3 years ago

Change https://golang.org/cl/300112 mentions this issue: runtime: support register ABI for finalizers

gopherbot commented 3 years ago

Change https://golang.org/cl/302049 mentions this issue: cmd/internal/objabi: centralize GOEXPERIMENT parsing

gopherbot commented 3 years ago

Change https://golang.org/cl/302050 mentions this issue: cmd/dist: build bootstrap without GOEXPERIMENT

gopherbot commented 3 years ago

Change https://golang.org/cl/302070 mentions this issue: all: explode GOEXPIREMENT=regabi into 5 sub-experiments

gopherbot commented 3 years ago

Change https://golang.org/cl/302109 mentions this issue: runtime: mark Windows' address-taken asm routines as ABIInternal

gopherbot commented 3 years ago

Change https://golang.org/cl/302071 mentions this issue: cmd/compile: (fixed) spill output parameters passed in registers as autos

gopherbot commented 3 years ago

Change https://golang.org/cl/302249 mentions this issue: cmd/compile: fix open defer of method call

gopherbot commented 3 years ago

Change https://golang.org/cl/302772 mentions this issue: test: make nosplit test invariant to ABI wrappers

gopherbot commented 3 years ago

Change https://golang.org/cl/303069 mentions this issue: runtime: call nanotimeQPC from nanotime1 without a wrapper

gopherbot commented 3 years ago

Change https://golang.org/cl/303070 mentions this issue: runtime: bypass ABI wrapper when calling needm on Windows

gopherbot commented 3 years ago

Change https://golang.org/cl/303314 mentions this issue: cmd/internal/obj/x86: use ABI scratch registers for WRAPPER prologue

gopherbot commented 3 years ago

Change https://golang.org/cl/303432 mentions this issue: cmd/compile: remove now-redundant AuxCall.args

gopherbot commented 3 years ago

Change https://golang.org/cl/303433 mentions this issue: cmd/compile: remove AuxCall.results, cleanup ssagen/ssa.go

gopherbot commented 3 years ago

Change https://golang.org/cl/304189 mentions this issue: cmd/compile: remove more dead code and data structures

gopherbot commented 3 years ago

Change https://golang.org/cl/304233 mentions this issue: cmd/compile: fix array case in types-for-register parameter

gopherbot commented 3 years ago

Change https://golang.org/cl/304549 mentions this issue: cmd/compile: update default ABI choices for calls and bodyless fn stack maps

emarj commented 3 years ago

@alexbrainman

@aclements I reckon there are very few instances of syscall.NewCallback usage. But they are used a lot. ... These two pretty much cover syscall.NewCallback usage. ... I reckon, that is about it. For Microsoft APIs (maybe google code for syscall.NewCallback or windows.NewCallback).

Actually there are other instances where NewCallback is used. One example is to interface with the Core Audio APIs. In this specific case, the support for callbacks with floating point argument is needed. See for example this callback. I think the situation may be similar in graphics APIs, as mentioned in this issue about syscalls https://github.com/golang/go/issues/37273

Would be great if we could get support for floats args (also) in callbacks.

PS: This is beyond the scope of this issue, but a full support of Windows syscall/callbacks could be useful since Microsoft is now trying to make APIs more accessible to other languages by describing them in metadata (announcement).

alexbrainman commented 3 years ago

@emarj

@alexbrainman

@aclements I reckon there are very few instances of syscall.NewCallback usage. But they are used a lot. ... These two pretty much cover syscall.NewCallback usage. ... I reckon, that is about it. For Microsoft APIs (maybe google code for syscall.NewCallback or windows.NewCallback).

Actually there are other instances where NewCallback is used.

No argument.

One example is to interface with the Core Audio APIs.

I did not know about that one. And I would argue that this API is rarely used comparing to building a Windows service or a Windows GUI.

Alex

elagergren-spideroak commented 3 years ago

These two pretty much cover syscall.NewCallback usage.

WinHTTP uses them too.

gopherbot commented 3 years ago

Change https://golang.org/cl/305271 mentions this issue: cmd/compile: set ir.Name.Func in more cases

gopherbot commented 3 years ago

Change https://golang.org/cl/305273 mentions this issue: cmd/compile: eliminate -abiwraplimit

gopherbot commented 3 years ago

Change https://golang.org/cl/305274 mentions this issue: cmd/compile: restructure ABI wrapper generation, export ABI

gopherbot commented 3 years ago

Change https://golang.org/cl/305272 mentions this issue: cmd/compile: assert that function values reference ABIInternal