Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "base/arena_allocator.h" |
| 18 | #include "builder.h" |
| 19 | #include "codegen_test_utils.h" |
| 20 | #include "common_compiler_test.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimizing_unit_test.h" |
| 23 | #include "pc_relative_fixups_x86.h" |
| 24 | #include "register_allocator.h" |
| 25 | #include "scheduler.h" |
| 26 | |
| 27 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 28 | #include "scheduler_arm64.h" |
| 29 | #endif |
| 30 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 31 | #ifdef ART_ENABLE_CODEGEN_arm |
| 32 | #include "scheduler_arm.h" |
| 33 | #endif |
| 34 | |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 35 | namespace art { |
| 36 | |
| 37 | // Return all combinations of ISA and code generator that are executable on |
| 38 | // hardware, or on simulator, and that we'd like to test. |
| 39 | static ::std::vector<CodegenTargetConfig> GetTargetConfigs() { |
| 40 | ::std::vector<CodegenTargetConfig> v; |
| 41 | ::std::vector<CodegenTargetConfig> test_config_candidates = { |
| 42 | #ifdef ART_ENABLE_CODEGEN_arm |
| 43 | CodegenTargetConfig(kArm, create_codegen_arm), |
| 44 | CodegenTargetConfig(kThumb2, create_codegen_arm), |
| 45 | #endif |
| 46 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 47 | CodegenTargetConfig(kArm64, create_codegen_arm64), |
| 48 | #endif |
| 49 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 50 | CodegenTargetConfig(kX86, create_codegen_x86), |
| 51 | #endif |
| 52 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
| 53 | CodegenTargetConfig(kX86_64, create_codegen_x86_64), |
| 54 | #endif |
| 55 | #ifdef ART_ENABLE_CODEGEN_mips |
| 56 | CodegenTargetConfig(kMips, create_codegen_mips), |
| 57 | #endif |
| 58 | #ifdef ART_ENABLE_CODEGEN_mips64 |
| 59 | CodegenTargetConfig(kMips64, create_codegen_mips64) |
| 60 | #endif |
| 61 | }; |
| 62 | |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 63 | for (const CodegenTargetConfig& test_config : test_config_candidates) { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 64 | if (CanExecute(test_config.GetInstructionSet())) { |
| 65 | v.push_back(test_config); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return v; |
| 70 | } |
| 71 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 72 | class SchedulerTest : public CommonCompilerTest { |
| 73 | public: |
| 74 | SchedulerTest() : pool_(), allocator_(&pool_) { |
| 75 | graph_ = CreateGraph(&allocator_); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 76 | } |
| 77 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 78 | // Build scheduling graph, and run target specific scheduling on it. |
| 79 | void TestBuildDependencyGraphAndSchedule(HScheduler* scheduler) { |
| 80 | HBasicBlock* entry = new (&allocator_) HBasicBlock(graph_); |
| 81 | HBasicBlock* block1 = new (&allocator_) HBasicBlock(graph_); |
| 82 | graph_->AddBlock(entry); |
| 83 | graph_->AddBlock(block1); |
| 84 | graph_->SetEntryBlock(entry); |
| 85 | |
| 86 | // entry: |
| 87 | // array ParameterValue |
| 88 | // c1 IntConstant |
| 89 | // c2 IntConstant |
| 90 | // block1: |
| 91 | // add1 Add [c1, c2] |
| 92 | // add2 Add [add1, c2] |
| 93 | // mul Mul [add1, add2] |
| 94 | // div_check DivZeroCheck [add2] (env: add2, mul) |
| 95 | // div Div [add1, div_check] |
| 96 | // array_get1 ArrayGet [array, add1] |
| 97 | // array_set1 ArraySet [array, add1, add2] |
| 98 | // array_get2 ArrayGet [array, add1] |
| 99 | // array_set2 ArraySet [array, add1, add2] |
| 100 | |
| 101 | HInstruction* array = new (&allocator_) HParameterValue(graph_->GetDexFile(), |
| 102 | dex::TypeIndex(0), |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 103 | 0, |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 104 | Primitive::kPrimNot); |
| 105 | HInstruction* c1 = graph_->GetIntConstant(1); |
| 106 | HInstruction* c2 = graph_->GetIntConstant(10); |
| 107 | HInstruction* add1 = new (&allocator_) HAdd(Primitive::kPrimInt, c1, c2); |
| 108 | HInstruction* add2 = new (&allocator_) HAdd(Primitive::kPrimInt, add1, c2); |
| 109 | HInstruction* mul = new (&allocator_) HMul(Primitive::kPrimInt, add1, add2); |
| 110 | HInstruction* div_check = new (&allocator_) HDivZeroCheck(add2, 0); |
| 111 | HInstruction* div = new (&allocator_) HDiv(Primitive::kPrimInt, add1, div_check, 0); |
| 112 | HInstruction* array_get1 = new (&allocator_) HArrayGet(array, add1, Primitive::kPrimInt, 0); |
| 113 | HInstruction* array_set1 = new (&allocator_) HArraySet(array, add1, add2, Primitive::kPrimInt, 0); |
| 114 | HInstruction* array_get2 = new (&allocator_) HArrayGet(array, add1, Primitive::kPrimInt, 0); |
| 115 | HInstruction* array_set2 = new (&allocator_) HArraySet(array, add1, add2, Primitive::kPrimInt, 0); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 116 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 117 | DCHECK(div_check->CanThrow()); |
| 118 | |
| 119 | entry->AddInstruction(array); |
| 120 | |
| 121 | HInstruction* block_instructions[] = {add1, |
| 122 | add2, |
| 123 | mul, |
| 124 | div_check, |
| 125 | div, |
| 126 | array_get1, |
| 127 | array_set1, |
| 128 | array_get2, |
| 129 | array_set2}; |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 130 | for (HInstruction* instr : block_instructions) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 131 | block1->AddInstruction(instr); |
| 132 | } |
| 133 | |
| 134 | HEnvironment* environment = new (&allocator_) HEnvironment(&allocator_, |
| 135 | 2, |
| 136 | graph_->GetArtMethod(), |
| 137 | 0, |
| 138 | div_check); |
| 139 | div_check->SetRawEnvironment(environment); |
| 140 | environment->SetRawEnvAt(0, add2); |
| 141 | add2->AddEnvUseAt(div_check->GetEnvironment(), 0); |
| 142 | environment->SetRawEnvAt(1, mul); |
| 143 | mul->AddEnvUseAt(div_check->GetEnvironment(), 1); |
| 144 | |
| 145 | SchedulingGraph scheduling_graph(scheduler, graph_->GetArena()); |
| 146 | // Instructions must be inserted in reverse order into the scheduling graph. |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 147 | for (HInstruction* instr : ReverseRange(block_instructions)) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 148 | scheduling_graph.AddNode(instr); |
| 149 | } |
| 150 | |
| 151 | // Should not have dependencies cross basic blocks. |
| 152 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, c1)); |
| 153 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add2, c2)); |
| 154 | |
| 155 | // Define-use dependency. |
| 156 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(add2, add1)); |
| 157 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, add2)); |
| 158 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div_check, add2)); |
| 159 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(div_check, add1)); |
| 160 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div, div_check)); |
| 161 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add1)); |
| 162 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add2)); |
| 163 | |
| 164 | // Read and write dependencies |
| 165 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, array_get1)); |
| 166 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_get2)); |
| 167 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_get2, array_set1)); |
| 168 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_set1)); |
| 169 | |
| 170 | // Env dependency. |
| 171 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(div_check, mul)); |
| 172 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(mul, div_check)); |
| 173 | |
| 174 | // CanThrow. |
| 175 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, div_check)); |
| 176 | |
| 177 | // Exercise the code path of target specific scheduler and SchedulingLatencyVisitor. |
| 178 | scheduler->Schedule(graph_); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 179 | } |
| 180 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 181 | void CompileWithRandomSchedulerAndRun(const uint16_t* data, bool has_result, int expected) { |
| 182 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
| 183 | HGraph* graph = CreateCFG(&allocator_, data); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 184 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 185 | // Schedule the graph randomly. |
| 186 | HInstructionScheduling scheduling(graph, target_config.GetInstructionSet()); |
| 187 | scheduling.Run(/*only_optimize_loop_blocks*/ false, /*schedule_randomly*/ true); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 188 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 189 | RunCode(target_config, |
| 190 | graph, |
| 191 | [](HGraph* graph_arg) { RemoveSuspendChecks(graph_arg); }, |
| 192 | has_result, expected); |
| 193 | } |
| 194 | } |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 195 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 196 | ArenaPool pool_; |
| 197 | ArenaAllocator allocator_; |
| 198 | HGraph* graph_; |
| 199 | }; |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 200 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 201 | #if defined(ART_ENABLE_CODEGEN_arm64) |
| 202 | TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM64) { |
| 203 | CriticalPathSchedulingNodeSelector critical_path_selector; |
| 204 | arm64::HSchedulerARM64 scheduler(&allocator_, &critical_path_selector); |
| 205 | TestBuildDependencyGraphAndSchedule(&scheduler); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 206 | } |
| 207 | #endif |
| 208 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 209 | #if defined(ART_ENABLE_CODEGEN_arm) |
| 210 | TEST_F(SchedulerTest, DependencyGrapAndSchedulerARM) { |
| 211 | CriticalPathSchedulingNodeSelector critical_path_selector; |
| 212 | arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr); |
| 213 | arm::HSchedulerARM scheduler(&allocator_, &critical_path_selector, &arm_latency_visitor); |
| 214 | TestBuildDependencyGraphAndSchedule(&scheduler); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 215 | } |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 216 | #endif |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 217 | |
| 218 | TEST_F(SchedulerTest, RandomScheduling) { |
| 219 | // |
| 220 | // Java source: crafted code to make sure (random) scheduling should get correct result. |
| 221 | // |
| 222 | // int result = 0; |
| 223 | // float fr = 10.0f; |
| 224 | // for (int i = 1; i < 10; i++) { |
| 225 | // fr ++; |
| 226 | // int t1 = result >> i; |
| 227 | // int t2 = result * i; |
| 228 | // result = result + t1 - t2; |
| 229 | // fr = fr / i; |
| 230 | // result += (int)fr; |
| 231 | // } |
| 232 | // return result; |
| 233 | // |
| 234 | const uint16_t data[] = SIX_REGISTERS_CODE_ITEM( |
| 235 | Instruction::CONST_4 | 0 << 12 | 2 << 8, // const/4 v2, #int 0 |
| 236 | Instruction::CONST_HIGH16 | 0 << 8, 0x4120, // const/high16 v0, #float 10.0 // #41200000 |
| 237 | Instruction::CONST_4 | 1 << 12 | 1 << 8, // const/4 v1, #int 1 |
| 238 | Instruction::CONST_16 | 5 << 8, 0x000a, // const/16 v5, #int 10 |
| 239 | Instruction::IF_GE | 5 << 12 | 1 << 8, 0x0014, // if-ge v1, v5, 001a // +0014 |
| 240 | Instruction::CONST_HIGH16 | 5 << 8, 0x3f80, // const/high16 v5, #float 1.0 // #3f800000 |
| 241 | Instruction::ADD_FLOAT_2ADDR | 5 << 12 | 0 << 8, // add-float/2addr v0, v5 |
| 242 | Instruction::SHR_INT | 3 << 8, 1 << 8 | 2 , // shr-int v3, v2, v1 |
| 243 | Instruction::MUL_INT | 4 << 8, 1 << 8 | 2, // mul-int v4, v2, v1 |
| 244 | Instruction::ADD_INT | 5 << 8, 3 << 8 | 2, // add-int v5, v2, v3 |
| 245 | Instruction::SUB_INT | 2 << 8, 4 << 8 | 5, // sub-int v2, v5, v4 |
| 246 | Instruction::INT_TO_FLOAT | 1 << 12 | 5 << 8, // int-to-float v5, v1 |
| 247 | Instruction::DIV_FLOAT_2ADDR | 5 << 12 | 0 << 8, // div-float/2addr v0, v5 |
| 248 | Instruction::FLOAT_TO_INT | 0 << 12 | 5 << 8, // float-to-int v5, v0 |
| 249 | Instruction::ADD_INT_2ADDR | 5 << 12 | 2 << 8, // add-int/2addr v2, v5 |
| 250 | Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8 | 1, // add-int/lit8 v1, v1, #int 1 // #01 |
| 251 | Instruction::GOTO | 0xeb << 8, // goto 0004 // -0015 |
| 252 | Instruction::RETURN | 2 << 8); // return v2 |
| 253 | |
| 254 | constexpr int kNumberOfRuns = 10; |
| 255 | for (int i = 0; i < kNumberOfRuns; ++i) { |
| 256 | CompileWithRandomSchedulerAndRun(data, true, 138774); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } // namespace art |