Skip to content

Commit a133377

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 d25eba2 commit a133377

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,18 +2847,6 @@ llvm::Constant *IRGenModule::getGlobalInitValue(SILGlobalVariable *var,
28472847
return llvm::ConstantAggregateZero::get(arrayTy);
28482848
}
28492849

2850-
if (auto *vector = dyn_cast<VectorInst>(initInst)) {
2851-
llvm::SmallVector<llvm::Constant *, 8> elementValues;
2852-
for (SILValue element : vector->getElements()) {
2853-
auto &ti = cast<FixedTypeInfo>(getTypeInfo(element->getType()));
2854-
Size paddingBytes = ti.getFixedStride() - ti.getFixedSize();
2855-
Explosion e = emitConstantValue(*this, element);
2856-
elementValues.push_back(getConstantValue(std::move(e), paddingBytes.getValue()));
2857-
}
2858-
auto *arrTy = llvm::ArrayType::get(elementValues[0]->getType(), elementValues.size());
2859-
return llvm::ConstantArray::get(arrTy, elementValues);
2860-
}
2861-
28622850
Explosion initExp = emitConstantValue(*this,
28632851
cast<SingleValueInstruction>(initInst));
28642852
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
@@ -7727,8 +7727,6 @@ void SILGlobalVariable::verify() const {
77277727
assert(!init->use_empty() && "dead instruction in static initializer");
77287728
assert(!isa<ObjectInst>(init) &&
77297729
"object instruction is only allowed for final initial value");
7730-
assert(!isa<VectorInst>(init) &&
7731-
"vector instruction is only allowed for final initial value");
77327730
}
77337731
assert(I.getParent() == &StaticInitializerBlock);
77347732
}

0 commit comments

Comments
 (0)