spirv

113 definitions in 14 files, 0 with an encoding diagram. src/isa/spirv

Files

FileDefinitionsOtherWhat
package.lisp00the ISA.SPIRV package: host target SPIR-V: encodings, semantics, module builder.
grammar.lisp07the SPIR-V grammar as compile-time tables.
gen/kinds.lisp00SPIR-V operand kinds and their enumerants (generated, do not edit).
gen/insts.lisp00every SPIR-V instruction: opcode, operands, requirements (generated, do not edit).
gen/glsl450.lisp00the GLSL.std.450 extended instruction set (generated, do not edit).
sem.lisp012DEFSPV: SPIR-V instruction semantics in the core vocabulary.
int.lisp460semantics of SPIR-V's integer, bit, logical and select operations.
flt.lisp260semantics of SPIR-V's floating-point arithmetic, comparisons, conversions.
comp.lisp80semantics of SPIR-V's composite and vector operations.
ext450.lisp330semantics of the GLSL.std.450 extended instructions used for selection.
emit.lisp010building SPIR-V modules at run time: sections, ids, instruction words.
shade.lisp058the abstract programmable pipeline (src/core/shade.lisp) spelled in SPIR-V, at run time.
cost.lisp011what each SPIR-V operation costs under the selected GPU model.
cost/gt1030.lisp00SPIR-V cost model gt1030, NVIDIA GeForce GT 1030 (generated, do not edit).

README

src/isa/spirv: SPIR-V encodings and semantics

Package ISA.SPIRV. SPIR-V is the GPU host's shader ISA (XSPE_GPU=vk): SPU code becomes SPIR-V compute work. SPIR-V is never emulated here. Its description lets ctil find, for each SPU operation, the cheapest SPIR-V operations with the same meaning (docs/isel.md). The same grammar also drives the run-time encoder that writes modules.

FileRole
gen/kinds.lisp, gen/insts.lisp, gen/glsl450.lispGenerated from SPIRV-Headers by tools/spvgen: every operand kind and enumerant, every instruction, GLSL.std.450
grammar.lispCompile-time tables for the generated forms, plus spv-opcode and spv-enum
sem.lispdefspv, the value model, instantiation, and the forms for open behaviour
int.lispInteger arithmetic, bits, comparisons, bools, select, integer conversions
flt.lispFloat arithmetic, comparisons and conversions, under Vulkan's float environment
comp.lispComposite and vector operations, OpBitcast
ext450.lispGLSL.std.450: FindUMsb, SAbs, FClamp, Fma, rounding, ...
emit.lispThe run-time module builder and encoder
cost/gt1030.lispThe GT 1030 cost model, transliterated from ucas's prelude by tools/costgen; the build loads XSPE_COST_GPU (default gt1030)
cost.lispspv-op-cost: ucas's class of each operation (from its spirv.c) and price (docs/cost.md)

Grammar (generated)

(spv-kind "StorageClass" :value-enum
  ("UniformConstant" 0 :version "1.0") ...)
(spv-inst "OpIAdd" 128 "Arithmetic"
  (("IdResultType") ("IdResult") ("IdRef" "Operand 1") ("IdRef" "Operand 2")) :version "1.0")
(spv-ext-inst "GLSL.std.450" "FindUMsb" 75 (("IdRef" "Value")))

Each call registers its entry at compile time and expands to nothing at run time. Names are the grammar's own and are looked up without case: (spv-opcode OpIAdd) gives 128, (spv-enum StorageClass StorageBuffer) gives 12, (spv-enum ImageOperands Bias Lod) gives 3, and (spv-enum Dim "1D") gives 0. Aliases are accepted. Do not edit gen/. Instead, configure with -DXSPE_REGEN=ON and build spirv_gen. The spirv_gen_fresh test checks that the committed files match a fresh run.

Values

Every SPIR-V value is its bit pattern, stored as an integer:

Signedness belongs to the operation, never to the type. So (:int 32) covers both OpTypeInt 32 0 and OpTypeInt 32 1.

Type designators: (:int w), (:float w), (:bool), (:vec n T), (:struct T...) and (:array n T). Shorthands are i32, u8, f64, bool, v4i32, v2f64 and v3bool.

Semantics: defspv

(defspv OpIAdd (a b)
  (:int (w) (wrap w (+ a b))))

(defspv OpShiftLeftLogical (base shift)
  "The result is undefined if Shift >= the bit width of Base's components."
  (:int (w) (if (>= shift w) (spv-undef w) (wrap w (ash base shift)))))

(defspv OpIAddCarry (a b)                      ; struct result: one form per member
  (:int (w) (spv-members (wrap w (+ a b)) (carry-out w a b))))

(defspv OpFAdd (a b) (:float (w) (fop w (a b) (fadd (ffmt w) :rne a b))))

(defspv (glsl FindUMsb) (value)
  (:int (w) :require (= w 32) (if (= value 0) (ones w) (- (1- w) (clz w value)))))

Instantiation gives the canonical tree that the matcher sees:

(spv-sem-lambda 'OpIAdd 'v4i32)
;; => (lambda (a b) (declare ...) (vmap 32 (lambda (a b) (wrap 32 (+ a b))) a b))
(spv-sem-lambda 'OpFAdd 'f32 :fenv '(:denorm :preserve :nan :preserve))
;; => (lambda (a b) (declare ...) (fadd :f32 :rne a b))
(spv-eval (OpVectorShuffle v4i32 :ops (v4i32 v2i32) :lits (5 0 4 3)) x y)
(define-spv-fn iadd-v4 (OpIAdd v4i32))

:ops gives the value operand types (the default is the result type for each), :lits the literal operands, and :fenv the float environment.

What SPIR-V leaves open

These stay as calls in the tree. The reference evaluator (ECL, ctil --eval) picks one allowed outcome and records it in *spv-undefined*.

FormMeaningReference
(spv-undef w)any w-bit value ("the result is undefined")0, sets 1
(spv-ub)undefined behaviour ("behavior is undefined")0, sets 2
(spv-either x y)either valuex
(spv-ulp w tenths x)within tenths/10 ULP of xx
(spv-inexact w x)precision inherited from other operationsx

The float environment

:fenv is (:denorm D :nan N). Rounding is always to nearest even.

Rounding is to nearest even (:rne). Exact operations (the primitive itself) are OpFAdd, OpFSub, OpFMul, comparisons, conversions, FAbs, FMin/FMax/FClamp and NMin/NMax/NClamp (GLSL's definitions with the primitive's <, not IEEE minimum/maximum), Floor, Ceil, Trunc, RoundEven (fround-int), and Round (either tie rule). OpFDiv is within 2.5 ULP (x / 0 exact), InverseSqrt within 2 ULP: spv-ulp around the primitive formula. OpFRem, OpFMod, Fma (fused or not), FMix, Fract and Sqrt are inherited, and the transcendental functions (Exp2, Log2, Sin, Cos, Pow) bounded by [VKSPEC]: spv-inexact around the formula. A NaN result is the primitives' default NaN (Vulkan leaves the payload open). The transcendentals use fexp2 flog2 fsin fcos fpow, local helpers in src/core/shade.lisp until float.lisp has IEEE 754-2019's recommended operations.

Encoder: emit.lisp

(let ((m (make-spv-module)) (out (make-spv-words)))
  (spv-op m OpCapability 'Shader)
  (let* ((u32 (spv-op m OpTypeInt :new 32 0))
         (var (spv-op-in m :types OpVariable ptr :new 'StorageBuffer)))
    (spv-op m OpDecorate var 'Binding 0)               ; an enumerant and its parameter
    (spv-op m OpEntryPoint 'GLCompute main "main" gid)) ; a literal string
  (spv-finish m out #x00010300))                        ; -> the word count

spv-op encodes the instruction at expansion time from the grammar and checks the number and kind of its operands. 'Name is an enumerant, and its parameters follow it. A literal string is turned into words. (:u64 x) writes two words. :new allocates the result id and spv-op returns it.

Each instruction goes to its section of the module layout: capabilities, extensions, imports, memory model, entry points, execution modes, debug, annotations, types/constants/globals, and code. Each section is a fixed-size (simple-array u32 (N)), so instructions can be written in any order. spv-op-in names the section explicitly. You must use it for OpVariable and OpUndef. spv-open / spv-put / spv-close write an instruction whose length is only known at run time. A full section sets spvm-overflow, and spv-finish then returns 0.

Tests (tests/isa/spirv)

TestWhat
spirv_grammaropcodes, enumerants, aliases, operands
spirv_semsample values and canonical trees
spirv_semallevery componentwise semantics on many types and environments, with vector lanes checked against scalar instances
spirv_modulea compute module's words
spirv_module_valthe module ctil writes, compared byte for byte with tests/golden/spirv_module_val.spv (and ECL's under XSPE_ECL_ORACLE), then checked with spirv-val --target-env vulkan1.1
spirv_gen_freshgen/ against a fresh spvgen run (-DXSPE_REGEN=ON)

TODO


Made from xspe at b2fac33349db by tools/gen-xspe.py. xspe is GPL-3.0-or-later.