Skip to content

Commit 3ad5047

Browse files
committed
[JSC] Add isGCSafe flag to BulkMemoryValue and rename MemoryCopy to MemoryMove
https://bugs.webkit.org/show_bug.cgi?id=327659 rdar://190096554 Reviewed by Keith Miller. This patch is preparation for wider use of BulkMemoryValue B3 Value. 1. Rename MemoryCopy to MemoryMove. It was originally aligned to wasm's memory.copy. But its semantics is memmove, thus renaming it to make it more easy to understand. 2. Add isGCSafe flag and support GC safe memory copy. 3. We do not modify memory fill side since its semantics is memset, which is byte-sized memory fill. Tests: Source/JavaScriptCore/b3/testb3_1.cpp Source/JavaScriptCore/b3/testb3_8.cpp * Source/JavaScriptCore/b3/B3BulkMemoryValue.cpp: (JSC::B3::BulkMemoryValue::dumpMeta const): (JSC::B3::BulkMemoryValue::BulkMemoryValue): * Source/JavaScriptCore/b3/B3BulkMemoryValue.h: * Source/JavaScriptCore/b3/B3LowerMacrosAfterOptimizations.cpp: * Source/JavaScriptCore/b3/B3LowerToAir.cpp: * Source/JavaScriptCore/b3/B3Opcode.h: * Source/JavaScriptCore/b3/B3Operations.cpp: (JSC::B3::JSC_DEFINE_NOEXCEPT_JIT_OPERATION): * Source/JavaScriptCore/b3/B3Operations.h: * Source/JavaScriptCore/b3/B3ReduceStrength.cpp: * Source/JavaScriptCore/b3/B3Validate.cpp: * Source/JavaScriptCore/b3/B3Value.cpp: (JSC::B3::Value::effectsSlow const): (JSC::B3::Value::key const): * Source/JavaScriptCore/b3/B3Value.h: * Source/JavaScriptCore/b3/B3ValueInlines.h: * Source/JavaScriptCore/b3/B3ValueKey.cpp: (JSC::B3::ValueKey::materialize const): * Source/JavaScriptCore/b3/testb3.h: * Source/JavaScriptCore/b3/testb3_1.cpp: (run): * Source/JavaScriptCore/b3/testb3_8.cpp: (testMemoryMove): (testMemoryMoveConstant): (testGCSafeMemoryMove): (testMemoryCopy): Deleted. (testMemoryCopyConstant): Deleted. * Source/JavaScriptCore/wasm/WasmBBQJIT.cpp: (JSC::Wasm::BBQJITImpl::BBQJIT::addArrayCopy): * Source/JavaScriptCore/wasm/WasmOMGIRGenerator.cpp: (JSC::Wasm::OMGIRGenerator::emitMemoryCopy): (JSC::Wasm::OMGIRGenerator::addArrayCopy): * Source/JavaScriptCore/wasm/WasmOperations.cpp: * Source/JavaScriptCore/wasm/WasmOperations.h: Canonical link: https://commits.webkit.org/323890@main
1 parent 1b9d7b2 commit 3ad5047

20 files changed

Lines changed: 94 additions & 43 deletions

‎Source/JavaScriptCore/b3/B3BulkMemoryValue.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ BulkMemoryValue::~BulkMemoryValue() = default;
3636

3737
void BulkMemoryValue::dumpMeta(CommaPrinter& comma, PrintStream& out) const
3838
{
39-
out.print(comma, "readRange = ", readRange(), comma, "writeRange = ", writeRange());
39+
out.print(comma, "readRange = ", readRange(), comma, "writeRange = ", writeRange(), comma, "gcSafe = ", isGCSafe());
4040
}
4141

42-
BulkMemoryValue::BulkMemoryValue(Kind kind, Origin origin, Value* child0, Value* child1, Value* child2)
42+
BulkMemoryValue::BulkMemoryValue(Kind kind, Origin origin, Value* child0, Value* child1, Value* child2, bool isGCSafe)
4343
: Value(CheckedOpcode, kind, Void, Three, origin, child0, child1, child2)
44+
, m_isGCSafe(isGCSafe)
4445
{
46+
ASSERT(!isGCSafe || kind == MemoryMove);
4547
}
4648

4749
} } // namespace JSC::B3

‎Source/JavaScriptCore/b3/B3BulkMemoryValue.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace JSC::B3 {
3535

3636
class JS_EXPORT_PRIVATE BulkMemoryValue final : public Value {
3737
public:
38-
static bool accepts(Kind kind) { return kind == MemoryCopy || kind == MemoryFill; }
38+
static bool accepts(Kind kind) { return kind == MemoryMove || kind == MemoryFill; }
3939

4040
~BulkMemoryValue() final;
4141

@@ -45,6 +45,10 @@ class JS_EXPORT_PRIVATE BulkMemoryValue final : public Value {
4545
const HeapRange& writeRange() const { return m_writeRange; }
4646
void setWriteRange(const HeapRange& range) { m_writeRange = range; }
4747

48+
// A GC safe MemoryMove copies whole, 8-byte aligned words, so a concurrent collector never sees
49+
// a torn JSValue. Its count is a multiple of 8.
50+
bool isGCSafe() const { return m_isGCSafe; }
51+
4852
B3_SPECIALIZE_VALUE_FOR_FIXED_CHILDREN(3)
4953
B3_SPECIALIZE_VALUE_FOR_FINAL_SIZE_FIXED_CHILDREN
5054

@@ -54,10 +58,11 @@ class JS_EXPORT_PRIVATE BulkMemoryValue final : public Value {
5458
friend class Procedure;
5559
friend class Value;
5660

57-
BulkMemoryValue(Kind, Origin, Value*, Value*, Value*);
61+
BulkMemoryValue(Kind, Origin, Value*, Value*, Value*, bool isGCSafe = false);
5862

5963
HeapRange m_readRange { HeapRange::top() };
6064
HeapRange m_writeRange { HeapRange::top() };
65+
bool m_isGCSafe { false };
6166
};
6267

6368
} // namespace JSC::B3

‎Source/JavaScriptCore/b3/B3LowerMacrosAfterOptimizations.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "AirArg.h"
3232
#include "B3BlockInsertionSet.h"
33+
#include "B3BulkMemoryValue.h"
3334
#include "B3CCallValue.h"
3435
#include "B3ConstDoubleValue.h"
3536
#include "B3ConstFloatValue.h"
@@ -205,8 +206,12 @@ class LowerMacrosAfterOptimizations {
205206
break;
206207
}
207208

208-
case MemoryCopy: {
209-
Value* functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<OperationPtrTag>(operationMemoryCopy));
209+
case MemoryMove: {
210+
Value* functionAddress;
211+
if (m_value->as<BulkMemoryValue>()->isGCSafe())
212+
functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<OperationPtrTag>(operationGCSafeMemoryMove));
213+
else
214+
functionAddress = m_insertionSet.insert<ConstPtrValue>(m_index, m_origin, tagCFunction<OperationPtrTag>(operationMemoryMove));
210215
Value* result = m_insertionSet.insert<CCallValue>(
211216
m_index,
212217
m_value->type(),

‎Source/JavaScriptCore/b3/B3LowerToAir.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5012,7 +5012,7 @@ class LowerToAir {
50125012
return;
50135013
}
50145014

5015-
case MemoryCopy:
5015+
case MemoryMove:
50165016
case MemoryFill: {
50175017
// They should be lowered already.
50185018
RELEASE_ASSERT_NOT_REACHED();

‎Source/JavaScriptCore/b3/B3Opcode.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ enum Opcode : uint8_t {
290290
// We do WasmAddress(ZExt32(ptr), ...) so that we can avoid generating extraneous moves in Air.
291291
WasmAddress,
292292

293-
// This is used for Wasm bulk memory operation `memory.copy`
294-
MemoryCopy,
293+
// This is used for Wasm bulk memory operation `memory.copy`. The ranges may overlap and the
294+
// result is as if the source were copied to a temporary first, as with memmove.
295+
MemoryMove,
295296

296297
// This is used for Wasm bulk memory operation `memory.fill`
297298
MemoryFill,

‎Source/JavaScriptCore/b3/B3Operations.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "config.h"
2727
#include "B3Operations.h"
2828

29+
#include "GCMemoryOperations.h"
2930
#include <wtf/Int128.h>
3031
#include <wtf/UnalignedAccess.h>
3132

@@ -55,7 +56,7 @@ ALWAYS_INLINE void fillOverlappingEnds(uint8_t* dst, T value, size_t count)
5556

5657
} // namespace
5758

58-
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationMemoryCopy, void, (void* dst, const void* src, size_t count))
59+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationMemoryMove, void, (void* dst, const void* src, size_t count))
5960
{
6061
// Short copies dominate this operation, because memory.copy is what a compiler targeting wasm
6162
// emits for memcpy. libc's memmove spends more on dispatching by size than a short copy costs,
@@ -93,6 +94,11 @@ JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationMemoryFill, void, (void* dst, int32_t
9394
memset(dst, target, count);
9495
}
9596

97+
JSC_DEFINE_NOEXCEPT_JIT_OPERATION(operationGCSafeMemoryMove, void, (void* dst, const void* src, size_t byteCount))
98+
{
99+
gcSafeMemmove(static_cast<uint64_t*>(dst), static_cast<const uint64_t*>(src), byteCount);
100+
}
101+
96102
} // namespace JSC::B3
97103

98104
#endif // ENABLE(B3_JIT)

‎Source/JavaScriptCore/b3/B3Operations.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434

3535
namespace JSC::B3 {
3636

37-
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(operationMemoryCopy, void, (void* dst, const void* src, size_t count));
37+
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(operationMemoryMove, void, (void* dst, const void* src, size_t count));
3838
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(operationMemoryFill, void, (void* dst, int32_t target, size_t count));
39+
JSC_DECLARE_NOEXCEPT_JIT_OPERATION(operationGCSafeMemoryMove, void, (void* dst, const void* src, size_t byteCount));
3940

4041
} // namespace JSC::B3
4142

‎Source/JavaScriptCore/b3/B3ReduceStrength.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,7 @@ class ReduceStrength {
28042804
break;
28052805
}
28062806

2807-
case MemoryCopy: {
2807+
case MemoryMove: {
28082808
if (m_value->child(2)->hasInt()) {
28092809
auto* memoryCopy = m_value->as<BulkMemoryValue>();
28102810
uint64_t count = m_value->child(2)->asInt();
@@ -2813,7 +2813,11 @@ class ReduceStrength {
28132813
int32_t offset = 0;
28142814
Vector<MemoryValue*, 16> loads;
28152815

2816-
if (count >= 16 && m_proc.usesSIMD()) {
2816+
// Only 64-bit accesses are known to be single-copy atomic per word on every target.
2817+
if (memoryCopy->isGCSafe())
2818+
RELEASE_ASSERT(!(count % sizeof(uint64_t)));
2819+
2820+
if (count >= 16 && !memoryCopy->isGCSafe() && m_proc.usesSIMD()) {
28172821
while (count >= 16) {
28182822
MemoryValue* load = m_insertionSet.insert<MemoryValue>(m_index, Load, V128, m_value->origin(), m_value->child(1), offset);
28192823
load->setRange(memoryCopy->readRange());
@@ -2891,7 +2895,6 @@ class ReduceStrength {
28912895
constexpr uint64_t threshold = 128;
28922896
if (count <= threshold) {
28932897
int32_t offset = 0;
2894-
28952898
if (count >= 16 && m_proc.usesSIMD()) {
28962899
uint64_t mask64 = target << 56 | target << 48 | target << 40 | target << 32 | target << 24 | target << 16 | target << 8 | target << 0;
28972900
v128_t mask { mask64, mask64 };

‎Source/JavaScriptCore/b3/B3Validate.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class Validater {
414414
validateFence(value);
415415
validateStackAccess(value);
416416
break;
417-
case MemoryCopy:
417+
case MemoryMove:
418418
VALIDATE(!value->kind().isChill(), ("At ", *value));
419419
VALIDATE(value->numChildren() == 3, ("At ", *value));
420420
VALIDATE(value->child(0)->type() == pointerType(), ("At ", *value));

‎Source/JavaScriptCore/b3/B3Value.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ constexpr Effects constantEffectsForOpcode(Opcode opcode)
675675
case Store8:
676676
case Store16:
677677
case Store:
678-
case MemoryCopy:
678+
case MemoryMove:
679679
case MemoryFill:
680680
case AtomicWeakCAS:
681681
case AtomicStrongCAS:
@@ -799,7 +799,7 @@ Effects Value::effectsSlow() const
799799
result.controlDependent = memory->controlDependent();
800800
break;
801801
}
802-
case MemoryCopy: {
802+
case MemoryMove: {
803803
const auto* memory = as<BulkMemoryValue>();
804804
result.reads = memory->readRange();
805805
result.writes = memory->writeRange();
@@ -1132,7 +1132,7 @@ ValueKey Value::key() const
11321132
case WasmArrayGet:
11331133
case WasmArraySet:
11341134
case WasmArrayNew:
1135-
case MemoryCopy:
1135+
case MemoryMove:
11361136
case MemoryFill:
11371137
case Fence:
11381138
case CCall:

0 commit comments

Comments
 (0)