Skip to content

Commit 8b08ac1

Browse files
committed
Fix SILInliner to create move_value in OSSA only
1 parent 11c32fa commit 8b08ac1

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,7 @@ class SILBuilder {
12781278
}
12791279

12801280
CopyValueInst *createCopyValue(SILLocation Loc, SILValue operand) {
1281+
assert(getFunction().hasOwnership());
12811282
assert(!operand->getType().isTrivial(getFunction()) &&
12821283
"Should not be passing trivial values to this api. Use instead "
12831284
"emitCopyValueOperation");
@@ -1296,6 +1297,7 @@ class SILBuilder {
12961297

12971298
DestroyValueInst *createDestroyValue(SILLocation Loc, SILValue operand,
12981299
bool poisonRefs = false) {
1300+
assert(getFunction().hasOwnership());
12991301
assert(isLoadableOrOpaque(operand->getType()));
13001302
assert(!operand->getType().isTrivial(getFunction()) &&
13011303
"Should not be passing trivial values to this api. Use instead "
@@ -1306,6 +1308,7 @@ class SILBuilder {
13061308

13071309
MoveValueInst *createMoveValue(SILLocation loc, SILValue operand,
13081310
bool isLexical = false) {
1311+
assert(getFunction().hasOwnership());
13091312
assert(!operand->getType().isTrivial(getFunction()) &&
13101313
"Should not be passing trivial values to this api. Use instead "
13111314
"emitMoveValueOperation");

include/swift/SIL/SILCloner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,9 @@ void SILCloner<ImplClass>::visitExplicitCopyValueInst(
18701870
template <typename ImplClass>
18711871
void SILCloner<ImplClass>::visitMoveValueInst(MoveValueInst *Inst) {
18721872
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
1873+
if (!getBuilder().hasOwnership()) {
1874+
return recordFoldedValue(Inst, getOpValue(Inst->getOperand()));
1875+
}
18731876
auto *MVI = getBuilder().createMoveValue(getOpLocation(Inst->getLoc()),
18741877
getOpValue(Inst->getOperand()),
18751878
Inst->isLexical());

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -449,50 +449,58 @@ void SILInlineCloner::cloneInline(ArrayRef<SILValue> AppliedArgs) {
449449

450450
SmallVector<SILValue, 4> entryArgs;
451451
entryArgs.reserve(AppliedArgs.size());
452+
453+
auto calleeConv = getCalleeFunction()->getConventions();
452454
SmallBitVector borrowedArgs(AppliedArgs.size());
453455
SmallBitVector copiedArgs(AppliedArgs.size());
456+
if (!Apply->getFunction()->hasOwnership()) {
454457

455-
auto calleeConv = getCalleeFunction()->getConventions();
456-
for (auto p : llvm::enumerate(AppliedArgs)) {
457-
SILValue callArg = p.value();
458-
SWIFT_DEFER { entryArgs.push_back(callArg); };
459-
unsigned idx = p.index();
460-
if (idx >= calleeConv.getSILArgIndexOfFirstParam()) {
461-
auto paramInfo = calleeConv.getParamInfoForSILArg(idx);
462-
if (callArg->getType().isAddress()) {
463-
// If lexical lifetimes are enabled, any alloc_stacks in the caller that
464-
// are passed to the callee being inlined (except mutating exclusive
465-
// accesses) need to be promoted to be lexical. Otherwise,
466-
// destroy_addrs could be hoisted through the body of the newly inlined
467-
// function without regard to the deinit barriers it contains.
468-
//
469-
// TODO: [begin_borrow_addr] Instead of marking the alloc_stack as a
470-
// whole lexical, just mark the inlined range lexical via
471-
// begin_borrow_addr [lexical]/end_borrow_addr just as is done
472-
// with values.
473-
auto &module = Apply.getFunction()->getModule();
474-
auto enableLexicalLifetimes =
475-
module.getASTContext().SILOpts.supportsLexicalLifetimes(module);
476-
if (!enableLexicalLifetimes)
477-
continue;
478-
479-
// Exclusive mutating accesses don't entail a lexical scope.
480-
if (paramInfo.getConvention() == ParameterConvention::Indirect_Inout)
481-
continue;
482-
483-
auto storage = AccessStorageWithBase::compute(callArg);
484-
if (auto *asi = dyn_cast_or_null<AllocStackInst>(storage.base))
485-
asi->setIsLexical();
486-
} else {
487-
// Insert begin/end borrow for guaranteed arguments.
488-
if (paramInfo.isGuaranteed()) {
489-
if (SILValue newValue = borrowFunctionArgument(callArg, idx)) {
490-
callArg = newValue;
491-
borrowedArgs[idx] = true;
492-
}
493-
} else if (paramInfo.isConsumed()) {
494-
if (SILValue newValue = moveFunctionArgument(callArg, idx)) {
495-
callArg = newValue;
458+
for (auto p : llvm::enumerate(AppliedArgs)) {
459+
SILValue callArg = p.value();
460+
entryArgs.push_back(callArg);
461+
}
462+
} else {
463+
for (auto p : llvm::enumerate(AppliedArgs)) {
464+
SILValue callArg = p.value();
465+
SWIFT_DEFER { entryArgs.push_back(callArg); };
466+
unsigned idx = p.index();
467+
if (idx >= calleeConv.getSILArgIndexOfFirstParam()) {
468+
auto paramInfo = calleeConv.getParamInfoForSILArg(idx);
469+
if (callArg->getType().isAddress()) {
470+
// If lexical lifetimes are enabled, any alloc_stacks in the caller
471+
// that are passed to the callee being inlined (except mutating
472+
// exclusive accesses) need to be promoted to be lexical. Otherwise,
473+
// destroy_addrs could be hoisted through the body of the newly
474+
// inlined function without regard to the deinit barriers it contains.
475+
//
476+
// TODO: [begin_borrow_addr] Instead of marking the alloc_stack as a
477+
// whole lexical, just mark the inlined range lexical via
478+
// begin_borrow_addr [lexical]/end_borrow_addr just as is done
479+
// with values.
480+
auto &module = Apply.getFunction()->getModule();
481+
auto enableLexicalLifetimes =
482+
module.getASTContext().SILOpts.supportsLexicalLifetimes(module);
483+
if (!enableLexicalLifetimes)
484+
continue;
485+
486+
// Exclusive mutating accesses don't entail a lexical scope.
487+
if (paramInfo.getConvention() == ParameterConvention::Indirect_Inout)
488+
continue;
489+
490+
auto storage = AccessStorageWithBase::compute(callArg);
491+
if (auto *asi = dyn_cast_or_null<AllocStackInst>(storage.base))
492+
asi->setIsLexical();
493+
} else {
494+
// Insert begin/end borrow for guaranteed arguments.
495+
if (paramInfo.isGuaranteed()) {
496+
if (SILValue newValue = borrowFunctionArgument(callArg, idx)) {
497+
callArg = newValue;
498+
borrowedArgs[idx] = true;
499+
}
500+
} else if (paramInfo.isConsumed()) {
501+
if (SILValue newValue = moveFunctionArgument(callArg, idx)) {
502+
callArg = newValue;
503+
}
496504
}
497505
}
498506
}

0 commit comments

Comments
 (0)