software-mansion / software-mansion/TypeGPU

feat: Granular tracking of stale references

Open
#2,753 1 comment 1 reaction 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

There has been TypeScript behavior that we have overlooked up until now that has to do with references:

const Boid = d.struct({
  pos: d.vec3f,
  vel: d.vec3f,
});

function main() {
  'use gpu';
  const boid = Boid();
  // getting a reference to the boid's position
  const boidPos = boid.pos;
  // replacing the `pos` property of the boid with a new vector
  boid.pos = d.vec3f(1, 2, 3);
  // returning the `x` coordinate of the vector that was once
  // the boid's position
  return boidPos.x;
}

We'd expect the result of main() to be 0, since we haven't actually updated the x coordinate of the existing boid.pos, rather we replaced that vector with an entirely new one, making boidPos no longer point to the actual value of boid.pos (they drifted out of sync).
We generate the following WGSL:

struct Boid {
  pos: vec3f,
  vel: vec3f,
}

fn main() -> f32 {
  var boid = Boid();
  let boidPos = (&boid.pos);
  boid.pos = vec3f(1, 2, 3);
  return (*boidPos).x;
}

In the generated WGSL, boidPos is a pointer to boid.pos, meaning it always reflects the up-to-date value of boid.pos, making the result of calling main() be 1.

Stale reference tracking

@cieplypolar's insight is that we can use a sort of borrow checker (like we were planning to do for consumption verification) to invalidate stale references. Essentially, as soon as it's determined that boid (the original value) could have been mutated by an instruction, boidPos would become invalidated and unaccessible.

struct Boid {
  pos: vec3f,
  vel: vec3f,
}

fn main() -> f32 {
  var boid = Boid();
  let boidPos = (&boid.pos);
  boid.pos = vec3f(1, 2, 3); // <- accessing the original invalidates the aliases
  return (*boidPos).x; // 🚫 `boidPos` is a stale reference
}

I believe there is a way to do this statically without introducing too much complexity to the system.

[!NOTE]
To be continued...

function main() {
  'use gpu';
  const boid = Boid();
  // boid
  //

  const boidPos = boid.pos;
  // boid
  // |
  // | (.pos)
  // |
  // boidPos

  randomFunction(boid) // <- arguments are immutable, so this is okay

  blackBox(d.ref(boid)); // <- could mutate `boid`
  // boid
  // |
  // | (.pos)
  // |
  // ☠️ boidPos

  return boidPos.x; // 🚫 `boidPos` is a stale reference
}
function main() {
  'use gpu';
  const boid = Boid();
  // boid
  //

  const boidPos = boid.pos;
  // boid
  // |
  // | (.pos)
  // |
  // boidPos

  const boidVel = boid.vel;
  // boid
  // |         \
  // | (.pos)   \ (.vel)
  // |           \
  // boidPos      boidVel

  blackBox(d.ref(boid)); // <- could mutate `boid`
  // boid
  // |         \
  // | (.pos)   \ (.vel)
  // |           \
  // ☠️ boidPos   ☠️ boidVel

  return boidPos.x; // 🚫 `boidPos` is a stale reference
}

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

No files or tests are identified in the issue. Start by tracing the TypeScript-to-WGSL reference generation and the existing consumption-verification work mentioned in the proposal. Done means references become inaccessible after the original value may be mutated, while immutable argument use remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.