0

Generally speaking, when writing a llvm frontend, one will take an AST and first check that its semantics is well-defined. After this, one will take the AST and perform the IR build phase. I was wondering, how realistic is to perform directly the IR build phase onto the AST, and if errors are found during the build process, revert any partial changes to the module object?

I assume something like this would be required:

  • remove defined Types
  • remove defined Globals
  • anything else i'm missing?

Any ideas about this? what are the general guidelines of what needs to done for a clean revert of module changes after a failed build phase?

Now, this is thinking in terms of optimistically compiling, and failing gracefully it somethings goes wrong. It might very well be that this is completely impossible or discouraged under the current LLVM model. A clear and well-documented answer in this regard is also completely acceptable

Edit In the end, I just want a reasonable way to add functions incrementally but revert gracefully to previous state of module and/or LLVMContext if a function build fails. Whatever is the preferred approach for that will be entirely satisfactory.

thanks!

lurscher
  • 25,930
  • 29
  • 122
  • 185

1 Answers1

1

Many compilers (not necessarily LLVM-related) mix semantic analysis with code generation, so it can definitely be done. However, I'm puzzled by your reference to "revert any partial changes to the module object". When you start building an IR module and encounter a semantic error in the AST, what is your plan? Do you want to spit an incomplete module? Why? Thinking about the way any regular compiler works, if there are semantic errors in the code (i.e. reference to an undefined variable), no output is created. Would you like something different?

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Thanks for replying - well, the idea is that the code (i.e: the module) should be kept live (for JIT purposes) even if an attempt to build/add a new function fails. From what you say i infer that maybe i need to transfer existing/validated functions to a separate module and build new functions always in a new module? – lurscher Feb 03 '12 at 15:13
  • @lurscher: if you're adding build functions to a module incrementally, can't you just wait until the function is built completely before adding it? If the build failed, you don't add anything to the module. If it succeeded, you add a complete function. – Eli Bendersky Feb 03 '12 at 15:24
  • that is exactly my question: can i add a function (and the associated types) *after* the function/types are successfully built? my understanding is that as you start doing things like CreateFunction or CreateType you are already modifying the module – lurscher Feb 03 '12 at 15:34
  • @lurscher: I'm pretty sure you can just add a function to a module. Take a look at the LLVM linker - it merges modules so it must move functions around – Eli Bendersky Feb 03 '12 at 18:05
  • `llvm::Function::Create` takes as last parameter the function module. Are you saying that i can call this function **after** all the blocks, instructions, etc. are correctly generated? assuming it is so, if something goes wrong, i can simply not call it? can i still free/recover/reuse the generated blocks/instructions or they will keep dangling on the `LLVMContext`? **OR**, maybe the approach i suggested on my first comment to you would be preferred? (That is assuming that separate modules in separate LLMVContexts can still exchange content) - Thanks for any suggestions you might have on this! – lurscher Feb 04 '12 at 17:53
  • @lurscher: I think you'd have a better chance of getting a good answer to this idea if you just open a whole new question and explain clearly what you want to achieve. I think it was now boiled down to a simpler, technical question (basically, can you build a function somewhere and only move it into a module when it's done), which I'm sure has a good answer. You'll have more eyes looking on it, since at this point there's a good chance that only I am reading this comment chain – Eli Bendersky Feb 05 '12 at 04:36
  • good grief i think you are right. Here it is (http://stackoverflow.com/q/9150190/170521) thanks! – lurscher Feb 05 '12 at 15:12