-
-
Notifications
You must be signed in to change notification settings - Fork 116
feat: Name collision avoidance in raw-WGSL implemented functions #2939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { type ResolvedSnippet, snip } from '../../data/snippet.ts'; | ||
| import { Void } from '../../data/wgslTypes.ts'; | ||
| import { getName } from '../../internal.ts'; | ||
| import { extractIdentifierLikeTokens, renameIdentifiers } from '../../rawShaderCodeUtils.ts'; | ||
| import { type ResolutionResult, resolve as resolveImpl } from '../../resolutionCtx.ts'; | ||
| import { $internal, $resolve, $soul } from '../../shared/symbols.ts'; | ||
| import { isBindGroupLayout } from '../../tgpuBindGroupLayout.ts'; | ||
|
|
@@ -211,11 +212,55 @@ function resolveFromTemplate(options: TgpuExtendedResolveOptions): ResolutionRes | |
| const resolutionObj: SelfResolvable = { | ||
| [$internal]: true, | ||
| [$resolve](ctx): ResolvedSnippet { | ||
| return snip( | ||
| replaceExternalsInWgsl(ctx, externals, template ?? ''), | ||
| Void, | ||
| /* origin */ 'runtime', | ||
| ); | ||
| try { | ||
| // It's technically not a function we're resolving, but we need a place to store local renames, so we treat the whole | ||
| const scope = ctx[$internal].itemStateStack.pushFunctionScope( | ||
| 'normal', | ||
| {}, | ||
| Void, | ||
| externals, | ||
| ); | ||
| // Pushing a block scope as well, so that any identifiers declared at this point will be scoped to the function body. | ||
| ctx.pushBlockScope(); | ||
|
|
||
| const identifiers = [ | ||
| ...new Set( | ||
| extractIdentifierLikeTokens(template ?? '').filter((ident) => { | ||
| return ( | ||
| !ctx.gen.isBannedToken(ident) && | ||
| !ctx.gen.isBuiltinGlobal(ident) && | ||
| externals[ident] === undefined | ||
| ); | ||
| }), | ||
| ), | ||
| ]; | ||
|
|
||
| const clashingIdentifiers = identifiers.filter((ident) => | ||
| ctx.isIdentifierTaken(ident, 'global'), | ||
| ); | ||
|
|
||
| const uniqueIdentifiers = identifiers.filter( | ||
| (ident) => !ctx.isIdentifierTaken(ident, 'global'), | ||
| ); | ||
|
|
||
| for (const ident of clashingIdentifiers) { | ||
| const renamed = ctx.makeUniqueIdentifier(ident, 'global'); | ||
| scope.localRenames.set(ident, renamed); | ||
| } | ||
| const renamedImpl = renameIdentifiers(template ?? '', scope.localRenames); | ||
| for (const ident of uniqueIdentifiers) { | ||
| ctx.reserveIdentifier(ident, 'global'); | ||
| } | ||
|
Comment on lines
+247
to
+253
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming every clashing identifier at permanent global scope mangles WGSL grammar tokens inside templates on re-resolution — the committed |
||
|
|
||
| return snip( | ||
| replaceExternalsInWgsl(ctx, externals, renamedImpl), | ||
| Void, | ||
| /* origin */ 'runtime', | ||
| ); | ||
| } finally { | ||
| ctx[$internal].itemStateStack.pop('blockScope'); | ||
| ctx[$internal].itemStateStack.pop('functionScope'); | ||
| } | ||
| }, | ||
| toString: () => '<root>', | ||
| }; | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"should rename references to local variables" only holds when another raw-WGSL function in the same resolution already produced the exact
{ARG: newName}entry. On its own, a JS-implemented function whose argument clashes with a global and that uses a snippet referencing that argument emits the wrong references.localRenamesis populated only infnCore.ts's raw-string branch (line ~170). The tinyest AST path (resolveFunctioninresolutionCtx.ts) renames function arguments viamakeUniqueIdentifierbut never records them intoscope.localRenames, so a snippet resolved inside that JS function reads an empty rename map.Reproduced (this is exactly the intended bullet-4 use case minus the sibling raw fn): with
const constant = tgpu.const(d.f32,123).$name('a')andjsFn = tgpu.fn([d.f32], d.f32)((a) => { 'use gpu'; return raw('a * 2 + constant').$uses({constant}) })in a tree with onlyjsFn, resolution yields:Adding a sibling raw-WGSL fn that consumes the same snippet concurrently flips jsFn to the intended
return a_1 * 2 + a;— so the emitted shader is order-dependent and, in the standalone case, wrong by silently reading a same-named global instead of the function's argument. The PR's added test only exercises the favorable ordering.