Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
write_barrier_elimination.cc
Go to the documentation of this file.
1// Copyright (c) 2020, 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
8
9namespace dart {
10
11#if defined(DEBUG)
12DEFINE_FLAG(bool,
13 trace_write_barrier_elimination,
14 false,
15 "Trace WriteBarrierElimination pass.");
16#endif
17
19 public:
20 typedef Definition* Key;
21 typedef intptr_t Value;
22 struct Pair {
24 intptr_t index = -1;
25 Pair() {}
28 };
29
30 static Key KeyOf(Pair kv) { return kv.definition; }
31 static Value ValueOf(Pair kv) { return kv.index; }
32 static inline uword Hash(Key key) {
33 return Utils::WordHash(reinterpret_cast<intptr_t>(key));
34 }
35 static inline bool IsKeyEqual(Pair kv, Key key) {
36 return kv.definition == key;
37 }
38};
39
41
42// Inter-block write-barrier elimination.
43//
44// This optimization removes write barriers from some store instructions under
45// certain assumptions which the runtime is responsible to sustain.
46//
47// We can skip a write barrier on a StoreField to a container object X
48// if we know that either:
49// - X is in new-space, or
50// - X is in old-space, and:
51// - X is in the store buffer, and
52// - X is in the deferred marking stack (if concurrent marking is enabled)
53//
54// The result of an Allocation instruction (Instruction::IsAllocation()) will
55// satisfy one of these requirements immediately after the instruction
56// if WillAllocateNewOrRemembered() is true.
57//
58// Without runtime support, we would have to assume that any instruction which
59// can trigger a new-space scavenge (Instruction::CanTriggerGC()) might promote
60// a new-space temporary into old-space, and we could not skip a store barrier
61// on a write into it afterward.
62//
63// However, many instructions can trigger GC in unlikely cases, like
64// CheckStackOverflow and Box. To avoid interrupting write barrier elimination
65// across these instructions, the runtime ensures that any live temporaries
66// (except arrays) promoted during a scavenge caused by a non-Dart-call
67// instruction (see Instruction::CanCallDart()) will be added to the store
68// buffer. Additionally, if concurrent marking was initiated, the runtime
69// ensures that all live temporaries are also in the deferred marking stack.
70//
71// See also Thread::RememberLiveTemporaries() and
72// Thread::DeferredMarkLiveTemporaries().
74 public:
75 WriteBarrierElimination(Zone* zone, FlowGraph* flow_graph);
76
77 void Analyze();
78 void SaveResults();
79
80 private:
81 void IndexDefinitions(Zone* zone);
82
83 bool AnalyzeBlock(BlockEntryInstr* entry);
84 void MergePredecessors(BlockEntryInstr* entry);
85
86 void UpdateVectorForBlock(BlockEntryInstr* entry, bool finalize);
87
88 static intptr_t Index(BlockEntryInstr* entry) {
89 return entry->postorder_number();
90 }
91
92 intptr_t Index(Definition* def) {
93 ASSERT(IsUsable(def));
94 return definition_indices_.LookupValue(def);
95 }
96
97 bool IsUsable(Definition* def) {
98 return def->IsPhi() || (def->IsAllocation() &&
99 def->AsAllocation()->WillAllocateNewOrRemembered());
100 }
101
102#if defined(DEBUG)
103 static bool SlotEligibleForWBE(const Slot& slot);
104#endif
105
106 FlowGraph* const flow_graph_;
107 const GrowableArray<BlockEntryInstr*>* const block_order_;
108
109 // Number of usable definitions in the graph.
110 intptr_t definition_count_ = 0;
111
112 // Maps each usable definition to its index in the bitvectors.
113 DefinitionIndexMap definition_indices_;
114
115 // Bitvector with all non-Array-allocation instructions set. Used to
116 // un-mark Array allocations as usable.
117 BitVector* large_array_allocations_mask_;
118
119 // Bitvectors for each block of which allocations are new or remembered
120 // at the start (after Phis).
121 GrowableArray<BitVector*> usable_allocs_in_;
122
123 // Bitvectors for each block of which allocations are new or remembered
124 // at the end of the block.
125 GrowableArray<BitVector*> usable_allocs_out_;
126
127 // Remaining blocks to process.
129
130 // Temporary used in many functions to avoid repeated zone allocation.
131 BitVector* vector_;
132
133 // Bitvector of blocks which have been processed, to ensure each block
134 // is processed at least once.
135 BitVector* processed_blocks_;
136
137#if defined(DEBUG)
138 bool tracing_ = false;
139#else
140 static constexpr bool tracing_ = false;
141#endif
142};
143
145 FlowGraph* flow_graph)
146 : flow_graph_(flow_graph), block_order_(&flow_graph->postorder()) {
147#if defined(DEBUG)
148 if (flow_graph->should_print() && FLAG_trace_write_barrier_elimination) {
149 tracing_ = true;
150 }
151#endif
152
153 IndexDefinitions(zone);
154
155 for (intptr_t i = 0; i < block_order_->length(); ++i) {
156 usable_allocs_in_.Add(new (zone) BitVector(zone, definition_count_));
157 usable_allocs_in_[i]->CopyFrom(vector_);
158
159 usable_allocs_out_.Add(new (zone) BitVector(zone, definition_count_));
160 usable_allocs_out_[i]->CopyFrom(vector_);
161 }
162
163 processed_blocks_ = new (zone) BitVector(zone, block_order_->length());
164}
165
167 for (intptr_t i = 0; i < block_order_->length(); ++i) {
168 worklist_.Add(block_order_->At(i));
169 }
170
171 while (!worklist_.is_empty()) {
172 auto* const entry = worklist_.RemoveLast();
173 if (AnalyzeBlock(entry)) {
174 for (intptr_t i = 0; i < entry->last_instruction()->SuccessorCount();
175 ++i) {
176 if (tracing_) {
177 THR_Print("Enqueueing block %" Pd "\n", entry->block_id());
178 }
179 worklist_.Add(entry->last_instruction()->SuccessorAt(i));
180 }
181 }
182 }
183}
184
186 for (intptr_t i = 0; i < block_order_->length(); ++i) {
187 vector_->CopyFrom(usable_allocs_in_[i]);
188 UpdateVectorForBlock(block_order_->At(i), /*finalize=*/true);
189 }
190}
191
192static bool IsCreateLargeArray(Definition* defn) {
193 if (auto create_array = defn->AsCreateArray()) {
196 "Invariant restoration code does not handle card marking.");
197 // Note: IsUsable would reject CreateArray instructions with non-constant
198 // number of elements.
199 return create_array->GetConstantNumElements() >
201 }
202 return false;
203}
204
205void WriteBarrierElimination::IndexDefinitions(Zone* zone) {
206 BitmapBuilder large_array_allocations;
207
208 GrowableArray<Definition*> create_array_worklist;
209
210 for (intptr_t i = 0; i < block_order_->length(); ++i) {
211 BlockEntryInstr* const block = block_order_->At(i);
212 if (auto join_block = block->AsJoinEntry()) {
213 for (PhiIterator it(join_block); !it.Done(); it.Advance()) {
214 large_array_allocations.Set(definition_count_, false);
215 definition_indices_.Insert({it.Current(), definition_count_++});
216#if defined(DEBUG)
217 if (tracing_) {
218 THR_Print("Definition (%" Pd ") has index %" Pd ".\n",
219 it.Current()->ssa_temp_index(), definition_count_ - 1);
220 }
221#endif
222 }
223 }
224 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
225 if (Definition* current = it.Current()->AsDefinition()) {
226 if (IsUsable(current)) {
227 const bool is_create_large_array = IsCreateLargeArray(current);
228 large_array_allocations.Set(definition_count_, is_create_large_array);
229 definition_indices_.Insert({current, definition_count_++});
230 if (is_create_large_array) {
231 create_array_worklist.Add(current);
232 }
233#if defined(DEBUG)
234 if (tracing_) {
235 THR_Print("Definition (%" Pd ") has index %" Pd ".\n",
236 current->ssa_temp_index(), definition_count_ - 1);
237 }
238#endif
239 }
240 }
241 }
242 }
243
244 while (!create_array_worklist.is_empty()) {
245 auto instr = create_array_worklist.RemoveLast();
246 for (Value::Iterator it(instr->input_use_list()); !it.Done();
247 it.Advance()) {
248 if (auto phi_use = it.Current()->instruction()->AsPhi()) {
249 const intptr_t index = Index(phi_use);
250 if (!large_array_allocations.Get(index)) {
251 large_array_allocations.Set(index,
252 /*can_be_create_large_array=*/true);
253 create_array_worklist.Add(phi_use);
254 }
255 }
256 }
257 }
258
259 vector_ = new (zone) BitVector(zone, definition_count_);
260 vector_->SetAll();
261 large_array_allocations_mask_ = new (zone) BitVector(zone, definition_count_);
262 for (intptr_t i = 0; i < definition_count_; ++i) {
263 if (!large_array_allocations.Get(i)) large_array_allocations_mask_->Add(i);
264 }
265}
266
267void WriteBarrierElimination::MergePredecessors(BlockEntryInstr* entry) {
268 vector_->Clear();
269 for (intptr_t i = 0; i < entry->PredecessorCount(); ++i) {
270 BitVector* predecessor_set =
271 usable_allocs_out_[Index(entry->PredecessorAt(i))];
272 if (i == 0) {
273 vector_->AddAll(predecessor_set);
274 } else {
275 vector_->Intersect(predecessor_set);
276 }
277 }
278
279 if (JoinEntryInstr* join = entry->AsJoinEntry()) {
280 // A Phi is usable if and only if all its inputs are usable.
281 for (PhiIterator it(join); !it.Done(); it.Advance()) {
282 PhiInstr* phi = it.Current();
283 ASSERT(phi->InputCount() == entry->PredecessorCount());
284 bool is_usable = true;
285 for (intptr_t i = 0; i < phi->InputCount(); ++i) {
286 BitVector* const predecessor_set =
287 usable_allocs_out_[Index(entry->PredecessorAt(i))];
288 Definition* const origin = phi->InputAt(i)->definition();
289 if (!IsUsable(origin) || !predecessor_set->Contains(Index(origin))) {
290 is_usable = false;
291 break;
292 }
293 }
294 vector_->Set(Index(phi), is_usable);
295 }
296
297#if defined(DEBUG)
298 if (tracing_) {
299 THR_Print("Merge predecessors for %" Pd ".\n", entry->block_id());
300 for (PhiIterator it(join); !it.Done(); it.Advance()) {
301 PhiInstr* phi = it.Current();
302 THR_Print("%" Pd ": %s\n", phi->ssa_temp_index(),
303 vector_->Contains(Index(phi)) ? "true" : "false");
304 }
305 }
306#endif
307 }
308}
309
310bool WriteBarrierElimination::AnalyzeBlock(BlockEntryInstr* entry) {
311 // Recompute the usable allocs in-set.
312 MergePredecessors(entry);
313
314 // If the in-set has not changed, there's no work to do.
315 BitVector* const in_set = usable_allocs_in_[Index(entry)];
316 ASSERT(vector_->SubsetOf(*in_set)); // convergence
317 if (vector_->Equals(*in_set) && processed_blocks_->Contains(Index(entry))) {
318 if (tracing_) {
319 THR_Print("Bailout of block %" Pd ": inputs didn't change.\n",
320 entry->block_id());
321 }
322 return false;
323 } else if (tracing_) {
324 THR_Print("Inputs of block %" Pd " changed: ", entry->block_id());
325 in_set->Print();
326 THR_Print(" -> ");
327 vector_->Print();
328 THR_Print("\n");
329 }
330
331 usable_allocs_in_[Index(entry)]->CopyFrom(vector_);
332 UpdateVectorForBlock(entry, /*finalize=*/false);
333
334 processed_blocks_->Add(Index(entry));
335
336 // Successors only need to be updated if the out-set changes.
337 if (vector_->Equals(*usable_allocs_out_[Index(entry)])) {
338 if (tracing_) {
339 THR_Print("Bailout of block %" Pd ": out-set didn't change.\n",
340 entry->block_id());
341 }
342 return false;
343 }
344
345 BitVector* const out_set = usable_allocs_out_[Index(entry)];
346 ASSERT(vector_->SubsetOf(*out_set)); // convergence
347 out_set->CopyFrom(vector_);
348 if (tracing_) {
349 THR_Print("Block %" Pd " changed.\n", entry->block_id());
350 }
351 return true;
352}
353
354#if defined(DEBUG)
355bool WriteBarrierElimination::SlotEligibleForWBE(const Slot& slot) {
356 // We assume that Dart code only stores into Instances, Contexts, and
357 // UnhandledExceptions. This assumption is used in
358 // RestoreWriteBarrierInvariantVisitor::VisitPointers.
359
360 switch (slot.kind()) {
361 case Slot::Kind::kCapturedVariable: // Context
362 case Slot::Kind::kDartField: // Instance
363 case Slot::Kind::kRecordField: // Instance
364 return true;
365
366#define TAGGED_NATIVE_DART_SLOT_CASE(class, underlying_type, field, __, ___) \
367 case Slot::Kind::k##class##_##field: \
368 return std::is_base_of<UntaggedInstance, underlying_type>::value || \
369 std::is_base_of<UntaggedContext, underlying_type>::value || \
370 std::is_base_of<UntaggedUnhandledException, \
371 underlying_type>::value;
372
373 TAGGED_NATIVE_DART_SLOTS_LIST(TAGGED_NATIVE_DART_SLOT_CASE)
374#undef TAGGED_NATIVE_DART_SLOT_CASE
375
376#define OTHER_NATIVE_SLOT_CASE(class, __, field, ___, ____) \
377 case Slot::Kind::k##class##_##field:
378 // No store barrier needed for non-tagged fields or fields of
379 // non-Dart objects.
380 NOT_TAGGED_NATIVE_DART_SLOTS_LIST(OTHER_NATIVE_SLOT_CASE)
381#undef OTHER_NATIVE_SLOT_CASE
382 return true;
383
384 default:
385 return false;
386 }
387}
388#endif
389
390void WriteBarrierElimination::UpdateVectorForBlock(BlockEntryInstr* entry,
391 bool finalize) {
392 for (ForwardInstructionIterator it(entry); !it.Done(); it.Advance()) {
393 Instruction* const current = it.Current();
394
395 if (finalize) {
396 if (StoreFieldInstr* instr = current->AsStoreField()) {
397 Definition* const container = instr->instance()->definition();
398 if (IsUsable(container) && vector_->Contains(Index(container))) {
399 DEBUG_ASSERT(SlotEligibleForWBE(instr->slot()));
400 instr->set_emit_store_barrier(kNoStoreBarrier);
401 }
402 } else if (StoreIndexedInstr* instr = current->AsStoreIndexed()) {
403 Definition* const array = instr->array()->definition();
404 if (IsUsable(array) && vector_->Contains(Index(array))) {
405 instr->set_emit_store_barrier(StoreBarrierType::kNoStoreBarrier);
406 }
407 }
408 }
409
410 if (current->CanCallDart()) {
411 vector_->Clear();
412 } else if (current->CanTriggerGC()) {
413 // Clear large array allocations. These are not added to the remembered
414 // set by Thread::RememberLiveTemporaries() after a scavenge.
415 vector_->Intersect(large_array_allocations_mask_);
416 }
417
418 if (AllocationInstr* const alloc = current->AsAllocation()) {
419 if (alloc->WillAllocateNewOrRemembered()) {
420 vector_->Add(Index(alloc));
421 }
422 }
423 }
424}
425
427 WriteBarrierElimination elimination(Thread::Current()->zone(), flow_graph);
428 elimination.Analyze();
429 elimination.SaveResults();
430}
431
432} // namespace dart
#define DEBUG_ASSERT(cond)
Definition assert.h:321
static constexpr intptr_t kMaxLengthForWriteBarrierElimination
Definition object.h:10806
static constexpr bool UseCardMarkingForAllocation(const intptr_t array_length)
Definition object.h:10797
KeyValueTrait::Value LookupValue(typename KeyValueTrait::Key key) const
Definition hash_map.h:159
void Insert(typename KeyValueTrait::Pair kv)
Definition hash_map.h:230
void Set(intptr_t i, bool value)
Definition bit_vector.h:73
void Add(intptr_t i)
Definition bit_vector.h:63
bool Equals(const BitVector &other) const
Definition bit_vector.cc:35
void Print() const
bool SubsetOf(const BitVector &other)
Definition bit_vector.h:97
bool Contains(intptr_t i) const
Definition bit_vector.h:91
bool AddAll(const BitVector *from)
Definition bit_vector.cc:52
void Intersect(const BitVector *other)
Definition bit_vector.cc:93
void CopyFrom(const BitVector *other)
Definition bit_vector.h:54
intptr_t postorder_number() const
Definition il.h:1652
static bool IsKeyEqual(Pair kv, Key key)
bool should_print() const
Definition flow_graph.h:505
static Thread * Current()
Definition thread.h:361
static uint32_t WordHash(intptr_t key)
Definition utils.cc:217
WriteBarrierElimination(Zone *zone, FlowGraph *flow_graph)
#define THR_Print(format,...)
Definition log.h:20
#define ASSERT(E)
#define DEFINE_FLAG(type, name, default_value, comment)
Definition flags.h:16
@ kNoStoreBarrier
Definition il.h:6252
void EliminateWriteBarriers(FlowGraph *flow_graph)
uintptr_t uword
Definition globals.h:501
DirectChainedHashMap< DefinitionIndexPairTrait > DefinitionIndexMap
static bool IsCreateLargeArray(Definition *defn)
#define Pd
Definition globals.h:408
#define TAGGED_NATIVE_DART_SLOTS_LIST(V)
Definition slot.h:343
#define NOT_TAGGED_NATIVE_DART_SLOTS_LIST(V)
Definition slot.h:352
Pair(Definition *definition, intptr_t index)