Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

naga incorrectly throws multiplication operation overflowed error in expressions involving abstract numerics #6023

Closed
matthew-wong1 opened this issue Jul 23, 2024 · 6 comments · Fixed by #6835
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working

Comments

@matthew-wong1
Copy link
Contributor

Description
Hi! I think I've found an issue with the way in which naga evaluates certain expressions involving abstract numerics. Naga throws a multiplication error overflowed for the following expression but Tint does not.

const num : i32 = -1 * i32(-2147483648)

Both tint and naga correctly throw that error for the following expression:

const num : i32 = -1 * -2147483648

Extra materials
I've spoken to some of the WGSL spec maintainers, who kindly provided the following explanation as for why the first expression should not throw an error:

"The first example is: -1 * i32(-2147483648)
That's type-checked as a multiply with abstract-int on the left, and i32 on the right.
There is no overload of the multiply operator with those two type parameters. There's no up-conversion from i32 to abstract int, and the only viable automatic conversion is to convert the LHS from abstract int to i32. This yields i32 * i32. It's two-s complement arithmetic and therefore wrapping occurs.

In the second example, -1 * -2147483648
It's a multiply of two abstract-ints. There's an overload for that, so the result produces another abstract int, of value 2147483648.
That value is a const-expression, and does not fit in i32, and that causes the error."

@teoxoy teoxoy added type: bug Something isn't working naga Shader Translator area: naga front-end lang: WGSL WebGPU Shading Language labels Jul 24, 2024
@ErichDonGubler
Copy link
Member

We definitely don't have correct behavior yet with automatic casting of literals; we expect to be pretty close once #4400 is implemented, modulo bugs (which, admittedly, will likely be similar to this one).

I won't close this bug as a duplicate, unless @gfx-rs/naga has a strong opinion about it.

@jimblandy
Copy link
Member

This actually is not related to type conversions. It's simply that the constant evaluator uses checked_mul for i32 multiplication, but WGSL says that multiplication should just wrap.

@sagudev
Copy link
Contributor

sagudev commented Dec 30, 2024

The spec says:

Expressions on concrete integer types that overflow produce a result that is modulo 2**bitwidth

so I think we need to change all checked_ to wrapped_, at least for (i32, i32).

@matthew-wong1
Copy link
Contributor Author

@sagudev I've now made that change in #6835 😄 I think the only instance I found for checked_mul for (i32, i32) was in constant_evaluator.rs

@sagudev
Copy link
Contributor

sagudev commented Dec 30, 2024

I meant other ops like add, or sub.

@matthew-wong1
Copy link
Contributor Author

Ah my bad, I misunderstood - I'll update my pull request. Thanks!

@github-project-automation github-project-automation bot moved this from Todo to Done in WebGPU for Firefox Jan 6, 2025
jamienicol added a commit to jamienicol/wgpu that referenced this issue Jan 7, 2025
I don't think this is actually about casting of abstract types. we
appear to handle that correctly. it's actually about overflow handling
for non-abstract types during const evaluation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: naga front-end lang: WGSL WebGPU Shading Language naga Shader Translator type: bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants