Commit a0c51862 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Add a flag --wasm-opt to test optimizations in the WASM pipeline.

R=ahaas@chromium.org,bradnelson@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2528033002
Cr-Commit-Position: refs/heads/master@{#41381}
parent 000dea2f
......@@ -50,12 +50,12 @@ Node* MachineOperatorReducer::Float64Mul(Node* lhs, Node* rhs) {
Node* MachineOperatorReducer::Float64PowHalf(Node* value) {
value =
graph()->NewNode(machine()->Float64Add(), Float64Constant(0.0), value);
return graph()->NewNode(
common()->Select(MachineRepresentation::kFloat64, BranchHint::kFalse),
graph()->NewNode(machine()->Float64LessThanOrEqual(), value,
Float64Constant(-V8_INFINITY)),
Float64Constant(V8_INFINITY),
graph()->NewNode(machine()->Float64Sqrt(), value));
Diamond d(graph(), common(),
graph()->NewNode(machine()->Float64LessThanOrEqual(), value,
Float64Constant(-V8_INFINITY)),
BranchHint::kFalse);
return d.Phi(MachineRepresentation::kFloat64, Float64Constant(V8_INFINITY),
graph()->NewNode(machine()->Float64Sqrt(), value));
}
Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) {
......@@ -841,14 +841,13 @@ Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
if (base::bits::IsPowerOfTwo32(divisor)) {
uint32_t const mask = divisor - 1;
Node* const zero = Int32Constant(0);
node->ReplaceInput(
0, graph()->NewNode(machine()->Int32LessThan(), dividend, zero));
node->ReplaceInput(
1, Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)));
node->ReplaceInput(2, Word32And(dividend, mask));
NodeProperties::ChangeOp(
node,
common()->Select(MachineRepresentation::kWord32, BranchHint::kFalse));
Diamond d(graph(), common(),
graph()->NewNode(machine()->Int32LessThan(), dividend, zero),
BranchHint::kFalse);
return Replace(
d.Phi(MachineRepresentation::kWord32,
Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask)),
Word32And(dividend, mask)));
} else {
Node* quotient = Int32Div(dividend, divisor);
DCHECK_EQ(dividend, node->InputAt(0));
......
......@@ -113,15 +113,19 @@ class PipelineData {
}
// For WASM compile entry point.
PipelineData(ZoneStats* zone_stats, CompilationInfo* info, Graph* graph,
PipelineData(ZoneStats* zone_stats, CompilationInfo* info, JSGraph* jsgraph,
SourcePositionTable* source_positions)
: isolate_(info->isolate()),
info_(info),
debug_name_(info_->GetDebugName()),
zone_stats_(zone_stats),
graph_zone_scope_(zone_stats_, ZONE_NAME),
graph_(graph),
graph_(jsgraph->graph()),
source_positions_(source_positions),
machine_(jsgraph->machine()),
common_(jsgraph->common()),
javascript_(jsgraph->javascript()),
jsgraph_(jsgraph),
instruction_zone_scope_(zone_stats_, ZONE_NAME),
instruction_zone_(instruction_zone_scope_.zone()),
register_allocation_zone_scope_(zone_stats_, ZONE_NAME),
......@@ -603,13 +607,13 @@ PipelineCompilationJob::Status PipelineCompilationJob::FinalizeJobImpl() {
class PipelineWasmCompilationJob final : public CompilationJob {
public:
explicit PipelineWasmCompilationJob(CompilationInfo* info, Graph* graph,
explicit PipelineWasmCompilationJob(CompilationInfo* info, JSGraph* jsgraph,
CallDescriptor* descriptor,
SourcePositionTable* source_positions)
: CompilationJob(info->isolate(), info, "TurboFan",
State::kReadyToExecute),
zone_stats_(info->isolate()->allocator()),
data_(&zone_stats_, info, graph, source_positions),
data_(&zone_stats_, info, jsgraph, source_positions),
pipeline_(&data_),
linkage_(descriptor) {}
......@@ -640,6 +644,23 @@ PipelineWasmCompilationJob::ExecuteJobImpl() {
}
pipeline_.RunPrintAndVerify("Machine", true);
if (FLAG_wasm_opt) {
PipelineData* data = &data_;
PipelineRunScope scope(data, "WASM optimization");
JSGraphReducer graph_reducer(data->jsgraph(), scope.zone());
DeadCodeElimination dead_code_elimination(&graph_reducer, data->graph(),
data->common());
ValueNumberingReducer value_numbering(scope.zone(), data->graph()->zone());
MachineOperatorReducer machine_reducer(data->jsgraph());
CommonOperatorReducer common_reducer(&graph_reducer, data->graph(),
data->common(), data->machine());
AddReducer(data, &graph_reducer, &dead_code_elimination);
AddReducer(data, &graph_reducer, &value_numbering);
AddReducer(data, &graph_reducer, &machine_reducer);
AddReducer(data, &graph_reducer, &common_reducer);
graph_reducer.ReduceGraph();
pipeline_.RunPrintAndVerify("Optimized Machine", true);
}
if (!pipeline_.ScheduleAndSelectInstructions(&linkage_, true)) return FAILED;
return SUCCEEDED;
......@@ -1703,9 +1724,9 @@ CompilationJob* Pipeline::NewCompilationJob(Handle<JSFunction> function) {
// static
CompilationJob* Pipeline::NewWasmCompilationJob(
CompilationInfo* info, Graph* graph, CallDescriptor* descriptor,
CompilationInfo* info, JSGraph* jsgraph, CallDescriptor* descriptor,
SourcePositionTable* source_positions) {
return new PipelineWasmCompilationJob(info, graph, descriptor,
return new PipelineWasmCompilationJob(info, jsgraph, descriptor,
source_positions);
}
......
......@@ -20,6 +20,7 @@ class RegisterConfiguration;
namespace compiler {
class CallDescriptor;
class JSGraph;
class Graph;
class InstructionSequence;
class Schedule;
......@@ -32,7 +33,7 @@ class Pipeline : public AllStatic {
// Returns a new compilation job for the WebAssembly compilation info.
static CompilationJob* NewWasmCompilationJob(
CompilationInfo* info, Graph* graph, CallDescriptor* descriptor,
CompilationInfo* info, JSGraph* jsgraph, CallDescriptor* descriptor,
SourcePositionTable* source_positions);
// Run the pipeline on a machine graph and generate code. The {schedule} must
......
......@@ -3428,8 +3428,8 @@ void WasmCompilationUnit::ExecuteCompilation() {
descriptor =
module_env_->GetI32WasmCallDescriptor(&compilation_zone_, descriptor);
}
job_.reset(Pipeline::NewWasmCompilationJob(&info_, jsgraph_->graph(),
descriptor, source_positions));
job_.reset(Pipeline::NewWasmCompilationJob(&info_, jsgraph_, descriptor,
source_positions));
ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t.
// TODO(ahaas): The counters are not thread-safe at the moment.
......
......@@ -522,6 +522,7 @@ DEFINE_BOOL(wasm_mv_prototype, false,
DEFINE_BOOL(wasm_atomics_prototype, false,
"enable prototype atomic opcodes for wasm")
DEFINE_BOOL(wasm_opt, false, "enable wasm optimization")
DEFINE_BOOL(wasm_no_bounds_checks, false,
"disable bounds checks (performance testing only)")
DEFINE_BOOL(wasm_no_stack_checks, false,
......
......@@ -564,7 +564,7 @@ class WasmFunctionCompiler : public HandleAndZoneScope,
CompilationInfo info(debug_name_, this->isolate(), this->zone(),
Code::ComputeFlags(Code::WASM_FUNCTION));
std::unique_ptr<CompilationJob> job(Pipeline::NewWasmCompilationJob(
&info, graph(), desc, &source_position_table_));
&info, &jsgraph, desc, &source_position_table_));
if (job->ExecuteJob() != CompilationJob::SUCCEEDED ||
job->FinalizeJob() != CompilationJob::SUCCEEDED)
return Handle<Code>::null();
......
......@@ -1183,12 +1183,16 @@ TEST_F(MachineOperatorReducerTest, Int32ModWithConstant) {
ASSERT_TRUE(r.Changed());
EXPECT_THAT(
r.replacement(),
IsSelect(MachineRepresentation::kWord32,
IsInt32LessThan(p0, IsInt32Constant(0)),
IsInt32Sub(IsInt32Constant(0),
IsWord32And(IsInt32Sub(IsInt32Constant(0), p0),
IsInt32Constant(mask))),
IsWord32And(p0, IsInt32Constant(mask))));
IsPhi(
MachineRepresentation::kWord32,
IsInt32Sub(IsInt32Constant(0),
IsWord32And(IsInt32Sub(IsInt32Constant(0), p0),
IsInt32Constant(mask))),
IsWord32And(p0, IsInt32Constant(mask)),
IsMerge(IsIfTrue(IsBranch(IsInt32LessThan(p0, IsInt32Constant(0)),
graph()->start())),
IsIfFalse(IsBranch(IsInt32LessThan(p0, IsInt32Constant(0)),
graph()->start())))));
}
TRACED_FORRANGE(int32_t, shift, 1, 31) {
Reduction const r = Reduce(graph()->NewNode(
......@@ -1199,12 +1203,16 @@ TEST_F(MachineOperatorReducerTest, Int32ModWithConstant) {
ASSERT_TRUE(r.Changed());
EXPECT_THAT(
r.replacement(),
IsSelect(MachineRepresentation::kWord32,
IsInt32LessThan(p0, IsInt32Constant(0)),
IsInt32Sub(IsInt32Constant(0),
IsWord32And(IsInt32Sub(IsInt32Constant(0), p0),
IsInt32Constant(mask))),
IsWord32And(p0, IsInt32Constant(mask))));
IsPhi(
MachineRepresentation::kWord32,
IsInt32Sub(IsInt32Constant(0),
IsWord32And(IsInt32Sub(IsInt32Constant(0), p0),
IsInt32Constant(mask))),
IsWord32And(p0, IsInt32Constant(mask)),
IsMerge(IsIfTrue(IsBranch(IsInt32LessThan(p0, IsInt32Constant(0)),
graph()->start())),
IsIfFalse(IsBranch(IsInt32LessThan(p0, IsInt32Constant(0)),
graph()->start())))));
}
TRACED_FOREACH(int32_t, divisor, kInt32Values) {
if (divisor == 0 || base::bits::IsPowerOfTwo32(Abs(divisor))) continue;
......
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