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 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
|
//===--- JVPCloner.cpp - JVP function generation --------------*- C++ -*---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 - 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
//
//===----------------------------------------------------------------------===//
//
// This file defines a helper class for generating JVP functions for automatic
// differentiation.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "differentiation"
#include "swift/SILOptimizer/Differentiation/JVPCloner.h"
#include "swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h"
#include "swift/SILOptimizer/Differentiation/ADContext.h"
#include "swift/SILOptimizer/Differentiation/AdjointValue.h"
#include "swift/SILOptimizer/Differentiation/DifferentiationInvoker.h"
#include "swift/SILOptimizer/Differentiation/LinearMapInfo.h"
#include "swift/SILOptimizer/Differentiation/PullbackCloner.h"
#include "swift/SILOptimizer/Differentiation/Thunk.h"
#include "swift/SIL/LoopInfo.h"
#include "swift/SIL/TypeSubstCloner.h"
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
#include "swift/SILOptimizer/PassManager/PrettyStackTrace.h"
#include "swift/SILOptimizer/Utils/DifferentiationMangler.h"
#include "swift/SILOptimizer/Utils/SILOptFunctionBuilder.h"
#include "llvm/ADT/DenseMap.h"
using namespace swift;
using namespace autodiff;
namespace swift {
namespace autodiff {
class JVPCloner::Implementation final
: public TypeSubstCloner<JVPCloner::Implementation, SILOptFunctionBuilder> {
private:
/// The global context.
ADContext &context;
/// The original function.
SILFunction *const original;
/// The witness.
SILDifferentiabilityWitness *const witness;
/// The JVP function.
SILFunction *const jvp;
llvm::BumpPtrAllocator allocator;
/// The differentiation invoker.
DifferentiationInvoker invoker;
/// Info from activity analysis on the original function.
const DifferentiableActivityInfo &activityInfo;
/// The loop info.
SILLoopInfo *loopInfo;
/// The differential info.
LinearMapInfo differentialInfo;
bool errorOccurred = false;
//--------------------------------------------------------------------------//
// Differential generation related fields
//--------------------------------------------------------------------------//
/// The builder for the differential function.
TangentBuilder differentialBuilder;
/// Mapping from original basic blocks to corresponding differential basic
/// blocks.
llvm::DenseMap<SILBasicBlock *, SILBasicBlock *> diffBBMap;
/// Mapping from original basic blocks and original values to corresponding
/// tangent values.
llvm::DenseMap<SILValue, AdjointValue> tangentValueMap;
/// Mapping from original basic blocks and original buffers to corresponding
/// tangent buffers.
llvm::DenseMap<std::pair<SILBasicBlock *, SILValue>, SILValue> bufferMap;
/// Mapping from differential basic blocks to differential struct arguments.
llvm::DenseMap<SILBasicBlock *, SILArgument *> differentialStructArguments;
/// Mapping from differential struct field declarations to differential struct
/// elements destructured from the linear map basic block argument. In the
/// beginning of each differential basic block, the block's differential
/// struct is destructured into the individual elements stored here.
llvm::DenseMap<SILBasicBlock *, SILInstructionResultArray> differentialTupleElements;
/// An auxiliary differential local allocation builder.
TangentBuilder diffLocalAllocBuilder;
/// Stack buffers allocated for storing local tangent values.
SmallVector<SILValue, 8> differentialLocalAllocations;
/// Mapping from original blocks to differential values. Used to build
/// differential struct instances.
llvm::DenseMap<SILBasicBlock *, SmallVector<SILValue, 8>> differentialValues;
//--------------------------------------------------------------------------//
// Getters
//--------------------------------------------------------------------------//
ASTContext &getASTContext() const { return jvp->getASTContext(); }
SILModule &getModule() const { return jvp->getModule(); }
const AutoDiffConfig getConfig() const { return witness->getConfig(); }
TangentBuilder &getDifferentialBuilder() { return differentialBuilder; }
SILFunction &getDifferential() { return differentialBuilder.getFunction(); }
SILArgument *getDifferentialStructArgument(SILBasicBlock *origBB) {
return differentialStructArguments[origBB];
}
//--------------------------------------------------------------------------//
// Differential tuple mapping
//--------------------------------------------------------------------------//
void initializeDifferentialTupleElements(SILBasicBlock *origBB,
SILInstructionResultArray values);
SILValue getDifferentialTupleElement(ApplyInst *ai);
//--------------------------------------------------------------------------//
// General utilities
//--------------------------------------------------------------------------//
/// Get the lowered SIL type of the given AST type.
SILType getLoweredType(Type type) {
auto jvpGenSig = jvp->getLoweredFunctionType()->getSubstGenericSignature();
Lowering::AbstractionPattern pattern(jvpGenSig,
type->getReducedType(jvpGenSig));
return jvp->getLoweredType(pattern, type);
}
/// Get the lowered SIL type of the given nominal type declaration.
SILType getNominalDeclLoweredType(NominalTypeDecl *nominal) {
auto nominalType =
getOpASTType(nominal->getDeclaredInterfaceType()->getCanonicalType());
return getLoweredType(nominalType);
}
/// Build a differential struct value for the original block corresponding to
/// the given terminator.
TupleInst *buildDifferentialValueStructValue(TermInst *termInst) {
assert(termInst->getFunction() == original);
auto loc = termInst->getFunction()->getLocation();
auto *origBB = termInst->getParent();
auto *jvpBB = BBMap[origBB];
assert(jvpBB && "Basic block mapping should exist");
auto tupleLoweredTy =
remapType(differentialInfo.getLinearMapTupleLoweredType(origBB));
auto bbDifferentialValues = differentialValues[origBB];
if (!origBB->isEntry()) {
auto *enumArg = jvpBB->getArguments().back();
bbDifferentialValues.insert(bbDifferentialValues.begin(), enumArg);
}
return getBuilder().createTuple(loc, tupleLoweredTy,
bbDifferentialValues);
}
//--------------------------------------------------------------------------//
// Tangent value factory methods
//--------------------------------------------------------------------------//
AdjointValue makeZeroTangentValue(SILType type) {
return AdjointValue::createZero(allocator,
remapSILTypeInDifferential(type));
}
AdjointValue makeConcreteTangentValue(SILValue value) {
return AdjointValue::createConcrete(allocator, value);
}
//--------------------------------------------------------------------------//
// Tangent materialization
//--------------------------------------------------------------------------//
void emitZeroIndirect(CanType type, SILValue buffer, SILLocation loc) {
auto builder = getDifferentialBuilder();
auto tangentSpace = getTangentSpace(type);
assert(tangentSpace && "No tangent space for this type");
switch (tangentSpace->getKind()) {
case TangentSpace::Kind::TangentVector:
builder.emitZeroIntoBuffer(loc, buffer, IsInitialization);
return;
case TangentSpace::Kind::Tuple: {
auto tupleType = tangentSpace->getTuple();
SmallVector<SILValue, 8> zeroElements;
for (unsigned i : range(tupleType->getNumElements())) {
auto eltAddr = builder.createTupleElementAddr(loc, buffer, i);
emitZeroIndirect(tupleType->getElementType(i)->getCanonicalType(),
eltAddr, loc);
}
return;
}
}
}
SILValue emitZeroDirect(CanType type, SILLocation loc) {
auto diffBuilder = getDifferentialBuilder();
auto silType = getModule().Types.getLoweredLoadableType(
type, TypeExpansionContext::minimal(), getModule());
auto *buffer = diffBuilder.createAllocStack(loc, silType);
emitZeroIndirect(type, buffer, loc);
auto loaded = diffBuilder.emitLoadValueOperation(
loc, buffer, LoadOwnershipQualifier::Take);
diffBuilder.createDeallocStack(loc, buffer);
return loaded;
}
SILValue materializeTangentDirect(AdjointValue val, SILLocation loc) {
assert(val.getType().isObject());
LLVM_DEBUG(getADDebugStream()
<< "Materializing tangents for " << val << '\n');
switch (val.getKind()) {
case AdjointValueKind::Zero: {
auto zeroVal = emitZeroDirect(val.getSwiftType(), loc);
return zeroVal;
}
case AdjointValueKind::Concrete:
return val.getConcreteValue();
case AdjointValueKind::Aggregate:
case AdjointValueKind::AddElement:
llvm_unreachable(
"Tuples and structs are not supported in forward mode yet.");
}
llvm_unreachable("Invalid adjoint value kind"); // silences MSVC C4715
}
SILValue materializeTangent(AdjointValue val, SILLocation loc) {
if (val.isConcrete()) {
LLVM_DEBUG(getADDebugStream()
<< "Materializing tangent: Value is concrete.\n");
return val.getConcreteValue();
}
LLVM_DEBUG(getADDebugStream() << "Materializing tangent: Value is "
"non-concrete. Materializing directly.\n");
return materializeTangentDirect(val, loc);
}
//--------------------------------------------------------------------------//
// Tangent value mapping
//--------------------------------------------------------------------------//
/// Get the tangent for an original value. The given value must be in the
/// original function.
///
/// This method first tries to find an entry in `tangentValueMap`. If an entry
/// doesn't exist, create a zero tangent.
AdjointValue getTangentValue(SILValue originalValue) {
assert(originalValue->getType().isObject());
assert(originalValue->getFunction() == original);
auto insertion = tangentValueMap.try_emplace(
originalValue,
makeZeroTangentValue(getRemappedTangentType(originalValue->getType())));
return insertion.first->getSecond();
}
/// Map the tangent value to the given original value.
void setTangentValue(SILBasicBlock *origBB, SILValue originalValue,
AdjointValue newTangentValue) {
#ifndef NDEBUG
if (auto *defInst = originalValue->getDefiningInstruction()) {
bool isTupleTypedApplyResult =
isa<ApplyInst>(defInst) && originalValue->getType().is<TupleType>();
assert(!isTupleTypedApplyResult &&
"Should not set tangent value for tuple-typed result from `apply` "
"instruction; use `destructure_tuple` on `apply` result and set "
"tangent value for `destructure_tuple` results instead.");
}
#endif
assert(originalValue->getType().isObject());
assert(newTangentValue.getType().isObject());
assert(originalValue->getFunction() == original);
LLVM_DEBUG(getADDebugStream()
<< "Setting tangent value for " << originalValue);
// The tangent value must be in the tangent space.
assert(newTangentValue.getType() ==
getRemappedTangentType(originalValue->getType()));
auto insertion =
tangentValueMap.try_emplace(originalValue, newTangentValue);
(void)insertion;
assert(insertion.second && "The tangent value should not already exist.");
}
//--------------------------------------------------------------------------//
// Tangent buffer mapping
//--------------------------------------------------------------------------//
/// Sets the tangent buffer for the original buffer. Asserts that the
/// original buffer does not already have a tangent buffer.
void setTangentBuffer(SILBasicBlock *origBB, SILValue originalBuffer,
SILValue tangentBuffer) {
assert(originalBuffer->getType().isAddress());
auto insertion =
bufferMap.try_emplace({origBB, originalBuffer}, tangentBuffer);
assert(insertion.second && "Tangent buffer already exists");
(void)insertion;
}
/// Returns the tangent buffer for the original buffer. Asserts that the
/// original buffer has a tangent buffer.
SILValue &getTangentBuffer(SILBasicBlock *origBB, SILValue originalBuffer) {
assert(originalBuffer->getType().isAddress());
assert(originalBuffer->getFunction() == original);
auto it = bufferMap.find({origBB, originalBuffer});
assert(it != bufferMap.end() && "Tangent buffer should already exist");
return it->getSecond();
}
//--------------------------------------------------------------------------//
// Differential type calculations
//--------------------------------------------------------------------------//
/// Substitutes all replacement types of the given substitution map using the
/// tangent function's substitution map.
SubstitutionMap remapSubstitutionMapInDifferential(SubstitutionMap substMap) {
return substMap.subst(getDifferential().getForwardingSubstitutionMap());
}
/// Remap any archetypes into the differential function's context.
Type remapTypeInDifferential(Type ty) {
if (ty->hasArchetype())
return getDifferential().mapTypeIntoContext(ty->mapTypeOutOfContext());
return getDifferential().mapTypeIntoContext(ty);
}
/// Remap any archetypes into the differential function's context.
SILType remapSILTypeInDifferential(SILType ty) {
if (ty.hasArchetype())
return getDifferential().mapTypeIntoContext(ty.mapTypeOutOfContext());
return getDifferential().mapTypeIntoContext(ty);
}
/// Find the tangent space of a given canonical type.
std::optional<TangentSpace> getTangentSpace(CanType type) {
// Use witness generic signature to remap types.
type = witness->getDerivativeGenericSignature().getReducedType(
type);
return type->getAutoDiffTangentSpace(
LookUpConformanceInModule(getModule().getSwiftModule()));
}
/// Assuming the given type conforms to `Differentiable` after remapping,
/// returns the associated tangent space SIL type.
SILType getRemappedTangentType(SILType type) {
return SILType::getPrimitiveType(
getTangentSpace(remapSILTypeInDifferential(type).getASTType())
->getCanonicalType(),
type.getCategory());
}
/// Set up the differential function. This includes:
/// - Creating all differential blocks.
/// - Creating differential entry block arguments based on the function type.
/// - Creating tangent value mapping for original/differential parameters.
/// - Checking for unvaried result and emitting related warnings.
void prepareForDifferentialGeneration();
public:
explicit Implementation(ADContext &context,
SILDifferentiabilityWitness *witness,
SILFunction *jvp, DifferentiationInvoker invoker);
static SILFunction *
createEmptyDifferential(ADContext &context,
SILDifferentiabilityWitness *witness,
LinearMapInfo *linearMapInfo);
/// Run JVP generation. Returns true on error.
bool run();
SILFunction &getJVP() const { return *jvp; }
void postProcess(SILInstruction *orig, SILInstruction *cloned) {
if (errorOccurred)
return;
SILClonerWithScopes::postProcess(orig, cloned);
}
/// Remap original basic blocks.
SILBasicBlock *remapBasicBlock(SILBasicBlock *bb) {
auto *jvpBB = BBMap[bb];
return jvpBB;
}
/// General visitor for all instructions. If any error is emitted by previous
/// visits, bail out.
void visit(SILInstruction *inst) {
if (errorOccurred)
return;
if (differentialInfo.shouldDifferentiateInstruction(inst)) {
LLVM_DEBUG(getADDebugStream() << "JVPCloner visited:\n[ORIG]" << *inst);
#ifndef NDEBUG
auto diffBuilder = getDifferentialBuilder();
auto beforeInsertion = std::prev(diffBuilder.getInsertionPoint());
#endif
TypeSubstCloner::visit(inst);
LLVM_DEBUG({
auto &s = llvm::dbgs() << "[TAN] Emitted in differential:\n";
auto afterInsertion = diffBuilder.getInsertionPoint();
for (auto it = ++beforeInsertion; it != afterInsertion; ++it)
s << *it;
});
} else {
TypeSubstCloner::visit(inst);
}
}
void visitSILInstruction(SILInstruction *inst) {
context.emitNondifferentiabilityError(
inst, invoker, diag::autodiff_expression_not_differentiable_note);
errorOccurred = true;
}
void visitInstructionsInBlock(SILBasicBlock *bb) {
// Destructure the differential struct to get the elements.
auto &diffBuilder = getDifferentialBuilder();
auto diffLoc = getDifferential().getLocation();
auto *diffBB = diffBBMap.lookup(bb);
auto *mainDifferentialStruct = diffBB->getArguments().back();
diffBuilder.setInsertionPoint(diffBB);
auto *dsi =
diffBuilder.createDestructureTuple(diffLoc, mainDifferentialStruct);
initializeDifferentialTupleElements(bb, dsi->getResults());
TypeSubstCloner::visitInstructionsInBlock(bb);
}
// If an `apply` has active results or active inout parameters, replace it
// with an `apply` of its JVP.
void visitApplyInst(ApplyInst *ai) {
bool shouldDifferentiate =
differentialInfo.shouldDifferentiateApplySite(ai);
// If the function has no active arguments or results, zero-initialize the
// tangent buffers of the active indirect results.
if (!shouldDifferentiate) {
for (auto indResult : ai->getIndirectSILResults())
if (activityInfo.isActive(indResult, getConfig())) {
auto &tanBuf = getTangentBuffer(ai->getParent(), indResult);
emitZeroIndirect(tanBuf->getType().getASTType(), tanBuf,
tanBuf.getLoc());
}
}
// If the function should not be differentiated or its the array literal
// initialization intrinsic, just do standard cloning.
if (!shouldDifferentiate ||
ArraySemanticsCall(ai, semantics::ARRAY_UNINITIALIZED_INTRINSIC)) {
LLVM_DEBUG(getADDebugStream() << "No active results:\n" << *ai << '\n');
TypeSubstCloner::visitApplyInst(ai);
return;
}
auto loc = ai->getLoc();
auto &builder = getBuilder();
auto origCallee = getOpValue(ai->getCallee());
auto originalFnTy = origCallee->getType().castTo<SILFunctionType>();
LLVM_DEBUG(getADDebugStream() << "JVP-transforming:\n" << *ai << '\n');
// Get the minimal parameter and result indices required for differentiating
// this `apply`.
SmallVector<SILValue, 4> allResults;
SmallVector<unsigned, 8> activeParamIndices;
SmallVector<unsigned, 8> activeResultIndices;
collectMinimalIndicesForFunctionCall(ai, getConfig(), activityInfo,
allResults, activeParamIndices,
activeResultIndices);
assert(!activeParamIndices.empty() && "Parameter indices cannot be empty");
assert(!activeResultIndices.empty() && "Result indices cannot be empty");
LLVM_DEBUG(auto &s = getADDebugStream() << "Active indices: params={";
llvm::interleave(
activeParamIndices.begin(), activeParamIndices.end(),
[&s](unsigned i) { s << i; }, [&s] { s << ", "; });
s << "}, results={"; llvm::interleave(
activeResultIndices.begin(), activeResultIndices.end(),
[&s](unsigned i) { s << i; }, [&s] { s << ", "; });
s << "}\n";);
// Form expected indices.
auto numResults =
ai->getSubstCalleeType()->getNumResults() +
ai->getSubstCalleeType()->getNumIndirectMutatingParameters();
AutoDiffConfig config(
IndexSubset::get(getASTContext(),
ai->getArgumentsWithoutIndirectResults().size(),
activeParamIndices),
IndexSubset::get(getASTContext(), numResults, activeResultIndices));
// Emit the JVP.
SILValue jvpValue;
// If functionSource is a `@differentiable` function, just extract it.
if (originalFnTy->isDifferentiable()) {
auto paramIndices = originalFnTy->getDifferentiabilityParameterIndices();
for (auto i : config.parameterIndices->getIndices()) {
if (!paramIndices->contains(i)) {
context.emitNondifferentiabilityError(
origCallee, invoker,
diag::
autodiff_function_noderivative_parameter_not_differentiable);
errorOccurred = true;
return;
}
}
builder.emitScopedBorrowOperation(
loc, origCallee, [&](SILValue borrowedDiffFunc) {
jvpValue = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP,
borrowedDiffFunc);
jvpValue = builder.emitCopyValueOperation(loc, jvpValue);
});
}
// If JVP has not yet been found, emit an `differentiable_function`
// instruction on the remapped function operand and
// an `differentiable_function_extract` instruction to get the JVP.
// The `differentiable_function` instruction will be canonicalized during
// the transform main loop.
if (!jvpValue) {
// FIXME: Handle indirect differentiation invokers. This may require some
// redesign: currently, each original function + witness pair is mapped
// only to one invoker.
/*
DifferentiationInvoker indirect(ai, attr);
auto insertion =
context.getInvokers().try_emplace({original, attr}, indirect);
auto &invoker = insertion.first->getSecond();
invoker = indirect;
*/
// If the original `apply` instruction has a substitution map, then the
// applied function is specialized.
// In the JVP, specialization is also necessary for parity. The original
// function operand is specialized with a remapped version of same
// substitution map using an argument-less `partial_apply`.
if (ai->getSubstitutionMap().empty()) {
origCallee = builder.emitCopyValueOperation(loc, origCallee);
} else {
auto substMap = getOpSubstitutionMap(ai->getSubstitutionMap());
auto jvpPartialApply = getBuilder().createPartialApply(
ai->getLoc(), origCallee, substMap, {},
ParameterConvention::Direct_Guaranteed);
origCallee = jvpPartialApply;
}
// Check and diagnose non-differentiable original function type.
auto diagnoseNondifferentiableOriginalFunctionType =
[&](CanSILFunctionType origFnTy) {
// Check and diagnose non-differentiable arguments.
for (auto paramIndex : config.parameterIndices->getIndices()) {
if (!originalFnTy->getParameters()[paramIndex]
.getSILStorageInterfaceType()
.isDifferentiable(getModule())) {
auto arg = ai->getArgumentsWithoutIndirectResults()[paramIndex];
auto startLoc = arg.getLoc().getStartSourceLoc();
auto endLoc = arg.getLoc().getEndSourceLoc();
context
.emitNondifferentiabilityError(
arg, invoker, diag::autodiff_nondifferentiable_argument)
.fixItInsert(startLoc, "withoutDerivative(at: ")
.fixItInsertAfter(endLoc, ")");
errorOccurred = true;
return true;
}
}
// Check and diagnose non-differentiable results.
for (auto resultIndex : config.resultIndices->getIndices()) {
SILType remappedResultType;
if (resultIndex >= originalFnTy->getNumResults()) {
auto inoutArgIdx = resultIndex - originalFnTy->getNumResults();
auto inoutArg =
*std::next(ai->getInoutArguments().begin(), inoutArgIdx);
remappedResultType = inoutArg->getType();
} else {
remappedResultType = originalFnTy->getResults()[resultIndex]
.getSILStorageInterfaceType();
}
if (!remappedResultType.isDifferentiable(getModule())) {
auto startLoc = ai->getLoc().getStartSourceLoc();
auto endLoc = ai->getLoc().getEndSourceLoc();
context
.emitNondifferentiabilityError(
origCallee, invoker,
diag::autodiff_nondifferentiable_result)
.fixItInsert(startLoc, "withoutDerivative(at: ")
.fixItInsertAfter(endLoc, ")");
errorOccurred = true;
return true;
}
}
return false;
};
if (diagnoseNondifferentiableOriginalFunctionType(originalFnTy))
return;
auto *diffFuncInst = context.createDifferentiableFunction(
builder, loc, config.parameterIndices, config.resultIndices,
origCallee);
// Record the `differentiable_function` instruction.
context.getDifferentiableFunctionInstWorklist().push_back(diffFuncInst);
builder.emitScopedBorrowOperation(
loc, diffFuncInst, [&](SILValue borrowedADFunc) {
auto extractedJVP = builder.createDifferentiableFunctionExtract(
loc, NormalDifferentiableFunctionTypeComponent::JVP,
borrowedADFunc);
jvpValue = builder.emitCopyValueOperation(loc, extractedJVP);
});
builder.emitDestroyValueOperation(loc, diffFuncInst);
}
// Call the JVP using the original parameters.
SmallVector<SILValue, 8> jvpArgs;
auto jvpFnTy = getOpType(jvpValue->getType()).castTo<SILFunctionType>();
auto numJVPArgs =
jvpFnTy->getNumParameters() + jvpFnTy->getNumIndirectFormalResults();
jvpArgs.reserve(numJVPArgs);
// Collect substituted arguments.
for (auto origArg : ai->getArguments())
jvpArgs.push_back(getOpValue(origArg));
assert(jvpArgs.size() == numJVPArgs);
// Apply the JVP.
// The JVP should be specialized, so no substitution map is necessary.
auto *jvpCall = getBuilder().createApply(loc, jvpValue, SubstitutionMap(),
jvpArgs, ai->getApplyOptions());
LLVM_DEBUG(getADDebugStream() << "Applied jvp function\n" << *jvpCall);
// Release the differentiable function.
builder.emitDestroyValueOperation(loc, jvpValue);
// Get the JVP results (original results and differential).
SmallVector<SILValue, 8> jvpDirectResults;
extractAllElements(jvpCall, builder, jvpDirectResults);
auto originalDirectResults =
ArrayRef<SILValue>(jvpDirectResults).drop_back(1);
auto originalDirectResult =
joinElements(originalDirectResults, getBuilder(), jvpCall->getLoc());
mapValue(ai, originalDirectResult);
// Some instructions that produce the callee may have been cloned.
// If the original callee did not have any users beyond this `apply`,
// recursively kill the cloned callee.
if (auto *origCallee = cast_or_null<SingleValueInstruction>(
ai->getCallee()->getDefiningInstruction()))
if (origCallee->hasOneUse())
recursivelyDeleteTriviallyDeadInstructions(
getOpValue(origCallee)->getDefiningInstruction());
// Add the differential function for when we create the struct we partially
// apply to the differential we are generating.
auto differential = jvpDirectResults.back();
auto differentialType = differentialInfo.lookUpLinearMapType(ai);
auto originalDifferentialType =
getOpType(differential->getType()).getAs<SILFunctionType>();
auto loweredDifferentialType =
getOpType(getLoweredType(differentialType)).castTo<SILFunctionType>();
// If actual differential type does not match lowered differential type,
// reabstract the differential using a thunk.
if (!loweredDifferentialType->isEqual(originalDifferentialType)) {
SILOptFunctionBuilder fb(context.getTransform());
differential = reabstractFunction(
builder, fb, loc, differential, loweredDifferentialType,
[this](SubstitutionMap subs) -> SubstitutionMap {
return this->getOpSubstitutionMap(subs);
});
}
differentialValues[ai->getParent()].push_back(differential);
// Differential emission.
emitTangentForApplyInst(ai, config, originalDifferentialType);
}
void visitReturnInst(ReturnInst *ri) {
auto loc = ri->getOperand().getLoc();
auto *origExit = ri->getParent();
auto &builder = getBuilder();
auto *diffStructVal = buildDifferentialValueStructValue(ri);
// Get the JVP value corresponding to the original functions's return value.
auto *origRetInst = cast<ReturnInst>(origExit->getTerminator());
auto origResult = getOpValue(origRetInst->getOperand());
SmallVector<SILValue, 8> origResults;
extractAllElements(origResult, builder, origResults);
// Get and partially apply the differential.
auto jvpSubstMap = jvp->getForwardingSubstitutionMap();
auto *differentialRef = builder.createFunctionRef(loc, &getDifferential());
auto *differentialPartialApply = builder.createPartialApply(
loc, differentialRef, jvpSubstMap, {diffStructVal},
ParameterConvention::Direct_Guaranteed);
auto differentialType = jvp->mapTypeIntoContext(
jvp->getConventions().getSILType(
jvp->getLoweredFunctionType()->getResults().back(),
jvp->getTypeExpansionContext()));
auto differentialFnType = differentialType.castTo<SILFunctionType>();
auto differentialSubstType =
differentialPartialApply->getType().castTo<SILFunctionType>();
// If necessary, convert the differential value to the returned differential
// function type.
SILValue differentialValue;
if (differentialSubstType == differentialFnType) {
differentialValue = differentialPartialApply;
} else if (differentialSubstType
->isABICompatibleWith(differentialFnType, *jvp)
.isCompatible()) {
differentialValue = builder.createConvertFunction(
loc, differentialPartialApply, differentialType,
/*withoutActuallyEscaping*/ false);
} else {
llvm::report_fatal_error("Differential value type is not ABI-compatible "
"with the returned differential type");
}
// Return a tuple of the original result and differential.
SmallVector<SILValue, 8> directResults;
directResults.append(origResults.begin(), origResults.end());
directResults.push_back(differentialValue);
builder.createReturn(ri->getLoc(),
joinElements(directResults, builder, loc));
}
void visitBranchInst(BranchInst *bi) {
llvm_unreachable("Unsupported SIL instruction.");
}
void visitCondBranchInst(CondBranchInst *cbi) {
llvm_unreachable("Unsupported SIL instruction.");
}
void visitSwitchEnumInst(SwitchEnumInst *sei) {
llvm_unreachable("Unsupported SIL instruction.");
}
void visitDifferentiableFunctionInst(DifferentiableFunctionInst *dfi) {
// Clone `differentiable_function` from original to JVP, then add the cloned
// instruction to the `differentiable_function` worklist.
TypeSubstCloner::visitDifferentiableFunctionInst(dfi);
auto *newDFI = cast<DifferentiableFunctionInst>(getOpValue(dfi));
context.getDifferentiableFunctionInstWorklist().push_back(newDFI);
}
void visitLinearFunctionInst(LinearFunctionInst *lfi) {
// Clone `linear_function` from original to JVP, then add the cloned
// instruction to the `linear_function` worklist.
TypeSubstCloner::visitLinearFunctionInst(lfi);
auto *newLFI = cast<LinearFunctionInst>(getOpValue(lfi));
context.getLinearFunctionInstWorklist().push_back(newLFI);
}
//--------------------------------------------------------------------------//
// Tangent emission helpers
//--------------------------------------------------------------------------//
#define CLONE_AND_EMIT_TANGENT(INST, ID) \
void visit##INST##Inst(INST##Inst *inst) { \
TypeSubstCloner::visit##INST##Inst(inst); \
if (differentialInfo.shouldDifferentiateInstruction(inst)) \
emitTangentFor##INST##Inst(inst); \
} \
void emitTangentFor##INST##Inst(INST##Inst *(ID))
CLONE_AND_EMIT_TANGENT(BeginBorrow, bbi) {
auto &diffBuilder = getDifferentialBuilder();
auto loc = bbi->getLoc();
auto tanVal = materializeTangent(getTangentValue(bbi->getOperand()), loc);
auto tanValBorrow = diffBuilder.emitBeginBorrowOperation(
loc, tanVal, bbi->isLexical(), bbi->hasPointerEscape(),
bbi->isFromVarDecl());
setTangentValue(bbi->getParent(), bbi,
makeConcreteTangentValue(tanValBorrow));
}
CLONE_AND_EMIT_TANGENT(EndBorrow, ebi) {
auto &diffBuilder = getDifferentialBuilder();
auto loc = ebi->getLoc();
auto tanVal = materializeTangent(getTangentValue(ebi->getOperand()), loc);
diffBuilder.emitEndBorrowOperation(loc, tanVal);
}
CLONE_AND_EMIT_TANGENT(DestroyValue, dvi) {
auto &diffBuilder = getDifferentialBuilder();
auto loc = dvi->getLoc();
auto tanVal = materializeTangent(getTangentValue(dvi->getOperand()), loc);
diffBuilder.emitDestroyValueOperation(loc, tanVal);
}
CLONE_AND_EMIT_TANGENT(CopyValue, cvi) {
auto &diffBuilder = getDifferentialBuilder();
auto tan = getTangentValue(cvi->getOperand());
auto tanVal = materializeTangent(tan, cvi->getLoc());
auto tanValCopy = diffBuilder.emitCopyValueOperation(cvi->getLoc(), tanVal);
setTangentValue(cvi->getParent(), cvi,
makeConcreteTangentValue(tanValCopy));
}
CLONE_AND_EMIT_TANGENT(MoveValue, mvi) {
auto &diffBuilder = getDifferentialBuilder();
auto tan = getTangentValue(mvi->getOperand());
auto tanVal = materializeTangent(tan, mvi->getLoc());
auto tanValMove = diffBuilder.emitMoveValueOperation(
mvi->getLoc(), tanVal, mvi->isLexical(), mvi->hasPointerEscape(),
mvi->isFromVarDecl());
setTangentValue(mvi->getParent(), mvi,
makeConcreteTangentValue(tanValMove));
}
/// Handle `load` instruction.
/// Original: y = load x
/// Tangent: tan[y] = load tan[x]
void visitLoadInst(LoadInst *li) {
TypeSubstCloner::visitLoadInst(li);
// If an active buffer is loaded with take to a non-active value, destroy
// the active buffer's tangent buffer.
if (!differentialInfo.shouldDifferentiateInstruction(li)) {
auto isTake =
(li->getOwnershipQualifier() == LoadOwnershipQualifier::Take);
if (isTake && activityInfo.isActive(li->getOperand(), getConfig())) {
auto &tanBuf = getTangentBuffer(li->getParent(), li->getOperand());
getDifferentialBuilder().emitDestroyOperation(tanBuf.getLoc(), tanBuf);
}
return;
}
// Otherwise, do standard differential cloning.
auto &diffBuilder = getDifferentialBuilder();
auto *bb = li->getParent();
auto loc = li->getLoc();
auto tanBuf = getTangentBuffer(bb, li->getOperand());
auto tanVal = diffBuilder.emitLoadValueOperation(
loc, tanBuf, li->getOwnershipQualifier());
setTangentValue(bb, li, makeConcreteTangentValue(tanVal));
}
/// Handle `load_borrow` instruction.
/// Original: y = load_borrow x
/// Tangent: tan[y] = load_borrow tan[x]
CLONE_AND_EMIT_TANGENT(LoadBorrow, lbi) {
auto &diffBuilder = getDifferentialBuilder();
auto *bb = lbi->getParent();
auto loc = lbi->getLoc();
auto tanBuf = getTangentBuffer(bb, lbi->getOperand());
auto tanVal = diffBuilder.emitLoadBorrowOperation(loc, tanBuf);
setTangentValue(bb, lbi, makeConcreteTangentValue(tanVal));
}
/// Handle `store` instruction in the differential.
/// Original: store x to y
/// Tangent: store tan[x] to tan[y]
void visitStoreInst(StoreInst *si) {
TypeSubstCloner::visitStoreInst(si);
// If a non-active value is stored into an active buffer, zero-initialize
// the active buffer's tangent buffer.
if (!differentialInfo.shouldDifferentiateInstruction(si)) {
if (activityInfo.isActive(si->getDest(), getConfig())) {
auto &tanBufDest = getTangentBuffer(si->getParent(), si->getDest());
emitZeroIndirect(tanBufDest->getType().getASTType(), tanBufDest,
tanBufDest.getLoc());
}
return;
}
// Otherwise, do standard differential cloning.
auto &diffBuilder = getDifferentialBuilder();
auto loc = si->getLoc();
auto tanValSrc = materializeTangent(getTangentValue(si->getSrc()), loc);
auto &tanValDest = getTangentBuffer(si->getParent(), si->getDest());
diffBuilder.emitStoreValueOperation(loc, tanValSrc, tanValDest,
si->getOwnershipQualifier());
}
/// Handle `store_borrow` instruction in the differential.
/// Original: store_borrow x to y
/// Tangent: store_borrow tan[x] to tan[y]
void visitStoreBorrowInst(StoreBorrowInst *sbi) {
TypeSubstCloner::visitStoreBorrowInst(sbi);
// If a non-active value is stored into an active buffer, zero-initialize
// the active buffer's tangent buffer.
if (!differentialInfo.shouldDifferentiateInstruction(sbi)) {
if (activityInfo.isActive(sbi->getDest(), getConfig())) {
auto &tanBufDest = getTangentBuffer(sbi->getParent(), sbi->getDest());
emitZeroIndirect(tanBufDest->getType().getASTType(), tanBufDest,
tanBufDest.getLoc());
}
return;
}
// Otherwise, do standard differential cloning.
auto &diffBuilder = getDifferentialBuilder();
auto loc = sbi->getLoc();
auto tanValSrc = materializeTangent(getTangentValue(sbi->getSrc()), loc);
auto &tanValDest = getTangentBuffer(sbi->getParent(), sbi->getDest());
diffBuilder.createStoreBorrow(loc, tanValSrc, tanValDest);
}
/// Handle `copy_addr` instruction.
/// Original: copy_addr x to y
/// Tangent: copy_addr tan[x] to tan[y]
void visitCopyAddrInst(CopyAddrInst *cai) {
TypeSubstCloner::visitCopyAddrInst(cai);
// If a non-active buffer is copied into an active buffer, zero-initialize
// the destination buffer's tangent buffer.
// If an active buffer is copied with take into a non-active buffer, destroy
// the source buffer's tangent buffer.
if (!differentialInfo.shouldDifferentiateInstruction(cai)) {
if (activityInfo.isActive(cai->getDest(), getConfig())) {
auto &tanBufDest = getTangentBuffer(cai->getParent(), cai->getDest());
emitZeroIndirect(tanBufDest->getType().getASTType(), tanBufDest,
tanBufDest.getLoc());
}
if (cai->isTakeOfSrc() &&
activityInfo.isActive(cai->getSrc(), getConfig())) {
auto &tanBufSrc = getTangentBuffer(cai->getParent(), cai->getSrc());
getDifferentialBuilder().emitDestroyOperation(tanBufSrc.getLoc(),
tanBufSrc);
}
return;
}
// Otherwise, do standard differential cloning.
auto diffBuilder = getDifferentialBuilder();
auto loc = cai->getLoc();
auto *bb = cai->getParent();
auto &tanSrc = getTangentBuffer(bb, cai->getSrc());
auto tanDest = getTangentBuffer(bb, cai->getDest());
diffBuilder.createCopyAddr(loc, tanSrc, tanDest, cai->isTakeOfSrc(),
cai->isInitializationOfDest());
}
/// Handle `unconditional_checked_cast_addr` instruction.
/// Original: unconditional_checked_cast_addr $X in x to $Y in y
/// Tangent: unconditional_checked_cast_addr $X.Tan in tan[x]
/// to $Y.Tan in tan[y]
CLONE_AND_EMIT_TANGENT(UnconditionalCheckedCastAddr, uccai) {
auto diffBuilder = getDifferentialBuilder();
auto loc = uccai->getLoc();
auto *bb = uccai->getParent();
auto &tanSrc = getTangentBuffer(bb, uccai->getSrc());
auto tanDest = getTangentBuffer(bb, uccai->getDest());
diffBuilder.createUnconditionalCheckedCastAddr(
loc, tanSrc, tanSrc->getType().getASTType(), tanDest,
tanDest->getType().getASTType());
}
/// Handle `begin_access` instruction (and do differentiability checks).
/// Original: y = begin_access x
/// Tangent: tan[y] = begin_access tan[x]
CLONE_AND_EMIT_TANGENT(BeginAccess, bai) {
// Check for non-differentiable writes.
if (bai->getAccessKind() == SILAccessKind::Modify) {
if (auto *gai = dyn_cast<GlobalAddrInst>(bai->getSource())) {
context.emitNondifferentiabilityError(
bai, invoker,
diag::autodiff_cannot_differentiate_writes_to_global_variables);
errorOccurred = true;
return;
}
if (auto *pbi = dyn_cast<ProjectBoxInst>(bai->getSource())) {
context.emitNondifferentiabilityError(
bai, invoker,
diag::autodiff_cannot_differentiate_writes_to_mutable_captures);
errorOccurred = true;
return;
}
}
auto &diffBuilder = getDifferentialBuilder();
auto *bb = bai->getParent();
auto tanSrc = getTangentBuffer(bb, bai->getSource());
auto *tanDest = diffBuilder.createBeginAccess(
bai->getLoc(), tanSrc, bai->getAccessKind(), bai->getEnforcement(),
bai->hasNoNestedConflict(), bai->isFromBuiltin());
setTangentBuffer(bb, bai, tanDest);
}
/// Handle `end_access` instruction.
/// Original: begin_access x
/// Tangent: end_access tan[x]
CLONE_AND_EMIT_TANGENT(EndAccess, eai) {
auto &diffBuilder = getDifferentialBuilder();
auto *bb = eai->getParent();
auto loc = eai->getLoc();
auto tanOperand = getTangentBuffer(bb, eai->getOperand());
diffBuilder.createEndAccess(loc, tanOperand, eai->isAborting());
}
/// Handle `alloc_stack` instruction.
/// Original: y = alloc_stack $T
/// Tangent: tan[y] = alloc_stack $T.Tangent
CLONE_AND_EMIT_TANGENT(AllocStack, asi) {
auto &diffBuilder = getDifferentialBuilder();
auto varInfo = asi->getVarInfo();
if (varInfo) {
// This is a new variable, it shouldn't keep the old scope, type, etc.
varInfo->Type = {};
varInfo->DIExpr = {};
varInfo->Loc = {};
varInfo->Scope = nullptr;
}
auto *mappedAllocStackInst = diffBuilder.createAllocStack(
asi->getLoc(), getRemappedTangentType(asi->getElementType()), varInfo);
setTangentBuffer(asi->getParent(), asi, mappedAllocStackInst);
}
/// Handle `dealloc_stack` instruction.
/// Original: dealloc_stack x
/// Tangent: dealloc_stack tan[x]
CLONE_AND_EMIT_TANGENT(DeallocStack, dsi) {
auto &diffBuilder = getDifferentialBuilder();
auto tanBuf = getTangentBuffer(dsi->getParent(), dsi->getOperand());
diffBuilder.createDeallocStack(dsi->getLoc(), tanBuf);
}
/// Handle `destroy_addr` instruction.
/// Original: destroy_addr x
/// Tangent: destroy_addr tan[x]
CLONE_AND_EMIT_TANGENT(DestroyAddr, dai) {
auto &diffBuilder = getDifferentialBuilder();
auto tanBuf = getTangentBuffer(dai->getParent(), dai->getOperand());
diffBuilder.createDestroyAddr(dai->getLoc(), tanBuf);
}
/// Handle `struct` instruction.
/// Original: y = struct $T (x0, x1, x2, ...)
/// Tangent: tan[y] = struct $T.Tangent (tan[x0], tan[x1], tan[x2], ...)
CLONE_AND_EMIT_TANGENT(Struct, si) {
auto &diffBuilder = getDifferentialBuilder();
SmallVector<SILValue, 4> tangentElements;
for (auto elem : si->getElements())
tangentElements.push_back(getTangentValue(elem).getConcreteValue());
auto tanExtract = diffBuilder.createStruct(
si->getLoc(), getRemappedTangentType(si->getType()), tangentElements);
setTangentValue(si->getParent(), si, makeConcreteTangentValue(tanExtract));
}
/// Handle `struct_extract` instruction.
/// Original: y = struct_extract x, #field
/// Tangent: tan[y] = struct_extract tan[x], #field'
/// ^~~~~~~
/// field in tangent space corresponding to #field
CLONE_AND_EMIT_TANGENT(StructExtract, sei) {
assert(!sei->getField()->getAttrs().hasAttribute<NoDerivativeAttr>() &&
"`struct_extract` with `@noDerivative` field should not be "
"differentiated; activity analysis should not marked as varied.");
auto diffBuilder = getDifferentialBuilder();
auto loc = getValidLocation(sei);
// Find the corresponding field in the tangent space.
auto structType =
remapSILTypeInDifferential(sei->getOperand()->getType()).getASTType();
auto *tanField =
getTangentStoredProperty(context, sei, structType, invoker);
if (!tanField) {
errorOccurred = true;
return;
}
// Emit tangent `struct_extract`.
auto tanStruct =
materializeTangent(getTangentValue(sei->getOperand()), loc);
auto tangentInst =
diffBuilder.createStructExtract(loc, tanStruct, tanField);
// Update tangent value mapping for `struct_extract` result.
auto tangentResult = makeConcreteTangentValue(tangentInst);
setTangentValue(sei->getParent(), sei, tangentResult);
}
/// Handle `struct_element_addr` instruction.
/// Original: y = struct_element_addr x, #field
/// Tangent: tan[y] = struct_element_addr tan[x], #field'
/// ^~~~~~~
/// field in tangent space corresponding to #field
CLONE_AND_EMIT_TANGENT(StructElementAddr, seai) {
assert(!seai->getField()->getAttrs().hasAttribute<NoDerivativeAttr>() &&
"`struct_element_addr` with `@noDerivative` field should not be "
"differentiated; activity analysis should not marked as varied.");
auto diffBuilder = getDifferentialBuilder();
auto *bb = seai->getParent();
auto loc = getValidLocation(seai);
// Find the corresponding field in the tangent space.
auto structType =
remapSILTypeInDifferential(seai->getOperand()->getType()).getASTType();
auto *tanField =
getTangentStoredProperty(context, seai, structType, invoker);
if (!tanField) {
errorOccurred = true;
return;
}
// Emit tangent `struct_element_addr`.
auto tanOperand = getTangentBuffer(bb, seai->getOperand());
auto tangentInst =
diffBuilder.createStructElementAddr(loc, tanOperand, tanField);
// Update tangent buffer map for `struct_element_addr`.
setTangentBuffer(bb, seai, tangentInst);
}
/// Handle `tuple` instruction.
/// Original: y = tuple (x0, x1, x2, ...)
/// Tangent: tan[y] = tuple (tan[x0], tan[x1], tan[x2], ...)
/// ^~~
/// excluding non-differentiable elements
CLONE_AND_EMIT_TANGENT(Tuple, ti) {
auto diffBuilder = getDifferentialBuilder();
// Get the tangents of all the tuple elements.
SmallVector<SILValue, 8> tangentTupleElements;
for (auto elem : ti->getElements()) {
if (!getTangentSpace(elem->getType().getASTType()))
continue;
tangentTupleElements.push_back(
materializeTangent(getTangentValue(elem), ti->getLoc()));
}
// Emit the instruction and add the tangent mapping.
auto tanTuple =
joinElements(tangentTupleElements, diffBuilder, ti->getLoc());
setTangentValue(ti->getParent(), ti, makeConcreteTangentValue(tanTuple));
}
/// Handle `tuple_extract` instruction.
/// Original: y = tuple_extract x, <n>
/// Tangent: tan[y] = tuple_extract tan[x], <n'>
/// ^~~~
/// tuple tangent space index corresponding to n
CLONE_AND_EMIT_TANGENT(TupleExtract, tei) {
auto &diffBuilder = getDifferentialBuilder();
auto loc = tei->getLoc();
auto origTupleTy = tei->getOperand()->getType().castTo<TupleType>();
unsigned tanIndex = 0;
for (unsigned i : range(tei->getFieldIndex())) {
if (getTangentSpace(
origTupleTy->getElement(i).getType()->getCanonicalType()))
++tanIndex;
}
auto tanType = getRemappedTangentType(tei->getType());
auto tanSource =
materializeTangent(getTangentValue(tei->getOperand()), loc);
// If the tangent value of the source does not have a tuple type, then
// it must represent a "single element tuple type". Use it directly.
if (!tanSource->getType().is<TupleType>()) {
setTangentValue(tei->getParent(), tei,
makeConcreteTangentValue(tanSource));
} else {
auto tanElt =
diffBuilder.createTupleExtract(loc, tanSource, tanIndex, tanType);
setTangentValue(tei->getParent(), tei, makeConcreteTangentValue(tanElt));
}
}
/// Handle `tuple_element_addr` instruction.
/// Original: y = tuple_element_addr x, <n>
/// Tangent: tan[y] = tuple_element_addr tan[x], <n'>
/// ^~~~
/// tuple tangent space index corresponding to n
CLONE_AND_EMIT_TANGENT(TupleElementAddr, teai) {
auto &diffBuilder = getDifferentialBuilder();
auto origTupleTy = teai->getOperand()->getType().castTo<TupleType>();
unsigned tanIndex = 0;
for (unsigned i : range(teai->getFieldIndex())) {
if (getTangentSpace(
origTupleTy->getElement(i).getType()->getCanonicalType()))
++tanIndex;
}
auto tanType = getRemappedTangentType(teai->getType());
auto tanSource = getTangentBuffer(teai->getParent(), teai->getOperand());
SILValue tanBuf;
// If the tangent buffer of the source does not have a tuple type, then
// it must represent a "single element tuple type". Use it directly.
if (!tanSource->getType().is<TupleType>()) {
tanBuf = tanSource;
} else {
tanBuf = diffBuilder.createTupleElementAddr(teai->getLoc(), tanSource,
tanIndex, tanType);
}
setTangentBuffer(teai->getParent(), teai, tanBuf);
}
/// Handle `destructure_tuple` instruction.
/// Original: (y0, y1, ...) = destructure_tuple x, <n>
/// Tangent: (tan[y0], tan[y1], ...) = destructure_tuple tan[x], <n'>
/// ^~~~
/// tuple tangent space index corresponding to n
CLONE_AND_EMIT_TANGENT(DestructureTuple, dti) {
assert(llvm::any_of(dti->getResults(),
[&](SILValue elt) {
return activityInfo.isActive(elt, getConfig());
}) &&
"`destructure_tuple` should have at least one active result");
auto &diffBuilder = getDifferentialBuilder();
auto *bb = dti->getParent();
auto loc = dti->getLoc();
auto tanTuple = materializeTangent(getTangentValue(dti->getOperand()), loc);
SmallVector<SILValue, 4> tanElts;
if (tanTuple->getType().is<TupleType>()) {
auto *tanDti = diffBuilder.createDestructureTuple(loc, tanTuple);
tanElts.append(tanDti->getResults().begin(), tanDti->getResults().end());
} else {
tanElts.push_back(tanTuple);
}
unsigned tanIdx = 0;
for (auto i : range(dti->getNumResults())) {
auto origElt = dti->getResult(i);
if (!getTangentSpace(origElt->getType().getASTType()))
continue;
setTangentValue(bb, origElt, makeConcreteTangentValue(tanElts[tanIdx++]));
}
}
#undef CLONE_AND_EMIT_TANGENT
/// Handle `apply` instruction, given:
/// - The minimal indices for differentiating the `apply`.
/// - The original non-reabstracted differential type.
///
/// Original: y = apply f(x0, x1, ...)
/// Tangent: tan[y] = apply diff_f(tan[x0], tan[x1], ...)
void emitTangentForApplyInst(ApplyInst *ai, const AutoDiffConfig &applyConfig,
CanSILFunctionType originalDifferentialType) {
assert(differentialInfo.shouldDifferentiateApplySite(ai));
auto *bb = ai->getParent();
auto loc = ai->getLoc();
auto &diffBuilder = getDifferentialBuilder();
// Get the differential value.
SILValue differential = getDifferentialTupleElement(ai);
auto differentialType = remapSILTypeInDifferential(differential->getType())
.castTo<SILFunctionType>();
// Get the differential arguments.
SmallVector<SILValue, 8> diffArgs;
for (auto indRes : ai->getIndirectSILResults())
diffArgs.push_back(getTangentBuffer(bb, indRes));
auto origArgs = ai->getArgumentsWithoutIndirectResults();
// Get the tangent value of the original arguments.
for (auto i : indices(origArgs)) {
auto origArg = origArgs[i];
// If the argument is not active:
// - Skip the element, if it is not differentiable.
// - Otherwise, add a zero value to that location.
if (!activityInfo.isActive(origArg, getConfig())) {
auto origCalleeType = ai->getSubstCalleeType();
if (!origCalleeType->isDifferentiable())
continue;
auto actualOrigCalleeIndices =
origCalleeType->getDifferentiabilityParameterIndices();
if (actualOrigCalleeIndices->contains(i)) {
SILValue tanParam;
if (origArg->getType().isObject()) {
tanParam = emitZeroDirect(
getRemappedTangentType(origArg->getType()).getASTType(), loc);
diffArgs.push_back(tanParam);
} else {
tanParam = diffBuilder.createAllocStack(
loc, getRemappedTangentType(origArg->getType()));
emitZeroIndirect(
getRemappedTangentType(origArg->getType()).getASTType(),
tanParam, loc);
}
}
}
// Otherwise, if the argument is active, handle the argument normally by
// getting its tangent value.
else {
SILValue tanParam;
if (origArg->getType().isObject()) {
tanParam = materializeTangent(getTangentValue(origArg), loc);
} else {
tanParam = getTangentBuffer(ai->getParent(), origArg);
}
diffArgs.push_back(tanParam);
if (errorOccurred)
return;
}
}
// If callee differential was reabstracted in JVP, reabstract the callee
// differential.
if (!differentialType->isEqual(originalDifferentialType)) {
SILOptFunctionBuilder fb(context.getTransform());
differential = reabstractFunction(
diffBuilder, fb, loc, differential, originalDifferentialType,
[this](SubstitutionMap subs) -> SubstitutionMap {
return this->getOpSubstitutionMap(subs);
});
}
// Call the differential.
auto *differentialCall =
diffBuilder.createApply(loc, differential, SubstitutionMap(), diffArgs);
diffBuilder.emitDestroyValueOperation(loc, differential);
// Get the original `apply` results.
SmallVector<SILValue, 8> origDirectResults;
forEachApplyDirectResult(ai, [&](SILValue directResult) {
origDirectResults.push_back(directResult);
});
SmallVector<SILValue, 8> origAllResults;
collectAllActualResultsInTypeOrder(ai, origDirectResults, origAllResults);
// Get the callee differential `apply` results.
SmallVector<SILValue, 8> differentialDirectResults;
extractAllElements(differentialCall, getDifferentialBuilder(),
differentialDirectResults);
SmallVector<SILValue, 8> differentialAllResults;
collectAllActualResultsInTypeOrder(
differentialCall, differentialDirectResults, differentialAllResults);
for (auto inoutArg : ai->getInoutArguments())
origAllResults.push_back(inoutArg);
for (auto inoutArg : differentialCall->getInoutArguments())
differentialAllResults.push_back(inoutArg);
assert(applyConfig.resultIndices->getNumIndices() ==
differentialAllResults.size());
// Set tangent values for original `apply` results.
unsigned differentialResultIndex = 0;
for (auto resultIndex : applyConfig.resultIndices->getIndices()) {
auto origResult = origAllResults[resultIndex];
auto differentialResult =
differentialAllResults[differentialResultIndex++];
if (origResult->getType().isObject()) {
if (!origResult->getType().is<TupleType>()) {
setTangentValue(bb, origResult,
makeConcreteTangentValue(differentialResult));
} else if (auto *dti =
ai->getSingleUserOfType<DestructureTupleInst>()) {
bool notSetValue = true;
for (auto result : dti->getResults()) {
if (activityInfo.isActive(result, getConfig())) {
assert(notSetValue &&
"This was incorrectly set, should only have one active "
"result from the tuple.");
notSetValue = false;
setTangentValue(bb, result,
makeConcreteTangentValue(differentialResult));
}
}
}
}
}
}
/// Generate a `return` instruction in the current differential basic block.
void emitReturnInstForDifferential() {
auto &differential = getDifferential();
auto diffLoc = differential.getLocation();
auto &diffBuilder = getDifferentialBuilder();
// Collect original results.
SmallVector<SILValue, 2> originalResults;
collectAllDirectResultsInTypeOrder(*original, originalResults);
// Collect differential direct results.
SmallVector<SILValue, 8> retElts;
for (auto i : range(originalResults.size())) {
auto origResult = originalResults[i];
if (!getConfig().resultIndices->contains(i))
continue;
auto tanVal = materializeTangent(getTangentValue(origResult), diffLoc);
retElts.push_back(tanVal);
}
diffBuilder.createReturn(diffLoc,
joinElements(retElts, diffBuilder, diffLoc));
}
};
//--------------------------------------------------------------------------//
// Initialization
//--------------------------------------------------------------------------//
/// Initialization helper function.
///
/// Returns the substitution map used for type remapping.
static SubstitutionMap getSubstitutionMap(SILFunction *original,
SILFunction *jvp) {
auto substMap = original->getForwardingSubstitutionMap();
if (auto *jvpGenEnv = jvp->getGenericEnvironment()) {
auto jvpSubstMap = jvpGenEnv->getForwardingSubstitutionMap();
substMap = SubstitutionMap::get(
jvpGenEnv->getGenericSignature(), QuerySubstitutionMap{jvpSubstMap},
LookUpConformanceInSubstitutionMap(jvpSubstMap));
}
return substMap;
}
/// Initialization helper function.
///
/// Returns the activity info for the given original function, autodiff indices,
/// and JVP generic signature.
static const DifferentiableActivityInfo &
getActivityInfo(ADContext &context, SILFunction *original,
const AutoDiffConfig &config, SILFunction *jvp) {
// Get activity info of the original function.
auto &passManager = context.getPassManager();
auto *activityAnalysis =
passManager.getAnalysis<DifferentiableActivityAnalysis>();
auto &activityCollection = *activityAnalysis->get(original);
auto &activityInfo = activityCollection.getActivityInfo(
jvp->getLoweredFunctionType()->getSubstGenericSignature(),
AutoDiffDerivativeFunctionKind::JVP);
LLVM_DEBUG(activityInfo.dump(config, getADDebugStream()));
return activityInfo;
}
JVPCloner::Implementation::Implementation(ADContext &context,
SILDifferentiabilityWitness *witness,
SILFunction *jvp,
DifferentiationInvoker invoker)
: TypeSubstCloner(*jvp, *witness->getOriginalFunction(),
getSubstitutionMap(witness->getOriginalFunction(), jvp)),
context(context), original(witness->getOriginalFunction()),
witness(witness), jvp(jvp), invoker(invoker),
activityInfo(
getActivityInfo(context, original, witness->getConfig(), jvp)),
loopInfo(context.getPassManager().getAnalysis<SILLoopAnalysis>()
->get(original)),
differentialInfo(context, AutoDiffLinearMapKind::Differential, original,
jvp, witness->getConfig(), activityInfo, loopInfo),
differentialBuilder(TangentBuilder(
*createEmptyDifferential(context, witness, &differentialInfo),
context)),
diffLocalAllocBuilder(getDifferential(), context) {
// Create empty differential function.
context.recordGeneratedFunction(&getDifferential());
}
JVPCloner::JVPCloner(ADContext &context, SILDifferentiabilityWitness *witness,
SILFunction *jvp, DifferentiationInvoker invoker)
: impl(*new Implementation(context, witness, jvp, invoker)) {}
JVPCloner::~JVPCloner() { delete &impl; }
//--------------------------------------------------------------------------//
// Differential struct mapping
//--------------------------------------------------------------------------//
void JVPCloner::Implementation::initializeDifferentialTupleElements(
SILBasicBlock *origBB, SILInstructionResultArray values) {
auto *diffTupleTyple = differentialInfo.getLinearMapTupleType(origBB);
assert(diffTupleTyple->getNumElements() == values.size() &&
"The number of differential tuple fields must equal the number of "
"differential struct element values");
auto res = differentialTupleElements.insert({origBB, values});
(void)res;
assert(res.second && "A pullback struct element already exists!");
}
/// Returns the differential tuple element value corresponding to the given
/// original block and apply inst.
SILValue JVPCloner::Implementation::getDifferentialTupleElement(ApplyInst *ai) {
unsigned idx = differentialInfo.lookUpLinearMapIndex(ai);
assert((idx > 0 || (idx == 0 && ai->getParentBlock()->isEntry())) &&
"impossible linear map index");
auto values = differentialTupleElements.lookup(ai->getParentBlock());
assert(idx < values.size() &&
"differential tuple element for this apply does not exist!");
return values[idx];
}
//--------------------------------------------------------------------------//
// Tangent emission helpers
//--------------------------------------------------------------------------//
void JVPCloner::Implementation::prepareForDifferentialGeneration() {
// Create differential blocks and arguments.
auto &differential = getDifferential();
auto diffLoc = differential.getLocation();
auto *origEntry = original->getEntryBlock();
auto origFnTy = original->getLoweredFunctionType();
for (auto &origBB : *original) {
auto *diffBB = differential.createBasicBlock();
diffBBMap.insert({&origBB, diffBB});
// If the BB is the original entry, then the differential block that we
// just created must be the differential function's entry. Create
// differential entry arguments and continue.
if (&origBB == origEntry) {
assert(diffBB->isEntry());
createEntryArguments(&differential);
auto *lastArg = diffBB->getArguments().back();
#ifndef NDEBUG
auto diffTupleLoweredType = remapSILTypeInDifferential(
differentialInfo.getLinearMapTupleLoweredType(&origBB));
assert(lastArg->getType() == diffTupleLoweredType);
#endif
differentialStructArguments[&origBB] = lastArg;
}
LLVM_DEBUG({
auto &s = getADDebugStream()
<< "Original bb" + std::to_string(origBB.getDebugID())
<< ": To differentiate or not to differentiate?\n";
for (auto &inst : origBB) {
s << (differentialInfo.shouldDifferentiateInstruction(&inst) ? "[x] "
: "[ ] ")
<< inst;
}
});
}
assert(diffBBMap.size() == 1 &&
"Can only currently handle single basic block functions");
// The differential function has type:
// (arg0', ..., argn', entry_df_struct) -> result'.
auto diffParamArgs =
differential.getArgumentsWithoutIndirectResults().drop_back();
assert(diffParamArgs.size() ==
witness->getConfig().parameterIndices->getNumIndices());
auto origParamArgs = original->getArgumentsWithoutIndirectResults();
// TODO(TF-788): Re-enable non-varied result warning.
/*
// Check if result is not varied.
SmallVector<SILValue, 8> origFormalResults;
collectAllFormalResultsInTypeOrder(*original, origFormalResults);
std::get<0>(pair);
for (auto resultIndex : getConfig().results->getIndices()) {
auto origResult = origFormalResults[resultIndex];
// Emit warning if original result is not varied, because it will always
// have a zero derivative.
if (!activityInfo.isVaried(origResult, getConfig().parameters)) {
// Emit fixit if original result has a valid source location.
auto startLoc = origResult.getLoc().getStartSourceLoc();
auto endLoc = origResult.getLoc().getEndSourceLoc();
if (startLoc.isValid() && endLoc.isValid()) {
context.diagnose(startLoc, diag::autodiff_nonvaried_result_fixit)
.fixItInsert(startLoc, "withoutDerivative(at:")
.fixItInsertAfter(endLoc, ")");
}
}
}
*/
// Initialize tangent mapping for parameters.
auto diffParamsIt = getConfig().parameterIndices->begin();
for (auto index : range(diffParamArgs.size())) {
auto *diffArg = diffParamArgs[index];
auto *origArg = origParamArgs[*diffParamsIt];
++diffParamsIt;
if (diffArg->getType().isAddress()) {
setTangentBuffer(origEntry, origArg, diffArg);
} else {
setTangentValue(origEntry, origArg, makeConcreteTangentValue(diffArg));
}
LLVM_DEBUG(getADDebugStream()
<< "Assigned parameter " << *diffArg
<< " as the tangent of original result " << *origArg);
}
// Initialize tangent mapping for original indirect results and non-wrt
// `inout` parameters. The tangent buffers of these address values are
// differential indirect results.
// Collect original results.
SmallVector<SILValue, 2> originalResults;
collectAllFormalResultsInTypeOrder(*original, originalResults);
// Iterate over differentiability results.
differentialBuilder.setInsertionPoint(differential.getEntryBlock());
auto diffIndResults = differential.getIndirectResults();
unsigned differentialIndirectResultIndex = 0;
for (auto resultIndex : getConfig().resultIndices->getIndices()) {
auto origResult = originalResults[resultIndex];
// Handle original formal indirect result.
if (resultIndex < origFnTy->getNumResults()) {
// Skip original direct results.
if (origResult->getType().isObject())
continue;
auto diffIndResult = diffIndResults[differentialIndirectResultIndex++];
setTangentBuffer(origEntry, origResult, diffIndResult);
// If original indirect result is non-varied, zero-initialize its tangent
// buffer.
if (!activityInfo.isVaried(origResult, getConfig().parameterIndices))
emitZeroIndirect(diffIndResult->getType().getASTType(), diffIndResult,
diffLoc);
continue;
}
// Handle original non-wrt `inout` parameter.
// Only original *non-wrt* `inout` parameters have corresponding
// differential indirect results.
auto inoutParamIndex = resultIndex - origFnTy->getNumResults();
auto inoutParamIt = std::next(
origFnTy->getIndirectMutatingParameters().begin(), inoutParamIndex);
auto paramIndex =
std::distance(origFnTy->getParameters().begin(), &*inoutParamIt);
if (getConfig().parameterIndices->contains(paramIndex))
continue;
auto diffIndResult = diffIndResults[differentialIndirectResultIndex++];
setTangentBuffer(origEntry, origResult, diffIndResult);
// Original `inout` parameters are initialized, so their tangent buffers
// must also be initialized.
emitZeroIndirect(diffIndResult->getType().getASTType(), diffIndResult,
diffLoc);
}
}
/*static*/ SILFunction *JVPCloner::Implementation::createEmptyDifferential(
ADContext &context, SILDifferentiabilityWitness *witness,
LinearMapInfo *linearMapInfo) {
auto &module = context.getModule();
auto *original = witness->getOriginalFunction();
auto *jvp = witness->getJVP();
auto origTy = original->getLoweredFunctionType();
// Get witness generic signature for remapping types.
// Witness generic signature may have more requirements than JVP generic
// signature: when witness generic signature has same-type requirements
// binding all generic parameters to concrete types, JVP function type uses
// all the concrete types and JVP generic signature is null.
auto witnessCanGenSig = witness->getDerivativeGenericSignature().getCanonicalSignature();
auto lookupConformance = LookUpConformanceInModule(module.getSwiftModule());
// Parameters of the differential are:
// - the tangent values of the wrt parameters.
// - the differential struct for the original entry.
// Result of the differential is in the tangent space of the original
// result.
SmallVector<SILParameterInfo, 8> dfParams;
SmallVector<SILResultInfo, 8> dfResults;
auto origParams = origTy->getParameters();
auto config = witness->getConfig();
for (auto resultIndex : config.resultIndices->getIndices()) {
if (resultIndex < origTy->getNumResults()) {
// Handle formal original result.
auto origResult = origTy->getResults()[resultIndex];
origResult = origResult.getWithInterfaceType(
origResult.getInterfaceType()->getReducedType(witnessCanGenSig));
dfResults.push_back(
SILResultInfo(origResult.getInterfaceType()
->getAutoDiffTangentSpace(lookupConformance)
->getType()
->getReducedType(witnessCanGenSig),
origResult.getConvention()));
} else {
// Handle original `inout` parameter.
auto inoutParamIndex = resultIndex - origTy->getNumResults();
auto inoutParamIt = std::next(
origTy->getIndirectMutatingParameters().begin(), inoutParamIndex);
auto paramIndex =
std::distance(origTy->getParameters().begin(), &*inoutParamIt);
// If the original `inout` parameter is a differentiability parameter,
// then it already has a corresponding differential parameter. Do not add
// a corresponding differential result.
if (config.parameterIndices->contains(paramIndex))
continue;
auto inoutParam = origTy->getParameters()[paramIndex];
auto paramTan = inoutParam.getInterfaceType()->getAutoDiffTangentSpace(
lookupConformance);
assert(paramTan && "Parameter type does not have a tangent space?");
dfResults.push_back(
{paramTan->getCanonicalType(), ResultConvention::Indirect});
}
}
// Add differential parameters for the requested wrt parameters.
for (auto i : config.parameterIndices->getIndices()) {
auto origParam = origParams[i];
origParam = origParam.getWithInterfaceType(
origParam.getInterfaceType()->getReducedType(witnessCanGenSig));
dfParams.push_back(
SILParameterInfo(origParam.getInterfaceType()
->getAutoDiffTangentSpace(lookupConformance)
->getType()
->getReducedType(witnessCanGenSig),
origParam.getConvention()));
}
// Accept a differential struct in the differential parameter list. This is
// the returned differential's closure context.
auto *origEntry = original->getEntryBlock();
auto dfTupleType =
linearMapInfo->getLinearMapTupleLoweredType(origEntry).getASTType();
dfParams.push_back({dfTupleType, ParameterConvention::Direct_Owned});
Mangle::DifferentiationMangler mangler;
auto diffName = mangler.mangleLinearMap(
witness->getOriginalFunction()->getName(),
AutoDiffLinearMapKind::Differential, witness->getConfig());
// Set differential generic signature equal to JVP generic signature.
// Do not use witness generic signature, which may have same-type requirements
// binding all generic parameters to concrete types.
auto diffGenericSig =
jvp->getLoweredFunctionType()->getSubstGenericSignature();
auto *diffGenericEnv = diffGenericSig.getGenericEnvironment();
auto diffType = SILFunctionType::get(
diffGenericSig, SILExtInfo::getThin(), origTy->getCoroutineKind(),
origTy->getCalleeConvention(), dfParams, {}, dfResults, std::nullopt,
origTy->getPatternSubstitutions(), origTy->getInvocationSubstitutions(),
original->getASTContext());
SILOptFunctionBuilder fb(context.getTransform());
auto linkage = jvp->isSerialized() ? SILLinkage::Public : SILLinkage::Private;
auto *differential = fb.createFunction(
linkage, context.getASTContext().getIdentifier(diffName).str(), diffType,
diffGenericEnv, original->getLocation(), original->isBare(),
IsNotTransparent, jvp->getSerializedKind(),
original->isDynamicallyReplaceable(), original->isDistributed(),
original->isRuntimeAccessible());
differential->setDebugScope(
new (module) SILDebugScope(original->getLocation(), differential));
return differential;
}
bool JVPCloner::Implementation::run() {
PrettyStackTraceSILFunction trace("generating JVP and differential for",
original);
LLVM_DEBUG(getADDebugStream() << "Cloning original @" << original->getName()
<< " to jvp @" << jvp->getName() << '\n');
// Create JVP and differential entry and arguments.
auto *entry = jvp->createBasicBlock();
createEntryArguments(jvp);
prepareForDifferentialGeneration();
// Clone.
SmallVector<SILValue, 4> entryArgs(entry->getArguments().begin(),
entry->getArguments().end());
cloneFunctionBody(original, entry, entryArgs);
emitReturnInstForDifferential();
// If errors occurred, back out.
if (errorOccurred)
return true;
LLVM_DEBUG(getADDebugStream()
<< "Generated JVP for " << original->getName() << ":\n"
<< *jvp);
LLVM_DEBUG(getADDebugStream()
<< "Generated differential for " << original->getName() << ":\n"
<< getDifferential());
return errorOccurred;
}
} // end namespace autodiff
} // end namespace swift
bool JVPCloner::run() {
bool foundError = impl.run();
#ifndef NDEBUG
if (!foundError)
getJVP().verify();
#endif
return foundError;
}
SILFunction &JVPCloner::getJVP() const { return impl.getJVP(); }
|