Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent collapsing of secondary bases used inside a hierarchy where a downcast is done #242

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/Cheerp/TypeOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class TypeOptimizer

llvm::Module* module;
const llvm::DataLayout* DL;
std::unordered_set<const llvm::Type*> uncollapsibleSecondaryBases;
std::unordered_map<const llvm::StructType*,std::set<llvm::StructType*>> downcastSourceToDestinationsMapping;
std::unordered_map<const llvm::StructType*, std::vector<std::pair<uint32_t, uint32_t>>> membersMappingData;
std::unordered_map<llvm::GlobalValue*, llvm::Constant*> globalsMapping;
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/CheerpUtils/TypeOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,21 @@ void TypeOptimizer::gatherAllTypesInfo(const Module& M)
uint32_t fieldIndex = cast<ConstantInt>(*std::prev(GEP->op_end()))->getZExtValue();
escapingFields.emplace(containerStructType, fieldIndex, TypeAndIndex::STRUCT_MEMBER);
}

// Mark classes that cannot be collapsed because they are used as a secondary base
for (const auto sTy : M.getIdentifiedStructTypes())
{
uint32_t firstBase;
uint32_t baseCount;
if (TypeSupport::getBasesInfo(M, sTy, firstBase, baseCount))
{
for (uint32_t i = firstBase; i< (firstBase + baseCount); i++)
{
assert(sTy->getElementType(i)->isStructTy());
uncollapsibleSecondaryBases.insert(sTy->getElementType(i));
}
}
}
}

/**
Expand Down Expand Up @@ -294,6 +309,9 @@ bool TypeOptimizer::canCollapseStruct(llvm::StructType* st, llvm::StructType* ne
assert(st->isLiteral());
return true;
}
// Secondary bases used inside a hierarchy where a downcast is performed on cannot safely be collapsed
if (uncollapsibleSecondaryBases.count(st))
return false;
// Stop if the element is just a int8, we may be dealing with an empty struct
// Empty structs are unsafe as the int8 inside is just a placeholder and will be replaced
// by a different type in a derived class
Expand Down
Loading