Skip to content

Commit afc1884

Browse files
DadaIsCrazyV8 LUCI CQ
authored andcommitted
[turboshaft] Fix wrong constant folding of strings map loads
is_stable should only be used on non-primitive maps. Strings can change maps despite their maps being "stable". Bug: chromium:1518396 Change-Id: I3bcb33223f6d08df30d97da6eca2b06efb052960 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5217051 Auto-Submit: Darius Mercadier <[email protected]> Reviewed-by: Toon Verwaest <[email protected]> Commit-Queue: Darius Mercadier <[email protected]> Cr-Commit-Position: refs/heads/main@{#91942}
1 parent 422b38c commit afc1884

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,8 +1790,7 @@ class MachineOptimizationReducer : public Next {
17901790
UnparkedScopeIfNeeded scope(broker);
17911791
AllowHandleDereference allow_handle_dereference;
17921792
OptionalMapRef map = TryMakeRef(broker, base.handle()->map());
1793-
if (map.has_value() && map->is_stable() && !map->is_deprecated()) {
1794-
broker->dependencies()->DependOnStableMap(*map);
1793+
if (MapLoadCanBeConstantFolded(map)) {
17951794
return __ HeapConstant(map->object());
17961795
}
17971796
}
@@ -2399,6 +2398,27 @@ class MachineOptimizationReducer : public Next {
23992398
return base::nullopt;
24002399
}
24012400

2401+
// Returns true if loading the map of an object with map {map} can be constant
2402+
// folded and done at compile time or not. For instance, doing this for
2403+
// strings is not safe, since the map of a string could change during a GC,
2404+
// but doing this for a HeapNumber is always safe.
2405+
bool MapLoadCanBeConstantFolded(OptionalMapRef map) {
2406+
if (!map.has_value()) return false;
2407+
2408+
if (map->IsJSObjectMap() && map->is_stable()) {
2409+
broker->dependencies()->DependOnStableMap(*map);
2410+
// For JS objects, this is only safe is the map is stable.
2411+
return true;
2412+
}
2413+
2414+
if (map->instance_type() ==
2415+
any_of(BIG_INT_BASE_TYPE, HEAP_NUMBER_TYPE, ODDBALL_TYPE)) {
2416+
return true;
2417+
}
2418+
2419+
return false;
2420+
}
2421+
24022422
static constexpr bool IsNegativePowerOfTwo(int64_t x) {
24032423
if (x >= 0) return false;
24042424
if (x == std::numeric_limits<int64_t>::min()) return true;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// Flags: --allow-natives-syntax
6+
7+
function get_thin_string(a, b) {
8+
var str = a + b;
9+
var o = {};
10+
o[str];
11+
return str;
12+
}
13+
14+
var str = get_thin_string("bar");
15+
16+
function CheckCS() {
17+
return str.charCodeAt();
18+
}
19+
20+
%PrepareFunctionForOptimization(CheckCS);
21+
assertEquals(CheckCS(), 98);
22+
%OptimizeFunctionOnNextCall(CheckCS);
23+
assertEquals(CheckCS(), 98);

0 commit comments

Comments
 (0)