|
| 1 | +//===--- AccessEnforcementDom.cpp - dominated access removal opt ---===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +/// This function pass removes dynamic access enforcement based on dominance. |
| 14 | +/// |
| 15 | +/// General case: |
| 16 | +/// begin_access A (may or may not have no_nested_conflict) |
| 17 | +/// load/store |
| 18 | +/// end_access |
| 19 | +/// ... |
| 20 | +/// begin_access A [no_nested_conflict] // dominated by the first access |
| 21 | +/// load/store |
| 22 | +/// end_access A |
| 23 | +/// The second access scope does not need to be emitted. |
| 24 | +/// |
| 25 | +/// Note: This optimization must be aware of all possible access to a Class or |
| 26 | +/// Global address. This includes unpaired access instructions and keypath |
| 27 | +/// entry points. Ignoring any access pattern would weaken enforcement. |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | + |
| 30 | +#define DEBUG_TYPE "access-enforcement-dom" |
| 31 | + |
| 32 | +#include "swift/SIL/DebugUtils.h" |
| 33 | +#include "swift/SIL/MemAccessUtils.h" |
| 34 | +#include "swift/SIL/SILFunction.h" |
| 35 | +#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h" |
| 36 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 37 | +#include "swift/SILOptimizer/Utils/Local.h" |
| 38 | + |
| 39 | +using namespace swift; |
| 40 | + |
| 41 | +namespace { |
| 42 | +class DominatedAccessRemoval { |
| 43 | +public: |
| 44 | + using AccessedStoragePair = std::pair<BeginAccessInst *, AccessedStorage>; |
| 45 | + using AccessedStorageInfo = llvm::SmallVector<AccessedStoragePair, 32>; |
| 46 | + using DominatorToDominatedPair = |
| 47 | + std::pair<BeginAccessInst *, BeginAccessInst *>; |
| 48 | + using DomPairSet = llvm::SmallVector<DominatorToDominatedPair, 32>; |
| 49 | + using KeyPathEntryPointsSet = llvm::SmallSet<SILInstruction *, 8>; |
| 50 | + using UnpairedAccessToStoragePair = |
| 51 | + std::pair<BeginUnpairedAccessInst *, AccessedStorage>; |
| 52 | + using UnpairedAccessToStorageInfo = |
| 53 | + llvm::SmallVector<UnpairedAccessToStoragePair, 8>; |
| 54 | + |
| 55 | +public: |
| 56 | + DominatedAccessRemoval(SILFunction &func, DominanceInfo *domInfo) |
| 57 | + : func(func), domInfo(domInfo) {} |
| 58 | + |
| 59 | + void perform(); |
| 60 | + |
| 61 | +protected: |
| 62 | + void visitInstruction(SILInstruction *instr); |
| 63 | + void visitBeginAccess(BeginAccessInst *beginAccess, AccessedStorage storage); |
| 64 | + bool domByKeyPath(BeginAccessInst *dominatedInstr); |
| 65 | + bool domByRelevantUnpairedAccess(DominatorToDominatedPair pair); |
| 66 | + void analyze(); |
| 67 | + void optimize(); |
| 68 | + |
| 69 | +private: |
| 70 | + SILFunction &func; |
| 71 | + DominanceInfo *domInfo; |
| 72 | + AccessedStorageInfo accessInfo; |
| 73 | + DomPairSet domPairs; |
| 74 | + KeyPathEntryPointsSet keypathEntries; |
| 75 | + UnpairedAccessToStorageInfo unpairedEntries; |
| 76 | +}; |
| 77 | +} // namespace |
| 78 | + |
| 79 | +bool DominatedAccessRemoval::domByKeyPath(BeginAccessInst *dominatedInstr) { |
| 80 | + for (SILInstruction *keyPathEntry : keypathEntries) { |
| 81 | + if (domInfo->properlyDominates(keyPathEntry, dominatedInstr)) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + return false; |
| 86 | +} |
| 87 | + |
| 88 | +bool DominatedAccessRemoval::domByRelevantUnpairedAccess( |
| 89 | + DominatorToDominatedPair pair) { |
| 90 | + BeginAccessInst *parentBegin = pair.first; |
| 91 | + BeginAccessInst *dominatedInstr = pair.second; |
| 92 | + auto predEqual = [&](AccessedStoragePair it) { |
| 93 | + auto currInstr = it.first; |
| 94 | + return currInstr == parentBegin; |
| 95 | + }; |
| 96 | + auto currStorageIt = |
| 97 | + std::find_if(accessInfo.begin(), accessInfo.end(), predEqual); |
| 98 | + assert(currStorageIt != accessInfo.end() && "Expected storage in accessInfo"); |
| 99 | + AccessedStorage currStorage = currStorageIt->second; |
| 100 | + for (UnpairedAccessToStoragePair unpairedEntry : unpairedEntries) { |
| 101 | + auto *instr = unpairedEntry.first; |
| 102 | + if (!domInfo->properlyDominates(instr, dominatedInstr)) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + auto entryStorage = unpairedEntry.second; |
| 106 | + if (!currStorage.isDistinctFrom(entryStorage)) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + return false; |
| 111 | +} |
| 112 | + |
| 113 | +void DominatedAccessRemoval::visitInstruction(SILInstruction *instr) { |
| 114 | + if (auto *BAI = dyn_cast<BeginAccessInst>(instr)) { |
| 115 | + if (BAI->getEnforcement() != SILAccessEnforcement::Dynamic) { |
| 116 | + return; |
| 117 | + } |
| 118 | + AccessedStorage storage = findAccessedStorageNonNested(BAI->getSource()); |
| 119 | + if (!storage) { |
| 120 | + return; |
| 121 | + } |
| 122 | + visitBeginAccess(BAI, storage); |
| 123 | + } else if (auto fullApply = FullApplySite::isa(instr)) { |
| 124 | + SILFunction *callee = fullApply.getReferencedFunction(); |
| 125 | + if (!callee) |
| 126 | + return; |
| 127 | + if (!callee->hasSemanticsAttr("_keyPathEntryPoint")) |
| 128 | + return; |
| 129 | + // we can't eliminate dominated checks even when we can prove that |
| 130 | + // the dominated scope has no internal nested conflicts. |
| 131 | + keypathEntries.insert(fullApply.getInstruction()); |
| 132 | + } else if (auto *BUAI = dyn_cast<BeginUnpairedAccessInst>(instr)) { |
| 133 | + AccessedStorage storage = findAccessedStorageNonNested(BUAI->getSource()); |
| 134 | + unpairedEntries.push_back(std::make_pair(BUAI, storage)); |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +void DominatedAccessRemoval::visitBeginAccess(BeginAccessInst *beginAccess, |
| 139 | + AccessedStorage storage) { |
| 140 | + auto predEqual = [&](AccessedStoragePair it) { |
| 141 | + auto currStorage = it.second; |
| 142 | + return currStorage.hasIdenticalBase(storage); |
| 143 | + }; |
| 144 | + |
| 145 | + // If the currnet access has nested conflict, just add it to map |
| 146 | + // we can't remove it by finding a dominating access |
| 147 | + if (!beginAccess->hasNoNestedConflict()) { |
| 148 | + accessInfo.push_back(std::make_pair(beginAccess, storage)); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + auto it = std::find_if(accessInfo.begin(), accessInfo.end(), predEqual); |
| 153 | + while (it != accessInfo.end()) { |
| 154 | + BeginAccessInst *parentBeginAccess = it->first; |
| 155 | + if (!domInfo->properlyDominates(parentBeginAccess, beginAccess)) { |
| 156 | + ++it; |
| 157 | + it = std::find_if(it, accessInfo.end(), predEqual); |
| 158 | + continue; |
| 159 | + } |
| 160 | + // Found a pair that can potentially be optimized |
| 161 | + domPairs.push_back(std::make_pair(parentBeginAccess, beginAccess)); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + // Did not find a dominating access to same storage |
| 166 | + accessInfo.push_back(std::make_pair(beginAccess, storage)); |
| 167 | +} |
| 168 | + |
| 169 | +// Finds domPairs for which we can change the dominated instruction to static |
| 170 | +// NOTE: We might not be able to optimize some the pairs due to other |
| 171 | +// restrictions Such as key-path or unpaired begin access We only traverse the |
| 172 | +// function once, if we find a pattern that *might* prevent optimization, we |
| 173 | +// just add it to appropriate data structures which will be analyzed later. |
| 174 | +void DominatedAccessRemoval::analyze() { |
| 175 | + SILBasicBlock *entry = &func.front(); |
| 176 | + DominanceOrder domOrder(entry, domInfo, func.size()); |
| 177 | + while (SILBasicBlock *block = domOrder.getNext()) { |
| 178 | + for (auto &instr : *block) { |
| 179 | + visitInstruction(&instr); |
| 180 | + } |
| 181 | + domOrder.pushChildren(block); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// Sets the dominated instruction to static. |
| 186 | +// Goes through the data structures initialized by the analysis method |
| 187 | +// and makes sure we are not Weakening enforcement |
| 188 | +void DominatedAccessRemoval::optimize() { |
| 189 | + for (DominatorToDominatedPair pair : domPairs) { |
| 190 | + LLVM_DEBUG(llvm::dbgs() |
| 191 | + << "Processing optimizable pair - Dominator: " << *pair.first |
| 192 | + << " , Dominated: " << *pair.second << "\n"); |
| 193 | + BeginAccessInst *dominatedInstr = pair.second; |
| 194 | + // look through keypathEntries to see if dominatedInstr |
| 195 | + // can no longer be optimized |
| 196 | + if (domByKeyPath(dominatedInstr)) { |
| 197 | + LLVM_DEBUG(llvm::dbgs() |
| 198 | + << "Can not set " << *dominatedInstr |
| 199 | + << " access enforcement to static - it is properly dominated " |
| 200 | + "by a key-path entry point\n"); |
| 201 | + continue; |
| 202 | + } |
| 203 | + if (domByRelevantUnpairedAccess(pair)) { |
| 204 | + LLVM_DEBUG(llvm::dbgs() |
| 205 | + << "Can not set " << *dominatedInstr |
| 206 | + << " access enforcement to static - there's an unpaired " |
| 207 | + "access that is not distinct from it in the way\n"); |
| 208 | + continue; |
| 209 | + } |
| 210 | + LLVM_DEBUG(llvm::dbgs() << "Setting " << *dominatedInstr |
| 211 | + << " access enforcement to static\n"); |
| 212 | + dominatedInstr->setEnforcement(SILAccessEnforcement::Static); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +void DominatedAccessRemoval::perform() { |
| 217 | + if (func.empty()) |
| 218 | + return; |
| 219 | + |
| 220 | + analyze(); |
| 221 | + optimize(); |
| 222 | +} |
| 223 | + |
| 224 | +namespace { |
| 225 | +struct AccessEnforcementDom : public SILFunctionTransform { |
| 226 | + void run() override { |
| 227 | + DominanceAnalysis *domAnalysis = getAnalysis<DominanceAnalysis>(); |
| 228 | + DominanceInfo *domInfo = domAnalysis->get(getFunction()); |
| 229 | + DominatedAccessRemoval eliminationPass(*getFunction(), domInfo); |
| 230 | + eliminationPass.perform(); |
| 231 | + } |
| 232 | +}; |
| 233 | +} // namespace |
| 234 | + |
| 235 | +SILTransform *swift::createAccessEnforcementDom() { |
| 236 | + return new AccessEnforcementDom(); |
| 237 | +} |
0 commit comments