1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 11:38:21 +02:00

Fixed bug with stack saving

In certain cases when we need to remove the StackSave (because we crossed the save threshold with an allocation), there may already be restores using that stack save which need to be removed as well.
This commit is contained in:
Brian Fiete 2019-09-18 13:00:44 -07:00
parent be7e82f5b6
commit 9f1ea28953
8 changed files with 25 additions and 10 deletions

View file

@ -1767,8 +1767,11 @@ void BeIRCodeGen::HandleNextCmd()
CMD_PARAM(BeValue*, instVal);
BeInst* inst = (BeInst*)instVal;
auto itr = std::find(inst->mParentBlock->mInstructions.begin(), inst->mParentBlock->mInstructions.end(), inst);
inst->mParentBlock->mInstructions.erase(itr);
bool wasRemoved = inst->mParentBlock->mInstructions.Remove(inst);
BF_ASSERT(wasRemoved);
#ifdef _DEBUG
inst->mWasRemoved = true;
#endif
}
break;
case BfIRCmd_CreateBr:
@ -3050,6 +3053,7 @@ BeValue* BeIRCodeGen::GetBeValue(int id)
BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
#ifdef _DEBUG
BF_ASSERT(!result.mBeValue->mLifetimeEnded);
BF_ASSERT(!result.mBeValue->mWasRemoved);
#endif
return result.mBeValue;
}

View file

@ -2940,10 +2940,12 @@ void BeModule::AddBlock(BeFunction* function, BeBlock* block)
void BeModule::RemoveBlock(BeFunction* function, BeBlock* block)
{
auto itr = std::find(function->mBlocks.begin(), function->mBlocks.end(), block);
BF_ASSERT(itr != function->mBlocks.end());
if (itr != function->mBlocks.end())
function->mBlocks.erase(itr);
bool didRemove = function->mBlocks.Remove(block);
BF_ASSERT(didRemove);
#ifdef _DEBUG
for (auto inst : block->mInstructions)
inst->mWasRemoved = true;
#endif
}
BeBlock* BeModule::GetInsertBlock()

View file

@ -207,9 +207,11 @@ class BeValue : public BeHashble
public:
#ifdef _DEBUG
bool mLifetimeEnded;
bool mWasRemoved;
BeValue()
{
mLifetimeEnded = false;
mWasRemoved = false;
}
#endif

View file

@ -3843,10 +3843,11 @@ BfIRValue BfIRBuilder::CreateStackSave()
return retVal;
}
void BfIRBuilder::CreateStackRestore(BfIRValue stackVal)
BfIRValue BfIRBuilder::CreateStackRestore(BfIRValue stackVal)
{
BfIRValue retVal = WriteCmd(BfIRCmd_StackRestore, stackVal);
NEW_CMD_INSERTED;
return retVal;
}
BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)

View file

@ -1099,7 +1099,7 @@ public:
BfIRValue CreateMemSet(BfIRValue addr, BfIRValue val, BfIRValue size, int align);
void CreateFence(BfIRFenceType fenceType);
BfIRValue CreateStackSave();
void CreateStackRestore(BfIRValue stackVal);
BfIRValue CreateStackRestore(BfIRValue stackVal);
BfIRValue CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
void GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr);

View file

@ -1585,6 +1585,7 @@ void BfIRCodeGen::HandleNextCmd()
CMD_PARAM(llvm::Value*, stackVal);
auto intrin = llvm::Intrinsic::getDeclaration(mLLVMModule, llvm::Intrinsic::stackrestore);
auto callInst = mIRBuilder->CreateCall(intrin, llvm::SmallVector<llvm::Value*, 1> {stackVal });
SetResult(curId, callInst);
}
break;
case BfIRCmd_GlobalVariable:

View file

@ -2122,8 +2122,12 @@ void BfModule::SaveStackState(BfScopeData* scopeData)
}
if (checkScope->mSavedStack)
{
for (auto usage : checkScope->mSavedStackUses)
mBfIRBuilder->EraseInstFromParent(usage);
checkScope->mSavedStackUses.Clear();
mBfIRBuilder->EraseInstFromParent(checkScope->mSavedStack);
checkScope->mSavedStack = BfIRValue();
checkScope->mSavedStack = BfIRValue();
}
checkScope = checkScope->mPrevScope;
}
@ -12394,7 +12398,7 @@ void BfModule::EmitDeferredScopeCalls(bool useSrcPositions, BfScopeData* scopeDa
if (checkScope->mSavedStack)
{
mBfIRBuilder->CreateStackRestore(checkScope->mSavedStack);
checkScope->mSavedStackUses.Add(mBfIRBuilder->CreateStackRestore(checkScope->mSavedStack));
if (mCurMethodState->mDynStackRevIdx)
{

View file

@ -315,6 +315,7 @@ public:
BfIRValue mBlock;
BfIRValue mValueScopeStart;
BfIRValue mSavedStack;
Array<BfIRValue> mSavedStackUses;
Array<BfDeferredHandler> mDeferredHandlers; // These get cleared when us our a parent gets new entries added into mDeferredCallEntries
Array<BfIRBlock> mAtEndBlocks; // Move these to the end after we close scope
Array<BfIRValue> mDeferredLifetimeEnds;