Skip to content

Commit

Permalink
Merge branch 'main' into defers
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Sep 23, 2023
2 parents 49496c5 + c986eda commit 46b022e
Show file tree
Hide file tree
Showing 10 changed files with 1,098 additions and 47 deletions.
7 changes: 7 additions & 0 deletions compiler/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func colorFunctions(cg *callgraph.Graph, yieldInstances functionColors) (functio
type functionColors map[*ssa.Function]*types.Signature

func colorFunctions0(cg *callgraph.Graph, colors functionColors, fn *ssa.Function, color *types.Signature) error {
if origin := fn.Origin(); origin != nil && origin.Pkg != nil {
// Don't follow edges into and through the coroutine package.
if pkgPath := origin.Pkg.Pkg.Path(); pkgPath == coroutinePackage {
return nil
}
}

existing, ok := colors[fn]
if ok {
if !types.Identical(existing, color) {
Expand Down
45 changes: 32 additions & 13 deletions compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/callgraph/vta"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
Expand Down Expand Up @@ -109,28 +110,33 @@ func (c *compiler) compile(path string) error {
return fmt.Errorf("pattern more than one module (%s + %s)", moduleDir, p.Module.Dir)
}
}
flatpkgs := flattenPackages(pkgs)
for _, p := range flatpkgs {
for _, err := range p.Errors {
return err
err = nil
packages.Visit(pkgs, func(p *packages.Package) bool {
for _, e := range p.Errors {
err = e
break
}
return err == nil
}, nil)
if err != nil {
return err
}

log.Printf("building SSA program")
prog, _ := ssautil.Packages(pkgs, ssa.InstantiateGenerics|ssa.GlobalDebug)
prog, _ := ssautil.AllPackages(pkgs, ssa.InstantiateGenerics|ssa.GlobalDebug)
prog.Build()

log.Printf("building call graph")
cg := cha.CallGraph(prog)
cg := vta.CallGraph(ssautil.AllFunctions(prog), cha.CallGraph(prog))

log.Printf("finding generic yield instantiations")
var coroutinePkg *packages.Package
for _, p := range flatpkgs {
packages.Visit(pkgs, func(p *packages.Package) bool {
if p.PkgPath == coroutinePackage {
coroutinePkg = p
break
}
}
return coroutinePkg == nil
}, nil)
if coroutinePkg == nil {
log.Printf("%s not imported by the module. Nothing to do", coroutinePackage)
return nil
Expand All @@ -149,9 +155,10 @@ func (c *compiler) compile(path string) error {
return err
}
pkgsByTypes := map[*types.Package]*packages.Package{}
for _, p := range flatpkgs {
packages.Visit(pkgs, func(p *packages.Package) bool {
pkgsByTypes[p.Types] = p
}
return true
}, nil)
colorsByPkg := map[*packages.Package]functionColors{}
for fn, color := range colors {
if fn.Pkg == nil {
Expand All @@ -167,10 +174,22 @@ func (c *compiler) compile(path string) error {
pkgColors[fn] = color
}

for p, colors := range colorsByPkg {
var needVendoring []*packages.Package
for p := range colorsByPkg {
if p.Module == nil || p.Module.Dir != moduleDir {
return fmt.Errorf("not implemented: compilation for packages outside module (need to compile %s)", p.PkgPath)
needVendoring = append(needVendoring, p)
break
}
}
if len(needVendoring) > 0 {
log.Printf("vendoring packages")
newRoot := filepath.Join(moduleDir, "goroot")
if err := vendor(newRoot, needVendoring); err != nil {
return err
}
}

for p, colors := range colorsByPkg {
if err := c.compilePackage(p, colors, prog); err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions compiler/desugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,13 +706,20 @@ func (d *desugarer) decomposeExpression(expr ast.Expr, flags exprFlags) (ast.Exp
// Need to hoist the CallExpr out into a temporary variable in
// this case, so that the relative order of calls (and their
// prerequisites) is preserved.
queue[i] = decompose(e)
} else {
e.Fun = decompose(e.Fun)
for i, arg := range e.Args {
e.Args[i] = decompose(arg)
switch d.info.TypeOf(e).(type) {
case *types.Tuple:
// TODO: can't hoist like this when it's a function
// that returns multiple values
default:
queue[i] = decompose(e)
continue
}
}
e.Fun = decompose(e.Fun)
for i, arg := range e.Args {
e.Args[i] = decompose(arg)
}

case *ast.CompositeLit:
for i, elt := range e.Elts {
e.Elts[i] = decompose(elt)
Expand Down
20 changes: 20 additions & 0 deletions compiler/desugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,26 @@ _l0:
_v2 := c(_v3)
_v0 <- _v2
}
`,
},
{
name: "FIXME: don't hoist function calls when there are multiple results",
body: "a, b, c = d(e())",
info: func(stmts []ast.Stmt, info *types.Info) {
callExpr := stmts[0].(*ast.AssignStmt).Rhs[0]
info.Types[callExpr] = types.TypeAndValue{
Type: types.NewTuple(
types.NewVar(0, nil, "a", intType),
types.NewVar(0, nil, "b", intType),
types.NewVar(0, nil, "c", intType),
),
}
},
expect: `
{
_v0 := e()
a, b, c = d(_v0)
}
`,
},
} {
Expand Down
1 change: 1 addition & 0 deletions compiler/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.0
require (
github.com/google/go-cmp v0.5.9
github.com/stealthrocket/coroutine v0.0.0-20230906012022-7474cda88ddc
golang.org/x/sync v0.3.0
golang.org/x/tools v0.13.0
)

Expand Down
29 changes: 0 additions & 29 deletions compiler/package.go

This file was deleted.

1 change: 1 addition & 0 deletions compiler/testdata/http/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/goroot
Loading

0 comments on commit 46b022e

Please sign in to comment.