Skip to content

Commit ea0feab

Browse files
committed
[JSC] Install approximate FTL bytecode cost sooner
https://bugs.webkit.org/show_bug.cgi?id=327210 rdar://189782048 Reviewed by Tadeu Zagallo. FTL's B3 compilation takes enough long time, but approximate FTL bytecode cost only requires its compiled FTL node counts. So let's record it early in BaselineJITCode so that we can use it as soon as possible. * Source/JavaScriptCore/bytecode/CodeBlock.cpp: (JSC::approximateBytecodeCostForFTLNodes): (JSC::CodeBlock::bytecodeCost const): (JSC::CodeBlock::bytecodeCostForFTLInlining const): * Source/JavaScriptCore/bytecode/CodeBlock.h: * Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp: (JSC::DFG::InliningPlan::priceCandidate const): (JSC::DFG::ByteCodeParser::inliningCost): * Source/JavaScriptCore/dfg/DFGCapabilities.cpp: (JSC::DFG::mightInlineFunctionForCall): (JSC::DFG::mightInlineFunctionForClosureCall): (JSC::DFG::mightInlineFunctionForConstruct): * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::lower): * Source/JavaScriptCore/jit/BaselineJITCode.h: (JSC::BaselineJITCode::numberOfCompiledFTLNodes const): (JSC::BaselineJITCode::setNumberOfCompiledFTLNodes): Canonical link: https://commits.webkit.org/323741@main
1 parent 78c6828 commit ea0feab

6 files changed

Lines changed: 40 additions & 6 deletions

File tree

‎Source/JavaScriptCore/bytecode/CodeBlock.cpp‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3597,12 +3597,34 @@ CodePtr<JSEntryPtrTag> CodeBlock::addressForCallConcurrently(const ConcurrentJSL
35973597
return m_jitCode->addressForCall(arityCheck);
35983598
}
35993599

3600+
#if ENABLE(FTL_JIT)
3601+
static unsigned approximateBytecodeCostForFTLNodes(unsigned numberOfCompiledDFGNodes)
3602+
{
3603+
return numberOfCompiledDFGNodes * Options::ratioFTLNodesToBytecodeCost();
3604+
}
3605+
#endif
3606+
36003607
unsigned CodeBlock::bytecodeCost() const
36013608
{
36023609
#if ENABLE(FTL_JIT)
36033610
if (jitType() == JITType::FTLJIT) {
36043611
if (auto* jitCode = static_cast<FTL::JITCode*>(m_jitCode.get()))
3605-
return std::min(static_cast<unsigned>(jitCode->numberOfCompiledDFGNodes() * Options::ratioFTLNodesToBytecodeCost()), m_bytecodeCost);
3612+
return std::min(approximateBytecodeCostForFTLNodes(jitCode->numberOfCompiledDFGNodes()), m_bytecodeCost);
3613+
}
3614+
#endif
3615+
return m_bytecodeCost;
3616+
}
3617+
3618+
unsigned CodeBlock::bytecodeCostForFTLInlining() const
3619+
{
3620+
#if ENABLE(FTL_JIT)
3621+
if (jitType() == JITType::FTLJIT)
3622+
return bytecodeCost();
3623+
3624+
const CodeBlock* baseline = m_alternative ? m_alternative.get() : this;
3625+
if (baseline->jitType() == JITType::BaselineJIT) {
3626+
if (unsigned numberOfCompiledFTLNodes = static_cast<BaselineJITCode*>(baseline->m_jitCode.get())->numberOfCompiledFTLNodes())
3627+
return std::min(approximateBytecodeCostForFTLNodes(numberOfCompiledFTLNodes), m_bytecodeCost);
36063628
}
36073629
#endif
36083630
return m_bytecodeCost;

‎Source/JavaScriptCore/bytecode/CodeBlock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ class CodeBlock : public JSCell {
339339

340340
unsigned instructionsSize() const { return instructions().size(); }
341341
unsigned bytecodeCost() const;
342+
unsigned bytecodeCostForFTLInlining() const;
342343

343344
// Exactly equivalent to codeBlock->ownerExecutable()->newReplacementCodeBlockFor(codeBlock->specializationKind())
344345
CodeBlock* newReplacement();

‎Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ unsigned InliningPlan::priceCandidate(CallVariant callee, CodeSpecializationKind
234234
return UINT_MAX;
235235
}
236236

237-
return targetCodeBlock->bytecodeCost();
237+
return m_jitType == JITType::FTLJIT ? targetCodeBlock->bytecodeCostForFTLInlining() : targetCodeBlock->bytecodeCost();
238238
}
239239

240240
// Because call frequency data is available only for polymorphic sites and not monomorphic ones,
@@ -2107,7 +2107,8 @@ std::tuple<unsigned, InlineAttribute> ByteCodeParser::inliningCost(CallVariant c
21072107
VERBOSE_LOG(" Inlining should be possible.\n");
21082108

21092109
// It might be possible to inline.
2110-
return { targetCodeBlock->bytecodeCost(), codeBlock->ownerExecutable()->inlineAttribute() };
2110+
unsigned cost = m_graph.m_plan.isFTL() ? targetCodeBlock->bytecodeCostForFTLInlining() : targetCodeBlock->bytecodeCost();
2111+
return { cost, codeBlock->ownerExecutable()->inlineAttribute() };
21112112
}
21122113

21132114
template<typename ChecksFunctor>

‎Source/JavaScriptCore/dfg/DFGCapabilities.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ bool mightInlineFunctionForCall(JITType jitType, CodeBlock* codeBlock)
8080
if (codeBlock->bytecodeCost() > Options::maximumFunctionForCallInlineCandidateBytecodeCostForDFG())
8181
return false;
8282
} else {
83-
if (codeBlock->bytecodeCost() > Options::maximumFunctionForCallInlineCandidateBytecodeCostForFTL())
83+
if (codeBlock->bytecodeCostForFTLInlining() > Options::maximumFunctionForCallInlineCandidateBytecodeCostForFTL())
8484
return false;
8585
}
8686
}
@@ -93,7 +93,7 @@ bool mightInlineFunctionForClosureCall(JITType jitType, CodeBlock* codeBlock)
9393
if (codeBlock->bytecodeCost() > Options::maximumFunctionForClosureCallInlineCandidateBytecodeCostForDFG())
9494
return false;
9595
} else {
96-
if (codeBlock->bytecodeCost() > Options::maximumFunctionForClosureCallInlineCandidateBytecodeCostForFTL())
96+
if (codeBlock->bytecodeCostForFTLInlining() > Options::maximumFunctionForClosureCallInlineCandidateBytecodeCostForFTL())
9797
return false;
9898
}
9999
}
@@ -106,7 +106,7 @@ bool mightInlineFunctionForConstruct(JITType jitType, CodeBlock* codeBlock)
106106
if (codeBlock->bytecodeCost() > Options::maximumFunctionForConstructInlineCandidateBytecodeCostForDFG())
107107
return false;
108108
} else {
109-
if (codeBlock->bytecodeCost() > Options::maximumFunctionForConstructInlineCandidateBytecodeCostForFTL())
109+
if (codeBlock->bytecodeCostForFTLInlining() > Options::maximumFunctionForConstructInlineCandidateBytecodeCostForFTL())
110110
return false;
111111
}
112112
}

‎Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "B3PatchpointValue.h"
3737
#include "B3SlotBaseValue.h"
3838
#include "B3StackmapGenerationParams.h"
39+
#include "BaselineJITCode.h"
3940
#include "BaselineJITRegisters.h"
4041
#include "B3ValueInlines.h"
4142
#include "ButterflyInlines.h"
@@ -414,6 +415,11 @@ class LowerDFGToB3 {
414415
for (DFG::BasicBlock* block : preOrder)
415416
numberOfCompiledDFGNodes += compileBlock(block);
416417
m_ftlState.jitCode->setNumberOfCompiledDFGNodes(numberOfCompiledDFGNodes);
418+
if (m_graph.m_plan.mode() == JITCompilationMode::FTL) {
419+
CodeBlock* baseline = m_graph.m_codeBlock->alternative();
420+
if (baseline && baseline->jitType() == JITType::BaselineJIT)
421+
SUPPRESS_MEMORY_UNSAFE_CAST static_cast<BaselineJITCode*>(baseline->jitCode().get())->setNumberOfCompiledFTLNodes(numberOfCompiledDFGNodes);
422+
}
417423

418424
// Make sure everything is decorated. This does a bunch of deferred decorating. This has
419425
// to happen last because our abstract heaps are generated lazily. They have to be

‎Source/JavaScriptCore/jit/BaselineJITCode.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class BaselineJITCode : public DirectJITCode, public MathICHolder {
100100
double fullnessRate() const { return m_fullnessRate; }
101101
void setFullnessRate(double rate) { m_fullnessRate = rate; }
102102

103+
unsigned numberOfCompiledFTLNodes() const { return m_numberOfCompiledFTLNodes.load(std::memory_order_relaxed); }
104+
void setNumberOfCompiledFTLNodes(unsigned count) { m_numberOfCompiledFTLNodes.store(count, std::memory_order_relaxed); }
105+
103106
FixedVector<BaselineUnlinkedCallLinkInfo> m_unlinkedCalls;
104107
FixedVector<BaselineUnlinkedPropertyInlineCache> m_unlinkedPropertyInlineCaches;
105108
FixedVector<SimpleJumpTable> m_switchJumpTables;
@@ -112,6 +115,7 @@ class BaselineJITCode : public DirectJITCode, public MathICHolder {
112115
double m_livenessRate { 0 };
113116
// The percentage of ValueProfile buckets that had a value in them.
114117
double m_fullnessRate { 0 };
118+
std::atomic<unsigned> m_numberOfCompiledFTLNodes { 0 };
115119
public:
116120
bool m_isShareable { true };
117121
};

0 commit comments

Comments
 (0)