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