LLVM 无法将数组类型强制转换为常量数组

LLVM Can't cast array type to ConstantArray

本文关键字:数组 转换 常量 类型 LLVM      更新时间:2023-10-16

我正在尝试将类型为[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的LLVM release_70分支,它只是一个稍微修改过的LLVM 7.1.0库。

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

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

还有一个ConstantExpr::getPointerCast()。这两者都只是简单地重新解释了你指向的比特