Skip to content

Commit 1984003

Browse files
committed
[YARR] Unroll the ParenContext frame slot copy on x86_64
https://bugs.webkit.org/show_bug.cgi?id=328895 Reviewed by Yusuke Suzuki. YARR JIT saves the frame slots of a quantified group into a ParenContext on every iteration and restores them on backtrack. On ARM64 this copy is unrolled, and a loop is emitted only from 16 slots, copying 8 slots per iteration. On x86_64 five or more slots were copied by a loop that moves one slot per iteration, spending 6 instructions per slot. This patch does the same as ARM64 on x86_64. Fewer than 16 slots are copied directly, and the loop copies 8 slots per iteration. Baseline Patched regexp-parencontext-frame-slots 106.3776+-0.4497 ^ 59.2219+-0.2018 ^ definitely 1.7963x faster Tests: JSTests/microbenchmarks/regexp-parencontext-frame-slots.js JSTests/stress/regexp-parencontext-frame-slot-counts.js * JSTests/microbenchmarks/regexp-parencontext-frame-slots.js: Added. * JSTests/stress/regexp-parencontext-frame-slot-counts.js: Added. (shouldBe): (test): * Source/JavaScriptCore/yarr/YarrJIT.cpp: Canonical link: https://commits.webkit.org/324829@main
1 parent 14c21a0 commit 1984003

3 files changed

Lines changed: 92 additions & 14 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(function() {
2+
var result = 0;
3+
var n = 20000;
4+
5+
var regExps = [
6+
/^(?:a+b+c+|d+e+f+)+!/,
7+
/^(?:(?:a+b+|c+d+)(?:e+f+|g+h+)(?:i+j+|k+l+)(?:m+n+|o+p+))+!/,
8+
/^(?:\w+=(?:\d+|"[^"]*"|'[^']*');\s*)+!/,
9+
/^(?:\/[a-z]+(?:-[a-z]+)*(?:\.[a-z]+(?:-[a-z]+)*)*)+!/,
10+
];
11+
var inputs = [
12+
"aabbccddeeff".repeat(6),
13+
"aabbgghhiijjoopp".repeat(5),
14+
"key=12; name=\"va lue\"; x='y';".repeat(4),
15+
"/foo-bar.baz/qux.quux-corge".repeat(4),
16+
];
17+
var matchInputs = inputs.map((input) => input + "!");
18+
var failInputs = inputs.map((input) => input + "?");
19+
20+
for (var i = 0; i < n; ++i) {
21+
for (var j = 0; j < regExps.length; ++j) {
22+
if (regExps[j].test(matchInputs[j]))
23+
++result;
24+
if (!regExps[j].test(failInputs[j]))
25+
++result;
26+
}
27+
}
28+
29+
if (result != n * regExps.length * 2)
30+
throw "Error: bad result: " + result;
31+
})();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ runDefault
2+
3+
function shouldBe(actual, expected) {
4+
if (actual !== expected)
5+
throw new Error(`expected ${expected} but got ${actual}`);
6+
}
7+
8+
const letters = "abcdefghijklmnopqrstuvwxyz";
9+
10+
function test(termCount, target, hasSecondAlternative, unicode) {
11+
let body = "";
12+
let first = "";
13+
let second = "";
14+
for (let i = 0; i < termCount; ++i) {
15+
const letter = letters[i % letters.length];
16+
body += unicode ? `[${letter}\\u{1F600}]*` : `${letter}*`;
17+
if (i >= target)
18+
first += letter;
19+
second += letter;
20+
}
21+
if (hasSecondAlternative)
22+
body += "|_";
23+
const letter = letters[target % letters.length];
24+
const regExp = new RegExp(`^(?:#${body})+${letter}${letter}`, unicode ? "u" : "");
25+
const padding = unicode ? "\u3042\u3042\u3042" : "!!!";
26+
shouldBe(String(regExp.exec(`#${letter}${first}#${second}${padding}`)), `#${letter}${letter}`);
27+
if (hasSecondAlternative)
28+
shouldBe(String(regExp.exec(`#${letter}${first}_${padding}`)), `#${letter}${letter}`);
29+
}
30+
31+
for (const termCount of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 33]) {
32+
for (let target = 0; target < termCount; ++target) {
33+
test(termCount, target, false, false);
34+
test(termCount, target, true, false);
35+
test(termCount, target, false, true);
36+
test(termCount, target, true, true);
37+
}
38+
}

‎Source/JavaScriptCore/yarr/YarrJIT.cpp‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,17 @@ class YarrGenerator final : public YarrJITInfo {
778778

779779
// Frame slots are copied to/from the ParenContext save area by unrolling (small
780780
// counts) or by walking a pointer (large counts, to bound generated code size).
781+
#if CPU(ARM64)
781782
static constexpr unsigned directCopySlotThreshold = 5;
782783

783-
#if CPU(ARM64)
784784
// Walking copies 16-byte ldp/stp pairs, emitting a runtime loop only once there are
785785
// enough pairs to amortize its setup; smaller counts are unrolled.
786786
static constexpr unsigned pairsPerLoopIteration = 4;
787787
static constexpr unsigned minLoopIterations = 2;
788+
#else
789+
static constexpr unsigned directCopySlotThreshold = 16;
790+
static constexpr unsigned slotsPerLoopIteration = 8;
791+
static_assert(directCopySlotThreshold >= slotsPerLoopIteration);
788792
#endif
789793

790794
enum class FrameSlotTransfer : bool { Save, Restore };
@@ -806,16 +810,20 @@ class YarrGenerator final : public YarrJITInfo {
806810
unsigned count = lastSlot - firstSlot;
807811
const ptrdiff_t contextSaveAreaOffset = ParenContext::savedFrameOffset(m_parenContextSizes);
808812

809-
// Copy each slot directly if count is less than the threshold.
810-
if (count < directCopySlotThreshold) {
811-
for (unsigned index = 0; index < count; ++index) {
812-
MacroAssembler::Address frameSlot = frameAddress().withOffset((firstSlot + index) * sizeof(uintptr_t));
813-
MacroAssembler::Address contextSlot(contextGPR, contextSaveAreaOffset + index * sizeof(uintptr_t));
813+
auto emitUnrolledCopy = [&](MacroAssembler::Address frame, MacroAssembler::Address context, unsigned slots) {
814+
for (unsigned index = 0; index < slots; ++index) {
815+
MacroAssembler::Address frameSlot = frame.withOffset(index * sizeof(uintptr_t));
816+
MacroAssembler::Address contextSlot = context.withOffset(index * sizeof(uintptr_t));
814817
if (isSave)
815818
m_jit.transferPtr(frameSlot, contextSlot);
816819
else
817820
m_jit.transferPtr(contextSlot, frameSlot);
818821
}
822+
};
823+
824+
// Copy each slot directly if count is less than the threshold.
825+
if (count < directCopySlotThreshold) {
826+
emitUnrolledCopy(frameAddress().withOffset(firstSlot * sizeof(uintptr_t)), MacroAssembler::Address(contextGPR, contextSaveAreaOffset), count);
819827
return;
820828
}
821829

@@ -826,6 +834,7 @@ class YarrGenerator final : public YarrJITInfo {
826834

827835
m_jit.addPtr(MacroAssembler::TrustedImmPtr(framePointerBias), GPRInfo::callFrameRegister, framePointerGPR);
828836
m_jit.addPtr(MacroAssembler::TrustedImmPtr(contextPointerBias), contextGPR);
837+
unsigned walkedSlots = count;
829838

830839
#if CPU(ARM64)
831840
auto emitCopy = [&](unsigned slots) {
@@ -861,19 +870,19 @@ class YarrGenerator final : public YarrJITInfo {
861870
if (hasOddSlot)
862871
emitCopy(1);
863872
#else
864-
m_jit.move(MacroAssembler::TrustedImm32(count), scratchGPR);
873+
unsigned tailSlots = count % slotsPerLoopIteration;
874+
m_jit.move(MacroAssembler::TrustedImm32(count / slotsPerLoopIteration), scratchGPR);
865875
MacroAssembler::Label loopHead(&m_jit);
866-
if (isSave)
867-
m_jit.transferPtr(MacroAssembler::Address(framePointerGPR), MacroAssembler::Address(contextGPR));
868-
else
869-
m_jit.transferPtr(MacroAssembler::Address(contextGPR), MacroAssembler::Address(framePointerGPR));
870-
m_jit.addPtr(MacroAssembler::TrustedImm32(sizeof(uintptr_t)), framePointerGPR);
871-
m_jit.addPtr(MacroAssembler::TrustedImm32(sizeof(uintptr_t)), contextGPR);
876+
emitUnrolledCopy(MacroAssembler::Address(framePointerGPR), MacroAssembler::Address(contextGPR), slotsPerLoopIteration);
877+
m_jit.addPtr(MacroAssembler::TrustedImm32(slotsPerLoopIteration * sizeof(uintptr_t)), framePointerGPR);
878+
m_jit.addPtr(MacroAssembler::TrustedImm32(slotsPerLoopIteration * sizeof(uintptr_t)), contextGPR);
872879
m_jit.branchSub32(MacroAssembler::NonZero, MacroAssembler::TrustedImm32(1), scratchGPR).linkTo(loopHead, &m_jit);
880+
emitUnrolledCopy(MacroAssembler::Address(framePointerGPR), MacroAssembler::Address(contextGPR), tailSlots);
881+
walkedSlots -= tailSlots;
873882
#endif
874883

875884
// Wind contextGPR back to its incoming value.
876-
m_jit.subPtr(MacroAssembler::TrustedImmPtr(contextPointerBias + static_cast<intptr_t>(count) * static_cast<intptr_t>(sizeof(uintptr_t))), contextGPR);
885+
m_jit.subPtr(MacroAssembler::TrustedImmPtr(contextPointerBias + static_cast<intptr_t>(walkedSlots) * static_cast<intptr_t>(sizeof(uintptr_t))), contextGPR);
877886
}
878887

879888
// On return matchAmountGPR holds the pre-iteration matchAmount for the caller to increment.

0 commit comments

Comments
 (0)