Skip to content

Commit 04c20b8

Browse files
authored
Merge pull request #30340 from apple/revert-29812-captureconv
Revert "Use in_guaranteed for let captures"
2 parents 8e39711 + ccbc26d commit 04c20b8

23 files changed

+115
-293
lines changed

include/swift/SIL/TypeLowering.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,6 @@ enum class CaptureKind {
561561
StorageAddress,
562562
/// A local value captured as a constant.
563563
Constant,
564-
/// A let constant captured as a pointer to storage
565-
Immutable
566564
};
567565

568566

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,12 @@ void releasePartialApplyCapturedArg(
364364
SILParameterInfo paramInfo,
365365
InstModCallbacks callbacks = InstModCallbacks());
366366

367-
void deallocPartialApplyCapturedArg(
368-
SILBuilder &builder, SILLocation loc, SILValue arg,
369-
SILParameterInfo paramInfo);
370-
371367
/// Insert destroys of captured arguments of partial_apply [stack].
372368
void insertDestroyOfCapturedArguments(
373369
PartialApplyInst *pai, SILBuilder &builder,
374370
llvm::function_ref<bool(SILValue)> shouldInsertDestroy =
375371
[](SILValue arg) -> bool { return true; });
376372

377-
void insertDeallocOfCapturedArguments(
378-
PartialApplyInst *pai, SILBuilder &builder);
379-
380373
/// This iterator 'looks through' one level of builtin expect users exposing all
381374
/// users of the looked through builtin expect instruction i.e it presents a
382375
/// view that shows all users as if there were no builtin expect instructions

lib/IRGen/GenFunc.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,25 +1319,9 @@ Optional<StackAddress> irgen::emitFunctionPartialApplication(
13191319
SmallVector<SILType, 4> argValTypes;
13201320
SmallVector<ParameterConvention, 4> argConventions;
13211321

1322-
// Go over the params and check if any of them can cause the HeapLayout to be non-fixed
1323-
// This is needed because we should not consider parameters of kind ClassPointer and Metadata sources
1324-
// If not, we may end up with missing TypeMetadata for a type dependent generic parameter
1325-
// while generating code for destructor of HeapLayout.
1326-
bool considerParameterSources = true;
1327-
for (auto param : params) {
1328-
SILType argType = IGF.IGM.silConv.getSILType(param, origType);
1329-
auto argLoweringTy = getArgumentLoweringType(argType.getASTType(), param);
1330-
auto &ti = IGF.getTypeInfoForLowered(argLoweringTy);
1331-
1332-
if (!isa<FixedTypeInfo>(ti)) {
1333-
considerParameterSources = false;
1334-
break;
1335-
}
1336-
}
1337-
13381322
// Reserve space for polymorphic bindings.
13391323
auto bindings =
1340-
NecessaryBindings::forPartialApplyForwarder(IGF.IGM, origType, subs, considerParameterSources);
1324+
NecessaryBindings::forPartialApplyForwarder(IGF.IGM, origType, subs);
13411325

13421326
if (!bindings.empty()) {
13431327
hasSingleSwiftRefcountedContext = No;
@@ -1540,7 +1524,7 @@ Optional<StackAddress> irgen::emitFunctionPartialApplication(
15401524
HeapNonFixedOffsets offsets(IGF, layout);
15411525
if (outType->isNoEscape()) {
15421526
stackAddr = IGF.emitDynamicAlloca(
1543-
IGF.IGM.Int8Ty, layout.isFixedLayout() ? layout.emitSize(IGF.IGM) : offsets.getSize() , Alignment(16));
1527+
IGF.IGM.Int8Ty, layout.emitSize(IGF.IGM), Alignment(16));
15441528
stackAddr = stackAddr->withAddress(IGF.Builder.CreateBitCast(
15451529
stackAddr->getAddress(), IGF.IGM.OpaquePtrTy));
15461530
data = stackAddr->getAddress().getAddress();

lib/IRGen/GenProto.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class PolymorphicConvention {
112112
}
113113

114114
public:
115-
PolymorphicConvention(IRGenModule &IGM, CanSILFunctionType fnType, bool considerParameterSources);
115+
PolymorphicConvention(IRGenModule &IGM, CanSILFunctionType fnType);
116116

117117
ArrayRef<MetadataSource> getSources() const { return Sources; }
118118

@@ -179,9 +179,8 @@ class PolymorphicConvention {
179179
} // end anonymous namespace
180180

181181
PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,
182-
CanSILFunctionType fnType,
183-
bool considerParameterSources = true)
184-
: IGM(IGM), M(*IGM.getSwiftModule()), FnType(fnType){
182+
CanSILFunctionType fnType)
183+
: IGM(IGM), M(*IGM.getSwiftModule()), FnType(fnType) {
185184
initGenerics();
186185

187186
auto rep = fnType->getRepresentation();
@@ -213,18 +212,16 @@ PolymorphicConvention::PolymorphicConvention(IRGenModule &IGM,
213212
unsigned selfIndex = ~0U;
214213
auto params = fnType->getParameters();
215214

216-
if (considerParameterSources) {
217-
// Consider 'self' first.
218-
if (fnType->hasSelfParam()) {
219-
selfIndex = params.size() - 1;
220-
considerParameter(params[selfIndex], selfIndex, true);
221-
}
215+
// Consider 'self' first.
216+
if (fnType->hasSelfParam()) {
217+
selfIndex = params.size() - 1;
218+
considerParameter(params[selfIndex], selfIndex, true);
219+
}
222220

223-
// Now consider the rest of the parameters.
224-
for (auto index : indices(params)) {
225-
if (index != selfIndex)
226-
considerParameter(params[index], index, false);
227-
}
221+
// Now consider the rest of the parameters.
222+
for (auto index : indices(params)) {
223+
if (index != selfIndex)
224+
considerParameter(params[index], index, false);
228225
}
229226
}
230227
}
@@ -3003,16 +3000,14 @@ NecessaryBindings::forFunctionInvocations(IRGenModule &IGM,
30033000
NecessaryBindings
30043001
NecessaryBindings::forPartialApplyForwarder(IRGenModule &IGM,
30053002
CanSILFunctionType origType,
3006-
SubstitutionMap subs,
3007-
bool considerParameterSources) {
3003+
SubstitutionMap subs) {
30083004
return computeBindings(IGM, origType, subs,
3009-
true /*forPartialApplyForwarder*/,
3010-
considerParameterSources);
3005+
true /*forPartialApplyForwarder*/);
30113006
}
30123007

30133008
NecessaryBindings NecessaryBindings::computeBindings(
30143009
IRGenModule &IGM, CanSILFunctionType origType, SubstitutionMap subs,
3015-
bool forPartialApplyForwarder, bool considerParameterSources) {
3010+
bool forPartialApplyForwarder) {
30163011

30173012
NecessaryBindings bindings;
30183013
bindings.forPartialApply = forPartialApplyForwarder;
@@ -3022,7 +3017,7 @@ NecessaryBindings NecessaryBindings::computeBindings(
30223017
return bindings;
30233018

30243019
// Figure out what we're actually required to pass:
3025-
PolymorphicConvention convention(IGM, origType, considerParameterSources);
3020+
PolymorphicConvention convention(IGM, origType);
30263021

30273022
// - unfulfilled requirements
30283023
convention.enumerateUnfulfilledRequirements(

lib/IRGen/NecessaryBindings.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ class NecessaryBindings {
5959
SubstitutionMap subs);
6060
static NecessaryBindings forPartialApplyForwarder(IRGenModule &IGM,
6161
CanSILFunctionType origType,
62-
SubstitutionMap subs,
63-
bool considerParameterSources = true);
62+
SubstitutionMap subs);
6463

6564
/// Add whatever information is necessary to reconstruct type metadata
6665
/// for the given type.
@@ -99,8 +98,7 @@ class NecessaryBindings {
9998
static NecessaryBindings computeBindings(IRGenModule &IGM,
10099
CanSILFunctionType origType,
101100
SubstitutionMap subs,
102-
bool forPartialApplyForwarder,
103-
bool considerParameterSources = true);
101+
bool forPartialApplyForwarder);
104102
};
105103

106104
} // end namespace irgen

lib/SIL/SILFunctionType.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,8 +1460,9 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
14601460
case CaptureKind::Constant: {
14611461
// Constants are captured by value.
14621462
ParameterConvention convention;
1463-
assert (!loweredTL.isAddressOnly());
1464-
if (loweredTL.isTrivial()) {
1463+
if (loweredTL.isAddressOnly()) {
1464+
convention = ParameterConvention::Indirect_In_Guaranteed;
1465+
} else if (loweredTL.isTrivial()) {
14651466
convention = ParameterConvention::Direct_Unowned;
14661467
} else {
14671468
convention = ParameterConvention::Direct_Guaranteed;
@@ -1494,16 +1495,6 @@ lowerCaptureContextParameters(TypeConverter &TC, SILDeclRef function,
14941495
inputs.push_back(param);
14951496
break;
14961497
}
1497-
case CaptureKind::Immutable: {
1498-
// 'let' constants that are address-only are captured as the address of the value
1499-
// and will be consumed by the closure.
1500-
SILType ty = loweredTy.getAddressType();
1501-
auto param =
1502-
SILParameterInfo(ty.getASTType(),
1503-
ParameterConvention::Indirect_In_Guaranteed);
1504-
inputs.push_back(param);
1505-
break;
1506-
}
15071498
}
15081499
}
15091500
}

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
11171117
if (VarInfo)
11181118
if (unsigned ArgNo = VarInfo->ArgNo) {
11191119
// It is a function argument.
1120-
if (ArgNo < DebugVars.size() && !DebugVars[ArgNo].empty() && !VarInfo->Name.empty()) {
1120+
if (ArgNo < DebugVars.size() && !DebugVars[ArgNo].empty()) {
11211121
require(
11221122
DebugVars[ArgNo] == VarInfo->Name,
11231123
"Scope contains conflicting debug variables for one function "

lib/SIL/TypeLowering.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ CaptureKind TypeConverter::getDeclCaptureKind(CapturedValue capture,
126126
return CaptureKind::Box;
127127
}
128128

129-
// For 'let' constants
130-
if (!var->supportsMutation()) {
131-
return CaptureKind::Immutable;
132-
}
133-
134129
// If we're capturing into a non-escaping closure, we can generally just
135130
// capture the address of the value as no-escape.
136131
return (capture.isNoEscape()

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ void SILGenFunction::emitCaptures(SILLocation loc,
262262
case CaptureKind::Constant:
263263
capturedArgs.push_back(emitUndef(getLoweredType(type)));
264264
break;
265-
case CaptureKind::Immutable:
266265
case CaptureKind::StorageAddress:
267266
capturedArgs.push_back(emitUndef(getLoweredType(type).getAddressType()));
268267
break;
@@ -334,23 +333,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,
334333
capturedArgs.push_back(emitManagedRValueWithCleanup(Val));
335334
break;
336335
}
337-
case CaptureKind::Immutable: {
338-
if (canGuarantee) {
339-
auto entryValue = getAddressValue(Entry.value);
340-
// No-escaping stored declarations are captured as the
341-
// address of the value.
342-
assert(entryValue->getType().isAddress() && "no address for captured var!");
343-
capturedArgs.push_back(ManagedValue::forLValue(entryValue));
344-
}
345-
else {
346-
auto entryValue = getAddressValue(Entry.value);
347-
auto addr = emitTemporaryAllocation(vd, entryValue->getType());
348-
auto val = B.createCopyAddr(loc, entryValue, addr, IsNotTake,
349-
IsInitialization);
350-
capturedArgs.push_back(ManagedValue::forLValue(addr));
351-
}
352-
break;
353-
}
336+
354337
case CaptureKind::StorageAddress: {
355338
auto entryValue = getAddressValue(Entry.value);
356339
// No-escaping stored declarations are captured as the

lib/SILGen/SILGenProlog.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ static void emitCaptureArguments(SILGenFunction &SGF,
420420
SGF.B.createDebugValueAddr(Loc, addr, DbgVar);
421421
break;
422422
}
423-
case CaptureKind::Immutable:
424423
case CaptureKind::StorageAddress: {
425424
// Non-escaping stored decls are captured as the address of the value.
426425
auto type = getVarTypeInCaptureContext();

lib/SILOptimizer/Mandatory/ClosureLifetimeFixup.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,6 @@ static bool tryRewriteToPartialApplyStack(
459459
saveDeleteInst(convertOrPartialApply);
460460
saveDeleteInst(origPA);
461461

462-
ApplySite site(newPA);
463-
SILFunctionConventions calleeConv(site.getSubstCalleeType(),
464-
newPA->getModule());
465-
466-
// Since we create temporary allocation for in_guaranteed captures during SILGen,
467-
// the dealloc_stack of it can occur before the apply due to conversion scopes.
468-
// When we insert destroy_addr of the in_guaranteed capture after the apply,
469-
// we may end up with a situation when the dealloc_stack occurs before the destroy_addr.
470-
// The code below proactively removes the dealloc_stack of in_guaranteed capture,
471-
// so that it can be reinserted at the correct place after the destroy_addr below.
472-
for (auto &arg : newPA->getArgumentOperands()) {
473-
unsigned calleeArgumentIndex = site.getCalleeArgIndex(arg);
474-
assert(calleeArgumentIndex >= calleeConv.getSILArgIndexOfFirstParam());
475-
auto paramInfo = calleeConv.getParamInfoForSILArg(calleeArgumentIndex);
476-
if (paramInfo.getConvention() == ParameterConvention::Indirect_In_Guaranteed)
477-
// go over all the dealloc_stack, remove it
478-
for (auto *use : arg.get()->getUses())
479-
if (auto *deallocInst = dyn_cast<DeallocStackInst>(use->getUser()))
480-
deallocInst->eraseFromParent();
481-
}
482-
483462
// Insert destroys of arguments after the apply and the dealloc_stack.
484463
if (auto *apply = dyn_cast<ApplyInst>(singleApplyUser)) {
485464
auto insertPt = std::next(SILBasicBlock::iterator(apply));
@@ -489,15 +468,11 @@ static bool tryRewriteToPartialApplyStack(
489468
SILBuilderWithScope b3(insertPt);
490469
b3.createDeallocStack(loc, newPA);
491470
insertDestroyOfCapturedArguments(newPA, b3);
492-
// dealloc_stack of the in_guaranteed capture is inserted
493-
insertDeallocOfCapturedArguments(newPA, b3);
494471
} else if (auto *tai = dyn_cast<TryApplyInst>(singleApplyUser)) {
495472
for (auto *succBB : tai->getSuccessorBlocks()) {
496473
SILBuilderWithScope b3(succBB->begin());
497474
b3.createDeallocStack(loc, newPA);
498475
insertDestroyOfCapturedArguments(newPA, b3);
499-
// dealloc_stack of the in_guaranteed capture is inserted
500-
insertDeallocOfCapturedArguments(newPA, b3);
501476
}
502477
} else {
503478
llvm_unreachable("Unknown FullApplySite instruction kind");

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,11 @@ bool TempRValueOptPass::collectLoads(
147147
LLVM_DEBUG(llvm::dbgs()
148148
<< " Temp use may write/destroy its source" << *user);
149149
return false;
150+
150151
case SILInstructionKind::BeginAccessInst:
151152
return cast<BeginAccessInst>(user)->getAccessKind() == SILAccessKind::Read;
152153

153-
case SILInstructionKind::MarkDependenceInst: {
154-
auto mdi = cast<MarkDependenceInst>(user);
155-
if (mdi->getBase() == address) {
156-
return true;
157-
}
158-
for (auto *mdiUseOper : mdi->getUses()) {
159-
if (!collectLoads(mdiUseOper, mdiUseOper->getUser(), mdi, srcAddr,
160-
loadInsts))
161-
return false;
162-
}
163-
return true;
164-
}
165154
case SILInstructionKind::ApplyInst:
166-
case SILInstructionKind::PartialApplyInst:
167155
case SILInstructionKind::TryApplyInst: {
168156
ApplySite apply(user);
169157

@@ -662,14 +650,7 @@ TempRValueOptPass::tryOptimizeStoreIntoTemp(StoreInst *si) {
662650
toDelete.push_back(fli);
663651
break;
664652
}
665-
case SILInstructionKind::MarkDependenceInst: {
666-
SILBuilderWithScope builder(user);
667-
auto mdi = cast<MarkDependenceInst>(user);
668-
auto newInst = builder.createMarkDependence(user->getLoc(), mdi->getValue(), si->getSrc());
669-
mdi->replaceAllUsesWith(newInst);
670-
toDelete.push_back(user);
671-
break;
672-
}
653+
673654
// ASSUMPTION: no operations that may be handled by this default clause can
674655
// destroy tempObj. This includes operations that load the value from memory
675656
// and release it.

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,15 +1372,6 @@ void swift::releasePartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
13721372
callbacks.createdNewInst(u.get<ReleaseValueInst *>());
13731373
}
13741374

1375-
void swift::deallocPartialApplyCapturedArg(SILBuilder &builder, SILLocation loc,
1376-
SILValue arg,
1377-
SILParameterInfo paramInfo) {
1378-
if (!paramInfo.isIndirectInGuaranteed())
1379-
return;
1380-
1381-
builder.createDeallocStack(loc, arg);
1382-
}
1383-
13841375
static bool
13851376
deadMarkDependenceUser(SILInstruction *inst,
13861377
SmallVectorImpl<SILInstruction *> &deleteInsts) {
@@ -1946,22 +1937,6 @@ void swift::insertDestroyOfCapturedArguments(
19461937
}
19471938
}
19481939

1949-
void swift::insertDeallocOfCapturedArguments(
1950-
PartialApplyInst *pai, SILBuilder &builder) {
1951-
assert(pai->isOnStack());
1952-
1953-
ApplySite site(pai);
1954-
SILFunctionConventions calleeConv(site.getSubstCalleeType(),
1955-
pai->getModule());
1956-
auto loc = RegularLocation::getAutoGeneratedLocation();
1957-
for (auto &arg : pai->getArgumentOperands()) {
1958-
unsigned calleeArgumentIndex = site.getCalleeArgIndex(arg);
1959-
assert(calleeArgumentIndex >= calleeConv.getSILArgIndexOfFirstParam());
1960-
auto paramInfo = calleeConv.getParamInfoForSILArg(calleeArgumentIndex);
1961-
deallocPartialApplyCapturedArg(builder, loc, arg.get(), paramInfo);
1962-
}
1963-
}
1964-
19651940
AbstractFunctionDecl *swift::getBaseMethod(AbstractFunctionDecl *FD) {
19661941
while (FD->getOverriddenDecl()) {
19671942
FD = FD->getOverriddenDecl();

0 commit comments

Comments
 (0)