<div dir="ltr">Hello/Merhaba Seyyid from what I understand you are trying to write a compiler Instrumentation.<div><br></div><div>To run a pass directly from clang:</div><div><div>For the old pass manager `-<span style="color:rgb(51,51,51);font-size:14px">Xclang -load -Xclang mypass.so ...`</span></div><div><div>For the new pass manager `-fpass-plugin=mypass.so ...`</div><div><br></div><div>If you really wanted you could fork llvm and add your pass as a built-in pass. </div><div>But I think you shouldn't do it that way. </div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Seyyid Hikmet Çelik via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>>, 26 May 2021 Çar, 03:50 tarihinde şunu yazdı:<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 everyone,<div>I am kind of a newbie here, working on llvm as a undergraduate student and I am struggling about some situations. Yet I can write meaningless passes, working tools by copying or trying to porting from llvm source and know how to build them. I am searching on the web, using <a href="https://llvm.org/doxygen" target="_blank">https://llvm.org/doxygen</a> to understand some functions, classes and how I can integrate these kind of things in my experimental codes but I think that I can't go any further now that's why I am writing this e-mail and probably more comes for different questions.</div><div>

I am trying to write a tool which will get target triple as a command option and a C or C++ code, will emit and change LLVM IR code

 (maybe adding a pass or meaningless operation for now :) ) inside, will emit and change object code from changed LLVM IR, and lastly will give output as an executable file.</div><div>Example tool in summary as I thought:</div><div>Usage (I can add some command line options later):</div><div></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">mytool --triple riscv32 example.c</pre></div><div><br></div><div>Sequence of this complete tool:</div><div>Run Tool --> Get Target Triple --> Emit LLVM IR code from source code --> Change LLVM IR code --> Emit Object Code --> Change Object Code --> Give Executable File Output</div><div><br></div><div>For this, firstly I thought implement to get file and target triple (If I am mistaken or incomplete you can warn):</div><div><br></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)"><span style="color:rgb(188,122,0)">#include</span> <span style="color:rgb(64,128,128);font-style:italic">"llvm/Support/CommandLine.h"</span><span style="color:rgb(188,122,0)"></span>

<span style="color:rgb(0,128,0);font-weight:bold">static</span> cl<span style="color:rgb(102,102,102)">::</span>list<span style="color:rgb(102,102,102)"><</span>std<span style="color:rgb(102,102,102)">::</span>string<span style="color:rgb(102,102,102)">></span>
InputFilenames(cl<span style="color:rgb(102,102,102)">::</span>Positional, cl<span style="color:rgb(102,102,102)">::</span>desc(<span style="color:rgb(186,33,33)">"<input files>"</span>),cl<span style="color:rgb(102,102,102)">::</span>ZeroOrMore);

<span style="color:rgb(0,128,0);font-weight:bold">extern</span> cl<span style="color:rgb(102,102,102)">::</span>opt<span style="color:rgb(102,102,102)"><</span>std<span style="color:rgb(102,102,102)">::</span>string<span style="color:rgb(102,102,102)">></span> TripleName;

cl<span style="color:rgb(102,102,102)">::</span>opt<span style="color:rgb(102,102,102)"><</span>std<span style="color:rgb(102,102,102)">::</span>string<span style="color:rgb(102,102,102)">></span>
llvm<span style="color:rgb(102,102,102)">::</span>TripleName(<span style="color:rgb(186,33,33)">"triple"</span>, cl<span style="color:rgb(102,102,102)">::</span>desc(<span style="color:rgb(186,33,33)">"target triple"</span>));</pre></div><div><br></div><div>Suppose that I did more coding and get source code file in main function, after that how can I emit LLVM IR or object code inside and apply on some operations? </div><div><br></div><div>I am asking this because for example we can use a pass in command line as:</div><div><br></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">opt -load libMyPass.so -mypass example.ll -o output.ll</pre></div><div></div><div>or in clang with <span style="background-color:transparent;color:rgb(0,0,0);font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em">-Xclang -load -Xclang </span>option.</div><div><br></div><div>or this is more proper example:</div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">clang -mem2reg example.ll -o output.ll</pre></div><div><br></div><div>And we can also use these passes in our codes (in other passes or tools):</div><div><br></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)"><span style="color:rgb(0,128,0);font-weight:bold">static</span> std<span style="color:rgb(102,102,102)">::</span>unique_ptr<span style="color:rgb(102,102,102)"><</span>legacy<span style="color:rgb(102,102,102)">::</span>FunctionPassManager<span style="color:rgb(102,102,102)">></span> TheFPM;  
TheFPM<span style="color:rgb(102,102,102)">-></span>add(createPromoteMemoryToRegisterPass());
TheFPM<span style="color:rgb(102,102,102)">-></span>run(<span style="color:rgb(102,102,102)">*</span>TheFunction);</pre></div><div>(TheFunction should be declared before.)</div><div><br></div><div>or maybe <span style="background-color:transparent;color:rgb(0,0,0);font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em">addPass </span>function (I am not sure.)</div><div><a href="https://llvm.org/doxygen/classllvm_1_1PassManager.html#a314ff184ce4ace8801e1158ef909e22e" target="_blank">https://llvm.org/doxygen/classllvm_1_1PassManager.html#a314ff184ce4ace8801e1158ef909e22e</a></div><div><br></div><div>or like using an analysis pass inside another different pass:</div><div><br></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)"><span style="color:rgb(176,0,64)">void</span> <span style="color:rgb(0,0,255)">LegacyAnotherPass::getAnalysisUsage</span>(AnalysisUsage <span style="color:rgb(102,102,102)">&</span>Info) <span style="color:rgb(0,128,0);font-weight:bold">const</span> {
  Info.addRequired<span style="color:rgb(102,102,102)"><</span>LegacyMyAnalysisPass<span style="color:rgb(102,102,102)">></span>();
}</pre></div><div><br></div><div>I thought we could other command line operations inside our code as this. I am trying to find a function for my desire. For example as in the pass example above:</div><div><br></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">clang -S -emit-llvm example.c -o example.ll
clang -c example.ll -o example.o</pre></div><div><br></div><div>And code equivalent (extremely making up :) ):</div><div>for llvm ir:</div><div></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">llvmir <span style="color:rgb(102,102,102)">=</span> file.getLLVMIRCode();</pre></div><div><br></div><div>for object code:</div><div></div><div><pre style="line-height:16.25px;overflow:auto hidden;clear:both;font-family:Consolas,"DejaVu Sans Mono","Bitstream Vera Sans Mono",monospace;font-size:13px;letter-spacing:0.015em;padding:0.5em;border:1px solid rgb(204,204,204);border-radius:0px;background-color:transparent;margin-top:0px;margin-bottom:0px;color:rgb(0,0,0)">objcode <span style="color:rgb(102,102,102)">=</span> llvmir.getObjectCode();</pre></div><div><br></div><div>Is there a way like this in simply and how I can?</div><div><br></div><div>Maybe you can say "write a bash script and do operations step by step" but I want to write this complete tool using llvm and its functions inside. If you can answer I will be satisfied and please don't forget I am newbie if I am mistaken on some points.</div><div><br></div><div>Thanks,</div><div>Seyyid</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>