JIT: Mapping global variable in JIT'ted code to variable in running program

Hi,

I think this is probably easiest to explain with code (I only provided the essentials for clarity):

// begin file jit.cpp

int myglobal;

void printMyGlobal() {
printf(“myglobal: %d\n”, myglobal);
}

int main(int argc, char *argv[]) {

// This file, jit.cpp has been compiled to bitcode (clang -S -emit-llvm jit.cpp)
// and is read into Module M here
Module *M = …

ExecutionEngine *ee = …

myglobal = 42;

ee->addGlobalMapping(M->getGlobalVariable(“myglobal”), (void*)&myglobal);

ee->finalizeObject();

void(fptr)() = (void()())ee->getFunctionAddress(“printMyGlobal”);

fptr();
printMyGlobal();

}

// end file jit.cpp

I compile this file (jit.cpp) into bitcode which I then load int M. M also contains a global variable called myglobal. I want to “map” the myglobal variable in M to the global variable in the running program. The idea is that the value of myglobal inside the JIT’ted module will also be 42. However, as it is right now, when I run the code, I get:

myglobal: 0
myglobal: 42

The first is the JIT’ted printMyGlobal() and the second is the AOT’ed printMyGlobal(). But when I lookup the address of the global variable myglobal (ee->getGlobalValueAddress()), it is mapped to the correct address. Similarly, if I lookup ee->getGlobalValueAtAddress(), I get the expected result.

Am I missing an important step? Is what I am trying to do even possible?

Thanks for the help.

Tarun

Hi Tarun,

My first thought is that you might be adding a definition (e.g. @x = global i32 7) to your module, rather than an external declaration (e.g. @x = external global i32). Make sure you have the latter: If you provide a definition in your module it will shadow any definitions in your mapping, which would explain the behavior you’re seeing.

Cheers,
Lang.

Hi Lang,

That’s exactly what it was.

Thanks a heap.