| |
| |
| |
|
|
| package main |
|
|
| import ( |
| "fmt" |
| "log" |
| "regexp" |
| "slices" |
| "strconv" |
| "strings" |
| "unicode" |
|
|
| "simd/archsimd/_gen/unify" |
| ) |
|
|
| type Operation struct { |
| rawOperation |
|
|
| |
| |
| |
| |
| Go string |
|
|
| |
| |
| |
| |
| |
| |
| |
| Documentation string |
|
|
| |
| |
| |
| In []Operand |
| } |
|
|
| |
| |
| type rawOperation struct { |
| Go string |
|
|
| GoArch string |
| Asm string |
| OperandOrder *string |
| |
| |
| SpecialLower *string |
|
|
| In []Operand |
| InVariant []Operand |
| Out []Operand |
| MemFeatures *string |
| MemFeaturesData *string |
| Commutative bool |
| CPUFeature string |
| Zeroing *bool |
| Documentation *string |
| AddDoc *string |
| |
| |
| ConstImm *string |
| |
| NameAndSizeCheck *bool |
| |
| NoTypes *string |
| |
| NoGenericOps *string |
| |
| SSAVariant *string |
| |
| |
| HideMaskMethods *bool |
| } |
|
|
| func (o *Operation) IsMasked() bool { |
| if len(o.InVariant) == 0 { |
| return false |
| } |
| if len(o.InVariant) == 1 && o.InVariant[0].Class == "mask" { |
| return true |
| } |
| panic(fmt.Errorf("unknown inVariant")) |
| } |
|
|
| func (o *Operation) SkipMaskedMethod() bool { |
| if o.HideMaskMethods == nil { |
| return false |
| } |
| if *o.HideMaskMethods && o.IsMasked() { |
| return true |
| } |
| return false |
| } |
|
|
| var reForName = regexp.MustCompile(`\bNAME\b`) |
|
|
| func (o *Operation) DecodeUnified(v *unify.Value) error { |
| if err := v.Decode(&o.rawOperation); err != nil { |
| return err |
| } |
|
|
| isMasked := o.IsMasked() |
|
|
| |
| o.Go = o.rawOperation.Go |
| if isMasked { |
| o.Go += "Masked" |
| } |
|
|
| |
| if o.rawOperation.Documentation != nil { |
| o.Documentation = *o.rawOperation.Documentation |
| } else { |
| o.Documentation = "// UNDOCUMENTED" |
| } |
| o.Documentation = reForName.ReplaceAllString(o.Documentation, o.Go) |
| if isMasked { |
| o.Documentation += "\n//\n// This operation is applied selectively under a write mask." |
| |
| if unicode.IsUpper([]rune(o.Go)[0]) { |
| trueVal := "true" |
| o.NoGenericOps = &trueVal |
| o.NoTypes = &trueVal |
| } |
| } |
| if o.rawOperation.AddDoc != nil { |
| o.Documentation += "\n" + reForName.ReplaceAllString(*o.rawOperation.AddDoc, o.Go) |
| } |
|
|
| o.In = append(o.rawOperation.In, o.rawOperation.InVariant...) |
|
|
| |
| |
| if len(o.In) > 0 && len(o.Out) > 0 { |
| inLanes := o.In[0].Lanes |
| outLanes := o.Out[0].Lanes |
| if inLanes != nil && outLanes != nil && *inLanes < *outLanes { |
| if (strings.Contains(o.Go, "Saturate") || strings.Contains(o.Go, "Truncate")) && |
| !strings.Contains(o.Go, "Concat") { |
| o.Documentation += "\n// Results are packed to low elements in the returned vector, its upper elements are zeroed." |
| } |
| } |
| } |
|
|
| return nil |
| } |
|
|
| func (o *Operation) VectorWidth() int { |
| out := o.Out[0] |
| if out.Class == "vreg" { |
| return *out.Bits |
| } else if out.Class == "greg" || out.Class == "mask" { |
| for i := range o.In { |
| if o.In[i].Class == "vreg" { |
| return *o.In[i].Bits |
| } |
| } |
| } |
| panic(fmt.Errorf("Figure out what the vector width is for %v and implement it", *o)) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| var demotingConvertOps = map[string]bool{ |
| "VPMOVQD128": true, "VPMOVSQD128": true, "VPMOVUSQD128": true, "VPMOVQW128": true, "VPMOVSQW128": true, |
| "VPMOVUSQW128": true, "VPMOVDW128": true, "VPMOVSDW128": true, "VPMOVUSDW128": true, "VPMOVQB128": true, |
| "VPMOVSQB128": true, "VPMOVUSQB128": true, "VPMOVDB128": true, "VPMOVSDB128": true, "VPMOVUSDB128": true, |
| "VPMOVWB128": true, "VPMOVSWB128": true, "VPMOVUSWB128": true, |
| "VPMOVQDMasked128": true, "VPMOVSQDMasked128": true, "VPMOVUSQDMasked128": true, "VPMOVQWMasked128": true, "VPMOVSQWMasked128": true, |
| "VPMOVUSQWMasked128": true, "VPMOVDWMasked128": true, "VPMOVSDWMasked128": true, "VPMOVUSDWMasked128": true, "VPMOVQBMasked128": true, |
| "VPMOVSQBMasked128": true, "VPMOVUSQBMasked128": true, "VPMOVDBMasked128": true, "VPMOVSDBMasked128": true, "VPMOVUSDBMasked128": true, |
| "VPMOVWBMasked128": true, "VPMOVSWBMasked128": true, "VPMOVUSWBMasked128": true, |
| } |
|
|
| func machineOpName(maskType maskShape, gOp Operation) string { |
| asm := gOp.Asm |
| if maskType == OneMask { |
| asm += "Masked" |
| } |
| asm = fmt.Sprintf("%s%d", asm, gOp.VectorWidth()) |
| if gOp.SSAVariant != nil { |
| asm += *gOp.SSAVariant |
| } |
| if demotingConvertOps[asm] { |
| |
| |
| asm = fmt.Sprintf("%s_%d", asm, *gOp.In[0].Bits) |
| } |
| return asm |
| } |
|
|
| func compareStringPointers(x, y *string) int { |
| if x != nil && y != nil { |
| return compareNatural(*x, *y) |
| } |
| if x == nil && y == nil { |
| return 0 |
| } |
| if x == nil { |
| return -1 |
| } |
| return 1 |
| } |
|
|
| func compareIntPointers(x, y *int) int { |
| if x != nil && y != nil { |
| return *x - *y |
| } |
| if x == nil && y == nil { |
| return 0 |
| } |
| if x == nil { |
| return -1 |
| } |
| return 1 |
| } |
|
|
| func compareOperations(x, y Operation) int { |
| if c := compareNatural(x.Go, y.Go); c != 0 { |
| return c |
| } |
| xIn, yIn := x.In, y.In |
|
|
| if len(xIn) > len(yIn) && xIn[len(xIn)-1].Class == "mask" { |
| xIn = xIn[:len(xIn)-1] |
| } else if len(xIn) < len(yIn) && yIn[len(yIn)-1].Class == "mask" { |
| yIn = yIn[:len(yIn)-1] |
| } |
|
|
| if len(xIn) < len(yIn) { |
| return -1 |
| } |
| if len(xIn) > len(yIn) { |
| return 1 |
| } |
| if len(x.Out) < len(y.Out) { |
| return -1 |
| } |
| if len(x.Out) > len(y.Out) { |
| return 1 |
| } |
| for i := range xIn { |
| ox, oy := &xIn[i], &yIn[i] |
| if c := compareOperands(ox, oy); c != 0 { |
| return c |
| } |
| } |
| return 0 |
| } |
|
|
| func compareOperands(x, y *Operand) int { |
| if c := compareNatural(x.Class, y.Class); c != 0 { |
| return c |
| } |
| if x.Class == "immediate" { |
| return compareStringPointers(x.ImmOffset, y.ImmOffset) |
| } else { |
| if c := compareStringPointers(x.Base, y.Base); c != 0 { |
| return c |
| } |
| if c := compareIntPointers(x.ElemBits, y.ElemBits); c != 0 { |
| return c |
| } |
| if c := compareIntPointers(x.Bits, y.Bits); c != 0 { |
| return c |
| } |
| return 0 |
| } |
| } |
|
|
| type Operand struct { |
| Class string |
|
|
| Go *string |
| AsmPos int |
|
|
| Base *string |
| ElemBits *int |
| Bits *int |
|
|
| Const *string |
| |
| |
| |
| |
| ImmOffset *string |
| Name *string |
| Lanes *int |
| |
| |
| |
| TreatLikeAScalarOfSize *int |
| |
| |
| OverwriteClass *string |
| |
| |
| OverwriteBase *string |
| |
| |
| |
| OverwriteElementBits *int |
| |
| FixedReg *string |
| } |
|
|
| |
| func isDigit(b byte) bool { |
| return b >= '0' && b <= '9' |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func compareNatural(s1, s2 string) int { |
| i, j := 0, 0 |
| len1, len2 := len(s1), len(s2) |
|
|
| for i < len1 && j < len2 { |
| |
| if isDigit(s1[i]) && isDigit(s2[j]) { |
| |
| numStart1 := i |
| for i < len1 && isDigit(s1[i]) { |
| i++ |
| } |
| num1, _ := strconv.Atoi(s1[numStart1:i]) |
|
|
| numStart2 := j |
| for j < len2 && isDigit(s2[j]) { |
| j++ |
| } |
| num2, _ := strconv.Atoi(s2[numStart2:j]) |
|
|
| if num1 < num2 { |
| return -1 |
| } |
| if num1 > num2 { |
| return 1 |
| } |
| |
| } else { |
| |
| if s1[i] < s2[j] { |
| return -1 |
| } |
| if s1[i] > s2[j] { |
| return 1 |
| } |
| i++ |
| j++ |
| } |
| } |
|
|
| |
| return strings.Compare(s1, s2) |
| } |
|
|
| const generatedHeader = `// Code generated by 'simdgen -o godefs -goroot $GOROOT -xedPath $XED_PATH go.yaml types.yaml categories.yaml'; DO NOT EDIT. |
| ` |
|
|
| func writeGoDefs(path string, cl unify.Closure) error { |
| |
| |
| var ops []Operation |
| for def := range cl.All() { |
| var op Operation |
| if !def.Exact() { |
| continue |
| } |
| if err := def.Decode(&op); err != nil { |
| log.Println(err.Error()) |
| log.Println(def) |
| continue |
| } |
| |
| op.sortOperand() |
| op.adjustAsm() |
| ops = append(ops, op) |
| } |
| slices.SortFunc(ops, compareOperations) |
| |
| |
| deduped := dedup(ops) |
| slices.SortFunc(deduped, compareOperations) |
|
|
| if *Verbose { |
| log.Printf("dedup len: %d\n", len(ops)) |
| } |
| var err error |
| if err = overwrite(deduped); err != nil { |
| return err |
| } |
| if *Verbose { |
| log.Printf("dedup len: %d\n", len(deduped)) |
| } |
| if !*FlagNoDedup { |
| |
| |
| if deduped, err = dedupGodef(deduped); err != nil { |
| return err |
| } |
| } |
| if *Verbose { |
| log.Printf("dedup len: %d\n", len(deduped)) |
| } |
| if !*FlagNoConstImmPorting { |
| if err = copyConstImm(deduped); err != nil { |
| return err |
| } |
| } |
| if *Verbose { |
| log.Printf("dedup len: %d\n", len(deduped)) |
| } |
| reportXEDInconsistency(deduped) |
| typeMap := parseSIMDTypes(deduped) |
|
|
| formatWriteAndClose(writeSIMDTypes(typeMap), path, "src/"+simdPackage+"/types_amd64.go") |
| formatWriteAndClose(writeSIMDFeatures(deduped), path, "src/"+simdPackage+"/cpu.go") |
| f, fI := writeSIMDStubs(deduped, typeMap) |
| formatWriteAndClose(f, path, "src/"+simdPackage+"/ops_amd64.go") |
| formatWriteAndClose(fI, path, "src/"+simdPackage+"/ops_internal_amd64.go") |
| formatWriteAndClose(writeSIMDIntrinsics(deduped, typeMap), path, "src/cmd/compile/internal/ssagen/simdintrinsics.go") |
| formatWriteAndClose(writeSIMDGenericOps(deduped), path, "src/cmd/compile/internal/ssa/_gen/simdgenericOps.go") |
| formatWriteAndClose(writeSIMDMachineOps(deduped), path, "src/cmd/compile/internal/ssa/_gen/simdAMD64ops.go") |
| formatWriteAndClose(writeSIMDSSA(deduped), path, "src/cmd/compile/internal/amd64/simdssa.go") |
| writeAndClose(writeSIMDRules(deduped).Bytes(), path, "src/cmd/compile/internal/ssa/_gen/simdAMD64.rules") |
|
|
| return nil |
| } |
|
|