[llvm-dev] ORC JIT - Can modules independently managed with one LLJIT instance? + problems with ExecutionSession.lookup

Gaier, Bjoern via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 23 01:02:45 PDT 2020


Hi Lang,

Thank you for your answer!  This helped me again a lot!! Also that ResourceTracker is a really neat feature! Looking forward to it! :3
I changed the title cause… there is another issue I have (sorry about that…)

I’m finally allowed to investigate the ORC JIT for integration into our system, which meant I got a few days to actually play around with it. However, another problem arise which breaks my concept. It is the never ending story of “cross references”. I have again two Modules, which are added to two different LLJIT instances, but they are referencing each other. In the past you suggested me to use the LookUp function of the ExecutionSession to get the addresses – so I wrote this:

bool ModuleLoader :: resolve()
{
       auto &ES = this->jit->getExecutionSession();
       SymbolLookupSet lookupSet;

       this->undefinedReferences.clear();

       for(const auto &element : this->symbolsOfInterrest)
       {
              lookupSet.add(element.second.name,llvm::orc::SymbolLookupFlags::WeaklyReferencedSymbol);
       }

auto result = ES.lookup({{&jit->getMainJITDylib(), llvm::orc::JITDylibLookupFlags::MatchAllSymbols}}, lookupSet, llvm::orc::LookupKind::Static, llvm::orc::SymbolState::Resolved);
       if(result)
       {
             for(const auto &element : *result)
             {
                    const llvm::StringRef &name = (*element.first);
                    const size_t           hash = calculateHash(name);

                    printf(">>>%s @ 0x%p\n", name.data(), element.second.getAddress());
                    this->symbolsOfInterrest[hash].adr = element.second.getAddress();
             }
       }

       else
       {
             ES.reportError(result.takeError());
       }

       this->mtx.unlock();
       return (this->undefinedReferences.size() == 0ull);
}

I also attached a reporter to the ES which will handle “llvm::orc::SymbolsNotFound” by copying SymbolVector to the “undefinedReferences”. If I call this function and have every symbol resolved, then I can use the addresses to actually execute them. That is great! However, when I have an undefined Reference, things get strange… The first call will trigger my “tryToGenerate” function but it will not be able to resolve a certain symbol. The reporter will be triggered and the “undefinedReferences” SymbolVector will have size 1.
When I call the function a second time however, the “tryToGenerate” function will not be called anymore, so my vector will be empty, the undefined reference is still not resolved, but I return with a true and crash my program. So even if I would have an address for that one symbol in the second run, I would have no chance to tell “anyone” cause the “tryToGenerate” function was never called… Said function looks like that though:

llvm::Error ReferenceManager::UndefinedReferenceResolver :: tryToGenerate(llvm::orc::LookupKind K, llvm::orc::JITDylib &JD, llvm::orc::JITDylibLookupFlags JDLookupFlags, const llvm::orc::SymbolLookupSet &LookupSet)
{
       llvm::orc::SymbolNameVector notFound;
       llvm::orc::SymbolMap        NewSymbols;

       for(const auto &name : LookupSet)
       {
             printf("Generate!\n");
             const uintptr_t adr = UndefinedReferenceResolver::lookup((*name.first).data());
             if(adr)
             {
                    NewSymbols[name.first] = llvm::JITEvaluatedSymbol(adr, llvm::JITSymbolFlags::Absolute);
             }

             else
             {
                    notFound.push_back(name.first);
             }
       }

       JD.define(absoluteSymbols(std::move(NewSymbols)));
       return (notFound.size() == 0) ? llvm::Error::success() : llvm::make_error<llvm::orc::SymbolsNotFound>(std::move(notFound));
}

When I use “llvm::orc::JITDylibLookupFlags::MatchExportedSymbolsOnly” then I will get 1 undefined Reference in the first run, but a total of 9 in the second run, because every symbol I wanted to lookup was now an undefined reference.

Thank you for the help in advance!

Kind greetings
Björn

From: Lang Hames <lhames at gmail.com>
Sent: 23 September 2020 07:54
To: Gaier, Bjoern <Bjoern.Gaier at horiba.com>
Cc: LLVM Developers Mailing List <llvm-dev at lists.llvm.org>
Subject: Re: ORC JIT - Can modules independently managed with one LLJIT instance?

Hi Bjoern,

Sounds like you want the removable code feature that's under development in the orcv1-removal branch of https://github.com/lhames/llvm-project.git<https://hes32-ctp.trendmicro.com:443/wis/clicktime/v1/query?url=https%3a%2f%2fgithub.com%2flhames%2fllvm%2dproject.git&umid=7d73edbc-e566-4ffc-bd01-827f3d6c8c2e&auth=b6f66d00f8195cc5198eee21f0dbabe6af0a3180-918a46b7d72afae12d8ff60470e902dac471e605>. I will be aiming to merge this work back into the mainline some time in the next couple of weeks.

I have not added a "removeJITDylib" method to ExecutionSession with this feature yet, but will try to design and publish that tomorrow. Then you should be able to do exactly what you want.

In the new system you can also perform fine grained removal: It is possible to track and remove individual modules from within a JITDylib. For an example of this see: https://github.com/lhames/llvm-project/blob/7ec9f8930f68760953a483157e010d0ff88285cd/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp#L1148<https://hes32-ctp.trendmicro.com:443/wis/clicktime/v1/query?url=https%3a%2f%2fgithub.com%2flhames%2fllvm%2dproject%2fblob%2f7ec9f8930f68760953a483157e010d0ff88285cd%2fllvm%2fexamples%2fKaleidoscope%2fBuildingAJIT%2fChapter1%2ftoy.cpp%23L1148&umid=7d73edbc-e566-4ffc-bd01-827f3d6c8c2e&auth=b6f66d00f8195cc5198eee21f0dbabe6af0a3180-c0d11d7f66acc6ff5b094b7166067e93a53cc1ff>, from lines 1148 to line 1161.

Also a related but different question:
When using llvm::parseIRFile I need to pass it a LLVMContext - should this be a unique one for every module I pass or can I use a global one?

It depends on whether you want to be able to compile concurrently in the JIT process. If you do want to compile concurrently then each module should get its own context. If you are happy to stick to single threaded compilation you can load all modules on the same context, potentially saving some memory.

-- Lang.

On Thu, Sep 17, 2020 at 11:59 PM Gaier, Bjoern <Bjoern.Gaier at horiba.com<mailto:Bjoern.Gaier at horiba.com>> wrote:
Hello everyone and Lang,

I have another design or "how-to" question about the ORC JIT. Sorry for having so many about them, to me this is a really complicated yet fascinating subject...

How would I design an ORC JIT with the following requirements?
At any time it should be possible to load a LLVM Module, every Module is independent and is not allowed to be linked with the other modules, every module can be removed at any time.

My first idea was to have an ORC JIT for every module I load, but then I wondered if I could use a single ORC JIT for it.

So, I would create an ORC JIT:
- using llvm::orc::LLJITBuilder
- configurating it
- adding a custom memory manager that requests the entire memory size

Then I would call "getMainJITDylib" and fill it with symbols that are valid for every module:
- adding printf, strlen, usw.

Now when I get a request to load a module:
- load module
- get symbols I want to look up
- create new DyLib and add module to that

Now I would do a lookup on that module, plus the main module to get symbol addresses and stuff

If a new module is added, it goes to a new DyLib as well and so on.

-----

However, now I want to delete a module again. Killing the memory is fairly easy with my custom memory manager, but I still have my DyLib of the now dead code... Can I get rid of it without dumping the others or the entire ORC JIT?
Or is there a different approach to do this? My goal with this is to speed up the setup phase for a module and also to reduce dynamic memory usage.

Also a related but different question:
When using llvm::parseIRFile I need to pass it a LLVMContext - should this be a unique one for every module I pass or can I use a global one?

Kind greetings and thank you all again,
Björn
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Junichi Tajika, Ergin Cansiz.
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Junichi Tajika, Ergin Cansiz.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200923/3c234b47/attachment.html>


More information about the llvm-dev mailing list