File: LexicalDestroyHoisting.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (431 lines) | stat: -rw-r--r-- 14,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//=-- LexicalDestroyHoisting.cpp - Hoist destroy_values to deinit barriers. -=//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Hoist destroys of owned lexical values (owned arguments and the results of
/// move_value [lexical] instructions) up to deinit barriers.
//===----------------------------------------------------------------------===//

#include "swift/AST/Builtins.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/Test.h"
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
#include "swift/SILOptimizer/Analysis/Reachability.h"
#include "swift/SILOptimizer/Analysis/VisitBarrierAccessScopes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CanonicalizeBorrowScope.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "llvm/ADT/STLExtras.h"

#define DEBUG_TYPE "copy-propagation"

using namespace swift;

//===----------------------------------------------------------------------===//
//                       MARK: LexicalDestroyHoisting
//===----------------------------------------------------------------------===//

namespace LexicalDestroyHoisting {

/// The environment within which to hoist.
struct Context final {
  /// The owned lexical value whose destroys are to be hoisted.
  SILValue const &value;

  /// value->getDefiningInstruction()
  SILInstruction *const definition;

  SILBasicBlock *defBlock;

  SILFunction &function;

  InstructionDeleter &deleter;

  BasicCalleeAnalysis *calleeAnalysis;

  Context(SILValue const &value, SILFunction &function,
          InstructionDeleter &deleter, BasicCalleeAnalysis *calleeAnalysis)
      : value(value), definition(value->getDefiningInstruction()),
        defBlock(value->getParentBlock()), function(function), deleter(deleter),
        calleeAnalysis(calleeAnalysis) {
    assert(value->isLexical());
    assert(value->getOwnershipKind() == OwnershipKind::Owned);
  }
  Context(Context const &) = delete;
  Context &operator=(Context const &) = delete;
};

/// How %value gets used.
struct Usage final {
  /// Instructions which are users of the simple (i.e. not reborrowed) value.
  SmallPtrSet<SILInstruction *, 16> users;
  // The instructions from which the hoisting starts, the destroy_values.
  llvm::SmallSetVector<SILInstruction *, 4> ends;

  Usage(){};
  Usage(Usage const &) = delete;
  Usage &operator=(Usage const &) = delete;
};

/// Identify users and destroy_values of %value.
///
/// returns true if all uses were found
///         false otherwise
bool findUsage(Context const &context, Usage &usage) {
  SmallVector<Operand *, 16> uses;
  if (!findUsesOfSimpleValue(context.value, &uses)) {
    // If the value escapes, don't hoist.
    return false;
  }
  for (auto *use : uses) {
    // Add the destroy_values to the collection of ends so we can seed the data
    // flow and determine whether any were reused.  They aren't uses over which
    // we can't hoist though.
    auto dv = dyn_cast<DestroyValueInst>(use->getUser());
    if (dv && dv->getOperand() == context.value) {
      usage.ends.insert(use->getUser());
    } else {
      usage.users.insert(use->getUser());
    }
  }
  return true;
}

/// How destroy_value hoisting is obstructed.
struct DeinitBarriers final {
  /// Instructions above which destroy_values cannot be hoisted.
  SmallVector<SILInstruction *, 4> instructions;

  /// Blocks one of whose phis is a barrier and consequently out of which
  /// destroy_values cannot be hoisted.
  SmallVector<SILBasicBlock *, 4> phis;

  SmallVector<SILBasicBlock *, 4> blocks;

  DeinitBarriers(Context &context) {}
  DeinitBarriers(DeinitBarriers const &) = delete;
  DeinitBarriers &operator=(DeinitBarriers const &) = delete;
};

class BarrierAccessScopeFinder;

/// Works backwards from the current location of destroy_values to the earliest
/// place they can be hoisted to.
///
/// Implements IterativeBackwardReachability::Effects
/// Implements IterativeBackwardReachability::bindBarriers::Visitor
/// Implements VisitBarrierAccessScopes::Effects
class Dataflow final {
  using Reachability = IterativeBackwardReachability<Dataflow>;
  using Effect = Reachability::Effect;
  Context const &context;
  Usage const &uses;
  DeinitBarriers &barriers;
  Reachability::Result result;
  Reachability reachability;
  SmallPtrSet<BeginAccessInst *, 8> barrierAccessScopes;

  enum class Classification { Barrier, Other };

public:
  Dataflow(Context const &context, Usage const &uses, DeinitBarriers &barriers)
      : context(context), uses(uses), barriers(barriers),
        result(&context.function),
        reachability(Reachability::untilInitialBlock(
            &context.function, context.defBlock, *this, result)) {}
  Dataflow(Dataflow const &) = delete;
  Dataflow &operator=(Dataflow const &) = delete;

  void run();

private:
  friend Reachability;
  friend class BarrierAccessScopeFinder;
  friend class VisitBarrierAccessScopes<Dataflow, BarrierAccessScopeFinder>;

  Classification classifyInstruction(SILInstruction *);

  bool classificationIsBarrier(Classification);

  /// IterativeBackwardReachability::Effects
  /// VisitBarrierAccessScopes::Effects

  auto gens() { return uses.ends; }

  Effect effectForInstruction(SILInstruction *);
  Effect effectForPhi(SILBasicBlock *);

  /// VisitBarrierAccessScopes::Effects

  auto localGens() { return result.localGens; }

  bool isLocalGen(SILInstruction *instruction) {
    return result.localGens.contains(instruction);
  }

  /// IterativeBackwardReachability::bindBarriers::Visitor

  void visitBarrierInstruction(SILInstruction *instruction) {
    barriers.instructions.push_back(instruction);
  }

  void visitBarrierPhi(SILBasicBlock *block) { barriers.phis.push_back(block); }

  void visitBarrierBlock(SILBasicBlock *block) {
    barriers.blocks.push_back(block);
  }

  void visitInitialBlock(SILBasicBlock *block) {
    barriers.blocks.push_back(block);
  }
};

Dataflow::Classification
Dataflow::classifyInstruction(SILInstruction *instruction) {
  if (instruction == context.definition) {
    return Classification::Barrier;
  }
  if (uses.users.contains(instruction)) {
    return Classification::Barrier;
  }
  if (auto *eai = dyn_cast<EndAccessInst>(instruction)) {
    return barrierAccessScopes.contains(eai->getBeginAccess())
               ? Classification::Barrier
               : Classification::Other;
  }
  if (isDeinitBarrier(instruction, context.calleeAnalysis)) {
    return Classification::Barrier;
  }
  return Classification::Other;
}

bool Dataflow::classificationIsBarrier(Classification classification) {
  switch (classification) {
  case Classification::Barrier:
    return true;
  case Classification::Other:
    return false;
  }
  llvm_unreachable("exhaustive switch not exhaustive?!");
}

Dataflow::Effect Dataflow::effectForInstruction(SILInstruction *instruction) {
  if (uses.ends.contains(instruction))
    return Effect::Gen();
  auto classification = classifyInstruction(instruction);
  return classificationIsBarrier(classification) ? Effect::Kill()
                                                 : Effect::NoEffect();
}

Dataflow::Effect Dataflow::effectForPhi(SILBasicBlock *block) {
  assert(llvm::all_of(block->getArguments(),
                      [&](auto argument) { return PhiValue(argument); }));

  bool isBarrier =
      llvm::any_of(block->getPredecessorBlocks(), [&](auto *predecessor) {
        return classificationIsBarrier(
            classifyInstruction(predecessor->getTerminator()));
      });
  return isBarrier ? Effect::Kill() : Effect::NoEffect();
}

/// Finds end_access instructions which are barriers to hoisting because the
/// access scopes they contain barriers to hoisting.  Hoisting destroy_values
/// into such access scopes could introduce exclusivity violations.
///
/// Implements BarrierAccessScopeFinder::Visitor
class BarrierAccessScopeFinder final {
  using Impl = VisitBarrierAccessScopes<Dataflow, BarrierAccessScopeFinder>;
  Impl impl;
  Dataflow &dataflow;

public:
  BarrierAccessScopeFinder(Context const &context, Dataflow &dataflow)
      : impl(&context.function, dataflow, *this), dataflow(dataflow) {}

  void find() { impl.visit(); }

private:
  friend Impl;

  bool isInRegion(SILBasicBlock *block) {
    return dataflow.result.discoveredBlocks.contains(block);
  }

  void visitBarrierAccessScope(BeginAccessInst *bai) {
    dataflow.barrierAccessScopes.insert(bai);
    for (auto *eai : bai->getEndAccesses()) {
      dataflow.reachability.addKill(eai);
    }
  }
};

void Dataflow::run() {
  reachability.initialize();
  BarrierAccessScopeFinder finder(context, *this);
  finder.find();
  reachability.solve();
  reachability.findBarriers(*this);
}

/// Hoist the destroy_values of %value.
class Rewriter final {
  Context &context;
  Usage const &uses;
  DeinitBarriers const &barriers;

  /// The destroy_value instructions for this owned lexical value that existed
  /// before LexicalDestroyHoisting ran and which were not modified.
  llvm::SmallPtrSet<SILInstruction *, 8> reusedDestroyValueInsts;

public:
  Rewriter(Context &context, Usage const &uses, DeinitBarriers const &barriers)
      : context(context), uses(uses), barriers(barriers) {}
  Rewriter(Rewriter const &) = delete;
  Rewriter &operator=(Rewriter const &) = delete;

  bool run();

private:
  bool createDestroyValue(SILInstruction *insertionPoint);
};

bool Rewriter::run() {
  bool madeChange = false;

  // Add destroy_values for phi barrier boundaries.
  //
  // A block is a phi barrier iff any of its predecessors' terminators get
  // classified as barriers.
  for (auto *block : barriers.phis) {
    madeChange |= createDestroyValue(&block->front());
  }

  // Add destroy_values for barrier boundaries.
  //
  // Insert destroy_values after every non-terminator barrier.
  //
  // For terminator barriers, add destroy_values at the beginning of the
  // successor blocks.  In order to reach a terminator and classify it as a
  // barrier, all of a block P's successors B had reachable beginnings.  If any
  // of them didn't, then BackwardReachability::meetOverSuccessors would never
  // have returned true for P, so none of its instructions would ever have been
  // classified (except for via checkReachablePhiBarrier, which doesn't record
  // terminator barriers).
  for (auto instruction : barriers.instructions) {
    if (auto *terminator = dyn_cast<TermInst>(instruction)) {
      auto successors = terminator->getParentBlock()->getSuccessorBlocks();
      for (auto *successor : successors) {
        madeChange |= createDestroyValue(&successor->front());
      }
    } else {
      auto *next = instruction->getNextInstruction();
      assert(next);
      madeChange |= createDestroyValue(next);
    }
  }

  // Add destroy_values for control-flow boundaries.
  //
  // Insert destroy_values at the beginning of blocks which were preceded by a
  // control flow branch (and which, thanks to the lack of critical edges,
  // don't have multiple predecessors) whose end was not reachable (because
  // reachability was not able to make it to the top of some other successor).
  //
  // In other words, a control flow boundary is the target edge from a block B
  // to its single predecessor P not all of whose successors S in succ(P) had
  // reachable beginnings.  We witness that fact about P's successors by way of
  // P not having a reachable end--see BackwardReachability::meetOverSuccessors.
  //
  // control-flow-boundary(B) := beginning-reachable(B) && !end-reachable(P)
  for (auto *block : barriers.blocks) {
    madeChange |= createDestroyValue(&block->front());
  }

  if (madeChange) {
    // Remove all the original destroy_values instructions.
    for (auto *end : uses.ends) {
      if (reusedDestroyValueInsts.contains(end)) {
        continue;
      }
      context.deleter.forceDelete(end);
    }
  }

  return madeChange;
}

bool Rewriter::createDestroyValue(SILInstruction *insertionPoint) {
  if (auto *ebi = dyn_cast<DestroyValueInst>(insertionPoint)) {
    if (llvm::find(uses.ends, insertionPoint) != uses.ends.end()) {
      reusedDestroyValueInsts.insert(insertionPoint);
      return false;
    }
  }
  auto builder = SILBuilderWithScope(insertionPoint);
  builder.createDestroyValue(
      RegularLocation::getAutoGeneratedLocation(insertionPoint->getLoc()),
      context.value);
  return true;
}

bool run(Context &context) {
  Usage usage;
  if (!findUsage(context, usage))
    return false;

  DeinitBarriers barriers(context);
  Dataflow flow(context, usage, barriers);
  flow.run();

  Rewriter rewriter(context, usage, barriers);

  return rewriter.run();
}
} // end namespace LexicalDestroyHoisting

bool swift::hoistDestroysOfOwnedLexicalValue(
    SILValue const value, SILFunction &function, InstructionDeleter &deleter,
    BasicCalleeAnalysis *calleeAnalysis) {
  if (!value->isLexical())
    return false;
  if (value->getOwnershipKind() != OwnershipKind::Owned)
    return false;
  LexicalDestroyHoisting::Context context(value, function, deleter,
                                          calleeAnalysis);
  return LexicalDestroyHoisting::run(context);
}

namespace swift::test {
// Arguments:
// - bool: pruneDebug
// - bool: maximizeLifetimes
// - bool: "respectAccessScopes", whether to contract lifetimes to end within
//         access scopes which they previously enclosed but can't be hoisted
//         before
// - SILValue: value to canonicalize
// Dumps:
// - function after value canonicalization
static FunctionTest LexicalDestroyHoistingTest(
    "lexical_destroy_hoisting",
    [](auto &function, auto &arguments, auto &test) {
      auto *calleeAnalysis = test.template getAnalysis<BasicCalleeAnalysis>();
      InstructionDeleter deleter;
      auto value = arguments.takeValue();
      hoistDestroysOfOwnedLexicalValue(value, *value->getFunction(), deleter,
                                       calleeAnalysis);
      function.print(llvm::outs());
    });
} // end namespace swift::test