software-mansion / software-mansion/TypeGPU

bug: GLSL aliases evaluate comptime index multiple times

Open
#3,029 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.2k
Forks
122
Avg merge
3d 5h
Merged PRs (30d)
34

Description

let calls = 0;
const nextIndex = tgpu.comptime(() => calls++);

const fn = () => {
  'use gpu';

  const values = d.arrayOf(d.vec2i, 3)([
    d.vec2i(10, 11),
    d.vec2i(20, 21),
    d.vec2i(30, 31),
  ]);

  const value = values[nextIndex()]!;
  return value.x;
};

tgpu.resolve([fn], glOptions());

nextIndex() is called once so the generated function should return values[0].x

the generated function returns values[2].x

_constStatement already evaluates the RHS, then GlslGenerator._aliasConstStatement evaluates the index again in #hoistIndexAccesses, followed by another evaluation of the whole access expression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the shown example through _constStatement, then GlslGenerator._aliasConstStatement and #hoistIndexAccesses. Reproduce the snippet and verify that nextIndex() is evaluated once and the generated function returns values[0].x rather than values[2].x.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.