5 ms·
Don't compile to C. It's very difficult to avoid undefined behavior, you won't be able to modify the compiler backend (and you will want to as you start optimiz
by pcwalton 11y ago
Don't compile to C. It's very difficult to avoid undefined behavior, you won't be able to modify the compiler backend (and you will want to as you start optimizing), you can't add new language intrinsics, good GC is pretty much incompatible with that, and precise control over debug info is impossible. Compile to LLVM IR and/or GCC IR instead.
- tachyonbeam 11y agoDo you know if you can compile to a text form of the LLVM IR? Also wondering how you'd interface with a GC if you did that.
- heinrich5991 11y agoI believe LLVM IR, even in its textual form, is not considered a stable interface.
- pcwalton 11y agoIt's not, but keeping up with upgrades generally isn't a problem. Not enough to outweigh the downsides of compiling to C.
- tachyonbeam 11y agoAnd to be fair, it's not like the LLVM C API is going to remain the same forever, that changes too.
- erickt 11y agoLast I worked on llvm-c, it was supposed to stay backwards compatible. They do add new functionality but the old API should still work until llvm 4. For rust, while we have been broken by bad code we generated and llvm bugs, I can't recall if the API ever was changed out from under us.
- pcwalton 11y agoYeah, you can compile to LLVM IR in assembly format and then run the result through llc. But you will pay in compilation time for this due to the serialization and deserialization steps, so I can't see a situation in which it'd be worth it to do so. The LLVM GC interface is controlled through intrinsics, so you can access it through the human-readable IR representation just as you can with the API.
- andrewchambers 11y ago> so I can't see a situation in which it'd be worth it to do so. The obvious one is not having a giant build time dependency that might change and be at different versions on different platforms. The text interface is far more stable. The problem I had with doing this is how picky llvm is when it comes to ordering your temporary registers.
- deleted 11y ago[deleted]
- Gankro 11y agoAs far as I've seen, the text interface is radically more unstable. Like when they recently striped types from pointers, I was like "oh no that sounds like it'll be a lot of work to upgrade to", and all the backend peeps seemed to think it wasn't a big deal because LLVM's API handled it. Particularly when you factor in that any breakage in the direct LLVM API can often be resolved by looking at what Clang did when it upgraded.
- andrewchambers 11y agoThat is a shame then.
- BruceM 11y agoIn Open Dylan (http://opendylan.org/ http://opendylan.org/), we have a library of our own written in Dylan that lets us write out LLVM IR in binary files. We have to update it as LLVM changes, but that typically isn't all that bad. We have some macros and a bunch of functions then that make emitting LLVM IR simpler. For example, the "ins--iterate" and "ins--if" / "ins--else" macros in this code handle phi nodes and so on: https://github.com/dylan-lang/opendylan/blob/05271f6fec9da05ef64c7cbba6c9c0fd271d88d7/sources/dfmc/llvm-back-end/llvm-entry-points.dylan#L522 https://github.com/dylan-lang/opendylan/blob/05271f6fec9da05... This library is currently in the process of being updated from LLVM 3.5 to current HEAD as debug info has changed significantly upstream. After that, we will be landing some documentation for it as well. If you're interested in learning or hearing more about this, we're typically around on #dylan on freenode IRC.
- mwcampbell 11y agoWhy is good GC basically incompatible with compiling to C? Because there's no portable way to scan the stack for live objects?
- pcwalton 11y agoThat, and the fact that you can't tell the compiler that it needs to be aware of pointers that can move across function calls. In fact, there isn't any dialect of C, standard or not, that I'm aware of that allows you to do this in a fashion that works with precise, moving GC. (Mostly-copying doesn't count.) Maybe C++/CLI, but the object model you're opting into with that brings you so far from C as to barely be considered "compiling to C".
- saurik 11y agoAdd a hidden argument to every function marked as capable of transient reference: a pointer to a pointer to a structure (just another object) which serves as a "stack frame". In that function, if you do reference stuff, build your own "stack frame" and use that to maintain a shadow stack. If you allow threads to transiently access objects between "sync points" this won't even be all that inefficient, as you will only be using it to track sub-graph roots as you make calls to other functions... for various reasons many of the more legit mechanisms have that same limitation anyway (where threads are only stopped to be scanned at moments they mark as safe). Maybe you don't consider this to be "good gc", but if your criteria is simply "precise GC with only a minor performance penalty" I don't see any reason to doubt that C is up for the challenge. (And as you are uniquely aware, one would hope that if you were buying the static compilation argument you are also likely to not stress a garbage collector much in the first place, so I would expect the shadow stack to get very little use.) Is there something I'm missing? I really would be interested in a more expansive explanation as to why building a GC with C as a compilation target (as opposed to as a library for people you are expecting to program in C) is considered such a serious showstopping issue.
- pcwalton 11y agoThat's precise GC with a huge performance penalty, not a minor one. The problem is not so much the spilling and reloading (though that is a large issue) as what that is going to do to the ability of the optimizer to reason about the code. (If you're curious how I'm so sure it's a huge performance penalty, I have been spending a fair amount of time of the last month working with others to debug large performance problems caused by a variant of exactly this technique used in Servo.) :)
- azakai 11y agoA long-time disagreement between us :) > [in C] It's very difficult to avoid undefined behavior, True, but LLVM IR and other IRs have undefined behavior as well. > you won't be able to modify the compiler backend (and you will want to as you start optimizing), Not all languages need backend changes. And you can still add IR optimizations or backend changes that help your language, if you do. Yes, this might not be as easy, but then if you compile to LLVM IR, you might need IR changes for your special things anyhow. > you can't add new language intrinsics, good GC is pretty much incompatible with that, Good point, for most GC languages compiling to C is not a good option, which makes sense since C has no GC support. > and precise control over debug info is impossible. Yes, but using the C preprocessor you can get pretty far. > Compile to LLVM IR and/or GCC IR instead. More power that way, sure, but 1. It limits you to one compiler, while C has many. 2. LLVM IR changes over time (and recently had plenty of examples of this), so you'll need to track that. Whereas C is extremely stable, so long-term, it's much less work. 3. Emitting C is very easy (for those familiar with C), and also very easy to debug. I think compiling to C is an excellent option. Sometimes better, sometimes worse.
- kibwen 11y ago> True, but LLVM IR and other IRs have undefined behavior > as well. LLVM IR has less undefined behavior than C, and even despite the reduced surface area frontends have still historically wrestled with taming its UB (both PNaCL and Rust have documented their struggles). > It limits you to one compiler, while C has many. To quote Walter Bright on this topic (which I've linked elsewhere in this thread, but here it is again: http://forum.dlang.org/post/n1vbos$11ov$1@digitalmars.com http://forum.dlang.org/post/n1vbos$11ov$1@digitalmars.com): > 1. You're at the mercy of bugs in the C compiler you > cannot fix. > 2. C leaves quite a lot as "implementation defined", > causing endless compatibility issues with various C > compilers. > [...] > 6. You'll suffer from endless bug reports caused by a > mismatch between your compiler and the user's C > compiler, whatever that might be. > [...] > 10. The order of evaluation of C code expressions is > implementation defined. > 11. Installation problems, again, because you don't > control the user's C compiler.
- 11y ago
- kibwen 11y agoRelated, here's Walter Bright on the difficulties of compiling to C: http://forum.dlang.org/post/n1vbos$11ov$1@digitalmars.com http://forum.dlang.org/post/n1vbos$11ov$1@digitalmars.com
- pcwalton 11y agoYes, he brings up some excellent points I hadn't considered. Sequence points and integral promotions are bugs waiting to happen.