Interp-level and app-level objects
SPy is written in Python, and SPy programs are executed by SPy. So two worlds coexist inside the compiler, and you must always know in which one you are.
Naming conventions¶
The prefix tells you what a name refers to.
| Name | What it is | Example |
|---|---|---|
W_Foo | A Python class whose instances are app-level values | W_I32, W_Str |
w_foo | A Python variable holding an instance of a W_ class | w_x, w_T |
wam_foo | A Python variable holding a W_MetaArg (see below) | wam_x |
The W_ is for “wrapped”: the class wraps an app-level value in a Python object. w_ is
the same idea for variables, in lowercase as usual for Python variables.
wam_ is the w_ prefix followed by ma, for W_MetaArg. The letters w_ma_ would be
the strictly regular spelling, but wam_ is easier to say aloud.
One concept, three objects¶
Take the SPy type i32. Three different Python objects are involved:
from spy.vm.b import B
from spy.vm.primitive import W_I32
from spy.vm.vm import SPyVM
vm = SPyVM()
w_three = vm.wrap(3)
assert isinstance(w_three, W_I32) # (1) a value: the app-level integer 3
assert W_I32._w is B.w_i32 # (2) the Python class -> the SPy type
assert B.w_i32.pyclass is W_I32 # (3) the SPy type -> the Python class
assert vm.dynamic_type(w_three) is B.w_i32w_threeis an instance ofW_I32: it represents the app-level value3.B.w_i32is an instance ofW_Type: it represents the app-level typei32. Types are objects too, so they are wrapped as well, which is why the variable starts withw_.W_I32is the Python class that implements the values of that type.
W_I32 and B.w_i32 are two different things that you must not confuse. The first is a
Python class, the second is a wrapped SPy type. They are linked in both directions:
W_I32._wgives the SPy type. It is set by the@builtin_typedecorator that decoratesW_I32:@B.builtin_type("i32") class W_I32(W_Object): ...B.w_i32.pyclassgives the Python class back.
vm.dynamic_type(w_obj) asks an object for its SPy type. By default, it answers
type(w_obj)._w.
Declaring the signature of a builtin function¶
Builtin functions are SPy functions implemented in Python. SPy needs their SPy signature,
for example def sqrt(x: f64) -> f64, to type-check calls. Instead of asking you to
write it twice, SPy reads it from the Python type annotations:
from spy.vm.modules.math import MATH
from spy.vm.primitive import W_F64
from spy.vm.vm import SPyVM
import math
@MATH.builtin_func
def w_sqrt(vm: SPyVM, w_x: W_F64) -> W_F64:
x = vm.unwrap_f64(w_x) # app-level f64 -> Python float
return vm.wrap(math.sqrt(x)) # Python float -> app-level f64The decorator turns each annotation into a SPy type with to_spy_type. Here, W_F64
becomes B.w_f64 by reading W_F64._w. The result is the SPy signature
def sqrt(x: f64) -> f64.
The first parameter, vm, is the virtual machine. It is not part of the SPy signature.
When one Python class is not enough: Annotated¶
Reading _w from the class works only when one Python class corresponds to exactly one
SPy type. This is not always the case.
Consider a generic SPy type such as a SIMD vector. The types simd[f32, 4] and
simd[i32, 8] are different SPy types, but they are all implemented by the same Python
class, W_Simd. The class cannot know which SPy type a given function expects.
The answer is typing.Annotated. Annotated[PyClass, w_type] is a standard Python
construct that attaches extra metadata to a type. SPy uses it to say: “at interp-level
this is a PyClass, and at app-level its type is exactly w_type”. to_spy_type
returns w_type when it finds one in the metadata.
The following code builds a splat function (it fills a vector with a copy of one value)
specialized for one particular vector type. It is a simplified version of the real code
in spy/vm/modules/_simd.py:
from typing import Annotated
from spy.vm.object import W_Object
from spy.vm.vm import SPyVM
def make_splat(vm: SPyVM, w_simdtype: "W_SimdType"):
w_dtype = w_simdtype.w_dtype # the type of one lane, e.g. B.w_f32
SIMD_T = Annotated[W_Simd, w_simdtype]
T = Annotated[W_Object, w_dtype]
@vm.register_builtin_func(w_simdtype.fqn, "splat")
def w_splat(vm: SPyVM, w_x: T) -> SIMD_T:
return W_Simd(w_simdtype, [w_x] * w_simdtype.length)
return w_splatEvery sentence of this code matters:
make_splatis called once per vector type, andw_simdtypeis that type.w_splatis a new function each time, which closes overw_simdtype.T = Annotated[W_Object, w_dtype]says: the argument is, at interp-level, anyW_Object. At app-level it must have the typew_dtype. Forsimd[f32, 4], that isf32.SIMD_T = Annotated[W_Simd, w_simdtype]says: the result is aW_Simdat interp-level and has the SPy typew_simdtypeat app-level.The resulting SPy signature is
def splat(x: f32) -> simd[f32, 4]for one call ofmake_splat, anddef splat(x: i32) -> simd[i32, 8]for another. Plain annotations such asw_x: W_Objectcould not express this.
The same problem exists for instances. Since all vectors are W_Simd objects, the
default dynamic_type would not give the right SPy type. So W_Simd overrides the
method spy_get_w_type to return the type stored in the instance:
class W_Simd(W_Object):
def spy_get_w_type(self, vm: SPyVM) -> W_Type:
return self.w_simdtypewam_: arguments seen by the type checker¶
SPy decides what an operation means from the static type of its operands, that is, the type declared in the source, not the type of the value that happens to be there at runtime:
x: MyClass = MyClass()
y: object = xx and y hold the same object, but they have different static types, so they may
behave differently.
Some builtin functions must therefore see more than a value: they need the static type,
the color (red or blue, i.e. whether the value is known at compile time), and so on.
This information is carried by a W_MetaArg, named wam_xxx in the code:
| Attribute | Meaning |
|---|---|
wam.w_static_T | The static type, a W_Type |
wam.color | "red" or "blue" |
wam.w_blueval | The value, only available when the color is blue |
Functions declared with kind="metafunc" receive wam_ arguments instead of plain w_
values. They run at compile time and return a W_OpSpec, which tells SPy which function
to call to implement the operation:
@builtin_classmethod("splat", color="blue", kind="metafunc")
@staticmethod
def w_SPLAT(vm: SPyVM, wam_self: W_MetaArg, wam_x: W_MetaArg) -> W_OpSpec:
w_simdtype = wam_self.w_blueval # blue: the type is known now
w_splat = make_splat(vm, w_simdtype)
return W_OpSpec(w_splat, [wam_x])This is where the two previous sections meet. The metafunction reads the exact SPy type
out of wam_self, then builds a function specialized for it whose signature is declared
with Annotated.