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 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
|
//===-- FlowIsolation.cpp - Enforces flow-sensitive actor isolation rules -===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 - 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 "flow-isolation"
#include "llvm/Support/WithColor.h"
#include "swift/AST/Expr.h"
#include "swift/AST/ActorIsolation.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/Sema/Concurrency.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/BitDataflow.h"
#include "swift/SIL/BasicBlockBits.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
using namespace swift;
namespace {
class AnalysisInfo;
// MARK: utilities
static SILFunction* getCallee(SILInstruction *someInst) {
if (auto apply = ApplySite::isa(someInst))
if (SILFunction *callee = apply.getCalleeFunction())
return callee;
return nullptr;
}
// Represents the state of isolation for `self` during the flow-analysis,
// at entry and exit to a block. The states are part of a semi-lattice,
// where the extra top element represents a conflict in isolation:
//
// T = "top"
// / \
// Iso NonIso
// \ /
// B = "bottom"
//
// While we will be talking about isolated vs nonisolated uses, the only
// isolated uses that we consider are stored property accesses.
struct State {
// Each state kind, as an integer, is its position in any bit vectors.
enum Kind {
Isolated = 0,
Nonisolated = 1
};
// Number of states, excluding Top or Bottom, in this flow problem.
static constexpr unsigned NumStates = 2;
};
/// Information gathered for analysis that is specific to a block.
struct Info {
using UseSet = SmallPtrSet<SILInstruction*, 8>;
/// Records all nonisolated uses of `self` in the block, and their kind of
/// use to aid diagnostics.
UseSet nonisolatedUses;
/// Records all stored property uses based on `self` in the block.
/// These are the only isolated uses that we care about.
UseSet propertyUses;
Info() : nonisolatedUses(), propertyUses() {}
// Diagnoses all property uses as being an error.
void diagnoseAll(AnalysisInfo &info, bool forDeinit,
SILInstruction *blame = nullptr);
/// Returns the block corresponding to this information.
SILBasicBlock* getBlock() const {
if (!propertyUses.empty())
return (*(propertyUses.begin()))->getParent();
if (!nonisolatedUses.empty())
return (*(nonisolatedUses.begin()))->getParent();
// I only expect to call this when there's a use, so to save memory
// we compute the corresponding block from its stored uses.
assert(false && "no uses to determine block");
return nullptr;
}
SILInstruction* firstPropertyUse() const {
auto *blk = getBlock();
for (auto &inst : *blk) {
if (propertyUses.count(&inst))
return &inst;
}
assert(false && "no first property use found!");
return nullptr;
}
bool hasNonisolatedUse() const {
return !nonisolatedUses.empty();
}
bool hasPropertyUse() const {
return !propertyUses.empty();
}
void dump() const LLVM_ATTRIBUTE_USED {
llvm::dbgs() << "nonisolatedUses:\n";
for (auto const *i : nonisolatedUses)
i->dump();
llvm::dbgs() << "propertyUses:\n";
for (auto const *i : propertyUses)
i->dump();
}
};
/// \returns true iff the function is a deinit, or a defer of a deinit.
static bool isWithinDeinit(SILFunction *fn) {
auto *astFn = fn->getDeclContext()->getAsDecl();
if (auto *funcDecl = dyn_cast<FuncDecl>(astFn))
if (funcDecl->isDeferBody())
astFn = funcDecl->getParent()->getAsDecl();
return isa<DestructorDecl>(astFn);
}
/// Carries the state of analysis for an entire SILFunction.
class AnalysisInfo : public BasicBlockData<Info> {
private:
/// Isolation state at the start of the entry block to this function.
/// This should always be `isolated`, unless if this is a `defer`.
State::Kind startingIsolation = State::Isolated;
public:
// The deferBlocks information is shared between all blocks of
// this analysis information's function.
llvm::SmallMapVector< SILFunction*,
std::unique_ptr<AnalysisInfo>, 8> deferBlocks;
// Only computed after calling solve()
BitDataflow flow;
/// This value represents the outgoing isolation state of the function if
/// a normal return is reached, along with the block that returns normally.
/// Only computed after calling solve(), where it remains None if the function
/// doesn't return normally.
std::optional<std::pair<SILBasicBlock *, State::Kind>> normalReturn =
std::nullopt;
/// indicates whether the SILFunction is (or contained in) a deinit.
bool forDeinit;
AnalysisInfo(SILFunction *fn) : BasicBlockData<Info>(fn),
flow(fn, State::NumStates) {
forDeinit = isWithinDeinit(fn);
}
// analyzes the function for uses of `self`.
void analyze(const SILArgument* selfParam);
// Solves the data-flow problem, assuming analysis has been performed.
void solve();
// Verifies uses in this function, assuming solving has been performed.
void verifyIsolation();
/// Finds an appropriate instruction that can be blamed for introducing a
/// source of `nonisolation` in a control-flow path leading the given
/// instruction. Preferring the closest block. Use for diagnostics.
/// \param start an instruction that can be reached by a `nonisolated`
/// use in the CFG.
/// \returns an instruction that can be used for blame in a diagnostic.
SILInstruction *findNonisolatedBlame(SILInstruction *start);
void diagnoseEntireFunction(SILInstruction* blame) {
assert(blame);
for (auto bnd : *this)
bnd.data.diagnoseAll(*this, forDeinit, blame);
}
/// Does this function have a nonisolated use?
bool hasNonisolatedUse() const {
for (auto const& bnd : *this)
if (bnd.data.hasNonisolatedUse())
return true;
return false;
}
/// Does this function have a property use?
bool hasPropertyUse() const {
for (auto const& bnd : *this)
if (bnd.data.hasPropertyUse())
return true;
return false;
}
/// Do we have sub-analysis information for this function, as a defer body?
bool haveDeferInfo(SILFunction *someFn) {
assert(someFn);
return deferBlocks.count(someFn) > 0;
}
AnalysisInfo& getOrCreateDeferInfo(SILFunction* someFn) {
assert(someFn);
if (haveDeferInfo(someFn))
return *(deferBlocks[someFn]);
// otherwise, insert fresh info and retry.
deferBlocks.insert({someFn, std::make_unique<AnalysisInfo>(someFn)});
return getOrCreateDeferInfo(someFn);
}
/// Records an incoming isolation kind to this function from a call-site.
/// \returns true iff the start state has changed from isolated to nonisolated
bool setNonisolatedStart() {
// once we enter the nonisolated state, nothing will change that.
if (startingIsolation == State::Nonisolated)
return false;
startingIsolation = State::Nonisolated;
return true;
}
/// Test whether the incoming isolation kind was set to nonisolated.
bool hasNonisolatedStart() const {
return startingIsolation == State::Nonisolated;
}
/// Records that the instruction accesses an isolated property.
void markPropertyUse(SILInstruction *i) {
LLVM_DEBUG(llvm::dbgs() << "marking as isolated: " << *i);
auto &blockData = this->operator[](i->getParent());
blockData.propertyUses.insert(i);
}
/// Records that the instruction causes 'self' to become nonisolated.
void markNonIsolated(SILInstruction *i) {
LLVM_DEBUG(llvm::dbgs() << "marking as non-isolated: " << *i);
auto &blockData = this->operator[](i->getParent());
blockData.nonisolatedUses.insert(i);
}
void dump() const LLVM_ATTRIBUTE_USED {
llvm::dbgs() << "analysis-info for " << getFunction()->getName() << "\n";
for (auto const& bnd : *this) {
llvm::dbgs() << "bb" << bnd.block.getDebugID() << "\n";
bnd.data.dump();
}
llvm::dbgs() << "flow-problem state:\n";
flow.dump();
// print the defer information in a different color, if supported.
llvm::WithColor color(llvm::dbgs(), raw_ostream::BLUE);
for (auto const& entry : deferBlocks)
entry.second->dump();
}
};
// MARK: diagnostics
SILInstruction *AnalysisInfo::findNonisolatedBlame(SILInstruction* startInst) {
assert(startInst);
SILBasicBlock* firstBlk = startInst->getParent();
assert(firstBlk->getParent() == getFunction());
// searches the a block starting at the provided position in reverse
// order of instructions (i.e., from terminator to first instruction).
auto searchBlockForNonisolated =
[&](SILBasicBlock::reverse_iterator cursor) -> SILInstruction * {
SILBasicBlock *block = cursor->getParent();
auto &state = flow[block];
// does this block generate non-isolation?
if (state.genSet[State::Nonisolated]) {
auto &data = this->operator[](block);
assert(!data.nonisolatedUses.empty());
// scan from the cursor backwards in this block.
while (cursor != block->rend()) {
auto *inst = &*cursor;
cursor++;
if (data.nonisolatedUses.count(inst)) {
return inst;
}
}
}
return nullptr;
};
// whether we should visit a given predecessor block in the search.
auto shouldVisit = [&](SILBasicBlock *pred) {
// visit blocks that contribute nonisolation to successors.
return flow[pred].exitSet[State::Nonisolated];
};
// first check if the nonisolated use precedes the start instruction in
// this same block.
if (auto *inst = searchBlockForNonisolated(startInst->getReverseIterator()))
return inst;
// Seed a workQueue with the predecessors of this start block to
// begin a breadth-first search to find one of the closest predecessors.
BasicBlockWorkqueue workQueue(firstBlk->getFunction());
for (auto *pred : firstBlk->getPredecessorBlocks())
if (shouldVisit(pred))
workQueue.push(pred);
while (auto *block = workQueue.pop()) {
// do we have a nonisolated use here?
if (auto *inst = searchBlockForNonisolated(block->rbegin()))
return inst;
// otherwise keep looking
for (auto *pred : block->getPredecessorBlocks()) {
if (shouldVisit(pred))
workQueue.pushIfNotVisited(pred);
}
}
llvm_unreachable("failed to find nonisolated blame.");
}
static StringRef verbForInvoking(ValueDecl *value) {
// Only computed properties need a different verb.
if (isa<AbstractStorageDecl>(value))
return "accessing ";
return "calling ";
}
/// For a specific note diagnostic that describes the blamed instruction for
/// introducing non-isolation, this function produces the values needed
/// to describe it to the user. Thus, the implementation of this function is
/// closely tied to that diagnostic.
static std::tuple<StringRef, StringRef, DeclName>
describe(SILInstruction *blame) {
auto &ctx = blame->getModule().getASTContext();
// check if it's a call-like thing.
if (auto apply = ApplySite::isa(blame)) {
/// First, look for a callee declaration.
///
/// We can't use ApplySite::getCalleeFunction because it is overly
/// precise in finding the specific corresponding SILFunction. We only care
/// about describing the referenced AST decl, since that's all programmers
/// know.
ValueDecl *callee = nullptr;
auto inspect = [](ValueDecl *decl) -> ValueDecl* {
// if this is an accessor, then return the storage instead.
if (auto accessor = dyn_cast<AccessorDecl>(decl))
return accessor->getStorage();
return decl;
};
SILValue silCallee = apply.getCalleeOrigin();
if (auto *methInst = dyn_cast<MethodInst>(silCallee))
callee = inspect(methInst->getMember().getDecl());
if (auto *funcInst = dyn_cast<FunctionRefBaseInst>(silCallee)) {
auto *refFunc = funcInst->getInitiallyReferencedFunction();
if (auto *declCxt = refFunc->getDeclContext()) {
if (auto *absFn =
dyn_cast_or_null<AbstractFunctionDecl>(declCxt->getAsDecl())) {
callee = inspect(absFn);
} else if (isa<AbstractClosureExpr>(declCxt)) {
// TODO: determine if the closure captures self, or is applied to it,
// so we can be more specific in this message.
return std::make_tuple("this closure involving", "", ctx.Id_self);
}
}
}
// if we have no callee info, all we know is it's a call involving self.
if (!callee)
return std::make_tuple("a call involving", "", ctx.Id_self);
// otherwise, form the tuple relative to the callee decl.
return std::make_tuple(
verbForInvoking(callee),
callee->getDescriptiveKindName(callee->getDescriptiveKind()),
callee->getName()
);
}
// handle other non-call blames.
switch (blame->getKind()) {
case SILInstructionKind::CopyValueInst:
return std::make_tuple("making a copy of", "", ctx.Id_self);
default:
return std::make_tuple("this use of", "", ctx.Id_self);
}
}
/// Emits errors for all isolated uses of `self` in the given block.
/// \param blame the instruction to blame for introducing non-isolation.
/// If not provided, a suitable instruction will be automatically found using a
/// search.
/// \param info the AnalysisInfo corresponding to the function containing this
/// block.
void Info::diagnoseAll(AnalysisInfo &info, bool forDeinit,
SILInstruction* blame) {
if (propertyUses.empty())
return;
auto *fn = info.getFunction();
auto &ctx = fn->getASTContext();
// Disable these diagnostics in deinitializers unless complete checking is
// enabled.
if (forDeinit && ctx.LangOpts.StrictConcurrencyLevel
!= StrictConcurrency::Complete)
return;
// Blame that is valid for the first property use is valid for all uses
// in this block.
if (!blame)
blame = info.findNonisolatedBlame(firstPropertyUse());
// if needed, find the blame inside of the defer callee.
if (auto *callee = getCallee(blame)) {
if (info.haveDeferInfo(callee)) {
auto &defer = info.getOrCreateDeferInfo(callee);
assert(defer.normalReturn && "noreturn defer should never be blamed!");
auto *retBlk = defer.normalReturn->first;
blame = defer.findNonisolatedBlame(retBlk->getTerminator());
}
}
auto &diag = fn->getASTContext().Diags;
SILLocation blameLoc = blame->getDebugLocation().getLocation();
for (auto *use : propertyUses) {
// If the illegal use is a call to a defer, then recursively diagnose
// all of the defer's uses, if this is the first time encountering it.
if (auto *callee = getCallee(use)) {
if (info.haveDeferInfo(callee)) {
auto &defer = info.getOrCreateDeferInfo(callee);
if (defer.setNonisolatedStart()) {
defer.diagnoseEntireFunction(blame);
}
continue;
}
// Init accessor `setter` use.
auto *accessor =
cast<AccessorDecl>(callee->getLocation().getAsDeclContext());
auto illegalLoc = use->getDebugLocation().getLocation();
diag.diagnose(illegalLoc.getSourceLoc(),
diag::isolated_property_mutation_in_nonisolated_context,
accessor->getStorage(), accessor->isSetter())
.warnUntilSwiftVersion(6);
continue;
}
assert(isa<RefElementAddrInst>(use) && "only expecting one kind of instr.");
SILLocation illegalLoc = use->getDebugLocation().getLocation();
VarDecl *var = cast<RefElementAddrInst>(use)->getField();
diag.diagnose(illegalLoc.getSourceLoc(), diag::isolated_after_nonisolated,
forDeinit, var->getDescriptiveKind(), var->getName())
.highlight(illegalLoc.getSourceRange())
.warnUntilSwiftVersion(6);
// after <verb><adjective> <subject>, ... can't use self anymore, etc ...
// example:
// after calling function 'hello()', ...
StringRef verb;
StringRef adjective;
DeclName subject;
std::tie(verb, adjective, subject) = describe(blame);
diag.diagnose(blameLoc.getSourceLoc(), diag::nonisolated_blame,
forDeinit, verb, adjective, subject)
.highlight(blameLoc.getSourceRange());
}
}
// MARK: analysis
/// \returns true iff the access is concurrency-safe in a nonisolated context
/// without an await.
static bool accessIsConcurrencySafe(ModuleDecl *module,
RefElementAddrInst *inst) {
VarDecl *var = inst->getField();
// must be accessible from nonisolated.
return isLetAccessibleAnywhere(module, var);
}
/// \returns true iff the ref_element_addr instruction is only used
/// to deinitialize the referenced element.
static bool onlyDeinitAccess(RefElementAddrInst *inst) {
if (auto operand = inst->getSingleUse()) {
if (auto *access = dyn_cast<BeginAccessInst>(operand->getUser())) {
return access->getAccessKind() == SILAccessKind::Deinit;
}
}
return false;
}
/// Checks that the accessed element conforms to Sendable; emitting a
/// diagnostic if it is not Sendable. The diagnostic assumes that the access
/// is happening in a deinit that uses flow-isolation.
/// \returns true iff a diagnostic was emitted for this reference.
static bool diagnoseNonSendableFromDeinit(RefElementAddrInst *inst) {
auto dc = inst->getFunction()->getDeclContext();
// For historical reasons, only diagnose this issue in strict mode.
if (dc->getASTContext().LangOpts.StrictConcurrencyLevel
!= StrictConcurrency::Complete)
return false;
return swift::diagnoseNonSendableFromDeinit(
inst->getLoc().getSourceLoc(),
inst->getField(),
dc);
}
class OperandWorklist {
SmallVector<Operand *, 32> worklist;
SmallPtrSet<Operand *, 16> visited;
public:
Operand *pop() {
if (worklist.empty())
return nullptr;
return worklist.pop_back_val();
}
void pushIfNotVisited(Operand *op) {
if (visited.insert(op).second) {
worklist.push_back(op);
}
}
void pushUsesOfValueIfNotVisited(SILValue value) {
for (Operand *use : value->getUses()) {
pushIfNotVisited(use);
}
}
};
/// Analyzes a function for uses of `self` and records the kinds of isolation
/// required.
/// \param selfParam the parameter of \c getFunction() that should be
/// treated as \c self
void AnalysisInfo::analyze(const SILArgument *selfParam) {
assert(selfParam && "analyzing a function with no self?");
ModuleDecl *module = getFunction()->getModule().getSwiftModule();
// Use a worklist to track the uses left to be searched.
OperandWorklist worklist;
// Seed with direct users of `self`
worklist.pushUsesOfValueIfNotVisited(selfParam);
while (Operand *operand = worklist.pop()) {
// A type-dependent use of `self` is an instruction that contains the
// DynamicSelfType. These instructions do not access any protected
// state.
if (operand->isTypeDependent())
continue;
SILInstruction *user = operand->getUser();
// First, check if this is an apply that involves `self`
if (auto apply = ApplySite::isa(user)) {
// Check if the callee is a function representing a defer block.
if (SILFunction *callee = apply.getCalleeFunction()) {
if (auto *dc = callee->getDeclContext()) {
if (auto *decl = dyn_cast_or_null<FuncDecl>(dc->getAsDecl())) {
if (decl->isDeferBody()) {
// If we need to analyze the defer first, do so.
if (!haveDeferInfo(callee)) {
// NOTE: the defer function is not like a method, because it
// doesn't satisfy hasSelfParam().
auto const* calleeSelfParam =
callee->getArgument(apply.getAppliedArgIndex(*operand));
// Recursion depth is bounded by the lexical nesting of
// defer blocks in the input program.
auto &defer = getOrCreateDeferInfo(callee);
defer.analyze(calleeSelfParam);
defer.solve();
}
auto const& defer = getOrCreateDeferInfo(callee);
// A defer effectively has one exit block, because it can't throw.
// Otherwise, it may never return (e.g., fatalError).
// So, we say that this instruction generates nonisolation only
// if it can return normally, and if it does, it carries
// nonisolation.
if (defer.normalReturn) {
if (defer.normalReturn->second == State::Nonisolated) {
markNonIsolated(user);
}
}
// If the defer body has any stored property uses, we record that
// in the parent by declaring this call-site being a property use.
if (defer.hasPropertyUse())
markPropertyUse(user);
continue;
}
}
}
// Detect and handle use of init accessor properties.
if (callee->hasLocation()) {
auto loc = callee->getLocation();
if (auto *accessor =
dyn_cast_or_null<AccessorDecl>(loc.getAsDeclContext())) {
auto *storage = accessor->getStorage();
// Note 'nonisolated' property use.
if (storage->getAttrs().hasAttribute<NonisolatedAttr>()) {
markNonIsolated(user);
continue;
}
// Init accessor is used exclusively for initialization
// of properties while 'self' is not fully initialized.
if (accessor->isInitAccessor()) {
markNonIsolated(user);
continue;
}
// Otherwise if this is an init accessor property, it's either
// a call to a getter or a setter and should be treated like
// an isolated computed property reference.
if (storage->hasInitAccessor()) {
markPropertyUse(user);
continue;
}
}
}
}
// For all other call-sites, uses of `self` are nonisolated.
markNonIsolated(user);
continue;
}
// Handle non-ApplySite instructions.
switch (user->getKind()) {
// Look for a property access.
// Sadly, formal accesses are not always emitted by SILGen, particularly,
// within the initializers we care about. So we rely on ref_element_addr.
case SILInstructionKind::RefElementAddrInst: {
RefElementAddrInst *refInst = cast<RefElementAddrInst>(user);
// skip auto-generated deinit accesses.
if (onlyDeinitAccess(refInst))
continue;
// skip known-safe accesses.
if (accessIsConcurrencySafe(module, refInst))
continue;
// emit a diagnostic and skip if it's non-sendable in a deinit
if (forDeinit && diagnoseNonSendableFromDeinit(refInst))
continue;
markPropertyUse(user);
break;
}
// Look through certian kinds of single-value instructions.
case SILInstructionKind::CopyValueInst:
// TODO: If we had some actual escape analysis information, we could
// avoid marking a trivial copy as a nonisolated use, since it doesn't
// actually escape the function. We have to be conservative here
// and assume it might.
markNonIsolated(user);
break;
case SILInstructionKind::BeginAccessInst:
case SILInstructionKind::BeginBorrowInst:
case SILInstructionKind::EndInitLetRefInst: {
auto *svi = cast<SingleValueInstruction>(user);
worklist.pushUsesOfValueIfNotVisited(svi);
break;
}
case SILInstructionKind::BranchInst: {
auto *arg = cast<BranchInst>(user)->getArgForOperand(operand);
worklist.pushUsesOfValueIfNotVisited(arg);
break;
}
default:
// don't follow this instruction.
LLVM_DEBUG(llvm::dbgs() << DEBUG_TYPE << " def-use walk skipping: "
<< *user);
break;
}
}
}
/// Initialize and solve the dataflow problem, assuming the entry block starts
/// isolated.
void AnalysisInfo::solve() {
SILFunction *fn = getFunction();
SILBasicBlock *returnBlk = nullptr;
// NOTE: if the starting isolation is nonisolated, the solution is trivial.
// Since we don't expect calls to solve in that situation, I haven't
// implemented that
// Initialize the forward dataflow problem.
for (auto pair : flow) {
SILBasicBlock *blk = &(pair.block);
auto &data = pair.data;
// record the return block
if (isa<ReturnInst>(blk->getTerminator())) {
assert(returnBlk == nullptr); // should only be one!
returnBlk = blk;
}
// Set everything to Bottom.
data.entrySet.reset();
data.genSet.reset();
data.killSet.reset();
data.exitSet.reset();
if (blk == fn->getEntryBlock())
data.entrySet.set(startingIsolation);
// A nonisolated use "kills" isolation and generates nonisolation.
if (this->operator[](blk).hasNonisolatedUse()) {
data.killSet.set(State::Isolated);
data.genSet.set(State::Nonisolated);
}
}
// Solve using a union so that Top represents a conflict.
flow.solveForwardWithUnion();
// If this function can return normally, update the outgoing isolation
// in that case. This is needed to implement `defer`.
if (returnBlk) {
auto &returnInfo = flow[returnBlk];
if (returnInfo.exitSet[State::Nonisolated])
normalReturn = std::make_pair(returnBlk, State::Nonisolated);
else
normalReturn = std::make_pair(returnBlk, State::Isolated);
}
}
/// Enforces isolation rules, given the flow and block-local information.
void AnalysisInfo::verifyIsolation() {
// go through all the blocks.
for (auto entry : *this) {
auto &block = entry.block;
auto &data = entry.data;
auto &flowInfo = flow[&block];
// If the block has no isolated uses, then skip it.
if (data.propertyUses.empty())
continue;
// If flow-analysis determined that we might be `nonisolated` coming
// into this block, then all isolated uses in this block are invalid.
if (flowInfo.entrySet[State::Nonisolated]) {
data.diagnoseAll(*this, forDeinit);
continue;
}
// Otherwise, we must be starting off isolated.
assert(flowInfo.entrySet[State::Isolated]);
// If this block doesn't introduce nonisolation, then we can skip it.
if (data.nonisolatedUses.empty()) {
// make sure flow analysis agrees.
assert(flowInfo.exitSet[State::Nonisolated] == 0);
continue;
}
// Finally, we must scan the block to determine which isolated uses
// are illegal. If isolated uses appear after nonisolated ones, then
// that is an error. So, our strategy is to remove the valid isolated
// uses, until we find the first nonisolated use. Then, we can simply
// diagnose the remaining uses.
SILInstruction *nonisolatedUse = nullptr;
auto current = block.begin();
while (current != block.end()) {
SILInstruction *inst = &*current;
auto result = data.nonisolatedUses.find(inst);
if (result != data.nonisolatedUses.end()) {
nonisolatedUse = *result;
break;
}
data.propertyUses.erase(inst);
current++;
}
assert(nonisolatedUse && "should have found a use!");
data.diagnoseAll(*this, forDeinit, nonisolatedUse);
}
// recursively verify isolation of defer functions.
for (auto &entry : deferBlocks) {
// skip those with nonisolated start, since we've already diagnosed those.
if (entry.second->hasNonisolatedStart())
continue;
entry.second->verifyIsolation();
}
}
// MARK: high-level setup
/// Performs flow-sensitive actor-isolation checking on the given SILFunction.
void checkFlowIsolation(SILFunction *fn) {
assert(fn->hasSelfParam() && "cannot analyze without a self param!");
// Step 1 -- Analyze uses of `self` within the function.
AnalysisInfo info(fn);
info.analyze(fn->getSelfArgument());
// Step 2 -- Initialize and solve the dataflow problem.
info.solve();
LLVM_DEBUG(info.dump());
// Step 3 -- With the information gathered, check for flow-isolation issues.
info.verifyIsolation();
}
/// The FlowIsolation pass performs flow-sensitive actor-isolation checking in
/// the body of actor member functions that treat `self` as `nonisolated` after
/// the first `nonisolated` use. This pass uses a simple forward dataflow
/// analysis to track these changes and emits diagnostics if an isolated use of
/// `self` appears when `self` may be `nonisolated` at that point in the
/// function.
class FlowIsolation : public SILFunctionTransform {
/// The entry point to the checker.
void run() override {
SILFunction *fn = getFunction();
// Don't rerun diagnostics on deserialized functions.
if (fn->wasDeserializedCanonical())
return;
// Look for functions that use flow-isolation.
if (auto *dc = fn->getDeclContext())
if (auto *afd = dyn_cast_or_null<AbstractFunctionDecl>(dc->getAsDecl()))
if (usesFlowSensitiveIsolation(afd))
checkFlowIsolation(fn);
return;
}
}; // class
} // anonymous namespace
/// This pass is known to depend on the following passes having run before it:
/// - NoReturnFolding
SILTransform *swift::createFlowIsolation() {
return new FlowIsolation();
}
|