Skip to content

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

Open
@aclements

Description

@aclements

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:

  • Define ABI specification (CL, @aclements)
  • Add GOEXPERIMENT=regabi and asm macro (CL)
  • cmd/compile: late call lowering (CLs)
  • cmd/compile: implement register arguments (@dr2chase)
    • spill around morestack call (CL)
    • add temporary ABI pragma for testing (CL)
    • add abstract argument registers (WIP CL)
    • implement ABI rules (CL)
    • use abstract argument registers
  • cmd/compile: implement register returns
  • cmd/compile: ABI wrappers (CL, old CL)
    • cmd/link: mangle wrapper names for ELF (CL)
    • cmd/link: mangle wrapper names for PE (CL)
    • implement zeroing of X15 in ABI0->ABIInternal wrappers (CL)
  • cmd/asm: add syntax for runtime packages to reference ABIInternal symbols (CL)
    • Mark runtime.{goexit,asyncPreempt}, reflect.{makeFuncStub,methodValueCall} definitions ABIInternal (CL)
    • Mark reference to runtime.callbackWrap in sys_windows_amd64.s as ABIInternal (CL)
  • runtime: fix assembly closure calls (mcall) (@aclements, CL)
  • reflect: support calling ABIInternal (CL)
  • reflect: support MakeFunc and method value calls (CL)
  • go and defer statements
    • cmd/compile: desugar go/defer to zero-argument (@thanm, CL)
    • cmd/compile: fix defer recover() (@cherrymui, CL)
    • cmd/compile: desugar go/defer with results (@cherrymui, CL)
  • cmd/cgo: decouple cgo callbacks from ABI (CL)
  • runtime: finalizer support (CL)
  • runtime: Windows callback support (CL)
  • cmd/internal/obj: introduce and use RegEntryTmp registers in prologue (@mknyszek, CL)
  • cmd/compile: generate correct ABI0 argument maps for body-less functions (see WriteFuncMap) (@dr2chase, CL)
  • cmd/compile: make cgo_unsafe_args generate an ABI0 function (or an ABIInternal-with-no-registers function) (@cherrymui, CL)
  • runtime: update debug call protocol (CL)
  • Fix ABI0/ABIInternal confusion in cgo (CL)
  • Run signature fuzzer and fix bugs (everyone)
  • Get all.bash to pass on Linux, Windows, and Darwin (everyone)

High-priority non-critical path

  • Export ABI information across package boundaries to eliminate more wrappers (@aclements, CL)
  • Make funcPC always return the "native" PC of a function (maybe also introduce ABIOther) (@cherrymui, CL)
  • Strip unnecessary ABIInternal annotations once funcPC is in
  • Tracebacks
    • Define extended argument metadata format (in progress, @cherrymui)
    • cmd/compile: produce extended argument metadata (@cherrymui, CL)
    • runtime: consume extended argument metadata (@cherrymui, CL)
    • cmd/compile,runtime: traceback metadata for spill slot liveness
  • DWARF
    • cmd/compile: ensure arguments have appropriate DWARF locations
    • Ensure GDB and Delve can find register arguments
    • [ ] cmd/compile: add a DWARF vendor attribute with function argument frame size (context)
  • Wrapper mangling for non-critical arches
    • cmd/link: mangle wrapper names for Mach-O (CL)
    • cmd/link: mangle wrapper names for xcoff
    • cmd/link: mangle wrapper names for Plan 9
  • runtime: simplify go/defer call paths
    • assert that go/defer frames are zero sized (CL)
    • replace defer reflectcall with a direct closure call (CL)
  • cmd/internal/obj: reject splittable ABIInternal functions without morestack spill info (e.g., asm functions) because we can't generate a correct morestack path (CL)
  • cmd/asm: don't reference args_stackmap in ABIInternal functions (because it's for ABI0) (CL)
  • Port performance-critical assembly to ABIInternal (see CL 296372 for valuable functions) (CL 308931, CL 310184)
  • math: fix overhead of assembly->Go calls CL 310331
  • Release notes
    • Mention undocumented behavior we've observed applications depending on that may have changed.
      • Using reflect.ValueOf(fn).Pointer() to get the PC of an assembly function will now return the PC of the ABI wrapper
    • Performance surprise: calling from assembly to Go is slightly more expensive.
    • Performance surprise: calling an assembly function via a closure is slightly more expensive.
    • Traceback format is much improved

Enabling steps

  • Enable regabiwrappers by default on amd64 (Linux, Windows, and Darwin)
  • Enable regabidefer
  • Enable regabireflect
  • Enable regabig
  • Enable regabiargs

Testing

  • Function signature fuzzer (ongoing)
    • Static call, closure call, method call, interface call, reflect.{ValueOf(target),MakeFunc(x, target),Method(x)}.{Call,Interface}, called from defer, called from go, called as a finalizer
    • {Big,Small} {argument,result} in {memory,registers} and {not addressed,addressed and not leaked to heap,addressed and leaked to heap}
    • Use defer in a test function (check arguments, modify results)
    • Pass pointers-to-stack and pointers-to-heap
    • Cause result to move to heap
    • runtime: add MoveStackOnNextCall, assertions for pointer-to-stack/pointer-to-heap, assertions for live/dead (@aclements, CL)
    • Integrate runtime testing hook
    • Clean up fuzzer, and get into tools repo.
  • cmd/compile: revive clobberdead (CL)
  • cmd/compile: add clobberdeadreg (CL)

Post-MVP

  • Move g from TLS to register (CL)
    • Use g register on non-Linux OSes (see CL)
  • Reserve and use X15 as zero register (CL)
    • [ ] Should this be X0 for more compact instruction encoding?
  • Port to other platforms
    • amd64
    • arm64
    • mips
    • ppc
    • s390x
    • risc-v
    • [ ] porting guide
  • Eliminate legacy ABI paths for all platforms, even if they're using ABIInternal with 0 registers
  • Eliminate spill slots?
  • Pass pointer-shaped method receiver in context register (cmd/compile: pass pointer-shaped receivers using context register #44827)
  • runtime: simplify go/defer call paths
    • simplify go/defer paths to assume zero args/results
    • simplify special _defer allocator and remove special _defer cases from mallocgc (because _defer records will be fixed size).
  • cmd/vet: add syntax and checks for register arguments and results in assembly like x+0(FP)
  • cmd/vet: check argument size for ABIInternal functions and fix runtime asm declarations

Cleanup (can be done later)

  • runtime: port all assembly -> Go calls to ABIInternal?
  • Eliminate ABIAlias support from cmd/compile, cmd/link, and object format
  • remove now-redundant fields from ssa.AuxCall
  • rationalize use of LocalsOffset/FrameOffset (for registers that use a link register, e.g. arm64, powerppc).
  • cmd/compile: revisit abiutils.go and clean up the API (also reduce ABIConfig copying)
  • cmd/internal/obj: generate stack maps in obj so we can better compact the morestack/body maps and reduce subtlety?
  • cmd/compile,runtime: re-enable passing arguments to go in runtime (context)
  • cmd/compile: always attach ir.Func to function ir.Names (context)
  • cmd/compile: support ABIInternal for tail calls (context)
  • cmd/compile: once wrapping of defers is on for all arch/OS combinations, it should be possible to simplify or remove some of the openDefer processing in ssagen (notablly we could get rid of openDeferSave()
  • [ ] runtime: think about an ABI-insensitive debugCall Delve uses DWARF information
  • Fix up named slots & value association
  • fix firstpos in ssa.go – sometimes it's late.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Triage Backlog

    Status

    Accepted

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions