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>
diff --git a/src/cmd/compile/internal/typecheck/builtin.go b/src/cmd/compile/internal/typecheck/builtin.go
index 3421c44..225dd68 100644
--- a/src/cmd/compile/internal/typecheck/builtin.go
+++ b/src/cmd/compile/internal/typecheck/builtin.go
@@ -73,7 +73,9 @@
 	{"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},
diff --git a/src/cmd/compile/internal/typecheck/builtin/runtime.go b/src/cmd/compile/internal/typecheck/builtin/runtime.go
index 614bd46..7c9599b 100644
--- a/src/cmd/compile/internal/typecheck/builtin/runtime.go
+++ b/src/cmd/compile/internal/typecheck/builtin/runtime.go
@@ -89,7 +89,9 @@
 // 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
 
diff --git a/src/cmd/compile/internal/walk/convert.go b/src/cmd/compile/internal/walk/convert.go
index fa8e2c0..168f42e 100644
--- a/src/cmd/compile/internal/walk/convert.go
+++ b/src/cmd/compile/internal/walk/convert.go
@@ -297,6 +297,25 @@
 // 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 @@
 		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
 		}
diff --git a/src/runtime/iface.go b/src/runtime/iface.go
index cd5fead..f5ac627 100644
--- a/src/runtime/iface.go
+++ b/src/runtime/iface.go
@@ -357,6 +357,10 @@
 	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 @@
 	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])