Use type gep in initializeArrayLiteral (#4092)

This commit is contained in:
Nicholas Wilson 2022-09-01 17:14:57 +08:00 committed by GitHub
parent 3c2b8906b0
commit f2155c6dd9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 6 deletions

View file

@ -588,7 +588,8 @@ llvm::Constant *arrayLiteralToConst(IRState *p, ArrayLiteralExp *ale) {
////////////////////////////////////////////////////////////////////////////////
void initializeArrayLiteral(IRState *p, ArrayLiteralExp *ale, LLValue *dstMem) {
void initializeArrayLiteral(IRState *p, ArrayLiteralExp *ale,
LLValue *dstMem, LLType *dstType) {
size_t elemCount = ale->elements->length;
// Don't try to write nothing to a zero-element array, we might represent it
@ -618,7 +619,7 @@ void initializeArrayLiteral(IRState *p, ArrayLiteralExp *ale, LLValue *dstMem) {
for (size_t i = 0; i < elemCount; ++i) {
Expression *rhsExp = indexArrayLiteral(ale, i);
LLValue *lhsPtr = DtoGEP(dstMem, 0, i, "", p->scopebb());
LLValue *lhsPtr = DtoGEP(dstType, dstMem, 0, i, "", p->scopebb());
DLValue lhs(rhsExp->type, DtoBitCast(lhsPtr, DtoPtrToType(rhsExp->type)));
// try to construct it in-place

View file

@ -53,7 +53,8 @@ llvm::Constant *arrayLiteralToConst(IRState *p, ArrayLiteralExp *ale);
/// Initializes a chunk of memory with the contents of an array literal.
///
/// dstMem is expected to be a pointer to the array allocation.
void initializeArrayLiteral(IRState *p, ArrayLiteralExp *ale, LLValue *dstMem);
void initializeArrayLiteral(IRState *p, ArrayLiteralExp *ale,
LLValue *dstMem, LLType *dstType);
void DtoArrayAssign(const Loc &loc, DValue *lhs, DValue *rhs, EXP op,
bool canSkipPostblit);

View file

@ -2210,13 +2210,13 @@ public:
e->loc, arrayType,
new DConstValue(Type::tsize_t, DtoConstSize_t(len)), false);
initializeArrayLiteral(
p, e, DtoBitCast(dynSlice->getPtr(), getPtrToType(llStoType)));
p, e, DtoBitCast(dynSlice->getPtr(), getPtrToType(llStoType)), llStoType);
result = dynSlice;
}
} else {
llvm::Value *storage =
DtoRawAlloca(llStoType, DtoAlignment(e->type), "arrayliteral");
initializeArrayLiteral(p, e, storage);
initializeArrayLiteral(p, e, storage, llStoType);
if (arrayType->ty == TY::Tsarray) {
result = new DLValue(e->type, storage);
} else if (arrayType->ty == TY::Tpointer) {
@ -2815,7 +2815,7 @@ bool toInPlaceConstruction(DLValue *lhs, Expression *rhs) {
else if (auto al = rhs->isArrayLiteralExp()) {
if (lhs->type->toBasetype()->ty == TY::Tsarray) {
Logger::println("success, in-place-constructing array literal");
initializeArrayLiteral(gIR, al, DtoLVal(lhs));
initializeArrayLiteral(gIR, al, DtoLVal(lhs), DtoMemType(lhs->type));
return true;
}
}