v8/test/unittests/compiler/control-flow-optimizer-unittest.cc
Tobias Tebbi 4f48d04f97 [turbofan] introduce a deterministic tick measurement and assert optimization doesn't take too long
This adds a simple counter to Turbofan that's incremented throughout the compilation, hopefully
frequently enough so we can use it to detect divergence and performance bugs.
In addition, we assert that this counter never gets too high. That's the equivalent of a simple
timeout, just more deterministic. The limitations on Turbofan input size should guarantee that
we never exceed this limit. Since we probably do exceed it rarely, this check is only a DCHECK and
intended to detect performance and divergence issues, but not supposed to be performed in release
builds.

In addition, this CL adds UMA stats to observe the real world distribution of the tick measurement.

Bug: v8:9444

Change-Id: I182dac6ecac64715e3f5885ff5c7c17549351cd0
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1695475
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: Georg Neis <neis@chromium.org>
Reviewed-by: Michael Stanton <mvstanton@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62754}
2019-07-17 07:00:00 +00:00

104 lines
3.8 KiB
C++

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/compiler/control-flow-optimizer.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/machine-operator.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"
#include "testing/gmock-support.h"
using testing::AllOf;
using testing::Capture;
using testing::CaptureEq;
namespace v8 {
namespace internal {
namespace compiler {
class ControlFlowOptimizerTest : public GraphTest {
public:
explicit ControlFlowOptimizerTest(int num_parameters = 3)
: GraphTest(num_parameters), machine_(zone()), javascript_(zone()) {}
~ControlFlowOptimizerTest() override = default;
protected:
void Optimize() {
ControlFlowOptimizer optimizer(graph(), common(), machine(), tick_counter(),
zone());
optimizer.Optimize();
}
JSOperatorBuilder* javascript() { return &javascript_; }
MachineOperatorBuilder* machine() { return &machine_; }
private:
MachineOperatorBuilder machine_;
JSOperatorBuilder javascript_;
};
TEST_F(ControlFlowOptimizerTest, BuildSwitch1) {
Node* index = Parameter(0);
Node* branch0 = graph()->NewNode(
common()->Branch(),
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)),
start());
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
Node* branch1 = graph()->NewNode(
common()->Branch(),
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)),
if_false0);
Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
Node* merge =
graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1);
graph()->SetEnd(graph()->NewNode(common()->End(1), merge));
Optimize();
Capture<Node*> switch_capture;
EXPECT_THAT(
end(), IsEnd(IsMerge(
IsIfValue(IfValueParameters(0, 1), CaptureEq(&switch_capture)),
IsIfValue(IfValueParameters(1, 2), CaptureEq(&switch_capture)),
IsIfDefault(AllOf(CaptureEq(&switch_capture),
IsSwitch(index, start()))))));
}
TEST_F(ControlFlowOptimizerTest, BuildSwitch2) {
Node* input = Parameter(0);
Node* context = Parameter(1);
Node* index = graph()->NewNode(javascript()->ToNumber(), input, context,
start(), start(), start());
Node* if_success = graph()->NewNode(common()->IfSuccess(), index);
Node* branch0 = graph()->NewNode(
common()->Branch(),
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)),
if_success);
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
Node* branch1 = graph()->NewNode(
common()->Branch(),
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)),
if_false0);
Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
Node* merge =
graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1);
graph()->SetEnd(graph()->NewNode(common()->End(1), merge));
Optimize();
Capture<Node*> switch_capture;
EXPECT_THAT(
end(), IsEnd(IsMerge(
IsIfValue(IfValueParameters(0, 1), CaptureEq(&switch_capture)),
IsIfValue(IfValueParameters(1, 2), CaptureEq(&switch_capture)),
IsIfDefault(AllOf(CaptureEq(&switch_capture),
IsSwitch(index, IsIfSuccess(index)))))));
}
} // namespace compiler
} // namespace internal
} // namespace v8