|
| 1 | +//===--- MoveOnlyBorrowToDestructureTransform.cpp -------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2022 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 This is a pass that converts the borrow + gep pattern to destructures |
| 14 | +/// or emits an error if it cannot be done. It is assumed that it runs |
| 15 | +/// immediately before move checking of objects runs. This ensures that the move |
| 16 | +/// checker does not need to worry about this problem and instead can just check |
| 17 | +/// that the newly inserted destructures do not cause move only errors. |
| 18 | +/// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#define DEBUG_TYPE "sil-move-only-checker" |
| 22 | + |
| 23 | +#include "MoveOnlyDiagnostics.h" |
| 24 | +#include "MoveOnlyObjectChecker.h" |
| 25 | + |
| 26 | +#include "swift/Basic/Defer.h" |
| 27 | +#include "swift/SIL/SILBuilder.h" |
| 28 | +#include "swift/SIL/SILInstruction.h" |
| 29 | +#include "swift/SILOptimizer/Analysis/Analysis.h" |
| 30 | +#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h" |
| 31 | +#include "swift/Basic/BlotSetVector.h" |
| 32 | +#include "swift/Basic/FrozenMultiMap.h" |
| 33 | +#include "swift/SILOptimizer/Analysis/PostOrderAnalysis.h" |
| 34 | +#include "swift/SILOptimizer/PassManager/Passes.h" |
| 35 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 36 | +#include "swift/SILOptimizer/Utils/CFGOptUtils.h" |
| 37 | +#include "llvm/ADT/ArrayRef.h" |
| 38 | + |
| 39 | +using namespace swift; |
| 40 | +using namespace swift::siloptimizer; |
| 41 | + |
| 42 | +//===----------------------------------------------------------------------===// |
| 43 | +// Top Level Entrypoint |
| 44 | +//===----------------------------------------------------------------------===// |
| 45 | + |
| 46 | +static bool runTransform(SILFunction *fn, |
| 47 | + ArrayRef<MarkMustCheckInst *> moveIntroducersToProcess, |
| 48 | + PostOrderAnalysis *poa, |
| 49 | + DiagnosticEmitter &diagnosticEmitter) { |
| 50 | + BorrowToDestructureTransform::IntervalMapAllocator allocator; |
| 51 | + bool madeChange = false; |
| 52 | + while (!moveIntroducersToProcess.empty()) { |
| 53 | + auto *mmci = moveIntroducersToProcess.back(); |
| 54 | + moveIntroducersToProcess = moveIntroducersToProcess.drop_back(); |
| 55 | + |
| 56 | + unsigned currentDiagnosticCount = diagnosticEmitter.getDiagnosticCount(); |
| 57 | + |
| 58 | + StackList<BeginBorrowInst *> borrowWorklist(mmci->getFunction()); |
| 59 | + |
| 60 | + // If we failed to gather borrows due to the transform not understanding |
| 61 | + // part of the SIL, emit a diagnostic, RAUW the mark must check, and |
| 62 | + // continue. |
| 63 | + if (!BorrowToDestructureTransform::gatherBorrows(mmci, borrowWorklist)) { |
| 64 | + diagnosticEmitter.emitCheckerDoesntUnderstandDiagnostic(mmci); |
| 65 | + mmci->replaceAllUsesWith(mmci->getOperand()); |
| 66 | + mmci->eraseFromParent(); |
| 67 | + madeChange = true; |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // If we do not have any borrows to process, continue and process the next |
| 72 | + // instruction. |
| 73 | + if (borrowWorklist.empty()) |
| 74 | + continue; |
| 75 | + |
| 76 | + SmallVector<SILBasicBlock *, 8> discoveredBlocks; |
| 77 | + |
| 78 | + // Now that we have found all of our borrows, we want to find struct_extract |
| 79 | + // uses of our borrow as well as any operands that cannot use an owned |
| 80 | + // value. |
| 81 | + SWIFT_DEFER { discoveredBlocks.clear(); }; |
| 82 | + BorrowToDestructureTransform transform(allocator, mmci, diagnosticEmitter, |
| 83 | + poa, discoveredBlocks); |
| 84 | + |
| 85 | + // Attempt to gather uses. Return if we saw something that we did not |
| 86 | + // understand. Emit a compiler did not understand diagnostic, RAUW the mmci |
| 87 | + // so later passes do not see it, and set madeChange to true. |
| 88 | + if (!transform.gatherUses(borrowWorklist)) { |
| 89 | + diagnosticEmitter.emitCheckerDoesntUnderstandDiagnostic(mmci); |
| 90 | + mmci->replaceAllUsesWith(mmci->getOperand()); |
| 91 | + mmci->eraseFromParent(); |
| 92 | + madeChange = true; |
| 93 | + continue; |
| 94 | + } |
| 95 | + |
| 96 | + // Next make sure that any destructure needing instructions are on the |
| 97 | + // boundary in a per bit field sensitive manner. |
| 98 | + transform.checkDestructureUsesOnBoundary(); |
| 99 | + |
| 100 | + // If we emitted any diagnostic, set madeChange to true, eliminate our mmci, |
| 101 | + // and continue. |
| 102 | + if (currentDiagnosticCount != diagnosticEmitter.getDiagnosticCount()) { |
| 103 | + mmci->replaceAllUsesWith(mmci->getOperand()); |
| 104 | + mmci->eraseFromParent(); |
| 105 | + madeChange = true; |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + // At this point, we know that all of our destructure requiring uses are on |
| 110 | + // the boundary of our live range. Now we need to do the rewriting. |
| 111 | + transform.blockToAvailableValues.emplace(transform.liveness); |
| 112 | + transform.rewriteUses(); |
| 113 | + |
| 114 | + // Now that we have done our rewritting, we need to do a few cleanups. |
| 115 | + // |
| 116 | + // NOTE: We do not eliminate our mark_must_check since we want later passes |
| 117 | + // to do additional checking upon the mark_must_check including making sure |
| 118 | + // that our destructures do not need cause the need for additional copies to |
| 119 | + // be inserted. We only eliminate the mark_must_check if we emitted a |
| 120 | + // diagnostic of some sort. |
| 121 | + transform.cleanup(borrowWorklist); |
| 122 | + madeChange = true; |
| 123 | + } |
| 124 | + |
| 125 | + return madeChange; |
| 126 | +} |
| 127 | + |
| 128 | +namespace { |
| 129 | + |
| 130 | +class MoveOnlyBorrowToDestructureTransformPass : public SILFunctionTransform { |
| 131 | + void run() override { |
| 132 | + auto *fn = getFunction(); |
| 133 | + |
| 134 | + // Only run this pass if the move only language feature is enabled. |
| 135 | + if (!fn->getASTContext().LangOpts.Features.contains(Feature::MoveOnly)) |
| 136 | + return; |
| 137 | + |
| 138 | + // Don't rerun diagnostics on deserialized functions. |
| 139 | + if (getFunction()->wasDeserializedCanonical()) |
| 140 | + return; |
| 141 | + |
| 142 | + assert(fn->getModule().getStage() == SILStage::Raw && |
| 143 | + "Should only run on Raw SIL"); |
| 144 | + |
| 145 | + LLVM_DEBUG(llvm::dbgs() |
| 146 | + << "===> MoveOnlyBorrowToDestructureTransform. Visiting: " |
| 147 | + << fn->getName() << '\n'); |
| 148 | + |
| 149 | + auto *postOrderAnalysis = getAnalysis<PostOrderAnalysis>(); |
| 150 | + |
| 151 | + SmallSetVector<MarkMustCheckInst *, 32> moveIntroducersToProcess; |
| 152 | + DiagnosticEmitter emitter; |
| 153 | + |
| 154 | + bool madeChange = searchForCandidateObjectMarkMustChecks( |
| 155 | + getFunction(), moveIntroducersToProcess, emitter); |
| 156 | + if (madeChange) { |
| 157 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 158 | + } |
| 159 | + |
| 160 | + if (emitter.emittedAnyDiagnostics()) { |
| 161 | + if (cleanupSILAfterEmittingObjectMoveOnlyDiagnostics(fn)) |
| 162 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + auto introducers = llvm::makeArrayRef(moveIntroducersToProcess.begin(), |
| 167 | + moveIntroducersToProcess.end()); |
| 168 | + if (runTransform(fn, introducers, postOrderAnalysis, emitter)) { |
| 169 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 170 | + } |
| 171 | + |
| 172 | + if (emitter.emittedAnyDiagnostics()) { |
| 173 | + if (cleanupSILAfterEmittingObjectMoveOnlyDiagnostics(fn)) |
| 174 | + invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions); |
| 175 | + } |
| 176 | + } |
| 177 | +}; |
| 178 | + |
| 179 | +} // namespace |
| 180 | + |
| 181 | +SILTransform *swift::createMoveOnlyBorrowToDestructureTransform() { |
| 182 | + return new MoveOnlyBorrowToDestructureTransformPass(); |
| 183 | +} |
0 commit comments