<div dir="ltr"><div>I was originally going to cover this in my now defunct EuroLLVM talk but... we had this exact same problem on Unity's HPC# Burst compiler - how to track no-aliasing on structs. We were constrained in that we had to make it work with LLVM versions all the way back to shipped LLVM 6, so what we did was:</div><div><ul><li>Add module-level metadata that tracked whether a given struct member field was no-alias.</li><li>Added our own alias analysis using createExternalAAWrapperPass to register it in the pass pipeline.</li></ul><div>This allowed us to have zero modifications to LLVM and do something useful with aliasing. The one 'issue' with it is if you have a stack-allocated struct that is SROA'ed you will lose the info that it was a struct, or if you are in a private/internal linkage function that has the struct as an argument, the opt passes can modify the function signature to lose the struct too. We had to do some mitigations here to get perfect aliasing on our usecases.</div><div><br></div><div>Hope this helps,</div><div>-Neil.<br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 22, 2020 at 5:44 AM Michael Kruse via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Unfortunately <a href="https://llvm.org/docs/LangRef.html#llvm-loop-parallel-accesses-metadata" rel="noreferrer" target="_blank">https://llvm.org/docs/LangRef.html#llvm-loop-parallel-accesses-metadata</a><br>
is not a solution here. A loop-parallel access does not imply<br>
non-aliasing. The obvious case is when only reading from a location,<br>
but even when a location is written to I'd be careful to deduce that<br>
they do not alias since it might be a "benign data race" or the value<br>
never used. Additionally, LLVM's loop unroller is known to now handle<br>
noalias metadata correctly as it just copies it.<br>
<br>
There has been a discussion here:<br>
<a href="http://lists.llvm.org/pipermail/llvm-dev/2020-May/141587.html" rel="noreferrer" target="_blank">http://lists.llvm.org/pipermail/llvm-dev/2020-May/141587.html</a><br>
<br>
Michael<br>
<br>
<br>
Am So., 21. Juni 2020 um 12:24 Uhr schrieb Johannes Doerfert via<br>
llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>:<br>
><br>
> Hi Bandhav,<br>
><br>
><br>
> Jeroen Dobbelaere (CC'ed) is currently working on support for restrict qualified local variables and struct members.<br>
><br>
> The patches exist but are not merged yet. If you want to give it a try apply <a href="https://reviews.llvm.org/D69542" rel="noreferrer" target="_blank">https://reviews.llvm.org/D69542</a>.<br>
><br>
><br>
> Initially I could only think of this solution for your problem: <a href="https://godbolt.org/z/6WtPXJ" rel="noreferrer" target="_blank">https://godbolt.org/z/6WtPXJ</a><br>
><br>
> Michael (CC'ed) might now another annotation to get `llvm.access` metadata for the loop, which should do what you intend.<br>
><br>
><br>
> Cheers,<br>
><br>
>   Johannes<br>
><br>
><br>
> On 6/21/20 11:56 AM, Bandhav Veluri via llvm-dev wrote:<br>
><br>
> Hi,<br>
><br>
> I'm trying to abstract some special pointers with a class, like in the<br>
> example program below:<br>
><br>
>   1 #define __remote __attribute__((address_space(1)))<br>
>   2 #include <stdint.h><br>
>   3<br>
>   4 __remote int* A;<br>
>   5 __remote int* B;<br>
>   6<br>
>   7 class RemotePtr {<br>
>   8   private:<br>
>   9     __remote int* __restrict a;<br>
>  10<br>
>  11   public:<br>
>  12     RemotePtr(__remote int* a) : a(a) {}<br>
>  13<br>
>  14     __remote int& at(int n) {<br>
>  15       return a[n];<br>
>  16     }<br>
>  17 };<br>
>  18<br>
>  19 int main(int argc, char** argv) {<br>
>  20   RemotePtr a(A);<br>
>  21   RemotePtr b(B);<br>
>  22<br>
>  23   #pragma unroll 4<br>
>  24   for(int i=0; i<4; ++i) {<br>
>  25     <a href="http://a.at" rel="noreferrer" target="_blank">a.at</a>(i) += <a href="http://b.at" rel="noreferrer" target="_blank">b.at</a>(i);<br>
>  26   }<br>
>  27<br>
>  28   return 0;<br>
>  29 }<br>
><br>
> It's given that pointer a, in each object of the class RemotePtr, is the<br>
> only pointer that can access the array pointed by it. So, I tried __remote<br>
> int* __restrict a; (line 9) construct to tell Clang the same. This doesn't<br>
> seem to work and I see no noliass in the generated IR. Specifically, I want<br>
> lines 23-26 optimized assuming no aliasing between A and B. Any reason why<br>
> Clang shouldn't annotate memory accesses in lines 23-26 with noaliass<br>
> taking line 9 into account?<br>
><br>
> The higher level problem is this: is there a way to compile lines 23-26<br>
> assuming no aliasing between A and B, by just doing something in the<br>
> RemotePtr class (so that main is clear of ugly code)? If that's not<br>
> possible, is there a way to tell Clang that lines 23-26 should assume no<br>
> aliasing at all, by some pragma?<br>
><br>
> Thank you,<br>
> Bandhav<br>
><br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><table style="border-collapse:collapse;border-spacing:0px;color:rgb(90,90,91);font-size:13px;margin:0px 0px 20px;padding:0px" width="100%" cellspacing="0" cellpadding="0" border="0"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px 0px 20px;vertical-align:top" align="left"><table style="border-collapse:collapse;border-spacing:0px;margin:0px;padding:0px" cellspacing="0" cellpadding="0" border="0" align="left"><tbody style="margin:0px;padding:0px"><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:1.12em;line-height:1.5em;padding:0px;vertical-align:top;width:64px"><img style="border: medium none; border-radius: 0px; display: block; font-size: 13px; height: auto; line-height: 100%; margin: 0px; max-width: 100%; outline-style: none; outline-width: medium; padding: 20px 0px 0px; width: 100%;" alt="" src="https://unity3d.com/profiles/unity3d/themes/unity/images/ui/other/unity-logo-dark-email.png" width="64" height="auto"></td></tr></tbody></table></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;font-weight:600;line-height:15px;margin:0px;padding:0px">Neil Henning</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:14px;line-height:15px;margin:0px;padding:0px 0px 10px">Senior Software Engineer Compiler</div></td></tr><tr style="margin:0px;padding:0px"><td style="border-collapse:collapse;font-size:0px;line-height:1.5em;padding:0px;vertical-align:top" align="left"><div style="color:rgb(0,0,0);font-family:Roboto,Arial;font-size:12px;line-height:15px;margin:0px;padding:0px"><a href="http://unity.com" target="_blank">unity.com</a></div></td></tr></tbody></table></div></div>