I have generated the following llvm ir at runtime and I am looking to execute the function immediately
;other declarations/functions
define %gen__struct__81ty_struct.catch_val @gen__fun__115(void**) {
entry:
%1 = getelementptr void*, void** %0, i32 0
%2 = load void*, void** %1, align 8
%3 = bitcast void* %2 to i64*
store i64 1, i64* %3, align 8
%4 = load i64, i64* %3, align 8
%5 = getelementptr void*, void** %0, i32 1
%6 = load void*, void** %5, align 8
%7 = bitcast void* %6 to i64*
store i64 1, i64* %7, align 8
%8 = load i64, i64* %7, align 8
%9 = getelementptr void*, void** %0, i32 2
%10 = load void*, void** %9, align 8
%11 = bitcast void* %10 to i64*
store i64 1, i64* %11, align 8
ret %gen__struct__81ty_struct.catch_val zeroinitializer
}
I then use the kaleidoscope jit and the following C++ code to compile and executed the above function but I keep getting segfault.
Function* out = 0;
//code that generates the function ...
runFunc fptr = 0;
jit->addModule(std::move(gblDevice->lmod));
fptr = (runFunc)jit->getPointerToFunction(out);
vector<void*> params;
uint64_t a = 0;
uint64_t b = 0;
uint64_t c = 0;
params.push_back(&a);
params.push_back(&b);
params.push_back(&c);
catch_val_pt cval = fptr(¶ms[0]);
I have tried for hours to work out what is wrong with the above code but everything seems to check out.
The getPointerToFunction function is.
class OrcJIT {
// ...
JITSymbol findSymbol(const std::string Name) {
std::string MangledName;
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, Name, DL);
return CODLayer.findSymbol(MangledNameStream.str(), true);
}
inline void* getPointerToFunction(Function* F) {
return (void*)findSymbol(F->getName().data()).getAddress();
}
//...
}