Lefteris Karapetsas
2015-Jun-08 14:11 UTC
[LLVMdev] Question about usage of LLVMLinkModules()
Hello all, I am using llvm as a backend to a hobby language I am making. I have experimented with the C bindings since the project is written in C. I had no problems building a single lllvm module so far. I am not using a JIT compiler but at least at the moment am simply generating executables. The workflow I am using is to build a module with LLVMModuleCreateWithName() and populate it with the LLVMBuilder. After everything is in the module I simply output it into a file with LLVMPrintModuleToFile(). Then I am calling llc to convert the IR to native machine code and finally gcc to create an executable out of it. All these from inside my program. It may not be the best way to generate an executable and I would really welcome as much advice as you can give me on that but it works. The issue comes when I actually create 2 different modules. One being a "standard" library of sorts and the other the main module which should use it. I create both modules, with no errors appearing if I use LLVMVerifyModule() on them. The problem is how do I link them? I tried to do it like this: // if an error occurs LLVMLinkModules() returns true if (true == LLVMLinkModules(main_module, stdlib_module, LLVMLinkerDestroySource, &error)) { bllvm_error("Could not link LLVM modules", error); goto end; } But this fails (returns true), with no error. Even though both modules are valid. I understand that the above may be a really naive mistake, and that my whole approach may be wrong. This is why I would appreciate any links to tutorials/documents which would show the proper way to link 2 modules or some good old simple advice tips from any of you guys. Thank you for your time. Best Regards, Lefteris(Eleftherios) Karapetsas Click here <http://lefteris.refu.co> for my CV/portfolio website <http://lefteris.refu.co> Also maintain a blog <http://blog.refu.co>on various topics, mostly engineering -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150608/ebed9edd/attachment.html>
Duncan P. N. Exon Smith
2015-Jun-11 16:41 UTC
[LLVMdev] Question about usage of LLVMLinkModules()
> On 2015-Jun-08, at 07:11, Lefteris Karapetsas <lefteris at refu.co> wrote: > > Hello all, > > I am using llvm as a backend to a hobby language I am making. I have experimented with the C bindings since the project is written in C. I had no problems building a single lllvm module so far. I am not using a JIT compiler but at least at the moment am simply generating executables. The workflow I am using is to build a module with LLVMModuleCreateWithName() and populate it with the LLVMBuilder. > > After everything is in the module I simply output it into a file with LLVMPrintModuleToFile(). Then I am calling llc to convert the IR to native machine code and finally gcc to create an executable out of it. All these from inside my program. It may not be the best way to generate an executable and I would really welcome as much advice as you can give me on that but it works. > > The issue comes when I actually create 2 different modules. One being a "standard" library of sorts and the other the main module which should use it. I create both modules, with no errors appearing if I use LLVMVerifyModule() on them. > > The problem is how do I link them? I tried to do it like this: > > // if an error occurs LLVMLinkModules() returns true > if (true == LLVMLinkModules(main_module, stdlib_module, LLVMLinkerDestroySource, &error)) { > bllvm_error("Could not link LLVM modules", error); > goto end; > } > > > But this fails (returns true), with no error. Even though both modules are valid. I understand that the above may be a really naive mistake, and that my whole approach may be wrong. This is why I would appreciate any links to tutorials/documents which would show the proper way to link 2 modules or some good old simple advice tips from any of you guys.Weird that you're not getting an error message. Best thing is probably to spin up a debugger (or add some printfs) and see what's going on. One hypothetical point of failure: module linking requires that both modules are in the same context. If that's not the problem (and it probably isn't, since I'd expect crashes there), my bet would be on some sort of linkage conflict between the modules.
Lefteris Karapetsas
2015-Jun-11 17:54 UTC
[LLVMdev] Question about usage of LLVMLinkModules()
Hello Duncan, Thank you for your reply. I did figure it out in the end. It was a linkage conflict between the modules. There were some global types that I was declaring in both modules. After removing them, linking worked perfectly. Still it's a mystery why I got no error string pointing me to the reason the linking failed in the first place. Best Regards, Lefteris(Eleftherios) Karapetsas Click here <http://lefteris.refu.co> for my CV/portfolio website <http://lefteris.refu.co> Also maintain a blog <http://blog.refu.co>on various topics, mostly engineering On Thu, Jun 11, 2015 at 6:41 PM, Duncan P. N. Exon Smith < dexonsmith at apple.com> wrote:> > > On 2015-Jun-08, at 07:11, Lefteris Karapetsas <lefteris at refu.co> wrote: > > > > Hello all, > > > > I am using llvm as a backend to a hobby language I am making. I have > experimented with the C bindings since the project is written in C. I had > no problems building a single lllvm module so far. I am not using a JIT > compiler but at least at the moment am simply generating executables. The > workflow I am using is to build a module with LLVMModuleCreateWithName() > and populate it with the LLVMBuilder. > > > > After everything is in the module I simply output it into a file with > LLVMPrintModuleToFile(). Then I am calling llc to convert the IR to native > machine code and finally gcc to create an executable out of it. All these > from inside my program. It may not be the best way to generate an > executable and I would really welcome as much advice as you can give me on > that but it works. > > > > The issue comes when I actually create 2 different modules. One being a > "standard" library of sorts and the other the main module which should use > it. I create both modules, with no errors appearing if I use > LLVMVerifyModule() on them. > > > > The problem is how do I link them? I tried to do it like this: > > > > // if an error occurs LLVMLinkModules() returns true > > if (true == LLVMLinkModules(main_module, stdlib_module, > LLVMLinkerDestroySource, &error)) { > > bllvm_error("Could not link LLVM modules", error); > > goto end; > > } > > > > > > But this fails (returns true), with no error. Even though both modules > are valid. I understand that the above may be a really naive mistake, and > that my whole approach may be wrong. This is why I would appreciate any > links to tutorials/documents which would show the proper way to link 2 > modules or some good old simple advice tips from any of you guys. > > Weird that you're not getting an error message. > > Best thing is probably to spin up a debugger (or add some printfs) > and see what's going on. > > One hypothetical point of failure: module linking requires that both > modules are in the same context. If that's not the problem (and it > probably isn't, since I'd expect crashes there), my bet would be on > some sort of linkage conflict between the modules.-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150611/960a47a4/attachment.html>
Duncan P. N. Exon Smith <dexonsmith <at> apple.com> writes:> > > > But this fails (returns true), with no error. Even though both modulesare valid. I understand that the> above may be a really naive mistake, and that my whole approach may bewrong. This is why I would appreciate> any links to tutorials/documents which would show the proper way to link 2modules or some good old simple> advice tips from any of you guys. > > Weird that you're not getting an error message.Not so weird: http://reviews.llvm.org/D10241 Regards Antoine.