[LLVMdev] Linking modules across contexts crashes

Yuri yuri at rawbw.com
Fri May 29 17:18:43 PDT 2015


I get a crash when I try to link multiple modules registered in their 
individual contexts.
Documentation for Linker::LinkModules doesn't mention anything about 
contexts, and the first link succeeds. But the second link crashes.

Is this not the right way to merge such modules? If not, then what is 
the right way?

In any case, documentation for Linker::LinkModules should say if 
contexts are or aren't expected to be the same.

Yuri


---testcase---
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include <string>
#include <iostream>

using namespace llvm;
using namespace std;

int main() {
   // vars
   string Message;
   raw_string_ostream Stream(Message);
   DiagnosticPrinterRawOStream DP(Stream);
   LLVMBool Result;
   // create blank modules and contexts
   LLVMContext *ctx1 = new LLVMContext;
   Module* module1 = new Module("module1", *ctx1);
   LLVMContext *ctx2 = new LLVMContext;
   Module* module2 = new Module("module2", *ctx2);
   LLVMContext *ctx3 = new LLVMContext;
   Module* module3 = new Module("module3", *ctx3);
   // fill modules
llvm::Function::Create(llvm::FunctionType::get(Type::getInt32Ty(*ctx1), 
false), llvm::Function::ExternalLinkage, "f1", module1);
llvm::Function::Create(llvm::FunctionType::get(Type::getInt32Ty(*ctx2), 
false), llvm::Function::ExternalLinkage, "f2", module2);
llvm::Function::Create(llvm::FunctionType::get(Type::getInt32Ty(*ctx3), 
false), llvm::Function::ExternalLinkage, "f3", module3);

   // merge f1 <- f2
   Result = Linker::LinkModules(module1, module2, [&](const 
DiagnosticInfo &DI) {DI.print(DP);});
   cout << "merge result=" << Result << endl;
   delete ctx2;
   cout << "--done merge #1--" << endl;

   // merge f3 <- f1
   Result = Linker::LinkModules(module3, module1, [&](const 
DiagnosticInfo &DI) {DI.print(DP);});
   cout << "merge result=" << Result << endl;
   delete ctx1;
   cout << "--done merge #2--" << endl;

   return 0;
}

---output---
merge result=0
--done merge #1--
Bus error

rev.237344



More information about the llvm-dev mailing list