]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile, runtime: use ABI-aware function converting float to interface
authorCherry Zhang <cherryyz@google.com>
Sun, 4 Apr 2021 00:49:03 +0000 (20:49 -0400)
committerCherry Zhang <cherryyz@google.com>
Mon, 5 Apr 2021 18:22:47 +0000 (18:22 +0000)
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>
src/cmd/compile/internal/typecheck/builtin.go
src/cmd/compile/internal/typecheck/builtin/runtime.go
src/cmd/compile/internal/walk/convert.go
src/runtime/iface.go

index 3421c445888836530e612a0082b7bf3ba33420ce..225dd682f03a5356e380c6b9f9ebbb3625a0aee5 100644 (file)
@@ -73,7 +73,9 @@ var runtimeDecls = [...]struct {
        {"convI2I", funcTag, 57},
        {"convT16", funcTag, 58},
        {"convT32", funcTag, 58},
+       {"convT32F", funcTag, 58},
        {"convT64", funcTag, 58},
+       {"convT64F", funcTag, 58},
        {"convTstring", funcTag, 58},
        {"convTslice", funcTag, 58},
        {"convT2E", funcTag, 59},
index 614bd46177f671150f86a681317cb7356d8d94f2..7c9599b54e8170cc1d8fb23ce1966c3c622c6cbc 100644 (file)
@@ -89,7 +89,9 @@ func convI2I(typ *byte, elem any) (ret any)
 // 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 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 convT64F(val any) unsafe.Pointer    // val must be float64-like
 func convTstring(val any) unsafe.Pointer // val must be a string
 func convTslice(val any) unsafe.Pointer  // val must be a slice
 
index fa8e2c0bb8db485d88d280e1f9b1c9eb43facfd4..168f42ee485936eb5b1dc27a91415fec55dbef87 100644 (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.
 // Not all names are possible. For example, we never generate convE2E or convE2I.
 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()
        switch from.Tie() {
        case 'I':
@@ -307,8 +326,12 @@ func convFuncName(from, to *types.Type) (fnname string, needsaddr bool) {
                switch {
                case from.Size() == 2 && from.Align == 2:
                        return "convT16", false
+               case from.Size() == 4 && isFloatLike(from):
+                       return "convT32F", false
                case from.Size() == 4 && from.Align == 4 && !from.HasPointers():
                        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():
                        return "convT64", false
                }
index cd5fead9990cbfe2c98cfd291620c16b14f59589..f5ac627d39a30dd5da17c59a7b6c3b8fe608ab1d 100644 (file)
@@ -357,6 +357,10 @@ func convT32(val uint32) (x unsafe.Pointer) {
        return
 }
 
+func convT32F(val float32) (x unsafe.Pointer) {
+       return convT32(*(*uint32)(unsafe.Pointer(&val)))
+}
+
 func convT64(val uint64) (x unsafe.Pointer) {
        if val < uint64(len(staticuint64s)) {
                x = unsafe.Pointer(&staticuint64s[val])
@@ -367,6 +371,10 @@ func convT64(val uint64) (x unsafe.Pointer) {
        return
 }
 
+func convT64F(val float64) (x unsafe.Pointer) {
+       return convT64(*(*uint64)(unsafe.Pointer(&val)))
+}
+
 func convTstring(val string) (x unsafe.Pointer) {
        if val == "" {
                x = unsafe.Pointer(&zeroVal[0])