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