News for hackers. Stuff that matters.
Re:Amplifying C (2010) by comex on Sunday February 07, 2016 @07:28PM (#11055754)
> Is it more portable in a way that matters though?

A few months ago when I was trying to compile swiftc's IR with emscripten, I would have said "definitely yes". It didn't work both because of a LLVM version mismatch and because, well, the NaCl backend passes were apparently only tested on IR generated by Clang - random differences in what swiftc generated, like using 'add' with a constant right hand side instead of get-element-ptr, not simplifying struct returns to return-via-pointer-argument, etc. made it variously abort or outright crash. As you know, Rust has never really worked with it either due to the version mismatch.

Admittedly, though, that's mainly emscripten's fault, and I expect the new WebAssembly backend in trunk to be much more robust. Other than that, a few reasons I can think of to want to compile to C:

- Performance. Other compiler backends often produce more efficient code than LLVM; not always, but it's nice to be able to test several independent production-quality C compilers and pick the fastest, while with LLVM IR you're stuck with what you have. Same goes for compilation speed, though LLVM usually ranks well at that, and even working around optimizer bugs.

- Windows support for LLVM is still not up to par yet. Supposedly it will be in the near future.

- (edit) Easier to integrate with unusual platform-specific compilation modes, like C++/CLI or Apple's LLVM bitcode distribution (well, that is LLVM, but imagine in the future someone comes up with a similar system based on a different compiler).

- You can distribute the C output from your compiler, and others can use it without having to work your compiler into their build process. Like distributing object files, but those only work on one platform, while C works anywhere. (Caveat: compilers often need to know data layout, which in practice means you might need to have separate C outputs for 64-bit and 32-bit pointer sizes, and not support any more exotic data representations. But that's still way more portable than an object file.) Notably, even if you only need to distribute to people using LLVM, LLVM IR is not designed to be cross-platform.

Incidentally, for this reason, I'd love if someone made a Rust to C compiler, preferably working on the AST level to avoid the unreadable spaghetti that the LLVM C backend used to generate. It's easier to say "swap out this insecure C library with a secure Rust library" (to a larger C/C++ program) if doing so is a matter of swapping C files - distributing 'binaries' is suboptimal from a maintainability perspective, but not much worse than, e.g., SQLite's "amalgamation" distribution.

Re:Amplifying C (2010) by pcwalton on Sunday February 07, 2016 @07:47PM (#11055817)
> - Performance. Other compiler backends often produce more efficient code than LLVM

I wouldn't say "often" here. LLVM's compiler backend is top-notch and is hard to beat. Often it comes down to random register allocation or scheduling differences.

> - Windows support for LLVM is still not up to par yet. Supposedly it will be in the near future.

What are you referring to in particular? The only thing I can really think of is exception handling, and that got overhauled recently for MSVC compatibility. PDB was an issue too, but that's gotten fixed and now LLVM can output PDB. I can't think of much that's left...

> - You can distribute the C output from your compiler, and others can use it without having to work your compiler into their build process.

That is a legitimate advantage. But on the whole I think that it's outweighed by the downsides of compiling to C.

Re:Amplifying C (2010) by comex on Sunday February 07, 2016 @10:06PM (#11056169)
> PDB was an issue too, but that's gotten fixed and now LLVM can output PDB. I can't think of much that's left...

This is what I was referring to. The RFC from a Microsoft employee on full CodeView (what gets compiled to PDB) support was posted three months ago, but AFAIK the implementation is still in progress. The preexisting support for CodeView is described by the documentation as "minimal" as it contains only line tables.

Re:Amplifying C (2010) by pcwalton on Sunday February 07, 2016 @11:11PM (#11056286)
But when compiling to C you can't do much better than #line. So it doesn't strike me of much of an argument in favor of compiling to C.
Re:Amplifying C (2010) by pcwalton on Sunday February 07, 2016 @11:18PM (#11056310)
Oh, and regarding the request for a Rust AST to C compiler, I hope that doesn't happen for a number of reasons. First, it undoes the work we're doing on MIR, which is very important (you couldn't run MIR-level optimizations, the borrow check and codegen would be at high risk of divergence, we'd have to take a big backwards step to serializing ASTs for generics, etc.) Second, it'd be very hard to dodge C's undefined behavior: think of what we'd have to do to make signed integer overflow crash cleanly instead of leading to UB, just to name one particularly egregious problem...
Re:Amplifying C (2010) by comex on Monday February 08, 2016 @01:22PM (#11059868)
I don't see how signed overflow is hard: whenever you see a signed arithmetic operation you just emit, e.g., `add_i32(a, b)` instead of `a + b`, and then include definitions of those functions for each signed integer type (all 4 of them) which are 1-4 lines long.

Strict aliasing and pointer rules, on the other hand, definitely are hard if you want to produce truly standard C code: you have to completely bypass C's type system, using `char *` or `uintptr_t` for everything, and while this can be done, the resulting code is likely to look pretty ugly. However, a reasonable alternative is to depend on nonstandard annotations to disable TBAA: `__attribute__((may_alias))` for every popular compiler other than MSVC, which I believe doesn't do TBAA at all. (If MSVC ever adds it, the generated C code would need updating, but that's not the end of the world.)

As for MIR - while I said AST, I suspect it would be fine to do it on MIR, at the cost of having to reconstruct some control flow. The biggest problem with doing it on LLVM IR is the simplified type info, while the full info is still there for MIR, right? Not an expert. Anyway, it's just an idea.

Re:Amplifying C (2010) by zem on Monday February 08, 2016 @02:19AM (#11056778)
I believe that is one of the reasons ats compiles to C - you can write a library that makes use of the advanced compile-time type features, but which can then be distributed as C code
Re:Amplifying C (2010) by Ono-Sendai on Sunday February 07, 2016 @07:36PM (#11055783)
LLVM works fine on Windows. Clang is not feature complete on Windows however.