Skip to content

fix: tgpu.unroll removing ranges ending at zero - #3040

Open
vende11s wants to merge 2 commits into
mainfrom
fix/unroll-range-ending-at-zero
Open

vende11s wants to merge 2 commits into
mainfrom
fix/unroll-range-ending-at-zero

Conversation

@vende11s

Copy link
Copy Markdown
Collaborator

before fix:

  it('', () => {
    const fn = () => {
      'use gpu';
      let res = 0;
      for (const i of tgpu.unroll(std.range(3, 0, -1))) {
        res += i;
      }
      return res;
    };

    expect(tgpu.resolve([fn])).toMatchInlineSnapshot(`
      "fn fn_1() -> i32 {
        let res = 0;
        return res;
      }"
    `);
  });

Copilot AI lite review requested due to automatic review settings September 18, 2026 14:43
@github-actions

github-actions Bot commented Sep 18, 2026

Copy link
Copy Markdown

pkg.pr.new

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

https://pkg.pr.new/software-mansion/TypeGPU/eslint-plugin-typegpu@d167c7b904a3b3060f8777a3225a7349f45fd310
https://pkg.pr.new/software-mansion/TypeGPU/tinyest@d167c7b904a3b3060f8777a3225a7349f45fd310
https://pkg.pr.new/software-mansion/TypeGPU/tinyest-for-wgsl@d167c7b904a3b3060f8777a3225a7349f45fd310
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@d167c7b904a3b3060f8777a3225a7349f45fd310
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@d167c7b904a3b3060f8777a3225a7349f45fd310
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@d167c7b904a3b3060f8777a3225a7349f45fd310

benchmark
view benchmark

commit
view commit

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Add regression coverage for descending ranges ending at zero.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes tgpu.unroll for descending ranges ending at zero.

Changes:

  • Removes the premature zero-endpoint return.
  • Checks materialized elements before omitting empty loops.
File summaries
File Summary Review comment
packages/typegpu/src/tgsl/wgslGenerator.ts Corrects compile-time range unrolling. Add a regression test for std.range(3, 0, -1) and ideally negative endpoints.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/typegpu/src/tgsl/wgslGenerator.ts
@github-actions

github-actions Bot commented Sep 18, 2026

Copy link
Copy Markdown

Resolution Time Benchmark

---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.91, 1.74, 3.79, 6.38, 7.56, 10.91, 22.23, 25.07]
  line [0.94, 1.84, 4.01, 6.21, 6.69, 12.73, 23.83, 22.62]
  line [0.88, 1.83, 3.80, 6.36, 7.08, 11.83, 23.18, 24.30]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.30, 0.49, 0.66, 0.73, 1.03, 1.06, 1.29, 1.43]
  line [0.34, 0.44, 0.68, 0.76, 1.03, 1.15, 1.46, 1.46]
  line [0.33, 0.50, 0.60, 0.74, 1.10, 1.06, 1.28, 1.50]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.91, 1.98, 3.67, 6.46, 11.33, 24.08, 53.82, 110.30]
  line [0.98, 1.95, 3.75, 7.02, 11.49, 25.13, 54.70, 107.80]
  line [0.92, 1.96, 3.50, 6.29, 11.63, 26.19, 54.30, 108.45]
Loading

@github-actions

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 Decreased ➖ Unchanged 🔴 Increased ❔ Unknown
0 325 0 0

No notable changes.

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ℹ️ No critical issues — one minor suggestion. The fix itself is correct and verified.

Reviewed changes

  • Correct empty-range detection in tgpu.unrollpackages/typegpu/src/tgsl/wgslGenerator.ts now builds elements first and returns empty code only when elements.length === 0, instead of bailing when range.end.value === 0. This removes the end-value/element-count conflation that dropped descending ranges ending at zero, and it also avoids the previous undefined elements[0] path for empty ranges whose end is non-zero (e.g. std.range(0, 5, -1)).
  • I verified the PR's case resolves to 3 iterations (3i, 2i, 1i), that std.range(3, -1, -1) resolves to 4, and that std.range(0, 5, -1) / std.range(0) still emit an empty body. The existing 28 tests in packages/typegpu/tests/unroll.test.ts all pass.

ℹ️ Missing regression test

This is a bug fix but ships without a committed test — changed_files: 1, and the failing snippet in the PR description was not added. unroll.test.ts has no descending-range (negative step) case, so nothing pins this behavior and a future refactor could silently reintroduce it.

Technical details
# Add a regression test for descending ranges / zero-end ranges

## Affected sites
- `packages/typegpu/tests/unroll.test.ts` — no `std.range` case with a negative step; existing range tests use `std.range(1, 9, 2)` (L813) and `std.range(1, 10)` (L906).

## Required outcome
- A test that fails against the pre-fix `wgslGenerator.ts` and passes now. The PR description's `std.range(3, 0, -1)` case is ideal (expects unrolled iterations `#0``3i`, `#1``2i`, `#2``1i`).
- Optionally also pin the empty-but-nonzero-end case (`std.range(0, 5, -1)`), which previously crashed on `elements[0]`.

## Suggested approach
- Add an `it(...)` to `packages/typegpu/tests/unroll.test.ts` using the existing `tgpu.resolve([fn])` + `toMatchInlineSnapshot` idiom, alongside the `'unrolls a range'` test (L809).

Pullfrog  | Fix it ➔View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • Added regression test for descending ranges ending at zeropackages/typegpu/tests/tgsl/wgslGenerator.test.ts now pins tgpu.unroll(std.range(3, 0, -1)) to three unrolled iterations (3i, 2i, 1i) plus a CPU main() === 6 assertion.
  • Added regression test for empty ranges with a nonzero endpoint — pins std.range(3, 3) to an empty body and CPU main() === 0, covering the previous elements[0] undefined crash.
  • Both tests were verified to fail against the pre-fix generator (snapshot mismatch for the descending case; TypeError in isAlias for the empty case) and to pass with the fix. The full wgslGenerator.test.ts (104 tests) and unroll.test.ts (28 tests) suites pass.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@cieplypolar cieplypolar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice catch 🚢 🇮🇹

@aleksanderkatan aleksanderkatan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lesgooo

@reczkok reczkok left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yup

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.

5 participants