IRGen: allow all kind of Builtin.zeroInitializer in statically initialized globals

Just use the same method to create the zero initializer constant as in `emitBuiltinCall`.
This commit is contained in:
Erik Eckstein
2025-08-03 11:03:47 +02:00
parent 162794ca35
commit 201bb44d2f
3 changed files with 10 additions and 43 deletions

View File

@@ -391,8 +391,7 @@ extension Instruction {
case let bi as BuiltinInst:
switch bi.id {
case .ZeroInitializer:
let type = bi.type.isBuiltinVector ? bi.type.builtinVectorElementType(in: parentFunction) : bi.type
return type.isBuiltinInteger || type.isBuiltinFloat
return bi.arguments.count == 0
case .PtrToInt:
return bi.operands[0].value is StringLiteralInst
case .IntToPtr:

View File

@@ -66,42 +66,6 @@ llvm::Constant *irgen::emitConstantInt(IRGenModule &IGM,
return llvm::ConstantInt::get(IGM.getLLVMContext(), value);
}
llvm::Constant *irgen::emitConstantZero(IRGenModule &IGM, BuiltinInst *BI) {
assert(IGM.getSILModule().getBuiltinInfo(BI->getName()).ID ==
BuiltinValueKind::ZeroInitializer);
auto helper = [&](CanType astType) -> llvm::Constant * {
if (auto type = astType->getAs<BuiltinIntegerType>()) {
APInt zero(type->getWidth().getLeastWidth(), 0);
return llvm::ConstantInt::get(IGM.getLLVMContext(), zero);
}
if (auto type = astType->getAs<BuiltinFloatType>()) {
const llvm::fltSemantics *sema = nullptr;
switch (type->getFPKind()) {
case BuiltinFloatType::IEEE16: sema = &APFloat::IEEEhalf(); break;
case BuiltinFloatType::IEEE32: sema = &APFloat::IEEEsingle(); break;
case BuiltinFloatType::IEEE64: sema = &APFloat::IEEEdouble(); break;
case BuiltinFloatType::IEEE80: sema = &APFloat::x87DoubleExtended(); break;
case BuiltinFloatType::IEEE128: sema = &APFloat::IEEEquad(); break;
case BuiltinFloatType::PPC128: sema = &APFloat::PPCDoubleDouble(); break;
}
auto zero = APFloat::getZero(*sema);
return llvm::ConstantFP::get(IGM.getLLVMContext(), zero);
}
llvm_unreachable("SIL allowed an unknown type?");
};
if (auto vector = BI->getType().getAs<BuiltinVectorType>()) {
auto zero = helper(vector.getElementType());
auto count = llvm::ElementCount::getFixed(vector->getNumElements());
return llvm::ConstantVector::getSplat(count, zero);
}
return helper(BI->getType().getASTType());
}
llvm::Constant *irgen::emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI) {
return llvm::ConstantFP::get(IGM.getLLVMContext(), FLI->getValue());
}
@@ -311,8 +275,15 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
} else if (auto *BI = dyn_cast<BuiltinInst>(operand)) {
auto args = BI->getArguments();
switch (IGM.getSILModule().getBuiltinInfo(BI->getName()).ID) {
case BuiltinValueKind::ZeroInitializer:
return emitConstantZero(IGM, BI);
case BuiltinValueKind::ZeroInitializer: {
auto &resultTI = cast<LoadableTypeInfo>(IGM.getTypeInfo(BI->getType()));
auto schema = resultTI.getSchema();
Explosion out;
for (auto &elt : schema) {
out.add(llvm::Constant::getNullValue(elt.getScalarType()));
}
return out;
}
case BuiltinValueKind::PtrToInt: {
auto *ptr = emitConstantValue(IGM, args[0]).claimNextConstant();
return llvm::ConstantExpr::getPtrToInt(ptr, IGM.IntPtrTy);

View File

@@ -27,9 +27,6 @@ namespace irgen {
/// Construct a ConstantInt from an IntegerLiteralInst.
llvm::Constant *emitConstantInt(IRGenModule &IGM, IntegerLiteralInst *ILI);
/// Construct a zero from a zero initializer BuiltinInst.
llvm::Constant *emitConstantZero(IRGenModule &IGM, BuiltinInst *Bi);
/// Construct a ConstantFP from a FloatLiteralInst.
llvm::Constant *emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI);