我正在尝试创建一个 vector 并使用 llvm 将其作为值返回。这是我的代码的样子:
Value *ArrayAST::codeGen() {
Type *dType = Type::getDoubleTy(mContext);
Type *vectorType = VectorType::get(dType, 4);
Value *emptyVector = UndefValue::get(vectorType);
Constant *index0 = Constant::getIntegerValue(dType, llvm::APInt(32, 0));
Value *numberValue = numbers[0] -> codeGen(); // double 1.000000e+00
Value *fullVector = InsertElementInst::Create(emptyVector, numberValue, index0);
return fullVector;
}
这会生成以下 IR 代码:
define <4 x double> @x() {
entry:
ret <4 x double> <badref>
}
但是,正如您在上面看到的,存在一个问题:<badref>
.当我尝试运行它时,它无法构建:
llvm-as: out.ll:3:21: error: expected type
ret <4 x double> <badref>
我是 LLVM 的新手,我不太确定解决这个问题的最佳方法是什么。
编辑
如果有帮助,所有代码都在 GitHub here 上.
编辑2
如果我没记错(我完全有可能),IR 代码应该是这样的:
define <4 x double> @x() {
entry:
%tmp4 = insertelement <4 x double> undef, double 1.000000e+01, i32 0
ret <4 x double> %tmp4
}
最佳答案
问题是我没有将 vector 添加到我的构建器中。以下对我有用:
Builder.Insert(fullVector);
关于c++ - 问题创建 vector LLVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50374289/