<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 14 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p
{mso-style-priority:99;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
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]--></head><body lang=EN-US link=blue vlink=purple><div class=WordSection1><p class=MsoNormal>Hi,<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>There are two patches attached can achieve this goal:<o:p></o:p></p><p class=MsoNormal> LoopVectorize-InterleaveAccess.patch teaches Loop Vectorizer about interleaved data access and generate target independent intrinsics for each load/store:<o:p></o:p></p><p class=MsoNormal> AArch64Backend-MatchIntrinsics.patch match several target independent intrinsics into one AArch64 ldN/stN intrinsics, so that AArch64 backend can generate ldN/stN instructions.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Currently, LoopVectorize can vectorize consecutive accesses well. It can vectorize loops like<o:p></o:p></p><p class=MsoNormal> for (int i = 0; i < n; i++)<o:p></o:p></p><p class=MsoNormal> sum += R[i];<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>But it doesn't handle strided access well. Interleaved access is a subset of strided access. Example for interleaved access:<o:p></o:p></p><p class=MsoNormal> for (int i = 0; i < n; i++) {<o:p></o:p></p><p class=MsoNormal> int even = A[2*i];<o:p></o:p></p><p class=MsoNormal> int odd = A[2*i + 1];<o:p></o:p></p><p class=MsoNormal> // do something with odd & even. <o:p></o:p></p><p class=MsoNormal> }<o:p></o:p></p><p class=MsoNormal>To vectorize such case, we need two vectors: one with even elements, another with odd elements. To gather even elements, we need several scalar loads for "A[0], A[2], A[4], ...", and several INSERT_ELEMENTs to combine them together. The cost is very high and will usually prevent loop vectorization on such case. Some backend like AArch64/ARM support interleaved load/store: ldN/stN (N is 2/3/4). And I know X86 can also support similar operations. One ld2 can load two vectors: one is with even elements, another is with only odd elements. So that this case can be vectorized into AArch64 instructions:<o:p></o:p></p><p class=MsoNormal> LD2 { V0, V1 } [X0]<o:p></o:p></p><p class=MsoNormal> // V0 contains even elements. Do something related to even elements with V0.<o:p></o:p></p><p class=MsoNormal> // V1 contains odd elements. Do something related to odd elements with V1.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>1. Design<o:p></o:p></p><p class=MsoNormal>My design is to follow current Loop Vecotirzer three phases.<o:p></o:p></p><p class=MsoNormal>(1) Legality Phase:<o:p></o:p></p><p class=MsoNormal> (a). Collect all the constant strided accesses except consecutive accesses. <o:p></o:p></p><p class=MsoNormal> (b). Collect the load/store accesses with the same Stride, Base pointer. <o:p></o:p></p><p class=MsoNormal> (c). Fine the consecutive chains in (b). If the number of accesses in one chain are equal to the Stride, they are interleaved accesses.<o:p></o:p></p><p class=MsoNormal>Example for the case about even and odd. We can find two loads for even and odd elements. The strides are both 2. They are also consecutive. So they are recorded as interleaved accesses.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>(2) Profitability Phase:<o:p></o:p></p><p class=MsoNormal> Add a target hook to calculate the cost. Currently the cost is 1. Currently this won't affect to much about the result. So I didn't do too much work in this phase.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>(3) Transform Phase: <o:p></o:p></p><p class=MsoNormal> As there is no IR for interleaved data, I think we should use intrinsics. The problem is that the relationship is "N to one". I.E. Several loads/stores to one ldN/stN instructions. There is already ldN/stN intrinsics in AArch64/ARM backend such llvm.aarch64.neon.ldN, which is like "call { <4 x i32>, <4 x i32>} llvm.aarch64.neon.ld2.v4i32()". In the middle end, there are two IR loads. <o:p></o:p></p><p class=MsoNormal><o:p></o:p></p><p class=MsoNormal>Need to think a way to match two loads to one target specific intrinsic. I think there are two ways:<o:p></o:p></p><p class=MsoNormal> (a). Two steps for middle end and backend. 1st step is to match each loads/stores to one target independent intrinsic in the loop vectorize. 2nd step is to match several intrinsics into one ldN/stN intrinsic. This is the choise of my attached patch. For the above odd-even example, it will generate two intrinsics in the loop vectorization: <o:p></o:p></p><p class=MsoNormal> "%even-elements = call <4 x i32> @llvm.interleave.load.v4i32", <o:p></o:p></p><p class=MsoNormal> "%odd-elements = call <4 x i32> @llvm.interleave.load.v4i32".<o:p></o:p></p><p class=MsoNormal>A backend pass will combine them together into one intrinsic:<o:p></o:p></p><p class=MsoNormal> "%even-odd-elements = call { <4 x i32>, <4 x i32> } @llvm.aarch64.neon.ld2.v4i32"<o:p></o:p></p><p class=MsoNormal>But I think the backend pass is vulnerable and diffecult to implement. It will fail to combine if one load is missing, or if one load is moved to another basic block. Also I haven't check about memory dependency.<o:p></o:p></p><p class=MsoNormal> (b). One step only for middle end. We can match several load/stores into one ldN/stN like target independent intrinsic. So that the AArch64/ARM backend only needs slight modification on replacing the current used intrinsic to the new independent intrinsic. This needs to introduce a new intrinsic such as "{ <4 x i32>, <4 x i32>} llvm.interleaved.load.v4i32()".<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal> Actually I prefer solution (b), which is easier to be implemented and stronger than solution (a). But solution (a) seems more target independent. How do you guys think?<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>2. Test<o:p></o:p></p><p class=MsoNormal>I've test the attached patch with llvm-test-suit, SPEC2000, SPEC2006, EEMBC, Geekbench on AArch64 machine. They all can pass.<o:p></o:p></p><p class=MsoNormal>But the performance is not affected. Some specific benchmarks like EEMBC rgbcmy and EEMBC rgbyiq are expected to have several times of acceleration. The reason is that there are other issues prevent vectorization opportunities. Some known issues are:<o:p></o:p></p><p class=MsoNormal> (1). Too many unnecessary runtime checkings (The interleaved accesses are compared with each other).<o:p></o:p></p><p class=MsoNormal> (2). Store-Load Forward checking (Doesn't consider about strided accesses).<o:p></o:p></p><p class=MsoNormal> (3). Type promotion issue (i8 is illegal but <16 x i8> is legal. i8 is promoted to i32 so the extend and truncate operations increase the total cost).<o:p></o:p></p><p class=MsoNormal> (4). The Vector Factor is selected according to the widest type. (If there are both i8 and i32, we select a small factor according to i32 rather than according to i8).<o:p></o:p></p><p class=MsoNormal>Anyway. We can fix them in the future and get performance improvements.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>What's your oppions on the solution? I'm still hesitating about the transform phase.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Thanks,<o:p></o:p></p><p class=MsoNormal>-Hao<o:p></o:p></p></div></body></html>