Skip to content

wgsl: detailed semantics of integer division and remainder - #1830

Merged
dneto0 merged 3 commits into
gpuweb:mainfrom
dneto0:issue-1774-signed-division
Jan 25, 2022
Merged

dneto0 merged 3 commits into
gpuweb:mainfrom
dneto0:issue-1774-signed-division

Conversation

@dneto0

@dneto0 dneto0 commented Jun 10, 2021 •

Copy link
Copy Markdown
Contributor

Adds definition for truncate

For divide by zero and signed integer division overlow, say they
produce a "dynamic error".

Fixes: #1774


Update 2022-01-18: Group wants to avoid dynamic error on divide by zero. So now the PR specifies definite answers for divide by zero and % by zero.

@dneto0 dneto0 added the wgsl WebGPU Shading Language Issues label Jun 10, 2021
@dneto0 dneto0 added this to the MVP milestone Jun 10, 2021
@dneto0
dneto0 requested review from alan-baker, kvark and litherum June 10, 2021 02:42
@dneto0

dneto0 commented Jun 10, 2021

Copy link
Copy Markdown
Contributor Author

This is a bit mealy-mouthed about divide by zero and the overflow case.
It says these produce a "dynamic error", but we haven't said what that entails.

Comment thread wgsl/index.bs Outdated
to perform more operations than when computing an unsigned division.
Use unsigned division when both operands are known to have the same sign.

Produces a [=dynamic error=] when |e2| evaluates to zero.

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.

so we can say "produces a dynamic error if e1 is the most negative value" as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So that's something we can debate. If the polyfill is used then no exception is required. Let's discuss this next week.

@kdashg kdashg changed the title wgsl: detailed semantics of integer division and remander wgsl: detailed semantics of integer division and remainder Jun 15, 2021
@litherum

Copy link
Copy Markdown
Contributor

Metal Shading Language spec in section 3.1:

Division on integer types that result in a value that lies outside of the range bounded by the maximum and minimum representable values of the integer type, such as TYPE_MIN/-1 for signed integer types or division by zero, does not cause an exception but results in an unspecified value.

@kdashg

kdashg commented Jun 15, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-06-15
  • (PR for signed integer division)
  • (For INT_MIN / -1 makes it a defined result (yielding INT_MIN), assuming the polyfill was used. Essentially forces the use of the polyfill)
  • MM: If backend APIs treat this as UB, then implementations will need extra care here. I guess if 2 of 3 of the backends need to detect this UB, then WGSL should define it both for consistency and because the implementation will have to guard against this anyway.
  • DM: ??
  • MM: This is related to a later discussion (Buffer indices should be unsigned #1135) because if the compiler identifies UB, it can do bad things.
  • MM: I see this as two steps:
      1. Our implementations must ensure that we don’t translate WGSL into an underlying shading language in a way that invokes UB troubles
      1. If we’re paving over the UB concerns in implementations, we should just standardize on a single result
  • ...
  • DN: In the two step arg, step 2 requires betting that our choice of definition will always be what we want. That is, maybe a different result becomes more efficient in the future.
  • DN will file a separate issue about the details of this general discussion

@dneto0

dneto0 commented Jun 25, 2021

Copy link
Copy Markdown
Contributor Author

We can avoid undefined behaviour on unsigned integer division-by-zero if you replace

a / b   

with

a / clamp(b,1,b);

@kvark

kvark commented Jul 7, 2021

Copy link
Copy Markdown
Contributor

clamp(b,1,b);

Is this a fancy way to write max(b, 1u)? :)

@dneto0

dneto0 commented Aug 6, 2021

Copy link
Copy Markdown
Contributor Author

From 2021-08-03, @jdashg suggested defining: x/ 0 = x

@dneto0

dneto0 commented Aug 6, 2021

Copy link
Copy Markdown
Contributor Author

To put the idea more concretely, if we want x/0 = x, then you have to do the select on the divisor to avoid the 0 case:

x / y  =    x / select(y,1,  y==0);   

It's incorrect to do the select on the division result because you'd still invoke the undefined behaviour in underlying languages. That is, this is the wrong solution: select(x/y, x, y==0) // still does the bad case.

@kvark

kvark commented Aug 7, 2021

Copy link
Copy Markdown
Contributor

Are there alternatives here? I.e. could you think of a different polyfill that is perhaps more efficient than a select?

@dneto0

dneto0 commented Aug 9, 2021

Copy link
Copy Markdown
Contributor Author

Are there alternatives here? I.e. could you think of a different polyfill that is perhaps more efficient than a select?

I think conditional select is pretty cheap.

To confirm that, I found that it's a native instruction in:

@kdashg

kdashg commented Aug 9, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-08-03
  • JG: Let’s just pick x/0=x for signed or unsigned for now, until someone has a better idea.

@kdashg

kdashg commented Oct 26, 2021

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2021-10-26
  • MM: I think JS is well-defined there, so we should consider following that.
  • JG:
  • MM: Two aspects, implementability and consistency.
  • JB: JS is IEEE, even for integers, so e.g. x/0=+inf, 0/0=NaN
  • MM: WGSL doesn’t say that NaNs are handled
  • JG: What about infinity?
  • AB: Also not guaranteed
  • MM: It’s more important that the spec says something, rather than nothing yet.
  • JB: I see the spec says we have IEEE, and inf and nan. Is that wrong?
  • AB: You can load them, but operations are allowed to assume that there aren’t inf and nan.
  • JB: Can we then just leave it IEEE, and [let the wheels fall off naturally]?
  • AB: Do we want to handle 0/0 different from x/0?
  • MM: Should probably use the same result for INT_MIN/-1 (overflow) too?
  • MM: For the rarer cases, we can probably change it later and people are unlikely to notice.
  • Up to DN for whether to change existing PR
  • Resolved to try to pick results rather than call it an error.

@dneto0

dneto0 commented Jan 18, 2022

Copy link
Copy Markdown
Contributor Author

Also would fix #2187 which requests changing the semantics from SPIR-V OpSMod to OpSRem. If I read correctly, that is already included in this proposal.

@dneto0
dneto0 force-pushed the issue-1774-signed-division branch from a801d88 to da9ef30 Compare January 18, 2022 20:01
@dneto0

dneto0 commented Jan 18, 2022

Copy link
Copy Markdown
Contributor Author

Only did a rebase

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (da9ef30):
WebGPU | IDL
WGSL
Explainer

@dneto0

dneto0 commented Jan 18, 2022

Copy link
Copy Markdown
Contributor Author

The group wanted to avoid dynamic error on divide by zero. So now the PR specifies definite answers for divide by zero and % by zero.

@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (79b616f):
WebGPU | IDL
WGSL
Explainer

Comment thread wgsl/index.bs Outdated
<!--
where Divisor = select(e2, 1, e2==0)
where MINIT = most negative value in |T|
select( truncate(e1/Divisor), MININT , (MININT==e1) && (Divisor == -1))

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.

That's a bit unfortunate. It's a complex condition per element of division that's meant to handle a case that is extremely rare (unlike, say, division by zero).

Adds definition for `truncate`

For divide by zero and signed integer division overlow, say they
produce a "dynamic error".

Fixes: gpuweb#1774
This pins down the result for both division and remainder.
These are no longer "dynamic errors".

For integer division:   e1 / 0 = e1

For integers,           e1 % 0 = 0

The % case is somewhat arbitrary, but it makes this true:
    e1 = (e1 / 0) + (e1 % 0)

Another way of formulating the signed integer cases is to forcibly
use a divisor of 1 in the edge cases:
     where MINIT = most negative value in |T|
     where Divisor = select(e2, 1, (e2==0) | ((e1 == MININT) & (e2 == -1)))
then
     "e1 / e2" = truncate(e1 / Divisor)
     "e1 % e2" = e1 - truncate(e1/Divisor) * Divisor

The unsigned integer case is similar but doesn't have (MININT,-1) case.
@dneto0
dneto0 force-pushed the issue-1774-signed-division branch from 79b616f to 52563eb Compare January 25, 2022 21:01
@dneto0

dneto0 commented Jan 25, 2022

Copy link
Copy Markdown
Contributor Author

Discussed in 2022-01-25 meeting

Agreed to land as proposed, after fixing merge conflict.

@dneto0

dneto0 commented Jan 25, 2022

Copy link
Copy Markdown
Contributor Author

In the meeting @kdashg suggested we make sure that the / and % formulations pick the quotient(divisor) in the same way.

I checked, and the proposed rules do pick them the same way. In fact, both edge case conditions end up selecting the divisor the same way:

Another way of formulating the signed integer cases is to forcibly
use a divisor of 1 in the edge cases:
     where MINIT = most negative value in |T|
     where Divisor = select(e2, 1, (e2==0) | ((e1 == MININT) & (e2 == -1)))
then
     "e1 / e2" = truncate(e1 / Divisor)
     "e1 % e2" = e1 - truncate(e1/Divisor) * Divisor

The unsigned integer case is similar but doesn't have (MININT,-1) case.

So there is only one select required, but its condition is a combination of equality checks. That seems nicer than having nested selects.

@dneto0
dneto0 merged commit 277148f into gpuweb:main Jan 25, 2022
github-actions Bot added a commit that referenced this pull request Jan 25, 2022
SHA: 277148f
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Previews, as seen when this build job started (52563eb):
WebGPU | IDL
WGSL
Explainer

github-actions Bot added a commit that referenced this pull request Jan 25, 2022
SHA: 277148f
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
github-actions Bot added a commit that referenced this pull request Jan 25, 2022
SHA: 277148f
Reason: push, by @dneto0

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@kdashg

kdashg commented Jan 26, 2022

Copy link
Copy Markdown
Contributor
WGSL meeting minutes 2022-01-25
  • DN: two exceptional cases: MIN_INT/-1 and div-by-0. Spec’d both in favor of portability. Polyfill would use two selects
  • MM: how would the MIN_INT case work without 64-bit ints. Can see how you’d pack into 64-bit, what if you don’t have it?
  • DN: two equalities and a boolean and as the condition of the select.
  • MM: just change denominator to 1? Would like to name the specific result you get. Happy with any branch-less polyfill
  • DN: think it names the value. This PR also changes modulus in consistent manner.
  • DM: is this worth it? INT_MIN case seems extra exceptional. Would hate this.
  • RM: would be ok if value was undefined, but not ok with UB.
  • KG: is part of the intuition that integer division is already expensive?
  • MM: yes, also rare. So making it worse isn’t a bad outcome for more well-defined language. Worthwhile to have integers fully defined.
  • DM: is it real UB?
  • DN: yes in C++
  • AB: and SPIR-V
  • DM: is it a security problem?
  • RM: think since it is based on C++, in metal it could be
  • MM: optimizations could exploit this and elide most of the functionality dependent on it.
  • JP: MSL spec says unspecified value. Doesn’t cause an exception.
    MSL spec says:
  • "Division on integer types that result in a value that lies outside of the range bounded by the maximum and minimum representable values of the integer type, such as TYPE_MIN/-1 for signed integer types or division by zero, does not cause an exception but results in an unspecified value."
  • GR: could we make the polyfill obvious to change per platform? Know how I’d do it in DXIL
  • DM: can we make WGSL say undefined value?
  • MM: still think portability is important, even if security is not a problem.
  • DM: think it’s about tradeoffs
  • DN: ok with undefined value
  • KG: ok because it’s integer division
  • MM: could see this in a fast math extension
  • JB: read most denominators are constant. Doubt this is expensive
  • Consensus leave as fully defined
  • KG: Would be nice to select the divisors in the same way.

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

Labels

wgsl WebGPU Shading Language Issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

signed integer division: unspecified rounding mode for the quotient; and overflow

5 participants