<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 11/16/2016 7:28 AM, Timofeev,
Alexander via llvm-dev wrote:<br>
</div>
<blockquote
cite="mid:0C4100CBD5B5A044A27924623841B2A72F97A5BC@SATLEXCHOV01.amd.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal">Hi All,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m writing to ask for the suggestion.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m working on the AMDGPU project. I was
recently analyzing our backend performance looking for
optimization opportunities and found that LoadStore vectorizer
is unable to handle simple case.<o:p></o:p></p>
<p class="MsoNormal">The deeper look revealed that the reason
was in a i64 to i32 truncation.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The OpenCL code looks like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">__kernel void read_linear(__global float
*input,__global float *output)<o:p></o:p></p>
<p class="MsoNormal">{<o:p></o:p></p>
<p class="MsoNormal"> float val = 0.0f;<o:p></o:p></p>
<p class="MsoNormal"> uint gid = get_global_id(0);<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> val = val + input[gid + 0];<o:p></o:p></p>
<p class="MsoNormal"> val = val + input[gid + 1];<o:p></o:p></p>
<p class="MsoNormal"> val = val + input[gid + 2];<o:p></o:p></p>
<p class="MsoNormal"> val = val + input[gid + 3];<o:p></o:p></p>
<p class="MsoNormal"> val = val + input[gid + 4];<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">*** and so on… ***<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> output[gid] = val;<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’d expect loads to be combined since
they all have same base and offsets are constants and
consecutively increasing.<o:p></o:p></p>
<p class="MsoNormal">That is exactly what happens if I change
‘uint’ to ‘ulong’ in the assignment: uint gid =
get_global_id(0) => ulong gid = get_global_id(0) – loads
are combined as expected.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The reason is simple:<o:p></o:p></p>
<p class="MsoNormal">OpenCL get_global_id(uint) returns i64<o:p></o:p></p>
<p class="MsoNormal">The result is assigned to i32<o:p></o:p></p>
<p class="MsoNormal">Clang generates trunc i64 to i32 as AND
with FFFFFFFF:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">%call = tail call i64
@_Z13get_global_idj(i32 0) #2<o:p></o:p></p>
<p class="MsoNormal"> %idxprom = and i64 %call, 4294967295<o:p></o:p></p>
<p class="MsoNormal"> %arrayidx = getelementptr inbounds float,
float addrspace(1)* %input, i64 %idxprom<o:p></o:p></p>
<p class="MsoNormal"> %0 = load float, float addrspace(1)*
%arrayidx, align 4, !tbaa !7<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, LoadStoreVectorizer cannot combine with
the next load:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> %add2 = add i64 %call, 1<o:p></o:p></p>
<p class="MsoNormal"> %idxprom3 = and i64 %add2, 4294967295<o:p></o:p></p>
<p class="MsoNormal"> %arrayidx4 = getelementptr inbounds
float, float addrspace(1)* %input, i64 %idxprom3<o:p></o:p></p>
<p class="MsoNormal"> %1 = load float, float addrspace(1)*
%arrayidx4, align 4, !tbaa !7<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Basically we have a case: C1 & ( A + C2
) => C1 & A + C1 & C2 that is not always legal.<o:p></o:p></p>
<p class="MsoNormal">In our case that really is trunc(A+C) =>
trunc(A) + trunc(C) that is legal.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">NaryReassociate cannot handle this because
it only considers ADD, MUL, GEP<o:p></o:p></p>
<p class="MsoNormal">Reassociate cannot handle this either just
because it explicitly looks for “<span style="font-size:9.5pt">X&~X
== 0, X|~X == -1.” or “Y ^ X^X -> Y”<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:9.5pt"><o:p> </o:p></span></p>
<p class="MsoNormal">We definitely should be able to optimize
0xFFFFFFFF & ( A + 1 ) to 0xFFFFFFFF & A + 1 given
that bitwise AND with FFFFFFFF is a special case.<o:p></o:p></p>
<p class="MsoNormal">We maybe want be able to enhance bitwise
operation processing to handle more common cases.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</blockquote>
<br>
I'm not sure what you're expecting reassociation to do here. If you
can prove "gid + 1" doesn't wrap in your example (because of some
property of the get_global_id() function), the AND is a no-op, so
you can just kill it. And if you can't prove "gid + 1" doesn't
wrap, your proposed transformation is illegal.<br>
<br>
-Eli<br>
<pre class="moz-signature" cols="72">--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
</body>
</html>