Skip to content

Commit 7d5eb1d

Browse files
victorgomesv8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[maglev] Materialize a fresh HeapNumber per deoptimization
This matches what Turbolev graph builder already does to Maglev. Fixed: 547819997 Change-Id: Iafc1185ef96db8816db36b295a3a4b7e528f9863 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/8255554 Reviewed-by: Leszek Swirski <[email protected]> Commit-Queue: Victor Gomes <[email protected]> Cr-Commit-Position: refs/heads/main@{#109347}
1 parent b5a03cd commit 7d5eb1d

3 files changed

Lines changed: 55 additions & 24 deletions

File tree

‎src/maglev/maglev-code-generator.cc‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,17 +1557,9 @@ class MaglevFrameTranslationBuilder {
15571557
return kNotDuplicated;
15581558
}
15591559

1560-
void BuildHeapNumber(const VirtualObject* vobject) {
1561-
DCHECK_EQ(vobject->object_type(), vobj::ObjectType::kHeapNumber);
1562-
ValueNode* value_node = vobject->get(offsetof(HeapNumber, value_));
1563-
return BuildHeapNumber(value_node->Cast<Float64Constant>()->value());
1564-
}
1565-
1566-
void BuildHeapNumber(Float64 number) {
1567-
DirectHandle<Object> value =
1568-
local_isolate_->factory()->NewHeapNumberFromBits<AllocationType::kOld>(
1569-
number.get_bits());
1570-
translation_array_builder_->StoreLiteral(GetDeoptLiteral(*value));
1560+
int CreateUnduplicatableId() {
1561+
object_ids_.push_back(kNotDuplicated);
1562+
return kNotDuplicated;
15711563
}
15721564

15731565
void BuildNestedValue(const ValueNode* value,
@@ -1622,13 +1614,16 @@ class MaglevFrameTranslationBuilder {
16221614
const InputLocation*& input_location,
16231615
const VirtualObjectList& virtual_objects) {
16241616
vobj::ObjectType object_type = object->object_type();
1625-
if (object_type == vobj::ObjectType::kHeapNumber) {
1626-
// TODO(jgruber): Could we use the standard path below instead?
1627-
return BuildHeapNumber(object);
1628-
}
16291617
DCHECK_NOT_NULL(object->allocation());
1618+
// HeapNumbers may be mutable object fields; each materialization must
1619+
// create a fresh box, so they are never deduplicated.
1620+
// TODO(victorgomes): Constrain which objects may contain mutable
1621+
// HeapNumbers. Immutable HeapNumbers can be stored as a literal object
1622+
// instead of a captured object.
16301623
int dup_id =
1631-
GetDuplicatedId(reinterpret_cast<intptr_t>(object->allocation()));
1624+
object_type == vobj::ObjectType::kHeapNumber
1625+
? CreateUnduplicatableId()
1626+
: GetDuplicatedId(reinterpret_cast<intptr_t>(object->allocation()));
16321627
if (dup_id != kNotDuplicated) {
16331628
translation_array_builder_->DuplicateObject(dup_id);
16341629
object->ForEachNestedRuntimeInput(
@@ -1796,7 +1791,7 @@ class MaglevFrameTranslationBuilder {
17961791
ZoneVector<IndirectHandle<TrustedObject>>* protected_deopt_literals_vector_;
17971792
ZoneVector<IndirectHandle<Object>>* deopt_literals_vector_;
17981793

1799-
static const int kNotDuplicated = -1;
1794+
static constexpr int kNotDuplicated = -1;
18001795
std::vector<intptr_t> object_ids_;
18011796
};
18021797

‎src/maglev/maglev-ir.h‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5874,11 +5874,9 @@ class VirtualObject : public FixedInputValueNodeT<0, VirtualObject> {
58745874
vobj::Field snd = FieldForOffset(offsetof(ConsString, second_));
58755875
return callback(slots_[snd.slot_index], snd);
58765876
}
5877-
if (object_type() == vobj::ObjectType::kHeapNumber) {
5878-
// HeapNumber materialization creates a literal object instead of
5879-
// slot traversal.
5880-
return true;
5881-
}
5877+
// TODO(victorgomes): Constrain which objects may contain mutable
5878+
// HeapNumbers. Immutable HeapNumbers can be stored as a literal object
5879+
// instead of traversing their slots.
58825880
}
58835881
for (int i = 0; i < slot_count(); i++) {
58845882
vobj::Field field = FieldForSlot(i);
@@ -6243,8 +6241,8 @@ struct VirtualPrimitiveHeapObjectShape : VirtualHeapObjectShape {};
62436241

62446242
struct VirtualHeapNumberShape : VirtualPrimitiveHeapObjectShape {
62456243
using T = HeapNumber;
6246-
// Special handling needed; deopt materialization uses a special path.
6247-
// TODO(jgruber): .. but could it take the standard path instead?
6244+
// Special handling needed; instances may be mutable object fields and thus
6245+
// must never be deduplicated in deopt frames.
62486246
static constexpr vobj::ObjectType kObjectType = vobj::ObjectType::kHeapNumber;
62496247
#define FIELD_LIST(V) V(value, offsetof(T, value_), vobj::FieldType::kFloat64)
62506248
DEF_SHAPE(VirtualPrimitiveHeapObjectShape, FIELD_LIST);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2026 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+
// Flags: --allow-natives-syntax --maglev
6+
7+
const boxes = [];
8+
9+
// Never called during warm-up, so this call site has no feedback and Maglev
10+
// emits an unconditional deopt for it. The literal below is therefore only
11+
// used by deopt frames and gets elided by escape analysis.
12+
function sink(o) {
13+
boxes.push(o);
14+
}
15+
16+
function foo(depth, take) {
17+
const o = {x: 1.5};
18+
if (depth > 0) foo(depth - 1, take);
19+
if (take) sink(o);
20+
}
21+
22+
%PrepareFunctionForOptimization(foo);
23+
foo(3, false);
24+
foo(3, false);
25+
%OptimizeMaglevOnNextCall(foo);
26+
foo(3, false);
27+
28+
// Deopts the innermost activation eagerly and the outer ones lazily, so every
29+
// activation materializes its own object.
30+
foo(3, true);
31+
32+
assertEquals(4, boxes.length);
33+
for (let i = 0; i < boxes.length; i++) {
34+
boxes[i].x = i;
35+
}
36+
for (let i = 0; i < boxes.length; i++) {
37+
assertEquals(i, boxes[i].x);
38+
}

0 commit comments

Comments
 (0)