go/test
thepudds ed24bb4e60 cmd/compile/internal/escape: propagate constants to interface conversions to avoid allocs
Currently, the integer value in the following interface conversion gets
heap allocated:

   v := 1000
   fmt.Println(v)

In contrast, this conversion does not currently cause the integer value
to be heap allocated:

   fmt.Println(1000)

The second example is able to avoid heap allocation because of an
optimization in walk (by Josh in #18704 and related issues) that
recognizes a literal is being used. In the first example, that
optimization is currently thwarted by the literal getting assigned
to a local variable prior to use in the interface conversion.

This CL propagates constants to interface conversions like
in the first example to avoid heap allocations, instead using
a read-only global. The net effect is roughly turning the first example
into the second.

One place this comes up in practice currently is with logging or
debug prints. For example, if we have something like:

   func conditionalDebugf(format string, args ...interface{}) {
   	if debugEnabled {
   		fmt.Fprintf(io.Discard, format, args...)
   	}
   }

Prior to this CL, this integer is heap allocated, even when the
debugEnabled flag is false, and even when the compiler
inlines conditionalDebugf:

   v := 1000
   conditionalDebugf("hello %d", v)

With this CL, the integer here is no longer heap allocated, even when
the debugEnabled flag is enabled, because the compiler can now see that
it can use a read-only global.

See the writeup in #71359 for more details.

CL 649076 (earlier in our stack) added most of the tests
along with debug diagnostics in convert.go to make it easier
to test this change.

Updates #71359
Updates #62653
Updates #53465
Updates #8618

Change-Id: I19a51e74b36576ebb0b9cf599267cbd2bd847ce4
Reviewed-on: https://go-review.googlesource.com/c/go/+/649079
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
2025-05-21 12:02:43 -07:00
..
abi
alias3.dir
arenas
asmhdr.dir
chan
closure3.dir cmd/compile: use very high budget for once-called closures 2024-11-22 02:04:41 +00:00
closure5.dir
codegen cmd/compile: add rules about ORN and ANDN 2025-05-21 08:28:37 -07:00
ddd2.dir
dwarf
fixedbugs cmd/compile/internal/escape: propagate constants to interface conversions to avoid allocs 2025-05-21 12:02:43 -07:00
import2.dir
import4.dir
interface
internal/runtime/sys runtime: move getcallersp to internal/runtime/sys 2024-09-17 17:01:20 +00:00
intrinsic.dir
ken
linkname.dir
linknameasm.dir
method4.dir
retjmp.dir
stress
syntax cmd/compile: better error message when offending/missing token is a keyword 2024-11-14 02:14:13 +00:00
typeparam
uintptrescapes.dir
wasmmemsize.dir test: add a test for wasm memory usage 2024-10-28 13:27:58 +00:00
64bit.go
235.go
README.md
alg.go
alias.go
alias1.go
alias2.go go/types, types2: qualify named types in error messages with type kind 2024-10-23 04:43:01 +00:00
alias3.go
align.go
append.go
append1.go
args.go
armimm.go
asmhdr.go
assign.go sync: add explicit noCopy fields to Map, Mutex, and Once 2024-11-18 18:52:54 +00:00
assign1.go
atomicload.go
bigalg.go
bigmap.go
blank.go
blank1.go
bom.go
bombad.go
bounds.go
cannotassign.go
chancap.go
chanlinear.go
char_lit.go
char_lit1.go
checkbce.go cmd/compile: rewrite the constant parts of the prove pass 2024-08-07 16:07:33 +00:00
clear.go
clearfat.go
closedchan.go
closure.go
closure1.go
closure2.go
closure3.go
closure4.go
closure5.go
closure6.go
closure7.go
cmp.go
cmp6.go
cmplx.go
cmplxdivide.c
cmplxdivide.go
cmplxdivide1.go
complit.go
complit1.go go/types, types2: report better error messages for slice expressions 2025-03-11 05:44:15 -07:00
compos.go
const.go
const1.go
const2.go
const3.go
const4.go
const5.go
const6.go
const7.go
const8.go
convT2X.go
convert.go
convert1.go
convert2.go
convert3.go
convert4.go
convinline.go
convlit.go
convlit1.go
copy.go
copy1.go go/types, types2: better error messages for copy built-in 2025-03-10 21:30:51 -07:00
crlf.go
ddd.go
ddd1.go
ddd2.go
decl.go
declbad.go
defer.go
defererrcheck.go test: add open-coded defer tests for too many exits path 2024-07-29 14:30:07 +00:00
deferfin.go
defernil.go
deferprint.go
deferprint.out
devirt.go
directive.go
directive2.go
divide.go
divmod.go
embedfunc.go
embedvers.go
empty.go
env.go
eof.go
eof1.go
escape.go
escape2.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
escape2n.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
escape3.go
escape4.go
escape5.go cmd/compile/internal/escape: propagate constants to interface conversions to avoid allocs 2025-05-21 12:02:43 -07:00
escape6.go cmd/compile: allow pointer-containing elements in stack allocations 2025-04-04 15:04:34 -07:00
escape_array.go cmd/compile: determine static values of len and cap in make() calls 2025-02-19 13:38:58 -08:00
escape_calls.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
escape_closure.go
escape_field.go
escape_goto.go
escape_hash_maphash.go
escape_iface.go cmd/compile/internal/escape: propagate constants to interface conversions to avoid allocs 2025-05-21 12:02:43 -07:00
escape_iface_data.go cmd/compile/internal/escape: propagate constants to interface conversions to avoid allocs 2025-05-21 12:02:43 -07:00
escape_indir.go
escape_level.go
escape_make_non_const.go cmd/compile: stack allocate variable-sized makeslice 2025-04-04 10:36:58 -07:00
escape_map.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
escape_mutations.go
escape_param.go
escape_reflect.go reflect: remove calling mapiterkey, mapiterelem 2024-09-18 20:57:20 +00:00
escape_runtime_atomic.go
escape_selfassign.go
escape_slice.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
escape_struct_param1.go
escape_struct_param2.go
escape_struct_return.go
escape_sync_atomic.go
escape_unsafe.go
fibo.go
finprofiled.go
float_lit.go
float_lit2.go
float_lit3.go
floatcmp.go
for.go
func.go
func1.go
func2.go
func3.go go/parser, syntax: better error message for parameter missing type 2024-09-30 22:04:40 +00:00
func4.go
func5.go
func6.go
func7.go
func8.go
funcdup.go
funcdup2.go
fuse.go cmd/compile: handle boolean and pointer relations 2024-08-07 16:07:55 +00:00
gc.go
gc1.go
gc2.go
gcgort.go
gcstring.go
goprint.go
goprint.out
goto.go
heapsampling.go
helloworld.go
helloworld.out
if.go
import.go
import1.go
import2.go
import4.go
import5.go
import6.go
index.go
index0.go
index1.go
index2.go
indirect.go
indirect1.go
init.go
init1.go
initcomma.go
initexp.go
initialize.go
initializerr.go
initloop.go go/types: improve recursive type error message 2024-10-22 22:20:29 +00:00
inline.go cmd/compile: remove no-longer-necessary recursive inlining checks 2025-03-06 10:07:17 -08:00
inline_big.go
inline_caller.go
inline_callers.go
inline_endian.go cmd/compile: allocate backing store for append on the stack 2025-05-19 16:14:53 -07:00
inline_literal.go
inline_math_bits_rotate.go
inline_sync.go sync: use atomic.Bool for Once.done 2025-04-22 08:28:13 -07:00
inline_testingbloop.go testing: detect a stopped timer in B.Loop 2025-03-24 11:41:09 -07:00
inline_variadic.go
int_lit.go
intcvt.go
intrinsic.go
intrinsic_atomic.go
iota.go
label.go
label1.go
linkmain.go
linkmain_run.go
linkname.go
linkname3.go
linknameasm.go
linkobj.go
linkx.go
linkx_run.go
literal.go
literal2.go
live.go runtime: rename mapiterinit and mapiternext 2025-01-28 10:54:43 -08:00
live1.go
live2.go cmd/compile,internal/runtime/maps: add extendible hashing 2024-10-21 14:16:20 +00:00
live_regabi.go runtime: rename mapiterinit and mapiternext 2025-01-28 10:54:43 -08:00
live_uintptrkeepalive.go
loopbce.go cmd/compile: remove no-longer-necessary recursive inlining checks 2025-03-06 10:07:17 -08:00
mainsig.go
makechan.go
makemap.go
makenew.go
makeslice.go
mallocfin.go
map.go
map1.go
mapclear.go
maplinear.go
maymorestack.go
mergemul.go
method.go
method1.go
method2.go
method3.go
method4.go
method5.go
method6.go
method7.go
named.go
named1.go
newinline.go cmd/compile: remove no-longer-necessary recursive inlining checks 2025-03-06 10:07:17 -08:00
nil.go
nilcheck.go
nilptr.go cmd/link: use >4GB base address for 64-bit PE binaries 2025-05-12 11:05:28 -07:00
nilptr2.go
nilptr3.go
nilptr4.go
nilptr5.go
nilptr5_aix.go
nilptr5_wasm.go
nilptr_aix.go
noinit.go
nosplit.go cmd/internal/objabi, internal/runtime: increase nosplit limit on OpenBSD 2024-12-06 00:35:41 +00:00
nowritebarrier.go
nul1.go
opt_branchlikely.go
parentype.go
peano.go
phiopt.go cmd/compile: add 2 phiopt cases 2025-05-08 10:18:37 -07:00
print.go
print.out
printbig.go
printbig.out
prove.go cmd/compile: use min & max builtins to assert constant bounds in prove's tests 2025-03-11 19:51:59 -07:00
prove_constant_folding.go cmd/compile: rewrite the constant parts of the prove pass 2024-08-07 16:07:33 +00:00
prove_invert_loop_with_unused_iterators.go
prove_popcount.go cmd/compile: compute bits.OnesCount's limits from argument's limits 2025-03-11 20:17:36 -07:00
range.go
range2.go
range3.go
range4.go
rangegen.go
recover.go
recover1.go
recover2.go
recover3.go
recover4.go
recover5.go
reflectmethod1.go
reflectmethod2.go
reflectmethod3.go
reflectmethod4.go
reflectmethod5.go
reflectmethod6.go
reflectmethod7.go
reflectmethod8.go
rename.go
rename1.go
reorder.go
reorder2.go
retjmp.go
return.go
rotate.go
rotate0.go
rotate1.go
rotate2.go
rotate3.go
rune.go
runtime.go
shift1.go
shift2.go
shift3.go
sieve.go
sigchld.go
sigchld.out
simassign.go
sizeof.go
slice3.go
slice3err.go
slicecap.go
sliceopt.go
solitaire.go
stack.go
stackobj.go
stackobj2.go
stackobj3.go
strcopy.go
strength.go
string_lit.go
stringrange.go
struct0.go
switch.go
switch2.go cmd/compile: better error message when offending/missing token is a keyword 2024-11-14 02:14:13 +00:00
switch3.go
switch4.go
switch5.go
switch6.go
switch7.go
tailcall.go cmd/compile: Enable inlining of tail calls 2025-03-11 14:18:43 -07:00
tighten.go cmd/compile: pair loads and stores on arm64 2025-02-13 14:07:47 -08:00
tinyfin.go
torture.go
turing.go
typecheck.go
typecheckloop.go
typeswitch.go
typeswitch1.go
typeswitch2.go
typeswitch2b.go
typeswitch3.go
uintptrescapes.go
uintptrescapes2.go
uintptrescapes3.go
uintptrkeepalive.go
undef.go
unsafe_slice_data.go
unsafe_string.go
unsafe_string_data.go
unsafebuiltins.go
used.go
utf.go
varerr.go
varinit.go
wasmexport.go cmd/compile: add basic wasmexport support 2024-08-09 20:07:54 +00:00
wasmexport2.go cmd/compile: allow more types for wasmimport/wasmexport parameters and results 2024-11-11 17:11:50 +00:00
wasmmemsize.go test: add a test for wasm memory usage 2024-10-28 13:27:58 +00:00
weak.go weak: prevent unsafe conversions using weak pointers 2025-02-06 13:16:59 -08:00
winbatch.go
writebarrier.go
zerodivide.go
zerosize.go

README.md

The test directory contains tests of the Go tool chain and runtime. It includes black box tests, regression tests, and error output tests. They are run as part of all.bash.

To run just these tests, execute:

../bin/go test cmd/internal/testdir

To run just tests from specified files in this directory, execute:

../bin/go test cmd/internal/testdir -run='Test/(file1.go|file2.go|...)'

Standard library tests should be written as regular Go tests in the appropriate package.

The tool chain and runtime also have regular Go tests in their packages. The main reasons to add a new test to this directory are:

  • it is most naturally expressed using the test runner; or
  • it is also applicable to gccgo and other Go tool chains.