Skip to content

Commit 201bb44

Browse files
committed
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`.
1 parent 162794c commit 201bb44

File tree

3 files changed

+10
-43
lines changed

3 files changed

+10
-43
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ extension Instruction {
391391
case let bi as BuiltinInst:
392392
switch bi.id {
393393
case .ZeroInitializer:
394-
let type = bi.type.isBuiltinVector ? bi.type.builtinVectorElementType(in: parentFunction) : bi.type
395-
return type.isBuiltinInteger || type.isBuiltinFloat
394+
return bi.arguments.count == 0
396395
case .PtrToInt:
397396
return bi.operands[0].value is StringLiteralInst
398397
case .IntToPtr:

lib/IRGen/GenConstant.cpp

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,6 @@ llvm::Constant *irgen::emitConstantInt(IRGenModule &IGM,
6666
return llvm::ConstantInt::get(IGM.getLLVMContext(), value);
6767
}
6868

69-
llvm::Constant *irgen::emitConstantZero(IRGenModule &IGM, BuiltinInst *BI) {
70-
assert(IGM.getSILModule().getBuiltinInfo(BI->getName()).ID ==
71-
BuiltinValueKind::ZeroInitializer);
72-
73-
auto helper = [&](CanType astType) -> llvm::Constant * {
74-
if (auto type = astType->getAs<BuiltinIntegerType>()) {
75-
APInt zero(type->getWidth().getLeastWidth(), 0);
76-
return llvm::ConstantInt::get(IGM.getLLVMContext(), zero);
77-
}
78-
79-
if (auto type = astType->getAs<BuiltinFloatType>()) {
80-
const llvm::fltSemantics *sema = nullptr;
81-
switch (type->getFPKind()) {
82-
case BuiltinFloatType::IEEE16: sema = &APFloat::IEEEhalf(); break;
83-
case BuiltinFloatType::IEEE32: sema = &APFloat::IEEEsingle(); break;
84-
case BuiltinFloatType::IEEE64: sema = &APFloat::IEEEdouble(); break;
85-
case BuiltinFloatType::IEEE80: sema = &APFloat::x87DoubleExtended(); break;
86-
case BuiltinFloatType::IEEE128: sema = &APFloat::IEEEquad(); break;
87-
case BuiltinFloatType::PPC128: sema = &APFloat::PPCDoubleDouble(); break;
88-
}
89-
auto zero = APFloat::getZero(*sema);
90-
return llvm::ConstantFP::get(IGM.getLLVMContext(), zero);
91-
}
92-
93-
llvm_unreachable("SIL allowed an unknown type?");
94-
};
95-
96-
if (auto vector = BI->getType().getAs<BuiltinVectorType>()) {
97-
auto zero = helper(vector.getElementType());
98-
auto count = llvm::ElementCount::getFixed(vector->getNumElements());
99-
return llvm::ConstantVector::getSplat(count, zero);
100-
}
101-
102-
return helper(BI->getType().getASTType());
103-
}
104-
10569
llvm::Constant *irgen::emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI) {
10670
return llvm::ConstantFP::get(IGM.getLLVMContext(), FLI->getValue());
10771
}
@@ -311,8 +275,15 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
311275
} else if (auto *BI = dyn_cast<BuiltinInst>(operand)) {
312276
auto args = BI->getArguments();
313277
switch (IGM.getSILModule().getBuiltinInfo(BI->getName()).ID) {
314-
case BuiltinValueKind::ZeroInitializer:
315-
return emitConstantZero(IGM, BI);
278+
case BuiltinValueKind::ZeroInitializer: {
279+
auto &resultTI = cast<LoadableTypeInfo>(IGM.getTypeInfo(BI->getType()));
280+
auto schema = resultTI.getSchema();
281+
Explosion out;
282+
for (auto &elt : schema) {
283+
out.add(llvm::Constant::getNullValue(elt.getScalarType()));
284+
}
285+
return out;
286+
}
316287
case BuiltinValueKind::PtrToInt: {
317288
auto *ptr = emitConstantValue(IGM, args[0]).claimNextConstant();
318289
return llvm::ConstantExpr::getPtrToInt(ptr, IGM.IntPtrTy);

lib/IRGen/GenConstant.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ namespace irgen {
2727
/// Construct a ConstantInt from an IntegerLiteralInst.
2828
llvm::Constant *emitConstantInt(IRGenModule &IGM, IntegerLiteralInst *ILI);
2929

30-
/// Construct a zero from a zero initializer BuiltinInst.
31-
llvm::Constant *emitConstantZero(IRGenModule &IGM, BuiltinInst *Bi);
32-
3330
/// Construct a ConstantFP from a FloatLiteralInst.
3431
llvm::Constant *emitConstantFP(IRGenModule &IGM, FloatLiteralInst *FLI);
3532

0 commit comments

Comments
 (0)