Skip to content

Commit

Permalink
Get rid of cycles in DtoType()
Browse files Browse the repository at this point in the history
Fixes #4734.
  • Loading branch information
kinke committed Aug 18, 2024
1 parent ba9af8d commit 0496358
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
35 changes: 8 additions & 27 deletions ir/irtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,12 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
auto &ctype = getIrType(dt);
assert(!ctype);

LLType *elemType;
unsigned addressSpace = 0;
if (dt->ty == TY::Tnull) {
elemType = llvm::Type::getInt8Ty(getGlobalContext());
} else {
elemType = DtoMemType(dt->nextOf());
if (dt->nextOf()->ty == TY::Tfunction) {
addressSpace = gDataLayout->getProgramAddressSpace();
}

// DtoType could have already created the same type, e.g. for
// dt == Node* in struct Node { Node* n; }.
if (ctype) {
return ctype->isPointer();
}
}
unsigned addressSpace =
dt->ty == TY::Tpointer && dt->nextOf()->ty == TY::Tfunction
? gDataLayout->getProgramAddressSpace()
: 0;

auto t =
new IrTypePointer(dt, llvm::PointerType::get(elemType, addressSpace));
auto t = new IrTypePointer(dt, getOpaquePtrType(addressSpace));
ctype = t;
return t;
}
Expand Down Expand Up @@ -173,15 +160,9 @@ IrTypeArray *IrTypeArray::get(Type *dt) {
auto &ctype = getIrType(dt);
assert(!ctype);

LLType *elemType = DtoMemType(dt->nextOf());

// Could have already built the type as part of a struct forward reference,
// just as for pointers.
if (!ctype) {
llvm::Type *types[] = {DtoSize_t(), llvm::PointerType::get(elemType, 0)};
LLType *at = llvm::StructType::get(getGlobalContext(), types, false);
ctype = new IrTypeArray(dt, at);
}
llvm::Type *types[] = {DtoSize_t(), getOpaquePtrType()};
LLType *at = llvm::StructType::get(getGlobalContext(), types, false);
ctype = new IrTypeArray(dt, at);

return ctype->isArray();
}
Expand Down
17 changes: 9 additions & 8 deletions ir/irtypeaggr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,18 @@ void AggrTypeBuilder::addAggregate(
// add default type
m_defaultTypes.push_back(llType);

unsigned fieldAlignment, fieldSize;
if (!llType->isSized()) {
// forward reference in a cycle or similar, we need to trust the D type
fieldAlignment = DtoAlignment(vd->type);
fieldSize = af.size;
} else {
fieldAlignment = getABITypeAlign(llType);
fieldSize = getTypeAllocSize(llType);
assert(fieldSize <= af.size);
error(vd->loc,
"unexpected IR type forward declaration for aggregate member of "
"type `%s`. This is an ICE, please file an LDC issue.",
vd->type->toPrettyChars());
fatal();
}

const unsigned fieldAlignment = getABITypeAlign(llType);
const unsigned fieldSize = getTypeAllocSize(llType);
assert(fieldSize <= af.size);

// advance offset to right past this field
if (!m_packed) {
assert(fieldAlignment);
Expand Down

0 comments on commit 0496358

Please sign in to comment.