<div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Rajesh,</div><div dir="ltr"><br></div><div dir="ltr">Adding the cfe-dev (Clang FrontEnd) mailing list as your question seems to be more targeted towards clang (your input is C code if I understood correctly).<br><div><br></div><div>At the LLVM level, there is a (limited but "stable") C API here: <a href="https://github.com/llvm/llvm-project/tree/master/llvm/include/llvm-c">https://github.com/llvm/llvm-project/tree/master/llvm/include/llvm-c</a></div><div>For the JIT, this file seems relevant: <a href="https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm-c/OrcBindings.h">https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm-c/OrcBindings.h</a></div><div>But this assumes you get LLVM IR in the first place, and I don't know of a C API for clang for this.</div><div><br></div><div>Best,</div><div><br></div><div>-- </div><div>Mehdi</div><div><br></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Mar 12, 2019 at 7:05 AM Rajesh Jayaprakash via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</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="auto">Hi Alexey,<div dir="auto"><br></div><div dir="auto">Thank you very much for your detailed reply, will look into the resources indicated. I'd like to avoid C++ if possible, let me see how it goes.</div><div dir="auto"><br></div><div dir="auto">Regards,</div><div dir="auto">Rajesh Jayaprakash</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue 12 Mar, 2019, 6:35 PM Sachkov, Alexey, <<a href="mailto:alexey.sachkov@intel.com" rel="noreferrer" target="_blank">alexey.sachkov@intel.com</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 lang="EN-US">
<div class="gmail-m_-4195670606799627116m_5727511374178736104m_7951470418514789474WordSection1">
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">Hi Rajesh,<br>
<br>
If I understand correctly, libclang is a C interface to Clang features, not LLVM. That means that you cannot get LLVM IR via libclang<a name="m_-4195670606799627116_m_5727511374178736104_m_7951470418514789474__MailEndCompose" rel="noreferrer noreferrer">: The C Interface to Clang provides a relatively small API that exposes facilities for parsing source
code into an abstract syntax tree (AST), loading already-parsed ASTs, traversing the AST, associating physical source locations with elements within the AST, and other facilities that support Clang-based development tools.<br>
<br>
To get LLVM IR and perform JIT compilation you need to use C++ API of Clang and LLVM.<u></u><u></u></a></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">Here is some links which might be helpful:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">* [cfe-dev] How to use clang and llvm for JIT, preferably via the C API </span><a href="http://lists.llvm.org/pipermail/cfe-dev/2015-August/044869.html" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">http://lists.llvm.org/pipermail/cfe-dev/2015-August/044869.html</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">* [cfe-dev] Help on Generating LLVM Module from C++ file using libClang
</span><a href="http://lists.llvm.org/pipermail/cfe-dev/2017-November/056033.html" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">http://lists.llvm.org/pipermail/cfe-dev/2017-November/056033.html</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">* [LLVMdev] libclang JIT frontend
</span><a href="http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">* Generate assembly from C code in memory using libclang
</span><a href="https://stackoverflow.com/questions/34828480/generate-assembly-from-c-code-in-memory-using-libclang" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">https://stackoverflow.com/questions/34828480/generate-assembly-from-c-code-in-memory-using-libclang</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">BTW, there is a library for translating OpenCL C source code into a LLVM IR which features in-memory translation:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif">*
</span><a href="https://github.com/intel/opencl-clang" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">https://github.com/intel/opencl-clang</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"><br>
It is not suitable for performing the whole JIT compilation, but it is used by </span>
<a href="https://github.com/intel/intel-graphics-compiler" rel="noreferrer noreferrer" target="_blank"><span style="font-size:11pt;font-family:Calibri,sans-serif">https://github.com/intel/intel-graphics-compiler</span></a><span style="font-size:11pt;font-family:Calibri,sans-serif"> as a front-end.
Also I’m not sure that you will be able to use it as-is for your purposes, but at least it can be used as an example<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></span></p>
<p class="MsoNormal"><a name="m_-4195670606799627116_m_5727511374178736104_m_7951470418514789474______replyseparator" rel="noreferrer noreferrer"></a><b><span style="font-size:11pt;font-family:Calibri,sans-serif">From:</span></b><span style="font-size:11pt;font-family:Calibri,sans-serif"> llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" rel="noreferrer noreferrer" target="_blank">llvm-dev-bounces@lists.llvm.org</a>]
<b>On Behalf Of </b>Rajesh Jayaprakash via llvm-dev<br>
<b>Sent:</b> Friday, March 8, 2019 2:14 PM<br>
<b>To:</b> <a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<b>Subject:</b> [llvm-dev] Query about JIT<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Dear llvm-dev list,<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Apologies if this list is not the right venue for this query - suitable redirection would be appreciated in that case.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I have a JIT use case that I'd like to know the best way to implement using LLVM.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I am looking to migrate from the existing native compilation option (Tiny C Compiler - TCC) for pLisp, a Lisp dialect and IDE.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">At present, the native compilation is done by converting the Lisp code to C, storing the C code in a char buffer, and passing it to TCC programmatically via the API provided. I get a function pointer in return, which I store and invoke
as needed.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Delving into the LLVM documentation, I found that one possible way to achieve the same functionality in LLVM is to use clag/libclang to convert the C source to LLVM IR, load this IR into the the JIT context and (skipping some steps I'm
yet to figure out) get the desired function pointer.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Is this approach the right one? One issue I foresee is that libclang's clang_parseTranslateUnit() function expects the C code to be from a file (although the file can be in-memory), whereas in my case the C code needs to be picked up from
a char buffer - necessitating fmemopen(), etc.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<p class="MsoNormal">Thanks for your assistance.<br clear="all">
<u></u><u></u></p>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Regards,<br>
Rajesh Jayaprakash<u></u><u></u></p>
</div>
<p class="MsoNormal" style="margin-bottom:12pt">(<a href="https://github.com/shikantaza/plisp" rel="noreferrer noreferrer" target="_blank">https://github.com/shikantaza/plisp</a>)<u></u><u></u></p>
</div>
<p><br>--------------------------------------------------------------------<br>Joint Stock Company Intel A/O<br>Registered legal address: Krylatsky Hills Business Park, <br>17 Krylatskaya Str., Bldg 4, Moscow 121614, <br>Russian Federation</p><p>This e-mail and any attachments may contain confidential material for<br>the sole use of the intended recipient(s). Any review or distribution<br>by others is strictly prohibited. If you are not the intended<br>recipient, please contact the sender and delete all copies.</p>
</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>