Skip to content

Commit e0a43f2

Browse files
jakobkummerowV8 LUCI CQ
authored andcommitted
[turboshaft][wasm] Fix 64-bit atomic load/store on 32-bit arch
Int64Lowering currently cannot deal with "elem_size_log2" scaling parameters, because AtomicWord32Pair doesn't support them. So the MachineOptimizationReducer shouldn't produce these parameters on this particular configuration. Fixed: chromium:1520311 Change-Id: I68f5cf48fc2d617bd62aeced018983eb6e77d56c Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5232164 Commit-Queue: Nico Hartmann <nicohartmann@chromium.org> Auto-Submit: Jakob Kummerow <jkummerow@chromium.org> Reviewed-by: Nico Hartmann <nicohartmann@chromium.org> Cr-Commit-Position: refs/heads/main@{#91985}
1 parent 682dadb commit e0a43f2

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

src/compiler/turboshaft/int64-lowering-reducer.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ class Int64LoweringReducer : public Next {
249249
if (kind.is_atomic) {
250250
if (loaded_rep == MemoryRepresentation::Int64() ||
251251
loaded_rep == MemoryRepresentation::Uint64()) {
252+
// TODO(jkummerow): Support non-zero scales in AtomicWord32PairOp, and
253+
// remove the corresponding bailout in MachineOptimizationReducer to
254+
// allow generating them.
255+
CHECK_EQ(element_scale, 0);
252256
return __ AtomicWord32PairLoad(base, index, offset);
253257
}
254258
if (result_rep == RegisterRepresentation::Word64()) {
@@ -282,8 +286,10 @@ class Int64LoweringReducer : public Next {
282286
stored_rep == MemoryRepresentation::Uint64()) {
283287
auto [low, high] = Unpack(value);
284288
if (kind.is_atomic) {
285-
// Linear wasm doesn't use element_size_log2.
286-
DCHECK_EQ(element_size_log2, 0);
289+
// TODO(jkummerow): Support non-zero scales in AtomicWord32PairOp, and
290+
// remove the corresponding bailout in MachineOptimizationReducer to
291+
// allow generating them.
292+
CHECK_EQ(element_size_log2, 0);
287293
return __ AtomicWord32PairStore(base, index, low, high, offset);
288294
}
289295
return __ Tuple(

src/compiler/turboshaft/machine-optimization-reducer.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,14 @@ class MachineOptimizationReducer : public Next {
17001700
maybe_indirect_pointer_tag);
17011701
}
17021702
if (ShouldSkipOptimizationStep()) goto no_change;
1703-
1703+
#if V8_TARGET_ARCH_32_BIT
1704+
if (kind.is_atomic && stored_rep.SizeInBytes() == 8) {
1705+
// AtomicWord32PairOp (as used by Int64Lowering) cannot handle
1706+
// element_scale != 0 currently.
1707+
// TODO(jkummerow): Add support for element_scale in AtomicWord32PairOp.
1708+
goto no_change;
1709+
}
1710+
#endif
17041711
if (stored_rep.SizeInBytes() <= 4) {
17051712
value = TryRemoveWord32ToWord64Conversion(value);
17061713
}
@@ -1758,6 +1765,14 @@ class MachineOptimizationReducer : public Next {
17581765
offset, element_scale);
17591766
}
17601767
if (ShouldSkipOptimizationStep()) goto no_change;
1768+
#if V8_TARGET_ARCH_32_BIT
1769+
if (kind.is_atomic && loaded_rep.SizeInBytes() == 8) {
1770+
// AtomicWord32PairOp (as used by Int64Lowering) cannot handle
1771+
// element_scale != 0 currently.
1772+
// TODO(jkummerow): Add support for element_scale in AtomicWord32PairOp.
1773+
goto no_change;
1774+
}
1775+
#endif
17611776

17621777
while (true) {
17631778
index =
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
d8.file.execute('test/mjsunit/wasm/wasm-module-builder.js');
6+
7+
const builder = new WasmModuleBuilder();
8+
9+
builder.addMemory(1, 1);
10+
builder.exportMemoryAs("memory");
11+
12+
builder.addFunction("store", kSig_v_i).exportFunc().addBody([
13+
kExprLocalGet, 0,
14+
kExprI32Const, 3,
15+
kExprI32Shl,
16+
kExprI64Const, 42,
17+
kAtomicPrefix, kExprI64AtomicStore, 3, 0, // alignment, offset
18+
]);
19+
20+
builder.addFunction("load", kSig_l_i).exportFunc().addBody([
21+
kExprLocalGet, 0,
22+
kExprI32Const, 3,
23+
kExprI32Shl,
24+
kAtomicPrefix, kExprI64AtomicLoad, 3, 0, // alignment, offset
25+
]);
26+
27+
let instance = builder.instantiate();
28+
const kStoreIndex = 1;
29+
instance.exports.store(kStoreIndex);
30+
31+
let i64 = new BigInt64Array(instance.exports.memory.buffer);
32+
33+
assertEquals(0n, i64[0]);
34+
assertEquals(42n, i64[kStoreIndex]);
35+
36+
const kLoadIndex = 10;
37+
const kLoadValue = 1234n;
38+
i64[kLoadIndex] = kLoadValue;
39+
let load = instance.exports.load;
40+
assertEquals(0n, load(kLoadIndex * 8));
41+
assertEquals(kLoadValue, load(kLoadIndex));

0 commit comments

Comments
 (0)