Codegen test coverage

One of GoLang optimization that I named “cmd/compile: avoid mapaccess at m[k]=append(m[k]..”  (Issue 24364 and CL 100838) removes excessive runtime.mapaccess call. I provided usual tests and benchmarks that guarantee that the map still works as expected and performance has been improved. And I had to generate ASM code manually to make sure that CALL runtime.mapaccess is not present.

../bin/go tool compile -S map_append_test.go | grep mapaccess

I did it every time before publishing the new patch. Then I accidentally found that the optimization doesn’t work for specific case when key has int8 and complex128 type. I have realized that the check of ASM code must be automated and cover different cases.

Codegen run.go

So, I needed a way to test the generated ASM code. Hopefully GoLang repository already has this functionality

https://github.com/golang/go/blob/master/test/README.md

In my case, I have added the following test

func mapAppendAssignmentInt32() {
  m := make(map[int32][]int32, 0)
  var k int32 = 0
  // 386:-".*mapaccess"
  // amd64:-".*mapaccess"
  // arm:-".*mapaccess"
  // arm64:-".*mapaccess"
  m[k] = append(m[k], 1)
~/go/test$../bin/go run run.go -v codegen/mapaccess.go

ok codegen/mapaccess.go 0.179s

Symbol “-” in the expression 386:-“.*mapaccess” means a negative condition. Lets see how it detects an error (note k + 1 key that prevents optimization)

func mapAppendAssignmentInt32() {
  m := make(map[int32][]int32, 0)
  var k int32 = 0
  // 386:-".*mapaccess"
  // amd64:-".*mapaccess"
  // arm:-".*mapaccess"
  // arm64:-".*mapaccess"
  m[k] = append(m[k+1], 1)
~/go/test$../bin/go run run.go -v codegen/mapaccess.go

codegen/mapaccess.go:341: 386: wrong opcode found: "^.*mapaccess"
FAIL codegen/mapaccess.go 0.099s

GoLang’s build script all.bash will run it as well and report error if the check fails.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s