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

Allow LdSlot to be optimized across calls if the variable being loaded is known not to be written to by any nested function. #5858

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions lib/Backend/Func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Func::Func(JitArenaAllocator *alloc, JITTimeWorkItem * workItem,
#endif
, m_funcStartLabel(nullptr)
, m_funcEndLabel(nullptr)
, bvStableSlotSyms(nullptr)
#if DBG
, hasCalledSetDoFastPaths(false)
, allowRemoveBailOutArgInstr(false)
Expand Down Expand Up @@ -172,6 +173,8 @@ Func::Func(JitArenaAllocator *alloc, JITTimeWorkItem * workItem,
outputData->hasJittedStackClosure = false;
outputData->localVarSlotsOffset = m_localVarSlotsOffset;
outputData->localVarChangedOffset = m_hasLocalVarChangedOffset;

this->bvStableSlotSyms = Anew(this->m_alloc, BVSparse<JitArenaAllocator>, this->m_alloc);
}

if (this->IsInlined())
Expand Down Expand Up @@ -808,6 +811,21 @@ void Func::OnAddSym(Sym* sym)
}
}

void Func::AddStableSlotSym(PropertySym *sym)
{
this->GetTopFunc()->bvStableSlotSyms->Set(sym->m_id);
}

bool Func::IsStableSlotSym(PropertySym *sym) const
{
return this->GetTopFunc()->bvStableSlotSyms->Test(sym->m_id);
}

BVSparse<JitArenaAllocator> *Func::GetStableSlotSyms() const
{
return this->GetTopFunc()->bvStableSlotSyms;
}

///
/// Returns offset of the flag (1 byte) whether any local was changed (in debugger).
/// If the function does not have any locals, returns -1.
Expand Down
5 changes: 5 additions & 0 deletions lib/Backend/Func.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ static const unsigned __int64 c_debugFillPattern8 = 0xcececececececece;
StackSym *GetLocalFrameDisplaySym() const { return m_localFrameDisplaySym; }
void SetLocalFrameDisplaySym(StackSym *sym) { m_localFrameDisplaySym = sym; }

void AddStableSlotSym(PropertySym *sym);
bool IsStableSlotSym(PropertySym *sym) const;
BVSparse<JitArenaAllocator> *GetStableSlotSyms() const;

intptr_t GetJittedLoopIterationsSinceLastBailoutAddress() const;
void EnsurePinnedTypeRefs();
void PinTypeRef(void* typeRef);
Expand Down Expand Up @@ -724,6 +728,7 @@ static const unsigned __int64 c_debugFillPattern8 = 0xcececececececece;
BitVector m_regsUsed;
StackSym * tempSymDouble;
StackSym * tempSymBool;
BVSparse<JitArenaAllocator> * bvStableSlotSyms;
uint32 loopCount;
uint32 unoptimizableArgumentsObjReference;
Js::ProfileId callSiteIdInParentFunc;
Expand Down
2 changes: 1 addition & 1 deletion lib/Backend/GlobOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ BOOL GlobOpt::PRE::PreloadPRECandidate(Loop *loop, GlobHashBucket* candidate)

// T3.y
PropertySym *newPropSym = PropertySym::FindOrCreate(
objPtrCopyPropSym->m_id, propertySym->m_propertyId, propertySym->GetPropertyIdIndex(), propertySym->GetInlineCacheIndex(), propertySym->m_fieldKind, this->globOpt->func);
objPtrCopyPropSym->m_id, propertySym->m_propertyId, propertySym->GetPropertyIdIndex(), propertySym->GetInlineCacheIndex(), propertySym->m_fieldKind, this->globOpt->func, this->globOpt->func->IsStableSlotSym(propertySym));

if (!landingPad->globOptData.FindValue(newPropSym))
{
Expand Down
7 changes: 3 additions & 4 deletions lib/Backend/GlobOptFields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ GlobOpt::KillLiveElems(IR::IndirOpnd * indirOpnd, BVSparse<JitArenaAllocator> *
void
GlobOpt::KillAllFields(BVSparse<JitArenaAllocator> * bv)
{
bv->ClearAll();
bv->And(func->GetStableSlotSyms());
if (this->IsLoopPrePass())
{
for (Loop * loop = this->rootLoopPrePass; loop != nullptr; loop = loop->parent)
Expand Down Expand Up @@ -447,9 +447,8 @@ GlobOpt::ProcessFieldKills(IR::Instr *instr, BVSparse<JitArenaAllocator> *bv, bo
void
GlobOpt::ProcessFieldKills(IR::Instr * instr)
{
if (!this->DoFieldCopyProp() && !this->DoFieldRefOpts() && !DoCSE())
if (this->currentBlock->globOptData.liveFields->IsEmpty())
{
Assert(this->currentBlock->globOptData.liveFields->IsEmpty());
return;
}

Expand Down Expand Up @@ -1808,7 +1807,7 @@ GlobOpt::CopyPropPropertySymObj(IR::SymOpnd *symOpnd, IR::Instr *instr)
if (copySym != nullptr)
{
PropertySym *newProp = PropertySym::FindOrCreate(
copySym->m_id, propertySym->m_propertyId, propertySym->GetPropertyIdIndex(), propertySym->GetInlineCacheIndex(), propertySym->m_fieldKind, this->func);
copySym->m_id, propertySym->m_propertyId, propertySym->GetPropertyIdIndex(), propertySym->GetInlineCacheIndex(), propertySym->m_fieldKind, this->func, this->func->IsStableSlotSym(propertySym));

if (!this->IsLoopPrePass() || SafeToCopyPropInPrepass(objSym, copySym, val))
{
Expand Down
93 changes: 77 additions & 16 deletions lib/Backend/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,10 @@ IRBuilder::BuildIndirOpnd(IR::RegOpnd *baseReg, uint32 offset, const char16 *des
#endif

IR::SymOpnd *
IRBuilder::BuildFieldOpnd(Js::OpCode newOpcode, Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, PropertyKind propertyKind, uint inlineCacheIndex)
IRBuilder::BuildFieldOpnd(Js::OpCode newOpcode, Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, PropertyKind propertyKind, uint inlineCacheIndex, bool stableSlotSym)
{
AssertOrFailFast(inlineCacheIndex < m_func->GetJITFunctionBody()->GetInlineCacheCount() || inlineCacheIndex == Js::Constants::NoInlineCacheIndex);
PropertySym * propertySym = BuildFieldSym(reg, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind);
PropertySym * propertySym = BuildFieldSym(reg, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind, stableSlotSym);
IR::SymOpnd * symOpnd;

// If we plan to apply object type optimization to this instruction or if we intend to emit a fast path using an inline
Expand Down Expand Up @@ -1243,14 +1243,14 @@ IRBuilder::BuildFieldOpnd(Js::OpCode newOpcode, Js::RegSlot reg, Js::PropertyId
}

PropertySym *
IRBuilder::BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, uint inlineCacheIndex, PropertyKind propertyKind)
IRBuilder::BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, uint inlineCacheIndex, PropertyKind propertyKind, bool stableSlotSym)
{
PropertySym * propertySym;
SymID symId = this->BuildSrcStackSymID(reg);

AssertMsg(m_func->m_symTable->FindStackSym(symId), "Tried to use an undefined stacksym?");

propertySym = PropertySym::FindOrCreate(symId, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind, m_func);
propertySym = PropertySym::FindOrCreate(symId, propertyId, propertyIdIndex, inlineCacheIndex, propertyKind, m_func, stableSlotSym);

return propertySym;
}
Expand Down Expand Up @@ -3528,19 +3528,34 @@ IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
StackSym * stackFuncPtrSym = nullptr;
SymID symID = m_func->GetJITFunctionBody()->GetLocalClosureReg();
bool isLdSlotThatWasNotProfiled = false;
bool stableSlot = false;
StackSym* closureSym = m_func->GetLocalClosureSym();

uint scopeSlotSize = this->IsParamScopeDone() ? m_func->GetJITFunctionBody()->GetScopeSlotArraySize() : m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();

switch (newOpcode)
{
case Js::OpCode::LdStableParamSlot:
stableSlot = true;
goto ParamSlotCommon;

case Js::OpCode::LdParamSlot:
stableSlot = false;

ParamSlotCommon:
scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
closureSym = m_func->GetParamClosureSym();
symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
// Fall through
goto LocalSlotCommon;

case Js::OpCode::LdStableLocalSlot:
stableSlot = true;
goto LocalSlotCommon;

case Js::OpCode::LdLocalSlot:
stableSlot = false;

LocalSlotCommon:
if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
{
if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
Expand Down Expand Up @@ -3580,7 +3595,7 @@ IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
this->EnsureLoopBodyLoadSlot(symID);
}

fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func, stableSlot);
fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
regOpnd = this->BuildDstOpnd(regSlot);
instr = nullptr;
Expand Down Expand Up @@ -3637,16 +3652,32 @@ IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
this->AddInstr(instr, offset);
break;

case Js::OpCode::StStableParamSlot:
case Js::OpCode::StStableParamSlotChkUndecl:
stableSlot = true;
goto StParamSlotCommon;

case Js::OpCode::StParamSlot:
case Js::OpCode::StParamSlotChkUndecl:
stableSlot = false;

StParamSlotCommon:
scopeSlotSize = m_func->GetJITFunctionBody()->GetParamScopeSlotArraySize();
closureSym = m_func->GetParamClosureSym();
symID = m_func->GetJITFunctionBody()->GetParamClosureReg();
newOpcode = newOpcode == Js::OpCode::StParamSlot ? Js::OpCode::StLocalSlot : Js::OpCode::StLocalSlotChkUndecl;
// Fall through
newOpcode = newOpcode == Js::OpCode::StParamSlot || newOpcode == Js::OpCode::StStableParamSlot ? Js::OpCode::StLocalSlot : Js::OpCode::StLocalSlotChkUndecl;
goto StLocalSlotCommon;

case Js::OpCode::StStableLocalSlot:
case Js::OpCode::StStableLocalSlotChkUndecl:
stableSlot = true;
goto StLocalSlotCommon;

case Js::OpCode::StLocalSlot:
case Js::OpCode::StLocalSlotChkUndecl:
stableSlot = false;

StLocalSlotCommon:
if (!PHASE_OFF(Js::ClosureRangeCheckPhase, m_func))
{
if ((uint32)slotId >= scopeSlotSize + Js::ScopeSlots::FirstSlotIndex)
Expand All @@ -3662,7 +3693,7 @@ IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
this->AddInstr(byteCodeUse, offset);
}

newOpcode = newOpcode == Js::OpCode::StLocalSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
newOpcode = newOpcode == Js::OpCode::StLocalSlot || newOpcode == Js::OpCode::StStableLocalSlot ? Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
if (m_func->DoStackFrameDisplay())
{
regOpnd = IR::RegOpnd::New(TyVar, m_func);
Expand All @@ -3687,7 +3718,7 @@ IRBuilder::BuildElementSlotI1(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
this->EnsureLoopBodyLoadSlot(symID);
}
}
fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
fieldSym = PropertySym::FindOrCreate(symID, slotId, (uint32)-1, (uint)-1, PropertyKindSlots, m_func, stableSlot);
fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);
regOpnd = this->BuildSrcOpnd(regSlot);
instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
Expand Down Expand Up @@ -3882,6 +3913,8 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
IR::Instr *instr;
PropertySym *fieldSym;
bool isLdSlotThatWasNotProfiled = false;
bool stableSlotArray = false;
bool stableSlot = false;

switch (newOpcode)
{
Expand Down Expand Up @@ -3915,14 +3948,26 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
break;
}

case Js::OpCode::LdStableEnvSlot:
stableSlotArray = true;
stableSlot = true;
goto EnvSlotCommon;

case Js::OpCode::LdEnvSlot:
case Js::OpCode::LdEnvObjSlot:
case Js::OpCode::StEnvSlot:
case Js::OpCode::StEnvSlotChkUndecl:
stableSlotArray = true;
stableSlot = false;
goto EnvSlotCommon;

case Js::OpCode::LdEnvObjSlot:
case Js::OpCode::StEnvObjSlot:
case Js::OpCode::StEnvObjSlotChkUndecl:
stableSlotArray = false;
stableSlot = false;

fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
EnvSlotCommon:
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray, (uint)-1, stableSlotArray);
regOpnd = IR::RegOpnd::New(TyVar, m_func);
instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
this->AddInstr(instr, offset);
Expand All @@ -3947,12 +3992,13 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
break;
}

fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func);
fieldSym = PropertySym::New(regOpnd->m_sym, slotId2, (uint32)-1, (uint)-1, PropertyKindSlots, m_func, stableSlot);
fieldOpnd = IR::SymOpnd::New(fieldSym, TyVar, m_func);

switch (newOpcode)
{
case Js::OpCode::LdEnvSlot:
case Js::OpCode::LdStableEnvSlot:
case Js::OpCode::LdEnvObjSlot:
newOpcode = Js::OpCode::LdSlot;
regOpnd = this->BuildDstOpnd(regSlot);
Expand Down Expand Up @@ -3986,10 +4032,18 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
}
break;

case Js::OpCode::StStableInnerSlot:
case Js::OpCode::StStableInnerSlotChkUndecl:
stableSlot = true;
goto StInnerSlotCommon;

case Js::OpCode::StInnerObjSlot:
case Js::OpCode::StInnerObjSlotChkUndecl:
case Js::OpCode::StInnerSlot:
case Js::OpCode::StInnerSlotChkUndecl:
stableSlot = false;

StInnerSlotCommon:
if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
{
Js::Throw::FatalInternalError();
Expand All @@ -4012,15 +4066,15 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
}
else
{
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::StSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::StSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots, (uint)-1, stableSlot);
if (!this->DoSlotArrayCheck(fieldOpnd, IsLoopBody()))
{
// Need a dynamic check on the size of the local slot array.
m_func->GetTopFunc()->AddSlotArrayCheck(fieldOpnd);
}
}
newOpcode =
newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerSlot ?
newOpcode == Js::OpCode::StInnerObjSlot || newOpcode == Js::OpCode::StInnerSlot || newOpcode == Js::OpCode::StStableInnerSlot ?
Js::OpCode::StSlot : Js::OpCode::StSlotChkUndecl;
instr = IR::Instr::New(newOpcode, fieldOpnd, regOpnd, m_func);
if (newOpcode == Js::OpCode::StSlotChkUndecl)
Expand All @@ -4032,8 +4086,15 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r

break;

case Js::OpCode::LdStableInnerSlot:
stableSlot = true;
goto LdInnerSlotCommon;

case Js::OpCode::LdInnerSlot:
case Js::OpCode::LdInnerObjSlot:
stableSlot = false;

LdInnerSlotCommon:
if ((uint)slotId1 >= m_func->GetJITFunctionBody()->GetInnerScopeCount())
{
Js::Throw::FatalInternalError();
Expand All @@ -4055,7 +4116,7 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
}
else
{
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots);
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlot, slotId1, slotId2, (Js::PropertyIdIndexType)-1, PropertyKindSlots, (uint)-1, stableSlot);
if (!this->DoSlotArrayCheck(fieldOpnd, IsLoopBody()))
{
// Need a dynamic check on the size of the local slot array.
Expand Down
4 changes: 2 additions & 2 deletions lib/Backend/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class IRBuilder
#if DBG_DUMP || defined(ENABLE_IR_VIEWER)
IR::IndirOpnd * BuildIndirOpnd(IR::RegOpnd *baseReg, uint32 offset, const char16 *desc);
#endif
IR::SymOpnd * BuildFieldOpnd(Js::OpCode newOpCode, Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, PropertyKind propertyKind, uint inlineCacheIndex = -1);
PropertySym * BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, uint inlineCacheIndex, PropertyKind propertyKind);
IR::SymOpnd * BuildFieldOpnd(Js::OpCode newOpCode, Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, PropertyKind propertyKind, uint inlineCacheIndex = -1, bool stableSlotSym = false);
PropertySym * BuildFieldSym(Js::RegSlot reg, Js::PropertyId propertyId, Js::PropertyIdIndexType propertyIdIndex, uint inlineCacheIndex, PropertyKind propertyKind, bool stableSlotSym = false);
SymID BuildSrcStackSymID(Js::RegSlot regSlot);
IR::RegOpnd * BuildDstOpnd(Js::RegSlot dstRegSlot, IRType type = TyVar, bool isCatchObjectSym = false);
IR::RegOpnd * BuildSrcOpnd(Js::RegSlot srcRegSlot, IRType type = TyVar);
Expand Down
Loading