Skip to content

Commit 8d898e5

Browse files
committed
[semantic-arc] Extract owned phi to guaranteed phi conversion from the visitor to its own function that works on the SemanticARCContext.
1 parent 0b00258 commit 8d898e5

File tree

5 files changed

+72
-16
lines changed

5 files changed

+72
-16
lines changed

lib/SILOptimizer/SemanticARC/Context.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ struct LLVM_LIBRARY_VISIBILITY Context {
7272
/// copy_values we /do/ allow.
7373
bool onlyGuaranteedOpts;
7474

75+
/// Callbacks that we must use to remove or RAUW values.
76+
InstModCallbacks instModCallbacks;
77+
7578
using FrozenMultiMapRange =
7679
decltype(joinedOwnedIntroducerToConsumedOperands)::PairToSecondEltRange;
7780

@@ -81,7 +84,7 @@ struct LLVM_LIBRARY_VISIBILITY Context {
8184
return *deadEndBlocks;
8285
}
8386

84-
Context(SILFunction &fn, bool onlyGuaranteedOpts)
87+
Context(SILFunction &fn, bool onlyGuaranteedOpts, InstModCallbacks callbacks)
8588
: fn(fn), deadEndBlocks(), lifetimeFrontier(),
8689
addressToExhaustiveWriteListCache(constructCacheValue),
8790
onlyGuaranteedOpts(onlyGuaranteedOpts) {}

lib/SILOptimizer/SemanticARC/OwnedToGuaranteedPhiOpt.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
///
1717
//===----------------------------------------------------------------------===//
1818

19+
#include "Context.h"
1920
#include "OwnershipPhiOperand.h"
20-
#include "SemanticARCOptVisitor.h"
21+
#include "Transforms.h"
2122

2223
using namespace swift;
2324
using namespace swift::semanticarc;
@@ -120,7 +121,7 @@ static bool getIncomingJoinedLiveRangeOperands(
120121
// Top Level Entrypoint
121122
//===----------------------------------------------------------------------===//
122123

123-
bool SemanticARCOptVisitor::performPostPeepholeOwnedArgElimination() {
124+
bool swift::semanticarc::tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) {
124125
bool madeChange = false;
125126

126127
// First freeze our multi-map so we can use it for map queries. Also, setup a
@@ -211,7 +212,7 @@ bool SemanticARCOptVisitor::performPostPeepholeOwnedArgElimination() {
211212
incomingValueUpdates.emplace_back(cvi->getOperand(), updateOffset);
212213
}
213214
std::move(lr).convertToGuaranteedAndRAUW(cvi->getOperand(),
214-
getCallbacks());
215+
ctx.instModCallbacks);
215216
continue;
216217
}
217218
llvm_unreachable("Unhandled optimizable introducer!");
@@ -227,7 +228,7 @@ bool SemanticARCOptVisitor::performPostPeepholeOwnedArgElimination() {
227228
// Then convert the phi's live range to be guaranteed.
228229
std::move(joinedLiveRange)
229230
.convertJoinedLiveRangePhiToGuaranteed(
230-
getDeadEndBlocks(), ctx.lifetimeFrontier, getCallbacks());
231+
ctx.getDeadEndBlocks(), ctx.lifetimeFrontier, ctx.instModCallbacks);
231232

232233
// Now if our phi operand consumes/forwards its guaranteed input, insert a
233234
// begin_borrow along the incoming value edges. We have to do this after

lib/SILOptimizer/SemanticARC/SemanticARCOptVisitor.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ struct LLVM_LIBRARY_VISIBILITY SemanticARCOptVisitor
4848
Context ctx;
4949

5050
explicit SemanticARCOptVisitor(SILFunction &fn, bool onlyGuaranteedOpts)
51-
: ctx(fn, onlyGuaranteedOpts) {}
51+
: ctx(fn, onlyGuaranteedOpts,
52+
InstModCallbacks(
53+
[this](SILInstruction *inst) { eraseInstruction(inst); },
54+
[](SILInstruction *) {}, [](SILValue, SILValue) {},
55+
[this](SingleValueInstruction *i, SILValue value) {
56+
eraseAndRAUWSingleValueInstruction(i, value);
57+
})) {}
5258

5359
DeadEndBlocks &getDeadEndBlocks() { return ctx.getDeadEndBlocks(); }
5460

@@ -190,7 +196,6 @@ struct LLVM_LIBRARY_VISIBILITY SemanticARCOptVisitor
190196
bool performGuaranteedCopyValueOptimization(CopyValueInst *cvi);
191197
bool eliminateDeadLiveRangeCopyValue(CopyValueInst *cvi);
192198
bool tryJoiningCopyValueLiveRangeWithOperand(CopyValueInst *cvi);
193-
bool performPostPeepholeOwnedArgElimination();
194199
};
195200

196201
} // namespace semanticarc

lib/SILOptimizer/SemanticARC/SemanticARCOpts.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
#define DEBUG_TYPE "sil-semantic-arc-opts"
1414

15-
#include "swift/Basic/LLVM.h"
16-
1715
#include "OwnershipLiveRange.h"
1816
#include "OwnershipPhiOperand.h"
1917
#include "SemanticARCOptVisitor.h"
18+
#include "Transforms.h"
19+
20+
#include "swift/Basic/LLVM.h"
2021

2122
#include "swift/Basic/BlotSetVector.h"
2223
#include "swift/Basic/FrozenMultiMap.h"
@@ -74,9 +75,6 @@ bool SemanticARCOptVisitor::optimize() {
7475
assert(!madeAdditionalChanges && "Should be at the fixed point");
7576
}
7677

77-
// Then use the newly seeded peephole map to
78-
madeChange |= performPostPeepholeOwnedArgElimination();
79-
8078
return madeChange;
8179
}
8280

@@ -180,13 +178,21 @@ struct SemanticARCOpts : SILFunctionTransform {
180178
}
181179
}
182180

183-
// Then process the worklist. We only destroy instructions, so invalidate
184-
// that. Once we modify the ownership of block arguments, we will need to
185-
// perhaps invalidate branches as well.
186-
if (visitor.optimize()) {
181+
// Then process the worklist, performing peepholes.
182+
bool madeChange = visitor.optimize();
183+
184+
// Now that we have seeded the map of phis to incoming values that could be
185+
// converted to guaranteed, ignoring the phi, try convert those phis to be
186+
// guaranteed.
187+
if (tryConvertOwnedPhisToGuaranteedPhis(visitor.ctx)) {
187188
invalidateAnalysis(
188189
SILAnalysis::InvalidationKind::BranchesAndInstructions);
190+
return;
189191
}
192+
193+
// Otherwise, we only deleted instructions and did not touch phis.
194+
if (madeChange)
195+
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
190196
}
191197
};
192198

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- Transforms.h -----------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
///
13+
/// \file
14+
///
15+
/// Top level transforms for SemanticARCOpts
16+
///
17+
//===----------------------------------------------------------------------===//
18+
19+
#ifndef SWIFT_SILOPTIMIZER_SEMANTICARCOPT_TRANSFORMS_H
20+
#define SWIFT_SILOPTIMIZER_SEMANTICARCOPT_TRANSFORMS_H
21+
22+
#include "llvm/Support/Compiler.h"
23+
24+
namespace swift {
25+
namespace semanticarc {
26+
27+
struct Context;
28+
29+
/// Given the current map of owned phi arguments to consumed incoming values in
30+
/// ctx, attempt to convert these owned phi arguments to guaranteed phi
31+
/// arguments if the phi arguments are the only thing that kept us from
32+
/// converting these incoming values to be guaranteed.
33+
///
34+
/// \returns true if we converted atleast one phi from owned -> guaranteed and
35+
/// eliminated ARC traffic as a result.
36+
bool tryConvertOwnedPhisToGuaranteedPhis(Context &ctx) LLVM_LIBRARY_VISIBILITY;
37+
38+
} // namespace semanticarc
39+
} // namespace swift
40+
41+
#endif

0 commit comments

Comments
 (0)