ジョウェットジェームス via llvm-dev
2016-Oct-27 10:05 UTC
[llvm-dev] What was the IR made for precisely?
Hi, wanting to build up some language of my own, I have always thought about feeding my compiler output to a C compiler like gcc or clang, however when I learned about (considered using) LLVM IR I immediately thought it could be of some help. LLVM doesn't seem to be portable though, some people seem to say "If you are generating C then why not go all the way and generate some intermediate representation like LLVM IR?", but what do they mean? C is like a portable assembly, I just have to check my numeric limits and be careful about portability and it's OK. With LLVM IR I will have to take the target platform into consideration when I compile. This leads to my second question, what is the true goal of LLVM IR? I feel like some people are disagreeing on this aspect. JJ iPhoneから送信
David Chisnall via llvm-dev
2016-Oct-27 15:18 UTC
[llvm-dev] What was the IR made for precisely?
On 27 Oct 2016, at 11:05, ジョウェットジェームス via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > Hi, > wanting to build up some language of my own, I have always thought about feeding my compiler output to a C compiler like gcc or clang, however when I learned about (considered using) LLVM IR I immediately thought it could be of some help. > > LLVM doesn't seem to be portable though, some people seem to say "If you are generating C then why not go all the way and generate some intermediate representation like LLVM IR?", but what do they mean? > > C is like a portable assembly, I just have to check my numeric limits and be careful about portability and it's OK. With LLVM IR I will have to take the target platform into consideration when I compile.How do you represent exceptions in C (including your own language’s notion of what your types are for catches and cleanups)? Weak linkage? Comdats? Overflow-checked arithmetic? Arbitrary-sized vectors? Some of these are possible if you’re willing to rely on compiler-specific builtins, but then you’re not really generating portable C anymore.> This leads to my second question, what is the true goal of LLVM IR? I feel like some people are disagreeing on this aspect.LLVM IR is intended as a compiler intermediate representation. It aims to be able to express anything that arbitrary source languages might want and anything that target architectures may implement, in a way that is not closely tied to either specific source languages or target architectures. It doesn’t entirely succeed in this, but it comes a lot closer than C to this goal (not surprising, as this was never a goal of C). David
Renato Golin via llvm-dev
2016-Oct-27 15:30 UTC
[llvm-dev] What was the IR made for precisely?
On 27 October 2016 at 16:18, David Chisnall via llvm-dev <llvm-dev at lists.llvm.org> wrote:> LLVM IR is intended as a compiler intermediate representation. It aims to be able to express anything that arbitrary source languages might want and anything that target architectures may implement, in a way that is not closely tied to either specific source languages or target architectures. It doesn’t entirely succeed in this, but it comes a lot closer than C to this goal (not surprising, as this was never a goal of C).Yeah, "LLVM IR is a compiler IR, not a target-independent language". Lowering your language to LLVM IR is a great way to reduce the cost of target-specific knowledge in your front-end 10-fold. But there's still the remaining 1/10th that you need to do. Exception handling is one obviously complicated one, but there's also: * Procedure-call standard differences: most arguments will marshal correctly in LLVM IR, but complex structures sometimes need to be lowered to arrays, or moved around. * Type sizes: The LLVM lowering is smart enough (in most cases) to split too-large values (64-bit int on 32-bit machine), but that's error prone and inefficient. You should emit the best types per architecture. * Your language's ABI: C++ has an ABI which defines the layout of classes and objects that can be different between targets. You may want to make special concessions due to speed and code-size concerns. Pretty much everything else should be dealt with by the IR passes and lowering. But the biggest benefit of lowering to LLVM IR is safety. Lowering to C will actually be lowering to "strings", not proper C. Unless you write your own C validation engine (ie. a compiler), you can't know if you have lowered to valid C or not. The IR libraries (IRBuilder et al) give you that for free. This immensely simplifies the task of lowering your language, and you can focus on what really matters: the semantics of your language. Hope that helps. --renato
Chris Lattner via llvm-dev
2016-Oct-27 20:57 UTC
[llvm-dev] What was the IR made for precisely?
> On Oct 27, 2016, at 3:05 AM, ジョウェットジェームス via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > Hi, > wanting to build up some language of my own, I have always thought about feeding my compiler output to a C compiler like gcc or clang, however when I learned about (considered using) LLVM IR I immediately thought it could be of some help. > > LLVM doesn't seem to be portable though, some people seem to say "If you are generating C then why not go all the way and generate some intermediate representation like LLVM IR?", but what do they mean? > > C is like a portable assembly, I just have to check my numeric limits and be careful about portability and it's OK. With LLVM IR I will have to take the target platform into consideration when I compile. > > This leads to my second question, what is the true goal of LLVM IR? I feel like some people are disagreeing on this aspect.Here is an introduction: http://www.aosabook.org/en/llvm.html It has this to say about generating C code: "Unfortunately, doing this prevents efficient implementation of exception handling, provides a poor debugging experience, slows down compilation, and can be problematic for languages that require guaranteed tail calls (or other features not supported by C)." -Chris -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20161027/8bb218c7/attachment.html>
ジョウェットジェームス via llvm-dev
2016-Oct-28 01:43 UTC
[llvm-dev] What was the IR made for precisely?
Thank you for your answers. In fact I'm still hesitating between C and C++, sorry for my incomplete message. What I want to write is some sort of DSL; so I can afford both I think. I'm thinking about generating standard-compliant code using numeric limits and stuff; given that I don't care about compilation time or debugging, I think I can still use C++ in case I decide to include exceptions or other things hard to implement in C. I understood from your replies that I still have a small part of target specific code to write, but honestly I don't know about all the main targets enough to do this correctly and efficiently I think. I would need to sum up all the rules and ABIs and sizes for all the targets I need and generate different IR for each, am I correct? I don't even know if there are many off such rules, or even where they are all defined precisely. I have used assembly and read processor manuals etc before but that was obviously never for writing cross platform code.