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 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
|
//===--- ClosureLifetimeFixup.cpp - Fixup the lifetime of closures --------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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 "closure-lifetime-fixup"
#include "swift/Basic/Defer.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
#include "swift/SILOptimizer/Utils/StackNesting.h"
#include "llvm/Support/CommandLine.h"
llvm::cl::opt<bool> DisableConvertEscapeToNoEscapeSwitchEnumPeephole(
"sil-disable-convert-escape-to-noescape-switch-peephole",
llvm::cl::init(false),
llvm::cl::desc(
"Disable the convert_escape_to_noescape switch enum peephole. "),
llvm::cl::Hidden);
using namespace swift;
/// Given an optional diamond, return the bottom of the diamond.
///
/// That is given that sei is in bb0,
///
/// /---> bb1 ---\
/// / \
/// bb0 ---> bb3
/// \ /
/// \---> bb2 ---/
///
/// this routine will return bb3.
static SILBasicBlock *getOptionalDiamondSuccessor(SwitchEnumInst *sei) {
auto numSuccs = sei->getNumSuccessors();
if (numSuccs != 2)
return nullptr;
auto *succSome = sei->getCase(0).second;
auto *succNone = sei->getCase(1).second;
if (succSome->args_size() != 1)
std::swap(succSome, succNone);
if (succSome->args_size() != 1 || succNone->args_size() != 0)
return nullptr;
auto *succ = succSome->getSingleSuccessorBlock();
if (!succ)
return nullptr;
if (succNone == succ)
return succ;
succNone = succNone->getSingleSuccessorBlock();
if (succNone == succ)
return succ;
if (succNone == nullptr)
return nullptr;
succNone = succNone->getSingleSuccessorBlock();
if (succNone == succ)
return succ;
return nullptr;
}
/// Find a safe insertion point for closure destruction. We might create a
/// closure that captures self in deinit of self. In this situation it is not
/// safe to destroy the closure after we called super deinit. We have to place
/// the closure destruction before that call.
///
/// %deinit = objc_super_method %0 : $C, #A.deinit!deallocator.foreign
/// %super = upcast %0 : $C to $A
/// apply %deinit(%super) : $@convention(objc_method) (A) -> ()
/// end_lifetime %super : $A
static SILInstruction *getDeinitSafeClosureDestructionPoint(SILBasicBlock *bb) {
for (auto &i : llvm::reverse(*bb)) {
if (auto *endLifetime = dyn_cast<EndLifetimeInst>(&i)) {
auto *superInstance = endLifetime->getOperand()->getDefiningInstruction();
assert(superInstance && "Expected an instruction");
return superInstance;
}
}
return bb->getTerminator();
}
static void findReachableExitBlocks(SILInstruction *i,
SmallVectorImpl<SILBasicBlock *> &result) {
BasicBlockWorklist worklist(i->getParent());
while (SILBasicBlock *bb = worklist.pop()) {
if (bb->getTerminator()->isFunctionExiting()) {
result.push_back(bb);
continue;
}
for (SILBasicBlock *succ : bb->getSuccessors()) {
worklist.pushIfNotVisited(succ);
}
}
}
/// We use this to ensure that we properly handle recursive cases by revisiting
/// phi nodes that we are tracking. This just makes it easier to reproduce in a
/// test case.
static llvm::cl::opt<bool> ReverseInitialWorklist(
"sil-closure-lifetime-fixup-reverse-phi-order", llvm::cl::init(false),
llvm::cl::desc(
"Reverse the order in which we visit phis for testing purposes"),
llvm::cl::Hidden);
// Finally, we need to prune phis inserted by the SSA updater that
// only take the .none from the entry block. This means that they are
// not actually reachable from the .some() so we know that we do not
// need to lifetime extend there at all. As an additional benefit, we
// eliminate the need to balance these arguments to satisfy the
// ownership verifier. This occurs since arguments are a place in SIL
// where the trivialness of an enums case is erased.
static void
cleanupDeadTrivialPhiArgs(SILValue initialValue,
SmallVectorImpl<SILPhiArgument *> &insertedPhis) {
// Just for testing purposes.
if (ReverseInitialWorklist) {
std::reverse(insertedPhis.begin(), insertedPhis.end());
}
SmallVector<SILArgument *, 8> worklist(insertedPhis.begin(),
insertedPhis.end());
sortUnique(insertedPhis);
SmallVector<SILValue, 8> incomingValues;
while (!worklist.empty()) {
// Clear the incoming values array after each iteration.
SWIFT_DEFER { incomingValues.clear(); };
auto *phi = worklist.pop_back_val();
{
auto it = lower_bound(insertedPhis, phi);
if (it == insertedPhis.end() || *it != phi)
continue;
}
// TODO: When we split true phi arguments from transformational terminators,
// this will always succeed and the assert can go away.
bool foundPhiValues = phi->getIncomingPhiValues(incomingValues);
(void)foundPhiValues;
assert(foundPhiValues && "Should always have 'true' phi arguments since "
"these were inserted by the SSA updater.");
if (llvm::any_of(incomingValues,
[&](SILValue v) { return v != initialValue; }))
continue;
// Remove it from our insertedPhis list to prevent us from re-visiting this.
{
auto it = lower_bound(insertedPhis, phi);
assert((it != insertedPhis.end() && *it == phi) &&
"Should have found the phi");
insertedPhis.erase(it);
}
// See if any of our users are branch or cond_br. If so, we may have
// exposed additional unneeded phis. Add it back to the worklist in such a
// case.
for (auto *op : phi->getUses()) {
auto *user = op->getUser();
if (!isa<BranchInst>(user) && !isa<CondBranchInst>(user))
continue;
auto *termInst = cast<TermInst>(user);
for (auto succBlockArgList : termInst->getSuccessorBlockArgumentLists()) {
llvm::copy_if(succBlockArgList, std::back_inserter(worklist),
[&](SILArgument *succArg) -> bool {
auto it = lower_bound(insertedPhis, succArg);
return it != insertedPhis.end() && *it == succArg;
});
}
}
// Then RAUW the phi with the entryBlockOptionalNone and erase the
// argument.
phi->replaceAllUsesWith(initialValue);
erasePhiArgument(phi->getParent(), phi->getIndex(),
/*cleanupDeadPhiOp*/ false);
}
}
/// Extend the lifetime of the convert_escape_to_noescape's operand to the end
/// of the function.
/// Create a copy of the escaping closure operand and end its lifetime at
/// function exits. Since, the cvt may not be dominating function exits, we
/// need to create an optional and use the SSAUpdater to extend the lifetime. In
/// order to prevent the optional being optimized away, create a borrow scope
/// and insert a mark_dependence of the non escaping closure on the borrow.
/// NOTE: Since we are lifetime extending a copy that we have introduced, we do
/// not need to consider destroy_value emitted by SILGen unlike
/// copy_block_without_escaping which consumes its sentinel parameter. Unlike
/// that case where we have to consider that destroy_value, we have a simpler
/// time here.
static void extendLifetimeToEndOfFunction(SILFunction &fn,
ConvertEscapeToNoEscapeInst *cvt,
SILSSAUpdater &updater) {
auto escapingClosure = cvt->getOperand();
auto escapingClosureTy = escapingClosure->getType();
auto optionalEscapingClosureTy = SILType::getOptionalType(escapingClosureTy);
auto loc = RegularLocation::getAutoGeneratedLocation();
SmallVector<SILBasicBlock *, 4> exitingBlocks;
fn.findExitingBlocks(exitingBlocks);
auto createLifetimeEnd = [](SILLocation loc, SILInstruction *insertPt,
SILValue value) {
SILBuilderWithScope builder(insertPt);
if (value->getOwnershipKind() == OwnershipKind::Owned) {
builder.emitDestroyOperation(loc, value);
return;
}
builder.emitEndBorrowOperation(loc, value);
};
auto createLifetimeEndAtFunctionExits =
[&](std::function<SILValue(SILBasicBlock *)> getValue) {
for (auto *block : exitingBlocks) {
auto *safeDestructionPoint =
getDeinitSafeClosureDestructionPoint(block);
createLifetimeEnd(loc, safeDestructionPoint, getValue(block));
}
};
// If our cvt is in the initial block, we do not need to use the SSA updater
// since we know cvt cannot be in a loop and must dominate all exits
// (*). Just insert a copy of the escaping closure at the cvt and destroys at
// the exit blocks of the function.
//
// (*) In fact we can't use the SILSSAUpdater::GetValueInMiddleOfBlock.
if (cvt->getParent() == cvt->getFunction()->getEntryBlock()) {
auto *copy = SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(copy);
createLifetimeEndAtFunctionExits([©](SILBasicBlock *) { return copy; });
return;
}
// Create a copy of the convert_escape_to_no_escape.
// NOTE: The SSAUpdater does not support providing multiple values in the same
// block without extra work. So the fact that cvt is not in the entry block
// means that we don't have to worry about overwriting the .none value.
auto *copy = SILBuilderWithScope(cvt).createCopyValue(loc, escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(copy);
// Create an optional some to extend the lifetime of copy until function
// exits.
SILBuilderWithScope lifetimeExtendBuilder(std::next(cvt->getIterator()));
auto *optionalSome = lifetimeExtendBuilder.createOptionalSome(
loc, copy, optionalEscapingClosureTy);
// Create a borrow scope and a mark_dependence to prevent the enum being
// optimized away.
auto *borrow = lifetimeExtendBuilder.createBeginBorrow(loc, optionalSome);
auto *mdi =
lifetimeExtendBuilder.createMarkDependence(loc, cvt, borrow,
MarkDependenceKind::Escaping);
// Replace all uses of the non escaping closure with mark_dependence
SmallVector<Operand *, 4> convertUses;
for (auto *cvtUse : cvt->getUses()) {
convertUses.push_back(cvtUse);
}
for (auto *cvtUse : convertUses) {
auto *cvtUser = cvtUse->getUser();
if (cvtUser == mdi)
continue;
cvtUser->setOperand(cvtUse->getOperandNumber(), mdi);
}
auto fixupSILForLifetimeExtension = [&](SILValue value, SILValue entryValue) {
// Use SSAUpdater to find insertion points for lifetime ends.
updater.initialize(value->getFunction(), optionalEscapingClosureTy,
value->getOwnershipKind());
SmallVector<SILPhiArgument *, 8> insertedPhis;
updater.setInsertedPhis(&insertedPhis);
updater.addAvailableValue(fn.getEntryBlock(), entryValue);
updater.addAvailableValue(value->getParentBlock(), value);
{
// Since value maybe in a loop, insert an extra lifetime end. Since we
// used our enum value, this is safe.
SILValue midValue =
updater.getValueInMiddleOfBlock(value->getParentBlock());
createLifetimeEnd(loc, cvt, midValue);
}
// Insert lifetime ends.
createLifetimeEndAtFunctionExits([&updater](SILBasicBlock *block) {
return updater.getValueAtEndOfBlock(block);
});
// Prune the phis inserted by the SSA updater that only take
// the .none from the entry block.
// TODO: Should we sort inserted phis before or after we initialize
// the worklist or maybe backwards? We should investigate how the
// SSA updater adds phi nodes to this list to resolve this question.
cleanupDeadTrivialPhiArgs(entryValue, insertedPhis);
};
// Create an optional none at the function entry.
auto *optionalNone = SILBuilderWithScope(fn.getEntryBlock()->begin())
.createOptionalNone(loc, optionalEscapingClosureTy);
auto *borrowNone = SILBuilderWithScope(optionalNone->getNextInstruction())
.createBeginBorrow(loc, optionalNone);
// Use the SSAUpdater to create lifetime ends for the copy and the borrow.
fixupSILForLifetimeExtension(borrow, borrowNone);
fixupSILForLifetimeExtension(optionalSome, optionalNone);
}
static SILInstruction *lookThroughRebastractionUsers(
SILInstruction *inst,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized) {
if (inst == nullptr)
return nullptr;
// Try a cached lookup.
auto res = memoized.find(inst);
if (res != memoized.end())
return res->second;
// Cache recursive results.
auto memoizeResult = [&](SILInstruction *from, SILInstruction *toResult) {
memoized[from] = toResult;
return toResult;
};
auto getSingleNonDebugNonRefCountUser =
[](SILValue v) -> SILInstruction* {
SILInstruction *singleNonDebugNonRefCountUser = nullptr;
for (auto *use : getNonDebugUses(v)) {
auto *user = use->getUser();
if (onlyAffectsRefCount(user))
continue;
if (isa<EndBorrowInst>(user))
continue;
if (singleNonDebugNonRefCountUser) {
return nullptr;
}
singleNonDebugNonRefCountUser = user;
}
return singleNonDebugNonRefCountUser;
};
// If we have a convert_function, just look at its user.
if (auto *cvt = dyn_cast<ConvertFunctionInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(cvt), memoized));
if (auto *cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(cvt), memoized));
// If we have a partial_apply user look at its single (non release) user.
if (auto *pa = dyn_cast<PartialApplyInst>(inst))
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(pa), memoized));
// TODO: If the single user is a borrow, then generally the lifetime of that
// borrow ought to delineate the lifetime of the closure. But some codegen
// patterns in SILGen will try to notionally lifetime-extend the value by
// copying it and putting the lifetime on the copy. So look at the single
// user of the borrow, if any, to determine the lifetime this should have.
if (auto borrow = dyn_cast<BeginBorrowInst>(inst)) {
return memoizeResult(inst, lookThroughRebastractionUsers(
getSingleNonDebugNonRefCountUser(borrow), memoized));
}
return inst;
}
/// Insert a mark_dependence for any non-trivial argument of a partial_apply.
static SILValue insertMarkDependenceForCapturedArguments(PartialApplyInst *pai,
SILBuilder &b) {
SILValue curr(pai);
// Mark dependence on all non-trivial arguments that weren't borrowed.
for (auto &arg : pai->getArgumentOperands()) {
if (isa<BeginBorrowInst>(arg.get())
|| arg.get()->getType().isTrivial(*pai->getFunction()))
continue;
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(arg.get()))
if (m->hasGuaranteedInitialKind())
continue;
curr = b.createMarkDependence(pai->getLoc(), curr, arg.get(),
MarkDependenceKind::NonEscaping);
}
return curr;
}
/// Returns the (single) "endAsyncLetLifetime" builtin if \p startAsyncLet is a
/// "startAsyncLetWithLocalBuffer" builtin.
static BuiltinInst *getEndAsyncLet(BuiltinInst *startAsyncLet) {
if (startAsyncLet->getBuiltinKind() != BuiltinValueKind::StartAsyncLetWithLocalBuffer)
return nullptr;
BuiltinInst *endAsyncLet = nullptr;
for (Operand *op : startAsyncLet->getUses()) {
auto *endBI = dyn_cast<BuiltinInst>(op->getUser());
if (endBI && endBI->getBuiltinKind() == BuiltinValueKind::EndAsyncLetLifetime) {
// At this stage of the pipeline, it's always the case that a
// startAsyncLet has an endAsyncLet: that's how SILGen generates it.
// Just to be on the safe side, do this check.
if (endAsyncLet)
return nullptr;
endAsyncLet = endBI;
}
}
return endAsyncLet;
}
/// Call the \p insertFn with a builder at all insertion points after
/// a closure is used by \p closureUser.
static void insertAfterClosureUser(SILInstruction *closureUser,
function_ref<void(SILBuilder &)> insertFn) {
// Don't insert any destroy or deallocation right before an unreachable.
// It's not needed an will only add up to code size.
auto insertAtNonUnreachable = [&](SILBuilder &builder) {
if (isa<UnreachableInst>(builder.getInsertionPoint()))
return;
insertFn(builder);
};
{
SILInstruction *userForBorrow = closureUser;
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(userForBorrow))
if (m->hasGuaranteedInitialKind())
if (auto *svi = dyn_cast<SingleValueInstruction>(m->getOperand()))
userForBorrow = svi;
if (auto *beginBorrow = dyn_cast<BeginBorrowInst>(userForBorrow)) {
// Insert everywhere after the borrow is ended.
SmallVector<EndBorrowInst *, 4> endBorrows;
for (auto eb : beginBorrow->getEndBorrows()) {
endBorrows.push_back(eb);
}
for (auto eb : endBorrows) {
SILBuilderWithScope builder(std::next(eb->getIterator()));
insertAtNonUnreachable(builder);
}
return;
}
}
if (auto *startAsyncLet = dyn_cast<BuiltinInst>(closureUser)) {
BuiltinInst *endAsyncLet = getEndAsyncLet(startAsyncLet);
if (!endAsyncLet)
return;
SILBuilderWithScope builder(std::next(endAsyncLet->getIterator()));
insertAtNonUnreachable(builder);
return;
}
FullApplySite fas = FullApplySite::isa(closureUser);
assert(fas);
fas.insertAfterApplication(insertAtNonUnreachable);
}
static SILValue skipConvert(SILValue v) {
auto *cvt = dyn_cast<ConvertFunctionInst>(v);
if (!cvt)
return v;
auto *pa = dyn_cast<PartialApplyInst>(cvt->getOperand());
if (!pa || !pa->hasOneUse())
return v;
return pa;
}
static SILAnalysis::InvalidationKind
analysisInvalidationKind(const bool &modifiedCFG) {
return modifiedCFG ? SILAnalysis::InvalidationKind::FunctionBody
: SILAnalysis::InvalidationKind::CallsAndInstructions;
}
/// Find the stack closure's lifetime ends. This should be indicated either by
/// direct destruction of the closure after its application, or the destruction
/// of its consuming use, which should be either another function conversion
/// or a partial_apply into a closure that will also be imminently transformed
/// into a stack partial apply. The lifetime of the closure should not escape
/// the current function or we wouldn't be able to embark on this transform.
static void
collectStackClosureLifetimeEnds(SmallVectorImpl<SILInstruction *> &lifetimeEnds,
SILValue v) {
for (Operand *consume : v->getConsumingUses()) {
SILInstruction *consumer = consume->getUser();
if (isa<DestroyValueInst>(consumer)) {
lifetimeEnds.push_back(consumer);
continue;
}
if (auto pa = dyn_cast<PartialApplyInst>(consumer)) {
// The closure may be captured into another partial_apply (usually
// a reabstraction thunk, but possibly a nested closure-in-closure).
// This other partial_apply ought to be imminently changing into
// a nonescaping closure as well, so we want the end of the
// `convert_escape_to_noescape` operation's lifetime rather than the
// original escaping closure's.
//
// Any partial_apply already converted to a stack closure should have
// also been converted to borrowing its captures.
assert(!pa->isOnStack());
SILValue singlePAUser = pa;
do {
SILInstruction *nextUser = nullptr;
for (auto use : singlePAUser->getUses()) {
if (isa<DestroyValueInst>(use->getUser())) {
continue;
}
assert(!nextUser && "more than one non-destroying use?!");
nextUser = use->getUser();
}
assert(nextUser && nextUser->getNumResults() == 1
&& "partial_apply capturing a nonescaping closure that isn't"
"itself nonescaping?!");
singlePAUser = nextUser->getResult(0);
} while (!isa<ConvertEscapeToNoEscapeInst>(singlePAUser));
auto convert = cast<ConvertEscapeToNoEscapeInst>(singlePAUser);
collectStackClosureLifetimeEnds(lifetimeEnds, convert);
continue;
}
// There shouldn't be any other consuming uses of the value that aren't
// forwarding.
assert(consumer->hasResults());
for (auto result : consumer->getResults()) {
collectStackClosureLifetimeEnds(lifetimeEnds, result);
}
}
}
/// Rewrite a partial_apply convert_escape_to_noescape sequence with a single
/// apply/try_apply user to a partial_apply [stack] terminated with a
/// dealloc_stack placed after the apply.
///
/// %p = partial_apply %f(%a, %b)
/// %ne = convert_escape_to_noescape %p
/// apply %f2(%p)
/// destroy_value %p
///
/// =>
///
/// %ab = begin_borrow %a
/// %bb = begin_borrow %b
/// %p = partial_apply [stack] %f(%aa, %bb)
/// apply %f2(%p)
/// destroy_value %p
/// end_borrow %bb
/// end_borrow %aa
static SILValue tryRewriteToPartialApplyStack(
ConvertEscapeToNoEscapeInst *cvt, SILInstruction *closureUser,
DominanceAnalysis *dominanceAnalysis, InstructionDeleter &deleter,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
const bool &modifiedCFG) {
auto *origPA = dyn_cast<PartialApplyInst>(skipConvert(cvt->getOperand()));
if (!origPA)
return SILValue();
auto *convertOrPartialApply = cast<SingleValueInstruction>(origPA);
if (cvt->getOperand() != origPA)
convertOrPartialApply = cast<ConvertFunctionInst>(cvt->getOperand());
// Whenever we delete an instruction advance the iterator and remove the
// instruction from the memoized map.
auto saveDeleteInst = [&](SILInstruction *i) {
memoized.erase(i);
deleter.forceDelete(i);
};
// Look for a single non ref count user of the partial_apply.
SmallVector<SILInstruction *, 8> refCountInsts;
SILInstruction *singleNonDebugNonRefCountUser = nullptr;
for (auto *use : getNonDebugUses(convertOrPartialApply)) {
auto *user = use->getUser();
if (onlyAffectsRefCount(user)) {
refCountInsts.push_back(user);
continue;
}
if (singleNonDebugNonRefCountUser)
return SILValue();
singleNonDebugNonRefCountUser = user;
}
SILBuilderWithScope b(cvt);
// Remove the original destroy of the partial_apply, if any, since the
// nonescaping closure's lifetime becomes the lifetime of the new
// partial_apply.
if (auto destroy = convertOrPartialApply->getSingleUserOfType<DestroyValueInst>()) {
saveDeleteInst(destroy);
}
// Borrow the arguments that need borrowing.
SmallVector<MoveOnlyWrapperToCopyableValueInst *, 8>
noImplicitCopyWrapperToDelete;
SmallVector<SILValue, 8> args;
for (Operand &arg : origPA->getArgumentOperands()) {
auto argTy = arg.get()->getType();
if (!argTy.isAddress() && !argTy.isTrivial(*cvt->getFunction())) {
SILValue argValue = arg.get();
bool foundNoImplicitCopy = false;
if (auto *mmci = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(argValue)) {
if (mmci->hasOwnedInitialKind() && mmci->hasOneUse()) {
foundNoImplicitCopy = true;
argValue = mmci->getOperand();
noImplicitCopyWrapperToDelete.push_back(mmci);
}
}
SILValue borrow = b.createBeginBorrow(origPA->getLoc(), argValue);
if (foundNoImplicitCopy)
borrow = b.createGuaranteedMoveOnlyWrapperToCopyableValue(
origPA->getLoc(), borrow);
args.push_back(borrow);
} else {
args.push_back(arg.get());
}
}
// The convert_escape_to_noescape is the only user of the partial_apply.
// Convert to a partial_apply [stack].
auto newPA = b.createPartialApply(
origPA->getLoc(), origPA->getCallee(), origPA->getSubstitutionMap(), args,
origPA->getCalleeConvention(), origPA->getResultIsolation(),
PartialApplyInst::OnStackKind::OnStack);
// Insert mark_dependence for any non-trivial address operands to the
// partial_apply.
auto closure = insertMarkDependenceForCapturedArguments(newPA, b);
SILValue closureOp = closure;
// Optionally, replace the convert_function instruction.
if (auto *convert = dyn_cast<ConvertFunctionInst>(convertOrPartialApply)) {
/* DEBUG
llvm::errs() << "=== replacing conversion\n";
convert->dumpInContext();
*/
auto origTy = convert->getType().castTo<SILFunctionType>();
auto origWithNoEscape = SILType::getPrimitiveObjectType(
origTy->getWithExtInfo(origTy->getExtInfo().withNoEscape()));
closureOp = b.createConvertFunction(convert->getLoc(), closure,
origWithNoEscape, false);
/* DEBUG
llvm::errs() << "--- with\n";
closureOp->dumpInContext();
*/
}
// Replace the convert_escape_to_noescape uses with the new
// partial_apply [stack].
cvt->replaceAllUsesWith(closureOp);
saveDeleteInst(cvt);
// Delete the ref count operations on the original partial_apply.
for (auto *refInst : refCountInsts)
saveDeleteInst(refInst);
convertOrPartialApply->replaceAllUsesWith(newPA);
if (convertOrPartialApply != origPA)
saveDeleteInst(convertOrPartialApply);
saveDeleteInst(origPA);
// Delete the mmci of the origPA.
while (!noImplicitCopyWrapperToDelete.empty())
saveDeleteInst(noImplicitCopyWrapperToDelete.pop_back_val());
ApplySite site(newPA);
SILFunctionConventions calleeConv(site.getSubstCalleeType(),
newPA->getModule());
// Since we create temporary allocation for in_guaranteed captures during SILGen,
// the dealloc_stack of it can occur before the apply due to conversion scopes.
// When we insert destroy_addr of the in_guaranteed capture after the apply,
// we may end up with a situation when the dealloc_stack occurs before the destroy_addr.
// The code below proactively removes the dealloc_stack of in_guaranteed capture,
// so that it can be reinserted at the correct place after the destroy_addr below.
for (auto &arg : newPA->getArgumentOperands()) {
unsigned calleeArgumentIndex = site.getCalleeArgIndex(arg);
assert(calleeArgumentIndex >= calleeConv.getSILArgIndexOfFirstParam());
auto paramInfo = calleeConv.getParamInfoForSILArg(calleeArgumentIndex);
if (paramInfo.getConvention() == ParameterConvention::Indirect_In_Guaranteed) {
SILValue argValue = arg.get();
if (auto *mmci = dyn_cast<MoveOnlyWrapperToCopyableAddrInst>(argValue))
argValue = mmci->getOperand();
// go over all the dealloc_stack, remove it
SmallVector<Operand *, 16> Uses(argValue->getUses());
for (auto use : Uses) {
if (auto *deallocInst = dyn_cast<DeallocStackInst>(use->getUser()))
deleter.forceDelete(deallocInst);
}
}
}
// End borrows and insert destroys of arguments after the stack closure's
// lifetime ends.
SmallVector<SILInstruction *, 4> lifetimeEnds;
collectStackClosureLifetimeEnds(lifetimeEnds, closureOp);
// For noncopyable address-only captures, see if we can eliminate the copy
// that SILGen emitted to allow the original partial_apply to take ownership.
// We do this here because otherwise the move checker will see the copy as an
// attempt to consume the value, which we don't want.
SmallVector<SILBasicBlock *, 8> discoveredBlocks;
SSAPrunedLiveness closureLiveness(cvt->getFunction(), &discoveredBlocks);
closureLiveness.initializeDef(closureOp);
llvm::SmallSetVector<SILValue, 4> borrowedOriginals;
unsigned appliedArgStartIdx =
newPA->getOrigCalleeType()->getNumParameters() - newPA->getNumArguments();
MarkDependenceInst *markDepChain = nullptr;
for (unsigned i : indices(newPA->getArgumentOperands())) {
auto &arg = newPA->getArgumentOperands()[i];
SILValue copy = arg.get();
// The temporary should be a local stack allocation.
LLVM_DEBUG(llvm::dbgs() << "considering whether to eliminate copy of capture\n";
copy->printInContext(llvm::dbgs());
llvm::dbgs() << "\n");
auto stack = dyn_cast<AllocStackInst>(copy);
if (!stack) {
LLVM_DEBUG(llvm::dbgs() << "-- not an alloc_stack\n");
continue;
}
// This would be a nice optimization to attempt for all types, but for now,
// limit the effect to move-only types.
if (!copy->getType().isMoveOnly()) {
LLVM_DEBUG(llvm::dbgs() << "-- not move-only\n");
continue;
}
// Is the capture a borrow?
auto paramIndex = i + appliedArgStartIdx;
auto param = newPA->getOrigCalleeType()->getParameters()[paramIndex];
LLVM_DEBUG(param.print(llvm::dbgs());
llvm::dbgs() << '\n');
if (!param.isIndirectInGuaranteed()) {
LLVM_DEBUG(llvm::dbgs() << "-- not an in_guaranteed parameter\n";
newPA->getOrigCalleeType()->getParameters()[paramIndex]
.print(llvm::dbgs());
llvm::dbgs() << "\n");
continue;
}
// It needs to have been initialized by copying from somewhere else.
CopyAddrInst *initialization = nullptr;
MarkDependenceInst *markDep = nullptr;
for (auto *use : stack->getUses()) {
// Since we removed the `dealloc_stack`s from the capture arguments,
// the only uses of this stack slot should be the initialization, the
// partial application, and possibly a mark_dependence from the
// buffer to the partial application.
if (use->getUser() == newPA) {
continue;
}
if (auto mark = dyn_cast<MarkDependenceInst>(use->getUser())) {
// When we insert mark_dependence for non-trivial address operands, we
// emit a chain that looks like:
// %md = mark_dependence %pai on %0
// %md2 = mark_dependence %md on %1
// to tie all of those operands together on the same partial_apply.
//
// FIXME: Should we not be chaining like this and just emit independent
// mark_dependence?
if (markDepChain && mark->getValue() == markDepChain) {
markDep = mark;
markDepChain = mark;
continue;
}
// If we're marking dependence of the current partial_apply on this
// stack slot, that's fine.
if (mark->getValue() != newPA
|| mark->getBase() != stack) {
LLVM_DEBUG(llvm::dbgs() << "-- had unexpected mark_dependence use\n";
use->getUser()->print(llvm::dbgs());
llvm::dbgs() << "\n");
break;
}
markDep = mark;
if (!markDepChain) {
markDepChain = mark;
}
continue;
}
// If we saw more than just the initialization, this isn't a pattern we
// recognize.
if (initialization) {
LLVM_DEBUG(llvm::dbgs() << "-- had non-initialization, non-partial-apply use\n";
use->getUser()->print(llvm::dbgs());
llvm::dbgs() << "\n");
initialization = nullptr;
break;
}
if (auto possibleInit = dyn_cast<CopyAddrInst>(use->getUser())) {
// Should copy the source and initialize the destination.
if (possibleInit->isTakeOfSrc()
|| !possibleInit->isInitializationOfDest()) {
LLVM_DEBUG(llvm::dbgs() << "-- had non-initialization, non-partial-apply use\n";
use->getUser()->print(llvm::dbgs());
llvm::dbgs() << "\n");
break;
}
// This is the initialization if there are no other uses.
initialization = possibleInit;
continue;
}
LLVM_DEBUG(llvm::dbgs() << "-- unrecognized use\n");
break;
}
if (!initialization) {
LLVM_DEBUG(llvm::dbgs() << "-- failed to find single initializing use\n");
continue;
}
// The source should have no writes in the duration of the partial_apply's
// liveness.
auto orig = initialization->getSrc();
LLVM_DEBUG(llvm::dbgs() << "++ found original:\n";
orig->print(llvm::dbgs());
llvm::dbgs() << "\n");
bool origIsUnusedDuringClosureLifetime = true;
class OrigUnusedDuringClosureLifetimeWalker
: public TransitiveAddressWalker<
OrigUnusedDuringClosureLifetimeWalker> {
SSAPrunedLiveness &closureLiveness;
bool &origIsUnusedDuringClosureLifetime;
public:
OrigUnusedDuringClosureLifetimeWalker(SSAPrunedLiveness &closureLiveness,
bool &origIsUnusedDuringClosureLifetime)
: closureLiveness(closureLiveness),
origIsUnusedDuringClosureLifetime(origIsUnusedDuringClosureLifetime)
{}
bool visitUse(Operand *origUse) {
LLVM_DEBUG(llvm::dbgs() << "looking at use\n";
origUse->getUser()->printInContext(llvm::dbgs());
llvm::dbgs() << "\n");
// If the user doesn't write to memory, then it's harmless.
if (!origUse->getUser()->mayWriteToMemory()) {
return true;
}
if (closureLiveness.isWithinBoundary(origUse->getUser())) {
origIsUnusedDuringClosureLifetime = false;
LLVM_DEBUG(llvm::dbgs() << "-- original has other possibly writing use during closure lifetime\n";
origUse->getUser()->print(llvm::dbgs());
llvm::dbgs() << "\n");
return false;
}
return true;
}
};
OrigUnusedDuringClosureLifetimeWalker origUseWalker(closureLiveness,
origIsUnusedDuringClosureLifetime);
auto walkResult = std::move(origUseWalker).walk(orig);
if (walkResult == AddressUseKind::Unknown
|| !origIsUnusedDuringClosureLifetime) {
continue;
}
// OK, we can use the original. Eliminate the copy and replace it with the
// original.
LLVM_DEBUG(llvm::dbgs() << "++ replacing with original!\n");
arg.set(orig);
if (markDep) {
markDep->setBase(orig);
}
initialization->eraseFromParent();
stack->eraseFromParent();
borrowedOriginals.insert(orig);
}
/* DEBUG
llvm::errs() << "=== found lifetime ends for\n";
closureOp->dump();
llvm::errs() << "--- at\n";
*/
for (auto destroy : lifetimeEnds) {
/* DEBUG
destroy->dump();
*/
SILBuilderWithScope builder(std::next(destroy->getIterator()));
// This getCapturedArg hack attempts to perfectly compensate for all the
// other hacks involved in gathering new arguments above.
// argValue may be 'undef'
auto getArgToDestroy = [&](SILValue argValue) -> SILValue {
// A MoveOnlyWrapperToCopyableValueInst may produce a trivial value. Be
// careful not to emit an extra destroy of the original.
if (argValue->getType().isTrivial(destroy->getFunction()))
return SILValue();
// We may have inserted a new begin_borrow->moveonlywrapper_to_copyvalue
// when creating the new arguments. Now we need to end that borrow.
if (auto *m = dyn_cast<MoveOnlyWrapperToCopyableValueInst>(argValue))
if (m->hasGuaranteedInitialKind())
argValue = m->getOperand();
auto *argBorrow = dyn_cast<BeginBorrowInst>(argValue);
if (argBorrow) {
argValue = argBorrow->getOperand();
builder.createEndBorrow(newPA->getLoc(), argBorrow);
}
// Don't need to destroy if we borrowed in place .
return borrowedOriginals.count(argValue) ? SILValue() : argValue;
};
insertDestroyOfCapturedArguments(newPA, builder, getArgToDestroy,
newPA->getLoc());
}
/* DEBUG
llvm::errs() << "=== function after conversion to stack partial_apply of\n";
newPA->dump();
llvm::errs() << "---\n";
newPA->getFunction()->dump();
*/
// The CFG may have been modified during this run. If it was, the dominance
// analysis would no longer be valid. Invalidate it now if necessary,
// according to the kinds of changes that may have been made. Note that if
// the CFG hasn't been modified, this is a noop thanks to
// DominanceAnalysis::shouldInvalidate's definition.
dominanceAnalysis->invalidate(closureUser->getFunction(),
analysisInvalidationKind(modifiedCFG));
// Insert dealloc_stacks of any in_guaranteed captures.
// Don't run insertDeallocOfCapturedArguments if newPA is in an unreachable
// block insertDeallocOfCapturedArguments will run code that computes the DF
// for newPA that will loop infinetly.
if (unreachableBlocks.count(newPA->getParent()))
return closureOp;
auto getAddressToDealloc = [&](SILValue argAddress) -> SILValue {
if (auto moveWrapper =
dyn_cast<MoveOnlyWrapperToCopyableAddrInst>(argAddress)) {
argAddress = moveWrapper->getOperand();
}
// Don't need to destroy if we borrowed in place .
return borrowedOriginals.count(argAddress) ? SILValue() : argAddress;
};
insertDeallocOfCapturedArguments(
newPA, dominanceAnalysis->get(closureUser->getFunction()),
getAddressToDealloc);
return closureOp;
}
static bool tryExtendLifetimeToLastUse(
ConvertEscapeToNoEscapeInst *cvt, DominanceAnalysis *dominanceAnalysis,
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
llvm::DenseSet<SILBasicBlock *> &unreachableBlocks,
InstructionDeleter &deleter, const bool &modifiedCFG) {
// If there is a single user, this is simple: extend the
// lifetime of the operand until the use ends.
auto *singleUser = lookThroughRebastractionUsers(cvt, memoized);
if (!singleUser)
return false;
// Handle apply instructions and startAsyncLet.
BuiltinInst *endAsyncLet = nullptr;
if (FullApplySite::isa(singleUser)) {
// TODO: Enable begin_apply/end_apply. It should work, but is not tested yet.
if (isa<BeginApplyInst>(singleUser))
return false;
} else if (auto *bi = dyn_cast<BuiltinInst>(singleUser)) {
endAsyncLet = getEndAsyncLet(bi);
if (!endAsyncLet)
return false;
} else if (!isa<BeginBorrowInst>(singleUser)) {
return false;
}
if (SILValue closureOp = tryRewriteToPartialApplyStack(
cvt, singleUser, dominanceAnalysis, deleter, memoized,
unreachableBlocks, /*const*/ modifiedCFG)) {
if (endAsyncLet) {
// Add the closure as a second operand to the endAsyncLet builtin.
// This ensures that the closure arguments are kept alive until the
// endAsyncLet builtin.
assert(endAsyncLet->getNumOperands() == 1);
SILBuilderWithScope builder(endAsyncLet);
builder.createBuiltin(endAsyncLet->getLoc(), endAsyncLet->getName(),
endAsyncLet->getType(), endAsyncLet->getSubstitutions(),
{endAsyncLet->getOperand(0), closureOp});
deleter.forceDelete(endAsyncLet);
}
return true;
}
// Insert a copy at the convert_escape_to_noescape [not_guaranteed] and
// change the instruction to the guaranteed form.
auto escapingClosure = cvt->getOperand();
auto *closureCopy =
SILBuilderWithScope(cvt).createCopyValue(cvt->getLoc(), escapingClosure);
cvt->setLifetimeGuaranteed();
cvt->setOperand(closureCopy);
insertAfterClosureUser(singleUser, [closureCopy](SILBuilder &builder) {
auto loc = RegularLocation(builder.getInsertionPointLoc());
builder.createDestroyValue(loc, closureCopy);
});
/*
llvm::errs() << "after lifetime extension of\n";
escapingClosure->dump();
escapingClosure->getFunction()->dump();
*/
return true;
}
/// Ensure the lifetime of the closure across a two step optional conversion
/// from:
///
/// optional<@escaping () -> ()>
///
/// to:
///
/// optional<@noescape () -> ()>
///
/// to:
///
/// optional<@noescape @convention(block) () -> ()>
///
/// and all uses of the block. The pattern that we are looking for is:
///
/// switch_enum %optional_closure (1)
/// / \
/// %trivial_closure = CVT %closure nil (2)
/// \ /
/// switch_enum %optional_trivial_closure (3)
/// / \
/// %b = convertToBlock %trivial_closure nil (4)
/// \ /
/// ... uses of %optional_block ...
/// destroy_value %optional_block
///
/// where CVT is convert_escape_to_no_escape [not_guaranteed]. We assume that
/// the %optional_block is going through a conversion sequence in SILGen meaning
/// that we should only have a single destroy of the optional block.
///
/// NOTE: There is a *lifetime gap* during the usage of the trivial_closure!
/// This means we must be careful when lifetime extending. We can only assume
/// that the underlying closure is alive immediately at the CVT. So to perform
/// our lifetime extend, we do the following:
///
/// 1. We copy and borrow optional_closure, right before the switch_enum in
/// (1).
///
/// 2. We rewrite the convert_escape_to_no_escape guaranteed to use the copy
/// instead.
///
/// 3. To make sure that even after ossa is complete, we do not move any
/// destroys above the convert_escape_to_no_escape by putting a mark_dependence
/// on %closure
///
/// 4. We insert an end_borrow, destroy for the copy at the destroy of the
/// optional block.
static bool trySwitchEnumPeephole(ConvertEscapeToNoEscapeInst *cvt) {
auto *blockArg = dyn_cast<SILArgument>(cvt->getOperand());
if (!blockArg)
return false;
auto *convertSuccessorBlock = cvt->getParent()->getSingleSuccessorBlock();
if (!convertSuccessorBlock)
return false;
auto *predBB = cvt->getParent()->getSinglePredecessorBlock();
if (!predBB)
return false;
auto *switchEnum1 = dyn_cast<SwitchEnumInst>(predBB->getTerminator());
if (!switchEnum1)
return false;
auto *diamondSucc = getOptionalDiamondSuccessor(switchEnum1);
if (!diamondSucc)
return false;
auto *switchEnum2 = dyn_cast<SwitchEnumInst>(diamondSucc->getTerminator());
if (!switchEnum2)
return false;
auto *diamondSucc2 = getOptionalDiamondSuccessor(switchEnum2);
if (!diamondSucc2)
return false;
if (diamondSucc2->getNumArguments() != 1)
return false;
// Look for the last and only destroy of the diamond succ 2's argument. This
// is going to be the place where we destroy the lifetime extending copy.
SILInstruction *onlyDestroy = [&]() -> SILInstruction * {
SILInstruction *lastDestroy = nullptr;
for (auto *use : diamondSucc2->getArgument(0)->getUses()) {
SILInstruction *user = use->getUser();
if (isa<ReleaseValueInst>(user) || isa<StrongReleaseInst>(user) ||
isa<DestroyValueInst>(user)) {
if (lastDestroy)
return nullptr;
lastDestroy = user;
}
}
return lastDestroy;
}();
if (!onlyDestroy)
return false;
// Extend the lifetime.
auto loc = RegularLocation::getAutoGeneratedLocation();
SILValue copy, borrow;
std::tie(copy, borrow) = ([&]() -> std::pair<SILValue, SILValue> {
SILBuilderWithScope builder(switchEnum1);
auto copy = builder.emitCopyValueOperation(loc, switchEnum1->getOperand());
auto borrow = builder.emitBeginBorrowOperation(loc, copy);
return {copy, borrow};
})(); // end std::tie(copy, borrow).
{
SILBuilderWithScope builder(cvt);
auto value = builder.emitExtractOptionalPayloadOperation(loc, borrow);
cvt->setOperand(value);
cvt->setLifetimeGuaranteed();
}
{
SILBuilderWithScope builder(onlyDestroy);
builder.emitEndBorrowOperation(loc, borrow);
builder.emitDestroyValueOperation(loc, copy);
}
return true;
}
/// Look for a single destroy user and possibly unowned apply uses.
static SILInstruction *getOnlyDestroy(CopyBlockWithoutEscapingInst *cb) {
SILInstruction *onlyDestroy = nullptr;
for (auto *use : getNonDebugUses(cb)) {
SILInstruction *inst = use->getUser();
// If this an apply use, only handle unowned parameters.
if (auto apply = FullApplySite::isa(inst)) {
SILArgumentConvention conv = apply.getArgumentConvention(*use);
if (conv != SILArgumentConvention::Direct_Unowned)
return nullptr;
continue;
}
// We have already seen one destroy.
if (onlyDestroy)
return nullptr;
if (isa<DestroyValueInst>(inst) || isa<ReleaseValueInst>(inst) ||
isa<StrongReleaseInst>(inst)) {
onlyDestroy = inst;
continue;
}
// Some other instruction.
return nullptr;
}
if (!onlyDestroy)
return nullptr;
// Now look at whether the dealloc_stack or the destroy postdominates and
// return the post dominator.
auto *blockInit = dyn_cast<InitBlockStorageHeaderInst>(cb->getBlock());
if (!blockInit)
return nullptr;
auto *asi = dyn_cast<AllocStackInst>(blockInit->getBlockStorage());
if (!asi)
return nullptr;
auto *dealloc = asi->getSingleDeallocStack();
if (!dealloc || dealloc->getParent() != onlyDestroy->getParent())
return nullptr;
// Return the later instruction.
for (auto it = SILBasicBlock::iterator(onlyDestroy),
ie = dealloc->getParent()->end();
it != ie; ++it) {
if (&*it == dealloc)
return dealloc;
}
return onlyDestroy;
}
/// Lower a copy_block_without_escaping instruction.
///
/// This involves replacing:
///
/// %copy = copy_block_without_escaping %block withoutEscaping %closure
///
/// ...
/// destroy_value %copy
///
/// by (roughly) the instruction sequence:
///
/// %copy = copy_block %block
///
/// ...
/// destroy_value %copy
/// %e = is_escaping %closure
/// cond_fail %e
/// destroy_value %closure
static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
InstructionDeleter &deleter,
bool &modifiedCFG) {
// Find the end of the lifetime of the copy_block_without_escaping
// instruction.
auto &fn = *cb->getFunction();
// If we find a single destroy, this destroy is going to be a destroy that may
// be in the same block as CB. It is important that we make sure that the
// destroy is in a different block than CB or any terminating blocks to ensure
// that we can use the SSAUpdater if needed.
auto *singleDestroy = getOnlyDestroy(cb);
if (singleDestroy && singleDestroy->getParent() == cb->getParent()) {
modifiedCFG = true;
{
SILBuilderWithScope b(singleDestroy);
splitBasicBlockAndBranch(b, singleDestroy, nullptr, nullptr);
}
{
SILBuilderWithScope b(singleDestroy);
auto *term = singleDestroy->getParent()->getTerminator();
if (term->isFunctionExiting()) {
splitBasicBlockAndBranch(b, &*std::next(singleDestroy->getIterator()),
nullptr, nullptr);
}
}
}
auto sentinelClosure = cb->getClosure();
auto loc = cb->getLoc();
// At this point, we transform our copy_block_without_escaping into a
// copy_block. This has a few important implications:
//
// 1. copy_block_without_escaping takes the sentinel value at +1. We will need
// to balance that +1.
// 2. The destroy_value associated with the copy_block_without_escaping will
// be on the copy_block value.
SILBuilderWithScope b(cb);
auto *newCB = b.createCopyBlock(loc, cb->getBlock());
cb->replaceAllUsesWith(newCB);
deleter.forceDelete(cb);
auto autoGenLoc = RegularLocation::getAutoGeneratedLocation();
// If CB is in the entry block, we know that our definition of SentinelClosure
// must be as well. Thus we know that we do not need to worry about loops or
// dominance issues and can just insert destroy_values for the sentinel at the
// lifetime end points.
if (newCB->getParent() == newCB->getFunction()->getEntryBlock()) {
// Our single destroy must not be in the entry block since if so, we would
// have inserted an edge to appease the SSA updater.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
SILValue v = sentinelClosure;
SILValue isEscaping = b.createIsEscapingClosure(
loc, v, IsEscapingClosureInst::ObjCEscaping);
b.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
b.createDestroyValue(loc, v);
return true;
}
// If we couldn't find a specific destroy_value, lifetime extend to the end
// of the function.
SmallVector<SILBasicBlock *, 4> ExitingBlocks;
fn.findExitingBlocks(ExitingBlocks);
for (auto *Block : ExitingBlocks) {
SILBuilderWithScope B(Block->getTerminator());
SILValue V = sentinelClosure;
SILValue isEscaping = B.createIsEscapingClosure(
loc, V, IsEscapingClosureInst::ObjCEscaping);
B.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
B.createDestroyValue(loc, V);
}
return true;
}
// Otherwise, we need to be more careful since we can have loops and may not
// transitively dominate all uses of the closure. So we:
//
// 1. Create an Optional<T>.none at the entry.
// 2. Create a destroy_value(val), val = Optional<T>.some(sentinel) in the cvt
// block.
// 3. Create a destroy_value at all exits of the value.
//
// and then use the SSAUpdater to ensure that we handle loops correctly.
auto optionalEscapingClosureTy =
SILType::getOptionalType(sentinelClosure->getType());
SmallVector<SILPhiArgument *, 8> insertedPhis;
SILSSAUpdater updater(&insertedPhis);
updater.initialize(&fn, optionalEscapingClosureTy,
fn.hasOwnership() ? OwnershipKind::Owned
: OwnershipKind::None);
// Create the Optional.none as the beginning available value.
SILValue entryBlockOptionalNone;
{
SILBuilderWithScope b(fn.getEntryBlock()->begin());
entryBlockOptionalNone =
b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.addAvailableValue(fn.getEntryBlock(), entryBlockOptionalNone);
}
assert(entryBlockOptionalNone);
// Then create the Optional.some(closure sentinel).
//
// NOTE: We return the appropriate insertion point to insert the destroy_value
// before the value (to ensure we handle loops). We need to get all available
// values first though.
auto *initialValue = [&]() -> EnumInst * {
SILBuilderWithScope b(newCB);
// Create the closure sentinel (the copy_block_without_escaping closure
// operand consumed at +1, so we don't need a copy) to it.
auto *result = b.createOptionalSome(autoGenLoc, sentinelClosure,
optionalEscapingClosureTy);
updater.addAvailableValue(result->getParent(), result);
return result;
}();
// If we had a single destroy, creating a .none after it and add that as a
// value to the SSA updater.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
auto *result = b.createOptionalNone(autoGenLoc, optionalEscapingClosureTy);
updater.addAvailableValue(result->getParent(), result);
}
// Now that we have all of our available values, insert a destroy_value before
// the initial Optional.some value using the SSA updater to ensure that we
// handle loops correctly.
{
SILValue v = updater.getValueInMiddleOfBlock(initialValue->getParent());
SILBuilderWithScope(initialValue).createDestroyValue(autoGenLoc, v);
}
// And insert an is_escaping_closure, cond_fail, destroy_value at each of the
// lifetime end points. This ensures we do not expand our lifetime too much.
if (singleDestroy) {
SILBuilderWithScope b(std::next(singleDestroy->getIterator()));
SILValue v = updater.getValueInMiddleOfBlock(singleDestroy->getParent());
SILValue isEscaping =
b.createIsEscapingClosure(loc, v, IsEscapingClosureInst::ObjCEscaping);
b.createCondFail(loc, isEscaping, "non-escaping closure has escaped");
b.createDestroyValue(loc, v);
}
// Then to be careful with regards to loops, insert at each of the destroy
// blocks destroy_value to ensure that we obey ownership invariants.
{
SmallVector<SILBasicBlock *, 4> exitingBlocks;
findReachableExitBlocks(newCB, exitingBlocks);
for (auto *block : exitingBlocks) {
auto *safeDestructionPt = getDeinitSafeClosureDestructionPoint(block);
SILValue v = updater.getValueAtEndOfBlock(block);
SILBuilderWithScope(safeDestructionPt).createDestroyValue(autoGenLoc, v);
}
}
// Finally, we need to prune phis inserted by the SSA updater that only take
// the .none from the entry block.
//
// TODO: Should we sort inserted phis before or after we initialize
// the worklist or maybe backwards? We should investigate how the
// SSA updater adds phi nodes to this list to resolve this question.
cleanupDeadTrivialPhiArgs(entryBlockOptionalNone, insertedPhis);
return true;
}
static void computeUnreachableBlocks(
llvm::DenseSet<SILBasicBlock*> &unreachableBlocks,
SILFunction &fn) {
ReachableBlocks isReachable(&fn);
llvm::DenseSet<SILBasicBlock *> reachable;
isReachable.visit([&] (SILBasicBlock *block) -> bool {
reachable.insert(block);
return true;
});
for (auto &block : fn) {
if (!reachable.count(&block))
unreachableBlocks.insert(&block);
}
}
static bool fixupClosureLifetimes(SILFunction &fn,
DominanceAnalysis *dominanceAnalysis,
bool &checkStackNesting, bool &modifiedCFG) {
bool changed = false;
// tryExtendLifetimeToLastUse uses a cache of recursive instruction use
// queries.
llvm::DenseMap<SILInstruction *, SILInstruction *> memoizedQueries;
llvm::DenseSet<SILBasicBlock *> unreachableBlocks;
computeUnreachableBlocks(unreachableBlocks, fn);
for (auto &block : fn) {
SILSSAUpdater updater;
for (SILInstruction &inst : block.deletableInstructions()) {
// Handle, copy_block_without_escaping instructions.
if (auto *cb = dyn_cast<CopyBlockWithoutEscapingInst>(&inst)) {
if (fixupCopyBlockWithoutEscaping(cb, updater.getDeleter(), modifiedCFG)) {
changed = true;
}
continue;
}
// Otherwise, look at convert_escape_to_noescape [not_guaranteed]
// instructions.
auto *cvt = dyn_cast<ConvertEscapeToNoEscapeInst>(&inst);
if (!cvt || cvt->isLifetimeGuaranteed())
continue;
// First try to peephole a known pattern.
if (!DisableConvertEscapeToNoEscapeSwitchEnumPeephole) {
if (trySwitchEnumPeephole(cvt)) {
changed = true;
continue;
}
}
if (tryExtendLifetimeToLastUse(cvt, dominanceAnalysis, memoizedQueries,
unreachableBlocks, updater.getDeleter(),
/*const*/ modifiedCFG)) {
changed = true;
checkStackNesting = true;
continue;
}
// Otherwise, extend the lifetime of the operand to the end of the
// function.
extendLifetimeToEndOfFunction(fn, cvt, updater);
changed = true;
}
}
return changed;
}
/// Fix-up the lifetime of the escaping closure argument of
/// convert_escape_to_noescape [not_guaranteed] instructions.
///
/// convert_escape_to_noescape [not_guaranteed] assume that someone guarantees
/// the lifetime of the operand for the duration of the trivial closure result.
/// SILGen does not guarantee this for '[not_guaranteed]' instructions so we
/// ensure it here.
namespace {
class ClosureLifetimeFixup : public SILFunctionTransform {
/// The entry point to the transformation.
void run() override {
// Don't rerun diagnostics on deserialized functions.
if (getFunction()->wasDeserializedCanonical())
return;
// Fixup convert_escape_to_noescape [not_guaranteed] and
// copy_block_without_escaping instructions.
bool checkStackNesting = false;
bool modifiedCFG = false;
auto *dominanceAnalysis = PM->getAnalysis<DominanceAnalysis>();
if (fixupClosureLifetimes(*getFunction(), dominanceAnalysis,
checkStackNesting, modifiedCFG)) {
if (checkStackNesting){
modifiedCFG |=
StackNesting::fixNesting(getFunction()) == StackNesting::Changes::CFG;
}
invalidateAnalysis(analysisInvalidationKind(modifiedCFG));
}
LLVM_DEBUG(getFunction()->verify(getAnalysis<BasicCalleeAnalysis>()->getCalleeCache()));
}
};
} // end anonymous namespace
SILTransform *swift::createClosureLifetimeFixup() {
return new ClosureLifetimeFixup();
}
|