Tarun Prabhu via llvm-dev
2015-Sep-14 21:55 UTC
[llvm-dev] 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150914/9d1b7b26/attachment.html>
Lang Hames via llvm-dev
2015-Sep-14 22:44 UTC
[llvm-dev] JIT: Mapping global variable in JIT'ted code to variable in running program
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. On Mon, Sep 14, 2015 at 2:55 PM, Tarun Prabhu via llvm-dev < llvm-dev at lists.llvm.org> wrote:> 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 > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150914/da5aab1f/attachment.html>
Tarun Prabhu via llvm-dev
2015-Sep-15 00:38 UTC
[llvm-dev] JIT: Mapping global variable in JIT'ted code to variable in running program
Hi Lang, That's exactly what it was. Thanks a heap. On Mon, Sep 14, 2015 at 5:44 PM, Lang Hames <lhames at gmail.com> wrote:> 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. > > > On Mon, Sep 14, 2015 at 2:55 PM, Tarun Prabhu via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> 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 >> >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150914/980c1689/attachment.html>