-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_ops.go
More file actions
60 lines (49 loc) · 1.2 KB
/
binary_ops.go
File metadata and controls
60 lines (49 loc) · 1.2 KB
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
package main
import (
"fmt"
"os"
"os/exec"
"path"
"text/template"
"github.com/gomlx/stablehlo/internal/utils"
"github.com/gomlx/stablehlo/shapeinference"
)
const (
binaryOpsFile = "gen_binary_ops.go"
)
var (
binaryOpsTemplate = template.Must(
template.
New(binaryOpsFile).
Parse(
`/***** File generated by ./internal/cmd/ops_generator. Don't edit it directly. *****/
package stablehlo
import (
"github.com/gomlx/stablehlo/internal/optypes"
)
{{- range .}}
// {{.Name}} implements the corresponding standard binary operation.
func {{.Name}}(lhs, rhs *Value) (*Value, error) {
fn := lhs.fn
return fn.binaryOp(optypes.{{.Name}}, lhs, rhs)
}
{{- end}}
`))
)
type BinaryOp struct {
Name string
}
func GenerateBinaryOps() {
binaryOps := shapeinference.StandardBinaryOperations
data := make([]BinaryOp, 0, len(binaryOps))
for _, k := range utils.SortedKeys(binaryOps) {
data = append(data, BinaryOp{Name: k.String()})
}
fileName := binaryOpsFile
f := must1(os.Create(fileName))
must(binaryOpsTemplate.Execute(f, data))
must(f.Close())
cmd := exec.Command("gofmt", "-w", fileName)
must(cmd.Run())
fmt.Printf("✅ Successfully generated %s\n", path.Join(must1(os.Getwd()), fileName))
}