Skip to content

[move-only] Combine the address/object checker in the same pass so that we only run cleanups once. #63774

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
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
6 changes: 4 additions & 2 deletions include/swift/SILOptimizer/PassManager/Passes.def
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ PASS(BugReducerTester, "bug-reducer-tester",
PASS(AssemblyVisionRemarkGenerator, "assembly-vision-remark-generator",
"Emit assembly vision remarks that provide source level guidance of where runtime calls ended up")
PASS(MoveOnlyObjectChecker, "sil-move-only-object-checker",
"Pass that enforces move only invariants on raw SIL for objects")
"Utility pass that enforces move only invariants on raw SIL for objects for testing purposes")
PASS(MoveOnlyAddressChecker, "sil-move-only-address-checker",
"Pass that enforces move only invariants on raw SIL for addresses")
"Utility pass that enforces move only invariants on raw SIL for addresses for testing purposes")
PASS(MoveOnlyChecker, "sil-move-only-checker",
"Pass that enforces move only invariants on raw SIL for addresses and objects")
PASS(ConsumeOperatorCopyableValuesChecker, "sil-consume-operator-copyable-values-checker",
"Pass that performs checking of the consume operator for copyable values")
PASS(TrivialMoveOnlyTypeEliminator, "sil-trivial-move-only-type-eliminator",
Expand Down
12 changes: 8 additions & 4 deletions lib/SILOptimizer/Mandatory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ target_sources(swiftSILOptimizer PRIVATE
LowerHopToActor.cpp
MandatoryInlining.cpp
MovedAsyncVarDebugInfoPropagator.cpp
MoveOnlyAddressChecker.cpp
MoveOnlyBorrowToDestructureTransform.cpp
MoveOnlyBorrowToDestructureTransformTester.cpp
MoveOnlyAddressCheckerUtils.cpp
MoveOnlyAddressCheckerTester.cpp
MoveOnlyBorrowToDestructureUtils.cpp
MoveOnlyBorrowToDestructureTester.cpp
MoveOnlyDeinitInsertion.cpp
MoveOnlyDiagnostics.cpp
MoveOnlyObjectChecker.cpp
MoveOnlyObjectCheckerUtils.cpp
MoveOnlyObjectCheckerTester.cpp
MoveOnlyChecker.cpp
MoveOnlyUtils.cpp
NestedSemanticFunctionCheck.cpp
OptimizeHopToExecutor.cpp
PerformanceDiagnostics.cpp
Expand Down
141 changes: 141 additions & 0 deletions lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerTester.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//===--- MoveOnlyAddressCheckerTester.cpp ---------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "sil-move-only-checker"

#include "swift/AST/AccessScope.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FrozenMultiMap.h"
#include "swift/Basic/SmallBitVector.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/BasicBlockBits.h"
#include "swift/SIL/BasicBlockData.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/FieldSensitivePrunedLiveness.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/ClosureScope.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"

#include "MoveOnlyAddressCheckerUtils.h"
#include "MoveOnlyBorrowToDestructureUtils.h"
#include "MoveOnlyDiagnostics.h"
#include "MoveOnlyObjectCheckerUtils.h"
#include "MoveOnlyUtils.h"

#include <utility>

using namespace swift;
using namespace swift::siloptimizer;

//===----------------------------------------------------------------------===//
// MARK: Top Level Entrypoint
//===----------------------------------------------------------------------===//

namespace {

class MoveOnlyAddressCheckerTesterPass : public SILFunctionTransform {
void run() override {
auto *fn = getFunction();

// Only run this pass if the move only language feature is enabled.
if (!fn->getASTContext().LangOpts.Features.contains(Feature::MoveOnly))
return;

// Don't rerun diagnostics on deserialized functions.
if (getFunction()->wasDeserializedCanonical())
return;

assert(fn->getModule().getStage() == SILStage::Raw &&
"Should only run on Raw SIL");
LLVM_DEBUG(llvm::dbgs() << "===> MoveOnly Addr Checker. Visiting: "
<< fn->getName() << '\n');
auto *dominanceAnalysis = getAnalysis<DominanceAnalysis>();
DominanceInfo *domTree = dominanceAnalysis->get(fn);
auto *poa = getAnalysis<PostOrderAnalysis>();

DiagnosticEmitter diagnosticEmitter;
SmallSetVector<MarkMustCheckInst *, 32> moveIntroducersToProcess;
searchForCandidateAddressMarkMustChecks(fn, moveIntroducersToProcess,
diagnosticEmitter);

LLVM_DEBUG(llvm::dbgs()
<< "Emitting diagnostic when checking for mark must check inst: "
<< (diagnosticEmitter.getDiagnosticCount() ? "yes" : "no")
<< '\n');

bool madeChange = false;
unsigned diagCount = 0;
if (moveIntroducersToProcess.empty()) {
LLVM_DEBUG(llvm::dbgs() << "No move introducers found?!\n");
} else {
borrowtodestructure::IntervalMapAllocator allocator;
MoveOnlyAddressChecker checker{getFunction(), diagnosticEmitter,
allocator, domTree, poa};
madeChange = checker.check(moveIntroducersToProcess);
diagCount = checker.diagnosticEmitter.getDiagnosticCount();
}

// If we did not emit any diagnostics, emit a diagnostic if we missed any
// copies.
if (!diagCount) {
emitCheckerMissedCopyOfNonCopyableTypeErrors(getFunction(),
diagnosticEmitter);
}

// Then cleanup any copies we left behind for either reason and emit an
// error.
madeChange |=
cleanupNonCopyableCopiesAfterEmittingDiagnostic(getFunction());

if (madeChange) {
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
}
}
};

} // anonymous namespace

SILTransform *swift::createMoveOnlyAddressChecker() {
return new MoveOnlyAddressCheckerTesterPass();
}
Loading