1
0
mirror of https://github.com/golang/go synced 2024-09-29 04:14:27 -06:00
go/test/codegen
Matthew Dempsky ab8d7dd75e cmd/compile: set LocalPkg.Path to -p flag
Since CL 391014, cmd/compile now requires the -p flag to be set the
build system. This CL changes it to initialize LocalPkg.Path to the
provided path, rather than relying on writing out `"".` into object
files and expecting cmd/link to substitute them.

However, this actually involved a rather long tail of fixes. Many have
already been submitted, but a few notable ones that have to land
simultaneously with changing LocalPkg:

1. When compiling package runtime, there are really two "runtime"
packages: types.LocalPkg (the source package itself) and
ir.Pkgs.Runtime (the compiler's internal representation, for synthetic
references). Previously, these ended up creating separate link
symbols (`"".xxx` and `runtime.xxx`, respectively), but now they both
end up as `runtime.xxx`, which causes lsym collisions (notably
inittask and funcsyms).

2. test/codegen tests need to be updated to expect symbols to be named
`command-line-arguments.xxx` rather than `"".foo`.

3. The issue20014 test case is sensitive to the sort order of field
tracking symbols. In particular, the local package now sorts to its
natural place in the list, rather than to the front.

Thanks to David Chase for helping track down all of the fixes needed
for this CL.

Updates #51734.

Change-Id: Iba3041cf7ad967d18c6e17922fa06ba11798b565
Reviewed-on: https://go-review.googlesource.com/c/go/+/393715
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2022-05-16 18:19:47 +00:00
..
addrcalc.go
alloc.go
arithmetic.go test/codegen: updated arithmetic tests to verify on ppc64,ppc64le 2021-11-01 13:12:37 +00:00
bitfield.go cmd/compile: merge ANDconst and UBFX into UBFX on arm64 2022-03-25 01:24:44 +00:00
bits.go cmd/compile: use ANDL for small immediates 2021-10-12 22:02:39 +00:00
bmi.go cmd/compile: add SHLX&SHRX without load 2022-04-13 17:48:36 +00:00
bool.go cmd/compile: use ANDL for small immediates 2021-10-12 22:02:39 +00:00
clobberdead.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00
clobberdeadreg.go test: make codegen tests work with both ABIs 2021-04-12 21:59:59 +00:00
compare_and_branch.go
comparisons.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00
condmove.go cmd/compile: add rewrite rules for conditional instructions on arm64 2021-03-18 01:46:58 +00:00
copy.go cmd/compile: add PPC64-specific inlining for runtime.memmove 2021-09-29 16:55:51 +00:00
floats.go cmd/compile: ARM64 optimize []float64 and []float32 access 2021-02-24 19:49:08 +00:00
fuse.go
issue22703.go
issue25378.go test: make codegen tests work with both ABIs 2021-04-12 21:59:59 +00:00
issue31618.go
issue33580.go
issue38554.go
issue42610.go
issue48054.go test/codegen: fix package name for test case 2021-09-08 14:51:40 +00:00
issue52635.go cmd/compile: support pointers to arrays in arrayClear 2022-05-03 05:42:48 +00:00
logic.go cmd/compile: combine OR + NOT into ORN on PPC64 2022-05-04 18:49:50 +00:00
mapaccess.go
maps.go cmd/compile: include all entries in map literal hint size 2022-03-01 23:20:30 +00:00
math.go cmd/compile: don't emit unnecessary amd64 extension checks 2021-10-05 18:32:12 +00:00
mathbits.go cmd/compile: lower Add64/Sub64 into ssa on PPC64 2022-05-10 20:03:53 +00:00
memcombine.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00
memops.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00
noextend.go test: update test/codegen/noextend.go to work with either ABI on ppc64x 2021-09-29 17:30:11 +00:00
race.go
README
regabi_regalloc.go cmd/compile: reduce redundant register moves for regabi calls 2021-04-19 16:21:39 +00:00
retpoline.go
rotate.go cmd/compile: allow rotates to be merged with logical ops on arm64 2021-09-20 16:25:36 +00:00
select.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00
shift.go cmd/compile: optimise immediate operands with constants on riscv64 2021-09-24 10:51:48 +00:00
shortcircuit.go
slices.go test: make codegen tests work with both ABIs 2021-04-12 21:59:59 +00:00
smallintiface.go
spectre.go [dev.regabi] cmd/compile: make ordering for InvertFlags more stable 2021-01-13 02:40:43 +00:00
stack.go test: make codegen tests work with both ABIs 2021-04-12 21:59:59 +00:00
strings.go cmd/compile: loads from readonly globals into const for mips64x 2021-03-16 09:07:17 +00:00
structs.go [dev.regabi] cmd/compile: reserve X15 as zero register on AMD64 2021-02-03 22:44:53 +00:00
switch.go cmd/compile: use jump table on ARM64 2022-05-13 19:51:03 +00:00
zerosize.go cmd/compile: set LocalPkg.Path to -p flag 2022-05-16 18:19:47 +00:00

// Copyright 2018 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.

The codegen directory contains code generation tests for the gc
compiler.


- Introduction

The test harness compiles Go code inside files in this directory and
matches the generated assembly (the output of `go tool compile -S`)
against a set of regexps to be specified in comments that follow a
special syntax (described below). The test driver is implemented as a
step of the top-level test/run.go suite, called "asmcheck".

The codegen harness is part of the all.bash test suite, but for
performance reasons only the codegen tests for the host machine's
GOARCH are enabled by default, and only on GOOS=linux.

To perform comprehensive tests for all the supported architectures
(even on a non-Linux system), one can run the following command

  $ ../bin/go run run.go -all_codegen -v codegen

in the top-level test directory. This is recommended after any change
that affect the compiler's code.

The test harness compiles the tests with the same go toolchain that is
used to run run.go. After writing tests for a newly added codegen
transformation, it can be useful to first run the test harness with a
toolchain from a released Go version (and verify that the new tests
fail), and then re-runnig the tests using the devel toolchain.


- Regexps comments syntax

Instructions to match are specified inside plain comments that start
with an architecture tag, followed by a colon and a quoted Go-style
regexp to be matched. For example, the following test:

  func Sqrt(x float64) float64 {
  	   // amd64:"SQRTSD"
  	   // arm64:"FSQRTD"
  	   return math.Sqrt(x)
  }

verifies that math.Sqrt calls are intrinsified to a SQRTSD instruction
on amd64, and to a FSQRTD instruction on arm64.

It is possible to put multiple architectures checks into the same
line, as:

  // amd64:"SQRTSD" arm64:"FSQRTD"

although this form should be avoided when doing so would make the
regexps line excessively long and difficult to read.

Comments that are on their own line will be matched against the first
subsequent non-comment line. Inline comments are also supported; the
regexp will be matched against the code found on the same line:

  func Sqrt(x float64) float64 {
  	   return math.Sqrt(x) // arm:"SQRTD"
  }

It's possible to specify a comma-separated list of regexps to be
matched. For example, the following test:

  func TZ8(n uint8) int {
  	   // amd64:"BSFQ","ORQ\t\\$256"
  	   return bits.TrailingZeros8(n)
  }

verifies that the code generated for a bits.TrailingZeros8 call on
amd64 contains both a "BSFQ" instruction and an "ORQ $256".

Note how the ORQ regex includes a tab char (\t). In the Go assembly
syntax, operands are separated from opcodes by a tabulation.

Regexps can be quoted using either " or `. Special characters must be
escaped accordingly. Both of these are accepted, and equivalent:

  // amd64:"ADDQ\t\\$3"
  // amd64:`ADDQ\t\$3`

and they'll match this assembly line:

  ADDQ	$3

Negative matches can be specified using a - before the quoted regexp.
For example:

  func MoveSmall() {
  	   x := [...]byte{1, 2, 3, 4, 5, 6, 7}
  	   copy(x[1:], x[:]) // arm64:-".*memmove"
  }

verifies that NO memmove call is present in the assembly generated for
the copy() line.


- Architecture specifiers

There are three different ways to specify on which architecture a test
should be run:

* Specify only the architecture (eg: "amd64"). This indicates that the
  check should be run on all the supported architecture variants. For
  instance, arm checks will be run against all supported GOARM
  variations (5,6,7).
* Specify both the architecture and a variant, separated by a slash
  (eg: "arm/7"). This means that the check will be run only on that
  specific variant.
* Specify the operating system, the architecture and the variant,
  separated by slashes (eg: "plan9/386/sse2", "plan9/amd64/"). This is
  needed in the rare case that you need to do a codegen test affected
  by a specific operating system; by default, tests are compiled only
  targeting linux.


- Remarks, and Caveats

-- Write small test functions

As a general guideline, test functions should be small, to avoid
possible interactions between unrelated lines of code that may be
introduced, for example, by the compiler's optimization passes.

Any given line of Go code could get assigned more instructions than it
may appear from reading the source. In particular, matching all MOV
instructions should be avoided; the compiler may add them for
unrelated reasons and this may render the test ineffective.

-- Line matching logic

Regexps are always matched from the start of the instructions line.
This means, for example, that the "MULQ" regexp is equivalent to
"^MULQ" (^ representing the start of the line), and it will NOT match
the following assembly line:

  IMULQ	$99, AX

To force a match at any point of the line, ".*MULQ" should be used.

For the same reason, a negative regexp like -"memmove" is not enough
to make sure that no memmove call is included in the assembly. A
memmove call looks like this:

  CALL	runtime.memmove(SB)

To make sure that the "memmove" symbol does not appear anywhere in the
assembly, the negative regexp to be used is -".*memmove".