Crash using exceptions

Hello,

I get a crash in my program that uses exceptions and the LLVM JIT,
even though the exceptions are controlled and thrown/catched in a part
that doesn't deal with LLVM. I noticed that llvm-config --cxxflags
includes the -fno-exceptions flag. Do I need to throw no exceptions
whatsoever in my application to use LLVM JIT?

As a minimal example, I modified the code in
llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2 at master · llvm-mirror/llvm · GitHub.
In toy.cpp, I update LogError to throw an exception:

std::unique_ptr<ExprAST> LogError(const char *Str) {
  fprintf(stderr, "Error: %s\n", Str);
  throw std::runtime_error("");
}

and I catch that exception in MainLoop:

static void MainLoop() {
  while (true) {
    fprintf(stderr, "ready> ");
    try {
      switch (CurTok) {
      ...
      }
    } catch (std::runtime_error &e) {
      fprintf(stderr, "%s\n", e.what());
    }
  }
}

I compile with:

clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs
--libs core orcjit native` -O3 -fexceptions -o toy
./toy

var 3 = 2
Error: expected identifier after var

1

terminate called after throwing an instance of 'std::bad_alloc'
  what(): std::bad_alloc
zsh: abort ./toy

Technical details:
- LLVM latest master (commit 2c4ca6832fa6b306ee6a7010bfb80a3f2596f824)
on GitHub - llvm-mirror/llvm: Project moved to: https://github.com/llvm/llvm-project
- clang version 8.0.0-3

Thank you,

Hi Julien,

You can turn on exceptions for your example by adding ‘-frtti -fexceptions’ to your clang invocation:

clang++ -g toy.cpp llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native -frtti -fexceptions -O3 -fexceptions -o toy

You can also enable exception support and RTTI in your LLVM builds by adding -DLLVM_ENABLE_RTTI=On -DLLVM_ENABLE_EH=On to your CMake invocation.

Cheers,
Lang.

Hi Lang,

Thank you for your reply. I didn't know about LLVM_ENABLE_RTTI and
LLVM_ENABLE_EH, I'll give it a try. I realized yesterday that my
minimal example doesn't actually capture the error I get in my real
application, my mistake. I'll investigate all of this and come back
with a better minimal example if I can't figure it out on my own.

Thanks again!