Skip to content

Conversation

@aleksanderkatan
Copy link
Contributor

@aleksanderkatan aleksanderkatan commented Oct 23, 2025

So, now a warn is thrown when a slot is provided and not used!
Also, I "patched" the bug I talk about in the comment below. Here is an issue for a proper fix: #1854

The cases where I found a false-positive are:

  • slots in a cyclic dependency (in addition to error being thrown, now a warn is displayed)
  • overriden slots (the slots are kind of unused, but it may matter when providing a callback to a library):
    const colorSlot = tgpu.slot<string>(); // no default

    const getColor = tgpu.fn([], d.vec3f)`() -> vec3f {
      return colorSlot;
    }`
      .$uses({ colorSlot });

    const getColorWithGreen = getColor.with(colorSlot, GREEN);

    const main = tgpu.fn([])`() {
      getColorWithGreen();
    }`
      .$uses({ getColorWithGreen })
      .with(colorSlot, GREEN);

Let me know what you think.

@aleksanderkatan aleksanderkatan linked an issue Oct 23, 2025 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented Oct 23, 2025

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/typegpu@a5df776cfdbffb48ead948b2967b227d0a09a5d9
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@a5df776cfdbffb48ead948b2967b227d0a09a5d9
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@a5df776cfdbffb48ead948b2967b227d0a09a5d9

benchmark
view benchmark

commit
view commit

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a warning system to detect and alert developers when slots are provided to functions or pipelines but never actually used during resolution. The implementation tracks slot usage through the resolution context and emits console warnings for unused slots.

Key Changes:

  • Enhanced slot tracking to differentiate between bound and used slots
  • Added warning messages when slots are provided via .with() but remain unused
  • Removed an unused slot binding from the fluid simulation example

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/typegpu/src/resolutionCtx.ts Core implementation: added usedSet tracking and warning logic for unused slots
packages/typegpu/tests/slot.test.ts Comprehensive test coverage for the warning feature across different scenarios
apps/typegpu-docs/src/examples/simulation/fluid-double-buffering/index.ts Cleanup: removed unused inputGridSlot binding

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@aleksanderkatan aleksanderkatan marked this pull request as ready for review October 23, 2025 11:54
@aleksanderkatan aleksanderkatan marked this pull request as draft October 23, 2025 12:52
@aleksanderkatan
Copy link
Contributor Author

I'm pretty sure I found a small bug in the resolution code. Take a look at these two snippets:

resolutionCtx.ts

  unwrap<T>(eventual: Eventual<T>): T {
    if (isProviding(eventual)) {
      return this.withSlots(
        eventual[$providing].pairs,
        () => this.unwrap(eventual[$providing].inner) as T,
      );
    }

tgpuFn.ts

    with(
      slot: TgpuSlot<unknown> | TgpuAccessor,
      value: unknown,
    ): TgpuFn<ImplSchema> {
      return createBoundFunction(fn, [
        ...pairs,
        [isAccessor(slot) ? slot.slot : slot, value],
      ]);
    },

I think that if we call .with multiple times, like:

    const getSize = tgpu.fn([], d.f32)`...`
      .$uses({ ... })
      .with(sizeSlot, 1)
      .with(colorSlot, RED)
      .with(shapeSlot, 2);

then withSlots is called with [sizeSlot, colorSlot, shapeSlot], then with [sizeSlot, colorSlot], then with [sizeSlot].

@aleksanderkatan aleksanderkatan marked this pull request as ready for review October 23, 2025 15:04
Copy link
Contributor

@reczkok reczkok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

impr: Optimize slot resolution impr: Warn when performing .with with a slot that is unused

4 participants