3 ms·
The thing about llvm ir is you get a code builder "for free" from llvm, where you're just calling functions to build your ir. However, when emitting C you have
by RX14 9y ago
The thing about llvm ir is you get a code builder "for free" from llvm, where you're just calling functions to build your ir. However, when emitting C you have to build that yourself and take care to always emit valid code. Not sure how much difference that actually makes though.
- lucozade 9y agoI'd actually argue the other way. If you emit invalid C code, you'll at least be told by the compiler (though the error might be pretty obscure). LLVM tends to assume that you are emitting valid IR and panics when you don't. BTW this isn't a criticism of LLVM (far from it). It's expected to be used as a backend so any invalid IR is, pretty much by definition, a programmer error. At the quick and dirty level, IMO C wins here as you can just splat the C you're building to a file and inspect it. With LLVM you need to get through the hurdle of building valid IR before you can serialise it. In the longer term, I would argue that LLVM is better as you have way more control over what you're generating. Once your lowering works then the issue above is irrelevant.
- jcranmer 9y ago> I'd actually argue the other way. If you emit invalid C code, you'll at least be told by the compiler (though the error might be pretty obscure). LLVM tends to assume that you are emitting valid IR and panics when you don't. LLVM does have a verifier pass which should catch every invalid IR. However, optimization passes aren't necessarily robust against weird types (I don't expect <13 x i3> to go over very well), and the backends are liable to flip out if the IR isn't in the subset they expect (e.g., weird types).
- pjmlp 9y agoThere is another way to track down LLVM IR errors. Instead of serializing it, directly write as text and process the file with the LLVM IR assembler. Granted, it is still harder as just using C, but a step closer to LLVM and easier to debug.
- ChickeNES 9y ago>However, when emitting C you have to build that yourself and take care to always emit valid code. Not sure how much difference that actually makes though. I'm kind of surprised that there's not a library for emitting C code, though maybe my Google-fu is failing.
- shadowfox 9y agoI have used CIL [1] to generate C code before. Not exactly its main purpose, but it works nonetheless. Essentially you generate a C-ast and then pretty print it. It has some limitations though. [1] https://github.com/cil-project/cil https://github.com/cil-project/cil