Skip to content

Commit 456aadb

Browse files
committed
[JSC] FTL short constant string comparison should handle 16-bit strings inline
https://bugs.webkit.org/show_bug.cgi?id=327996 rdar://190346785 Reviewed by Sosuke Suzuki. We extend stringEqualsShortConstant to support 16bit characters for both input and constant string. Test: JSTests/stress/ftl-string-equal-short-constant-16bit.js * JSTests/stress/ftl-string-equal-short-constant-16bit.js: Added. (shouldBe): (variants): (inputsFor): * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileCompareStrictEq): Canonical link: https://commits.webkit.org/324146@main
1 parent db7830d commit 456aadb

2 files changed

Lines changed: 125 additions & 49 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//@ requireOptions("--useDollarVM=1")
2+
3+
function shouldBe(actual, expected, message) {
4+
if (actual !== expected)
5+
throw new Error("bad value: " + actual + " expected: " + expected + " " + message);
6+
}
7+
8+
const constants = ["", "a", "if", "for", "this", "while", "return", "default", "function", "debugger1", "instanceof", "abcdefghijk", "abcdefghijkl", "abcdefghijklm", "abcdefghijklmn", "abcdefghijklmno", "abcdefghijklmnop",
9+
"あ", "aあ", "あいう", "abcあ", "あいうえお", "abcdefgあ", "あいうえおかきくけ", "abcdefghijklmnoあ", "あいうえおかきくけこさしすせそた"];
10+
11+
let comparators = [];
12+
for (const constant of constants)
13+
comparators.push(new Function("s", `return s === ${JSON.stringify(constant)};`));
14+
15+
function variants(text) {
16+
let result = [];
17+
result.push(text);
18+
result.push($vm.make16BitStringIfPossible(text));
19+
result.push(("x" + text + "y").slice(1, 1 + text.length));
20+
result.push(("あ" + text + "い").slice(1, 1 + text.length));
21+
result.push($vm.make16BitStringIfPossible("xx" + text + "yy").substring(2, 2 + text.length));
22+
return result;
23+
}
24+
25+
function inputsFor(constant) {
26+
let inputs = variants(constant);
27+
inputs.push(...variants("q".repeat(constant.length)));
28+
if (constant.length) {
29+
for (let i = 0; i < constant.length; ++i) {
30+
let changed = constant.slice(0, i) + String.fromCharCode(constant.charCodeAt(i) + 1) + constant.slice(i + 1);
31+
inputs.push(...variants(changed));
32+
let wide = constant.slice(0, i) + String.fromCharCode(constant.charCodeAt(i) + 0x100) + constant.slice(i + 1);
33+
inputs.push(...variants(wide));
34+
}
35+
inputs.push(...variants(constant + "z"));
36+
inputs.push(...variants(constant.slice(1)));
37+
}
38+
return inputs;
39+
}
40+
41+
const ownInputs = constants.map(inputsFor);
42+
const inputs = ownInputs.map((own, i) => own.concat(ownInputs[(i + 1) % ownInputs.length]));
43+
44+
for (let iteration = 0; iteration < testLoopCount / 100; ++iteration) {
45+
for (let i = 0; i < comparators.length; ++i) {
46+
const comparator = comparators[i];
47+
const constant = constants[i];
48+
const candidates = inputs[i];
49+
for (let j = iteration % 7; j < candidates.length; j += 7)
50+
shouldBe(comparator(candidates[j]), candidates[j] == constant, JSON.stringify(candidates[j]) + " vs " + JSON.stringify(constant));
51+
}
52+
}

‎Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp‎

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22879,7 +22879,7 @@ IGNORE_CLANG_WARNINGS_END
2287922879
bool rightAtom = !rightConst.isNull() && rightConst.impl()->isAtom();
2288022880

2288122881
auto isShortConstant = [](const String& string) {
22882-
return !string.isNull() && string.is8Bit() && string.length() <= maxInlineConstantStringCompareLength;
22882+
return !string.isNull() && string.length() <= maxInlineConstantStringCompareLength;
2288322883
};
2288422884
if (isShortConstant(rightConst) && leftConst.isNull())
2288522885
return stringEqualsShortConstant(leftJSString, leftJSStringEdge, rightJSString, rightConst);
@@ -23090,18 +23090,22 @@ IGNORE_CLANG_WARNINGS_END
2309023090
// `id === "while" || id === "break"` on a sliced identifier stays free of calls.
2309123091
LValue stringEqualsShortConstant(LValue jsString, Edge edge, LValue constantJSString, const String& constant)
2309223092
{
23093-
ASSERT(constant.is8Bit());
2309423093
unsigned length = constant.length();
2309523094
ASSERT(length <= maxInlineConstantStringCompareLength);
23095+
// A constant outside Latin-1 can only equal 16-bit data, so the 16-bit path is not rare for it.
23096+
bool fitsIn8Bit = constant.containsOnlyLatin1();
23097+
auto to16BitCase = fitsIn8Bit ? rarely : unsure;
23098+
auto to8BitCase = fitsIn8Bit ? usually : unsure;
2309623099

2309723100
LBasicBlock nonRopeCase = m_out.newBlock();
2309823101
LBasicBlock nonRopeLengthMatched = m_out.newBlock();
23099-
LBasicBlock nonRope8BitCase = m_out.newBlock();
2310023102
LBasicBlock ropeCase = m_out.newBlock();
2310123103
LBasicBlock ropeLengthMatched = m_out.newBlock();
2310223104
LBasicBlock substringCase = m_out.newBlock();
2310323105
LBasicBlock substring8BitCase = m_out.newBlock();
23104-
LBasicBlock compareCase = m_out.newBlock();
23106+
LBasicBlock substring16BitCase = m_out.newBlock();
23107+
LBasicBlock compare8BitCase = m_out.newBlock();
23108+
LBasicBlock compare16BitCase = m_out.newBlock();
2310523109
LBasicBlock falseCase = m_out.newBlock();
2310623110
LBasicBlock slowCase = m_out.newBlock();
2310723111
LBasicBlock continuation = m_out.newBlock();
@@ -23115,12 +23119,11 @@ IGNORE_CLANG_WARNINGS_END
2311523119
LBasicBlock lastNext = m_out.appendTo(nonRopeCase, nonRopeLengthMatched);
2311623120
m_out.branch(m_out.notEqual(m_out.load32(value, m_heaps.StringImpl_length), m_out.constInt32(length)), unsure(falseCase), unsure(nonRopeLengthMatched));
2311723121

23118-
m_out.appendTo(nonRopeLengthMatched, nonRope8BitCase);
23119-
m_out.branch(m_out.testIsZero32(m_out.load32(value, m_heaps.StringImpl_hashAndFlags), m_out.constInt32(StringImpl::flagIs8Bit())), rarely(slowCase), usually(nonRope8BitCase));
23120-
23121-
m_out.appendTo(nonRope8BitCase, ropeCase);
23122-
ValueFromBlock nonRopeData = m_out.anchor(m_out.loadPtr(value, m_heaps.StringImpl_data));
23123-
m_out.jump(compareCase);
23122+
m_out.appendTo(nonRopeLengthMatched, ropeCase);
23123+
LValue nonRopeData = m_out.loadPtr(value, m_heaps.StringImpl_data);
23124+
ValueFromBlock nonRopeData8Bit = m_out.anchor(nonRopeData);
23125+
ValueFromBlock nonRopeData16Bit = m_out.anchor(nonRopeData);
23126+
m_out.branch(m_out.testIsZero32(m_out.load32(value, m_heaps.StringImpl_hashAndFlags), m_out.constInt32(StringImpl::flagIs8Bit())), to16BitCase(compare16BitCase), to8BitCase(compare8BitCase));
2312423127

2312523128
m_out.appendTo(ropeCase, ropeLengthMatched);
2312623129
m_out.branch(m_out.notEqual(m_out.load32NonNegative(jsString, m_heaps.JSRopeString_length), m_out.constInt32(length)), unsure(falseCase), unsure(ropeLengthMatched));
@@ -23135,24 +23138,26 @@ IGNORE_CLANG_WARNINGS_END
2313523138
m_out.lShr(packedLengthAndBaseLower, m_out.constInt32(32)),
2313623139
m_out.shl(m_out.bitAnd(packedBaseUpperAndOffset, m_out.constInt64(0xffff)), m_out.constInt32(32)));
2313723140
LValue baseImpl = m_out.loadPtr(substringBase, m_heaps.JSString_value);
23138-
// The rope's cached is8Bit bit can go stale: atomizing the base may swap in an equal 16-bit StringImpl.
23139-
m_out.branch(m_out.testIsZero32(m_out.load32(baseImpl, m_heaps.StringImpl_hashAndFlags), m_out.constInt32(StringImpl::flagIs8Bit())), rarely(slowCase), usually(substring8BitCase));
23140-
23141-
m_out.appendTo(substring8BitCase, compareCase);
2314223141
LValue substringOffset = m_out.lShr(packedBaseUpperAndOffset, m_out.constInt32(16));
23143-
ValueFromBlock substringData = m_out.anchor(m_out.add(m_out.loadPtr(baseImpl, m_heaps.StringImpl_data), substringOffset));
23144-
m_out.jump(compareCase);
23145-
23146-
m_out.appendTo(compareCase, falseCase);
23147-
LValue data = m_out.phi(pointerType(), nonRopeData, substringData);
23148-
LValue result;
23149-
if (!length)
23150-
result = m_out.booleanTrue;
23151-
else {
23152-
auto characters = constant.span8();
23153-
auto loadChunk = [&](unsigned offset, unsigned size) -> LValue {
23154-
TypedPointer pointer(m_heaps.characters8.atAnyIndex(), m_out.add(data, m_out.constIntPtr(offset)));
23155-
switch (size) {
23142+
LValue baseData = m_out.loadPtr(baseImpl, m_heaps.StringImpl_data);
23143+
// The rope's cached is8Bit bit can go stale: atomizing the base may swap in an equal 16-bit StringImpl.
23144+
m_out.branch(m_out.testIsZero32(m_out.load32(baseImpl, m_heaps.StringImpl_hashAndFlags), m_out.constInt32(StringImpl::flagIs8Bit())), to16BitCase(substring16BitCase), to8BitCase(substring8BitCase));
23145+
23146+
m_out.appendTo(substring8BitCase, substring16BitCase);
23147+
ValueFromBlock substringData8Bit = m_out.anchor(m_out.add(baseData, substringOffset));
23148+
m_out.jump(compare8BitCase);
23149+
23150+
m_out.appendTo(substring16BitCase, compare8BitCase);
23151+
ValueFromBlock substringData16Bit = m_out.anchor(m_out.add(baseData, m_out.shl(substringOffset, m_out.constInt32(1))));
23152+
m_out.jump(compare16BitCase);
23153+
23154+
auto emitCompare = [&](IndexedAbstractHeap& heap, LValue data, std::span<const uint8_t> expected) -> LValue {
23155+
unsigned size = expected.size();
23156+
if (!size)
23157+
return m_out.booleanTrue;
23158+
auto loadChunk = [&](unsigned offset, unsigned chunkSize) -> LValue {
23159+
TypedPointer pointer(heap.atAnyIndex(), m_out.add(data, m_out.constIntPtr(offset)));
23160+
switch (chunkSize) {
2315623161
case 1:
2315723162
return m_out.load8ZeroExt32(pointer);
2315823163
case 2:
@@ -23165,31 +23170,50 @@ IGNORE_CLANG_WARNINGS_END
2316523170
RELEASE_ASSERT_NOT_REACHED();
2316623171
return nullptr;
2316723172
};
23168-
auto expectedChunk = [&](unsigned offset, unsigned size) -> LValue {
23169-
uint64_t expected = 0;
23170-
memcpySpan(asMutableByteSpan(expected).first(size), characters.subspan(offset, size));
23171-
if (size == 8)
23172-
return m_out.constInt64(static_cast<int64_t>(expected));
23173-
return m_out.constInt32(static_cast<int32_t>(expected));
23173+
auto expectedChunk = [&](unsigned offset, unsigned chunkSize) -> LValue {
23174+
uint64_t bits = 0;
23175+
memcpySpan(asMutableByteSpan(bits).first(chunkSize), expected.subspan(offset, chunkSize));
23176+
if (chunkSize == 8)
23177+
return m_out.constInt64(static_cast<int64_t>(bits));
23178+
return m_out.constInt32(static_cast<int32_t>(bits));
23179+
};
23180+
auto difference = [&](unsigned offset, unsigned chunkSize) -> LValue {
23181+
return m_out.bitXor(loadChunk(offset, chunkSize), expectedChunk(offset, chunkSize));
2317423182
};
2317523183

23176-
static_assert(maxInlineConstantStringCompareLength <= 16);
23177-
unsigned chunkSize = length >= 8 ? 8 : length >= 4 ? 4 : length >= 2 ? 2 : 1;
23178-
if (length == chunkSize)
23179-
result = m_out.equal(loadChunk(0, chunkSize), expectedChunk(0, chunkSize));
23180-
else {
23181-
// The tail chunk overlaps the head chunk when the remainder is not a power of two.
23182-
unsigned tailSize = roundUpToPowerOfTwo(length - chunkSize);
23183-
unsigned tailOffset = length - tailSize;
23184-
LValue head = m_out.bitXor(loadChunk(0, chunkSize), expectedChunk(0, chunkSize));
23185-
LValue tail = m_out.bitXor(loadChunk(tailOffset, tailSize), expectedChunk(tailOffset, tailSize));
23186-
if (chunkSize == 8 && tailSize < 8)
23187-
tail = m_out.zeroExt(tail, Int64);
23188-
LValue difference = m_out.bitOr(head, tail);
23189-
result = chunkSize == 8 ? m_out.isZero64(difference) : m_out.isZero32(difference);
23184+
if (size > 8) {
23185+
// The last chunk overlaps the previous one when the size is not a multiple of 8.
23186+
LValue differences = difference(0, 8);
23187+
for (unsigned offset = 8; offset + 8 < size; offset += 8)
23188+
differences = m_out.bitOr(differences, difference(offset, 8));
23189+
differences = m_out.bitOr(differences, difference(size - 8, 8));
23190+
return m_out.isZero64(differences);
2319023191
}
23192+
23193+
unsigned chunkSize = size >= 8 ? 8 : size >= 4 ? 4 : size >= 2 ? 2 : 1;
23194+
if (size == chunkSize)
23195+
return m_out.equal(loadChunk(0, chunkSize), expectedChunk(0, chunkSize));
23196+
// The tail chunk overlaps the head chunk when the remainder is not a power of two.
23197+
unsigned tailSize = roundUpToPowerOfTwo(size - chunkSize);
23198+
return m_out.isZero32(m_out.bitOr(difference(0, chunkSize), difference(size - tailSize, tailSize)));
23199+
};
23200+
23201+
std::array<Latin1Character, maxInlineConstantStringCompareLength> characters8 { };
23202+
std::array<char16_t, maxInlineConstantStringCompareLength> characters16 { };
23203+
for (unsigned i = 0; i < length; ++i) {
23204+
characters16[i] = constant[i];
23205+
characters8[i] = static_cast<Latin1Character>(characters16[i]);
2319123206
}
23192-
ValueFromBlock compareResult = m_out.anchor(result);
23207+
23208+
m_out.appendTo(compare8BitCase, compare16BitCase);
23209+
LValue data8Bit = m_out.phi(pointerType(), nonRopeData8Bit, substringData8Bit);
23210+
LValue compare8Bit = fitsIn8Bit ? emitCompare(m_heaps.characters8, data8Bit, asByteSpan(std::span { characters8 }.first(length))) : m_out.booleanFalse;
23211+
ValueFromBlock compare8BitResult = m_out.anchor(compare8Bit);
23212+
m_out.jump(continuation);
23213+
23214+
m_out.appendTo(compare16BitCase, falseCase);
23215+
LValue data16Bit = m_out.phi(pointerType(), nonRopeData16Bit, substringData16Bit);
23216+
ValueFromBlock compare16BitResult = m_out.anchor(emitCompare(m_heaps.characters16, data16Bit, asByteSpan(std::span { characters16 }.first(length))));
2319323217
m_out.jump(continuation);
2319423218

2319523219
m_out.appendTo(falseCase, slowCase);
@@ -23201,7 +23225,7 @@ IGNORE_CLANG_WARNINGS_END
2320123225
m_out.jump(continuation);
2320223226

2320323227
m_out.appendTo(continuation, lastNext);
23204-
return m_out.phi(Int32, compareResult, falseResult, slowResult);
23228+
return m_out.phi(Int32, compare8BitResult, compare16BitResult, falseResult, slowResult);
2320523229
}
2320623230

2320723231
enum ScratchFPRUsage {

0 commit comments

Comments
 (0)