]> Cypherpunks.ru repositories - gostls13.git/commit
cmd/compile: experimental loop iterator capture semantics change
authorDavid Chase <drchase@google.com>
Sun, 12 Jun 2022 19:33:57 +0000 (15:33 -0400)
committerDavid Chase <drchase@google.com>
Mon, 6 Mar 2023 18:34:24 +0000 (18:34 +0000)
commitc20d95916357acf141159d4efc8894bfc14a2cd6
tree664889f3e4201633c5c80b50ecefb312d2bb06d5
parentdbdb3359b5b7cf948d74ba0f95e992ee98933bc5
cmd/compile: experimental loop iterator capture semantics change

Adds:
GOEXPERIMENT=loopvar (expected way of invoking)
-d=loopvar={-1,0,1,2,11,12} (for per-package control and/or logging)
-d=loopvarhash=... (for hash debugging)

loopvar=11,12 are for testing, benchmarking, and debugging.

If enabled,for loops of the form `for x,y := range thing`, if x and/or
y are addressed or captured by a closure, are transformed by renaming
x/y to a temporary and prepending an assignment to the body of the
loop x := tmp_x.  This changes the loop semantics by making each
iteration's instance of x be distinct from the others (currently they
are all aliased, and when this matters, it is almost always a bug).

3-range with captured iteration variables are also transformed,
though it is a more complex transformation.

"Optimized" to do a simpler transformation for
3-clause for where the increment is empty.

(Prior optimization of address-taking under Return disabled, because
it was incorrect; returns can have loops for children.  Restored in
a later CL.)

Includes support for -d=loopvarhash=<binary string> intended for use
with hash search and GOCOMPILEDEBUG=loopvarhash=<binary string>
(use `gossahash -e loopvarhash command-that-fails`).

Minor feature upgrades to hash-triggered features; clients can specify
that file-position hashes use only the most-inline position, and/or that
they use only the basenames of source files (not the full directory path).
Most-inlined is the right choice for debugging loop-iteration change
once the semantics are linked to the package across inlining; basename-only
makes it tractable to write tests (which, otherwise, depend on the full
pathname of the source file and thus vary).

Updates #57969.

Change-Id: I180a51a3f8d4173f6210c861f10de23de8a1b1db
Reviewed-on: https://go-review.googlesource.com/c/go/+/411904
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
30 files changed:
src/cmd/compile/internal/base/debug.go
src/cmd/compile/internal/base/flag.go
src/cmd/compile/internal/base/hashdebug.go
src/cmd/compile/internal/escape/stmt.go
src/cmd/compile/internal/gc/main.go
src/cmd/compile/internal/ir/stmt.go
src/cmd/compile/internal/loopvar/loopvar.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/loopvar_test.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_complicated_esc_address.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_esc_address.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_esc_closure.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_esc_method.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_esc_minimal_closure.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/for_nested.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/inlines/a/a.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/inlines/b/b.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/inlines/c/c.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/inlines/main.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/range_esc_address.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/range_esc_closure.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/range_esc_method.go [new file with mode: 0644]
src/cmd/compile/internal/loopvar/testdata/range_esc_minimal_closure.go [new file with mode: 0644]
src/cmd/compile/internal/noder/reader.go
src/internal/goexperiment/exp_loopvar_off.go [new file with mode: 0644]
src/internal/goexperiment/exp_loopvar_on.go [new file with mode: 0644]
src/internal/goexperiment/flags.go
src/runtime/race/testdata/mop_test.go
test/closure2.go
test/escape.go
test/fixedbugs/issue13799.go