LLVM无法将数组类型转换为ConstantArray

我试图将类型[3 x double]的数组强制转换为ConstantArray,以最终将基础类型从double更改为float。但是每当我尝试投射时,它都会失败,而且我一直无法弄清原因。

正在运行的通行证的代码片段(已编辑以添加更多信息):

CallInst& inst; //passed in by function arg
CallInst* oldCall = &inst;
Constant *constant = dyn_cast<Constant>(oldCall->getOperand(1)->stripPointerCasts());
errs() << "Operand is: " << *constant->getOperand(0) << "\n";
errs() << "Operand type: " << *constant->getOperand(0)->getType() << "\n";
ConstantArray *constantArray = cast<ConstantArray>(constant->getOperand(0));

输出:

Operand is: [3 x double] [double 2.100000e+00,double 2.200000e+00,double 2.300000e+00]
Operand type: [3 x double]
opt: /include/llvm/Support/Casting.h:255: typename llvm::cast_retty<X,Y*>::ret_type llvm::cast(Y*) 
[with X = llvm::ConstantArray; Y = llvm::Value; typename llvm::cast_retty<X,Y*>::ret_type = 
llvm::ConstantArray*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

可以看出,常量中的操作数是数组类型。尝试进行强制类型转换时,由于强制类型的本质(我认为?),它会看到Value,但仍然无法认为它们是兼容的。有人知道为什么吗?

注意:这使用的是flang's LLVM release_70 branch,它只是经过稍微修改的LLVM 7.1.0库。

niuhenan 回答:LLVM无法将数组类型转换为ConstantArray

您不能将数组转换为其他任何数组,包括其他数组(AIUI,因为转换规则是特定于语言的)。通常的方法是强制转换指针,类似这样。

auto c = CastInst::Create(CastInst::BitCast,sourcePointerToDoubleArray,ArrayType::get(Type::getFloatTy(),3)->getPointerto(),"funny_little_ast",targetBasicBlock);

还有一个ConstantExpr::getPointerCast()。这些都只是重新解释了您指向的位。

本文链接:https://www.f2er.com/3157212.html

大家都在问