Skip to content

[SILOptimizer] Use BitfieldRef instead of invalidating SSAPrunedLiveness. #65478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions lib/SILOptimizer/Mandatory/DiagnoseLifetimeIssues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class DiagnoseLifetimeIssues {
SILFunction *function = nullptr;

/// The liveness of the object in question, computed in visitUses.
SSAPrunedLiveness liveness;
BitfieldRef<SSAPrunedLiveness> liveness;

/// All weak stores of the object, which are found in visitUses.
llvm::SmallVector<SILInstruction *, 8> weakStores;
Expand All @@ -86,8 +86,7 @@ class DiagnoseLifetimeIssues {
void reportDeadStore(SILInstruction *allocationInst);

public:
DiagnoseLifetimeIssues(SILFunction *function)
: function(function), liveness(function) {}
DiagnoseLifetimeIssues(SILFunction *function) : function(function) {}

void diagnose();
};
Expand Down Expand Up @@ -215,7 +214,7 @@ visitUses(SILValue def, bool updateLivenessAndWeakStores, int callDepth) {
case OperandOwnership::UnownedInstantaneousUse:
case OperandOwnership::BitwiseEscape:
if (updateLivenessAndWeakStores)
liveness.updateForUse(user, /*lifetimeEnding*/ false);
liveness->updateForUse(user, /*lifetimeEnding*/ false);
break;
case OperandOwnership::GuaranteedForwarding:
case OperandOwnership::ForwardingConsume:
Expand All @@ -241,9 +240,9 @@ visitUses(SILValue def, bool updateLivenessAndWeakStores, int callDepth) {
return CanEscape;
break;
case OperandOwnership::Borrow: {
if (updateLivenessAndWeakStores
&& (liveness.updateForBorrowingOperand(use)
!= InnerBorrowKind::Contained)) {
if (updateLivenessAndWeakStores &&
(liveness->updateForBorrowingOperand(use) !=
InnerBorrowKind::Contained)) {
return CanEscape;
}
BorrowingOperand borrowOper(use);
Expand Down Expand Up @@ -326,11 +325,12 @@ static bool isOutOfLifetime(SILInstruction *inst, SSAPrunedLiveness &liveness) {
/// Reports a warning if the stored object \p storedObj is never loaded within
/// the lifetime of the stored object.
void DiagnoseLifetimeIssues::reportDeadStore(SILInstruction *allocationInst) {
liveness.invalidate();
BitfieldRef<SSAPrunedLiveness>::StackState livenessBitfieldContainer(
liveness, allocationInst->getFunction());
weakStores.clear();

SILValue storedDef = cast<SingleValueInstruction>(allocationInst);
liveness.initializeDef(storedDef);
liveness->initializeDef(storedDef);

// Compute the canonical lifetime of storedDef, like the copy-propagation pass
// would do.
Expand All @@ -346,7 +346,7 @@ void DiagnoseLifetimeIssues::reportDeadStore(SILInstruction *allocationInst) {
assert((state == IsStoredWeakly) == !weakStores.empty());

for (SILInstruction *storeInst : weakStores) {
if (isOutOfLifetime(storeInst, liveness)) {
if (isOutOfLifetime(storeInst, *liveness)) {
// Issue the warning.
storeInst->getModule().getASTContext().Diags.diagnose(
storeInst->getLoc().getSourceLoc(), diag::warn_dead_weak_store);
Expand Down
20 changes: 9 additions & 11 deletions lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,14 +1359,13 @@ struct GatherUsesVisitor final : public TransitiveAddressWalker {

// Pruned liveness used to validate that load [take]/load [copy] can be
// converted to load_borrow without violating exclusivity.
SSAPrunedLiveness &liveness;
BitfieldRef<SSAPrunedLiveness> liveness;

GatherUsesVisitor(MoveOnlyAddressCheckerPImpl &moveChecker,
UseState &useState, MarkMustCheckInst *markedValue,
DiagnosticEmitter &diagnosticEmitter,
SSAPrunedLiveness &gatherUsesLiveness)
DiagnosticEmitter &diagnosticEmitter)
: moveChecker(moveChecker), useState(useState), markedValue(markedValue),
diagnosticEmitter(diagnosticEmitter), liveness(gatherUsesLiveness) {}
diagnosticEmitter(diagnosticEmitter) {}

bool visitUse(Operand *op) override;
void reset(MarkMustCheckInst *address) { useState.address = address; }
Expand All @@ -1383,7 +1382,8 @@ struct GatherUsesVisitor final : public TransitiveAddressWalker {

/// Returns true if we emitted an error.
bool checkForExclusivityHazards(LoadInst *li) {
SWIFT_DEFER { liveness.invalidate(); };
BitfieldRef<SSAPrunedLiveness>::StackState state(liveness,
li->getFunction());

LLVM_DEBUG(llvm::dbgs() << "Checking for exclusivity hazards for: " << *li);

Expand All @@ -1404,10 +1404,10 @@ struct GatherUsesVisitor final : public TransitiveAddressWalker {
}

bool emittedError = false;
liveness.initializeDef(bai);
liveness.computeSimple();
liveness->initializeDef(bai);
liveness->computeSimple();
for (auto *consumingUse : li->getConsumingUses()) {
if (!liveness.isWithinBoundary(consumingUse->getUser())) {
if (!liveness->isWithinBoundary(consumingUse->getUser())) {
diagnosticEmitter.emitAddressExclusivityHazardDiagnostic(
markedValue, consumingUse->getUser());
emittedError = true;
Expand Down Expand Up @@ -2415,10 +2415,8 @@ bool MoveOnlyAddressCheckerPImpl::performSingleCheck(
// Then gather all uses of our address by walking from def->uses. We use this
// to categorize the uses of this address into their ownership behavior (e.x.:
// init, reinit, take, destroy, etc.).
SmallVector<SILBasicBlock *, 32> gatherUsesDiscoveredBlocks;
SSAPrunedLiveness gatherUsesLiveness(fn, &gatherUsesDiscoveredBlocks);
GatherUsesVisitor visitor(*this, addressUseState, markedAddress,
diagnosticEmitter, gatherUsesLiveness);
diagnosticEmitter);
SWIFT_DEFER { visitor.clear(); };
visitor.reset(markedAddress);
if (AddressUseKind::Unknown == std::move(visitor).walk(markedAddress)) {
Expand Down