|
| 1 | +// Copyright 2025 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | +) |
| 10 | + |
| 11 | +const simdIntrinsicsTmpl = `// Code generated by x/arch/internal/simdgen using 'go run . -xedPath $XED_PATH -o godefs -goroot $GOROOT go.yaml types.yaml categories.yaml'; DO NOT EDIT. |
| 12 | +package ssagen |
| 13 | +
|
| 14 | +import ( |
| 15 | + "cmd/compile/internal/ir" |
| 16 | + "cmd/compile/internal/ssa" |
| 17 | + "cmd/compile/internal/types" |
| 18 | + "cmd/internal/sys" |
| 19 | +) |
| 20 | +
|
| 21 | +func simdIntrinsics(addF func(pkg, fn string, b intrinsicBuilder, archFamilies ...sys.ArchFamily)) { |
| 22 | +{{- range .OpsLen1}} |
| 23 | + addF("internal/simd", "{{(index .In 0).Go}}.{{.Go}}", opLen1(ssa.Op{{.Go}}{{(index .In 0).Go}}, {{.GoArch}}), sys.AMD64) |
| 24 | +{{- end}} |
| 25 | +{{- range .OpsLen2}} |
| 26 | + addF("internal/simd", "{{(index .In 0).Go}}.{{.Go}}", opLen2(ssa.Op{{.Go}}{{(index .In 0).Go}}, {{.GoArch}}), sys.AMD64) |
| 27 | +{{- end}} |
| 28 | +{{- range .OpsLen3}} |
| 29 | + addF("internal/simd", "{{(index .In 0).Go}}.{{.Go}}", opLen3(ssa.Op{{.Go}}{{(index .In 0).Go}}, {{.GoArch}}), sys.AMD64) |
| 30 | +{{- end}} |
| 31 | +
|
| 32 | +{{- range .VectorConversions }} |
| 33 | + addF("internal/simd", "{{.Tsrc.Name}}.As{{.Tdst.Name}}", func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { return args[0] }, sys.AMD64) |
| 34 | +{{- end}} |
| 35 | +
|
| 36 | +{{- range $size, $ts := .TypeMap }} |
| 37 | +{{- range $t := $ts }} |
| 38 | + addF("internal/simd", "Load{{$t.Name}}", simdLoad(), sys.AMD64) |
| 39 | + addF("internal/simd", "{{$t.Name}}.Store", simdStore(), sys.AMD64) |
| 40 | +{{- end}} |
| 41 | +{{- end}} |
| 42 | +{{- range .Masks }} |
| 43 | + addF("internal/simd", "{{.Name}}.As{{.VectorCounterpart}}", func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { return args[0] }, sys.AMD64) |
| 44 | + addF("internal/simd", "{{.VectorCounterpart}}.As{{.Name}}", func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { return args[0] }, sys.AMD64) |
| 45 | + addF("internal/simd", "{{.Name}}.And", opLen2(ssa.OpAnd{{.ReshapedVectorWithAndOr}}, types.TypeVec{{.Size}}), sys.AMD64) |
| 46 | + addF("internal/simd", "{{.Name}}.Or", opLen2(ssa.OpOr{{.ReshapedVectorWithAndOr}}, types.TypeVec{{.Size}}), sys.AMD64) |
| 47 | +{{- end}} |
| 48 | +} |
| 49 | +
|
| 50 | +func opLen1(op ssa.Op, t *types.Type) func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 51 | + return func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 52 | + return s.newValue1(op, t, args[0]) |
| 53 | + } |
| 54 | +} |
| 55 | +
|
| 56 | +func opLen2(op ssa.Op, t *types.Type) func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 57 | + return func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 58 | + return s.newValue2(op, t, args[0], args[1]) |
| 59 | + } |
| 60 | +} |
| 61 | +
|
| 62 | +func opLen3(op ssa.Op, t *types.Type) func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 63 | + return func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 64 | + return s.newValue3(op, t, args[0], args[1], args[2]) |
| 65 | + } |
| 66 | +} |
| 67 | +
|
| 68 | +func simdLoad() func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 69 | + return func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 70 | + return s.newValue2(ssa.OpLoad, n.Type(), args[0], s.mem()) |
| 71 | + } |
| 72 | +} |
| 73 | +
|
| 74 | +func simdStore() func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 75 | + return func(s *state, n *ir.CallExpr, args []*ssa.Value) *ssa.Value { |
| 76 | + s.store(args[0].Type, args[1], args[0]) |
| 77 | + return nil |
| 78 | + } |
| 79 | +} |
| 80 | +` |
| 81 | + |
| 82 | +// writeSIMDIntrinsics generates the intrinsic mappings and writes it to simdintrinsics.go |
| 83 | +// within the specified directory. |
| 84 | +func writeSIMDIntrinsics(directory string, ops []Operation, typeMap simdTypeMap) error { |
| 85 | + file, t, err := openFileAndPrepareTemplate(directory, "src/cmd/compile/internal/ssagen/simdintrinsics.go", simdIntrinsicsTmpl) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + defer file.Close() |
| 90 | + opsLen1, opsLen2, opsLen3, err := genericOpsByLen(ops) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + type templateData struct { |
| 96 | + OpsLen1 []Operation |
| 97 | + OpsLen2 []Operation |
| 98 | + OpsLen3 []Operation |
| 99 | + TypeMap simdTypeMap |
| 100 | + VectorConversions []simdTypePair |
| 101 | + Masks []simdType |
| 102 | + } |
| 103 | + err = t.Execute(file, templateData{opsLen1, opsLen2, opsLen3, typeMap, vConvertFromTypeMap(typeMap), masksFromTypeMap(typeMap)}) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("failed to execute template: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + return nil |
| 109 | +} |
0 commit comments