gpt4 book ai didi

c++ - 如何让 llvm 优化以下浮点方程

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:42 25 4
gpt4 key购买 nike

我有一个小的测试程序,它使用 llvm 来计算一些方程式的值。设置如下:我创建了一个 bc 文件,其中包含加、乘、除、减和平方双数的函数。现在,我通过组合加法和乘法函数来建立具有不同参数的线性方程。然后我使用万花筒示例中的优化器转换函数。这很好地工作 - 生成的函数将 x 作为参数并简单地进行 2 个浮点计算(乘法和加法)。设置这些功能的代码是:

Function* createLinearFunction(const std::string& name, double factor, double summand, Module* module)
{
LLVMContext& context = getGlobalContext();

Function* func = cast<Function>(module->getOrInsertFunction(name.c_str(),Type::getDoubleTy(context),Type::getDoubleTy(context),(Type *)0));
//add basic block
BasicBlock* bb1 = BasicBlock::Create(context,"EntryBlock",func);
IRBuilder<> builder(bb1);
Argument* x0 = func->arg_begin();
x0->setName("x0");
Value* x1 = ConstantFP::get(context,APFloat(factor));
Value* x2 = ConstantFP::get(context,APFloat(summand));
std::vector<Value*> args1;
args1.push_back(x0);
args1.push_back(x1);
Value* x3 = builder.CreateCall(mul_d_dd,args1,"");
std::vector<Value*> args2;
args2.push_back(x2);
args2.push_back(x3);
Value* x4 = builder.CreateCall(add_d_dd,args2,"");
builder.CreateRet(x4);
return func;
}

我现在想要的是以下内容 - 当我生成一个因子为 1 的函数时,它应该优化乘法,而对于被加数 0,它应该优化加法。对于因子 0,它应该只返回被加数。有没有已经这样做的通行证?由于此处提到的原因,我只是假设 llvm 不会这样做:Why don't LLVM passes optimize floating point instructions?

谢谢你的帮助托拜厄斯


添加 - 我尝试通过 createInstructionCombiningPass() 添加 instcombine,但优化后的代码看起来仍然相同:

define double @Linear0xPlus0(double %x0) {
EntryBlock:
%0 = call double @mul_d_dd(double %x0, double 0.000000e+00)
%1 = call double @add_d_dd(double 0.000000e+00, double %0)
ret double %1
}

我现在尝试使用 createFuntionInliningPass() 添加内联传递 - 但这会导致断言错误

    FunctionPassManager fpm(module);
fpm.add(new DataLayout(module->getDataLayout()));
fpm.add(createFunctionInliningPass());
fpm.add(createBasicAliasAnalysisPass());
fpm.add(createInstructionCombiningPass());
fpm.add(createReassociatePass());
fpm.add(createGVNPass());
fpm.add(createCFGSimplificationPass());
fpm.add(createInstructionCombiningPass());
fpm.doInitialization();

并得到以下错误:Assertion failed: !PMS.empty() && "Unable to handle Call Graph Pass"

这个错误是因为内联不是一个函数,而是一个模块优化,必须在模块优化过程中使用。现在的设置如下所示:

PassManagerBuilder pmb;
pmb.OptLevel=3;
PassManager mpm;
pmb.populateModulePassManager(mpm);
mpm.add(createFunctionInliningPass());

但即使对包含 instcombine pass 的所有函数运行第二个函数优化 pass 也无法解决问题

FunctionPassManager instcombiner(module);
instcombiner.add(createInstructionCombiningPass());
instcombiner.doInitialization();
for (auto i=module->begin(); i!=module->end(); ++i)
{
instcombiner.run(*i);
}

最佳答案

instcombine pass 应该执行这些类型的优化,即使是浮点运算。

一个简单的尝试方法是在您的位码文件上运行 opt -instcombine 并检查输出。

关于c++ - 如何让 llvm 优化以下浮点方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19679580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com