1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
//===--- LoadCopyToLoadBorrowOpt.cpp --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// Defines the main optimization that converts load [copy] -> load_borrow if we
/// can prove that the +1 is not actually needed and the memory loaded from is
/// never written to while the load [copy]'s object value is being used.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-semantic-arc-opts"
#include "OwnershipLiveRange.h"
#include "SemanticARCOptVisitor.h"
#include "swift/SIL/LinearLifetimeChecker.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/Projection.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Utils/ValueLifetime.h"
#include "llvm/Support/CommandLine.h"
using namespace swift;
using namespace swift::semanticarc;
//===----------------------------------------------------------------------===//
// Memory Analysis
//===----------------------------------------------------------------------===//
namespace {
/// A class that computes in a flow insensitive way if we can prove that our
/// storage is either never written to, or is initialized exactly once and never
/// written to again. In both cases, we can convert load [copy] -> load_borrow
/// safely.
class StorageGuaranteesLoadVisitor
: public AccessUseDefChainVisitor<StorageGuaranteesLoadVisitor> {
// The context that contains global state used across all semantic arc
// optimizations.
Context &ctx;
// The live range of the original load.
const OwnershipLiveRange &liveRange;
// The current address being visited.
SILValue currentAddress;
std::optional<bool> isWritten;
public:
StorageGuaranteesLoadVisitor(Context &context, LoadInst *load,
const OwnershipLiveRange &liveRange)
: ctx(context), liveRange(liveRange), currentAddress(load->getOperand()) {
}
void answer(bool written) {
currentAddress = nullptr;
isWritten = written;
}
void next(SILValue address) { currentAddress = address; }
void visitNestedAccess(BeginAccessInst *access) {
// First see if we have read/modify. If we do not, just look through the
// nested access.
switch (access->getAccessKind()) {
case SILAccessKind::Init:
case SILAccessKind::Deinit:
return next(access->getOperand());
case SILAccessKind::Read:
case SILAccessKind::Modify:
break;
}
// Next check if our live range is completely in the begin/end access
// scope. If so, we may be able to use a load_borrow here!
SmallVector<Operand *, 8> endScopeUses;
transform(access->getEndAccesses(), std::back_inserter(endScopeUses),
[](EndAccessInst *eai) { return &eai->getAllOperands()[0]; });
LinearLifetimeChecker checker(&ctx.getDeadEndBlocks());
if (!checker.validateLifetime(access, endScopeUses,
liveRange.getAllConsumingUses())) {
// If we fail the linear lifetime check, then just recur:
return next(access->getOperand());
}
// Otherwise, if we have read, then we are done!
if (access->getAccessKind() == SILAccessKind::Read) {
return answer(false);
}
// If we have a modify, check if our value is /ever/ written to. If it is
// never actually written to, then we convert to a load_borrow.
auto result = ctx.addressToExhaustiveWriteListCache.get(access);
if (!result.has_value()) {
return answer(true);
}
if (result.value().empty()) {
return answer(false);
}
return answer(true);
}
void visitArgumentAccess(SILFunctionArgument *arg) {
// If this load_copy is from an indirect in_guaranteed argument, then we
// know for sure that it will never be written to.
if (arg->hasConvention(SILArgumentConvention::Indirect_In_Guaranteed)) {
return answer(false);
}
// If we have an inout parameter that isn't ever actually written to, return
// false.
if (!arg->isIndirectResult() &&
arg->getArgumentConvention().isExclusiveIndirectParameter()) {
auto wellBehavedWrites = ctx.addressToExhaustiveWriteListCache.get(arg);
if (!wellBehavedWrites.has_value()) {
return answer(true);
}
// No writes.
if (wellBehavedWrites->empty()) {
return answer(false);
}
// Ok, we have some writes. See if any of them are within our live
// range. If any are, we definitely can not promote to load_borrow.
SmallVector<BeginAccessInst *, 16> foundBeginAccess;
LinearLifetimeChecker checker(&ctx.getDeadEndBlocks());
SILValue introducerValue = liveRange.getIntroducer().value;
SmallVector<Operand *, 4> consumingUses;
for (auto *op : liveRange.getDestroyingUses()) {
consumingUses.push_back(op);
}
for (auto *op : liveRange.getUnknownConsumingUses()) {
consumingUses.push_back(op);
}
if (!checker.usesNotContainedWithinLifetime(
introducerValue, consumingUses, *wellBehavedWrites)) {
return answer(true);
}
// Finally, check if our live range is strictly contained within any of
// our scoped writes.
SmallVector<Operand *, 16> endAccessList;
for (Operand *use : *wellBehavedWrites) {
auto *bai = dyn_cast<BeginAccessInst>(use->getUser());
if (!bai) {
continue;
}
endAccessList.clear();
llvm::transform(
bai->getUsersOfType<EndAccessInst>(),
std::back_inserter(endAccessList),
[](EndAccessInst *eai) { return &eai->getAllOperands()[0]; });
// We know that our live range is based on a load [copy], so we know
// that our value must have a defining inst.
auto *definingInst =
cast<LoadInst>(introducerValue->getDefiningInstruction());
// Then if our defining inst is not in our bai, endAccessList region, we
// know that the two ranges must be disjoint, so continue.
if (!checker.validateLifetime(bai, endAccessList,
&definingInst->getAllOperands()[0])) {
continue;
}
// Otherwise, we do have an overlap, return true.
return answer(true);
}
// Otherwise, there isn't an overlap, so we don't write to it.
return answer(false);
}
// TODO: This should be extended:
//
// 1. We should be able to analyze in arguments and see if they are only
// ever destroyed at the end of the function. In such a case, we may be
// able to also to promote load [copy] from such args to load_borrow.
return answer(true);
}
void visitGlobalAccess(SILValue global) {
return answer(
!AccessStorage(global, AccessStorage::Global).isLetAccess());
}
void visitClassAccess(RefElementAddrInst *field) {
currentAddress = nullptr;
// We know a let property won't be written to if the base object is
// guaranteed for the duration of the access.
// For non-let properties conservatively assume they may be written to.
if (!field->getField()->isLet()) {
return answer(true);
}
// The lifetime of the `let` is guaranteed if it's dominated by the
// guarantee on the base. See if we can find a single borrow introducer for
// this object. If we could not find a single such borrow introducer, assume
// that our property is conservatively written to.
SILValue baseObject = field->getOperand();
auto value = getSingleBorrowIntroducingValue(baseObject);
if (!value) {
return answer(true);
}
// Ok, we have a single borrow introducing value. First do a quick check if
// we have a non-local scope that is a function argument. In such a case, we
// know statically that our let can not be written to in the current
// function. To be conservative, assume that all other non-local scopes
// write to memory.
if (!value.isLocalScope()) {
if (value.kind == BorrowedValueKind::SILFunctionArgument) {
return answer(false);
}
// TODO: Once we model Coroutine results as non-local scopes, we should be
// able to return false here for them as well.
return answer(true);
}
// TODO: This is disabled temporarily for guaranteed phi args just for
// staging purposes. Thus be conservative and assume true in these cases.
if (value.kind == BorrowedValueKind::Phi) {
return answer(true);
}
// Ok, we now know that we have a local scope whose lifetime we need to
// analyze. With that in mind, gather up the lifetime ending uses of our
// borrow scope introducing value and then use the linear lifetime checker
// to check whether the copied value is dominated by the lifetime of the
// borrow it's based on.
SmallVector<Operand *, 4> endScopeInsts;
value.visitLocalScopeEndingUses(
[&](Operand *use) { endScopeInsts.push_back(use); return true; });
LinearLifetimeChecker checker(&ctx.getDeadEndBlocks());
// Returns true on success. So we invert.
bool foundError = !checker.validateLifetime(
baseObject, endScopeInsts, liveRange.getAllConsumingUses());
return answer(foundError);
}
// TODO: Handle other access kinds?
void visitBase(SILValue base, AccessStorage::Kind kind) {
return answer(true);
}
void visitNonAccess(SILValue addr) { return answer(true); }
void visitCast(SingleValueInstruction *cast, Operand *parentAddr) {
return next(parentAddr->get());
}
void visitStorageCast(SingleValueInstruction *projectedAddr,
Operand *parentAddr, AccessStorageCast cast) {
return next(parentAddr->get());
}
void visitAccessProjection(SingleValueInstruction *projectedAddr,
Operand *parentAddr) {
return next(parentAddr->get());
}
void visitPhi(SILPhiArgument *phi) {
// We shouldn't have address phis in OSSA SIL, so we don't need to recur
// through the predecessors here.
return answer(true);
}
/// See if we have an alloc_stack that is only written to once by an
/// initializing instruction.
void visitStackAccess(AllocStackInst *stack) {
// These will contain all of the address destroying operands that form the
// lifetime of the object. They may not be destroy_addr!
SmallVector<Operand *, 8> addrDestroyingOperands;
bool initialAnswer = isSingleInitAllocStack(stack, addrDestroyingOperands);
if (!initialAnswer)
return answer(true);
// Then make sure that all of our load [copy] uses are within the
// destroy_addr.
LinearLifetimeChecker checker(&ctx.getDeadEndBlocks());
// Returns true on success. So we invert.
bool foundError = !checker.validateLifetime(
stack, addrDestroyingOperands /*consuming users*/,
liveRange.getAllConsumingUses() /*non consuming users*/);
return answer(foundError);
}
bool doIt() {
while (currentAddress) {
visit(currentAddress);
}
return *isWritten;
}
};
} // namespace
static bool isWrittenTo(Context &ctx, LoadInst *load,
const OwnershipLiveRange &lr) {
StorageGuaranteesLoadVisitor visitor(ctx, load, lr);
return visitor.doIt();
}
//===----------------------------------------------------------------------===//
// Top Level Entrypoint
//===----------------------------------------------------------------------===//
bool SemanticARCOptVisitor::visitLoadInst(LoadInst *li) {
// This optimization can use more complex analysis. We should do some
// experiments before enabling this by default as a guaranteed optimization.
if (ctx.onlyMandatoryOpts)
return false;
// If we are not supposed to perform this transform, bail.
if (!ctx.shouldPerform(ARCTransformKind::LoadCopyToLoadBorrowPeephole))
return false;
if (li->getOwnershipQualifier() != LoadOwnershipQualifier::Copy)
return false;
// Ok, we have our load [copy]. Try to optimize considering its live range.
if (performLoadCopyToLoadBorrowOptimization(li, li))
return true;
// Check whether the load [copy]'s only use is as an operand to a move_value.
auto *use = li->getSingleUse();
if (!use)
return false;
auto *mvi = dyn_cast<MoveValueInst>(use->getUser());
if (!mvi)
return false;
// Try to optimize considering the move_value's live range.
return performLoadCopyToLoadBorrowOptimization(li, mvi);
}
// Convert a load [copy] from unique storage [read] whose representative
// (either the load [copy] itself or a move from it) has all uses that can
// accept a guaranteed parameter to a load_borrow.
bool SemanticARCOptVisitor::performLoadCopyToLoadBorrowOptimization(
LoadInst *li, SILValue original) {
assert(li->getOwnershipQualifier() == LoadOwnershipQualifier::Copy);
assert(li == original || li->getSingleUse()->getUser() ==
cast<SingleValueInstruction>(original));
// Make sure its value is truly a dead live range implying it is only ever
// consumed by destroy_value instructions. If it is consumed, we need to pass
// off a +1 value, so bail.
//
// FIXME: We should consider if it is worth promoting a load [copy]
// -> load_borrow if we can put a copy_value on a cold path and thus
// eliminate RR traffic on a hot path.
OwnershipLiveRange lr(original);
if (bool(lr.hasUnknownConsumingUse()))
return false;
// Then check if our address is ever written to. If it is, then we cannot use
// the load_borrow because the stored value may be released during the loaded
// value's live range.
if (isWrittenTo(ctx, li, lr) ||
(li != original && isWrittenTo(ctx, li, OwnershipLiveRange(li))))
return false;
// Ok, we can perform our optimization. Convert the load [copy] into a
// load_borrow.
auto *lbi =
SILBuilderWithScope(li).createLoadBorrow(li->getLoc(), li->getOperand());
lr.insertEndBorrowsAtDestroys(lbi, getDeadEndBlocks(), ctx.lifetimeFrontier);
SILValue replacement = lbi;
if (original != li) {
getCallbacks().eraseAndRAUWSingleValueInst(li, lbi);
auto *bbi = SILBuilderWithScope(cast<SingleValueInstruction>(original))
.createBeginBorrow(li->getLoc(), lbi,
IsLexical_t(original->isLexical()));
replacement = bbi;
lr.insertEndBorrowsAtDestroys(bbi, getDeadEndBlocks(),
ctx.lifetimeFrontier);
}
std::move(lr).convertToGuaranteedAndRAUW(replacement, getCallbacks());
return true;
}
|