From a1235f56541fc301558406773af6462e69c27c74 Mon Sep 17 00:00:00 2001 From: Hyxogen <8938732+Hyxogen@users.noreply.github.com> Date: Tue, 6 Feb 2024 10:13:49 +0100 Subject: [PATCH] structmemfunclowering: fix crash when gep is optimized away 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: https://github.com/leaningtech/cheerp-meta/issues/141 --- llvm/lib/CheerpUtils/StructMemFuncLowering.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/llvm/lib/CheerpUtils/StructMemFuncLowering.cpp b/llvm/lib/CheerpUtils/StructMemFuncLowering.cpp index 0ec29976ff0a..511bf43d4ba7 100644 --- a/llvm/lib/CheerpUtils/StructMemFuncLowering.cpp +++ b/llvm/lib/CheerpUtils/StructMemFuncLowering.cpp @@ -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(indexes[0]) || cast(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(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)