<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)">
<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:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:2.0cm 42.5pt 2.0cm 3.0cm;}
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="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;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="_MailEndCompose">: 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.<o:p></o:p></a></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">Here is some links which might be helpful:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;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"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">http://lists.llvm.org/pipermail/cfe-dev/2015-August/044869.html</span></a><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;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"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">http://lists.llvm.org/pipermail/cfe-dev/2017-November/056033.html</span></a><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">* [LLVMdev] libclang JIT frontend 
</span><a href="http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html</span></a><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;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"><span style="font-size:11.0pt;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:11.0pt;font-family:"Calibri",sans-serif"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;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:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">*
</span><a href="https://github.com/intel/opencl-clang"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">https://github.com/intel/opencl-clang</span></a><span style="font-size:11.0pt;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"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">https://github.com/intel/intel-graphics-compiler</span></a><span style="font-size:11.0pt;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<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"><o:p> </o:p></span></p>
<p class="MsoNormal"><a name="_____replyseparator"></a><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<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> llvm-dev@lists.llvm.org<br>
<b>Subject:</b> [llvm-dev] Query about JIT<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Dear llvm-dev list,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></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.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">Thanks for your assistance.<br clear="all">
<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Regards,<br>
Rajesh Jayaprakash<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">(<a href="https://github.com/shikantaza/plisp">https://github.com/shikantaza/plisp</a>)<o:p></o:p></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>
</body>
</html>