Rule 8.28
Chapter 8, Functions and entry points Test
A call must not copy a struct or a fixed-size array that its callee only reads when the argument is a place: a let local, a module variable, an element or a field of a binding, a reference the caller holds (a parameter it writes through or reads in place), or a field or an element of one of those.
The arguments it covers are a method’s or a getter’s object, the object of a base’s body called through super, and a struct or an array argument of a function of the file that does not write that parameter; an entry’s parameters are not covered.
The callee reads the caller’s place for the whole call: on WGSL through a pointer, ptr<AS, T> with &place at the call (ptr<storage, T, read> for a read binding), and one copy of the function for each address space its calls use; on GLSL ES 3.00, for a module-scope place (a uniform block, an element of a read storage binding, a module variable), through a copy of the function that reads the place by its name, each index the place is reached through passed as a parameter and evaluated once at the call.
A local on GLSL ES 3.00, where no reference only reads, and an argument that is no place stay a copy taken at the call. On WGSL every struct or fixed-size array local is a place: one that WGSL would declare with let is declared as a var that nothing writes, with the same value (change 0105).
So does a place that can change while the callee runs, which is a place the callee or a function it calls writes by its name, a place another argument of the call is a reference the callee writes through to, and memory other invocations share (a workgroup variable, an element of a read_write storage binding) when the callee or a function it calls runs a barrier.
On GLSL ES 3.00 a reference a callee writes through (Rules 8.10 and 8.25) to a module variable is also reached through a copy of the function that writes the variable by its name.
A local must not copy a struct or a fixed-size array either: a const or a let local of such a type whose initializer is a place is a second name of that place (Rule 8.8, rule 1, change 0142), the front end emits no declaration for it, and each read of the name reads the place, written or not (changes 0104 and 0142).
Each index the place is reached through that is no literal is evaluated once, where the local is declared, into a name of the compiler’s own.
A write to the place shows through the name, and a write through the name shows in the place, as in JavaScript; a barrier changes nothing, because the name reads the place where the name is read.
On WGSL a struct or a fixed-size array that a function of the module returns into a local, a module variable or a read_write storage binding, or a field or an element of one, is written there through a pointer, f_into(&dst, …), when that saves a copy: the function builds its result in the one local every return returns, which then is the destination, or returns values that are no place; a call inside such a copy that returns into the destination is written there too. A call keeps its copy when an argument reads the destination or writes a name the destination is spelled with, and, for a module-scope destination, when the function or a function it calls reads or writes it by its name (change 0105). GLSL ES 3.00’s out parameter is a copy back, so the return stays there.
The rule text and the parts under it are the compiler's own English, as the design document writes them.
Rationale
in TypeScript an object argument and a method’s this are the object itself, so an author who writes items[i].area() writes a read of the fields area reads, not a load of the element whole; a struct parameter that its function only reads has the value of a copy, so the copy is the compiler’s to avoid, and a driver that inlines the call does not remove every one: an array field read at a dynamic index needs the copy in memory, a storage element loads every field nothing proves dead, a chain of methods copies at each level, and GLSL ES 3.00 rebuilt a uniform block from its members at each call and refused a method on an emulated storage element.
The callee reads the place for as long as it runs, where the copy was taken once at the call, and the two read one value unless the place changes during the call; the three routes that can change it keep the copy, so no program computes another value, and the first two are also the calls WGSL’s alias analysis refuses.
A copy of the function that names the global is the form a target without pointers has, and the one Tint gives a pointer parameter when it writes HLSL or MSL.
A local of a place is the place for the reason a parameter that only reads is: a copy would load the element whole, on each iteration of a loop, and an array field it indexes needs the copy in memory, and where the local or its place is written a copy would not have JavaScript’s result; the place is always the value the name holds. A struct a function returns is built in the callee and copied into the caller’s local, and a constructor’s self_ is the one local it returns, so the caller’s local can be built in place. A let has no address on WGSL, so a method called on one copies it, and so does each method that one calls in turn; a var that nothing writes holds the same one value and is a place.
Derives from
changes 0103, 0104, 0105 and 0142 in changes/; Function calls, Alias analysis and the language extension unrestricted_pointer_parameters in the WGSL specification; the GLSL ES 3.00 specification, section 6.1.1.
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:
the pointer-space pass in src/core/backends/wgsl-ptr.ts, the GLSL-local pass in src/core/backends/glsl-global-places.ts, the local pass in src/core/passes/local-places.ts, the binding of a local to its place in src/compiler/ts/lower/second-name.ts, the WGSL local-place pass in src/core/backends/wgsl-let-places.ts and the WGSL return pass in src/core/backends/wgsl-return-into.ts, pinned by src/compiler/ts/local-places.test.ts for the locals and the returns and by src/compiler/ts/reference-reads.test.ts, which compiles each place and each route that keeps the copy, holds the three CPU paths to one value for them, and reads the language service’s diagnostics on the same sources.
The files that verify it at commit af54ddad5, each at the first line that names the rule:
-
src/compiler/ts/local-places.test.ts:1 -
src/compiler/ts/lower/second-name.ts:1 -
src/compiler/ts/reference-reads.test.ts:1 -
src/core/backends/glsl-global-places.ts:9 -
src/core/backends/wgsl-let-places.ts:1 -
src/core/backends/wgsl-ptr.ts:25 -
src/core/backends/wgsl-return-into.ts:1 -
src/core/passes/local-places.ts:1
Explained in
The sections of the surface document that explain this rule, at commit af54ddad5:
See also
Source
The rule at commit af54ddad5:
docs/language-design.md:1188(the design document)reqs/rules/RULE-0828.md(its traceability item)