Rule 8.23
On this page

Rule 8.23

Chapter 8, Functions and entry points Test

A kernel function’s parameter of an array with no size is the caller’s array (change 0142): the body reads its elements and its .length, writes its elements in place, and cannot assign it whole (TS8018). The call reads back an array the body writes, and an array the body never writes comes back as it went in; return of the parameter gives the same array, or the same Buffer, with no copy. The author writes no qualifier, and a written @inout or @out is TS8073 (Rule 8.25); no call fills an array with zeros. A fixed-size array parameter, array<T, N>, is an object too (Rule 8.8). The .length of such an array counts elements, so an array<vec4f> counts vectors (change 0068). A Buffer of the parameter’s class, or of its scalars, is such storage too (Rule 8.21): the call reads and writes its device copy, and the host reads what the call wrote with await buffer.read(). A kernel function may also make the array it returns (change 0114, S5): one const at the top level of its body, before its first loop and its first store, initialized by new Float32Array(n) (n zeros of f32) or new Array(n).fill(v) (n copies of v, or for a struct n references to one object, as JavaScript’s fill gives), whose element is an f32, an i32 or a u32, a vector of them, or a struct whose fields are these (slice 2), and returned on every path. n has any number type and reads only the value parameters, their components and fields, module constants, the .length of an array parameter, number literals, u32, i32 and f32 of these, and top-level consts of these, with +, -, *, /, %, min and max; v reads the same operands, and may be a vector or a struct built of them. The host evaluates n and v before anything runs, with the program’s arithmetic, and a length that is negative or not an integer throws a RangeError that names the function and the length. The body reads and writes the array’s elements and reads its .length, as for an array parameter, and .length reads n on every tier, 0 included; the call holds the array as an array parameter of its own (the compiler’s choice of representation, OD-9), checks every index against n as for a parameter, and resolves to it in the form Rule 8.21 gives an array parameter of its element: the scalar’s typed array for a scalar or a vector element, a vector’s components one after another (Float32Array for vec4f[]), and an array of objects for a struct, instances of its class when the module exports one. A struct fill shares one object (Amendment A8, item 4; OD-5): in the result, every element the run does not write as a whole is one object, which is the caller’s own object when v is a struct parameter or a field of one, and each element the run writes as a whole (out[i] = w) is its own object, so a write on the host to a field of one unwritten element shows in every other. Where the compiler cannot prove that the run writes every element, the kernel records a written mark for each element and the host reads the marks (the compiler’s choice, OD-9). A vector is a value, so a vector fill shares nothing. Refused as not supported yet (TS8099), each with a sentence that names the form: an element that is a matrix, a bool, a key or an f64, or a struct that holds one; such an array in a helper or an entry; one made inside a loop, under a branch, or after a loop or a store; a length or a fill value that reads another operand; a whole write of a struct element whose object the compiler cannot tell is new to that element (out[i] = w where w is a parameter, another element or a const declared before the loop); Array.from({ length: n }, f) with such a length; another return; a use of the array as a value (out.push(v), a second name, an argument of a call); and a function that also takes a 2D storage texture. A result of an array with no size that is no new array stays TS8020. The const may instead start empty, annotated with an array with no size of an element the made array may hold (const out: f32[] = [], change 0114, S5d), and grow with out.push(v) in the for and for…of loops of the body: a loop at the top level, a loop nested in one, or a loop under a top-level if whose condition reads only the operands n reads (change 0114, Amendment A12, form 2, Route 1). The compiler computes its capacity, the sum over the pushes of the product of the trip counts of every loop around a push (a push in an outer body counts once for each outer trip, a push in an inner body once for each inner trip of each outer trip, and a push under a branch inside a loop counts as one), where every loop’s start and bound read only the operands n reads, and a loop under a top-level if counts only when the host, evaluating the condition with the program’s arithmetic, finds its branch taken; the host evaluates the start and the bound with the program’s arithmetic and the capacity exactly, with no wrap, before anything runs, and holds the array as an array parameter of that capacity. A loop nested in a top-level loop may instead start or end at a value the host does not know before the run (the counter of a loop around it, an element of an array parameter, the length of the array itself), when the function is one the call may run twice (change 0114, Amendment A12, form 2, Route 2): every loop is one the proof of Rule 8.22 accepts, the only write of the function is the push onto this one array, every push is a statement, the body reads no element of the grown array, an element of an array parameter is read only at the counter of a loop up to that array’s length, or at the counter of the top-level loop that holds the read when no element is read in that loop’s start or bound and the function has a GPU plan (the call checks such a read against the array’s length before anything runs, Rule 8.21; change 0114, Amendment A17), and no statement follows the loops but the return. The call then runs the function once to count the pushes, as a u32 that stops at 4294967295, reserves exactly that many elements and runs it again, and a count of 4294967295 or more throws a RangeError before the second run; a call whose first and second run push different counts throws an Error and returns nothing. A function whose capacity Route 1 sizes within the limit keeps Route 1, and one it sizes past the limit takes Route 2. On WebGPU the first run dispatches only the count and scan steps of its loops (change 0078, with no store step) and reads the total, and the count stops at 4294967295 there as it does on the CPU tier; on every other tier the first run is the function’s run on the CPU tier. A capacity past 4294967295, or past what one host array of its element holds, in a function that Route 2 does not serve (below), takes Route 3 (below) at the call when the runtime’s tier list holds the CPU tier (change 0114, Amendment A17, R3-Q1); where the list holds no CPU tier, the call throws a RangeError that names the function, the array and the capacity before anything runs, since a length past that limit is not supported yet. The pushes append as they do onto an array parameter (below), in the order of a sequential run, on every tier; out.length in the body reads the number pushed so far; and the call resolves to a new array of exactly the elements pushed, in the made array’s form, which the caller owns. Pushes of a struct keep JavaScript’s objects (Amendment A8, item 4): when every push pushes one const object declared at the top level of the body, which only the pushes use, the elements are one object; a push of a new object each time gives each element its own. Refused as not supported yet (TS8099), each with a sentence that names the loop or the form: pushes of a struct that the compiler cannot tell are one object or new ones (one object mixed with new ones, or one the body changes); a push outside every loop; a loop at the top level whose start or bound reads another operand, a range the device holds included, unless the bound reads the length of the array itself (Route 3, below); an element the made array may not hold; and a read or a write of an element before the first loop. Any other loop that pushes onto the array or writes an element of it that can append (a while, a for that is no count from a start to a bound by a constant whole step or whose body writes its counter, a loop inside such a loop, and a loop under a switch or under a branch inside a loop; a loop nested in a top-level loop whose start or bound reads a value that no route can size, which is the counter of a loop around it or an element of an array parameter, in a function that Route 2 does not serve; a loop under a top-level if whose condition reads another operand; a top-level loop whose bound reads out.length; and a write the compiler cannot prove below the length in a loop inside another loop or under a branch; a do … while stays refused by the front end) takes Route 3 where Route 2 does not serve the function (change 0114, Amendment A12, section 1, Route 3): the call reserves no capacity, runs the function once on the CPU tier, which appends in the order of a sequential run, and resolves to a new array of exactly the elements pushed, which the caller owns. A length of 4294967295 or more throws the RangeError of Route 2, and a host that cannot hold the elements pushed throws a RangeError that names the function, the array and the length it reached. Where the proof of Rule 8.22 accepts every loop and the function could otherwise run on a device, a loop whose start or bound the compiler cannot read draws a TS8070 warning (Rule 12.4), in both halves, on the first such loop: "for (let j: u32 = 0; j < counts[at[i]]; j++)" pushes onto "out", and its start or bound reads a value the call does not know before it runs, so this function runs on the CPU, where the array grows as it is pushed to. A function that Route 1 sizes past the limit, or whose reserve the host cannot allocate, and that Route 2 does not serve, runs as Route 3 at the call when the runtime’s tier list holds the CPU tier. No GPU tier runs a function of Route 3: the reason a call names for WebGPU and WebGL2 is the proof’s, or else that growing an array during a run on those tiers is not supported yet, and a call whose list holds only those tiers throws the error of Rule 11.8. The other refusals of the made array apply to it. An element of the array is read and written as JavaScript reads and writes it at that point of a sequential run (change 0114, Amendment A12, section 2): a read needs an index below the length, a write an index at most the length, and a write at the length appends as push does. An index the compiler proves below the length, the counter of a loop that counts up from 0 to out.length by 1 and that the body does not write, runs on every tier as an array parameter’s does. Any other index is checked at the call on the CPU tier only (A12, Q1 (C)): the function has no GPU plan, and the reason names the access; each such write counts as one push in the capacity; and an index past what the access may reach throws a RangeError that names the function, the access, the index and the length, before the caller’s arrays change. For a read-only, non-resident array of flat scalar objects, a private WebGPU input may contain only live fields in declaration order (change 0082). Field use includes all loops and reachable helpers. Unproved uses retain the full layout. Fusion facts do not select an execution mode: an eligible ordinary call can project inputs while its face carries full-layout facts (change 0085). The CPU and WebGL2 paths, authored entry bindings, raw bytes, resident and fused calls retain their original layout. Element indexing and array length do not change. A kernel function’s texture parameter (change 0077), a texture_2d<f32> or a 2D storage texture, stays the caller’s texture, passed by reference with no qualifier, as a Map and a Set do: the body loads it with textureLoad, measures it with textureDimensions, stores into a storage texture with textureStore, and cannot assign it whole. An array parameter a loop pushes onto (change 0078) starts empty in the function, or at its held length when the call passes one (change 0094): the pushes append in the order a sequential run of the function makes them, loop by loop, iteration by iteration and statement by statement, on every tier, so a second loop that pushes onto the array appends after the first, and its .length is the held length (0 without one) plus the count appended, a u32, which may exceed the host array’s length. The host array is the capacity: the call writes the elements from the held length up to min(length, capacity) - 1 and leaves the rest as they were, and the host reads the length through the function’s own return out.length, or in the held length’s word, which the call leaves holding the new length. A held length is { array, length, offset? } (Rule 8.21): array is what the parameter takes otherwise, and element offset of length, a Buffer<u32> (change 0138, Amendment A10, confirmation item 3), holds the array’s length when the call runs, as an array’s length before ECMAScript’s push. The call refuses, with a TypeError that names the function and the parameter, before anything runs: the object on an array no loop pushes onto (decision D5); a length that is no Buffer<u32>, a scratch array of a fuse scope or a disposed Buffer; an offset that is no whole number from 0, or is past length; a length that is another argument of the call or the length of another; and a Buffer of another owner (Rule 11.8). An object with another key is refused as a wrong array is. A Map or a Set parameter (change 0080) is the caller’s JavaScript Map or Set, passed by reference: its entries are there before the loop, and when the call resolves the entries a sequential run of the function leaves are in it. A key is added once, and its place in the order is the first add or set of it; a Map keeps the value of the last set of each key; keys compare by SameValueZero, so one NaN key holds every NaN and -0 is 0. The call refuses, with a TypeError that names the parameter, a value that is not a Map or a Set as the parameter says, and the first key or value that does not fit its type. For a key-type K, the host passes strings: the call converts keys to ids before the loops and back to strings after them, preserving insertion order; a key outside the union is a TypeError, and Map<string, V> and Set<string> stay refused (change 0081). A kernel function is emitted by no target, since it runs on the host’s side of the call; an array<T, N> parameter is an object (Rule 8.8).

The rule text and the parts under it are the compiler's own English, as the design document writes them.

Rationale

a runtime-sized array exists only in storage (Rule 12.6). A kernel function’s array is the caller’s array, as a JavaScript function’s array parameter is, so a function that updates it writes its elements and the call writes them back; the compiler reads which arrays a body writes, so no qualifier says it (OD-5, change 0142). A write that nothing carries back is no longer a refusal: the caller’s array changes, as it does in JavaScript, and the code of that refusal is retired.

Derives from

change 0013 in changes/; change 0094 (a held length, and two loops that push onto one array); change 0138 (Amendment A10, Q-A and Amendment A15 give the value reading that change 0142 withdraws); change 0142 in changes/; Rule 8.8; Rule 12.6.

How it is verified

Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.

Where the rule says the compiler enforces it:

parseParams and fillFunctionBody in src/compiler/ts/lower/function.ts, which take the parameter as an inout storage array for a kernel function and refuse it on any other (TS8020); runChecked in src/core/host-kernel.ts, which reads back an array the body writes and no other, pinned by src/compiler/ts/host-kernel-out.test.ts (the array written in place on every call, an array the body never writes back as it went in, a Buffer, and a held length refused on an array no loop pushes onto); withoutKernels in src/core/ir/kernels.ts, which every backend and reflect() read through; pinned by src/compiler/ts/kernel-loops.test.ts, which runs a kernel function on the CPU oracle and holds the caller’s array written in place. A texture parameter is a handle binding of the loop’s entry (lowerKernel in src/core/passes/kernel-lower.ts) and a texture parameter of the face (kernelFace in src/compiler/ts/host-face.ts); pinned by src/compiler/ts/host-kernel-texture.test.ts and by the import journey, whose invertImage stores into a device’s texture on WebGPU in Chromium, each byte the reference’s. A pushed array’s count is pushedParams and countPushedLengths in src/compiler/ts/lower/kernel-push.ts and the builtins push and pushCount of src/core/cpu-runtime.ts; pinned by src/compiler/ts/host-kernel-push.test.ts on the CPU tier: the order, an empty result, a count past the capacity, two pushes in one iteration and a second call that starts empty. A held length (change 0094) is heldArgument in src/core/host-arity.ts and checkHeld in src/core/host-kernel.ts, which the CPU tier reads and writes through startPushesAt and pushedLength in src/core/push-count.ts, the table the builtins push and pushCount keep the count in, and the pushed parameter’s type in the host view from kernelFace and viewText in src/compiler/ts/host-face.ts; pinned by src/compiler/ts/host-kernel-push.test.ts (both halves on one source, the view under tsc and in the editor, the CPU tier from a held length inside, at and past the capacity, each refusal, and two loops onto one array). A Map or a Set is a collection parameter of the face (kernelFace in src/compiler/ts/host-face.ts), checked and copied by checkCollection in src/core/host-kernel.ts and written back by collectionBack, over the builtins of src/core/cpu-runtime.ts, which are JavaScript’s own Map and Set; pinned by src/compiler/ts/host-kernel-collections.test.ts on the CPU tier against JavaScript’s own on the same input: duplicates, the host’s entries before the call, the last value set, NaN and -0 keys, a vector value, and each refusal. The array a kernel function makes and returns (change 0114, S5): madeCandidate and refuseResultArray in src/compiler/ts/lower/made-array/candidate.ts, lowerMade in src/compiler/ts/lower/made-array/lower.ts, which binds it as an array parameter of its own and refuses the elements later slices take (TS8099), settleMadeArray in src/compiler/ts/lower/made-array/settle.ts, which checks the operands of its length and fill value, lengthFromHost in src/compiler/ts/lower/made-array/length.ts, which makes each read of its .length read the length the host computed, madeFace in src/compiler/ts/host-face/made.ts, which records the parameters the length and the fill value read, madeArray in src/core/host-kernel-made/making.ts, which evaluates the length and throws the RangeError, callMadeKernel in src/core/host-kernel-made.ts, which resolves to the array, and callMadeOnResource in src/core/host-kernel-made/on-resource.ts, which returns the new Buffer of a call on a resource (A1 item 7), throws the RangeError for a length of 0 and refuses the forms it does not support yet, pinned by src/compiler/ts/host-kernel-made-resource.test.ts (both halves, a chain of two calls, a vector element as its scalars, a Buffer<C>, the index check at the call, the length of 0, each refusal and the host view’s overload); pinned by src/compiler/ts/host-kernel-made.test.ts (both halves and the hover, the CPU tier against the parameter form and a JavaScript loop, the index check, the length of 0, the host view under tsc), by src/compiler/ts/host-kernel-made-elements.test.ts (vector and struct elements: both halves and the hover, the CPU tier against the parameter form, the length of 0, the index check in elements, the pushed vectors, instances of an exported class, the host view), by src/compiler/ts/made-array-refusals.test.ts (each refusal in both halves) and on WebGPU by renderMade of journeys/_host-import. A struct fill that shares one object (Amendment A8, item 4): refuseSharedStores in src/compiler/ts/lower/made-array/identity.ts, which refuses a whole write or a push whose object the compiler cannot tell, writtenOf and markWrites in src/compiler/ts/lower/made-array/written.ts, which prove every element written or record a written mark for each, and callMadeKernel, which gives the unwritten elements one object; pinned by src/compiler/ts/host-kernel-made-shared.test.ts (both halves, the face’s marks, the CPU tier against a JavaScript run with === and a host-side field write, the caller’s object of a parameter fill, pushes of one object and of new ones, each refusal in both halves) and on WebGPU by shareMade of journeys/_host-import. The array a kernel function grows with push (change 0114, S5d): isGrownForm in src/compiler/ts/lower/made-array/candidate.ts, lowerGrown in src/compiler/ts/lower/made-array/grown.ts, which binds it and refuses a push outside the top-level loops and an element read or write, grownCapacity in src/compiler/ts/lower/made-array/capacity.ts, which records each loop’s start, bound, step and pushes and refuses a top-level loop whose bound it cannot read, settleMadeArray, which makes the function return the number pushed, exactCapacity, checkedCapacity and allocatedFor in src/core/host-kernel-made/capacity.ts, which sum the capacity exactly and throw the RangeError past the limit, and callMadeKernel, which resolves to the elements pushed and throws if more were pushed than the capacity; pinned by src/compiler/ts/host-kernel-grown.test.ts (both halves and the hover, the CPU tier against a JavaScript loop: an unconditional push, a conditional push, two loops, two pushes in one iteration, out.length in the body, an empty result, the host view under tsc), by src/compiler/ts/host-kernel-grown-capacity.test.ts (both halves, the exact capacity past 2^32 of two pushes, a signed counter by a step of 3 and two loops, the limit and one past it, the RangeError before the run), by src/compiler/ts/host-kernel-grown-nest.test.ts (a push in a nest and under a top-level branch: both halves, the capacity summed over every push at every depth, exact past 2^32 where a u32 product wraps to 0, a branch not taken that throws nothing, and the CPU tier against a JavaScript loop), by src/compiler/ts/host-kernel-grown-nest-refusals.test.ts (the refusals that stay, in both halves), by src/compiler/ts/grown-array-refusals.test.ts (each refusal in both halves), by src/compiler/ts/host-kernel-grown-elements.test.ts (an element of the grown array: both halves and the hover, the GPU plan where every access is proved and its absence where one is not, the capacity of an unproved write, the CPU tier against JavaScript, the RangeError and the caller’s arrays after it) and on WebGPU by renderPushed and renderKept of journeys/_host-import. grownAccesses and grownChecks in src/compiler/ts/lower/made-array/bounds.ts prove an index below the length and give the CPU tier its checked accesses (pushedReadAt and pushedStoreAt in src/core/pushed-index.ts); madeFace in src/compiler/ts/host-face/made.ts drops the GPU plan of a function with one. Route 3 (change 0114, Amendment A12, S5d slice 3): nestTerms in src/compiler/ts/lower/made-array/nest.ts marks a loop it cannot count, a branch it cannot read and a top-level bound that reads out.length open, and a nested header it cannot read unread, routeOf in src/compiler/ts/lower/made-array/route.ts gives the function counted or growing, never both, and hands the unread loop to kernelLoopDiagnostics, which draws the TS8070 warning, cpuOnly in src/compiler/ts/host-face/made.ts words the reason of the CPU tier (the proof’s, then the growing reason, then the access’s), and growingRun, callGrowing and materialized in src/core/host-kernel-made/growing.ts run it, and onCpuTier there with callMadeKernel gives a call whose capacity Route 1 cannot use the CPU face of Route 3; pinned by src/compiler/ts/host-kernel-growing.test.ts, host-kernel-growing-run.test.ts, host-kernel-growing-route.test.ts, host-kernel-growing-retired.test.ts and host-kernel-growing-open.test.ts. TS8080 is a retired code and is never reused (src/compiler/ts/codes.ts). Private field analysis is projectKernelInputs in src/core/passes/kernel-project.ts, serialized by kernelFace and selected by projectedInputs in src/core/host-kernel.ts. It is pinned by src/compiler/ts/kernel-project.test.ts with default and explicit fusion options, by src/runtime/device.test.ts alternating ordinary, resident and fused calls on one face, by the projected cases of scripts/gpu-differential.ts, and the packed import journey.

The files that verify it at commit af54ddad5, each at the first line that names the rule:

Explained in

The sections of the surface document that explain this rule, at commit af54ddad5:

Error codes that enforce it

The diagnostic codes the rule names under Enforced by, or whose registry text names the rule:

TS8020 FUNCTION_SHAPE
A function declaration or parameter shape that TypeShade does not support, such as a missing name or body, a rest parameter or a destructured parameter.
TS8070 KERNEL_LOOP_ON_CPU
A warning on a loop of a kernel function (Rule 8.22) that the independence proof refused: the loop runs on the CPU. The message names the line and the names that stop it, and the remedy. The program is correct either way.
TS8080
Retired by change 0142 (VALUE_ARRAY_WRITE_LOST): TS8080 was the refused write to an array parameter that no return carries back (change 0138, Amendment A15); change 0142 makes an array parameter the caller's object, so such a write is the program's meaning and no refusal has a true text.
TS8099 UNSUPPORTED
The catch-all for a diagnostic whose site does not yet deserve its own code. It stays parked at TS8099, past the sequential range.

See also

Source

The rule at commit af54ddad5:

Edit this page Report a problem