Optimizer: support static initialization of global arrays

The buffer of global arrays could already be statically initialized.
The missing piece was the array itself, which is basically a reference to the array buffer.
For example:
```
var a = [1, 2, 3]
```
ends up in two statically initialized globals:
1. the array buffer, which contains the elements
2. the variable `a` which is a single reference (= pointer) of the array buffer

This optimization removes the need for lazy initialization of such variables.

rdar://127757554
This commit is contained in:
Erik Eckstein
2024-05-14 20:37:18 +02:00
parent 4f73008177
commit af68435d90
8 changed files with 164 additions and 129 deletions

View File

@@ -355,6 +355,9 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
} else if (auto *CFI = dyn_cast<ConvertFunctionInst>(operand)) {
return emitConstantValue(IGM, CFI->getOperand(), flatten);
} else if (auto *URCI = dyn_cast<UncheckedRefCastInst>(operand)) {
return emitConstantValue(IGM, URCI->getOperand(), flatten);
} else if (auto *T2TFI = dyn_cast<ThinToThickFunctionInst>(operand)) {
SILType type = operand->getType();
auto *sTy = cast<llvm::StructType>(IGM.getTypeInfo(type).getStorageType());
@@ -405,6 +408,14 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
return llvm::ConstantPointerNull::get(IGM.OpaquePtrTy);
}
Address addr = IGM.getAddrOfSILGlobalVariable(var, ti, NotForDefinition);
return addr.getAddress();
} else if (auto *gVal = dyn_cast<GlobalValueInst>(operand)) {
assert(IGM.canMakeStaticObjectReadOnly(gVal->getType()));
SILGlobalVariable *var = gVal->getReferencedGlobal();
auto &ti = IGM.getTypeInfo(var->getLoweredType());
auto expansion = IGM.getResilienceExpansionForLayout(var);
assert(ti.isFixedSize(expansion));
Address addr = IGM.getAddrOfSILGlobalVariable(var, ti, NotForDefinition);
return addr.getAddress();
} else if (auto *atp = dyn_cast<AddressToPointerInst>(operand)) {