/
gopar.go
148 lines (132 loc) · 3.26 KB
/
gopar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
var verbose = flag.Bool("verbose", false, "Print verbose compiler output")
var leaveSource = flag.Bool("source", false, "Don't delete the transformed source")
func main() {
flag.Parse()
compilecmd := flag.Arg(0) // run|build|install
compilepkg := flag.Arg(1)
if compilepkg == "" {
panic(fmt.Errorf("Empty compile package"))
}
fmt.Println("GoPar Compiler")
dir, err := ioutil.TempDir(os.TempDir(), "gopar_")
if err != nil {
panic(err)
}
if *verbose {
fmt.Println(dir)
}
if !*leaveSource {
defer os.RemoveAll(dir)
}
project := NewProject(compilepkg)
var importGraph func(string) ([]string, error)
importGraph = func(pkgPath string) (result []string, err error) {
if exists := project.get(pkgPath); exists != nil {
return
}
err = project.load(pkgPath)
if err != nil {
return
}
if *verbose {
fmt.Println(pkgPath)
}
var packageImports []string
pkg := project.get(pkgPath)
if pkg == nil {
return
}
if *verbose {
fmt.Println("->", pkg.Imports())
}
for _, i := range pkg.Imports() {
packageImports, err = importGraph(i)
if err != nil {
return
}
result = append(result, packageImports...)
}
result = append(result, pkgPath)
return
}
// Generate an import dependency graph of all used packages
var importOrder []string
importOrder, err = importGraph(compilepkg)
if err != nil {
panic(err)
}
fmt.Println(importOrder)
mainPkg := project.get(compilepkg)
if mainPkg.name != "main" {
panic(fmt.Errorf("%s is not a main package", compilepkg))
}
AddAnalysisPasses := func(compiler *Compiler) {
compiler.AddPass(NewDefinedTypesPass())
compiler.AddPass(NewBasicBlockPass())
//compiler.AddPass(NewInvalidConstructPass())
compiler.AddPass(NewCallGraphPass())
// analysis starts
compiler.AddPass(NewAccessPass())
compiler.AddPass(NewAccessPassPropogate())
compiler.AddPass(NewAccessPassFuncPropogate())
compiler.AddPass(NewDependencyPass())
}
AddParallelizePasses := func(compiler *Compiler) {
compiler.AddPass(NewParallelizePass())
// modification starts
compiler.AddPass(NewInsertBlocksPass())
compiler.AddPass(NewWriteKernelsPass())
}
// analyze all of the used packages
var analyzed map[string]bool = make(map[string]bool)
compiler := NewCompiler(project)
AddAnalysisPasses(compiler)
for _, pkgPath := range importOrder {
if analyzed[pkgPath] {
continue
}
//showGraph(project, pkgPath)
pkg := project.get(pkgPath)
err = compiler.Run(pkg)
if err != nil {
panic(err)
}
analyzed[pkgPath] = true
}
// modify the main package
AddParallelizePasses(compiler)
if err = compiler.Run(mainPkg); err != nil {
panic(err)
}
if err = project.write(dir, mainPkg); err != nil {
panic(err)
}
goparPath, _ := exec.LookPath(os.Args[0])
goparRoot, _ := filepath.Abs(path.Clean(path.Dir(goparPath) + "/../"))
cmd := exec.Command("go", compilecmd, compilepkg)
env := os.Environ()
for i, _ := range env {
if strings.HasPrefix(env[i], "GOPATH=") {
env[i] = os.ExpandEnv(fmt.Sprintf("GOPATH=%s:%s:${GOPATH}", dir, goparRoot))
if *verbose {
fmt.Println(env[i])
}
}
}
cmd.Env = env
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}