Skip to content

Commit

Permalink
[release-branch.go1.22] cmd/compile: fix copying SSA-able variables o…
Browse files Browse the repository at this point in the history
…ptimization

CL 541715 added an optimization to copy SSA-able variables.

When handling m[k] = append(m[k], ...) case, it uses ir.SameSafeExpr to
check that m[k] expressions are the same, then doing type assertion to
convert the map index to ir.IndexExpr node. However, this assertion is
not safe for m[k] expression in append(m[k], ...), since it may be
wrapped by ir.OCONVNOP node.

Fixing this by un-wrapping any ir.OCONVNOP before doing type assertion.

Fixes #66178

Change-Id: I9ff7165ab97bc7f88d0e9b7b31604da19a8ca206
Reviewed-on: https://go-review.googlesource.com/c/go/+/569716
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/569818
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
cuonglm authored and thanm committed Mar 26, 2024
1 parent 9508eae commit e71b0b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cmd/compile/internal/walk/order.go
Expand Up @@ -643,7 +643,12 @@ func (o *orderState) stmt(n ir.Node) {
indexLHS.Index = o.cheapExpr(indexLHS.Index)

call := n.Y.(*ir.CallExpr)
indexRHS := call.Args[0].(*ir.IndexExpr)
arg0 := call.Args[0]
// ir.SameSafeExpr skips OCONVNOPs, so we must do the same here (#66096).
for arg0.Op() == ir.OCONVNOP {
arg0 = arg0.(*ir.ConvExpr).X
}
indexRHS := arg0.(*ir.IndexExpr)
indexRHS.X = indexLHS.X
indexRHS.Index = indexLHS.Index

Expand Down
17 changes: 17 additions & 0 deletions test/fixedbugs/issue66096.go
@@ -0,0 +1,17 @@
// 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 p

type Message struct {
Header map[string][]string
}

func f() {
m := Message{Header: map[string][]string{}}
m.Header[""] = append([]string(m.Header[""]), "")
_ = m
}

0 comments on commit e71b0b1

Please sign in to comment.