Skip to content

Commit 6bb9253

Browse files
committed
[mem-access-utils] Allow for getStackInitAllocStackUse to recognize an always take from a checked_cast_addr_br as a destroy_addr.
Since it is an always take, we know that the original value will always be invalidated by the checked_cast_addr_br. This also lets me use this to recognize simple cases of checked casts in opt-remark-gen.
1 parent 8750b40 commit 6bb9253

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/swift/SIL/Consumption.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#ifndef SWIFT_SIL_CONSUMPTION_H
1919
#define SWIFT_SIL_CONSUMPTION_H
2020

21+
#include "llvm/Support/ErrorHandling.h"
22+
#include <cstdint>
23+
2124
namespace swift {
2225

2326
/// Is an operation a "take"? A take consumes the original value,

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/SIL/SILModule.h"
1717
#include "swift/SIL/SILUndef.h"
1818
#include "swift/SIL/DynamicCasts.h"
19+
#include "swift/SIL/Consumption.h"
1920
#include "llvm/Support/Debug.h"
2021

2122
using namespace swift;
@@ -1578,6 +1579,21 @@ swift::getSingleInitAllocStackUse(AllocStackInst *asi,
15781579
switch (user->getKind()) {
15791580
default:
15801581
break;
1582+
case SILInstructionKind::CheckedCastAddrBranchInst: {
1583+
auto *ccabi = cast<CheckedCastAddrBranchInst>(user);
1584+
// We only handle the case where we are doing a take of our alloc_stack as
1585+
// a source.
1586+
//
1587+
// TODO: Can we expand this?
1588+
if (use->get() == ccabi->getDest())
1589+
break;
1590+
if (ccabi->getConsumptionKind() != CastConsumptionKind::TakeAlways)
1591+
break;
1592+
// Ok, we are the Src and are performing a take. Treat it as a destroy!
1593+
if (destroyingUses)
1594+
destroyingUses->push_back(use);
1595+
continue;
1596+
}
15811597
case SILInstructionKind::DestroyAddrInst:
15821598
if (destroyingUses)
15831599
destroyingUses->push_back(use);

0 commit comments

Comments
 (0)