forked from bjwbell/gensimd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensimd.go
207 lines (184 loc) · 5.29 KB
/
gensimd.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
//go:generate stringer -type=Instruction,InstrOpType,InstructionType,SimdInstr,XmmData codegen
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/bjwbell/gensimd/codegen"
"github.com/bjwbell/gensimd/simd"
"go/build"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
"golang.org/x/tools/go/types"
)
func filePath(pathName string) string {
split := strings.Split(pathName, "/")
dir := ""
if len(split) == 1 {
dir = "."
} else if len(split) == 2 {
dir = split[0] + "/"
} else {
dir = strings.Join(split[0:len(split)-2], "/")
}
return dir
}
func fileName(pathName string) string {
split := strings.Split(pathName, "/")
name := ""
if len(split) == 0 {
name = ""
} else {
name = split[len(split)-1]
}
return name
}
func main() {
var ssaDump = flag.Bool("ssa", false, "dump ssa representation")
var debug = flag.Bool("debug", false, "include debug comments in assembly")
var trace = flag.Bool("trace", false, "trace of assembly generation to stdout")
var printSpills = flag.Bool("spills", false, "print each register spill")
var disableOptimizations = flag.Bool("N", false, "disable optimizations")
var output = flag.String("o", "", "Go assembly output file")
var f = flag.String("f", "", "input file with function definitions")
var flagFn = flag.String("fn", "", "comma separated list of function names")
var flagOutFn = flag.String("outfn", "", "comma separated list of output function names")
var goprotofile = flag.String("goprotofile", "", "output file for SIMD function prototype(s)")
flag.Parse()
optimize := !*disableOptimizations
file := os.ExpandEnv("$GOFILE")
log.SetFlags(log.Lshortfile)
if *f != "" {
file = *f
}
if *flagFn == "" {
log.Fatalf("Error no function name(s) provided")
}
spills := os.ExpandEnv("$GENSIMDSPILLS")
if spills != "" {
*printSpills = spills == "1"
}
fnnames := strings.Split(*flagFn, ",")
outFns := []string{}
if *flagOutFn == "" {
for _, fn := range fnnames {
outFns = append(outFns, "gensimd_"+fn)
}
} else {
outFns = strings.Split(*flagOutFn, ",")
}
if len(fnnames) != len(outFns) {
log.Fatalf("Error # fns (%v) doesn't match # outfns (%v)\n", len(fnnames), len(outFns))
}
for i := range fnnames {
fnnames[i] = strings.TrimSpace(fnnames[i])
outFns[i] = strings.TrimSpace(outFns[i])
}
parsed, err := simd.ParseFile(file)
if err != nil {
msg := "Error parsing file \"%v\", error msg \"%v\"\n"
log.Fatalf(msg, file, err)
}
filePkgName := parsed.Pkg.Name()
filePkgPath := parsed.Pkg.Path()
conf := loader.Config{Build: &build.Default}
// Choose types.Sizes from conf.Build.
var wordSize int64 = 8
switch conf.Build.GOARCH {
case "386", "arm":
panic("SIMD invalid for x86 and arm")
}
conf.TypeChecker.Sizes = &types.StdSizes{
MaxAlign: 8,
WordSize: wordSize,
}
// Use the initial file from the command line/$GOFILE.
conf.CreateFromFilenames(filePath(file), file)
// Load, parse and type-check
iprog, err := conf.Load()
if err != nil {
log.Fatalf("conf.Load, error msg \"%v\"", err)
}
// Create and build SSA-form program representation.
builderMode := ssa.SanityCheckFunctions | ssa.GlobalDebug
if *ssaDump {
builderMode = ssa.PrintFunctions
}
prog := ssautil.CreateProgram(iprog, builderMode)
if prog == nil {
log.Fatalf("Couldn't create ssa representation")
}
// Build and display only the initial packages (and synthetic wrappers)
for _, info := range iprog.InitialPackages() {
prog.Package(info.Pkg).Build()
}
assembly := codegen.AssemblyFilePreamble()
goprotos := ""
protoPkgName := ""
protoImports := ""
foundpkg := false
for _, pkg := range prog.AllPackages() {
if pkg.Pkg.Path() == filePkgPath && pkg.Pkg.Name() == filePkgName {
foundpkg = true
for i := range fnnames {
fnname := fnnames[i]
outfn := outFns[i]
if fn := pkg.Func(fnname); fn == nil {
msg := "Func \"%v\" not found in package \"%v\""
log.Fatalf(msg, fnname, filePkgName)
} else {
dbg := *debug
fn, err := codegen.CreateFunction(fn, outfn, dbg, *trace, optimize)
fn.PrintSpills = *printSpills
if err != nil {
msg := "codegen error msg \"%v\""
log.Fatalf(msg, err)
}
if asm, err := fn.GoAssembly(); err != nil {
msg := "Error creating fn asm: \"%v\"\n"
msgp := "Error creating fn asm, %v, \"%v\"\n"
position := fn.Position(err.Pos)
if position.IsValid() {
log.Fatalf(msgp, position, err.Err)
} else {
log.Fatalf(msg, err.Err)
}
} else {
if *output == "" {
fmt.Println(asm)
} else {
if *goprotofile != "" {
pkg, imports, proto := fn.GoProto()
goprotos += proto
if protoPkgName == "" {
protoPkgName = pkg + "\n"
}
if protoImports == "" {
protoImports = imports
}
}
assembly += asm
}
}
}
}
}
}
if !foundpkg {
msg := "Error didn't find package, \"%v\"\n"
panic(fmt.Sprintf(msg, filePkgName))
}
writeFile(*output, assembly)
if *goprotofile != "" {
writeFile(*goprotofile, protoPkgName+"\n"+protoImports+"\n"+goprotos)
}
}
func writeFile(filename, contents string) {
if err := ioutil.WriteFile(filename, []byte(contents), 0644); err != nil {
log.Fatalf("Cannot write to file \"%v\", error \"%v\"\n", filename, err)
}
}