Skip to content

Commit 2b7b986

Browse files
committed
[JSC] Allocate poly proto objects in create_this fast paths
https://bugs.webkit.org/show_bug.cgi?id=328035 rdar://190392057 Reviewed by Keith Miller. Poly proto means "this structure may be shared by multiple closures so that prototype object is not determined as a singleton". This means that each Function's profile can store this prototype for this site anyway. So we can implement poly-proto object allocation in the fast path easily. Test: JSTests/stress/poly-proto-create-this-fast-path.js * JSTests/stress/poly-proto-create-this-fast-path.js: Added. (shouldBe): (makeClass.Base.prototype.method): (makeClass.Base): (makeClass.Derived): (makeClass.Plain): (makeClass.Plain.prototype): (makeClass): (construct): (check): * Source/JavaScriptCore/bytecode/ObjectAllocationProfileInlines.h: (JSC::ObjectAllocationProfileBase<Derived>::initializeProfile): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp: (JSC::DFG::SpeculativeJIT::compileCreateThis): * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileCreateThis): * Source/JavaScriptCore/jit/JITOpcodes.cpp: (JSC::JIT::emit_op_create_this): * Source/JavaScriptCore/lol/LOLJIT.cpp: (JSC::LOL::LOLJIT::emit_op_create_this): * Source/JavaScriptCore/runtime/JSGlobalProxy.cpp: (JSC::JSGlobalProxy::setTarget): Canonical link: https://commits.webkit.org/324153@main
1 parent a3313ff commit 2b7b986

7 files changed

Lines changed: 98 additions & 11 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error('bad value: ' + actual + ' expected: ' + expected);
4+
}
5+
6+
function makeClass(i) {
7+
class Base {
8+
method() { return i; }
9+
}
10+
class Derived extends Base {
11+
constructor() {
12+
super();
13+
this.x = i;
14+
}
15+
}
16+
function Plain() {
17+
this.y = i;
18+
}
19+
Plain.prototype.get = function () { return this.y; };
20+
return { Derived, Plain };
21+
}
22+
23+
var classes = [];
24+
for (var i = 0; i < 4; ++i)
25+
classes.push(makeClass(i));
26+
27+
function construct(C) {
28+
return new C();
29+
}
30+
noInline(construct);
31+
32+
function check(object, C) {
33+
shouldBe(Object.getPrototypeOf(object), C.prototype);
34+
shouldBe(object instanceof C, true);
35+
}
36+
noInline(check);
37+
38+
for (var i = 0; i < testLoopCount; ++i) {
39+
var index = i % classes.length;
40+
var { Derived, Plain } = classes[index];
41+
42+
var derived = construct(Derived);
43+
check(derived, Derived);
44+
shouldBe(derived.method(), index);
45+
shouldBe(derived.x, index);
46+
47+
var plain = new Plain();
48+
check(plain, Plain);
49+
shouldBe(plain.get(), index);
50+
shouldBe(Object.keys(plain).join(), "y");
51+
}
52+
53+
var { Plain } = classes[1];
54+
var replacement = { get() { return "replacement"; } };
55+
Plain.prototype = replacement;
56+
for (var i = 0; i < testLoopCount; ++i) {
57+
var plain = construct(Plain);
58+
shouldBe(Object.getPrototypeOf(plain), replacement);
59+
shouldBe(plain.get(), "replacement");
60+
}
61+
62+
Plain.prototype = 42;
63+
var plain = construct(Plain);
64+
shouldBe(Object.getPrototypeOf(plain), Object.prototype);

‎Source/JavaScriptCore/bytecode/ObjectAllocationProfileInlines.h‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ ALWAYS_INLINE void ObjectAllocationProfileBase<Derived>::initializeProfile(VM& v
3737
ASSERT(!m_allocator);
3838
ASSERT(!m_structure);
3939

40-
// FIXME: Teach create_this's fast path how to allocate poly
41-
// proto objects: https://bugs.webkit.org/show_bug.cgi?id=177517
42-
4340
bool isPolyProto = false;
4441
FunctionExecutable* executable = nullptr;
4542
if (constructor) {
@@ -53,7 +50,8 @@ ALWAYS_INLINE void ObjectAllocationProfileBase<Derived>::initializeProfile(VM& v
5350

5451
if (Structure* structure = executable->cachedPolyProtoStructure()) {
5552
RELEASE_ASSERT(structure->typeInfo().type() == FinalObjectType);
56-
m_allocator = Allocator();
53+
prototype->didBecomePrototype(vm);
54+
m_allocator = subspaceFor<JSFinalObject>(vm)->allocatorFor(JSFinalObject::allocationSize(structure->inlineCapacity()), AllocatorForMode::EnsureAllocator);
5755
m_structure.set(vm, owner, structure);
5856
static_cast<Derived*>(this)->setPrototype(vm, owner, prototype);
5957
return;
@@ -113,7 +111,7 @@ ALWAYS_INLINE void ObjectAllocationProfileBase<Derived>::initializeProfile(VM& v
113111

114112
if (isPolyProto) {
115113
ASSERT(structure->hasPolyProto());
116-
m_allocator = Allocator();
114+
m_allocator = allocator;
117115
executable->setCachedPolyProtoStructure(vm, structure);
118116
} else {
119117
if (executable) {

‎Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16076,7 +16076,6 @@ void SpeculativeJIT::compileCreateThis(Node* node)
1607616076
// Rare data is only used to access the allocator & structure
1607716077
// We can avoid using an additional GPR this way
1607816078
GPRReg rareDataGPR = structureGPR;
16079-
GPRReg inlineCapacityGPR = rareDataGPR;
1608016079

1608116080
JumpList slowPath;
1608216081

@@ -16089,8 +16088,13 @@ void SpeculativeJIT::compileCreateThis(Node* node)
1608916088
auto butterfly = TrustedImmPtr(nullptr);
1609016089
emitAllocateJSObject(resultGPR, JITAllocator::variable(), allocatorGPR, structureGPR, butterfly, scratchGPR, slowPath, SlowAllocationResult::UndefinedBehavior);
1609116090

16092-
load8(Address(structureGPR, Structure::inlineCapacityOffset()), inlineCapacityGPR);
16093-
emitInitializeInlineStorage(resultGPR, inlineCapacityGPR);
16091+
load8(Address(structureGPR, Structure::inlineCapacityOffset()), scratchGPR);
16092+
emitInitializeInlineStorage(resultGPR, scratchGPR);
16093+
Jump hasMonoProto = branchTest64(NonZero, Address(structureGPR, Structure::prototypeOffset()));
16094+
loadPtr(Address(calleeGPR, JSFunction::offsetOfExecutableOrRareData()), scratchGPR);
16095+
loadPtr(Address(scratchGPR, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfPrototype() - JSFunction::rareDataTag), scratchGPR);
16096+
store64(scratchGPR, Address(resultGPR, offsetRelativeToBase(knownPolyProtoOffset)));
16097+
hasMonoProto.link(this);
1609416098
mutatorFence(vm());
1609516099

1609616100
addSlowPathGenerator(slowPathCall(slowPath, this, operationCreateThis, resultGPR, LinkableConstant::globalObject(*this, node), calleeGPR, node->inlineCapacity()));

‎Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10525,6 +10525,7 @@ IGNORE_CLANG_WARNINGS_END
1052510525

1052610526
LBasicBlock isFunctionBlock = m_out.newBlock();
1052710527
LBasicBlock hasRareData = m_out.newBlock();
10528+
LBasicBlock polyProtoCase = m_out.newBlock();
1052810529
LBasicBlock slowPath = m_out.newBlock();
1052910530
LBasicBlock continuation = m_out.newBlock();
1053010531

@@ -10534,12 +10535,19 @@ IGNORE_CLANG_WARNINGS_END
1053410535
LValue rareDataTags = m_out.loadPtr(callee, m_heaps.JSFunction_executableOrRareData);
1053510536
m_out.branch(m_out.testIsZeroPtr(rareDataTags, m_out.constIntPtr(JSFunction::rareDataTag)), rarely(slowPath), usually(hasRareData));
1053610537

10537-
m_out.appendTo(hasRareData, slowPath);
10538+
m_out.appendTo(hasRareData, polyProtoCase);
1053810539
LValue rareData = m_out.sub(rareDataTags, m_out.constIntPtr(JSFunction::rareDataTag));
1053910540
LValue allocator = m_out.loadPtr(rareData, m_heaps.FunctionRareData_allocator);
1054010541
LValue structure = m_out.loadPtr(rareData, m_heaps.FunctionRareData_structure);
1054110542
LValue butterfly = m_out.constIntPtr(0);
10542-
ValueFromBlock fastResult = m_out.anchor(allocateObject(allocator, structure, butterfly, slowPath));
10543+
LValue fastObject = allocateObject(allocator, structure, butterfly, slowPath);
10544+
ValueFromBlock fastResult = m_out.anchor(fastObject);
10545+
m_out.branch(m_out.isZero64(m_out.load64(structure, m_heaps.Structure_prototype)), rarely(polyProtoCase), usually(continuation));
10546+
10547+
m_out.appendTo(polyProtoCase, slowPath);
10548+
LValue prototype = m_out.loadPtr(rareData, m_heaps.FunctionRareData_prototype);
10549+
m_out.store64(prototype, m_out.address(m_heaps.properties.atAnyNumber(), fastObject, offsetRelativeToBase(knownPolyProtoOffset)));
10550+
ValueFromBlock polyProtoResult = m_out.anchor(fastObject);
1054310551
m_out.jump(continuation);
1054410552

1054510553
m_out.appendTo(slowPath, continuation);
@@ -10548,7 +10556,7 @@ IGNORE_CLANG_WARNINGS_END
1054810556
m_out.jump(continuation);
1054910557

1055010558
m_out.appendTo(continuation, lastNext);
10551-
LValue result = m_out.phi(Int64, fastResult, slowResult);
10559+
LValue result = m_out.phi(Int64, fastResult, polyProtoResult, slowResult);
1055210560

1055310561
mutatorFence();
1055410562
setJSValue(result);

‎Source/JavaScriptCore/jit/JITOpcodes.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,12 @@ void JIT::emit_op_create_this(const JSInstruction* currentInstruction)
16311631
emitAllocateJSObject(resultReg, JITAllocator::variable(), allocatorReg, structureReg, butterfly, scratchReg, slowCases, SlowAllocationResult::UndefinedBehavior);
16321632
load8(Address(structureReg, Structure::inlineCapacityOffset()), scratchReg);
16331633
emitInitializeInlineStorage(resultReg, scratchReg);
1634+
Jump hasMonoProto = branchTest64(NonZero, Address(structureReg, Structure::prototypeOffset()));
1635+
emitGetVirtualRegister(callee, scratchReg);
1636+
loadPtr(Address(scratchReg, JSFunction::offsetOfExecutableOrRareData()), scratchReg);
1637+
loadPtr(Address(scratchReg, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfPrototype() - JSFunction::rareDataTag), scratchReg);
1638+
store64(scratchReg, Address(resultReg, offsetRelativeToBase(knownPolyProtoOffset)));
1639+
hasMonoProto.link(this);
16341640
mutatorFence(*m_vm);
16351641
addSlowCase(slowCases);
16361642
emitPutVirtualRegister(bytecode.m_dst, resultReg);

‎Source/JavaScriptCore/lol/LOLJIT.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,11 @@ void LOLJIT::emit_op_create_this(const JSInstruction* currentInstruction)
18321832
emitAllocateJSObject(resultReg, JITAllocator::variable(), allocatorReg, structureReg, butterfly, s_scratch, slowCases, SlowAllocationResult::UndefinedBehavior);
18331833
load8(Address(structureReg, Structure::inlineCapacityOffset()), s_scratch);
18341834
emitInitializeInlineStorage(resultReg, s_scratch);
1835+
Jump hasMonoProto = branchTest64(NonZero, Address(structureReg, Structure::prototypeOffset()));
1836+
loadPtr(Address(calleeReg, JSFunction::offsetOfExecutableOrRareData()), s_scratch);
1837+
loadPtr(Address(s_scratch, FunctionRareData::offsetOfObjectAllocationProfile() + ObjectAllocationProfileWithPrototype::offsetOfPrototype() - JSFunction::rareDataTag), s_scratch);
1838+
store64(s_scratch, Address(resultReg, offsetRelativeToBase(knownPolyProtoOffset)));
1839+
hasMonoProto.link(this);
18351840
mutatorFence(*m_vm);
18361841
addSlowCase(slowCases);
18371842
move(resultReg, dstGPR);

‎Source/JavaScriptCore/runtime/JSGlobalProxy.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void JSGlobalProxy::setTarget(VM& vm, JSGlobalObject* globalObject)
5454
DeferredStructureTransitionWatchpointFire deferred(vm, oldStructure);
5555
Structure* newStructure = Structure::changeGlobalProxyTargetTransition(vm, oldStructure, globalObject, deferred);
5656
setStructure(vm, newStructure);
57+
if (mayBePrototype())
58+
globalObject->didBecomePrototype(vm);
5759
}
5860

5961
bool JSGlobalProxy::getOwnPropertySlot(JSObject* object, JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)

0 commit comments

Comments
 (0)