Skip to content

Commit

Permalink
[dev.unified] cmd/compile: plumb rtype through for OMAPLIT
Browse files Browse the repository at this point in the history
OMAPLIT gets lowered into a bunch of OINDEXMAP operations, which in
general may require a *runtime._type argument. This CL adds
CompLitExpr.RType, updates the GOEXPERIMENT=unified frontend to start
setting it, and updates walk to propagate it through to any generated
OINDEXMAP operations.

Change-Id: I278e7e8e615ea6d01f65a5eba6d6fc8e00045735
Reviewed-on: https://go-review.googlesource.com/c/go/+/413360
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
mdempsky committed Jun 23, 2022
1 parent 7368647 commit 3d432b6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ir/expr.go
Expand Up @@ -194,6 +194,7 @@ type CompLitExpr struct {
miniExpr
origNode
List Nodes // initialized values
RType Node `mknode:"-"` // *runtime._type for OMAPLIT map types
Prealloc *Name
// For OSLICELIT, Len is the backing array length.
// For OMAPLIT, Len is the number of entries that we've removed from List and
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/compile/internal/noder/reader.go
Expand Up @@ -1812,6 +1812,11 @@ func (r *reader) compLit() ir.Node {
}

lit := typecheck.Expr(ir.NewCompLitExpr(pos, ir.OCOMPLIT, typ, elems))
switch lit.Op() {
case ir.OMAPLIT:
lit := lit.(*ir.CompLitExpr)
lit.RType = reflectdata.TypePtrAt(pos, typ)
}
if typ0.IsPtr() {
lit = typecheck.Expr(typecheck.NodAddrAt(pos, lit))
lit.SetType(typ0)
Expand Down
8 changes: 2 additions & 6 deletions src/cmd/compile/internal/reflectdata/helpers.go
Expand Up @@ -160,9 +160,7 @@ func DeleteMapRType(pos src.XPos, n *ir.CallExpr) ir.Node {
// map type.
func IndexMapRType(pos src.XPos, n *ir.IndexExpr) ir.Node {
assertOp(n, ir.OINDEXMAP)
// TODO(mdempsky): Need to propagate RType from OMAPLIT nodes to
// emitted OINDEXMAP nodes.
if haveRType(n, n.RType, "RType", false) {
if haveRType(n, n.RType, "RType", true) {
return n.RType
}
return mapRType(pos, n.X.Type())
Expand All @@ -184,9 +182,7 @@ func MakeChanRType(pos src.XPos, n *ir.MakeExpr) ir.Node {
// representing that map type.
func MakeMapRType(pos src.XPos, n *ir.MakeExpr) ir.Node {
assertOp(n, ir.OMAKEMAP)
// TODO(mdempsky): Need to propagate RType from OMAPLIT nodes to
// emitted OMAKEMAP nodes.
if haveRType(n, n.RType, "RType", false) {
if haveRType(n, n.RType, "RType", true) {
return n.RType
}
return mapRType(pos, n.Type())
Expand Down
3 changes: 3 additions & 0 deletions src/cmd/compile/internal/walk/complit.go
Expand Up @@ -416,6 +416,7 @@ func maplit(n *ir.CompLitExpr, m ir.Node, init *ir.Nodes) {
// make the map var
args := []ir.Node{ir.TypeNode(n.Type()), ir.NewInt(n.Len + int64(len(n.List)))}
a := typecheck.Expr(ir.NewCallExpr(base.Pos, ir.OMAKE, nil, args)).(*ir.MakeExpr)
a.RType = n.RType
a.SetEsc(n.Esc())
appendWalkStmt(init, ir.NewAssignStmt(base.Pos, m, a))

Expand Down Expand Up @@ -471,6 +472,7 @@ func maplit(n *ir.CompLitExpr, m ir.Node, init *ir.Nodes) {
// typechecker rewrites OINDEX to OINDEXMAP
lhs := typecheck.AssignExpr(ir.NewIndexExpr(base.Pos, m, kidx)).(*ir.IndexExpr)
base.AssertfAt(lhs.Op() == ir.OINDEXMAP, lhs.Pos(), "want OINDEXMAP, have %+v", lhs)
lhs.RType = n.RType

zero := ir.NewAssignStmt(base.Pos, i, ir.NewInt(0))
cond := ir.NewBinaryExpr(base.Pos, ir.OLT, i, ir.NewInt(tk.NumElem()))
Expand Down Expand Up @@ -510,6 +512,7 @@ func maplit(n *ir.CompLitExpr, m ir.Node, init *ir.Nodes) {
// typechecker rewrites OINDEX to OINDEXMAP
lhs := typecheck.AssignExpr(ir.NewIndexExpr(base.Pos, m, tmpkey)).(*ir.IndexExpr)
base.AssertfAt(lhs.Op() == ir.OINDEXMAP, lhs.Pos(), "want OINDEXMAP, have %+v", lhs)
lhs.RType = n.RType

var a ir.Node = ir.NewAssignStmt(base.Pos, lhs, tmpelem)
a = typecheck.Stmt(a)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/compile/internal/walk/order.go
Expand Up @@ -1452,6 +1452,7 @@ func (o *orderState) expr1(n, lhs ir.Node) ir.Node {
for _, r := range dynamics {
lhs := typecheck.AssignExpr(ir.NewIndexExpr(base.Pos, m, r.Key)).(*ir.IndexExpr)
base.AssertfAt(lhs.Op() == ir.OINDEXMAP, lhs.Pos(), "want OINDEXMAP, have %+v", lhs)
lhs.RType = n.RType

as := ir.NewAssignStmt(base.Pos, lhs, r.Value)
typecheck.Stmt(as)
Expand Down

0 comments on commit 3d432b6

Please sign in to comment.