Skip to content

Commit 82abf02

Browse files
committed
[pruned-liveness] Change the internal machinery of updateForUse to use only scalar logic.
I did this by doing the following: 1. I renamed computeUseBlockLiveness to computeScalarUseBlockLiveness and changed it to take a specific bit that it is testing for. 2. I changed the logic that already existed in this code path that worked scalar by scalar to use scalar logic rather than call the broken multi-bit at a time code path. 3. We took advantage of resultingFoundLiveness now only returning the requested bits instead of all bits. This ensures that we do not run computeScalarUseBlockLiveness for those unneeded dead bits resulting in liveness being inappropriately propagated into predecessors.
1 parent be50c56 commit 82abf02

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

include/swift/SIL/PrunedLiveness.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class PrunedLiveBlocks {
283283
auto liveness = getBlockLiveness(block, bitNo);
284284
if (liveness != Dead)
285285
return liveness;
286-
computeUseBlockLiveness(block, bitNo, bitNo + 1);
286+
computeScalarUseBlockLiveness(block, bitNo);
287287
return getBlockLiveness(block, bitNo);
288288
}
289289

@@ -348,8 +348,14 @@ class PrunedLiveBlocks {
348348
}
349349
}
350350

351-
void computeUseBlockLiveness(SILBasicBlock *userBB, unsigned startBitNo,
352-
unsigned endBitNo);
351+
private:
352+
/// A helper routine that as a fast path handles the scalar case. We do not
353+
/// handle the mult-bit case today since the way the code is written today
354+
/// assumes we process a bit at a time.
355+
///
356+
/// TODO: Make a multi-bit query for efficiency reasons.
357+
void computeScalarUseBlockLiveness(SILBasicBlock *userBB,
358+
unsigned startBitNo);
353359
};
354360

355361
/// If inner borrows are 'Contained', then liveness is fully described by the

lib/SIL/Utils/PrunedLiveness.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121

2222
using namespace swift;
2323

24-
/// Mark blocks live during a reverse CFG traversal from one specific block
25-
/// containing a user.
26-
void PrunedLiveBlocks::computeUseBlockLiveness(SILBasicBlock *userBB,
27-
unsigned startBitNo,
28-
unsigned endBitNo) {
24+
void PrunedLiveBlocks::computeScalarUseBlockLiveness(SILBasicBlock *userBB,
25+
unsigned startBitNo) {
26+
unsigned endBitNo = startBitNo + 1;
27+
2928
// If, we are visiting this block, then it is not already LiveOut. Mark it
3029
// LiveWithin to indicate a liveness boundary within the block.
3130
markBlockLive(userBB, startBitNo, endBitNo, LiveWithin);
3231

33-
SmallVector<IsLive, 8> predLivenessInfo;
32+
// Specialize the given code for scalar code.
3433
BasicBlockWorklist worklist(userBB->getFunction());
3534
worklist.push(userBB);
3635

@@ -40,19 +39,15 @@ void PrunedLiveBlocks::computeUseBlockLiveness(SILBasicBlock *userBB,
4039
// Traversal terminates at any previously visited block, including the
4140
// blocks initialized as definition blocks.
4241
for (auto *predBlock : block->getPredecessorBlocks()) {
43-
SWIFT_DEFER { predLivenessInfo.clear(); };
44-
getBlockLiveness(predBlock, startBitNo, endBitNo, predLivenessInfo);
45-
for (unsigned i : indices(predLivenessInfo)) {
46-
switch (predLivenessInfo[i]) {
47-
case Dead:
48-
worklist.pushIfNotVisited(predBlock);
49-
LLVM_FALLTHROUGH;
50-
case LiveWithin:
51-
markBlockLive(predBlock, startBitNo, endBitNo, LiveOut);
52-
break;
53-
case LiveOut:
54-
break;
55-
}
42+
switch (getBlockLiveness(predBlock, startBitNo)) {
43+
case Dead:
44+
worklist.pushIfNotVisited(predBlock);
45+
LLVM_FALLTHROUGH;
46+
case LiveWithin:
47+
markBlockLive(predBlock, startBitNo, endBitNo, LiveOut);
48+
break;
49+
case LiveOut:
50+
break;
5651
}
5752
}
5853
}
@@ -66,22 +61,26 @@ void PrunedLiveBlocks::computeUseBlockLiveness(SILBasicBlock *userBB,
6661
void PrunedLiveBlocks::updateForUse(
6762
SILInstruction *user, unsigned startBitNo, unsigned endBitNo,
6863
SmallVectorImpl<IsLive> &resultingLivenessInfo) {
64+
resultingLivenessInfo.clear();
65+
6966
SWIFT_ASSERT_ONLY(seenUse = true);
7067

7168
auto *bb = user->getParent();
7269
getBlockLiveness(bb, startBitNo, endBitNo, resultingLivenessInfo);
7370

74-
for (auto isLive : resultingLivenessInfo) {
75-
switch (isLive) {
71+
for (auto pair : llvm::enumerate(resultingLivenessInfo)) {
72+
unsigned index = pair.index();
73+
unsigned specificBitNo = startBitNo + index;
74+
switch (pair.value()) {
7675
case LiveOut:
7776
case LiveWithin:
7877
continue;
7978
case Dead: {
8079
// This use block has not yet been marked live. Mark it and its
8180
// predecessor blocks live.
82-
computeUseBlockLiveness(bb, startBitNo, endBitNo);
83-
resultingLivenessInfo.clear();
84-
return getBlockLiveness(bb, startBitNo, endBitNo, resultingLivenessInfo);
81+
computeScalarUseBlockLiveness(bb, specificBitNo);
82+
resultingLivenessInfo.push_back(getBlockLiveness(bb, specificBitNo));
83+
continue;
8584
}
8685
}
8786
llvm_unreachable("covered switch");

0 commit comments

Comments
 (0)