Skip to content

Commit

Permalink
structmemfunclowering: fix crash when gep is optimized away
Browse files Browse the repository at this point in the history
LLVM is smart enough to be able to fold a gep if the source and indices
are constants. However, it was assumed that it would always emit a gep
instruction, which would cause a crash when trying to cast it.

Upon further inspection, I believe that the surrounding if statement was
meant as a way of working around one of such folds. The new code should
work for the general case, so I've removed it.

This fixes: leaningtech/cheerp-meta#141
  • Loading branch information
Hyxogen committed Feb 6, 2024
1 parent 5bc97cc commit a1235f5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions llvm/lib/CheerpUtils/StructMemFuncLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,15 @@ void StructMemFuncLowering::recursiveCopy(IRBuilder<>* IRB, Value* baseDst, Valu
{
Value* elementSrc = baseSrc;
Value* elementDst = baseDst;
Type* loadType = containingType;
if(indexes.size() != 1 || !isa<ConstantInt>(indexes[0]) || cast<ConstantInt>(indexes[0])->getZExtValue()!=0)
{
assert(baseSrc->getType() == containingType->getPointerTo());
assert(baseSrc->getType() == baseDst->getType());
elementSrc = IRB->CreateGEP(containingType, baseSrc, indexes);
elementDst = IRB->CreateGEP(containingType, baseDst, indexes);
loadType = cast<GEPOperator>(elementDst)->getResultElementType();
}

assert(baseSrc->getType() == containingType->getPointerTo());
assert(baseSrc->getType() == baseDst->getType());
elementSrc = IRB->CreateGEP(containingType, baseSrc, indexes);
elementDst = IRB->CreateGEP(containingType, baseDst, indexes);

Type* loadType = GetElementPtrInst::getIndexedType(containingType, indexes);
assert(loadType->getPointerTo() == elementSrc->getType());

Instruction* element = IRB->CreateAlignedLoad(loadType, elementSrc, MaybeAlign(baseAlign));
MDNode* newAliasScope = NULL;
if(aliasDomain)
Expand Down

0 comments on commit a1235f5

Please sign in to comment.