Over the last few years the LLVM team here at Apple and development teams elsewhere have been busily working on finding new and interesting uses for LLVM. Some of these uses are traditional compilers, but a growing number of them aren’t. Some of LLVM’s new clients, like WebKit, are embedding LLVM into existing applications. These embedded uses of LLVM have their own unique challenges.
Over the next few months, a few of us at Apple are going to be working on tackling a few new problems that we would like solved in open source so other projects can benefit from them. Some of these efforts will be non-trivial, so we’d like to start a few discussions over the next few weeks.
Our primary goals are to (1) make it easier to embed LLVM into external projects as a shared library, and (2) generally improve the performance of LLVM as a shared library.
The list of the problems we’re currently planning to tackle is:
(1) Reduce or eliminate static initializers, global constructors, and global destructors
(2) Clean up cross compiling in the CMake build system
(3) Update LLVM debugging mechanisms for being part of a dynamic library
(4) Move overridden sys calls (like abort) into the tools, rather than the libraries
(5) Update TableGen to support stripping unused content (i.e. Intrinsics for backends you’re not building)
We will be sending more specific proposals and patches for each of the changes listed above starting this week. If you’re interested in these problems and their solutions, please speak up and help us develop a solution that will work for your needs and ours.
Thanks,
-Chris
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
I would be happy to help.
> On Aug 5, 2014, at 12:38 PM, Chris Bieneman <be...@apple.com> wrote:
>
> Hello LLVM community,
>
> Over the last few years the LLVM team here at Apple and development teams elsewhere have been busily working on finding new and interesting uses for LLVM. Some of these uses are traditional compilers, but a growing number of them aren’t. Some of LLVM’s new clients, like WebKit, are embedding LLVM into existing applications. These embedded uses of LLVM have their own unique challenges.
>
> Over the next few months, a few of us at Apple are going to be working on tackling a few new problems that we would like solved in open source so other projects can benefit from them. Some of these efforts will be non-trivial, so we’d like to start a few discussions over the next few weeks.
>
> Our primary goals are to (1) make it easier to embed LLVM into external projects as a shared library, and (2) generally improve the performance of LLVM as a shared library.
>
> The list of the problems we’re currently planning to tackle is:
>
> (1) Reduce or eliminate static initializers, global constructors, and global destructors
> (2) Clean up cross compiling in the CMake build system
> (3) Update LLVM debugging mechanisms for being part of a dynamic library
> (4) Move overridden sys calls (like abort) into the tools, rather than the libraries
> (5) Update TableGen to support stripping unused content (i.e. Intrinsics for backends you’re not building)
Also:
(6) Determine if command line options are the best way of passing configuration settings into LLVM.
It’s an awkward abstraction when LLVM is embedded. I suspect (6) will be closely related to (1) since command line option parsing was the hardest impediment to getting rid of static initializers.
My understanding of the shared library proposal is that the library only exposes the C API since the C++ API is not intended to allow for binary compatibility. So, I think we need to either add the following as either an explicit goal of the shared library work, or as a closely related project:
(7) Make the C API truly great.
I think it’s harmful to LLVM in the long run if external embedders use the C++ API. I think that one way of ensuring that they don’t have an excuse to do it is to flesh out some things:
- Add more tests of the C API to ensure that people don’t break it accidentally and to give more gravitas to the C API backwards compatibility claims.
- Increase C API coverage.
- For example, WebKit currently sidesteps the C API to pass some commandline options to LLVM. We don’t want that.
- Add more support for reasoning about targets and triples. WebKit still has to hardcode triples in some places even though it only ever does in-process JITing where host==target. That’s weird.
- Expose debugging and runtime stuff and make sure that there’s a coherent integration story with the MCJIT C API.
- Currently it’s difficult to round-trip debug info: creating it in C is awkward and parsing DWARF sections that MCJIT generates involves lots of weirdness. WebKit has its own DWARF parser for this, which shouldn’t be necessary.
- WebKit is about to have its own copies of both a compactunwind and EH frame parser. The contributor who “wrote” the EH frame parser actually just took it from LLVM. The licenses are compatible, but nonetheless, copy-paste from LLVM into WebKit should be discouraged.
- Engage with non-WebKit embedders that currently use the C++ API to figure out what it would take to get them to switch to the C API.
I think that a lot of time when C API discussions arise, lots of embedders give excuses for using the C++ API. WebKit used the C API for generating IR and even doing some IR manipulation, and for driving the MCJIT. It’s been a positive experience and we enjoy the binary compatibility that it gives us. I think it would be great to see if other embedders can do the same.
-Filip
One other thing that I'd like to see is a common framework for
defining, describing and inferring architectural support.
Tools use a lot of string parsing and, as Filip said, command line
options don't normally mean the exact same thing across tools.
So that the same arch/cpu/fpu/abi/target options will be parsed
identically across all tools (and external users) and mean exactly the
same thing to the back-end, when building a new sub-target, it should
only accept a TargetDescription object or whatever holds all the
options.
cheers,
--renato
This sounds great.
> The list of the problems we’re currently planning to tackle is:
>
> (1) Reduce or eliminate static initializers, global constructors, and global destructors
> (2) Clean up cross compiling in the CMake build system
One problem we have with the Mesa project is that the automake and CMake build
system produce different shared libraries. Automake builds libLLVM-major.minor.so
while CMake builds a different shared library for each component: e.g. libLLVMSupport.so
To cope with this, Mesa's build system has to try to guess which build system
was used in order to find the libraries.
Do you have plans to standardize the shared libraries produced by LLVM's build systems?
Even better, will improving cross compiling in the CMake build system make
it possible to completely drop automake?
-Tom
Honestly I think if you want to make the C API great we should burn it
to the ground and come up with another one - and one that can be
versioned as well so we don't have the problems of being limited in
what we can do to llvm by needing compatibility with the C API.
-eric
We had not planned to standardize the build systems, but it is interesting to consider. IMHO, maintaining two build systems is a royal pain.
>
> Even better, will improving cross compiling in the CMake build system make
> it possible to completely drop automake?
I would really like to think so, but there will be quite a bit of work involved in getting everyone using the Automake build system to migrate off. One snag with getting off automake that I’m aware of is compiler-rt. I haven’t been able to make sense of the compiler-rt CMake configs well enough to come up with a good solution for cross-compiling.
-Chris
Just to give a bit of perspective from another external LLVM client:
GoLLVM [1], the LLVM bindings for Go used by the llgo compiler [2], mostly
uses the C bindings, but it does need to resort to the C++ API (with its
own set of C bindings) for a few things:
Exporting bitcode to memory buffer:
https://github.com/go-llvm/llvm/blob/master/bitwriter.cpp
Use of attribute masks above 1 << 31:
https://github.com/go-llvm/llvm/blob/master/core.cpp
Debug info generation:
https://github.com/go-llvm/llvm/blob/master/dibuilder.cpp
Loading plugins and setting flags:
https://github.com/go-llvm/llvm/blob/master/support.cpp
Adding instrumentation passes:
https://github.com/go-llvm/llvm/blob/master/transforms_instrumentation.cpp
I think most of this could be upstreamable in some shape or form, but I heard
from debug info experts a few months ago that the IR format was unstable,
so the solution we went with was to wrap the C++ API so that we would be
notified (by the compiler) when the format changes, rather than creating
debug info directly and having it potentially silently discarded. I'm not
sure if the debug info situation has changed since then.
The plugin/flags stuff is valuable to external projects for exactly the same
reasons that Clang supports plugins and LLVM flags. I don't see any reason
to make the specific flag semantics stable, and we can document this as such.
Thanks,
--
Peter
[1] https://github.com/go-llvm/llvm
[2] https://github.com/go-llvm/llgo
Can you come up with specific reasons why building a new API would be better for the community than maintaining the one we’ve got?
-Filip
Rafael came up with a few in his, but also having an API that lightly
wraps the C++ api is hard if we want to change a major C++ interface
or completely remove a class, etc. There's no existing way in the API
to either version or remove an interface given our current promises.
-eric
Right. I believe this is the right approach.
>
> Two examples where we do break C APIs:
>
> * An hypothetical off by one source range bug in clang. It will break
> a user of libclang that might have been compensating for the bug. In
> cases like this we seem to just assume there is a low risk and just
> fix the bug.
Yup.
Speaking for WebKit: we would be happy to get rid of workarounds even if it meant a brief period of breakage. We would handle that breakage on our end by ensuring that we don’t build the new (sans workaround) version of WebKit against the old (pre bugfix) version of LLVM or vice-versa. We’re OK with short-term pain for long-term gain.
>
> * Dropping features like the old JIT. It will break users of the C API
> that depend on the old JIT. In cases like this we provide an upgrade
> path (MCJIT) and a deprecation period.
Yup. I wonder how many people still use the old JIT via the C API. I know of old JIT users but I thought that many (most? all?) were using the C++ API.
-Filip
This is a really good point Filip. I'm totally down with being able to
do the occasional API migration etc. Also since we've got a release
branch now I wonder what the odds are of "we won't break it between
releases" would get? At any rate, being able to migrate API versions
in some clean way would be nice.
I'm almost tempted to want a SWIG wrapped API but I'm pretty sure
that's actually worse than what we've got and still wouldn't solve the
migration issues. ;)
>>
>> * Dropping features like the old JIT. It will break users of the C API
>> that depend on the old JIT. In cases like this we provide an upgrade
>> path (MCJIT) and a deprecation period.
>
> Yup. I wonder how many people still use the old JIT via the C API. I know of old JIT users but I thought that many (most? all?) were using the C++ API.
>
Not sure. I seem to recall a few, but they may have moved off when I
wasn't looking.
-eric
Sure, these are going to be a bit vague because I'm a bit busy at the
moment, but I recall a couple of times during the year that we've had
API up for review (or even committed temporarily) that exposed
internal constants via enums, and I think Rafael had some issues with
visibility changes for the same reasons.
In a more recent case here's a thread:
[LLVMdev] Inconsistent third field in global_ctors (was Re: [llvm]
r214321 - UseListOrder: Visit global values)
and
[PATCH] Add return value attribute to C interface
also I think the conversation we were having in here:
[PATCH] Expose MCInst in C Disassembler API
is somewhat relevant :)
Just a couple of quick things I could find with a search. I could
probably dig up more given some more time.
and
[PATCH] Add return value attribute to C interface
also I think the conversation we were having in here:
[PATCH] Expose MCInst in C Disassembler API
is somewhat relevant :)
Just a couple of quick things I could find with a search. I could
probably dig up more given some more time.
On Aug 5, 2014, at 4:02 PM, Josh Klontz <josh....@gmail.com> wrote:Filip,As a non-WebKit embedder currently using the C++ API (www.liblikely.org), here are my thoughts:- Perhaps the only reason I'm using the C++ API instead of the C API is that the Kaleidoscope tutorial is written against the C++ API. That's where I started, and momentum has prevented me from switching. It may make sense to re-write this tutorial using the C API if we want to encourage new developers to default to this interface.
- Based on the (excellent) documentation online and a close following of this mailing list, I haven't been convinced that the C API is really given first class support in LLVM. Perhaps this is just an issue of advertising better, but it makes me hesitant to change.
- I run into enough minor bugs that my project ends up tracking the master branch pretty closely. As such, I don't think I'll get away from static builds in the near future. Being unable to switch to shared-library-tagged-releases disincentives the API switch.
- A barebones transition guide hosted with the rest of the LLVM docs could lower the activation energy needed to switch.
Hope that helps!
As I said, I haven't spent a lot of time on it. It's friction etc.
How about a theoretical?
Let's say we write a decent PRE pass that encompasses these two passes:
/** See llvm::createMergedLoadStoreMotionPass function. */
void LLVMAddMergedLoadStoreMotionPass(LLVMPassManagerRef PM);
/** See llvm::createGVNPass function. */
void LLVMAddGVNPass(LLVMPassManagerRef PM);
then, why not delete the existing passes? No need keeping dead code
around right? Except we can't because the passes are in the C API and
someone might be using them.
Another example: We've already had multiple C lto APIs, moving those
has only been possible because we just change everyone and there are
only 2 users.
Here's a different theoretical that I'm considering - the
TargetMachine interface:
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
const char *Triple, const char *CPU, const char *Features,
LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, LLVMCodeModel CodeModel);
The last 3 arguments here are a little weird.
Optimization doesn't necessarily make sense within the target machine
and should probably be pulled out somewhere else - perhaps the
PassManagerBuilder? Perhaps somewhere else? The code model and reloc
model make a bit more sense, but are more of an overarching object
file matter for the first. The second is definitely something that
deals with a particular machine. Now, let's say we come up with
extensions to allow multiple architectures generate code within a
single object file. The relocation model will need to be part of some
overarching class on top of the TargetMachine that'll need the
relocation model.
This probably won't be necessary until we hit something like ARM/X86
in the same module. Though with the existing ARM/Thumb support and the
way those subtargets are actually held as target machines we'll
actually need this if we want to handle emitting arm and thumb into
the same module. Now we can try rewriting that support ala mips16 for
the mips port and probably should, but with the increasing relevance
of accelerator computing I see this happening sooner rather than
later. I don't want to close the door on it by having a C API that
can't be revved or changed and is even more extensive than this
relatively odd problem I mention here.
I realize it seems mostly like "aaa! change! aaa! fear!" but we've
been able to do pretty well over the years with the C API by keeping
it very general and having a lot of forethought with what we allow
into it. If we're going to expand the C API greatly (as it sounds like
you want) then I feel we're going to run into issues with wanting to
change the C API and not being able to - hence the request for things
like versioning etc.
They're already banned, so there isn't anything left to determine here,
just code to fix.
> It’s an awkward abstraction when LLVM is embedded. I suspect (6) will be closely related to (1) since command line option parsing was the hardest impediment to getting rid of static initializers.
Yes, for all these reasons. Two libraries may be using llvm under the
hood unaware of each other, they can't both share global state. Command
line flags block that. Our command-line tools should be parsing their
own flags and setting state through some other mechanism, and that state
musn't be more global than an LLVMContext.
> My understanding of the shared library proposal is that the library only exposes the C API since the C++ API is not intended to allow for binary compatibility. So, I think we need to either add the following as either an explicit goal of the shared library work, or as a closely related project:
>
> (7) Make the C API truly great.
>
> I think it’s harmful to LLVM in the long run if external embedders use the C++ API.
The quality with which we maintain the C API today suggests that we
collectively think of it as an albatross to be suffered. There is work
necessary to change that perception too.
I think that one way of ensuring that they don’t have an excuse to do
it is to flesh out some things:
>
> - Add more tests of the C API to ensure that people don’t break it accidentally and to give more gravitas to the C API backwards compatibility claims.
Yes, for well-designed high level APIs like libLTO and libIndex. For
other APIs, we should remove the backwards compatibility guarantees ...
> - Increase C API coverage.
... which in turn allows us to do this.
Designing a good high-level API is hard (even libLTO has very ugly
cracks in its API surface) and that makes it hard to do. What actually
happens is that people write C APIs that closely match the C++ APIs in
order to access them through other languages, but there's no way we can
guarantee compatibility without freezing the C++ API too. Which we never
will. This isn't a theoretical problem either, look at this case:
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140804/229354.html
where we made a straight-forward update to the LLVM IR, but in theory a
user of the C API would be able to observe the difference, and that
could in turn break a C API user that was relying on the way old LLVM
worked.
The solution is to offer two levels of C API, one intended for people to
use to bind to their own language. This matches the C++ API closely and
changes when the C++ API changes. (It could even be partially/wholy
auto-generated via a clang tool?) Users of it will be broken with newer
versions.
Secondly, some people really want a stable interface, so we give them an
API expressed in higher-level tasks they want to achieve, so that we can
change the underlying workings of how LLVM works without disturbing the
API. That can be made ABI stable.
> - For example, WebKit currently sidesteps the C API to pass some commandline options to LLVM. We don’t want that.
Seconded!
> - Add more support for reasoning about targets and triples. WebKit still has to hardcode triples in some places even though it only ever does in-process JITing where host==target. That’s weird.
Sounds good.
> - Expose debugging and runtime stuff and make sure that there’s a coherent integration story with the MCJIT C API.
> - Currently it’s difficult to round-trip debug info: creating it in C is awkward and parsing DWARF sections that MCJIT generates involves lots of weirdness. WebKit has its own DWARF parser for this, which shouldn’t be necessary.
> - WebKit is about to have its own copies of both a compactunwind and EH frame parser. The contributor who “wrote” the EH frame parser actually just took it from LLVM. The licenses are compatible, but nonetheless, copy-paste from LLVM into WebKit should be discouraged.
I am not familiar with the MCJIT C API, but this sounds reasonable. I'll
trust that you know what you're doing.
> - Engage with non-WebKit embedders that currently use the C++ API to figure out what it would take to get them to switch to the C API.
Engage with our users? That's crazy talk! ;)
Nick
Filip Pizlo wrote:This is exciting!
I would be happy to help.On Aug 5, 2014, at 12:38 PM, Chris Bieneman<be...@apple.com> wrote:
Hello LLVM community,
Over the last few years the LLVM team here at Apple and development teams elsewhere have been busily working on finding new and interesting uses for LLVM. Some of these uses are traditional compilers, but a growing number of them aren’t. Some of LLVM’s new clients, like WebKit, are embedding LLVM into existing applications. These embedded uses of LLVM have their own unique challenges.
Over the next few months, a few of us at Apple are going to be working on tackling a few new problems that we would like solved in open source so other projects can benefit from them. Some of these efforts will be non-trivial, so we’d like to start a few discussions over the next few weeks.
Our primary goals are to (1) make it easier to embed LLVM into external projects as a shared library, and (2) generally improve the performance of LLVM as a shared library.
The list of the problems we’re currently planning to tackle is:
(1) Reduce or eliminate static initializers, global constructors, and global destructors
(2) Clean up cross compiling in the CMake build system
(3) Update LLVM debugging mechanisms for being part of a dynamic library
(4) Move overridden sys calls (like abort) into the tools, rather than the libraries
(5) Update TableGen to support stripping unused content (i.e. Intrinsics for backends you’re not building)
Also:
(6) Determine if command line options are the best way of passing configuration settings into LLVM.
They're already banned, so there isn't anything left to determine here, just code to fix.
It’s an awkward abstraction when LLVM is embedded. I suspect (6) will be closely related to (1) since command line option parsing was the hardest impediment to getting rid of static initializers.
Yes, for all these reasons. Two libraries may be using llvm under the hood unaware of each other, they can't both share global state. Command line flags block that. Our command-line tools should be parsing their own flags and setting state through some other mechanism, and that state musn't be more global than an LLVMContext.
I was thinking about this a while ago w.r.t. the backend options
coming from clang as well. One thought would be to add
elements/structures onto something like the LLVMContext and have them
be able to be set via populating a structure while we take the command
lines out/separated/etc that can also fill in the structure.
I think this would work for most of the command line options within
the passes as well, just give each one their own options structure?
*shrug*
-eric
On Aug 6, 2014, at 12:00 AM, Nick Lewycky <nich...@mxc.ca> wrote:
-eric
> I haven’t yet worked out the cleanest way to do this. There’s 2 options II was thinking about this a while ago w.r.t. the backend options
> think. Either make some kind of command line registration available via the
> PassRegistry already passed to initialize, or we add another parameter to
> initialize which is the command line registry.
>
> What do you think?
>
coming from clang as well. One thought would be to add
elements/structures onto something like the LLVMContext and have them
be able to be set via populating a structure while we take the command
lines out/separated/etc that can also fill in the structure.
I think this would work for most of the command line options within
the passes as well, just give each one their own options structure?
Worth noting is that we do not use the C interface and have little
interesting in ever doing so. We've chosen to accept the version lock
as a necessary evil - mostly because we need enough internal changes to
LLVM we'd be stuck with it anyway. I'm mostly just pointing this out
for the purpose of contrast with other responders on the thread.
I would honestly love to see a slightly more stable C++ interface, but
I've mostly accepted that's not going to happen. I'm not even talking
stability from release to release; I'd be thrilled with depreciation
periods in terms of days or weeks. We're generally fairly in sync with
ToT, but even with that, we face a lot of breaking changes. There's a
general assumption that only llvm subprojects need to be migrated, and
as soon as that's complete, old APIs are removed. Simply having a
slightly longer period to do our own migrations would be immensely helpful.
Philip