Skip to content

Commit 20407e2

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/compiler] Follow redefinitions in Value::NeedsWriteBarrier()
This is needed in order to account for redefinitions with less precise type, such as AssertAssignable of Smi to a generic T. x64, bytecode compiler: CryptoDecrypt(RunTime): 8516.8 -> 8190.6 us Change-Id: I8c5b8eb3efcc7ae1ccf204f5081772081be29c2f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112755 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Martin Kustermann <[email protected]>
1 parent 2f02836 commit 20407e2

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

runtime/vm/compiler/backend/il.cc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,17 +1271,28 @@ void FlowGraphVisitor::VisitBlocks() {
12711271
}
12721272

12731273
bool Value::NeedsWriteBarrier() {
1274-
if (Type()->IsNull() || (Type()->ToNullableCid() == kSmiCid) ||
1275-
(Type()->ToNullableCid() == kBoolCid)) {
1276-
return false;
1277-
}
1274+
Value* value = this;
1275+
do {
1276+
if (value->Type()->IsNull() ||
1277+
(value->Type()->ToNullableCid() == kSmiCid) ||
1278+
(value->Type()->ToNullableCid() == kBoolCid)) {
1279+
return false;
1280+
}
12781281

1279-
// Strictly speaking, the incremental barrier can only be skipped for
1280-
// immediate objects (Smis) or permanent objects (vm-isolate heap or
1281-
// image pages). Here we choose to skip the barrier for any constant on
1282-
// the assumption it will remain reachable through the object pool.
1282+
// Strictly speaking, the incremental barrier can only be skipped for
1283+
// immediate objects (Smis) or permanent objects (vm-isolate heap or
1284+
// image pages). Here we choose to skip the barrier for any constant on
1285+
// the assumption it will remain reachable through the object pool.
1286+
if (value->BindsToConstant()) {
1287+
return false;
1288+
}
12831289

1284-
return !BindsToConstant();
1290+
// Follow the chain of redefinitions as redefined value could have a more
1291+
// accurate type (for example, AssertAssignable of Smi to a generic T).
1292+
value = value->definition()->RedefinedValue();
1293+
} while (value != nullptr);
1294+
1295+
return true;
12851296
}
12861297

12871298
void JoinEntryInstr::AddPredecessor(BlockEntryInstr* predecessor) {

runtime/vm/compiler/backend/il_test.cc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
#include "vm/compiler/backend/il.h"
6+
#include "vm/compiler/backend/il_test_helper.h"
67
#include "vm/unit_test.h"
78

89
namespace dart {
@@ -40,4 +41,48 @@ ISOLATE_UNIT_TEST_CASE(OptimizationTests) {
4041
EXPECT(!c3->Equals(c1));
4142
}
4243

44+
ISOLATE_UNIT_TEST_CASE(IRTest_EliminateWriteBarrier) {
45+
const char* kScript =
46+
R"(
47+
class Container<T> {
48+
operator []=(var index, var value) {
49+
return data[index] = value;
50+
}
51+
52+
List<T> data = new List<T>()..length = 10;
53+
}
54+
55+
Container<int> x = new Container<int>();
56+
57+
foo() {
58+
for (int i = 0; i < 10; ++i) {
59+
x[i] = i;
60+
}
61+
}
62+
)";
63+
64+
const auto& root_library = Library::Handle(LoadTestScript(kScript));
65+
const auto& function = Function::Handle(GetFunction(root_library, "foo"));
66+
67+
Invoke(root_library, "foo");
68+
69+
TestPipeline pipeline(function, CompilerPass::kJIT);
70+
FlowGraph* flow_graph = pipeline.RunPasses({});
71+
72+
auto entry = flow_graph->graph_entry()->normal_entry();
73+
EXPECT(entry != nullptr);
74+
75+
StoreIndexedInstr* store_indexed = nullptr;
76+
77+
ILMatcher cursor(flow_graph, entry, true);
78+
RELEASE_ASSERT(cursor.TryMatch({
79+
kMoveGlob,
80+
kMatchAndMoveBranchTrue,
81+
kMoveGlob,
82+
{kMatchStoreIndexed, &store_indexed},
83+
}));
84+
85+
EXPECT(!store_indexed->value()->NeedsWriteBarrier());
86+
}
87+
4388
} // namespace dart

0 commit comments

Comments
 (0)