Repository navigation
Expand file tree
/
Copy pathAOTArrayBuiltinEmitterCopies.cpp
More file actions
439 lines (400 loc) · 21.5 KB
/
Copy pathAOTArrayBuiltinEmitterCopies.cpp
File metadata and controls
439 lines (400 loc) · 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright (C) 2026 Oven, Inc. All rights reserved.
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "config.h"
#include "AOTArrayBuiltinEmitter.h"
#if ENABLE(AOT)
#include "AOTArrayAllocationProfile.h"
#include "AOTBarrierEmitter.h"
#include "AOTCellAllocationEmitter.h"
#include "AOTConversionEmitter.h"
#include "AOTElementEmitterAllocation.h"
#include <algorithm>
#include <optional>
namespace JSC { namespace AOT { namespace Typed {
void ArrayBuiltinEmitter::copyElements(const ArrayStorageForm& form, const ElementRange& source, const ElementRange& target, Integer count, CopyDirection direction)
{
copyElements(form, form, source, target, count, direction);
}
void ArrayBuiltinEmitter::copyElements(const ArrayStorageForm& form, const ArrayStorageForm& formOfTarget, const ElementRange& source, const ElementRange& target, Integer count, CopyDirection direction, LBasicBlock ifHole, bool reverses)
{
bool isBackward = direction == CopyDirection::Backward;
LBasicBlock loop = newBlock();
LBasicBlock element = newBlock();
LBasicBlock copied = newBlock();
ValueFromBlock start = m_out.anchor((isBackward ? count : integer(0)).value());
m_out.jump(loop);
m_out.appendTo(loop);
Integer position(m_out.phi(Machine::Int64, start));
branch(isBackward ? isNotZero(position) : below(position, count), { normalEdge(element), normalEdge(copied) });
m_out.appendTo(element);
Integer index = isBackward ? sub(position, integer(1)) : position;
Operand rawElement = elements().loadRawElement(form, source.elements, add(source.first, index));
if (ifHole)
branchUnless(logicalNot(elements().isHole(form, rawElement)), ifHole);
if (formOfTarget.kind == ArrayStorage::Double && form.kind != formOfTarget.kind) {
RELEASE_ASSERT(form.kind == ArrayStorage::Int32);
Number number = toNumber(unboxInt32Element(rawElement.as<Boxed>()));
rawElement = ifHole ? number : select(elements().isHole(form, rawElement), elements().rawHole(formOfTarget).as<Number>(), number);
} else if (form.kind == ArrayStorage::Double && form.kind != formOfTarget.kind) {
RELEASE_ASSERT(formOfTarget.kind == ArrayStorage::Contiguous);
Boxed boxed = elements().boxRawElement(form, rawElement);
rawElement = ifHole ? boxed : select(elements().isHole(form, rawElement), elements().rawHole(formOfTarget).as<Boxed>(), boxed);
} else
RELEASE_ASSERT(form.kind == formOfTarget.kind || (form.kind == ArrayStorage::Int32 && formOfTarget.kind == ArrayStorage::Contiguous));
elements().storeRawElement(formOfTarget, target.elements, add(target.first, reverses ? sub(sub(count, integer(1)), index) : index), rawElement);
m_out.addIncomingToPhi(position.value(), m_out.anchor((isBackward ? index : add(position, integer(1))).value()));
m_out.jump(loop);
m_out.appendTo(copied);
}
static std::span<const ValueID> itemsInsertedBySplice(const InlineCall& call)
{
return call.numberOfArguments() > 2 ? call.arguments().subspan(2) : std::span<const ValueID> { };
}
void ArrayBuiltinEmitter::generateArrayConcat(const InlineCall& call)
{
bool areElementsOrArrays = std::ranges::all_of(call.arguments(), [&](ValueID argument) {
return !typeOf(argument).hasObjects() || types().isSubset(typeIDOf(argument), types().builtinClass(BuiltinClasses::Array));
});
if (!checkChosenForm(call, call.canMiss() && areElementsOrArrays && call.numberOfArguments() <= maxInlineConcatOperands))
return;
ArrayAllocationEmitter allocation(generator());
LBasicBlock miss = missBlockOf(call);
Join<CellPointer> join(m_out);
bool isExpectedToHoldDoubles = resultIsExpectedToHoldDoubles(call);
auto knownFormOf = [&](ValueID list) -> const ArrayStorageForm* {
ValueID written = definitionOf(list);
const Operation& literal = operationAt(written);
if (literal.opcode != Opcode::NewArray || literal.elementsOperands().empty() || !literal.holes().empty())
return nullptr;
if (const ArrayStorageForm* named = findArrayStorageForm(literal.arrayStorage()))
return named;
return &allocation.formOfLiteral(call.context(), literal.elementsOperands(), typeOf(written));
};
auto concatenate = [&](const ArrayStorageForm& form) {
Vector<CellPointer, maxConcatenatedArrays> arrays { receiverOf(call) };
Vector<const ArrayStorageForm*, maxConcatenatedArrays> forms { &form };
const ArrayStorageForm* widest = &form;
for (ValueID argument : call.arguments()) {
if (!typeOf(argument).hasObjects()) {
arrays.append(CellPointer());
forms.append(nullptr);
continue;
}
CellPointer array = cellOperand(argument);
const ArrayStorageForm* known = knownFormOf(argument);
forms.append(known ? known : &form);
missUnless(call, elements().isPristineArray(array, { PristineAspects::Storage, PristineAspects::Names }));
missUnless(call, elements().hasShape(elements().indexingMode(array, arrayStorageTestMask), *forms.last()));
arrays.append(array);
if (forms.last()->shape > widest->shape)
widest = forms.last();
}
const ArrayStorageForm& made = isExpectedToHoldDoubles && widest->kind == ArrayStorage::Int32 ? arrayStorageFormOf(ArrayStorage::Double) : *widest;
Vector<ArrayElements, maxConcatenatedArrays> sources;
Vector<Integer, maxConcatenatedArrays> lengths;
Integer length = integer(0);
for (CellPointer array : arrays) {
sources.append(array ? elements().elementsOf(array) : ArrayElements { });
lengths.append(array ? elements().publicLength(sources.last()) : integer(1));
length = add(length, lengths.last());
}
missUnless(call, belowOrEqual(length, integer(maxLengthForElementwiseCopy)));
Vector<std::optional<Operand>, maxConcatenatedArrays> rawItems;
for (unsigned i = 0; i < arrays.size(); ++i) {
rawItems.append(arrays[i] ? std::nullopt : elements().rawElementToStore(made, call.argument(i - 1), miss));
if (rawItems.last())
elements().stopWhereHoleWouldBeStored(call.context(), made, *rawItems.last());
else if (!arrays[i]) {
m_out.jump(miss);
m_out.appendTo(newBlock());
}
}
CellPointer concatenated = allocation.allocateArrayOfHoles(call.context(), length, made, miss, ArrayAllocationEmitter::ElementsBelowLength::NotCleared);
ArrayElements target = elements().elementsOf(concatenated);
{
CodeGenerator::InitializationScope scope(generator());
Integer position = integer(0);
for (unsigned i = 0; i < arrays.size(); ++i) {
if (arrays[i])
copyElements(*forms[i], made, { sources[i], integer(0) }, { target, position }, lengths[i], CopyDirection::Forward);
else if (rawItems[i])
elements().storeRawElement(made, target, position, *rawItems[i]);
position = add(position, lengths[i]);
}
}
emitter<BarrierEmitter>().fenceAfterInitialization();
join.add(concatenated);
};
forEachFormOrMiss(call, receiverOf(call), arrayStorageTestMask, concatenate);
finish(call, join.finish());
}
const ArrayStorageForm& ArrayBuiltinEmitter::formOfCopy(const InlineCall& call, const ArrayStorageForm& formOfReceiver)
{
if (formOfReceiver.kind != ArrayStorage::Int32)
return formOfReceiver;
return resultIsExpectedToHoldDoubles(call) ? arrayStorageFormOf(ArrayStorage::Double) : formOfReceiver;
}
void ArrayBuiltinEmitter::copyIntoNewArray(const InlineCall& call, const LengthOfCopy& lengthOfCopy, const FillOfCopy& fill)
{
ArrayAllocationEmitter allocation(generator());
Join<CellPointer> join(m_out);
forEachFormOrMiss(call, receiverOf(call), arrayStorageTestMask, [&](const ArrayStorageForm& form) {
const ArrayStorageForm& made = formOfCopy(call, form);
ArrayElements source = elements().elementsOf(receiverOf(call));
Integer length = lengthOfCopy(elements().publicLength(source));
missUnless(call, belowOrEqual(length, integer(maxLengthForElementwiseCopy)));
CellPointer copy = allocation.allocateArrayOfHoles(call.context(), length, made, missBlockOf(call), ArrayAllocationEmitter::ElementsBelowLength::Cleared, ArrayAllocationEmitter::LengthIsSetBy::Caller);
ArrayElements target = elements().elementsOf(copy);
{
CodeGenerator::InitializationScope scope(generator());
fill(form, made, source, target);
elements().setPublicLength(target, length);
}
emitter<BarrierEmitter>().fenceAfterInitialization();
join.add(copy);
});
finish(call, join.finish());
}
Integer ArrayBuiltinEmitter::clampToLength(Integer relative, Integer length)
{
Integer fromEnd = add(relative, length);
return select(lessThan(relative, 0), select(lessThan(fromEnd, 0), 0, fromEnd), select(lessThan(relative, length), relative, length));
}
void ArrayBuiltinEmitter::storeItem(const InlineCall& call, const ArrayStorageForm& form, ArrayElements target, Integer position, ValueID item)
{
if (std::optional<Operand> rawElement = elements().rawElementToStore(form, item, missBlockOf(call))) {
elements().stopWhereHoleWouldBeStored(call.context(), form, *rawElement);
elements().storeRawElement(form, target, position, *rawElement);
return;
}
m_out.jump(missBlockOf(call));
m_out.appendTo(newBlock());
}
void ArrayBuiltinEmitter::generateArrayWith(const InlineCall& call)
{
if (!checkChosenForm(call, call.canMiss() && call.numberOfArguments() == 2 && typeOf(call.argument(0)).isNumber()))
return;
LBasicBlock miss = missBlockOf(call);
Integer relative = wholeNumberOrElse(call.argument(0), miss);
Integer length;
Integer position;
copyIntoNewArray(call, [&](Integer lengthOfReceiver) {
length = lengthOfReceiver;
position = select(lessThan(relative, 0), add(relative, length), relative);
missUnless(call, below(position, length));
return length;
}, [&](const ArrayStorageForm& form, const ArrayStorageForm& made, ArrayElements source, ArrayElements target) {
copyElements(form, made, { source, integer(0) }, { target, integer(0) }, length, CopyDirection::Forward, miss);
storeItem(call, made, target, position, call.argument(1));
});
}
void ArrayBuiltinEmitter::generateArrayToReversed(const InlineCall& call)
{
if (!checkChosenForm(call, call.canMiss() && !call.numberOfArguments()))
return;
Integer length;
copyIntoNewArray(call, [&](Integer lengthOfReceiver) {
return length = lengthOfReceiver;
}, [&](const ArrayStorageForm& form, const ArrayStorageForm& made, ArrayElements source, ArrayElements target) {
copyElements(form, made, { source, integer(0) }, { target, integer(0) }, length, CopyDirection::Forward, missBlockOf(call), true);
});
}
void ArrayBuiltinEmitter::generateArrayToSpliced(const InlineCall& call)
{
size_t numberOfRangeArguments = std::min<size_t>(call.numberOfArguments(), 2);
bool hasNumberRange = std::ranges::all_of(call.arguments().first(numberOfRangeArguments), [&](ValueID bound) {
return typeOf(bound).isNumber();
});
std::span<const ValueID> items = itemsInsertedBySplice(call);
if (!checkChosenForm(call, call.canMiss() && hasNumberRange && items.size() <= maxInlineSpliceInsertions))
return;
LBasicBlock miss = missBlockOf(call);
Integer start = numberOfRangeArguments ? wholeNumberOrElse(call.argument(0), miss) : integer(0);
Integer skipCount = numberOfRangeArguments > 1 ? wholeNumberOrElse(call.argument(1), miss) : Integer();
Integer from;
Integer behind;
Integer tail;
copyIntoNewArray(call, [&](Integer length) {
from = clampToLength(start, length);
Integer left = sub(length, from);
Integer skipped = !numberOfRangeArguments ? integer(0) : !skipCount ? left : select(lessThan(skipCount, 0), 0, select(lessThan(skipCount, left), skipCount, left));
behind = add(from, skipped);
tail = sub(length, behind);
return add(add(from, tail), integer(static_cast<int64_t>(items.size())));
}, [&](const ArrayStorageForm& form, const ArrayStorageForm& made, ArrayElements source, ArrayElements target) {
copyElements(form, made, { source, integer(0) }, { target, integer(0) }, from, CopyDirection::Forward, miss);
Integer position = from;
for (ValueID item : items) {
storeItem(call, made, target, position, item);
position = add(position, integer(1));
}
copyElements(form, made, { source, behind }, { target, position }, tail, CopyDirection::Forward, miss);
});
}
CellPointer ArrayBuiltinEmitter::copyRemovedElements(const InlineCall& call, const InlineSplice& splice, const ArrayStorageForm& form)
{
static_assert(maxRemovedByInlineSplice <= maxArrayElementsInRegisters);
auto removedElement = [&](int64_t index) {
return elements().loadRawElement(form, splice.elements, add(splice.from, integer(index)));
};
if (form.element == Representation::Double) {
Vector<Number, maxRemovedByInlineSplice> numbers;
for (int64_t i = 0; i < splice.removed; ++i)
numbers.append(removedElement(i).as<Number>());
return ArrayAllocationEmitter(generator()).newArrayOfNumbers(call.context(), numbers.span());
}
Vector<Boxed, maxRemovedByInlineSplice> values;
for (int64_t i = 0; i < splice.removed; ++i)
values.append(removedElement(i).as<Boxed>());
return emitter<CellAllocationEmitter>().newArrayOf(call.context(), values.span());
}
std::optional<ArrayBuiltinEmitter::RawItems> ArrayBuiltinEmitter::rawItemsToInsert(const InlineCall& call, const ArrayStorageForm& form, LBasicBlock doesNotFit)
{
RawItems inserted;
for (ValueID item : itemsInsertedBySplice(call)) {
std::optional<Operand> rawElement = elements().rawElementToStore(form, item, doesNotFit);
if (!rawElement) {
m_out.jump(doesNotFit);
return std::nullopt;
}
elements().stopWhereHoleWouldBeStored(call.context(), form, *rawElement);
inserted.append(*rawElement);
}
return inserted;
}
void ArrayBuiltinEmitter::replaceElements(const InlineCall& call, const InlineSplice& splice, const ArrayStorageForm& form, std::span<const Operand> inserted)
{
CellPointer array = splice.elements.array;
Integer from = splice.from;
int64_t added = inserted.size();
int64_t removed = splice.removed;
ArrayElements storage = elements().elementsOf(array);
if (added != removed) {
CopyDirection direction = added > removed ? CopyDirection::Backward : CopyDirection::Forward;
Integer tailBefore = add(from, removed);
copyElements(form, { storage, tailBefore }, { storage, add(from, added) }, splice.tail, direction);
for (int64_t i = 0; i < removed - added; ++i) {
Integer position = add(splice.newLength, i);
elements().storeRawElement(form, storage, position, elements().rawHole(form));
}
}
for (size_t i = 0; i < inserted.size(); ++i)
elements().storeRawElement(form, storage, add(from, integer(i)), inserted[i]);
if (added != removed)
elements().setPublicLength(storage, splice.newLength);
if (form.holdsCells)
emitter<BarrierEmitter>().barrier(call.context(), array);
}
void ArrayBuiltinEmitter::spliceInline(const InlineCall& call, Integer start, int64_t removed, LBasicBlock outOfLine, const SpliceArm& emitArm)
{
CellPointer array = receiverOf(call);
int64_t added = itemsInsertedBySplice(call).size();
ArrayElements storage = elements().elementsOf(array);
Integer length = elements().publicLength(storage);
Integer from = clampToLength(start, length);
Integer tail = sub(length, add(from, integer(removed)));
Integer maxTail = added == removed ? length : integer(maxLengthForElementwiseMove);
branchUnless(belowOrEqual(tail, maxTail), outOfLine);
Integer newLength = add(length, integer(added - removed));
if (added > removed)
branchUnless(belowOrEqual(newLength, elements().vectorLength(storage)), outOfLine);
InlineSplice splice { storage, from, tail, newLength, removed };
Word32 mode = elements().indexingMode(array, IndexingShapeAndWritabilityMask);
elements().forEachFormByShape(mode, &ElementAccessEmitter::hasWritableShape, candidateFormsOf(call), outOfLine, [&](const ArrayStorageForm& form) {
emitArm(splice, form);
}, firstCandidateFormOf(call));
}
bool ArrayBuiltinEmitter::hasNumberSpliceRange(const InlineCall& call)
{
return call.numberOfArguments() >= 2 && typeOf(call.argument(0)).isNumber() && typeOf(call.argument(1)).isNumber();
}
Integer ArrayBuiltinEmitter::wholeNumberOrElse(ValueID id, LBasicBlock isNotWhole)
{
if (typeOf(id).isInteger())
return integerOperand(id);
ConversionEmitter& conversions = emitter<ConversionEmitter>();
Number number = numberOperand(id);
if (std::optional<Integer> exact = conversions.exactIntegerBehind(number))
return *exact;
Integer whole = conversions.toInteger(number);
branchUnless(equal(toNumber(whole), number), isNotWhole);
return whole;
}
std::optional<int64_t> ArrayBuiltinEmitter::deleteCountOfInlineSplice(const InlineCall& call)
{
const Type& deleteCount = typeOf(call.argument(1));
if (!deleteCount.isInteger() || deleteCount.integers.minimum != deleteCount.integers.maximum)
return std::nullopt;
if (deleteCount.integers.minimum < 0 || deleteCount.integers.minimum > maxRemovedByInlineSplice)
return std::nullopt;
return deleteCount.integers.minimum;
}
Word32 ArrayBuiltinEmitter::int32OrElse(ValueID id, LBasicBlock otherwise)
{
Integer operand = wholeNumberOrElse(id, otherwise);
if (!typeOf(id).isInt32())
branchUnless(equal(signExtended(low32(operand)), operand), otherwise);
return low32(operand);
}
void ArrayBuiltinEmitter::spliceDiscardingByOperation(const InlineCall& call, LBasicBlock otherwise, const CallOptions& options)
{
Word32 start = int32OrElse(call.argument(0), otherwise);
Word32 deleteCount = int32OrElse(call.argument(1), otherwise);
Vector<Boxed, maxInlineSpliceInsertions> boxedItems;
for (ValueID item : itemsInsertedBySplice(call))
boxedItems.append(boxedOperand(call.context(), item));
Word32 count = word32(static_cast<int32_t>(boxedItems.size()));
Condition isDone = callOperation<Condition>(call.context(), RuntimeOperation::ArraySpliceDiscarding, { receiverOf(call), start, deleteCount, count, argumentBuffer(boxedItems.span()) }, options);
branchUnless(isDone, otherwise);
}
Boxed ArrayBuiltinEmitter::spliceByOperation(const InlineCall& call, LBasicBlock otherwise, const CallOptions& options)
{
Word32 start = int32OrElse(call.argument(0), otherwise);
Word32 deleteCount = int32OrElse(call.argument(1), otherwise);
Vector<Boxed, maxInlineSpliceInsertions> boxedItems;
for (ValueID item : itemsInsertedBySplice(call))
boxedItems.append(boxedOperand(call.context(), item));
Word32 count = word32(static_cast<int32_t>(boxedItems.size()));
Boxed removedElements = callOperation<Boxed>(call.context(), RuntimeOperation::ArraySplice, { receiverOf(call), start, deleteCount, count, argumentBuffer(boxedItems.span()) }, options);
branchUnless(isNotEmpty(removedElements), otherwise);
return removedElements;
}
void ArrayBuiltinEmitter::generateArraySplice(const InlineCall& call)
{
if (!checkChosenForm(call, call.canMiss() && hasNumberSpliceRange(call) && itemsInsertedBySplice(call).size() <= maxInlineSpliceInsertions))
return;
Representation representation = resultIsRead(call) ? resultRepresentationOf(call) : Representation::Void;
OperandJoin join(m_out, representation);
std::optional<int64_t> inlineDeleteCount = deleteCountOfInlineSplice(call);
if (inlineDeleteCount) {
LBasicBlock outOfLine = newBlock();
spliceInline(call, wholeNumberOrElse(call.argument(0), outOfLine), *inlineDeleteCount, outOfLine, [&](const InlineSplice& splice, const ArrayStorageForm& form) {
std::optional<RawItems> inserted = rawItemsToInsert(call, form, outOfLine);
if (!inserted)
return;
Operand removedElements;
if (representation != Representation::Void) {
for (int64_t i = 0; i < splice.removed; ++i) {
Operand rawElement = elements().loadRawElement(form, splice.elements, add(splice.from, integer(i)));
branchUnless(logicalNot(elements().isHole(form, rawElement)), outOfLine);
}
removedElements = inRepresentation(call.context(), representation, copyRemovedElements(call, splice, form), resultTypeOf(call));
}
replaceElements(call, splice, form, inserted->span());
join.add(removedElements);
});
m_out.appendTo(outOfLine);
}
if (representation == Representation::Void) {
spliceDiscardingByOperation(call, missBlockOf(call), { .isOnRarePath = inlineDeleteCount.has_value() });
join.add(Operand());
} else {
Boxed removedElements = spliceByOperation(call, missBlockOf(call), { .isOnRarePath = inlineDeleteCount.has_value() });
join.add(inRepresentation(call.context(), representation, removedElements, resultTypeOf(call)));
}
finishInlineCall(call, join.finish());
}
} } } // namespace JSC::AOT::Typed
#endif // ENABLE(AOT)