Skip to content

Commit 6f5d774

Browse files
committed
cmd/compile: add 0-sized-value simplification to copyelim
The problem was caused by faulty handling of unSSA-able operations on zero-sized data in expand calls, but there is no point to operations on zero-sized data. This CL adds a simplify step to the first place in SSA where all values are processed and replaces anything producing a 0-sized struct/array with the corresponding Struct/Array Make0 operation (of the appropriate type). I attempted not generating them in ssagen, but that was a larger change, and also had bugs. This is simple and obvious. The only question is whether it would be worthwhile to do it earlier (in numberlines or phielem). Fixes #65808. Change-Id: I0a596b3d272798015e7bb6b1a20411241759fe0e Reviewed-on: https://go-review.googlesource.com/c/go/+/568258 Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent b5a64ba commit 6f5d774

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/cmd/compile/internal/ssa/copyelim.go

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ func copyelim(f *Func) {
1111
// of OpCopy) is a copy.
1212
for _, b := range f.Blocks {
1313
for _, v := range b.Values {
14+
15+
// This is an early place in SSA where all values are examined.
16+
// Rewrite all 0-sized Go values to remove accessors, dereferences, loads, etc.
17+
if t := v.Type; (t.IsStruct() || t.IsArray()) && t.Size() == 0 {
18+
if t.IsStruct() {
19+
v.reset(OpStructMake0)
20+
} else {
21+
v.reset(OpArrayMake0)
22+
}
23+
}
24+
1425
copyelimValue(v)
1526
}
1627
}

test/fixedbugs/issue65808.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// compile
2+
3+
// Copyright 2024 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.package main
6+
7+
package main
8+
9+
type Stringer interface {
10+
String() string
11+
}
12+
13+
type (
14+
stringer struct{}
15+
stringers [2]stringer
16+
foo struct {
17+
stringers
18+
}
19+
)
20+
21+
func (stringer) String() string { return "" }
22+
func toString(s Stringer) string { return s.String() }
23+
24+
func (v stringers) toStrings() []string {
25+
return []string{toString(v[0]), toString(v[1])}
26+
}
27+
28+
func main() {
29+
_ = stringers{}
30+
}

0 commit comments

Comments
 (0)