我想创建一个新模块,其中填充了一些可以在其他模块中调用的全局函数。
但是在链接时,clang
显示:
“架构x86_64的未定义符号”。
这是功能代码:
LLVMContext context;
Module* module = new llvm::Module("testModule",context);
llvm::IRBuilder<> builder(context);
std::string funcName = "funcAdd";
std::string bbName = "entry";
Function* func = llvm::Function::Create(type, llvm::Function::ExternalLinkage, funcName, module);
llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, funcName, func);
builder.SetInsertPoint(entry);
Function::arg_iterator args = func->arg_begin();
llvm::Value *op0 = args++;
llvm::Value *op1 = args++;
llvm::Value *retValue = builder.CreateAdd(op0, op1);
builder.CreateRet(retValue);
当IR指令为FunctionPass
时,我在add
中调用它:
Type* op0ty = inst->getOperand(0)->getType();
Type* op1ty = inst->getOperand(1)->getType();
llvm::FunctionType *funcTy = llvm::FunctionType::get(ty, {op0ty,op1ty}, false);
Module *instModule = instruction->getModule();
Function *callfunc = dyn_cast<Function>(instModule->getOrInsertFunction("funcAdd", funcTy));
callfunc->setCallingConv(CallingConv::C);
llvm::Value *op0 = inst->getOperand(0);
llvm::Value *op1 = inst->getOperand(1);
CallInst *CI = instBuilder.CreateCall(callfunc, {op0,op1});
inst->replaceAllUsesWith(CI);
inst->eraseFromParent();
谢谢!