LLVM 插入内在函数 Cos [英] LLVM insert intrinsic function Cos
问题描述
我正在尝试将内部 cos() 函数调用插入 LLVM 传递.我在 FunctionPass 中的代码:
I am trying to insert intrinsic cos() function call to LLVM pass. My code in a FunctionPass:
std::vector<Type *> arg_type;
arg_type.push_back(Type::getFloatTy(getGlobalContext()));
Function *fun = Intrinsic::getDeclaration(F.getParent(), Intrinsic::cos, arg_type);
CallInst* callInst = CallInst::Create(fun, args, Twine("cos"), (Instruction *)&I);
当我遗漏最后一行时,生成的 IR 是:
When I leave out last line generated IR is:
define i32 @main() nounwind uwtable {
entry:
...
}
declare float @llvm.cos.f32(float) nounwind readonly
,但包含 CallInst,我得到的只是:
, but with CallInst included all i get is:
0 opt 0x000000000094f4bf
1 opt 0x000000000094f9c9
2 libpthread.so.0 0x00007fb92b652cb0
3 opt 0x00000000008be244 llvm::Instruction::Instruction(llvm::Type*, unsigned int, llvm::Use*, unsigned int, llvm::Instruction*) + 148
4 LLVMObfuscation.so 0x00007fb92a66c52f
5 LLVMObfuscation.so 0x00007fb92a66c9a5
6 LLVMObfuscation.so 0x00007fb92a66df21
7 opt 0x00000000008e921f llvm::FPPassManager::runOnFunction(llvm::Function&) + 591
8 opt 0x00000000008e9293 llvm::FPPassManager::runOnModule(llvm::Module&) + 51
9 opt 0x00000000008e8f34 llvm::MPPassManager::runOnModule(llvm::Module&) + 532
10 opt 0x00000000008ea4fb llvm::PassManagerImpl::run(llvm::Module&) + 171
11 opt 0x0000000000496208 main + 4104
12 libc.so.6 0x00007fb92a89376d __libc_start_main + 237
13 opt 0x000000000049cfe5
我还需要做什么?我想我不需要在模块中定义这个函数.
What else do I need to do ? I think i do not need to define this function in module.
发送给函数的参数是这样定义的:
args sent to function is defined this way:
std::vector<Value *> args;
args.push_back(fp);
其中 fp 是先前插入的指令:
where fp is previously inserted Instruction:
Instruction *fp = BinaryOperator::Create(Instruction::FSub, ...
变量 I 是 inst_iterator,但我在外面这样做是为了循环.
Variable I is inst_iterator, but i am doing this outside for cycle.
谢谢.
推荐答案
我建议你使用 IRBuilder
.它简化了 LLVM pass 中的 IR 生成.在您的情况下,您可以这样使用它:
I advice you to use IRBuilder
. It simplifies IR generation inside LLVM pass. In your case you can use it like that:
std::vector<Type *> arg_type;
arg_type.push_back(Type::getFloatTy(getGlobalContext()));
Function *fun = Intrinsic::getDeclaration(F.getParent(), Intrinsic::cos, arg_type);
IRBuilder<> Builder(&I);
Builder.CreateCall(fun, args);
这篇关于LLVM 插入内在函数 Cos的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!