<div dir="ltr"><div>Right, I missed that but...</div><div><br></div><div>In general, I think it's quite important to understand sort of how it all comes down to opt, otherwise it seems like "magic" and if something goes wrong, well tough luck.<br></div><div><br></div>It's even more important in this topic given that very many times the -mllvm variant doesn't work. Meaning, not just random things I have tried, but even things that<div>other people have tried successfully and recommend.<br><div><br></div><div>In any case, thanks for pointing it out.</div><div><br></div><div>Kind regards,</div><div>Stefanos</div><div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Τρί, 26 Ιαν 2021 στις 6:32 π.μ., ο/η Mehdi AMINI <<a href="mailto:joker.eph@gmail.com">joker.eph@gmail.com</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 25, 2021 at 6:53 PM Stefanos Baziotis via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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"><div dir="ltr">Hi Sudakshina, <div><br></div><div>Glad it helped :)</div><div><br></div><div>> I did not find any 'print-changed' option for llvm.</div><div>Hmm.. I can't reproduce it myself right now either.. Anyway, let's go with what works for sure. That's `-print-after-all`. This prints the IR after every (middle-end) pass, no matter whether the pass made any changes or not.</div><div><br></div><div>Alright, now to use that: This is _not_ an option of Clang (or the Clang driver; i.e., the command: clang test.c -print-after-all won't work), but an option of opt.</div></div></blockquote><div><br></div><div>These debug options are available from clang when prefixed with `-mllvm ` (so `-mllvm --print-after-all` here).</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div> opt, in case you're not familiar with it, is basically the middle-end optimizer of LLVM</div><div>i.e. it's supposed to be doing target-independent optimizations, which from what I understand is that you're interested in. If you want to also print back-end passes (i.e., register allocation etc.), that's another story.</div><div><br></div><div>Anyway, when you type e.g., clang test.c -o test, 3 high-level steps happen:</div><div>1) Clang parses, type-checks etc. the C source and generates (very trivial) LLVM IR</div><div>2) This is then passed to opt, which issues a bunch of passes (again, depending on whether you used -O1, -O2 etc.). Each pass takes IR and outputs IR</div><div>3) When it's done, it passes it to the back-end, which is another story and uses another IR.</div><div><br></div><div>Now, what you want is to get the IR after the first step, so that you can pass it _yourself_ to opt, with any options _you_ want (one of them being `print-after-all`). To do that, you type e.g.,: clang test.c -o test.ll -S -emit-llvm</div><div>-emit-llvm tells Clang to stop at step 1) and output the IR in a text file (note that we used no -O1, -O2 options because we want the fully unoptimized, trivial IR at this step)</div><div>-S tells clang to print in textual format (let's not go now into what is the other format)</div><div><br></div><div>If your file can't be linked to an executable (e.g., it doesn't have a main()), you should add a -c there.</div><div><br></div><div>Alright, now we have our IR but there's a problem. Our functions have the `optnone` attribute, which tells the optimizer to not touch them (that's because we used no optimization options).</div><div>We don't want that, so we add another option to clang, -Xclang -disable-O0-optnone</div><div><br></div><div>So, all in all, it looks something like this: clang test.c -o test.ll -c -emit-llvm -S -Xclang -disable-O0-optnone</div><div><br></div><div>Now, we have the LLVM IR in test.ll and we can pass it to opt. You can now say: opt test.ll -O3 -print-after-all</div><div><br></div><div>Let me show you the steps in Godbolt:</div><div>- Generate (unoptimized) IR from Clang: <a href="https://godbolt.org/z/rY3Thx" target="_blank">https://godbolt.org/z/rY3Thx</a> (note that I also added -g0 to avoid printing debug info, which are probably not helpful)</div><div>- Copy this exact IR and pass it to opt: <a href="https://godbolt.org/z/7cjdcf" target="_blank">https://godbolt.org/z/7cjdcf</a> (you can see on the right window that SROA changed the code a lot)</div><div><br></div><div>As you can understand, you can automate all that with a script.</div><div><br></div><div>Final comment: After step 1), it's useful to pass your IR through opt with the option: -metarenamer or -instnamer. This message has already become way too big so let me not explain now why it's useful, but trust me, it is.</div><div><br></div><div>Best,</div><div>Stefanos</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Τρί, 26 Ιαν 2021 στις 4:18 π.μ., ο/η Sudakshina Dutta <<a href="mailto:sudakshina@iitgoa.ac.in" target="_blank">sudakshina@iitgoa.ac.in</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Dear Stefanos,</div><div><br></div><div>Thank you for your reply. It helped me to understand the optimization phase of LLVM. However, I did not find any 'print-changed' option for llvm. Can you kindly help me in this regard ? I want to generate the IRs after each optimization pass.</div><div><br></div><div>Regards,<br></div><div>Sudakshina<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jan 24, 2021 at 7:13 PM Stefanos Baziotis <<a href="mailto:stefanos.baziotis@gmail.com" target="_blank">stefanos.baziotis@gmail.com</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"><div dir="ltr">Hi
Sudakshina,<div><br></div><div>> The optimization applied in the optimization pass depends on the source program; hence, the number of optimizations applied differs from source program to source program.</div><div><br></div><div>"applied" is still ambiguous, at least to me. If by "applied" you mean "attempted", then no, that does not depend on the source program. It depends on the optimization level (e.g., O1, O2, ...) or the individual passes that you may request yourself.</div><div>That is, for -O1 for example, there is a predetermined sequence of passes that _attempt_ to optimize the program and you can see that with the options I mentioned above (e.g., `-mllvm -opt-bisect-limit=-1`)</div><div><br></div><div>If by applied you mean "actually changed the code", then yes, this differs from program to program. You can see that with `print-changed`, it'll show you the IR after every transformation that changed your program.</div><div><br></div><div>Finally, if you want to see why a transformation could or not change the code, you can use the related comments about remarks.</div><div><br></div><div>Best,</div><div>Stefanos</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Κυρ, 24 Ιαν 2021 στις 7:24 π.μ., ο/η Sudakshina Dutta <<a href="mailto:sudakshina@iitgoa.ac.in" target="_blank">sudakshina@iitgoa.ac.in</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>Dear all,<div dir="auto"><br></div><div dir="auto">In the optimization phase, the compiler applies some optimization to generate an optimized program. The optimization applied in the optimization pass depends on the source program; hence, the number of optimizations applied differs from source program to source program. By mentioning "applied" transformation, I wanted to know what all transformations are applied for a specific input program when subjected to the LLVM optimizer.</div><div dir="auto"><br></div><div dir="auto">Thanks,</div><div dir="auto">Sudakshina </div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 24 Jan 2021, 09:27 Stefanos Baziotis, <<a href="mailto:stefanos.baziotis@gmail.com" target="_blank">stefanos.baziotis@gmail.com</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"><div dir="ltr">Hi Sudakshina,<br><br>Not really sure what you mean by "applied", so, let me offer some more ideas other than Brian's and Adrian's great suggestions. First, there are some<div>diagnostics / remarks flags in Clang like the -R family [1] or some -f flags about printing optimization reports [2] from Clang. They can be useful or useless depending</div><div>on your case. They can also be parsed relatively easily.</div><div><br></div><div>If you just want to see a list of passes that were attempted in your code, you can do it with: `-mllvm -opt-bisect-limit=-1`</div><div>You can also use `-mllvm-debug-pass=Arguments` to see the arguments that were passed.</div><div><br></div><div>Moving into opt, you can use something like `print-after-all`, which was already mentioned. If you don't know what these flags do, is they show you</div><div>the IR in different stages in the pipeline (e.g., `print-after-all` shows you each pass attempted and how the IR is after it).</div><div><br></div><div>Hope it helps,</div><div>Stefanos<br><br>[1] <a href="https://clang.llvm.org/docs/ClangCommandLineReference.html#diagnostic-flags" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/ClangCommandLineReference.html#diagnostic-flags</a></div><div>[2] <a href="https://clang.llvm.org/docs/UsersManual.html#cmdoption-f-no-save-optimization-record" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/UsersManual.html#cmdoption-f-no-save-optimization-record</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Στις Κυρ, 24 Ιαν 2021 στις 5:47 π.μ., ο/η Adrian Vogelsgesang via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> έγραψε:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="en-DE">
<div>
<p class="MsoNormal"><span lang="EN-US">I used “-print-changed”, “-print-before-all”, “print-after-all” last time I wanted to see the passes together with their inout/output IR modules.<br>
<br>
In my case, I used them through “clang++”, i.e. I had to prefix them with “-mllvm”<br>
> clang++ test_file.cpp -mllvm -print-after-all<u></u><u></u></span></p>
<p class="MsoNormal"><span><u></u> <u></u></span></p>
<div style="border-color:rgb(181,196,223) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0cm 0cm">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From:
</span></b><span style="font-size:12pt;color:black">llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev-bounces@lists.llvm.org</a>> on behalf of Brian Cain via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Date: </b>Sunday, 24. January 2021 at 04:40<br>
<b>To: </b>Sudakshina Dutta <<a href="mailto:sudakshina@iitgoa.ac.in" rel="noreferrer" target="_blank">sudakshina@iitgoa.ac.in</a>><br>
<b>Cc: </b>LLVM Development List <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Subject: </b>Re: [llvm-dev] LLVM log file<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal">I don't know if it's exhaustive but there's the "remarks" feature:<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><a href="https://llvm.org/docs/Remarks.html#introduction-to-the-llvm-remark-diagnostics" rel="noreferrer" target="_blank">https://llvm.org/docs/Remarks.html#introduction-to-the-llvm-remark-diagnostics</a><u></u><u></u></p>
</div>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">On Sat, Jan 23, 2021 at 9:20 PM Sudakshina Dutta via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<u></u><u></u></p>
</div>
<blockquote style="border-color:currentcolor currentcolor currentcolor rgb(204,204,204);border-style:none none none solid;border-width:medium medium medium 1pt;padding:0cm 0cm 0cm 6pt;margin-left:4.8pt;margin-right:0cm">
<div>
<div>
<p class="MsoNormal">Dear all,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Good morning. I want to know whether LLVM creates any log file consisting of applied optimizations in the optimization phase. It will be really useful for the researchers who work on compilers, formal methods, etc.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Thanks,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Sudakshina<u></u><u></u></p>
</div>
</div>
<p class="MsoNormal">_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" 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><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">-- <u></u><u></u></p>
<div>
<p class="MsoNormal">-Brian<u></u><u></u></p>
</div>
</div>
</div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></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>