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 LdSlotArr to be hoisted across calls when the result is known to be invariant #5856

Open
wants to merge 1 commit 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
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
18 changes: 12 additions & 6 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 @@ -3882,6 +3882,7 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
IR::Instr *instr;
PropertySym *fieldSym;
bool isLdSlotThatWasNotProfiled = false;
bool stableSlots = false;

switch (newOpcode)
{
Expand Down Expand Up @@ -3916,13 +3917,18 @@ IRBuilder::BuildElementSlotI2(Js::OpCode newOpcode, uint32 offset, Js::RegSlot r
}

case Js::OpCode::LdEnvSlot:
case Js::OpCode::LdEnvObjSlot:
case Js::OpCode::StEnvSlot:
case Js::OpCode::StEnvSlotChkUndecl:
stableSlots = true;
goto SlotsCommon;

case Js::OpCode::LdEnvObjSlot:
case Js::OpCode::StEnvObjSlot:
case Js::OpCode::StEnvObjSlotChkUndecl:
stableSlots = false;

fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray);
SlotsCommon:
fieldOpnd = this->BuildFieldOpnd(Js::OpCode::LdSlotArr, this->GetEnvReg(), slotId1, (Js::PropertyIdIndexType)-1, PropertyKindSlotArray, (uint)-1, stableSlots);
regOpnd = IR::RegOpnd::New(TyVar, m_func);
instr = IR::Instr::New(Js::OpCode::LdSlotArr, regOpnd, fieldOpnd, m_func);
this->AddInstr(instr, offset);
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
17 changes: 12 additions & 5 deletions lib/Backend/Sym.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,18 +949,18 @@ StackSym *StackSym::EnsureAuxSlotPtrSym(Func * func)
///----------------------------------------------------------------------------

PropertySym *
PropertySym::New(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func)
PropertySym::New(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym)
{
StackSym * stackSym;

stackSym = func->m_symTable->FindStackSym(stackSymID);
AssertMsg(stackSym != nullptr, "Adding propertySym to non-existing stackSym... Can this happen??");

return PropertySym::New(stackSym, propertyId, propertyIdIndex, inlineCacheIndex, fieldKind, func);
return PropertySym::New(stackSym, propertyId, propertyIdIndex, inlineCacheIndex, fieldKind, func, stableSlotSym);
}

PropertySym *
PropertySym::New(StackSym *stackSym, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func)
PropertySym::New(StackSym *stackSym, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym)
{
PropertySym * propertySym;

Expand Down Expand Up @@ -989,6 +989,12 @@ PropertySym::New(StackSym *stackSym, int32 propertyId, uint32 propertyIdIndex, u

func->m_symTable->Add(propertySym);

if (stableSlotSym)
{
Assert(fieldKind == PropertyKindSlots || fieldKind == PropertyKindLocalSlots || fieldKind == PropertyKindSlotArray);
func->AddStableSlotSym(propertySym);
}

// Keep track of all the property we use from this sym so we can invalidate
// the value in glob opt
ObjectSymInfo * objectSymInfo = stackSym->EnsureObjectInfo(func);
Expand Down Expand Up @@ -1022,18 +1028,19 @@ PropertySym::Find(SymID stackSymID, int32 propertyId, Func *func)
///----------------------------------------------------------------------------

PropertySym *
PropertySym::FindOrCreate(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func)
PropertySym::FindOrCreate(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym)
{
PropertySym * propertySym;

propertySym = Find(stackSymID, propertyId, func);

if (propertySym)
{
Assert(stableSlotSym == func->IsStableSlotSym(propertySym));
return propertySym;
}

return PropertySym::New(stackSymID, propertyId, propertyIdIndex, inlineCacheIndex, fieldKind, func);
return PropertySym::New(stackSymID, propertyId, propertyIdIndex, inlineCacheIndex, fieldKind, func, stableSlotSym);
}
#if DBG_DUMP || defined(ENABLE_IR_VIEWER)

Expand Down
6 changes: 3 additions & 3 deletions lib/Backend/Sym.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,10 @@ class PropertySym: public Sym
{
friend class Sym;
public:
static PropertySym * New(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func);
static PropertySym * New(StackSym *stackSym, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func);
static PropertySym * New(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym = false);
static PropertySym * New(StackSym *stackSym, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym = false);
static PropertySym * Find(SymID stackSymID, int32 propertyId, Func *func);
static PropertySym * FindOrCreate(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func);
static PropertySym * FindOrCreate(SymID stackSymID, int32 propertyId, uint32 propertyIdIndex, uint inlineCacheIndex, PropertyKind fieldKind, Func *func, bool stableSlotSym = false);

Func *GetFunc() { return m_func; }
bool HasPropertyIdIndex() { return m_propertyIdIndex != -1; }
Expand Down