Skip to content

Commit

Permalink
cmd/compile: add 0-sized-value simplification to copyelim
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dr2chase committed Mar 2, 2024
1 parent b5a64ba commit 6f5d774
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/cmd/compile/internal/ssa/copyelim.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ func copyelim(f *Func) {
// of OpCopy) is a copy.
for _, b := range f.Blocks {
for _, v := range b.Values {

// This is an early place in SSA where all values are examined.
// Rewrite all 0-sized Go values to remove accessors, dereferences, loads, etc.
if t := v.Type; (t.IsStruct() || t.IsArray()) && t.Size() == 0 {
if t.IsStruct() {
v.reset(OpStructMake0)
} else {
v.reset(OpArrayMake0)
}
}

copyelimValue(v)
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/fixedbugs/issue65808.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// compile

// Copyright 2024 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.package main

package main

type Stringer interface {
String() string
}

type (
stringer struct{}
stringers [2]stringer
foo struct {
stringers
}
)

func (stringer) String() string { return "" }
func toString(s Stringer) string { return s.String() }

func (v stringers) toStrings() []string {
return []string{toString(v[0]), toString(v[1])}
}

func main() {
_ = stringers{}
}

0 comments on commit 6f5d774

Please sign in to comment.