Skip to content

fix: Invalid std.select code generation #1879

@iwoplaza

Description

@iwoplaza

The following code:

const counter = root.createMutable(d.u32);

const incrementFn = (n: number) => {
  "use gpu";
  return std.select(d.u32(0), d.u32(1), n >= 0);
}

const incrementPipeline = root['~unstable'].createGuardedComputePipeline(() => {
  'use gpu';
  counter.$ += incrementFn(counter.$);
});

... generates the following WGSL for the incrementFn function:

fn incrementFn_3(n: u32) -> u32 {
  return select(0, 1, (n >= 0));
}

This crashes the shader generator, as the type of 0 and 1 is inferred to be i32, so the return type is also i32.

Possible cause

While generating code for std.select, we don't stitch with exact types, meaning numeric literals stay abstract (without any type suffix, like u in this case, or a wrapping with u32())

A possible fix for this is just using stitchWithExactTypes instead of stitch in the std.select implementation

This revelation makes the idea of sometimes omitting typed suffixes to save a few characters not worth it. If we always emit type suffixes, it would fix this issue, along with simplifying the resolution process (which is always welcome)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions