Skip to content

Commit f872d00

Browse files
authored
Merge pull request #39460 from nate-chandler/lexical_lifetimes/mem2reg
2 parents 86a4e05 + 79bc4cb commit f872d00

File tree

10 files changed

+1416
-110
lines changed

10 files changed

+1416
-110
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ void SILCloner<ImplClass>::visitBeginBorrowInst(BeginBorrowInst *Inst) {
10831083

10841084
recordClonedInstruction(
10851085
Inst, getBuilder().createBeginBorrow(getOpLocation(Inst->getLoc()),
1086-
getOpValue(Inst->getOperand()),
1086+
getOpValue(Inst->getOperand()),
10871087
Inst->isLexical()));
10881088
}
10891089

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ bool isAddressOfArrayElement(SILValue addr);
190190
/// Move an ApplyInst's FuncRef so that it dominates the call site.
191191
void placeFuncRef(ApplyInst *ai, DominanceInfo *dt);
192192

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

199199
/// Get the linkage to be used for specializations of a function with
200200
/// the given linkage.

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,15 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
533533
SILBuilderWithScope Builder(ABI);
534534
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
535535
&& "rewriting multi-field box not implemented");
536+
bool isLexical = ABI->getFunction()
537+
->getModule()
538+
.getASTContext()
539+
.LangOpts.EnableExperimentalLexicalLifetimes;
536540
auto *ASI = Builder.createAllocStack(
537541
ABI->getLoc(),
538542
getSILBoxFieldType(TypeExpansionContext(*ABI->getFunction()),
539543
ABI->getBoxType(), ABI->getModule().Types, 0),
540-
ABI->getVarInfo(), ABI->hasDynamicLifetime());
544+
ABI->getVarInfo(), ABI->hasDynamicLifetime(), isLexical);
541545

542546
// Transfer a mark_uninitialized if we have one.
543547
SILValue StackBox = ASI;

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 436 additions & 97 deletions
Large diffs are not rendered by default.

lib/SILOptimizer/Transforms/SILSROA.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {
202202
// TODO: Add op_fragment support for tuple type
203203
for (unsigned EltNo : indices(TT->getElementTypes())) {
204204
SILType EltTy = Type.getTupleElementType(EltNo);
205-
NewAllocations.push_back(
206-
B.createAllocStack(Loc, EltTy, {}, AI->hasDynamicLifetime()));
205+
NewAllocations.push_back(B.createAllocStack(
206+
Loc, EltTy, {}, AI->hasDynamicLifetime(), AI->isLexical()));
207207
}
208208
} else {
209209
assert(SD && "SD should not be null since either it or TT must be set at "
@@ -218,7 +218,7 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {
218218

219219
NewAllocations.push_back(B.createAllocStack(
220220
Loc, Type.getFieldType(VD, M, TypeExpansionContext(B.getFunction())),
221-
NewDebugVarInfo, AI->hasDynamicLifetime()));
221+
NewDebugVarInfo, AI->hasDynamicLifetime(), AI->isLexical()));
222222
}
223223
}
224224
}

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,8 @@ void swift::placeFuncRef(ApplyInst *ai, DominanceInfo *domInfo) {
864864
/// Add an argument, \p val, to the branch-edge that is pointing into
865865
/// block \p Dest. Return a new instruction and do not erase the old
866866
/// instruction.
867-
TermInst *swift::addArgumentToBranch(SILValue val, SILBasicBlock *dest,
868-
TermInst *branch) {
867+
TermInst *swift::addArgumentsToBranch(ArrayRef<SILValue> vals,
868+
SILBasicBlock *dest, TermInst *branch) {
869869
SILBuilderWithScope builder(branch);
870870

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

881881
if (dest == cbi->getTrueBB()) {
882-
trueArgs.push_back(val);
882+
for (auto val : vals)
883+
trueArgs.push_back(val);
883884
assert(trueArgs.size() == dest->getNumArguments());
884885
} else {
885-
falseArgs.push_back(val);
886+
for (auto val : vals)
887+
falseArgs.push_back(val);
886888
assert(falseArgs.size() == dest->getNumArguments());
887889
}
888890

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

901-
args.push_back(val);
903+
for (auto val : vals)
904+
args.push_back(val);
902905
assert(args.size() == dest->getNumArguments());
903906
return builder.createBranch(bi->getLoc(), bi->getDestBB(), args);
904907
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -allocbox-to-stack -enable-experimental-lexical-lifetimes | %FileCheck %s
2+
3+
import Builtin
4+
5+
struct Int {
6+
var _value: Builtin.Int64
7+
}
8+
9+
struct Bool {
10+
var _value: Builtin.Int1
11+
}
12+
13+
protocol Error {}
14+
15+
// CHECK-LABEL: sil [ossa] @simple_promotion
16+
sil [ossa] @simple_promotion : $@convention(thin) (Int) -> Int {
17+
bb0(%0 : $Int):
18+
%1 = alloc_box ${ var Int }
19+
%1a = project_box %1 : ${ var Int }, 0
20+
store %0 to [trivial] %1a : $*Int
21+
22+
%3 = load [trivial] %1a : $*Int
23+
destroy_value %1 : ${ var Int }
24+
return %3 : $Int
25+
// CHECK: alloc_stack [lexical]
26+
// CHECK-NOT: alloc_box
27+
// CHECK-NOT: destroy_value
28+
// CHECK: return
29+
}
30+

0 commit comments

Comments
 (0)