Source file src/simd/archsimd/_gen/specdoc/phase.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  	"simd/archsimd/_gen/sgutil"
     9  	"simd/archsimd/_gen/specgen"
    10  	"simd/archsimd/_gen/specgen/specexpr"
    11  )
    12  
    13  // detectUnknown implements [Options.RejectUnknown] for the exported
    14  // declaration decl, with the matching spec function specFn or spec type
    15  // specType (or neither). It reports decl if it doesn't belong in the file,
    16  // either because it's unknown or because it isn't allowed in a file of the
    17  // file's phase.
    18  func (f *filler) detectUnknown(decl Decl, specFn *specgen.Func, specType *specgen.TypeDecl) {
    19  	if !f.opts.RejectUnknown || f.test {
    20  		return
    21  	}
    22  	allowed, apiReason := f.allowedPhases(decl, specFn, specType)
    23  	if allowed.has(f.phase) {
    24  		return
    25  	}
    26  	if apiReason == "" {
    27  		// decl is in neither spec nor sgutil.NonSpecAPI, and the file isn't
    28  		// experimental, so report it as unknown.
    29  		f.report.UnknownDecls = append(f.report.UnknownDecls, UnknownDecl{decl})
    30  		return
    31  	}
    32  	var fileReason string
    33  	switch f.phase {
    34  	case 0:
    35  		fileReason = "the file builds without GOEXPERIMENT=simdstable or simd"
    36  	case sgutil.PhaseStable:
    37  		fileReason = "the file is generally available (goexperiment.simdstable)"
    38  	case sgutil.PhaseExperiment:
    39  		fileReason = "the file is experimental (goexperiment.simd)"
    40  	}
    41  	f.report.PhaseViolations = append(f.report.PhaseViolations, PhaseViolation{D: decl, Phase: f.phase, Reason: apiReason + ", but " + fileReason})
    42  }
    43  
    44  // A phaseSet is a set of [sgutil.Phase]s.
    45  type phaseSet uint8
    46  
    47  // phases returns the set of ps.
    48  func phases(ps ...sgutil.Phase) phaseSet {
    49  	var s phaseSet
    50  	for _, p := range ps {
    51  		s |= 1 << p
    52  	}
    53  	return s
    54  }
    55  
    56  // has reports whether s contains p.
    57  func (s phaseSet) has(p sgutil.Phase) bool { return s&(1<<p) != 0 }
    58  
    59  // allowedPhases returns the phases of the files that may declare decl, with
    60  // the matching spec function specFn or spec type specType (or neither), and
    61  // the reason decl is allowed only in those phases. If a file's phase isn't one
    62  // of them, decl doesn't belong in the file, and the reason is used to report
    63  // that. The reason is "" if decl is unknown (in neither spec nor
    64  // [sgutil.NonSpecAPI]), which is reported separately.
    65  //
    66  // No file without a phase may declare anything, since it builds regardless of
    67  // GOEXPERIMENT. The APIs in [sgutil.NonSpecAPI] may be in a file of either
    68  // phase. Experimental APIs, including unknown ones, may only be in an
    69  // experimental file, and generally available APIs only in a generally
    70  // available one.
    71  //
    72  // An API described by spec is generally available unless spec marks it
    73  // //specgen:experiment, except that in archsimd, an API is only generally
    74  // available if it uses only fixed-width vectors, and only on the
    75  // architectures whose archsimd API can be generally available (see
    76  // [sgutil.BuildConstraints.StableArchOnly]). Everything else in archsimd is
    77  // experimental until its architecture is added to that list. This matters
    78  // because spec describes vector shapes, not packages: for example, a
    79  // scalable shape such as Int8s describes both the portable simd.Int8s and
    80  // archsimd.Int8s, the experimental SVE type.
    81  func (f *filler) allowedPhases(decl Decl, specFn *specgen.Func, specType *specgen.TypeDecl) (allowed phaseSet, reason string) {
    82  	stable, experimental := phases(sgutil.PhaseStable), phases(sgutil.PhaseExperiment)
    83  
    84  	// The SIMD APIs that spec doesn't describe, such as the CPU feature
    85  	// checks, may be in a file of either phase.
    86  	if sgutil.NonSpecAPI(f.pkg, decl.Recv, decl.Name) {
    87  		return stable | experimental, "listed in sgutil.NonSpecAPI"
    88  	}
    89  
    90  	// Anything else that spec doesn't describe is unknown. Experimental APIs
    91  	// needn't be in spec, so unknown APIs may be in an experimental file.
    92  	if specFn == nil && specType == nil {
    93  		return experimental, ""
    94  	}
    95  
    96  	// The rest are described by spec, and are generally available unless
    97  	// one of these makes them experimental.
    98  	if specFn != nil && specFn.Experiment {
    99  		return experimental, "marked //specgen:experiment in spec"
   100  	}
   101  	if f.pkg == "archsimd" {
   102  		if specType != nil && isScalable(specType.Shape) || specFn != nil && hasScalable(specFn) {
   103  			return experimental, "archsimd API with scalable vectors, which is experimental"
   104  		}
   105  		if !f.stableArchOnly {
   106  			return experimental, "archsimd API in a file that can build for architectures other than amd64, arm64 and wasm"
   107  		}
   108  	}
   109  	return stable, "generally available in spec"
   110  }
   111  
   112  // isScalable reports whether t is a vector type with a scalable width.
   113  func isScalable(t specexpr.Type) bool {
   114  	v, ok := t.(specexpr.Vector)
   115  	if !ok {
   116  		return false
   117  	}
   118  	_, fixed := v.Width.(specexpr.Int)
   119  	return !fixed
   120  }
   121  
   122  // hasScalable reports whether fn's receiver, parameters or results have
   123  // scalable vector types.
   124  func hasScalable(fn *specgen.Func) bool {
   125  	if fn.Recv.Type != nil && isScalable(fn.Recv.Type) {
   126  		return true
   127  	}
   128  	for _, args := range [][]specgen.Arg{fn.In, fn.Out} {
   129  		for _, a := range args {
   130  			if isScalable(a.Type) {
   131  				return true
   132  			}
   133  		}
   134  	}
   135  	return false
   136  }
   137  

View as plain text