1
0
mirror of https://github.com/golang/go synced 2024-11-14 06:50:21 -07:00

cmd/compile, runtime: use ABI-aware function converting float to interface

Currently, when converting a float (say float64), we use convT64
function. In the runtime convT64 expects a uint64 argument. In
the compiler, convT64 is defined as taking an "any" argument (so
it works with also uint64-like types such as [1]uint64). The "any"
type is instantiated with the concrete type in walk. So the
backend will see instances such as convT64([1]uint64).

Currently, float64 is treated as uint64-like. So the backend will
see convT64(float64). With a memory-based calling convention this
is fine. With a register-based calling convention, however, it
will pass the argument in a floating point register, whereas the
runtime expects the argument in an integer register (as it is
declared as uint64).

To fix this, this CL introduces runtime functions convT32F and
convT64F. They behave the same as convT32/convT64, but with a
float argument. In the compiler, use convT32F/convT64F to convert
float-like type to interface.

With this, "GOEXPERIMENT=regabi,regabiargs go test math fmt"
works.

Updates #40724.

Change-Id: I8b2e232096a95e4a7c4ab81795d77ef224ffaab3
Reviewed-on: https://go-review.googlesource.com/c/go/+/307232
Trust: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Zhang 2021-04-03 20:49:03 -04:00
parent a040ebeb98
commit cf148f3d46
4 changed files with 35 additions and 0 deletions

View File

@ -73,7 +73,9 @@ var runtimeDecls = [...]struct {
{"convI2I", funcTag, 57}, {"convI2I", funcTag, 57},
{"convT16", funcTag, 58}, {"convT16", funcTag, 58},
{"convT32", funcTag, 58}, {"convT32", funcTag, 58},
{"convT32F", funcTag, 58},
{"convT64", funcTag, 58}, {"convT64", funcTag, 58},
{"convT64F", funcTag, 58},
{"convTstring", funcTag, 58}, {"convTstring", funcTag, 58},
{"convTslice", funcTag, 58}, {"convTslice", funcTag, 58},
{"convT2E", funcTag, 59}, {"convT2E", funcTag, 59},

View File

@ -89,7 +89,9 @@ func convI2I(typ *byte, elem any) (ret any)
// These return only a data pointer. // These return only a data pointer.
func convT16(val any) unsafe.Pointer // val must be uint16-like (same size and alignment as a uint16) func convT16(val any) unsafe.Pointer // val must be uint16-like (same size and alignment as a uint16)
func convT32(val any) unsafe.Pointer // val must be uint32-like (same size and alignment as a uint32) func convT32(val any) unsafe.Pointer // val must be uint32-like (same size and alignment as a uint32)
func convT32F(val any) unsafe.Pointer // val must be float32-like
func convT64(val any) unsafe.Pointer // val must be uint64-like (same size and alignment as a uint64 and contains no pointers) func convT64(val any) unsafe.Pointer // val must be uint64-like (same size and alignment as a uint64 and contains no pointers)
func convT64F(val any) unsafe.Pointer // val must be float64-like
func convTstring(val any) unsafe.Pointer // val must be a string func convTstring(val any) unsafe.Pointer // val must be a string
func convTslice(val any) unsafe.Pointer // val must be a slice func convTslice(val any) unsafe.Pointer // val must be a slice

View File

@ -297,6 +297,25 @@ func walkStringToRunes(n *ir.ConvExpr, init *ir.Nodes) ir.Node {
// It also reports whether the function expects the data by address. // It also reports whether the function expects the data by address.
// Not all names are possible. For example, we never generate convE2E or convE2I. // Not all names are possible. For example, we never generate convE2E or convE2I.
func convFuncName(from, to *types.Type) (fnname string, needsaddr bool) { func convFuncName(from, to *types.Type) (fnname string, needsaddr bool) {
// With register-based ABI, float32 and uint32 are passed in different
// registers, so we cannot use convT32 for float32.
// isFloatLike returns whether t is a float-like type (float32, float64,
// single-element array/struct with a float-like element), for which
// the argument is passed in a floating point register under register-
// based ABI.
var isFloatLike func(t *types.Type) bool
isFloatLike = func(t *types.Type) bool {
switch t.Kind() {
case types.TFLOAT32, types.TFLOAT64:
return true
case types.TARRAY:
return t.NumElem() == 1 && isFloatLike(t.Elem())
case types.TSTRUCT:
return t.NumFields() == 1 && isFloatLike(t.Field(0).Type)
}
return false
}
tkind := to.Tie() tkind := to.Tie()
switch from.Tie() { switch from.Tie() {
case 'I': case 'I':
@ -307,8 +326,12 @@ func convFuncName(from, to *types.Type) (fnname string, needsaddr bool) {
switch { switch {
case from.Size() == 2 && from.Align == 2: case from.Size() == 2 && from.Align == 2:
return "convT16", false return "convT16", false
case from.Size() == 4 && isFloatLike(from):
return "convT32F", false
case from.Size() == 4 && from.Align == 4 && !from.HasPointers(): case from.Size() == 4 && from.Align == 4 && !from.HasPointers():
return "convT32", false return "convT32", false
case from.Size() == 8 && isFloatLike(from):
return "convT64F", false
case from.Size() == 8 && from.Align == types.Types[types.TUINT64].Align && !from.HasPointers(): case from.Size() == 8 && from.Align == types.Types[types.TUINT64].Align && !from.HasPointers():
return "convT64", false return "convT64", false
} }

View File

@ -357,6 +357,10 @@ func convT32(val uint32) (x unsafe.Pointer) {
return return
} }
func convT32F(val float32) (x unsafe.Pointer) {
return convT32(*(*uint32)(unsafe.Pointer(&val)))
}
func convT64(val uint64) (x unsafe.Pointer) { func convT64(val uint64) (x unsafe.Pointer) {
if val < uint64(len(staticuint64s)) { if val < uint64(len(staticuint64s)) {
x = unsafe.Pointer(&staticuint64s[val]) x = unsafe.Pointer(&staticuint64s[val])
@ -367,6 +371,10 @@ func convT64(val uint64) (x unsafe.Pointer) {
return return
} }
func convT64F(val float64) (x unsafe.Pointer) {
return convT64(*(*uint64)(unsafe.Pointer(&val)))
}
func convTstring(val string) (x unsafe.Pointer) { func convTstring(val string) (x unsafe.Pointer) {
if val == "" { if val == "" {
x = unsafe.Pointer(&zeroVal[0]) x = unsafe.Pointer(&zeroVal[0])