<div dir="ltr"><div><div><div><div><div><div><div><div><div>Yup that is quite similar to my use case, but in addition I use LLVM to JIT as well from C using MCJIT.<br><br></div>I have a patch for a DIBuilder binding, but it does change the API a bit, so depending on how this discussion goes, it can or cannot be added. It was necessary as the current binding wrap all metadata in values, which is very wasteful when dealing with a large amount of metadata. The breakage is small (all my existing code still compile and work even using the old binding API) but very real. I obviously think the change is good or I wouldn't have made it, but that may be too much for others.<br><br></div>Back on point, the importants things for me to do through the C API are :<br></div> - Write IR, including debug infos and other metadata (it is mostly good, some IR specificities are not covered, like ordered loads/stores, but these are edges cases that ca be added. I can prepare a patch if people want these).<br></div> - Read IR, so I can do transformations.<br></div> - Codegen to object files<br></div> - JIT (using MCJIT is fine, Orca would be better but making a good C api for it may prove challenging).<br><br></div>I preferably would like that the ABI is not broken, as typecheking won't work cross language boundary. That being said, I'm ok with this being a "best practice" or whatever people want to call it rather than a strong, enforced, requirement. However, if it is not a strong, enforced requirement, we need a way to test what versions of LLVM is currently in use as to choose the right version of the function to call. As long as ABI breakage are not too frequent, this is workable as far as I'm concerned, and put less constraint on the evolution of the whole thing.<br><br></div>Last but not least, I started working on a test that would read IR and regenerate it. This would enforce that IR is readable and writable from the C API, and that breakage are not done unknowingly like it happened in the past several times. I really would like this to move forward whatever the policy is chosen. Random breakage + detective work to figure out what happened are the worst when we could know things in an automated manner.<br><br></div>As a final note, I'd like for us to focus on resolving this soon enough so that some work can be done related to the C API before 3.8 is out. Nothing is on fire yet, but let's not let this slip too much.<br><div><div><div><div><div><div><div><div><div><br></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-10-20 13:16 GMT-07:00 Rodney M. Bates via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
<br>
On 10/17/2015 01:20 PM, Justin Bogner via llvm-dev wrote:<br>
</span><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
(Moving this to llvm-dev)<div><div class="h5"><br>
<br>
On Friday, October 16, 2015, Justin Bogner <<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a> <mailto:<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>>> wrote:<br>
<br>
    Some users of llvm-c want stable API interfaces into various parts of<br>
    the LLVM infrasture, others want further ABI guarantees about this<br>
    usage, and still others simply want a way to bind to LLVM through their<br>
    language frontend’s existing FFI support for C.<br>
<br>
    If we want to improve the situation for any of these users, we need to<br>
    properly understand how these APIs are being used (or abused)<br>
    today. Juergen and I will be hosting a BoF at the dev meeting where we<br>
    can discuss what the requirements of a sustainable C API are, and how we<br>
    can organize things in LLVM to support this.<br>
<br>
    There's been a fair amount of discussion about this lately, and it's<br>
    pretty clear that the "hopefully stable bindings to whatever APIs<br>
    somebody needed" approach isn't really good enough for anybody. There<br>
    are a couple of points that I think are fairly non-controversial, but<br>
    will more or less drive the discussion at the BoF:<br>
<br>
    1. It isn't practical to keep a bindings API stable, unless the<br>
        underlying API is also stable.<br>
<br>
    2. Handrolling bindings as they're needed tends to leave conspicuous<br>
        gaps where some API is inaccessible for no good reason.<br>
<br>
    So based on (1), we'll really want to create some purpose built APIs<br>
    that we can keep stable for various tasks. What's needed here? People<br>
    want to do things like building a pass manager, setting up a canned JIT<br>
    config, and to some degree even emit IR. We'll discuss what's practical<br>
    and what people want, and hopefully strike a good balance.<br>
<br>
    Similarly, (2) implies that if we really need a *full* bindings API<br>
    we'll want to automate it. But what is a full bindings API? Who uses it,<br>
    and what do they want from it? If it's automated, should installing LLVM<br>
    install this API, or should we simply provide an easy way to generate<br>
    it?<br>
<br>
    In the end, I hope to have a good idea of what people are actually using<br>
    these APIs for, and both to support stable API users less haphazardly<br>
    and to make unstable API more thorough and/or easier to create.<br>
<br>
</div></div></blockquote>
<br>
One actual use:<br>
<br>
A couple of us in the Modula3 support community are working on splicing a<br>
llvm back end onto our Modula3 compiler front end.  We have zero-thickness<br>
bindings written in Modula3 which match C bindings like core.h.  In our front<br>
end executable, we only build llvm IR in memory, then use LLVMPrintModuleToFile<br>
and/or LLVMWriteBitcodeToFile.  We only link in the needed parts of the llvm<br>
infrastructure to build and write the llvm IR.  Then we run stock llc on the<br>
IR code in the emitted file.<br>
<br>
We have had to write an additional binding to DIBuilder for this purpose,<br>
as well as to a few odd other C++ functions here and there.  The latest<br>
version of this is for llvm 3.6.1, after the separation of metadata from<br>
values.  It has had minimal testing, but is fairly complete.  Older bindings<br>
to an older DIBuilder exist, and are at least partly working, but only<br>
contain specifically needed functions.<br>
<br>
This scheme is passing a majority of preexisting compiler tests of code<br>
function, but there is not much testing of emitted debug info.  Speaking<br>
for myself, better debugger support is one of the primary motivations for<br>
using llvm.  Our older back ends use a really cobbled-up extension of<br>
stabs+.  It's a mess, and lots of debugger function is difficult to<br>
provide.<br>
<br>
The DIBuilder binding includes .h and .cpp files similar to core, that provide<br>
a C binding, with lots of [un]wrapping, etc.  Like core, it loses the type<br>
hierarchy.  Modula3 has a type hierarchy, but machine-level representation<br>
is undoubtedly not ABI compatible with any C++ compiler.  The current handling<br>
of this is not thoroughly thought out, just using lots of unsafe casting of<br>
pointers.  I would like to think of something better, but haven't had much time<br>
for that, so far.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
_______________________________________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br><span class="HOEnZb"><font color="#888888">
</font></span></blockquote><span class="HOEnZb"><font color="#888888">
<br>
-- <br>
Rodney Bates<br>
<a href="mailto:rodney.m.bates@acm.org" target="_blank">rodney.m.bates@acm.org</a><br>
_______________________________________________<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</font></span></blockquote></div><br></div>