Dear llvm-dev list, Apologies if this list is not the right venue for this query - suitable redirection would be appreciated in that case. I have a JIT use case that I'd like to know the best way to implement using LLVM. I am looking to migrate from the existing native compilation option (Tiny C Compiler - TCC) for pLisp, a Lisp dialect and IDE. 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. 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. 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. Thanks for your assistance. Regards, Rajesh Jayaprakash (https://github.com/shikantaza/plisp) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190308/7b357f28/attachment-0001.html>
Hi Rajesh, If I understand correctly, libclang is a C interface to Clang features, not LLVM. That means that you cannot get LLVM IR via libclang: 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. To get LLVM IR and perform JIT compilation you need to use C++ API of Clang and LLVM. Here is some links which might be helpful: * [cfe-dev] How to use clang and llvm for JIT, preferably via the C API http://lists.llvm.org/pipermail/cfe-dev/2015-August/044869.html * [cfe-dev] Help on Generating LLVM Module from C++ file using libClang http://lists.llvm.org/pipermail/cfe-dev/2017-November/056033.html * [LLVMdev] libclang JIT frontend http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html * Generate assembly from C code in memory using libclang https://stackoverflow.com/questions/34828480/generate-assembly-from-c-code-in-memory-using-libclang BTW, there is a library for translating OpenCL C source code into a LLVM IR which features in-memory translation: * https://github.com/intel/opencl-clang It is not suitable for performing the whole JIT compilation, but it is used by https://github.com/intel/intel-graphics-compiler 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 From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Rajesh Jayaprakash via llvm-dev Sent: Friday, March 8, 2019 2:14 PM To: llvm-dev at lists.llvm.org Subject: [llvm-dev] Query about JIT Dear llvm-dev list, Apologies if this list is not the right venue for this query - suitable redirection would be appreciated in that case. I have a JIT use case that I'd like to know the best way to implement using LLVM. I am looking to migrate from the existing native compilation option (Tiny C Compiler - TCC) for pLisp, a Lisp dialect and IDE. 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. 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. 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. Thanks for your assistance. Regards, Rajesh Jayaprakash (https://github.com/shikantaza/plisp) -------------------------------------------------------------------- Joint Stock Company Intel A/O Registered legal address: Krylatsky Hills Business Park, 17 Krylatskaya Str., Bldg 4, Moscow 121614, Russian Federation This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190312/c55e44b9/attachment.html>
Hi Alexey, 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. Regards, Rajesh Jayaprakash On Tue 12 Mar, 2019, 6:35 PM Sachkov, Alexey, <alexey.sachkov at intel.com> wrote:> Hi Rajesh, > > If I understand correctly, libclang is a C interface to Clang features, > not LLVM. That means that you cannot get LLVM IR via libclang: 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. > > To get LLVM IR and perform JIT compilation you need to use C++ API of > Clang and LLVM. > > > > Here is some links which might be helpful: > > * [cfe-dev] How to use clang and llvm for JIT, preferably via the C API > http://lists.llvm.org/pipermail/cfe-dev/2015-August/044869.html > > * [cfe-dev] Help on Generating LLVM Module from C++ file using libClang > http://lists.llvm.org/pipermail/cfe-dev/2017-November/056033.html > > * [LLVMdev] libclang JIT frontend > http://lists.llvm.org/pipermail/llvm-dev/2013-October/066088.html > > * Generate assembly from C code in memory using libclang > https://stackoverflow.com/questions/34828480/generate-assembly-from-c-code-in-memory-using-libclang > > > > BTW, there is a library for translating OpenCL C source code into a LLVM > IR which features in-memory translation: > > * https://github.com/intel/opencl-clang > It is not suitable for performing the whole JIT compilation, but it is > used by https://github.com/intel/intel-graphics-compiler 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 > > > > *From:* llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] *On Behalf Of *Rajesh > Jayaprakash via llvm-dev > *Sent:* Friday, March 8, 2019 2:14 PM > *To:* llvm-dev at lists.llvm.org > *Subject:* [llvm-dev] Query about JIT > > > > Dear llvm-dev list, > > > > Apologies if this list is not the right venue for this query - suitable > redirection would be appreciated in that case. > > > > I have a JIT use case that I'd like to know the best way to implement > using LLVM. > > > > I am looking to migrate from the existing native compilation option (Tiny > C Compiler - TCC) for pLisp, a Lisp dialect and IDE. > > > > 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. > > > > 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. > > > > 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. > > > > Thanks for your assistance. > > > > Regards, > Rajesh Jayaprakash > > (https://github.com/shikantaza/plisp) > > > -------------------------------------------------------------------- > Joint Stock Company Intel A/O > Registered legal address: Krylatsky Hills Business Park, > 17 Krylatskaya Str., Bldg 4, Moscow 121614, > Russian Federation > > This e-mail and any attachments may contain confidential material for > the sole use of the intended recipient(s). Any review or distribution > by others is strictly prohibited. If you are not the intended > recipient, please contact the sender and delete all copies. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190312/61f3f896/attachment.html>