Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

RobinMorisset
Copy link
Contributor

Inspired by #2204.
It has not been discussed by the group yet, and thus should not be merged.

<td>|e1| = |e1| << |e2|
</table>

Note: in all of the cases above, |e1| is guaranteed by a combination of the syntax and typing rules to be composed of some optional sequence of `*` and `&` followed by an identifier.
Copy link
Contributor

Choose a reason for hiding this comment

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

I dislike that the constraints on the LHS are unlike for regular assignment.

Instead, I would rather that the LHS is a reference that could be on a regular assignment, and whose store type allows the operation.

And then the translation for

LHS op= RHS

would be

{ let p = &LHS;
*p = *p op RHS }

Then there is no concern for issuing side effects multiple times.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm.. Except for the component-of-a-vector case.

      var v = vec4<f32>();
      v.y += 1.0;

We can't take the address of a component of a vector. :-(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only difference in syntactic constraints with regular assignment is that _ is not allowed on the LHS side.
The meaningful constraints are in terms of type, and they flow naturally from the fact that the expression on the LHS must also be a valid left-operand to the operator.

My hope was that by making complex assignments mere syntactic sugar I could both minimize implementers' burden, and make it conceptually easy for programmers.

Copy link
Contributor

Choose a reason for hiding this comment

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

The only difference in syntactic constraints with regular assignment is that _ is not allowed on the LHS side.

Maybe I was confused.

This PR has the LHS as unary_expression. That might work, but I'd see it more clearly as similar to regular assignment if it uses lhs_expression. I kind of got lost tracing back through unary_expression to see what it expands to.

@dneto0 dneto0 added the wgsl WebGPU Shading Language Issues label Nov 3, 2021
@kdashg kdashg changed the title Add complex assignments Add complex assignments (e.g. +=) Nov 8, 2021
@RobinMorisset
Copy link
Contributor Author

After discussing it with Myles, I realized that my proposal could lead to duplicated work in the following case:

a[f()] += 1;

Since David correctly noted that taking the address of the LHS fails for vector component, this leaves no easy solution (we would need to do something different depending on the type of the LHS), and I no longer think that this is appropriate for V1.

So I am cancelling the PR.

@Kangz
Copy link
Contributor

Kangz commented Nov 9, 2021

Isn't a simple fix to disallow the compound operators on vector elements in v1?

@jrprice
Copy link
Contributor

jrprice commented Nov 9, 2021

I'm not sure that I understand why this is hard to spec. I don't think we need to try and come up with an equivalent WGSL translation, we just need to say something like:

e1 += e2 is equivalent to e1 = e1 + e2 , where the expression e1 is only evaluated once.

This is how compound assignment is described in the C/C++ standards as well.

This seems very low cost to spec, test, and implement, and has clear benefits for shader authors who would expect this work (HLSL, GLSL, and MSL all have this).

@dneto0
Copy link
Contributor

dneto0 commented Nov 9, 2021

Interacts with #2261

@kdashg
Copy link
Contributor

kdashg commented Nov 10, 2021

WGSL meeting minutes 2021-11-09
  • (RM: Cancelled: it is not trivial sugar after all, so I am delaying until after V1.)
  • DN: James wants to revive.
  • JP: Will take it over.

@kvark
Copy link
Contributor

kvark commented Nov 24, 2021

Also unclear to me what went wrong here. Previously, when we discussed v.x = 1 case, I noted that this can be a difference between "taking a pointer" (which isn't allowed) and "taking a reference" (which is allowed). Is "v.x += 1" non portable? That would be surprising :)

@dneto0
Copy link
Contributor

dneto0 commented Nov 25, 2021

Also unclear to me what went wrong here. Previously, when we discussed v.x = 1 case, I noted that this can be a difference between "taking a pointer" (which isn't allowed) and "taking a reference" (which is allowed). Is "v.x += 1" non portable? That would be surprising :)

You're right. I think James pointed that out in the meeting. That you can have a reference to a component of a vector, which is why v.x = 1; works as a statement.
Note that this still suffers from the fact that writing one component is considered to touch all the components; the practical effect is that different invocations can race when writing to different components of the same vector. #2152

Like @jrprice said above: This feature can and should be rescued by stating the semantics directly, not as a translation to some other WGSL that uses pointers.
That's what I did with #2342

I have time today so I can reframe this PR in that way.

@kvark
Copy link
Contributor

kvark commented Nov 25, 2021

This feature can and should be rescued by stating the semantics directly, not as a translation to some other WGSL that uses pointers.

Can it be described in terms of references?

@dneto0
Copy link
Contributor

dneto0 commented Nov 25, 2021

Can it be described in terms of references?

Yes. But the reference has to be evaluated only once. So it's not a pure syntactic sugar replacement.

PTAL #2345

@Kangz
Copy link
Contributor

Kangz commented Dec 22, 2021

Closing since this has been done in #2345

@Kangz Kangz closed this Dec 22, 2021
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.

6 participants