Skip to content

Commit 93672e3

Browse files
authored
Merge pull request swiftlang#28060 from gottesmm/pr-bc9bc2096e0844c7b311502cfc94d8a6e0cf0fbe
2 parents fd55ab7 + 8d9455e commit 93672e3

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,8 @@ SILValue AvailableValueAggregator::handlePrimitiveValue(SILType loadTy,
753753
SingleValueInstruction *
754754
AvailableValueAggregator::addMissingDestroysForCopiedValues(
755755
SingleValueInstruction *svi, SILValue newVal) {
756-
// If ownership is not enabled... bail. We do not need to do this since we do
757-
// not need to insert an extra copy unless we have ownership since without
758-
// ownership stores do not consume.
759-
if (!B.hasOwnership())
760-
return svi;
756+
assert(B.hasOwnership() &&
757+
"We assume this is only called if we have ownership");
761758

762759
assert((isa<LoadBorrowInst>(svi) || isa<LoadInst>(svi)) &&
763760
"Expected to have a /real/ load here since we assume that we have a "
@@ -1492,9 +1489,9 @@ class AllocOptimize {
14921489
Optional<std::pair<SILType, unsigned>>
14931490
computeAvailableValues(SILValue SrcAddr, SILInstruction *Inst,
14941491
SmallVectorImpl<AvailableValue> &AvailableValues);
1495-
bool promoteLoadCopy(LoadInst *Inst);
1496-
bool promoteLoadBorrow(LoadBorrowInst *Inst);
1497-
bool promoteCopyAddr(CopyAddrInst *CAI);
1492+
bool promoteLoadCopy(LoadInst *li);
1493+
bool promoteLoadBorrow(LoadBorrowInst *lbi);
1494+
bool promoteCopyAddr(CopyAddrInst *cai);
14981495
void promoteLoadTake(LoadInst *Inst, MutableArrayRef<AvailableValue> values);
14991496
void promoteDestroyAddr(DestroyAddrInst *dai,
15001497
MutableArrayRef<AvailableValue> values);
@@ -1570,7 +1567,7 @@ static SILValue tryFindSrcAddrForLoad(SILInstruction *i) {
15701567
/// cross element accesses have been scalarized.
15711568
///
15721569
/// This returns true if the load has been removed from the program.
1573-
bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
1570+
bool AllocOptimize::promoteLoadCopy(LoadInst *li) {
15741571
// Note that we intentionally don't support forwarding of weak pointers,
15751572
// because the underlying value may drop be deallocated at any time. We would
15761573
// have to prove that something in this function is holding the weak value
@@ -1579,29 +1576,40 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
15791576

15801577
// First attempt to find a source addr for our "load" instruction. If we fail
15811578
// to find a valid value, just return.
1582-
SILValue SrcAddr = tryFindSrcAddrForLoad(Inst);
1583-
if (!SrcAddr)
1579+
SILValue srcAddr = tryFindSrcAddrForLoad(li);
1580+
if (!srcAddr)
15841581
return false;
15851582

1586-
SmallVector<AvailableValue, 8> AvailableValues;
1587-
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1588-
if (!Result.hasValue())
1583+
SmallVector<AvailableValue, 8> availableValues;
1584+
auto result = computeAvailableValues(srcAddr, li, availableValues);
1585+
if (!result.hasValue())
15891586
return false;
15901587

1591-
SILType LoadTy = Result->first;
1592-
unsigned FirstElt = Result->second;
1588+
SILType loadTy = result->first;
1589+
unsigned firstElt = result->second;
15931590

15941591
// Aggregate together all of the subelements into something that has the same
15951592
// type as the load did, and emit smaller loads for any subelements that were
15961593
// not available. We are "propagating" a +1 available value from the store
15971594
// points.
1598-
auto *load = dyn_cast<SingleValueInstruction>(Inst);
1599-
AvailableValueAggregator agg(load, AvailableValues, Uses, deadEndBlocks,
1595+
AvailableValueAggregator agg(li, availableValues, Uses, deadEndBlocks,
16001596
false /*isTake*/);
1601-
SILValue newVal = agg.aggregateValues(LoadTy, load->getOperand(0), FirstElt);
1597+
SILValue newVal = agg.aggregateValues(loadTy, li->getOperand(), firstElt);
16021598

1603-
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *load << "\n");
1599+
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *li << "\n");
16041600
LLVM_DEBUG(llvm::dbgs() << " To value: " << *newVal << "\n");
1601+
++NumLoadPromoted;
1602+
1603+
// If we did not have ownership, we did not insert extra copies at our stores,
1604+
// so we can just RAUW and return.
1605+
if (!li->getFunction()->hasOwnership()) {
1606+
li->replaceAllUsesWith(newVal);
1607+
SILValue addr = li->getOperand();
1608+
li->eraseFromParent();
1609+
if (auto *addrI = addr->getDefiningInstruction())
1610+
recursivelyDeleteTriviallyDeadInstructions(addrI);
1611+
return true;
1612+
}
16051613

16061614
// If we inserted any copies, we created the copies at our stores. We know
16071615
// that in our load block, we will reform the aggregate as appropriate at the
@@ -1610,9 +1618,7 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
16101618
// blocks that we may have can be found by performing a linear lifetime check
16111619
// over all copies that we found using the load as the "consuming uses" (just
16121620
// for the purposes of identifying the consuming block).
1613-
auto *oldLoad = agg.addMissingDestroysForCopiedValues(load, newVal);
1614-
1615-
++NumLoadPromoted;
1621+
auto *oldLoad = agg.addMissingDestroysForCopiedValues(li, newVal);
16161622

16171623
// If we are returned the load, eliminate it. Otherwise, it was already
16181624
// handled for us... so return true.
@@ -1627,7 +1633,7 @@ bool AllocOptimize::promoteLoadCopy(LoadInst *Inst) {
16271633
return true;
16281634
}
16291635

1630-
bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
1636+
bool AllocOptimize::promoteCopyAddr(CopyAddrInst *cai) {
16311637
// Note that we intentionally don't support forwarding of weak pointers,
16321638
// because the underlying value may drop be deallocated at any time. We would
16331639
// have to prove that something in this function is holding the weak value
@@ -1636,19 +1642,19 @@ bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
16361642

16371643
// First attempt to find a source addr for our "load" instruction. If we fail
16381644
// to find a valid value, just return.
1639-
SILValue SrcAddr = tryFindSrcAddrForLoad(Inst);
1640-
if (!SrcAddr)
1645+
SILValue srcAddr = tryFindSrcAddrForLoad(cai);
1646+
if (!srcAddr)
16411647
return false;
16421648

1643-
SmallVector<AvailableValue, 8> AvailableValues;
1644-
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1645-
if (!Result.hasValue())
1649+
SmallVector<AvailableValue, 8> availableValues;
1650+
auto result = computeAvailableValues(srcAddr, cai, availableValues);
1651+
if (!result.hasValue())
16461652
return false;
16471653

16481654
// Ok, we have some available values. If we have a copy_addr, explode it now,
16491655
// exposing the load operation within it. Subsequent optimization passes will
16501656
// see the load and propagate the available values into it.
1651-
DataflowContext.explodeCopyAddr(Inst);
1657+
DataflowContext.explodeCopyAddr(cai);
16521658

16531659
// This is removing the copy_addr, but explodeCopyAddr takes care of
16541660
// removing the instruction from Uses for us, so we return false.
@@ -1661,7 +1667,7 @@ bool AllocOptimize::promoteCopyAddr(CopyAddrInst *Inst) {
16611667
/// cross element accesses have been scalarized.
16621668
///
16631669
/// This returns true if the load has been removed from the program.
1664-
bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
1670+
bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *lbi) {
16651671
// Note that we intentionally don't support forwarding of weak pointers,
16661672
// because the underlying value may drop be deallocated at any time. We would
16671673
// have to prove that something in this function is holding the weak value
@@ -1670,28 +1676,27 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
16701676

16711677
// First attempt to find a source addr for our "load" instruction. If we fail
16721678
// to find a valid value, just return.
1673-
SILValue SrcAddr = tryFindSrcAddrForLoad(Inst);
1674-
if (!SrcAddr)
1679+
SILValue srcAddr = tryFindSrcAddrForLoad(lbi);
1680+
if (!srcAddr)
16751681
return false;
16761682

1677-
SmallVector<AvailableValue, 8> AvailableValues;
1678-
auto Result = computeAvailableValues(SrcAddr, Inst, AvailableValues);
1679-
if (!Result.hasValue())
1683+
SmallVector<AvailableValue, 8> availableValues;
1684+
auto result = computeAvailableValues(srcAddr, lbi, availableValues);
1685+
if (!result.hasValue())
16801686
return false;
16811687

1682-
SILType LoadTy = Result->first;
1683-
unsigned FirstElt = Result->second;
1688+
SILType loadTy = result->first;
1689+
unsigned firstElt = result->second;
16841690

16851691
// Aggregate together all of the subelements into something that has the same
16861692
// type as the load did, and emit smaller loads for any subelements that were
16871693
// not available. We are "propagating" a +1 available value from the store
16881694
// points.
1689-
auto *load = dyn_cast<SingleValueInstruction>(Inst);
1690-
AvailableValueAggregator agg(load, AvailableValues, Uses, deadEndBlocks,
1695+
AvailableValueAggregator agg(lbi, availableValues, Uses, deadEndBlocks,
16911696
false /*isTake*/);
1692-
SILValue newVal = agg.aggregateValues(LoadTy, load->getOperand(0), FirstElt);
1697+
SILValue newVal = agg.aggregateValues(loadTy, lbi->getOperand(), firstElt);
16931698

1694-
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *load << "\n");
1699+
LLVM_DEBUG(llvm::dbgs() << " *** Promoting load: " << *lbi << "\n");
16951700
LLVM_DEBUG(llvm::dbgs() << " To value: " << *newVal << "\n");
16961701

16971702
// If we inserted any copies, we created the copies at our stores. We know
@@ -1701,7 +1706,7 @@ bool AllocOptimize::promoteLoadBorrow(LoadBorrowInst *Inst) {
17011706
// blocks that we may have can be found by performing a linear lifetime check
17021707
// over all copies that we found using the load as the "consuming uses" (just
17031708
// for the purposes of identifying the consuming block).
1704-
auto *oldLoad = agg.addMissingDestroysForCopiedValues(load, newVal);
1709+
auto *oldLoad = agg.addMissingDestroysForCopiedValues(lbi, newVal);
17051710

17061711
++NumLoadPromoted;
17071712

0 commit comments

Comments
 (0)