Bug 198198 - [WHLSL] Make sure we properly emit code for "&*x"
Summary: [WHLSL] Make sure we properly emit code for "&*x"
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebGPU (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Saam Barati
URL:
Keywords: InRadar
: 197448 (view as bug list)
Depends on:
Blocks: 198399
  Show dependency treegraph
 
Reported: 2019-05-23 15:16 PDT by Saam Barati
Modified: 2019-05-31 11:17 PDT (History)
7 users (show)

See Also:


Attachments
patch (10.62 KB, patch)
2019-05-31 01:56 PDT, Saam Barati
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Saam Barati 2019-05-23 15:16:08 PDT
...
Comment 1 Saam Barati 2019-05-23 15:16:54 PDT
int foo(thread Bar* bar) {
    return bar->x;
}

turns into

int foo(thread Bar* bar) {
   return *operator&.x(&*bar);
}

the "&*bar" is suspect.
Comment 2 Myles C. Maxfield 2019-05-23 15:32:22 PDT
Yeah, the parser turns bar->x into (*bar).x, then the property resolver reduces the dot expression via the ander. The ander requires a pointer, so we get one by calling &. The checker detects that (*bar) is an lvalue, so we think it's legal to say &(*bar)
Comment 3 Saam Barati 2019-05-23 16:06:02 PDT
Actually, after Robin and Myles thought about it, it seems like it's correct. "*x" is an lvalue if x is an lvalue.

I guess we sort of need to treat "*x" like C++ treats references. Not sure how far this will extend in terms of implementation. But there's a chance all we need is an AST rewrite rule since we can't variables with reference types.
Comment 4 Saam Barati 2019-05-23 16:06:32 PDT
As Myles suggested, a good test would be
&*p == p => true
Comment 5 Myles C. Maxfield 2019-05-23 16:30:17 PDT
One way to do this would be a pass that cancels out adjacent & and * operators. But that may not be sufficient.
Comment 6 Myles C. Maxfield 2019-05-24 09:36:48 PDT
For &*x, the emitted Metal code right now will be

auto temp = *x;
auto temp2 = &temp;

Which is wrong. Perhaps we could modify the Metal codegen to instead emit

auto& temp = *x;
auto temp2 = &temp;
Comment 7 Myles C. Maxfield 2019-05-24 09:40:45 PDT
If we take this approach, it may actually solve https://bugs.webkit.org/show_bug.cgi?id=197448.

*x = 4;

Would get compiled to

auto& temp = *x;
temp = 4;

Which I think is correct
Comment 8 Saam Barati 2019-05-24 09:58:40 PDT
(In reply to Myles C. Maxfield from comment #7)
> If we take this approach, it may actually solve
> https://bugs.webkit.org/show_bug.cgi?id=197448.
> 
> *x = 4;
> 
> Would get compiled to
> 
> auto& temp = *x;
> temp = 4;
> 
> Which I think is correct

Looks like it’d be correct. Though we’d need to make sure to selectively emit references since I’m assuming “auto& temp = 42” is invalid metal
Comment 9 Saam Barati 2019-05-24 09:59:28 PDT
(In reply to Saam Barati from comment #8)
> (In reply to Myles C. Maxfield from comment #7)
> > If we take this approach, it may actually solve
> > https://bugs.webkit.org/show_bug.cgi?id=197448.
> > 
> > *x = 4;
> > 
> > Would get compiled to
> > 
> > auto& temp = *x;
> > temp = 4;
> > 
> > Which I think is correct
> 
> Looks like it’d be correct. Though we’d need to make sure to selectively
> emit references since I’m assuming “auto& temp = 42” is invalid metal

Oooh. I think you mean just for dereference nodes!
Comment 10 Saam Barati 2019-05-31 01:56:40 PDT
Created attachment 371046 [details]
patch
Comment 11 Myles C. Maxfield 2019-05-31 09:12:13 PDT
*** Bug 197448 has been marked as a duplicate of this bug. ***
Comment 12 WebKit Commit Bot 2019-05-31 11:16:51 PDT
Comment on attachment 371046 [details]
patch

Clearing flags on attachment: 371046

Committed r245973: <https://trac.webkit.org/changeset/245973>
Comment 13 WebKit Commit Bot 2019-05-31 11:16:52 PDT
All reviewed patches have been landed.  Closing bug.
Comment 14 Radar WebKit Bug Importer 2019-05-31 11:17:22 PDT
<rdar://problem/51308436>