Commit 887d8d7e authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

Reland "[turbofan] Handle mixed packed/unpacked multimaps in Array.prototype.push"

This is a reland of ae14edca
Original change's description:
> [turbofan] Handle mixed packed/unpacked multimaps in Array.prototype.push
> 
> Bug: v8:7127, v8:7204, v8:7205
> Change-Id: I4eb009492222b208ff8875b4b7940174dfb132ff
> Reviewed-on: https://chromium-review.googlesource.com/847576
> Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
> Reviewed-by: Michael Stanton <mvstanton@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#50451}

Bug: v8:7127, v8:7204, v8:7205
Change-Id: I327aa69f0a12f8b3e3fd4e00219591f59e7ed746
Reviewed-on: https://chromium-review.googlesource.com/859857Reviewed-by: 's avatarMichael Stanton <mvstanton@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#50524}
parent 43d588cc
...@@ -3612,11 +3612,12 @@ Reduction JSCallReducer::ReduceArrayPrototypePush(Node* node) { ...@@ -3612,11 +3612,12 @@ Reduction JSCallReducer::ReduceArrayPrototypePush(Node* node) {
if (result == NodeProperties::kNoReceiverMaps) return NoChange(); if (result == NodeProperties::kNoReceiverMaps) return NoChange();
DCHECK_NE(0, receiver_maps.size()); DCHECK_NE(0, receiver_maps.size());
const ElementsKind kind = receiver_maps[0]->elements_kind(); ElementsKind kind = receiver_maps[0]->elements_kind();
for (Handle<Map> receiver_map : receiver_maps) { for (Handle<Map> receiver_map : receiver_maps) {
if (!CanInlineArrayResizeOperation(receiver_map)) return NoChange(); if (!CanInlineArrayResizeOperation(receiver_map)) return NoChange();
if (receiver_map->elements_kind() != kind) return NoChange(); if (!UnionElementsKindUptoPackedness(&kind, receiver_map->elements_kind()))
return NoChange();
} }
// Install code dependencies on the {receiver} global array protector cell. // Install code dependencies on the {receiver} global array protector cell.
......
...@@ -191,6 +191,43 @@ inline ElementsKind GetHoleyElementsKind(ElementsKind packed_kind) { ...@@ -191,6 +191,43 @@ inline ElementsKind GetHoleyElementsKind(ElementsKind packed_kind) {
return packed_kind; return packed_kind;
} }
inline bool UnionElementsKindUptoPackedness(ElementsKind* a_out,
ElementsKind b) {
// Assert that the union of two ElementKinds can be computed via std::max.
static_assert(PACKED_SMI_ELEMENTS < HOLEY_SMI_ELEMENTS,
"ElementsKind union not computable via std::max.");
static_assert(PACKED_ELEMENTS < HOLEY_ELEMENTS,
"ElementsKind union not computable via std::max.");
static_assert(PACKED_DOUBLE_ELEMENTS < HOLEY_DOUBLE_ELEMENTS,
"ElementsKind union not computable via std::max.");
ElementsKind a = *a_out;
switch (a) {
case HOLEY_SMI_ELEMENTS:
case PACKED_SMI_ELEMENTS:
if (b == PACKED_SMI_ELEMENTS || b == HOLEY_SMI_ELEMENTS) {
*a_out = std::max(a, b);
return true;
}
break;
case PACKED_ELEMENTS:
case HOLEY_ELEMENTS:
if (b == PACKED_ELEMENTS || b == HOLEY_ELEMENTS) {
*a_out = std::max(a, b);
return true;
}
break;
case PACKED_DOUBLE_ELEMENTS:
case HOLEY_DOUBLE_ELEMENTS:
if (b == PACKED_DOUBLE_ELEMENTS || b == HOLEY_DOUBLE_ELEMENTS) {
*a_out = std::max(a, b);
return true;
}
break;
default:
break;
}
return false;
}
inline ElementsKind FastSmiToObjectElementsKind(ElementsKind from_kind) { inline ElementsKind FastSmiToObjectElementsKind(ElementsKind from_kind) {
DCHECK(IsSmiElementsKind(from_kind)); DCHECK(IsSmiElementsKind(from_kind));
......
...@@ -2,98 +2,118 @@ ...@@ -2,98 +2,118 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Flags: --allow-natives-syntax --opt // Flags: --allow-natives-syntax --opt --no-always-opt
(function singleUnreliableReceiverMap() { function runTest(f, message, mkICTraining, deoptArg) {
function f(a, g) { function test(f, message, ictraining, deoptArg) {
a.push(2, g()); // Train the call ic to the maps.
} let t = ictraining;
f([1], () => 3); // We put the training data into local variables
f([1], () => 3); // to ensure their maps are kepts alive. If the
%OptimizeFunctionOnNextCall(f); // maps die, gc *may* deoptimize {f}, which makes
f([1], () => 3); // the test flaky.
assertOptimized(f); let t1 = t();
})(); let t2 = t();
let t3 = t();
(function singleUnreliableReceiverMapDeopt() { for (let a of t1) {
function f(a, g) { f(a.arr, () => a.el);
a.push(2, g()); }
for (let a of t2) {
f(a.arr, () => a.el);
}
%OptimizeFunctionOnNextCall(f);
message += " trained with" + JSON.stringify(t());
if (deoptArg == undefined) {
// Make sure the optimized function can handle
// all trained maps without deopt.
for (let a of t3) {
f(a.arr, () => a.el);
message += " for args " + JSON.stringify(a);
assertOptimized(f, undefined, message + " should have been optimized");
}
} else {
// Trigger deopt, causing no-speculation bit to be set.
let a1 = deoptArg;
let a2 = deoptArg;
message += " for args " + JSON.stringify(a1);
f(a1.arr, () => a1.el);
assertUnoptimized(f, undefined, message + " should have been unoptimized");
%OptimizeFunctionOnNextCall(f);
// No speculation should protect against further deopts.
f(a2.arr, () => a2.el);
assertOptimized(f, undefined, message + " should have been optimized");
}
} }
f([1], () => 3); // Get function as a string.
f([1], () => 3); var testString = test.toString();
%OptimizeFunctionOnNextCall(f); // Remove the function header..
f([1], () => true); testString = testString.replace(new RegExp("[^\n]*"), "let f = " + f.toString() + ";");
%OptimizeFunctionOnNextCall(f); // ..and trailing '}'.
f([1], () => true); testString = testString.replace(new RegExp("[^\n]*$"), "");
assertOptimized(f); // Substitute parameters.
})(); testString = testString.replace(new RegExp("ictraining", 'g'), mkICTraining.toString());
testString = testString.replace(new RegExp("deoptArg", 'g'),
(function multipleUnreliableReceiverMaps(){ deoptArg ? JSON.stringify(deoptArg) : "undefined");
function f(a, g) {
a.push(2, g());
}
let b = [1]
b.x = 3;
f([1], () => 3); var modTest = new Function("message", testString);
f(b, () => 3); //print(modTest);
f([1], () => 3); modTest(message);
f(b, () => 3); }
%OptimizeFunctionOnNextCall(f);
f([1], () => 3);
assertOptimized(f);
})();
(function multipleUnreliableReceiverMapsDeopt(){ let checks = {
function f(a, g) { smiReceiver:
a.push(2, g()); { mkTrainingArguments : () => [{arr:[1], el:3}],
} deoptingArguments : [{arr:[1], el:true}, {arr:[0.1], el:1}, {arr:[{}], el:1}]
let b = [1] },
b.x = 3; objectReceiver:
{ mkTrainingArguments : () => [{arr:[{}], el:0.1}],
deoptingArguments : []
},
multipleSmiReceivers:
{ mkTrainingArguments : () => { let b = [1]; b.x=3; return [{arr:[1], el:3}, {arr:b, el:3}] },
deoptingArguments : [{arr:[1], el:true}, {arr:[0.1], el:1}, {arr:[{}], el:1}]
},
multipleSmiReceiversPackedUnpacked:
{ mkTrainingArguments : () => { let b = [1]; b[100] = 3; return [{arr:[1], el:3}, {arr:b, el:3}] },
deoptingArguments : [ {arr:[1], el:true} ]
},
multipleDoubleReceivers:
{ mkTrainingArguments : () => { let b = [0.1]; b.x=0.3; return [{arr:[0.1], el:0.3}, {arr:b, el:0.3}] },
deoptingArguments : [{arr:[{}], el:true}, {arr:[0.1], el:true}]
},
multipleDoubleReceiversPackedUnpacked:
{ mkTrainingArguments : () => { let b = [0.1]; b[100] = 0.3; return [{arr:[0.1], el:0.3}, {arr:b, el:0.3}] },
deoptingArguments : [{arr:[{}], el:true}, {arr:[0.1], el:true}]
},
multipleMixedReceivers:
{ mkTrainingArguments : () => { let b = [0.1]; b.x=0.3; return [{arr:[1], el:0.3}, {arr:[{}], el:true}, {arr:b, el:0.3}] },
deoptingArguments : []
},
multipleMixedReceiversPackedUnpacked:
{ mkTrainingArguments : () => { let b = [0.1]; b[100] = 0.3; return [{arr:[1], el:0.3}, {arr:[{}], el:true}, {arr:b, el:0.3}] },
deoptingArguments : []
},
};
f([1], () => 3);
f(b, () => 3);
f([1], () => 3);
f(b, () => 3);
%OptimizeFunctionOnNextCall(f);
f([0.1], () => 3);
%OptimizeFunctionOnNextCall(f);
f([0.1], () => 3);
assertOptimized(f);
})();
(function multipleReliableReceiverMaps(){ const functions = {
function f(a) { push_reliable: (a,g) => { let b = g(); return a.push(2, b); },
a.push(2); push_unreliable: (a,g) => { return a.push(2, g()); },
} }
let b = [1]
b.x = 3;
f([1]); Object.keys(checks).forEach(
f(b); key => {
f([1]); let check = checks[key];
f(b);
%OptimizeFunctionOnNextCall(f);
f([1]);
assertOptimized(f);
})();
(function multipleReliableReceiverMapsDeopt(){ for (fnc in functions) {
function f(a) { runTest(functions[fnc], "test-reliable-" + key, check.mkTrainingArguments);
a.push(2); // Test each deopting arg separately.
for (let deoptArg of check.deoptingArguments) {
runTest(functions[fnc], "testDeopt-reliable-" + key, check.mkTrainingArguments, deoptArg);
}
}
} }
let b = [1] );
b.x = 3;
f([1]);
f(b);
f([1]);
f(b);
%OptimizeFunctionOnNextCall(f);
f([0.1]);
%OptimizeFunctionOnNextCall(f);
f([0.1]);
assertOptimized(f);
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment