<div dir="ltr"><div dir="ltr">In general you'll find many cases where O3 does not reproduce either.<div><br></div><div>Have you tried `llc -O2` in your case? Clang sets the backend optimization level that way for Oz/Os/O2: <a href="https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/BackendUtil.cpp#L430">https://github.com/llvm/llvm-project/blob/main/clang/lib/CodeGen/BackendUtil.cpp#L430</a></div><div><br></div><div>-- </div><div>Mehdi</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 20, 2021 at 9:06 PM Jiayu Zhao <<a href="mailto:scjzh@leeds.ac.uk">scjzh@leeds.ac.uk</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hi,</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
I can <span style="color:rgb(32,31,30);font-size:15px;background-color:rgb(255,255,255);display:inline">reproduce<span> the
results of clang++ -O3 by opt -O3. But I cannot <span style="background-color:rgb(255,255,255);display:inline">reproduce</span><span style="margin:0px"> <span style="background-color:rgb(255,255,255);display:inline">the results of<span> </span></span><span style="background-color:rgb(255,255,255);display:inline">clang++
-Oz by opt -Oz.</span></span></span></span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<span style="color:rgb(32,31,30);font-size:15px;background-color:rgb(255,255,255);display:inline"><span><span style="margin:0px"><span style="background-color:rgb(255,255,255);display:inline"><br>
</span></span></span></span></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<span style="color:rgb(32,31,30);font-size:15px;background-color:rgb(255,255,255);display:inline"><span><span style="margin:0px"><span style="background-color:rgb(255,255,255);display:inline">Just
see the previous commands I used to produce the result of opt -O3.</span></span></span></span></div>
<div id="gmail-m_-4523415073551340736appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="gmail-m_-4523415073551340736divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Mehdi AMINI <<a href="mailto:joker.eph@gmail.com" target="_blank">joker.eph@gmail.com</a>><br>
<b>Sent:</b> 21 March 2021 3:23<br>
<b>To:</b> Jiayu Zhao <<a href="mailto:scjzh@leeds.ac.uk" target="_blank">scjzh@leeds.ac.uk</a>><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a> <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b> Re: [llvm-dev] How to produce the same result of clang++ -Oz through opt -Oz</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi,
<div><br>
</div>
<div>This is a known issue: clang -O3 is slightly different from `opt -O3` and it is hard to reproduce exactly. It'd be great to refactor it all so that LLVM exposes a common way for frontend to run the exact same thing.</div>
<div><br>
</div>
<div>-- </div>
<div>Mehdi</div>
<div><br>
</div>
</div>
<br>
<div>
<div dir="ltr">On Sat, Mar 20, 2021 at 9:17 AM Jiayu Zhao via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
</div>
<blockquote style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div><span style="font-size:12pt">Dear developers,</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">I am trying to use 'opt' command with different Optlevels -O3 and -Oz to an IR file to reproduce the results of directly applying clang++ -O3 and -Oz to a source c++ file.</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">1. Firstly I use the following commands to produce the result of opt -O3:</span><br>
</div>
<div><span style="font-size:12pt"><i><b>clang++ -O3 -Xclang -disable-llvm-optzns -emit-llvm -c raytracer.cpp -o raytracer.bc</b></i></span><i><b><br>
</b></i></div>
<div><span style="font-size:12pt"><i><b>opt -O3 raytracer.bc -o tmp.bc </b></i></span><i><b><br>
</b></i></div>
<div><span style="font-size:12pt"><i><b>llc -O3 tmp.bc -o tmp.s</b></i></span><i><b><br>
</b></i></div>
<div><span style="font-size:12pt"><b><i>clang++ tmp.s -o tmp.out</i></b></span><b><br>
</b></div>
<div><br>
</div>
<div><span style="font-size:12pt">2. Then I use clang -O3 directly:</span><br>
</div>
<div><span style="font-size:12pt"><i><b>clang++ -O3 raytracer.cpp -o raytracer.out</b></i></span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">3. Finally I compare the two files tmp.o and raytracer.o:</span><br>
</div>
<div><span style="font-size:12pt"><i><b>diff tmp.out raytracer.out</b></i></span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">We can find the two files are exactly the same.</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">However, things are different in -Oz level</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">1. I use the following commands to produce the result of opt -Oz:</span><br>
</div>
<div><span style="font-size:12pt"><i><b>clang++ -Oz -Xclang -disable-llvm-optzns -emit-llvm -c raytracer.cpp -o raytracer.bc</b></i></span><i><b><br>
</b></i></div>
<div><span style="font-size:12pt"><i><b>opt -Oz raytracer.bc -o tmp.bc </b></i></span><i><b><br>
</b></i></div>
<div><span style="font-size:12pt"><i><b>llc -filetype=obj tmp.bc -o tmp.o (there is no -Oz option for llc)</b></i></span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">2. Then I use clang -Oz directly:</span><br>
</div>
<div><span style="font-size:12pt"><b><i>clang++ -Oz -c raytracer.cpp -o raytracer.o</i></b></span><b><i><br>
</i></b></div>
<div><br>
</div>
<div><span style="font-size:12pt">3. Finally I compare the two files tmp.o and raytracer.o:</span><br>
</div>
<div><span style="font-size:12pt"><b><i>diff tmp.o raytracer.o</i></b></span><b><i><br>
</i></b></div>
<div><br>
</div>
<div><span style="font-size:12pt">It shows 'Binary files tmp.o and raytracer.o differ'</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">why 'opt -Oz' cannot produce the same result as '</span><span style="color:rgb(0,0,0);font-size:12pt;background-color:rgb(255,255,255);display:inline">clang++ -Oz' and how to solve it ? I use </span><span style="color:rgb(0,0,0);font-size:14px;background-color:rgb(255,255,255);display:inline"><span style="font-size:12pt">LLVM
10.0.1 and CentOS 7.6.</span></span></div>
<div><br>
</div>
<div><span style="font-size:12pt">I'd very much appreciate if you could help me with this. Thank you.</span><br>
</div>
<div><br>
</div>
<div><span style="font-size:12pt">Kind Regards,</span><br>
</div>
<div><span style="font-size:12pt">Jiayu Zhao</span></div>
<div><br>
</div>
</div>
_______________________________________________<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>
</div>
</div>
</blockquote></div>