1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-23 18:18:00 +02:00

Use lifetime extension for mixin results

This commit is contained in:
Brian Fiete 2022-06-16 07:21:19 -07:00
parent 1639542fed
commit ccb1646990
12 changed files with 105 additions and 44 deletions

View file

@ -3402,12 +3402,6 @@ void BfExprEvaluator::Visit(BfBlock* blockExpr)
}
mModule->VisitEmbeddedStatement(blockExpr, this, BfNodeIsA<BfUnscopedBlock>(blockExpr) ? BfEmbeddedStatementFlags_Unscoped : BfEmbeddedStatementFlags_None);
mResult = mModule->SanitizeAddr(mResult);
if ((mResult) && (mResult.mType->IsStruct()))
{
mResult = mModule->MakeAddressable(mResult, true);
mResult.MakeTemporary(true);
}
}
bool BfExprEvaluator::CheckVariableDeclaration(BfAstNode* checkNode, bool requireSimpleIfExpr, bool exprMustBeTrue, bool silentFail)
@ -17193,9 +17187,14 @@ void BfExprEvaluator::InjectMixin(BfAstNode* targetSrc, BfTypedValue target, boo
mResult = BfTypedValue(BfIRValue(), mModule->GetPrimitiveType(BfTypeCode_None));
}
mResult = mModule->LoadValue(mResult);
mResult = mModule->SanitizeAddr(mResult);
mResult = mModule->RemoveRef(mResult);
if (mResult.IsAddr())
{
if (mModule->mCurMethodState->mCurScope->ExtendLifetime(mResult.mValue))
mModule->mBfIRBuilder->CreateLifetimeSoftEnd(mResult.mValue);
}
int localIdx = startLocalIdx;
argExprEvaluatorItr = argExprEvaluators.begin();
@ -17253,12 +17252,6 @@ void BfExprEvaluator::InjectMixin(BfAstNode* targetSrc, BfTypedValue target, boo
mModule->mBfIRBuilder->RestoreDebugLocation();
mModule->mBfIRBuilder->DupDebugLocation();
if ((mResult) && (mResult.mType->IsStruct()))
{
mResult = mModule->MakeAddressable(mResult, true);
mResult.MakeTemporary(true);
}
}
void BfExprEvaluator::SetMethodElementType(BfAstNode* target)
@ -19513,7 +19506,7 @@ void BfExprEvaluator::PerformAssignment(BfAssignmentExpression* assignExpr, bool
ResolveGenericType();
auto ptr = mModule->RemoveRef(mResult);
mResult = BfTypedValue();
if (mPropDef != NULL)
{
bool hasLeftVal = false;
@ -21583,7 +21576,7 @@ void BfExprEvaluator::PerformUnaryOperation_OnResult(BfExpression* unaryOpExpr,
if (!mResult)
return;
mResult = mModule->RemoveRef(mResult);
mResult = mModule->RemoveRef(mResult);
if (mResult.mType->IsVar())
{

View file

@ -4907,6 +4907,13 @@ BfIRValue BfIRBuilder::CreateLifetimeEnd(BfIRValue val)
return retVal;
}
BfIRValue BfIRBuilder::CreateLifetimeSoftEnd(BfIRValue val)
{
BfIRValue retVal = WriteCmd(BfIRCmd_LifetimeSoftEnd, val);
NEW_CMD_INSERTED;
return retVal;
}
BfIRValue BfIRBuilder::CreateLifetimeExtend(BfIRValue val)
{
BfIRValue retVal = WriteCmd(BfIRCmd_LifetimeExtend, val);

View file

@ -230,7 +230,8 @@ enum BfIRCmd : uint8
BfIRCmd_AliasValue,
BfIRCmd_LifetimeStart,
BfIRCmd_LifetimeEnd,
BfIRCmd_LifetimeExtend,
BfIRCmd_LifetimeSoftEnd,
BfIRCmd_LifetimeExtend,
BfIRCmd_ValueScopeStart,
BfIRCmd_ValueScopeRetain,
BfIRCmd_ValueScopeSoftEnd,
@ -1253,8 +1254,9 @@ public:
void SetAllocaNoChkStkHint(BfIRValue val);
void SetAllocaForceMem(BfIRValue val);
BfIRValue CreateAliasValue(BfIRValue val);
BfIRValue CreateLifetimeStart(BfIRValue val);
BfIRValue CreateLifetimeStart(BfIRValue val);
BfIRValue CreateLifetimeEnd(BfIRValue val);
BfIRValue CreateLifetimeSoftEnd(BfIRValue val);
BfIRValue CreateLifetimeExtend(BfIRValue val);
BfIRValue CreateValueScopeStart();
void CreateValueScopeRetain(BfIRValue val); // When a value is held by a variable -- don't release until we have a HardValueScopeEnd

View file

@ -2487,6 +2487,11 @@ void BfIRCodeGen::HandleNextCmd()
SetResult(curId, mIRBuilder->CreateLifetimeEnd(val));
}
break;
case BfIRCmd_LifetimeSoftEnd:
{
CMD_PARAM_NOTRANS(llvm::Value*, val);
}
break;
case BfIRCmd_LifetimeExtend:
{
CMD_PARAM_NOTRANS(llvm::Value*, val);

View file

@ -587,6 +587,17 @@ public:
}
return depth;
}
bool ExtendLifetime(BfIRValue irValue)
{
if (mDeferredLifetimeEnds.Remove(irValue))
{
if (mPrevScope != NULL)
mPrevScope->mDeferredLifetimeEnds.Add(irValue);
return true;
}
return false;
}
};
struct BfCaptureInfo

View file

@ -3526,6 +3526,12 @@ void BfModule::VisitCodeBlock(BfBlock* block)
(wasReadOnly ? BfTypedValueKind_ReadOnlyAddr : BfTypedValueKind_Addr));
}
}
if (exprEvaluator->mResult.IsAddr())
{
if (mCurMethodState->mCurScope->ExtendLifetime(exprEvaluator->mResult.mValue))
mBfIRBuilder->CreateLifetimeSoftEnd(exprEvaluator->mResult.mValue);
}
}
break;

View file

@ -1860,9 +1860,10 @@ void CeBuilder::Build()
}
switch (instType)
{
case BeNopInst::TypeId:
case BeLifetimeStartInst::TypeId:
{
case BeNopInst::TypeId:
case BeLifetimeSoftEndInst::TypeId:
case BeLifetimeStartInst::TypeId:
case BeLifetimeExtendInst::TypeId:
case BeValueScopeStartInst::TypeId:
case BeValueScopeEndInst::TypeId: