Issue 73527
Summary How can I JIT a multiple function IR file with ORC JIT?
Labels new issue
Assignees
Reporter Thrrreeee
    I'm trying to use ORC JIT to compile a .ll file, but I'm encountering some issues. 
When I attempted to compile a .ll file containing multiple functions using ORC JIT, the following error occurred.
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ./JIT_testSymbol_NoOtherFunc
 #0 0x0000ffff8a3513bc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/jinrui/llvm-project-main/llvm/lib/Support/Unix/Signals.inc:723:11
 #1 0x0000ffff8a3518c4 PrintStackTraceSignalHandler(void*) /home/jinrui/llvm-project-main/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x0000ffff8a34fa58 llvm::sys::RunSignalHandlers() /home/jinrui/llvm-project-main/llvm/lib/Support/Signals.cpp:105:5
 #3 0x0000ffff8a35211c SignalHandler(int) /home/jinrui/llvm-project-main/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x0000ffff9453578c (linux-vdso.so.1+0x78c)
 #5 0x0000ffff89f15bec std::ostream::sentry::sentry(std::ostream&) (/lib/aarch64-linux-gnu/libstdc++.so.6+0x128bec)
 #6 0x0000ffff89f16438 std::basic_ostream<char, std::char_traits<char>>& std::__ostream_insert<char, std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>>&, char const*, long) (/lib/aarch64-linux-gnu/libstdc++.so.6+0x129438)
 #7 0x0000ffff89f16930 std::basic_ostream<char, std::char_traits<char>>& std::operator<<<std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>>&, char const*) (/lib/aarch64-linux-gnu/libstdc++.so.6+0x129930)
 #8 0x0000ffff89fe40d0
 #9 0x0000aaaad3925b70 main /home/jinrui/llvm-project-main/llvm/examples/OrcV2Examples/JIT_testSymbol.cpp:41:7
#10 0x0000ffff89bc9e10 __libc_start_main /build/glibc-RIFKjK/glibc-2.31/csu/../csu/libc-start.c:342:3
#11 0x0000aaaad3925848 _start (./JIT_testSymbol_NoOtherFunc+0x5848)
Segmentation fault (core dumped)
```
Why does this happen? I tried a similar approach successful when compiling a simple 'helloworld' program, but when I switched to a program with multiple functions, errors occurred. Could this be related to _symbols_ or does JIT require a special method to handle Modules with multiple functions? I'm currently having trouble understanding this.
Here is my main JIT program code.
```CPP
#include "llvm/ADT/StringMap.h"
#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"

#include "ExampleModules.h"

using namespace llvm;
using namespace llvm::orc;

ExitOnError ExitOnErr;

int main(int argc, char *argv[]) {
  // Initialize LLVM.
  InitLLVM X(argc, argv);

 InitializeNativeTarget();
  InitializeNativeTargetAsmPrinter();

 cl::ParseCommandLineOptions(argc, argv, "LLJITDumpObjects_RepeatLookup");
 ExitOnErr.setBanner(std::string(argv[0]) + ": ");

  auto J = ExitOnErr(LLJITBuilder().create());

  auto M1 = ExitOnErr(parseExampleModuleFromFile("/home/jinrui/llvm-project-main/sjr-test/testMutilFunction.ll"));

 ExitOnErr(J->addIRModule(std::move(M1)));
 
  // Look up the main function
  auto MainAddr = ExitOnErr(J->lookup("main"));
  int (*runmain)() = MainAddr.toPtr<int()>();

  int result = runmain();
 outs() << "exit " << result << "\n";
  return 0;
}
```
Here is my original CPP code, which I compiled into an .ll file using the command 'clang -S -emit-llvm helloWorld.c -o helloWorld.ll'.
```cpp
#include <iostream>

// function 1: add a and b
int add(int a, int b) {
 return a + b;
}

// function 2:multiply a and b
int multiply(int a, int b) {
    return a * b;
}

// function 3: printf a string
void printMessage(const std::string& message) {
    std::cout << "Message: " << message << std::endl;
}

int main() {
    int x = 5, y = 3;

    // call function 1
    int sum = add(x, y);
 std::cout << "Sum: " << sum << std::endl;

    // call function 2
 int product = multiply(x, y);
    std::cout << "Product: " << product << std::endl;

    // call function 3
    std::string message = "Hello, functions!";
    printMessage(message);

    return 0;
}
```
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to