1

I am trying to generate a bit code from a c++ source code and running through the just-in-time compiler. When I compile through the clang++ and generate binary executable it runs perfectly but when I generated the bitcode and tried running through the JIT with lli command it generates run-time error. Could you please help me understanding what's going on.

For example: Let example.cpp contains the following code:

 #include <iostream>

 int main(){
    std::cout << "\nHello World!";
    return 0;
 }

I am using the following command to generate executable which runs perfectly fine.

clang++ example.cpp 

I am using the following command to generate the bitcode:

clang++ -S -emit-llvm example.cpp 

And then running through the JIT using the following command which generates run-time error:

lli example.ll 

I am getting the following access violation error:

Stack dump:
0.      Program arguments: lli example.ll
#0 0x00000000025fd9af llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:564:0
#1 0x00000000025fda42 PrintStackTraceSignalHandler(void*) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:625:0
#2 0x00000000025fb7ca llvm::sys::RunSignalHandlers() /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Signals.cpp:68:0
#3 0x00000000025fd329 SignalHandler(int) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:406:0
#4 0x00007fa75dbdc390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
Segmentation fault (core dumped)

Leer
  • 11
  • 1
  • Not exactly sure what's happening, but with a bit of testing seems that the issue is C++ (C code produces no issue, and as does C like C++ generated by `clang++`). Not sure if `lli` expects some arguments to support C++ bytecode(explicitly specifying external shared objects) or what. Unfortunately I couldn't find example of C++ bytecode compilation/use, but good luck with your search – Lala5th Aug 21 '21 at 04:27

2 Answers2

1

Try compiling with clang++ -S -emit-llvm -fno-use-cxa-atexit example.cpp.

I think it's probably because clang and gcc try prefer __cxa_atexit to atexit by default (these are functions used for cleaning up global objects when a program exits). Meaning you'll get a linker error if your libc implementation doesn't support the former. So disabling the use-cxa-atexit flag should work.

droptop
  • 1,372
  • 13
  • 24
0

It seems that it's a library linking issue. Try to use the followings:

  1. Try lli -force-interpreter example.ll instead of lli example.ll When JIT can't work properly, you should try interpreter. if the following err: LLVM ERROR: Could not resolve external global address: __dso_handle Then add -fno-use-cxa-atexit to clang flags, reasons from [LLVMdev] MCJIT/interpreter and iostream.
  2. After retry lli -force-interpreter example.ll if still have some errors like LLVM ERROR: Tried to execute an unknown external function: .... Then you should recompile LLVM with ffi library enabled. See the following for reasons: Advice on Packaging LLVM¶ and [LLVMdev] lli --force-interpreter does not find external function
hddmss
  • 231
  • 1
  • 12