Skip to content

Commit 0d1b7f4

Browse files
committed
IRGen: alloc the vector instruction to be located anywhere in a global initializer
So far it was only possible that `vector` is the top-level instruction in an initializer.
1 parent dff88f9 commit 0d1b7f4

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

lib/IRGen/GenConstant.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,24 @@ Explosion irgen::emitConstantValue(IRGenModule &IGM, SILValue operand,
433433
} else if (auto *atp = dyn_cast<AddressToPointerInst>(operand)) {
434434
auto *val = emitConstantValue(IGM, atp->getOperand()).claimNextConstant();
435435
return val;
436+
} else if (auto *vector = dyn_cast<VectorInst>(operand)) {
437+
if (flatten) {
438+
Explosion out;
439+
for (SILValue element : vector->getElements()) {
440+
Explosion e = emitConstantValue(IGM, element, flatten);
441+
out.add(e.claimAll());
442+
}
443+
return out;
444+
}
445+
llvm::SmallVector<llvm::Constant *, 8> elementValues;
446+
for (SILValue element : vector->getElements()) {
447+
auto &ti = cast<FixedTypeInfo>(IGM.getTypeInfo(element->getType()));
448+
Size paddingBytes = ti.getFixedStride() - ti.getFixedSize();
449+
Explosion e = emitConstantValue(IGM, element, flatten);
450+
elementValues.push_back(IGM.getConstantValue(std::move(e), paddingBytes.getValue()));
451+
}
452+
auto *arrTy = llvm::ArrayType::get(elementValues[0]->getType(), elementValues.size());
453+
return llvm::ConstantArray::get(arrTy, elementValues);
436454
} else {
437455
llvm_unreachable("Unsupported SILInstruction in static initializer!");
438456
}

lib/IRGen/GenDecl.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,19 +2832,6 @@ llvm::Constant *IRGenModule::getGlobalInitValue(SILGlobalVariable *var,
28322832
return initVal;
28332833
}
28342834
if (SILInstruction *initInst = var->getStaticInitializerValue()) {
2835-
2836-
if (auto *vector = dyn_cast<VectorInst>(initInst)) {
2837-
llvm::SmallVector<llvm::Constant *, 8> elementValues;
2838-
for (SILValue element : vector->getElements()) {
2839-
auto &ti = cast<FixedTypeInfo>(getTypeInfo(element->getType()));
2840-
Size paddingBytes = ti.getFixedStride() - ti.getFixedSize();
2841-
Explosion e = emitConstantValue(*this, element);
2842-
elementValues.push_back(getConstantValue(std::move(e), paddingBytes.getValue()));
2843-
}
2844-
auto *arrTy = llvm::ArrayType::get(elementValues[0]->getType(), elementValues.size());
2845-
return llvm::ConstantArray::get(arrTy, elementValues);
2846-
}
2847-
28482835
Explosion initExp = emitConstantValue(*this,
28492836
cast<SingleValueInstruction>(initInst));
28502837
return getConstantValue(std::move(initExp), /*paddingBytes=*/ 0);

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7717,8 +7717,6 @@ void SILGlobalVariable::verify() const {
77177717
assert(!init->use_empty() && "dead instruction in static initializer");
77187718
assert(!isa<ObjectInst>(init) &&
77197719
"object instruction is only allowed for final initial value");
7720-
assert(!isa<VectorInst>(init) &&
7721-
"vector instruction is only allowed for final initial value");
77227720
}
77237721
assert(I.getParent() == &StaticInitializerBlock);
77247722
}

0 commit comments

Comments
 (0)