cmd/compile: add register abi tests

Change-Id: I4b2b62a8eb1c4bf47f552214127d4ed5710af196
Reviewed-on: https://go-review.googlesource.com/c/go/+/297030
Trust: David Chase <drchase@google.com>
Run-TryBot: David Chase <drchase@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
David Chase 2021-02-24 12:58:01 -05:00
parent 56d52e6611
commit 5c5552c5ba
13 changed files with 294 additions and 0 deletions

View file

@ -1,9 +1,15 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import "fmt"

View file

@ -7,6 +7,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (

View file

@ -0,0 +1,45 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (
"fmt"
)
var sink int = 3
//go:registerparams
//go:noinline
func F(a, b, c, d, e, f *int) {
G(f, e, d, c, b, a)
sink += *a // *a == 6 after swapping in G
}
//go:registerparams
//go:noinline
func G(a, b, c, d, e, f *int) {
var scratch [1000 * 100]int
scratch[*a] = *f // scratch[6] = 1
fmt.Println(*a, *b, *c, *d, *e, *f) // Forces it to spill b
sink = scratch[*b+1] // scratch[5+1] == 1
*f, *a = *a, *f
*e, *b = *b, *e
*d, *c = *c, *d
}
func main() {
a, b, c, d, e, f := 1, 2, 3, 4, 5, 6
F(&a, &b, &c, &d, &e, &f)
fmt.Println(a, b, c, d, e, f)
fmt.Println(sink)
}

View file

@ -0,0 +1,3 @@
6 5 4 3 2 1
6 5 4 3 2 1
7

View file

@ -0,0 +1,44 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (
"fmt"
)
var sink int
//go:registerparams
//go:noinline
func F(a, b, c, d, e, f, g, h, i, j, k, l, m *int) {
G(m, l, k, j, i, h, g, f, e, d, c, b, a)
// did the pointers get properly updated?
sink = *a + *m
}
//go:registerparams
//go:noinline
func G(a, b, c, d, e, f, g, h, i, j, k, l, m *int) {
// Do not reference the parameters
var scratch [1000 * 100]int
I := *c - *e - *l // zero.
scratch[I] = *d
fmt.Println("Got this far!")
sink += scratch[0]
}
func main() {
a, b, c, d, e, f, g, h, i, j, k, l, m := 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
F(&a, &b, &c, &d, &e, &f, &g, &h, &i, &j, &k, &l, &m)
fmt.Printf("Sink = %d\n", sink-7)
}

View file

@ -0,0 +1,2 @@
Got this far!
Sink = 7

View file

@ -0,0 +1,94 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (
"fmt"
)
var sink *string
var y int
//go:registerparams
//go:noinline
func F(a, b, c *int) (x int) {
x = *a
G(&x)
x += *b
G(&x)
x += *c
G(&x)
return
}
//go:registerparams
//go:noinline
func G(x *int) {
y += *x
fmt.Println("y = ", y)
}
//go:registerparams
//go:noinline
func X() {
*sink += " !!!!!!!!!!!!!!!"
}
//go:registerparams
//go:noinline
func H(s, t string) (result string) { // result leaks to heap
result = "Aloha! " + s + " " + t
sink = &result
r := ""
if len(s) <= len(t) {
r = "OKAY! "
X()
}
return r + result
}
//go:registerparams
//go:noinline
func K(s, t string) (result string) { // result spills
result = "Aloha! " + s + " " + t
r := ""
if len(s) <= len(t) {
r = "OKAY! "
X()
}
return r + result
}
func main() {
a, b, c := 1, 4, 16
x := F(&a, &b, &c)
fmt.Printf("x = %d\n", x)
y := H("Hello", "World!")
fmt.Println("len(y) =", len(y))
fmt.Println("y =", y)
z := H("Hello", "Pal!")
fmt.Println("len(z) =", len(z))
fmt.Println("z =", z)
fmt.Println()
y = K("Hello", "World!")
fmt.Println("len(y) =", len(y))
fmt.Println("y =", y)
z = K("Hello", "Pal!")
fmt.Println("len(z) =", len(z))
fmt.Println("z =", z)
}

View file

@ -0,0 +1,13 @@
y = 1
y = 6
y = 27
x = 21
len(y) = 41
y = OKAY! Aloha! Hello World! !!!!!!!!!!!!!!!
len(z) = 17
z = Aloha! Hello Pal!
len(y) = 25
y = OKAY! Aloha! Hello World!
len(z) = 17
z = Aloha! Hello Pal!

38
test/abi/return_stuff.go Normal file
View file

@ -0,0 +1,38 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (
"fmt"
)
//go:registerparams
//go:noinline
func F(a, b, c *int) int {
return *a + *b + *c
}
//go:registerparams
//go:noinline
func H(s, t string) string {
return s + " " + t
}
func main() {
a, b, c := 1, 4, 16
x := F(&a, &b, &c)
fmt.Printf("x = %d\n", x)
y := H("Hello", "World!")
fmt.Println("len(y) =", len(y))
fmt.Println("y =", y)
}

View file

@ -0,0 +1,3 @@
x = 21
len(y) = 12
y = Hello World!

View file

@ -0,0 +1,40 @@
// run
//go:build !wasm
// +build !wasm
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import (
"fmt"
)
var sink *string
type toobig struct {
a, b, c string
}
//go:registerparams
//go:noinline
func H(x toobig) string {
return x.a + " " + x.b + " " + x.c
}
func main() {
s := H(toobig{"Hello", "there,", "World"})
gotVsWant(s, "Hello there, World")
}
func gotVsWant(got, want string) {
if got != want {
fmt.Printf("FAIL, got %s, wanted %s\n", got, want)
}
}

View file

View file

@ -7,6 +7,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// wasm is excluded because the compiler chatter about register abi pragma ends up
// on stdout, and causes the expected output to not match.
package main
import "fmt"