Skip to content

Commit e7acf9b

Browse files
committed
[JSC] DFG iterator open fast paths should pass JSTypeRange to IsCellWithType
https://bugs.webkit.org/show_bug.cgi?id=327661 Reviewed by Yusuke Suzuki. Since 317660@main, IsCellWithType takes a JSTypeRange. Five callers in handleIteratorOpen and handleAsyncIteratorOpen still pass a bare JSType. `OpInfo(JSMapType)` compiles via the constructor for enums and is read back as the empty range { JSMapType, 0 }, so the abstract interpreter folds the check to false and the fast path is removed. These checks are emitted only when the site has seen more than one iteration mode. For example, when a for-of has seen Maps and Sets, a Map falls through to the check for Sets, which is the last one, and causes an OSR exit every time. This patch passes a JSTypeRange in these callers, and deletes `OpInfo(JSType)` so that the same mistake does not compile. Tests: JSTests/microbenchmarks/for-of-map-set-mixed.js JSTests/stress/for-await-of-mixed-iteration-modes.js JSTests/stress/for-of-mixed-iteration-modes-should-not-keep-exiting.js JSTests/stress/for-of-mixed-iteration-modes.js * JSTests/microbenchmarks/for-of-map-set-mixed.js: Added. (test): (const.set new): * JSTests/stress/for-await-of-mixed-iteration-modes.js: Added. (shouldBe): (makeSite): * JSTests/stress/for-of-mixed-iteration-modes-should-not-keep-exiting.js: Added. (shouldBe): (makeSite): * JSTests/stress/for-of-mixed-iteration-modes.js: Added. (shouldBe): (fold): (reference): (run): (makeSite): * Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp: (JSC::DFG::ByteCodeParser::handleIteratorOpen): (JSC::DFG::ByteCodeParser::handleAsyncIteratorOpen): * Source/JavaScriptCore/dfg/DFGOpInfo.h: Canonical link: https://commits.webkit.org/323817@main
1 parent 2caceaf commit e7acf9b

6 files changed

Lines changed: 250 additions & 5 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function test(iterable) {
2+
let sum = 0;
3+
for (const e of iterable) {
4+
if (typeof e === "number")
5+
sum += e;
6+
else
7+
sum += e[0] + e[1];
8+
}
9+
return sum;
10+
}
11+
noInline(test);
12+
13+
const map = new Map([[1, 10], [2, 20], [3, 30], [4, 40], [5, 50]]);
14+
const set = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
15+
16+
for (let i = 0; i < 1e5; ++i) {
17+
test(map);
18+
test(set);
19+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function shouldBe(actual, expected, message) {
2+
if (actual !== expected)
3+
throw new Error(message + ": expected " + expected + " but got " + actual);
4+
}
5+
6+
let siteCount = 0;
7+
function makeSite() {
8+
return new Function(`
9+
return async function (iterable) {
10+
try {
11+
let result = 0;
12+
for await (let x of iterable)
13+
result = result * 10 + x;
14+
return result;
15+
} catch (error) {
16+
return error instanceof TypeError ? "TypeError" : "unexpected " + error;
17+
}
18+
};
19+
// ${siteCount++}`)();
20+
}
21+
22+
async function* asyncGenerator() { yield 1; yield 2; }
23+
function* syncGenerator() { yield 3; yield 4; }
24+
let AsyncGeneratorPrototype = Object.getPrototypeOf(asyncGenerator.prototype);
25+
let AsyncIteratorPrototype = Object.getPrototypeOf(AsyncGeneratorPrototype);
26+
27+
function countingNext() {
28+
let i = 0;
29+
return () => Promise.resolve(i < 2 ? { value: ++i + 4, done: false } : { value: undefined, done: true });
30+
}
31+
32+
let kinds = {
33+
asyncGenerator: [() => asyncGenerator(), 12],
34+
sync: [() => [3, 4], 34],
35+
generic: [() => ({ [Symbol.asyncIterator]() { return { next: countingNext() }; } }), 56],
36+
};
37+
38+
let strangers = [
39+
[() => Object.create(AsyncGeneratorPrototype), "TypeError"],
40+
[() => Object.setPrototypeOf(syncGenerator(), AsyncGeneratorPrototype), "TypeError"],
41+
[() => Object.setPrototypeOf([1, 2], AsyncGeneratorPrototype), "TypeError"],
42+
[() => Object.assign(Object.create(AsyncIteratorPrototype), { next: countingNext() }), 56],
43+
];
44+
45+
let sites = [
46+
["asyncGenerator", "sync"],
47+
["asyncGenerator", "generic"],
48+
];
49+
50+
async function main() {
51+
for (let names of sites) {
52+
let site = makeSite();
53+
for (let i = 0; i < testLoopCount; ++i) {
54+
let name = names[i % names.length];
55+
shouldBe(await site(kinds[name][0]()), kinds[name][1], names + " with " + name);
56+
}
57+
for (let name in kinds)
58+
shouldBe(await site(kinds[name][0]()), kinds[name][1], names + " with " + name);
59+
for (let i = 0; i < strangers.length; ++i)
60+
shouldBe(await site(strangers[i][0]()), strangers[i][1], names + " with stranger " + i);
61+
}
62+
}
63+
64+
let failure = null;
65+
let finished = false;
66+
main().then(() => { finished = true; }, error => { failure = error; });
67+
drainMicrotasks();
68+
if (failure)
69+
throw failure;
70+
shouldBe(finished, true, "finished");
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//@ skip if not $jitTests
2+
//@ $skipModes << :lockdown
3+
4+
function shouldBe(actual, expected, message) {
5+
if (actual !== expected)
6+
throw new Error(message + ": expected " + expected + " but got " + actual);
7+
}
8+
9+
let siteCount = 0;
10+
function makeSite() {
11+
let site = new Function("iterable", `
12+
let count = 0;
13+
for (let x of iterable)
14+
count++;
15+
return count;
16+
// ${siteCount++}`);
17+
noInline(site);
18+
return site;
19+
}
20+
21+
let array = [1];
22+
let map = new Map([[1, 2]]);
23+
let set = new Set([1]);
24+
let kinds = {
25+
arrayValues: () => array.values(),
26+
arrayKeys: () => array.keys(),
27+
map: () => map,
28+
set: () => set,
29+
string: () => "1",
30+
};
31+
32+
let sites = [
33+
["map", "set"],
34+
["set", "string"],
35+
["arrayValues", "arrayKeys"],
36+
];
37+
38+
function test(names) {
39+
let site = makeSite();
40+
for (let i = 0; i < testLoopCount * 6; ++i)
41+
shouldBe(site(kinds[names[i % names.length]]()), 1, names);
42+
let compiles = numberOfDFGCompiles(site);
43+
if (compiles > 4)
44+
throw new Error(names + ": compiled " + compiles + " times");
45+
}
46+
noDFG(test);
47+
48+
for (let names of sites)
49+
test(names);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
function shouldBe(actual, expected, message) {
2+
if (actual !== expected)
3+
throw new Error(message + ": expected " + expected + " but got " + actual);
4+
}
5+
6+
function fold(result, x) {
7+
return result * 100 + (typeof x === "object" ? x[0] * 10 + x[1] : +x);
8+
}
9+
noDFG(fold);
10+
11+
function reference(iterable) {
12+
let iterator = iterable[Symbol.iterator]();
13+
let next = iterator.next;
14+
let result = 0;
15+
for (;;) {
16+
let step = next.call(iterator);
17+
if (step.done)
18+
return result;
19+
result = fold(result, step.value);
20+
}
21+
}
22+
noDFG(reference);
23+
24+
function run(f, iterable) {
25+
try {
26+
return f(iterable);
27+
} catch (error) {
28+
return error instanceof TypeError ? "TypeError" : "unexpected " + error;
29+
}
30+
}
31+
noDFG(run);
32+
33+
let siteCount = 0;
34+
function makeSite() {
35+
let site = new Function("iterable", `
36+
let result = 0;
37+
for (let x of iterable)
38+
result = fold(result, x);
39+
return result;
40+
// ${siteCount++}`);
41+
noInline(site);
42+
return site;
43+
}
44+
45+
function withProperty(object, key, value) {
46+
object[key] = value;
47+
return object;
48+
}
49+
50+
let array = [1, 2, 3];
51+
let map = new Map([[1, 4], [2, 5]]);
52+
let set = new Set([6, 7, 8]);
53+
let kinds = {
54+
arrayValues: () => array.values(),
55+
arrayKeys: () => array.keys(),
56+
map: () => map,
57+
set: () => set,
58+
string: () => "123",
59+
generic: () => ({
60+
[Symbol.iterator]() {
61+
let i = 0;
62+
return { next() { return i < 2 ? { value: ++i, done: false } : { value: undefined, done: true }; } };
63+
}
64+
}),
65+
};
66+
67+
let strangers = [
68+
() => ({ [Symbol.iterator]: Map.prototype.entries }),
69+
() => ({ [Symbol.iterator]: Set.prototype.values }),
70+
() => ({ [Symbol.iterator]: String.prototype[Symbol.iterator], toString() { return "45"; } }),
71+
() => withProperty(new Set([1]), Symbol.iterator, Map.prototype.entries),
72+
() => withProperty(new Map([[1, 2]]), Symbol.iterator, Set.prototype.values),
73+
() => withProperty([12], Symbol.iterator, String.prototype[Symbol.iterator]),
74+
() => new String("67"),
75+
() => new Uint8Array([1, 2]).values(),
76+
() => new Uint8Array([1, 2]).keys(),
77+
() => Array.prototype.values.call({ length: 2, 0: 3, 1: 4 }),
78+
];
79+
80+
let sites = [
81+
["arrayValues", "arrayKeys"],
82+
["map", "set"],
83+
["set", "string"],
84+
["string", "generic"],
85+
];
86+
87+
function test(names) {
88+
let site = makeSite();
89+
for (let i = 0; i < testLoopCount; ++i) {
90+
let name = names[i % names.length];
91+
shouldBe(site(kinds[name]()), expectedForKinds[name], names + " with " + name);
92+
}
93+
for (let name in kinds)
94+
shouldBe(run(site, kinds[name]()), expectedForKinds[name], names + " with " + name);
95+
for (let i = 0; i < strangers.length; ++i)
96+
shouldBe(run(site, strangers[i]()), expectedForStrangers[i], names + " with stranger " + i);
97+
}
98+
noDFG(test);
99+
100+
let expectedForKinds = { };
101+
for (let name in kinds)
102+
expectedForKinds[name] = run(reference, kinds[name]());
103+
let expectedForStrangers = strangers.map(stranger => run(reference, stranger()));
104+
105+
for (let names of sites)
106+
test(names);

‎Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12123,7 +12123,7 @@ void ByteCodeParser::handleIteratorOpen(const JSInstruction* currentInstruction,
1212312123
keepUsesOfCurrentInstructionAlive(currentInstruction, m_currentIndex.checkpoint());
1212412124

1212512125
Node* iteratedObject = addToGraph(GetInternalField, OpInfo(static_cast<uint32_t>(JSArrayIterator::Field::IteratedObject)), OpInfo(SpecObject), get(bytecode.m_iterable));
12126-
Node* isIteratedArray = addToGraph(IsCellWithType, OpInfo(ArrayType), iteratedObject);
12126+
Node* isIteratedArray = addToGraph(IsCellWithType, OpInfo(JSTypeRange { ArrayType, ArrayType }), iteratedObject);
1212712127

1212812128
BranchData* arrayBranchData = m_graph.m_branchData.add();
1212912129
arrayBranchData->taken = BranchTarget(fastBlock);
@@ -12330,7 +12330,7 @@ void ByteCodeParser::handleIteratorOpen(const JSInstruction* currentInstruction,
1233012330
failedBlock = allocateUntargetableBlock();
1233112331

1233212332
Node* isKnownIterFunction = addToGraph(CompareEqPtr, OpInfo(frozenMapEntriesFunction), get(bytecode.m_symbolIterator));
12333-
Node* isMap = addToGraph(IsCellWithType, OpInfo(JSMapType), get(bytecode.m_iterable));
12333+
Node* isMap = addToGraph(IsCellWithType, OpInfo(JSTypeRange { JSMapType, JSMapType }), get(bytecode.m_iterable));
1233412334

1233512335
BranchData* branchData = m_graph.m_branchData.add();
1233612336
branchData->taken = BranchTarget(fastMapBlock);
@@ -12402,7 +12402,7 @@ void ByteCodeParser::handleIteratorOpen(const JSInstruction* currentInstruction,
1240212402
failedBlock = allocateUntargetableBlock();
1240312403

1240412404
Node* isKnownIterFunction = addToGraph(CompareEqPtr, OpInfo(frozenSetValuesFunction), get(bytecode.m_symbolIterator));
12405-
Node* isSet = addToGraph(IsCellWithType, OpInfo(JSSetType), get(bytecode.m_iterable));
12405+
Node* isSet = addToGraph(IsCellWithType, OpInfo(JSTypeRange { JSSetType, JSSetType }), get(bytecode.m_iterable));
1240612406

1240712407
BranchData* branchData = m_graph.m_branchData.add();
1240812408
branchData->taken = BranchTarget(fastSetBlock);
@@ -12471,7 +12471,7 @@ void ByteCodeParser::handleIteratorOpen(const JSInstruction* currentInstruction,
1247112471
failedBlock = allocateUntargetableBlock();
1247212472

1247312473
Node* isKnownIterFunction = addToGraph(CompareEqPtr, OpInfo(frozenStringSymbolIteratorFunction), symbolIterator);
12474-
Node* isString = addToGraph(IsCellWithType, OpInfo(StringType), get(bytecode.m_iterable));
12474+
Node* isString = addToGraph(IsCellWithType, OpInfo(JSTypeRange { StringType, StringType }), get(bytecode.m_iterable));
1247512475

1247612476
BranchData* branchData = m_graph.m_branchData.add();
1247712477
branchData->taken = BranchTarget(fastStringBlock);
@@ -13416,7 +13416,7 @@ void ByteCodeParser::handleAsyncIteratorOpen(const JSInstruction* currentInstruc
1341613416
addToGraph(CheckJSCast, OpInfo(JSAsyncGenerator::info()), get(bytecode.m_iterable));
1341713417
addToGraph(CheckIsConstant, OpInfo(primordialIter), symbolAsyncIterator);
1341813418
} else {
13419-
Node* isAsyncGenerator = addToGraph(IsCellWithType, OpInfo(JSAsyncGeneratorType), get(bytecode.m_iterable));
13419+
Node* isAsyncGenerator = addToGraph(IsCellWithType, OpInfo(JSTypeRange { JSAsyncGeneratorType, JSAsyncGeneratorType }), get(bytecode.m_iterable));
1342013420
Node* isprimordialIter = addToGraph(CompareEqPtr, OpInfo(primordialIter), symbolAsyncIterator);
1342113421
Node* eligible = addToGraph(ArithBitAnd, isAsyncGenerator, isprimordialIter);
1342213422
emitExitOK();

‎Source/JavaScriptCore/dfg/DFGOpInfo.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct OpInfo {
5858
explicit OpInfo(ECMAMode ecmaMode) : m_value(ecmaMode.value()) { }
5959
explicit OpInfo(JSTypeRange range)
6060
: m_value(range.rawValue()) { }
61+
explicit OpInfo(JSType) = delete;
6162
explicit OpInfo(PrivateFieldPutKind putKind) : m_value(putKind.value()) { }
6263
template<typename EnumType>
6364
explicit OpInfo(OptionSet<EnumType> optionSet) : m_value(optionSet.toRaw()) { }

0 commit comments

Comments
 (0)