Skip to content

[Mem2Reg] Lexical allocs create lexical borrows. #39460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ void SILCloner<ImplClass>::visitBeginBorrowInst(BeginBorrowInst *Inst) {

recordClonedInstruction(
Inst, getBuilder().createBeginBorrow(getOpLocation(Inst->getLoc()),
getOpValue(Inst->getOperand()),
getOpValue(Inst->getOperand()),
Inst->isLexical()));
}

Expand Down
6 changes: 3 additions & 3 deletions include/swift/SILOptimizer/Utils/InstOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ bool isAddressOfArrayElement(SILValue addr);
/// Move an ApplyInst's FuncRef so that it dominates the call site.
void placeFuncRef(ApplyInst *ai, DominanceInfo *dt);

/// Add an argument, \p val, to the branch-edge that is pointing into
/// Add arguments, \p vals, to the branch-edge that is pointing into
/// block \p Dest. Return a new instruction and do not erase the old
/// instruction.
TermInst *addArgumentToBranch(SILValue val, SILBasicBlock *dest,
TermInst *branch);
TermInst *addArgumentsToBranch(ArrayRef<SILValue> vals, SILBasicBlock *dest,
TermInst *branch);

/// Get the linkage to be used for specializations of a function with
/// the given linkage.
Expand Down
6 changes: 5 additions & 1 deletion lib/SILOptimizer/Transforms/AllocBoxToStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,15 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
SILBuilderWithScope Builder(ABI);
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
&& "rewriting multi-field box not implemented");
bool isLexical = ABI->getFunction()
->getModule()
.getASTContext()
.LangOpts.EnableExperimentalLexicalLifetimes;
auto *ASI = Builder.createAllocStack(
ABI->getLoc(),
getSILBoxFieldType(TypeExpansionContext(*ABI->getFunction()),
ABI->getBoxType(), ABI->getModule().Types, 0),
ABI->getVarInfo(), ABI->hasDynamicLifetime());
ABI->getVarInfo(), ABI->hasDynamicLifetime(), isLexical);

// Transfer a mark_uninitialized if we have one.
SILValue StackBox = ASI;
Expand Down
533 changes: 436 additions & 97 deletions lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/SILOptimizer/Transforms/SILSROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {
// TODO: Add op_fragment support for tuple type
for (unsigned EltNo : indices(TT->getElementTypes())) {
SILType EltTy = Type.getTupleElementType(EltNo);
NewAllocations.push_back(
B.createAllocStack(Loc, EltTy, {}, AI->hasDynamicLifetime()));
NewAllocations.push_back(B.createAllocStack(
Loc, EltTy, {}, AI->hasDynamicLifetime(), AI->isLexical()));
}
} else {
assert(SD && "SD should not be null since either it or TT must be set at "
Expand All @@ -218,7 +218,7 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {

NewAllocations.push_back(B.createAllocStack(
Loc, Type.getFieldType(VD, M, TypeExpansionContext(B.getFunction())),
NewDebugVarInfo, AI->hasDynamicLifetime()));
NewDebugVarInfo, AI->hasDynamicLifetime(), AI->isLexical()));
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,8 +864,8 @@ void swift::placeFuncRef(ApplyInst *ai, DominanceInfo *domInfo) {
/// Add an argument, \p val, to the branch-edge that is pointing into
/// block \p Dest. Return a new instruction and do not erase the old
/// instruction.
TermInst *swift::addArgumentToBranch(SILValue val, SILBasicBlock *dest,
TermInst *branch) {
TermInst *swift::addArgumentsToBranch(ArrayRef<SILValue> vals,
SILBasicBlock *dest, TermInst *branch) {
SILBuilderWithScope builder(branch);

if (auto *cbi = dyn_cast<CondBranchInst>(branch)) {
Expand All @@ -879,10 +879,12 @@ TermInst *swift::addArgumentToBranch(SILValue val, SILBasicBlock *dest,
falseArgs.push_back(arg);

if (dest == cbi->getTrueBB()) {
trueArgs.push_back(val);
for (auto val : vals)
trueArgs.push_back(val);
assert(trueArgs.size() == dest->getNumArguments());
} else {
falseArgs.push_back(val);
for (auto val : vals)
falseArgs.push_back(val);
assert(falseArgs.size() == dest->getNumArguments());
}

Expand All @@ -898,7 +900,8 @@ TermInst *swift::addArgumentToBranch(SILValue val, SILBasicBlock *dest,
for (auto arg : bi->getArgs())
args.push_back(arg);

args.push_back(val);
for (auto val : vals)
args.push_back(val);
assert(args.size() == dest->getNumArguments());
return builder.createBranch(bi->getLoc(), bi->getDestBB(), args);
}
Expand Down
30 changes: 30 additions & 0 deletions test/SILOptimizer/allocbox_to_stack_lifetime.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -allocbox-to-stack -enable-experimental-lexical-lifetimes | %FileCheck %s

import Builtin

struct Int {
var _value: Builtin.Int64
}

struct Bool {
var _value: Builtin.Int1
}

protocol Error {}

// CHECK-LABEL: sil [ossa] @simple_promotion
sil [ossa] @simple_promotion : $@convention(thin) (Int) -> Int {
bb0(%0 : $Int):
%1 = alloc_box ${ var Int }
%1a = project_box %1 : ${ var Int }, 0
store %0 to [trivial] %1a : $*Int

%3 = load [trivial] %1a : $*Int
destroy_value %1 : ${ var Int }
return %3 : $Int
// CHECK: alloc_stack [lexical]
// CHECK-NOT: alloc_box
// CHECK-NOT: destroy_value
// CHECK: return
}

Loading