<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=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><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:等线;
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:"\@等线";
panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
{mso-style-priority:99;
mso-style-link:"纯文本 字符";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.5pt;
font-family:"Calibri",sans-serif;}
span.a
{mso-style-name:"纯文本 字符";
mso-style-priority:99;
mso-style-link:纯文本;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:等线;}
/* Page Definitions */
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.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="ZH-CN" link="#0563C1" vlink="#954F72" style="text-justify-trim:punctuation">
<div class="WordSection1">
<p class="MsoPlainText"><span lang="EN-US">Hi all developers,<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> I'm changing compiler from gcc to llvm on a RISCV target now. but I found in some case the assembly code generated by llvm is much more than gcc. It cause my program's performance about 40% decrease.<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> The flowing is a simple test code. It shows the problem. We can see than gcc prefer to use pointer to iterate the array, but llvm perfere to use index to iterate the array. So llvm generate more codes to calculate
the memory address of an array element from the index. <o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> My question is that if there's an option of llvm, to let it iterate array by pointer. Or it</span><span lang="EN-US" style="font-family:"Courier New"">’</span><span lang="EN-US">s a bug of llvm not resolved now
?<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Test C code:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Int func(int w1, int w2, int *b, int *c) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int wstart = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int i = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int j = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int sum = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> In wend = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int dst_idx = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> Int dst_idx2 = 0;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> for (I = 0; I < w2; i++) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> wstart = i * w1;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> wend = i / w1;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> sum = c[wstart];<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> for (j = wstart + 1; j < wend; j++) {<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> sum += c[j * w2];<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> sum += c[j * w1];<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> dst_idx = w1 * i + w2;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> dst_idx2 = w2 * i + w1;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> b[dst_idx] = sum;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> b[dst_idx2] = sum/2;<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"> }<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">}<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Compile command:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">riscv32-unkown-elf-g++ -nostartfiles -nostdlib -O2 -march=rv32imf -mabi=ilp32f -fno-builtin -S perf.c -o perf.g++<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">clang++ -O2 </span><span lang="EN-US" style="font-family:"Courier New"">–</span><span lang="EN-US">target=riscv32 -march=rv32img -mabi=ilp32f -nostdlib -fno-builtin -S perf.c -o perf.lang<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">the gcc version is 7.2.0<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">the llvm version is 10.0.0<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US">Assembly code of the loop generated by gcc and llvm:<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US"><img width="1039" height="607" style="width:10.8194in;height:6.3194in" id="图片_x0020_1" src="cid:image002.jpg@01D61BBF.0A4250B0"></span><span lang="EN-US"><o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US" style="color:black"> Thank you all for your time and any help you can provide.<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US" style="color:black">Lori<o:p></o:p></span></p>
<p class="MsoPlainText"><span lang="EN-US" style="color:black"><o:p> </o:p></span></p>
</div>
</body>
</html>