|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +#include "vm/compiler/backend/il_printer.h" |
| 6 | +#include "vm/compiler/backend/il_test_helper.h" |
| 7 | +#include "vm/compiler/compiler_pass.h" |
| 8 | +#include "vm/object.h" |
| 9 | +#include "vm/unit_test.h" |
| 10 | + |
| 11 | +namespace dart { |
| 12 | + |
| 13 | +static int CountCheckBounds(FlowGraph* flow_graph) { |
| 14 | + int checks = 0; |
| 15 | + for (BlockIterator block_it = flow_graph->reverse_postorder_iterator(); |
| 16 | + !block_it.Done(); block_it.Advance()) { |
| 17 | + for (ForwardInstructionIterator it(block_it.Current()); !it.Done(); |
| 18 | + it.Advance()) { |
| 19 | + if (it.Current()->IsCheckBoundBase()) { |
| 20 | + checks++; |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + return checks; |
| 25 | +} |
| 26 | + |
| 27 | +ISOLATE_UNIT_TEST_CASE(BoundsCheckElimination_Pragma) { |
| 28 | + const char* kScript = R"( |
| 29 | + import 'dart:typed_data'; |
| 30 | +
|
| 31 | + @pragma('vm:unsafe:no-bounds-checks') |
| 32 | + @pragma('vm:prefer-inline') |
| 33 | + int foo(Uint8List list) { |
| 34 | + int result = 0; |
| 35 | + for (int i = 0; i < 10; i++) { |
| 36 | + result = list[i]; |
| 37 | + } |
| 38 | + return result; |
| 39 | + } |
| 40 | +
|
| 41 | + int test(Uint8List list) { |
| 42 | + return foo(list); |
| 43 | + } |
| 44 | + )"; |
| 45 | + |
| 46 | + const auto& root_library = Library::Handle(LoadTestScript(kScript)); |
| 47 | + const auto& function = Function::Handle(GetFunction(root_library, "test")); |
| 48 | + |
| 49 | + TestPipeline pipeline(function, CompilerPass::kAOT); |
| 50 | + auto flow_graph = pipeline.RunPasses({}); |
| 51 | + EXPECT_EQ(0, CountCheckBounds(flow_graph)); |
| 52 | +} |
| 53 | + |
| 54 | +ISOLATE_UNIT_TEST_CASE(BoundsCheckElimination_Pragma_learning) { |
| 55 | + // Test that BCE takes into account (i.e. 'learns from') checks that are |
| 56 | + // annotated to be removed. |
| 57 | + const char* kScript = R"( |
| 58 | + import 'dart:typed_data'; |
| 59 | +
|
| 60 | + @pragma('vm:unsafe:no-bounds-checks') |
| 61 | + @pragma('vm:prefer-inline') |
| 62 | + int load(Uint8List list, int index) => list[index]; |
| 63 | +
|
| 64 | + int test(Uint8List list) { |
| 65 | + int value1 = load(list, 10); |
| 66 | + int value2 = list[5]; |
| 67 | + return value1 + value2; |
| 68 | + } |
| 69 | + )"; |
| 70 | + |
| 71 | + const auto& root_library = Library::Handle(LoadTestScript(kScript)); |
| 72 | + const auto& function = Function::Handle(GetFunction(root_library, "test")); |
| 73 | + |
| 74 | + TestPipeline pipeline(function, CompilerPass::kAOT); |
| 75 | + auto flow_graph = pipeline.RunPasses({}); |
| 76 | + |
| 77 | + // No checks because unsafe (trusted) check `list[10]` dominates `list[5]`. |
| 78 | + EXPECT_EQ(0, CountCheckBounds(flow_graph)); |
| 79 | +} |
| 80 | + |
| 81 | +ISOLATE_UNIT_TEST_CASE(BoundsCheckElimination_Pragma_learning_control) { |
| 82 | + // Sister test to BoundsCheckElimination_Pragma_learning that shows without |
| 83 | + // the annotation, there is a check that corresponds to the check removed via |
| 84 | + // the annotation. |
| 85 | + const char* kScript = R"( |
| 86 | + import 'dart:typed_data'; |
| 87 | +
|
| 88 | + @pragma('vm:prefer-inline') |
| 89 | + int load(Uint8List list, int index) => list[index]; |
| 90 | +
|
| 91 | + int test(Uint8List list) { |
| 92 | + int value1 = load(list, 10); |
| 93 | + int value2 = list[5]; |
| 94 | + return value1 + value2; |
| 95 | + } |
| 96 | + )"; |
| 97 | + |
| 98 | + const auto& root_library = Library::Handle(LoadTestScript(kScript)); |
| 99 | + const auto& function = Function::Handle(GetFunction(root_library, "test")); |
| 100 | + |
| 101 | + TestPipeline pipeline(function, CompilerPass::kAOT); |
| 102 | + auto flow_graph = pipeline.RunPasses({}); |
| 103 | + |
| 104 | + // Single check because `list[10]` dominates `list[5]`. |
| 105 | + EXPECT_EQ(1, CountCheckBounds(flow_graph)); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace dart |
0 commit comments