Source file src/simd/archsimd/_gen/specdoc/phase_test.go

     1  // Copyright 2026 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 specdoc
     6  
     7  import (
     8  	"errors"
     9  	"slices"
    10  	"strings"
    11  	"testing"
    12  
    13  	"simd/archsimd/_gen/specgen"
    14  	"simd/archsimd/_gen/specgen/specexpr"
    15  )
    16  
    17  func TestRejectUnknownPhase(t *testing.T) {
    18  	vec := func(name string) specexpr.Type {
    19  		typ, err := specexpr.ParseTypeName(name)
    20  		if err != nil {
    21  			t.Fatal(err)
    22  		}
    23  		return typ
    24  	}
    25  	method := func(recv, name string, experiment bool) *specgen.Func {
    26  		return &specgen.Func{Name: name, Recv: specgen.Arg{Name: "x", Type: vec(recv)}, Experiment: experiment}
    27  	}
    28  	idx := specgen.NewIndex([]*specgen.Func{
    29  		method("Int32x4", "Add", false),
    30  		method("Int32x4", "Exp", true),
    31  		method("Int8s", "Add", false),
    32  	}, []*specgen.TypeDecl{
    33  		typeDecl(t, "Int32x4", "Int32x4 is a vector."),
    34  		typeDecl(t, "Int8s", "Int8s is a scalable vector."),
    35  	})
    36  
    37  	const decls = `
    38  type Int32x4 struct{}
    39  type Int8s struct{}
    40  type X86Features struct{}
    41  type Other struct{}
    42  type unexported struct{}
    43  
    44  var X86 X86Features
    45  var Var int
    46  const Const = 1
    47  
    48  func (Int32x4) Add(y Int32x4) Int32x4
    49  func (Int32x4) Exp() Int32x4
    50  func (Int32x4) Unknown() Int32x4
    51  
    52  // Deprecated: use Add. Spec doesn't describe deprecated APIs.
    53  func (Int32x4) Sub(y Int32x4) Int32x4
    54  
    55  func (Int8s) Add(y Int8s) Int8s
    56  func (X86Features) AVX() bool
    57  func (X86Features) SHA() bool
    58  func (unexported) Unknown()
    59  func unknown()
    60  `
    61  	tests := []struct {
    62  		name     string
    63  		filename string
    64  		header   string
    65  		want     []string // violating declarations
    66  	}{
    67  		{
    68  			name:   "stable archsimd",
    69  			header: "//go:build goexperiment.simdstable && amd64\n\npackage archsimd\n",
    70  			want:   []string{"(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(Int8s) Add", "Const", "Int8s", "Other", "Var"},
    71  		},
    72  		{
    73  			// archsimd's API is generally available only for the
    74  			// architectures in the stable archsimd architectures, so a file for
    75  			// any architecture can't declare it...
    76  			name:   "stable archsimd, any architecture",
    77  			header: "//go:build goexperiment.simdstable\n\npackage archsimd\n",
    78  			want:   []string{"(Int32x4) Add", "(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(Int8s) Add", "Const", "Int32x4", "Int8s", "Other", "Var"},
    79  		},
    80  		{
    81  			// ... and neither can a file for another architecture.
    82  			name:     "stable archsimd, riscv64",
    83  			filename: "simd/archsimd/ops_riscv64.go",
    84  			header:   "//go:build goexperiment.simdstable\n\npackage archsimd\n",
    85  			want:     []string{"(Int32x4) Add", "(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(Int8s) Add", "Const", "Int32x4", "Int8s", "Other", "Var"},
    86  		},
    87  		{
    88  			// ... or a file for a GA architecture or another one.
    89  			name:   "stable archsimd, amd64 or mips",
    90  			header: "//go:build goexperiment.simdstable && (amd64 || mips)\n\npackage archsimd\n",
    91  			want:   []string{"(Int32x4) Add", "(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(Int8s) Add", "Const", "Int32x4", "Int8s", "Other", "Var"},
    92  		},
    93  		{
    94  			name:   "experimental archsimd",
    95  			header: "//go:build goexperiment.simd && amd64\n\npackage archsimd\n",
    96  			want:   []string{"(Int32x4) Add", "Int32x4"},
    97  		},
    98  		{
    99  			// On an architecture whose archsimd API isn't generally
   100  			// available, everything is experimental.
   101  			name:     "experimental archsimd, riscv64",
   102  			filename: "simd/archsimd/ops_riscv64.go",
   103  			header:   "//go:build goexperiment.simd\n\npackage archsimd\n",
   104  		},
   105  		{
   106  			// The architecture rule is only for archsimd, and the non-spec
   107  			// APIs depend on the package.
   108  			name:   "stable simd",
   109  			header: "//go:build goexperiment.simdstable\n\npackage simd\n",
   110  			want:   []string{"(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(X86Features) AVX", "(X86Features) SHA", "Const", "Other", "Var", "X86", "X86Features"},
   111  		},
   112  		{
   113  			// In package simd, scalable types aren't SVE.
   114  			name:   "experimental simd",
   115  			header: "//go:build goexperiment.simd\n\npackage simd\n",
   116  			want:   []string{"(Int32x4) Add", "(Int8s) Add", "Int32x4", "Int8s"},
   117  		},
   118  		{
   119  			// A file without a phase builds regardless of GOEXPERIMENT, so
   120  			// it can't export anything.
   121  			name:   "no phase",
   122  			header: "//go:build amd64\n\npackage archsimd\n",
   123  			want:   []string{"(Int32x4) Add", "(Int32x4) Exp", "(Int32x4) Sub", "(Int32x4) Unknown", "(Int8s) Add", "(X86Features) AVX", "(X86Features) SHA", "Const", "Int32x4", "Int8s", "Other", "Var", "X86", "X86Features"},
   124  		},
   125  		{
   126  			name:     "test file",
   127  			filename: "x_test.go",
   128  			header:   "//go:build goexperiment.simdstable\n\npackage archsimd\n",
   129  		},
   130  	}
   131  	for _, tt := range tests {
   132  		t.Run(tt.name, func(t *testing.T) {
   133  			_, err := Fill([]byte(tt.header+decls), idx, Options{
   134  				Filename:            tt.filename,
   135  				AllowDocRewrite:     true,
   136  				AllowNameMismatches: true,
   137  				NoFillDoc:           true,
   138  				NoFillNames:         true,
   139  				RejectUnknown:       true,
   140  			})
   141  			var got []string
   142  			var rep *Report
   143  			if errors.As(err, &rep) {
   144  				for _, v := range rep.UnknownDecls {
   145  					got = append(got, v.D.String())
   146  				}
   147  				for _, v := range rep.PhaseViolations {
   148  					got = append(got, v.D.String())
   149  				}
   150  			} else if err != nil {
   151  				t.Fatal(err)
   152  			}
   153  			slices.Sort(got)
   154  			if !slices.Equal(got, tt.want) {
   155  				t.Errorf("violations:\n\t%s\nwant:\n\t%s", strings.Join(got, "\n\t"), strings.Join(tt.want, "\n\t"))
   156  			}
   157  		})
   158  	}
   159  
   160  	// Without RejectUnknown, nothing is rejected.
   161  	_, err := Fill([]byte("//go:build goexperiment.simdstable\n\npackage archsimd\n"+decls), idx, Options{AllowDocRewrite: true, AllowNameMismatches: true})
   162  	var rep *Report
   163  	if errors.As(err, &rep) && (len(rep.PhaseViolations) > 0 || len(rep.UnknownDecls) > 0) {
   164  		t.Errorf("rejected declarations without RejectUnknown: %v", rep)
   165  	}
   166  
   167  	// A file that never builds is an error.
   168  	_, err = Fill([]byte("//go:build goexperiment.simd && !goexperiment.simdstable\n\npackage archsimd\n"), idx, Options{RejectUnknown: true})
   169  	if err == nil || errors.As(err, &rep) {
   170  		t.Errorf("want error for a file that never builds, got %v", err)
   171  	}
   172  }
   173  

View as plain text