Skip to content

Commit a1e5cdc

Browse files
committed
[JSC] Add DFG / FTL intrinsic for %TypedArray%.prototype.set
https://bugs.webkit.org/show_bug.cgi?id=326994 Reviewed by Yusuke Suzuki. %TypedArray%.prototype.set is always a host function call. For a small source, the call and the argument validation cost more than the copy. This patch adds a TypedArraySet node to DFG and FTL. It speculates that the source has the same type as the target, that neither is resizable or detached, and that the copy fits in the target. Then it calls an operation which only does memmove. If a speculation fails, we OSR exit and stop using the intrinsic at that call site. Baseline Patched typed-array-prototype-set 1.8820+-0.0399 1.8734+-0.0415 typed-array-set-uint8-and-uint8clamped 1.4566+-0.0252 ? 1.4673+-0.0425 ? typed-array-set-small 180.8575+-1.2421 ^ 55.4910+-1.9837 ^ definitely 3.2592x faster typed-array-set-concat 267.4650+-0.7874 ^ 65.7571+-0.5393 ^ definitely 4.0675x faster Tests: JSTests/microbenchmarks/typed-array-set-concat.js JSTests/microbenchmarks/typed-array-set-small.js JSTests/stress/typed-array-set-intrinsic-exit.js JSTests/stress/typed-array-set-intrinsic-large.js JSTests/stress/typed-array-set-intrinsic.js * JSTests/microbenchmarks/typed-array-set-concat.js: Added. (concat): * JSTests/microbenchmarks/typed-array-set-small.js: Added. (test): * JSTests/stress/typed-array-set-intrinsic-exit.js: Added. (shouldBe): (shouldBeArray): (shouldThrow): (makeSet.const.set new): (makeSet): * JSTests/stress/typed-array-set-intrinsic-large.js: Added. (shouldBe): (shouldThrow): (set target): * JSTests/stress/typed-array-set-intrinsic.js: Added. (shouldBe): (shouldBeArray): (shouldThrow): (makeSet.const.set new): (makeSet): * Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h: (JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects): * Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp: (JSC::DFG::ByteCodeParser::handleIntrinsicCall): * Source/JavaScriptCore/dfg/DFGClobberize.h: (JSC::DFG::clobberize): * Source/JavaScriptCore/dfg/DFGCloneHelper.h: * Source/JavaScriptCore/dfg/DFGDoesGC.cpp: (JSC::DFG::doesGC): * Source/JavaScriptCore/dfg/DFGFixupPhase.cpp: (JSC::DFG::FixupPhase::fixupNode): * Source/JavaScriptCore/dfg/DFGNode.h: (JSC::DFG::Node::hasTypedArrayType): * Source/JavaScriptCore/dfg/DFGNodeType.h: * Source/JavaScriptCore/dfg/DFGOperations.cpp: (JSC::DFG::JSC_DEFINE_NOEXCEPT_JIT_OPERATION): * Source/JavaScriptCore/dfg/DFGOperations.h: * Source/JavaScriptCore/dfg/DFGPredictionPropagationPhase.cpp: * Source/JavaScriptCore/dfg/DFGSafeToExecute.h: (JSC::DFG::safeToExecute): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp: (JSC::DFG::SpeculativeJIT::compileTypedArraySet): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h: * Source/JavaScriptCore/dfg/DFGSpeculativeJIT64.cpp: (JSC::DFG::SpeculativeJIT::compile): * Source/JavaScriptCore/ftl/FTLCapabilities.cpp: (JSC::FTL::canCompile): * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileNode): (JSC::FTL::DFG::LowerDFGToB3::compileCompareStrictEq): * Source/JavaScriptCore/runtime/Intrinsic.h: * Source/JavaScriptCore/runtime/JSTypedArrayViewPrototype.cpp: (JSC::JSTypedArrayViewPrototype::finishCreation): Canonical link: https://commits.webkit.org/323823@main
1 parent 062f362 commit a1e5cdc

24 files changed

Lines changed: 649 additions & 1 deletion
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function concat(chunks, result) {
2+
let offset = 0;
3+
for (let i = 0; i < chunks.length; i++) {
4+
const chunk = chunks[i];
5+
result.set(chunk, offset);
6+
offset += chunk.length;
7+
}
8+
return offset;
9+
}
10+
noInline(concat);
11+
12+
const chunks = [];
13+
for (let i = 0; i < 16; i++) {
14+
const chunk = new Uint8Array(8 + i * 8);
15+
chunk.fill(i + 1);
16+
chunks.push(chunk);
17+
}
18+
const result = new Uint8Array(1088);
19+
20+
let total = 0;
21+
for (let i = 0; i < 1e6; i++)
22+
total += concat(chunks, result);
23+
24+
if (total !== 1088e6 || result[0] !== 1 || result[1087] !== 16)
25+
throw new Error("bad: " + total);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test(target, source, offset) {
2+
target.set(source, offset);
3+
}
4+
noInline(test);
5+
6+
const target = new Uint8Array(64);
7+
const source = new Uint8Array(16);
8+
for (let i = 0; i < source.length; i++)
9+
source[i] = i + 1;
10+
11+
for (let i = 0; i < 1e7; i++)
12+
test(target, source, i & 31);
13+
14+
if (target[46] !== 16)
15+
throw new Error("bad: " + target[46]);
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${expected} but got ${actual}`);
4+
}
5+
6+
function shouldBeArray(actual, expected) {
7+
shouldBe(actual.length, expected.length);
8+
for (let i = 0; i < expected.length; i++)
9+
shouldBe(actual[i], expected[i]);
10+
}
11+
12+
function shouldThrow(func, errorType) {
13+
let error;
14+
try {
15+
func();
16+
} catch (e) {
17+
error = e;
18+
}
19+
if (!(error instanceof errorType))
20+
throw new Error(`expected ${errorType.name} but got ${error}`);
21+
}
22+
23+
let functionCount = 0;
24+
25+
function makeSet(body = "return target.set(source, offset);") {
26+
const set = new Function("target", "source", "offset", `${body} // ${functionCount++}`);
27+
noInline(set);
28+
return set;
29+
}
30+
31+
function makeSetWithCall() {
32+
return makeSet("return Uint8Array.prototype.set.call(target, source, offset);");
33+
}
34+
35+
function warmUp(set, targetConstructor = Uint8Array) {
36+
const source = new Uint8Array([1, 2, 3, 4]);
37+
const target = new targetConstructor(8);
38+
for (let i = 0; i < testLoopCount; i++)
39+
shouldBe(set(target, source, i & 3), undefined);
40+
shouldBe(set(target, source, 4), undefined);
41+
shouldBeArray(target.subarray(4), [1, 2, 3, 4]);
42+
return set;
43+
}
44+
45+
const source = new Uint8Array([1, 2, 3, 4]);
46+
47+
function testOffsets() {
48+
{
49+
const set = warmUp(makeSet());
50+
const target = new Uint8Array(8);
51+
for (const offset of [5, 8, 9, -1, -5, 2 ** 31 - 1, -(2 ** 31), 2 ** 31, 2 ** 32, 2 ** 53, Infinity, -Infinity, -1.5])
52+
shouldThrow(() => set(target, source, offset), RangeError);
53+
shouldBeArray(target, [0, 0, 0, 0, 0, 0, 0, 0]);
54+
}
55+
56+
{
57+
const set = warmUp(makeSet());
58+
for (const [offset, expected] of [[1.5, 1], [-0.5, 0], [NaN, 0], [-0, 0], [undefined, 0], [null, 0], ["2", 2], [true, 1], [{ valueOf() { return 3; } }, 3], [4, 4]]) {
59+
const target = new Uint8Array(8);
60+
shouldBe(set(target, source, offset), undefined);
61+
for (let j = 0; j < 8; j++)
62+
shouldBe(target[j], j >= expected && j < expected + 4 ? source[j - expected] : 0);
63+
}
64+
const target = new Uint8Array(8);
65+
shouldThrow(() => set(target, source, { valueOf() { throw new SyntaxError(); } }), SyntaxError);
66+
shouldThrow(() => set(target, source, Symbol()), TypeError);
67+
shouldThrow(() => set(target, source, 1n), TypeError);
68+
shouldBeArray(target, [0, 0, 0, 0, 0, 0, 0, 0]);
69+
}
70+
}
71+
72+
function testDetached(set) {
73+
const target = new Uint8Array(8);
74+
const detached = new Uint8Array(new ArrayBuffer(64));
75+
$.detachArrayBuffer(detached.buffer);
76+
shouldThrow(() => set(target, detached, 0), TypeError);
77+
shouldThrow(() => set(detached, source, 0), TypeError);
78+
shouldThrow(() => set(detached, new Uint8Array(0), 0), TypeError);
79+
shouldThrow(() => set(detached, detached, 0), TypeError);
80+
shouldBeArray(target, [0, 0, 0, 0, 0, 0, 0, 0]);
81+
}
82+
83+
function testResizable(set) {
84+
{
85+
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
86+
const fixedLength = new Uint8Array(buffer, 0, 8);
87+
const lengthTracking = new Uint8Array(buffer);
88+
shouldBe(set(fixedLength, source, 4), undefined);
89+
shouldBeArray(fixedLength, [0, 0, 0, 0, 1, 2, 3, 4]);
90+
shouldBe(set(lengthTracking, source, 0), undefined);
91+
shouldBeArray(fixedLength, [1, 2, 3, 4, 1, 2, 3, 4]);
92+
buffer.resize(4);
93+
shouldThrow(() => set(fixedLength, source, 0), TypeError);
94+
shouldThrow(() => set(lengthTracking, source, 1), RangeError);
95+
shouldBe(set(lengthTracking, source.subarray(1), 1), undefined);
96+
shouldBeArray(lengthTracking, [1, 2, 3, 4]);
97+
buffer.resize(16);
98+
shouldBe(set(lengthTracking, source, 12), undefined);
99+
shouldBeArray(lengthTracking, [1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]);
100+
}
101+
102+
{
103+
const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
104+
const fixedLength = new Uint8Array(buffer, 0, 4);
105+
const lengthTracking = new Uint8Array(buffer);
106+
fixedLength.set(source);
107+
const target = new Uint8Array(8);
108+
shouldBe(set(target, fixedLength, 4), undefined);
109+
shouldBeArray(target, [0, 0, 0, 0, 1, 2, 3, 4]);
110+
shouldThrow(() => set(target, lengthTracking, 1), RangeError);
111+
buffer.resize(2);
112+
shouldThrow(() => set(target, fixedLength, 0), TypeError);
113+
shouldBe(set(target, lengthTracking, 0), undefined);
114+
shouldBeArray(target, [1, 2, 0, 0, 1, 2, 3, 4]);
115+
}
116+
117+
{
118+
const growable = new Uint8Array(new SharedArrayBuffer(8, { maxByteLength: 16 }));
119+
const shared = new Uint8Array(new SharedArrayBuffer(8));
120+
shouldBe(set(growable, source, 4), undefined);
121+
shouldBe(set(shared, growable.subarray(4, 8), 2), undefined);
122+
shouldBeArray(shared, [0, 0, 1, 2, 3, 4, 0, 0]);
123+
growable.buffer.grow(16);
124+
shouldBe(set(growable, shared, 8), undefined);
125+
shouldBeArray(growable, [0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 1, 2, 3, 4, 0, 0]);
126+
}
127+
}
128+
129+
function testOtherTargets() {
130+
{
131+
const set = warmUp(makeSet());
132+
shouldThrow(() => set({ set: Uint8Array.prototype.set }, source, 0), TypeError);
133+
}
134+
135+
{
136+
const set = warmUp(makeSetWithCall());
137+
shouldThrow(() => set([], source, 0), TypeError);
138+
shouldThrow(() => set(new DataView(new ArrayBuffer(8)), source, 0), TypeError);
139+
shouldThrow(() => set(1, source, 0), TypeError);
140+
}
141+
142+
{
143+
class Derived extends Uint8Array { }
144+
const set = warmUp(makeSet(), Derived);
145+
const target = new Derived(8);
146+
shouldThrow(() => set(target, source, 5), RangeError);
147+
shouldBe(set(target, new Derived([5, 6]), 6), undefined);
148+
shouldBeArray(target, [0, 0, 0, 0, 0, 0, 5, 6]);
149+
testDetached(set);
150+
}
151+
152+
{
153+
const set = warmUp(makeSet());
154+
const other = new (createGlobalObject().Uint8Array)(8);
155+
shouldBe(Uint8Array.prototype.set.call(other, source, 2), undefined);
156+
const target = new Uint8Array(8);
157+
shouldBe(set(target, other, 0), undefined);
158+
shouldBeArray(target, [0, 0, 1, 2, 3, 4, 0, 0]);
159+
shouldBe(set(other, source, 4), undefined);
160+
shouldBeArray(other, [0, 0, 1, 2, 1, 2, 3, 4]);
161+
}
162+
}
163+
164+
function testOtherSources() {
165+
{
166+
const set = warmUp(makeSet());
167+
const target = new Uint8Array(8);
168+
shouldBe(set(target, new Int16Array([-1, 256, 257, 2]), 1), undefined);
169+
shouldBeArray(target, [0, 255, 0, 1, 2, 0, 0, 0]);
170+
shouldBe(set(target, new Uint8ClampedArray([7, 8]), 6), undefined);
171+
shouldBe(set(target, new Int8Array([-2]), 5), undefined);
172+
shouldBe(set(target, new Float64Array([1.5, -1]), 0), undefined);
173+
shouldBeArray(target, [1, 255, 0, 1, 2, 254, 7, 8]);
174+
shouldThrow(() => set(target, new BigInt64Array(1), 0), TypeError);
175+
shouldThrow(() => set(target, null, 0), TypeError);
176+
shouldThrow(() => set(target, undefined, 0), TypeError);
177+
}
178+
179+
{
180+
const set = warmUp(makeSet());
181+
const target = new Uint8Array(8);
182+
shouldBe(set(target, [1, 2, 3], 1), undefined);
183+
shouldBe(set(target, { length: 2, 0: 9, 1: "10" }, 5), undefined);
184+
shouldBe(set(target, "78", 6), undefined);
185+
shouldBe(set(target, 42, 0), undefined);
186+
shouldBeArray(target, [0, 1, 2, 3, 0, 9, 7, 8]);
187+
shouldThrow(() => set(target, [1, 2, 3], 6), RangeError);
188+
let count = 0;
189+
const object = {
190+
get length() {
191+
count++;
192+
return 1;
193+
},
194+
get 0() {
195+
count++;
196+
return 5;
197+
},
198+
};
199+
shouldBe(set(target, object, 7), undefined);
200+
shouldBe(count, 2);
201+
shouldBe(target[7], 5);
202+
}
203+
204+
{
205+
const set = makeSet();
206+
const plain = [5, 6, 7, 8];
207+
const target = new Uint8Array(8);
208+
for (let i = 0; i < testLoopCount; i++) {
209+
set(target, i & 1 ? source : plain, 0);
210+
shouldBe(target[0], i & 1 ? 1 : 5);
211+
set(target, i & 1 ? plain : source, 4);
212+
shouldBe(target[7], i & 1 ? 8 : 4);
213+
}
214+
}
215+
}
216+
217+
function testReplacedFunction() {
218+
const set = warmUp(makeSet());
219+
const original = Uint8Array.prototype.set;
220+
let called = 0;
221+
Uint8Array.prototype.set = function (source, offset) {
222+
called++;
223+
return original.call(this, source, offset);
224+
};
225+
const target = new Uint8Array(8);
226+
shouldBe(set(target, new Uint8Array([1, 2]), 6), undefined);
227+
shouldBe(called, 1);
228+
shouldBeArray(target, [0, 0, 0, 0, 0, 0, 1, 2]);
229+
delete Uint8Array.prototype.set;
230+
shouldBe(set(target, new Uint8Array([3]), 0), undefined);
231+
shouldBe(called, 1);
232+
shouldBe(target[0], 3);
233+
}
234+
235+
testOffsets();
236+
testDetached(warmUp(makeSet()));
237+
testDetached(warmUp(makeSetWithCall()));
238+
testResizable(warmUp(makeSet()));
239+
testResizable(warmUp(makeSetWithCall()));
240+
testOtherTargets();
241+
testOtherSources();
242+
testReplacedFunction();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//@ memoryHog!
2+
//@ skip if $addressBits <= 32
3+
//@ runDefault("--useConcurrentJIT=0")
4+
//@ runDefault("--useConcurrentJIT=0", "--useFTLJIT=0")
5+
6+
function shouldBe(actual, expected) {
7+
if (actual !== expected)
8+
throw new Error(`expected ${expected} but got ${actual}`);
9+
}
10+
11+
function shouldThrow(func, errorType) {
12+
let error;
13+
try {
14+
func();
15+
} catch (e) {
16+
error = e;
17+
}
18+
if (!(error instanceof errorType))
19+
throw new Error(`expected ${errorType.name} but got ${error}`);
20+
}
21+
22+
function set(target, source, offset) {
23+
return target.set(source, offset);
24+
}
25+
noInline(set);
26+
27+
const gib = 1024 * 1024 * 1024;
28+
const length = 5 * gib;
29+
const target = new Uint8Array(length);
30+
const source = new Uint8Array([1, 2, 3, 4]);
31+
32+
for (let i = 0; i < testLoopCount; i++)
33+
set(target, source, i & 1023);
34+
35+
shouldThrow(() => set(target, source, -1), RangeError);
36+
shouldThrow(() => set(target, source, -4), RangeError);
37+
shouldThrow(() => set(target, source, -(2 ** 31)), RangeError);
38+
shouldBe(target[2 ** 32 - 4], 0);
39+
shouldBe(target[2 ** 32 - 1], 0);
40+
shouldBe(target[2 ** 31], 0);
41+
42+
set(target, source, 2 ** 31 - 1);
43+
shouldBe(target[2 ** 31 - 2], 0);
44+
shouldBe(target[2 ** 31 - 1], 1);
45+
shouldBe(target[2 ** 31 + 2], 4);
46+
shouldBe(target[2 ** 31 + 3], 0);
47+
48+
set(target, source, length - 4);
49+
shouldBe(target[length - 5], 0);
50+
shouldBe(target[length - 4], 1);
51+
shouldBe(target[length - 1], 4);
52+
shouldThrow(() => set(target, source, length - 3), RangeError);

0 commit comments

Comments
 (0)