Hi,
I stumbled upon a strange thing regarding types and module linking, which I don't quite get, so maybe someone around here can enlighten me.
Consider the following code, which will create 2 modules m1 and m2, and a named structured type %T = { i32 }; m1 contains only a function definition f(%T), m2 contains a function declaration f(%T) and a function definition h(%T), where h will call f in its body. Note that all functions in m1 and m2 are based upon the same structured type %T:
LLVMContext context;
BasicBlock* b;
// modules
Module *m1 = new Module( "m1", context ),
*m2 = new Module( "m2", context );
// types
vector<Type*> types;
types.push_back( IntegerType::get( context, 32 ) );
Type* sType = StructType::create( types, "T" );
types.clear();
types.push_back( sType );
FunctionType* ft = FunctionType::get( Type::getVoidTy( context ), types, false );
// m1
Function* f = Function::Create( ft, GlobalValue::ExternalLinkage, "f", m1 );
b = BasicBlock::Create( context, "b", f );
ReturnInst::Create( context, b );
verifyModule(*m1);
// m2
Function* fDecl = Function::Create( ft, GlobalValue::ExternalLinkage, "f", m2 );
Function* h = Function::Create( ft, GlobalValue::ExternalLinkage, "h", m2 );
vector<Value*> args;
args.push_back( h->arg_begin() );
b = BasicBlock::Create( context, "b", h );
CallInst::Create( fDecl, args, "", b );
ReturnInst::Create( context, b );
verifyModule(*m2);
Each module for itself is okay and passes the verification, leading to the following IR code:
; ModuleID = 'm1'
%T = type { i32 }
define void @f(%T) {
b:
ret void
}