[llvm-dev] JIT: Mapping global variable in JIT'ted code to variable in running program

Tarun Prabhu via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 14 14:55:18 PDT 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150914/9d1b7b26/attachment.html>


More information about the llvm-dev mailing list