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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 17 | #include "scheduler.h" |
| 18 | |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 19 | #include "base/arena_allocator.h" |
| 20 | #include "builder.h" |
| 21 | #include "codegen_test_utils.h" |
| 22 | #include "common_compiler_test.h" |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 23 | #include "load_store_analysis.h" |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 24 | #include "nodes.h" |
| 25 | #include "optimizing_unit_test.h" |
| 26 | #include "pc_relative_fixups_x86.h" |
| 27 | #include "register_allocator.h" |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 28 | |
| 29 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 30 | #include "scheduler_arm64.h" |
| 31 | #endif |
| 32 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 33 | #ifdef ART_ENABLE_CODEGEN_arm |
| 34 | #include "scheduler_arm.h" |
| 35 | #endif |
| 36 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 37 | namespace art { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 38 | |
| 39 | // Return all combinations of ISA and code generator that are executable on |
| 40 | // hardware, or on simulator, and that we'd like to test. |
| 41 | static ::std::vector<CodegenTargetConfig> GetTargetConfigs() { |
| 42 | ::std::vector<CodegenTargetConfig> v; |
| 43 | ::std::vector<CodegenTargetConfig> test_config_candidates = { |
| 44 | #ifdef ART_ENABLE_CODEGEN_arm |
Roland Levillain | 9983e30 | 2017-07-14 14:34:22 +0100 | [diff] [blame] | 45 | // TODO: Should't this be `kThumb2` instead of `kArm` here? |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 46 | CodegenTargetConfig(InstructionSet::kArm, create_codegen_arm_vixl32), |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 47 | #endif |
| 48 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 49 | CodegenTargetConfig(InstructionSet::kArm64, create_codegen_arm64), |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 50 | #endif |
| 51 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 52 | CodegenTargetConfig(InstructionSet::kX86, create_codegen_x86), |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 53 | #endif |
| 54 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 55 | CodegenTargetConfig(InstructionSet::kX86_64, create_codegen_x86_64), |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 56 | #endif |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 57 | }; |
| 58 | |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 59 | for (const CodegenTargetConfig& test_config : test_config_candidates) { |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 60 | if (CanExecute(test_config.GetInstructionSet())) { |
| 61 | v.push_back(test_config); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return v; |
| 66 | } |
| 67 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 68 | class SchedulerTest : public OptimizingUnitTest { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 69 | public: |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 70 | SchedulerTest() : graph_(CreateGraph()) { } |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 71 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 72 | // Build scheduling graph, and run target specific scheduling on it. |
| 73 | void TestBuildDependencyGraphAndSchedule(HScheduler* scheduler) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 74 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
| 75 | HBasicBlock* block1 = new (GetAllocator()) HBasicBlock(graph_); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 76 | graph_->AddBlock(entry); |
| 77 | graph_->AddBlock(block1); |
| 78 | graph_->SetEntryBlock(entry); |
| 79 | |
| 80 | // entry: |
| 81 | // array ParameterValue |
| 82 | // c1 IntConstant |
| 83 | // c2 IntConstant |
| 84 | // block1: |
| 85 | // add1 Add [c1, c2] |
| 86 | // add2 Add [add1, c2] |
| 87 | // mul Mul [add1, add2] |
| 88 | // div_check DivZeroCheck [add2] (env: add2, mul) |
| 89 | // div Div [add1, div_check] |
| 90 | // array_get1 ArrayGet [array, add1] |
| 91 | // array_set1 ArraySet [array, add1, add2] |
| 92 | // array_get2 ArrayGet [array, add1] |
| 93 | // array_set2 ArraySet [array, add1, add2] |
| 94 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 95 | HInstruction* array = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 96 | dex::TypeIndex(0), |
| 97 | 0, |
| 98 | DataType::Type::kReference); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 99 | HInstruction* c1 = graph_->GetIntConstant(1); |
| 100 | HInstruction* c2 = graph_->GetIntConstant(10); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 101 | HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, c1, c2); |
| 102 | HInstruction* add2 = new (GetAllocator()) HAdd(DataType::Type::kInt32, add1, c2); |
| 103 | HInstruction* mul = new (GetAllocator()) HMul(DataType::Type::kInt32, add1, add2); |
| 104 | HInstruction* div_check = new (GetAllocator()) HDivZeroCheck(add2, 0); |
| 105 | HInstruction* div = new (GetAllocator()) HDiv(DataType::Type::kInt32, add1, div_check, 0); |
| 106 | HInstruction* array_get1 = |
| 107 | new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 108 | HInstruction* array_set1 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 109 | new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0); |
| 110 | HInstruction* array_get2 = |
| 111 | new (GetAllocator()) HArrayGet(array, add1, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 112 | HInstruction* array_set2 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 113 | new (GetAllocator()) HArraySet(array, add1, add2, DataType::Type::kInt32, 0); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 114 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 115 | DCHECK(div_check->CanThrow()); |
| 116 | |
| 117 | entry->AddInstruction(array); |
| 118 | |
| 119 | HInstruction* block_instructions[] = {add1, |
| 120 | add2, |
| 121 | mul, |
| 122 | div_check, |
| 123 | div, |
| 124 | array_get1, |
| 125 | array_set1, |
| 126 | array_get2, |
| 127 | array_set2}; |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 128 | for (HInstruction* instr : block_instructions) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 129 | block1->AddInstruction(instr); |
| 130 | } |
| 131 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 132 | HEnvironment* environment = new (GetAllocator()) HEnvironment(GetAllocator(), |
| 133 | 2, |
| 134 | graph_->GetArtMethod(), |
| 135 | 0, |
| 136 | div_check); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 137 | div_check->SetRawEnvironment(environment); |
| 138 | environment->SetRawEnvAt(0, add2); |
| 139 | add2->AddEnvUseAt(div_check->GetEnvironment(), 0); |
| 140 | environment->SetRawEnvAt(1, mul); |
| 141 | mul->AddEnvUseAt(div_check->GetEnvironment(), 1); |
| 142 | |
Evgeny Astigeevich | 957c538 | 2019-03-18 12:37:58 +0000 | [diff] [blame] | 143 | TestSchedulingGraph scheduling_graph(GetScopedAllocator()); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 144 | // Instructions must be inserted in reverse order into the scheduling graph. |
Vladimir Marko | 7d157fc | 2017-05-10 16:29:23 +0100 | [diff] [blame] | 145 | for (HInstruction* instr : ReverseRange(block_instructions)) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 146 | scheduling_graph.AddNode(instr); |
| 147 | } |
| 148 | |
| 149 | // Should not have dependencies cross basic blocks. |
| 150 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, c1)); |
| 151 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add2, c2)); |
| 152 | |
| 153 | // Define-use dependency. |
| 154 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(add2, add1)); |
| 155 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(add1, add2)); |
| 156 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div_check, add2)); |
| 157 | ASSERT_FALSE(scheduling_graph.HasImmediateDataDependency(div_check, add1)); |
| 158 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(div, div_check)); |
| 159 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add1)); |
| 160 | ASSERT_TRUE(scheduling_graph.HasImmediateDataDependency(array_set1, add2)); |
| 161 | |
| 162 | // Read and write dependencies |
| 163 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set1, array_get1)); |
| 164 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_get2)); |
| 165 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(array_get2, array_set1)); |
Vladimir Marko | 09d041b | 2018-07-30 12:51:59 +0100 | [diff] [blame] | 166 | // Unnecessary dependency is not stored, we rely on transitive dependencies. |
| 167 | // The array_set2 -> array_get2 -> array_set1 dependencies are tested above. |
| 168 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(array_set2, array_set1)); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 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 | |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 181 | void CompileWithRandomSchedulerAndRun(const std::vector<uint16_t>& data, |
| 182 | bool has_result, |
| 183 | int expected) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 184 | for (CodegenTargetConfig target_config : GetTargetConfigs()) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 185 | HGraph* graph = CreateCFG(data); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 186 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 187 | // Schedule the graph randomly. |
| 188 | HInstructionScheduling scheduling(graph, target_config.GetInstructionSet()); |
| 189 | scheduling.Run(/*only_optimize_loop_blocks*/ false, /*schedule_randomly*/ true); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 190 | |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 191 | std::unique_ptr<CompilerOptions> compiler_options = |
| 192 | CommonCompilerTest::CreateCompilerOptions(target_config.GetInstructionSet(), "default"); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 193 | RunCode(target_config, |
Vladimir Marko | f91fc12 | 2020-05-13 09:21:00 +0100 | [diff] [blame] | 194 | *compiler_options, |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 195 | graph, |
| 196 | [](HGraph* graph_arg) { RemoveSuspendChecks(graph_arg); }, |
| 197 | has_result, expected); |
| 198 | } |
| 199 | } |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 200 | |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 201 | void TestDependencyGraphOnAliasingArrayAccesses(HScheduler* scheduler) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 202 | HBasicBlock* entry = new (GetAllocator()) HBasicBlock(graph_); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 203 | graph_->AddBlock(entry); |
| 204 | graph_->SetEntryBlock(entry); |
| 205 | graph_->BuildDominatorTree(); |
| 206 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 207 | HInstruction* arr = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 208 | dex::TypeIndex(0), |
| 209 | 0, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 210 | DataType::Type::kReference); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 211 | HInstruction* i = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 212 | dex::TypeIndex(1), |
| 213 | 1, |
| 214 | DataType::Type::kInt32); |
| 215 | HInstruction* j = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 216 | dex::TypeIndex(1), |
| 217 | 1, |
| 218 | DataType::Type::kInt32); |
| 219 | HInstruction* object = new (GetAllocator()) HParameterValue(graph_->GetDexFile(), |
| 220 | dex::TypeIndex(0), |
| 221 | 0, |
| 222 | DataType::Type::kReference); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 223 | HInstruction* c0 = graph_->GetIntConstant(0); |
| 224 | HInstruction* c1 = graph_->GetIntConstant(1); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 225 | HInstruction* add0 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c0); |
| 226 | HInstruction* add1 = new (GetAllocator()) HAdd(DataType::Type::kInt32, i, c1); |
| 227 | HInstruction* sub0 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c0); |
| 228 | HInstruction* sub1 = new (GetAllocator()) HSub(DataType::Type::kInt32, i, c1); |
| 229 | HInstruction* arr_set_0 = |
| 230 | new (GetAllocator()) HArraySet(arr, c0, c0, DataType::Type::kInt32, 0); |
| 231 | HInstruction* arr_set_1 = |
| 232 | new (GetAllocator()) HArraySet(arr, c1, c0, DataType::Type::kInt32, 0); |
| 233 | HInstruction* arr_set_i = new (GetAllocator()) HArraySet(arr, i, c0, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 234 | HInstruction* arr_set_add0 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 235 | new (GetAllocator()) HArraySet(arr, add0, c0, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 236 | HInstruction* arr_set_add1 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 237 | new (GetAllocator()) HArraySet(arr, add1, c0, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 238 | HInstruction* arr_set_sub0 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 239 | new (GetAllocator()) HArraySet(arr, sub0, c0, DataType::Type::kInt32, 0); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 240 | HInstruction* arr_set_sub1 = |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 241 | new (GetAllocator()) HArraySet(arr, sub1, c0, DataType::Type::kInt32, 0); |
| 242 | HInstruction* arr_set_j = new (GetAllocator()) HArraySet(arr, j, c0, DataType::Type::kInt32, 0); |
| 243 | HInstanceFieldSet* set_field10 = new (GetAllocator()) HInstanceFieldSet(object, |
| 244 | c1, |
| 245 | nullptr, |
| 246 | DataType::Type::kInt32, |
| 247 | MemberOffset(10), |
| 248 | false, |
| 249 | kUnknownFieldIndex, |
| 250 | kUnknownClassDefIndex, |
| 251 | graph_->GetDexFile(), |
| 252 | 0); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 253 | |
| 254 | HInstruction* block_instructions[] = {arr, |
| 255 | i, |
| 256 | j, |
| 257 | object, |
| 258 | add0, |
| 259 | add1, |
| 260 | sub0, |
| 261 | sub1, |
| 262 | arr_set_0, |
| 263 | arr_set_1, |
| 264 | arr_set_i, |
| 265 | arr_set_add0, |
| 266 | arr_set_add1, |
| 267 | arr_set_sub0, |
| 268 | arr_set_sub1, |
| 269 | arr_set_j, |
| 270 | set_field10}; |
| 271 | |
| 272 | for (HInstruction* instr : block_instructions) { |
| 273 | entry->AddInstruction(instr); |
| 274 | } |
| 275 | |
Alex Light | 86fe9b8 | 2020-11-16 16:54:01 +0000 | [diff] [blame] | 276 | HeapLocationCollector heap_location_collector( |
Alex Light | 3a73ffb | 2021-01-25 14:11:05 +0000 | [diff] [blame] | 277 | graph_, GetScopedAllocator(), LoadStoreAnalysisType::kBasic); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 278 | heap_location_collector.VisitBasicBlock(entry); |
| 279 | heap_location_collector.BuildAliasingMatrix(); |
Evgeny Astigeevich | 957c538 | 2019-03-18 12:37:58 +0000 | [diff] [blame] | 280 | TestSchedulingGraph scheduling_graph(GetScopedAllocator(), &heap_location_collector); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 281 | |
| 282 | for (HInstruction* instr : ReverseRange(block_instructions)) { |
| 283 | // Build scheduling graph with memory access aliasing information |
| 284 | // from LSA/heap_location_collector. |
| 285 | scheduling_graph.AddNode(instr); |
| 286 | } |
| 287 | |
| 288 | // LSA/HeapLocationCollector should see those ArraySet instructions. |
| 289 | ASSERT_EQ(heap_location_collector.GetNumberOfHeapLocations(), 9U); |
| 290 | ASSERT_TRUE(heap_location_collector.HasHeapStores()); |
| 291 | |
| 292 | // Test queries on HeapLocationCollector's aliasing matrix after load store analysis. |
| 293 | // HeapLocationCollector and SchedulingGraph should report consistent relationships. |
| 294 | size_t loc1 = HeapLocationCollector::kHeapLocationNotFound; |
| 295 | size_t loc2 = HeapLocationCollector::kHeapLocationNotFound; |
| 296 | |
| 297 | // Test side effect dependency: array[0] and array[1] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 298 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_0); |
| 299 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_1); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 300 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 301 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_1, arr_set_0)); |
| 302 | |
| 303 | // Test side effect dependency based on LSA analysis: array[i] and array[j] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 304 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i); |
| 305 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_j); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 306 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
Vladimir Marko | 09d041b | 2018-07-30 12:51:59 +0100 | [diff] [blame] | 307 | // Unnecessary dependency is not stored, we rely on transitive dependencies. |
| 308 | // The arr_set_j -> arr_set_sub0 -> arr_set_add0 -> arr_set_i dependencies are tested below. |
| 309 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i)); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 310 | |
| 311 | // Test side effect dependency based on LSA analysis: array[i] and array[i+0] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 312 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i); |
| 313 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add0); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 314 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
| 315 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_add0, arr_set_i)); |
| 316 | |
| 317 | // Test side effect dependency based on LSA analysis: array[i] and array[i-0] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 318 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i); |
| 319 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub0); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 320 | ASSERT_TRUE(heap_location_collector.MayAlias(loc1, loc2)); |
Vladimir Marko | 09d041b | 2018-07-30 12:51:59 +0100 | [diff] [blame] | 321 | // Unnecessary dependency is not stored, we rely on transitive dependencies. |
| 322 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_i)); |
| 323 | // Instead, we rely on arr_set_sub0 -> arr_set_add0 -> arr_set_i, the latter is tested above. |
| 324 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub0, arr_set_add0)); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 325 | |
| 326 | // Test side effect dependency based on LSA analysis: array[i] and array[i+1] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 327 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_i); |
| 328 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_add1); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 329 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 330 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_add1, arr_set_i)); |
| 331 | |
| 332 | // Test side effect dependency based on LSA analysis: array[i+1] and array[i-1] |
Aart Bik | b765a3f | 2018-05-10 14:47:48 -0700 | [diff] [blame] | 333 | loc1 = heap_location_collector.GetArrayHeapLocation(arr_set_add1); |
| 334 | loc2 = heap_location_collector.GetArrayHeapLocation(arr_set_sub1); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 335 | ASSERT_FALSE(heap_location_collector.MayAlias(loc1, loc2)); |
| 336 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_sub1, arr_set_add1)); |
| 337 | |
| 338 | // Test side effect dependency based on LSA analysis: array[j] and all others array accesses |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 339 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub0)); |
| 340 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add1)); |
| 341 | ASSERT_TRUE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_sub1)); |
Vladimir Marko | 09d041b | 2018-07-30 12:51:59 +0100 | [diff] [blame] | 342 | // Unnecessary dependencies are not stored, we rely on transitive dependencies. |
| 343 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_i)); |
| 344 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, arr_set_add0)); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 345 | |
| 346 | // Test that ArraySet and FieldSet should not have side effect dependency |
| 347 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_i, set_field10)); |
| 348 | ASSERT_FALSE(scheduling_graph.HasImmediateOtherDependency(arr_set_j, set_field10)); |
| 349 | |
| 350 | // Exercise target specific scheduler and SchedulingLatencyVisitor. |
| 351 | scheduler->Schedule(graph_); |
| 352 | } |
| 353 | |
Evgeny Astigeevich | 957c538 | 2019-03-18 12:37:58 +0000 | [diff] [blame] | 354 | class TestSchedulingGraph : public SchedulingGraph { |
| 355 | public: |
| 356 | explicit TestSchedulingGraph(ScopedArenaAllocator* allocator, |
| 357 | const HeapLocationCollector *heap_location_collector = nullptr) |
| 358 | : SchedulingGraph(allocator, heap_location_collector) {} |
| 359 | |
| 360 | bool HasImmediateDataDependency(const HInstruction* instruction, |
| 361 | const HInstruction* other_instruction) const { |
| 362 | const SchedulingNode* node = GetNode(instruction); |
| 363 | const SchedulingNode* other = GetNode(other_instruction); |
| 364 | if (node == nullptr || other == nullptr) { |
| 365 | // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their |
| 366 | // corresponding SchedulingNode in the graph, and tell whether there is a dependency. |
| 367 | // Otherwise there is no dependency from SchedulingGraph's perspective, for example, |
| 368 | // instruction and other_instruction are in different basic blocks. |
| 369 | return false; |
| 370 | } |
| 371 | return node->HasDataDependency(other); |
| 372 | } |
| 373 | |
| 374 | bool HasImmediateOtherDependency(const HInstruction* instruction, |
| 375 | const HInstruction* other_instruction) const { |
| 376 | const SchedulingNode* node = GetNode(instruction); |
| 377 | const SchedulingNode* other = GetNode(other_instruction); |
| 378 | if (node == nullptr || other == nullptr) { |
| 379 | // Both instructions must be in current basic block, i.e. the SchedulingGraph can see their |
| 380 | // corresponding SchedulingNode in the graph, and tell whether there is a dependency. |
| 381 | // Otherwise there is no dependency from SchedulingGraph's perspective, for example, |
| 382 | // instruction and other_instruction are in different basic blocks. |
| 383 | return false; |
| 384 | } |
| 385 | return node->HasOtherDependency(other); |
| 386 | } |
| 387 | }; |
| 388 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 389 | HGraph* graph_; |
| 390 | }; |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 391 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 392 | #if defined(ART_ENABLE_CODEGEN_arm64) |
| 393 | TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM64) { |
| 394 | CriticalPathSchedulingNodeSelector critical_path_selector; |
Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 395 | arm64::HSchedulerARM64 scheduler(&critical_path_selector); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 396 | TestBuildDependencyGraphAndSchedule(&scheduler); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 397 | } |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 398 | |
| 399 | TEST_F(SchedulerTest, ArrayAccessAliasingARM64) { |
| 400 | CriticalPathSchedulingNodeSelector critical_path_selector; |
Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 401 | arm64::HSchedulerARM64 scheduler(&critical_path_selector); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 402 | TestDependencyGraphOnAliasingArrayAccesses(&scheduler); |
| 403 | } |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 404 | #endif |
| 405 | |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 406 | #if defined(ART_ENABLE_CODEGEN_arm) |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 407 | TEST_F(SchedulerTest, DependencyGraphAndSchedulerARM) { |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 408 | CriticalPathSchedulingNodeSelector critical_path_selector; |
| 409 | arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr); |
Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 410 | arm::HSchedulerARM scheduler(&critical_path_selector, &arm_latency_visitor); |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 411 | TestBuildDependencyGraphAndSchedule(&scheduler); |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 412 | } |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 413 | |
| 414 | TEST_F(SchedulerTest, ArrayAccessAliasingARM) { |
| 415 | CriticalPathSchedulingNodeSelector critical_path_selector; |
| 416 | arm::SchedulingLatencyVisitorARM arm_latency_visitor(/*CodeGenerator*/ nullptr); |
Vladimir Marko | ced0483 | 2018-07-26 14:42:17 +0100 | [diff] [blame] | 417 | arm::HSchedulerARM scheduler(&critical_path_selector, &arm_latency_visitor); |
xueliang.zhong | 2a3471f | 2017-05-08 18:36:40 +0100 | [diff] [blame] | 418 | TestDependencyGraphOnAliasingArrayAccesses(&scheduler); |
| 419 | } |
xueliang.zhong | f7caf68 | 2017-03-01 16:07:02 +0000 | [diff] [blame] | 420 | #endif |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 421 | |
| 422 | TEST_F(SchedulerTest, RandomScheduling) { |
| 423 | // |
| 424 | // Java source: crafted code to make sure (random) scheduling should get correct result. |
| 425 | // |
| 426 | // int result = 0; |
| 427 | // float fr = 10.0f; |
| 428 | // for (int i = 1; i < 10; i++) { |
| 429 | // fr ++; |
| 430 | // int t1 = result >> i; |
| 431 | // int t2 = result * i; |
| 432 | // result = result + t1 - t2; |
| 433 | // fr = fr / i; |
| 434 | // result += (int)fr; |
| 435 | // } |
| 436 | // return result; |
| 437 | // |
Mathieu Chartier | fa3db3d | 2018-01-12 14:42:18 -0800 | [diff] [blame] | 438 | const std::vector<uint16_t> data = SIX_REGISTERS_CODE_ITEM( |
Alexandre Rames | 22aa54b | 2016-10-18 09:32:29 +0100 | [diff] [blame] | 439 | Instruction::CONST_4 | 0 << 12 | 2 << 8, // const/4 v2, #int 0 |
| 440 | Instruction::CONST_HIGH16 | 0 << 8, 0x4120, // const/high16 v0, #float 10.0 // #41200000 |
| 441 | Instruction::CONST_4 | 1 << 12 | 1 << 8, // const/4 v1, #int 1 |
| 442 | Instruction::CONST_16 | 5 << 8, 0x000a, // const/16 v5, #int 10 |
| 443 | Instruction::IF_GE | 5 << 12 | 1 << 8, 0x0014, // if-ge v1, v5, 001a // +0014 |
| 444 | Instruction::CONST_HIGH16 | 5 << 8, 0x3f80, // const/high16 v5, #float 1.0 // #3f800000 |
| 445 | Instruction::ADD_FLOAT_2ADDR | 5 << 12 | 0 << 8, // add-float/2addr v0, v5 |
| 446 | Instruction::SHR_INT | 3 << 8, 1 << 8 | 2 , // shr-int v3, v2, v1 |
| 447 | Instruction::MUL_INT | 4 << 8, 1 << 8 | 2, // mul-int v4, v2, v1 |
| 448 | Instruction::ADD_INT | 5 << 8, 3 << 8 | 2, // add-int v5, v2, v3 |
| 449 | Instruction::SUB_INT | 2 << 8, 4 << 8 | 5, // sub-int v2, v5, v4 |
| 450 | Instruction::INT_TO_FLOAT | 1 << 12 | 5 << 8, // int-to-float v5, v1 |
| 451 | Instruction::DIV_FLOAT_2ADDR | 5 << 12 | 0 << 8, // div-float/2addr v0, v5 |
| 452 | Instruction::FLOAT_TO_INT | 0 << 12 | 5 << 8, // float-to-int v5, v0 |
| 453 | Instruction::ADD_INT_2ADDR | 5 << 12 | 2 << 8, // add-int/2addr v2, v5 |
| 454 | Instruction::ADD_INT_LIT8 | 1 << 8, 1 << 8 | 1, // add-int/lit8 v1, v1, #int 1 // #01 |
| 455 | Instruction::GOTO | 0xeb << 8, // goto 0004 // -0015 |
| 456 | Instruction::RETURN | 2 << 8); // return v2 |
| 457 | |
| 458 | constexpr int kNumberOfRuns = 10; |
| 459 | for (int i = 0; i < kNumberOfRuns; ++i) { |
| 460 | CompileWithRandomSchedulerAndRun(data, true, 138774); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | } // namespace art |