Skip to content

Commit e0b4ced

Browse files
committed
Revert "Merge pull request #25038 from atrick/fix-specializer-leak"
This reverts commit d402077, reversing changes made to 651045f.
1 parent 00e1a88 commit e0b4ced

14 files changed

+308
-617
lines changed

include/swift/AST/SILOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class SILOptions {
5050
/// Remove all runtime assertions during optimizations.
5151
bool RemoveRuntimeAsserts = false;
5252

53+
/// Enable existential specializer optimization.
54+
bool ExistentialSpecializer = false;
55+
5356
/// Controls whether the SIL ARC optimizations are run.
5457
bool EnableARCOptimizations = true;
5558

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ def sil_unroll_threshold : Separate<["-"], "sil-unroll-threshold">,
472472
MetaVarName<"<250>">,
473473
HelpText<"Controls the aggressiveness of loop unrolling">;
474474

475+
def sil_existential_specializer : Flag<["-"], "sil-existential-specializer">,
476+
HelpText<"Enable SIL existential specializer optimization">;
477+
475478
def sil_merge_partial_modules : Flag<["-"], "sil-merge-partial-modules">,
476479
HelpText<"Merge SIL from all partial swiftmodules into the final module">;
477480

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,9 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
703703
return true;
704704
}
705705
}
706+
if (Args.hasArg(OPT_sil_existential_specializer)) {
707+
Opts.ExistentialSpecializer = true;
708+
}
706709
if (const Arg *A = Args.getLastArg(OPT_num_threads)) {
707710
if (StringRef(A->getValue()).getAsInteger(10, Opts.NumThreads)) {
708711
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,

lib/SILOptimizer/FunctionSignatureTransforms/ExistentialSpecializer.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828

2929
using namespace swift;
3030

31-
static llvm::cl::opt<bool>
32-
EnableExistentialSpecializer("enable-existential-specializer",
33-
llvm::cl::Hidden,
34-
llvm::cl::init(true));
35-
3631
STATISTIC(NumFunctionsWithExistentialArgsSpecialized,
3732
"Number of functions with existential args specialized");
3833

@@ -71,7 +66,7 @@ class ExistentialSpecializer : public SILFunctionTransform {
7166
auto *F = getFunction();
7267

7368
/// Don't optimize functions that should not be optimized.
74-
if (!F->shouldOptimize() || !EnableExistentialSpecializer) {
69+
if (!F->shouldOptimize() || !F->getModule().getOptions().ExistentialSpecializer) {
7570
return;
7671
}
7772

@@ -115,6 +110,19 @@ bool ExistentialSpecializer::findConcreteTypeFromSoleConformingType(
115110
return true;
116111
}
117112

113+
/// Check if the argument Arg is used in a destroy_use instruction.
114+
static void
115+
findIfCalleeUsesArgInDestroyUse(SILValue Arg,
116+
ExistentialTransformArgumentDescriptor &ETAD) {
117+
for (Operand *ArgUse : Arg->getUses()) {
118+
auto *ArgUser = ArgUse->getUser();
119+
if (isa<DestroyAddrInst>(ArgUser)) {
120+
ETAD.DestroyAddrUse = true;
121+
break;
122+
}
123+
}
124+
}
125+
118126
/// Helper function to ensure that the argument is not InOut or InOut_Aliasable
119127
static bool isNonInoutIndirectArgument(SILValue Arg,
120128
SILArgumentConvention ArgConvention) {
@@ -180,27 +188,24 @@ bool ExistentialSpecializer::canSpecializeExistentialArgsInFunction(
180188
continue;
181189
}
182190

183-
/// Determine attributes of the existential addr argument.
191+
/// Determine attributes of the existential addr arguments such as
192+
/// destroy_use, immutable_access.
184193
ExistentialTransformArgumentDescriptor ETAD;
185194
auto paramInfo = origCalleeConv.getParamInfoForSILArg(Idx);
186-
// The ExistentialSpecializerCloner copies the incoming generic argument
187-
// into an existential. This won't work if the original argument is
188-
// mutated. Furthermore, SILCombine would not be able to replace a mutated
189-
// existential with a concrete value, so the specialization thunk could not
190-
// be optimized away.
191-
if (paramInfo.isIndirectMutating())
192-
continue;
193-
194-
ETAD.AccessType = paramInfo.isConsumed()
195+
ETAD.AccessType = (paramInfo.isIndirectMutating() || paramInfo.isConsumed())
195196
? OpenedExistentialAccess::Mutable
196197
: OpenedExistentialAccess::Immutable;
197-
ETAD.isConsumed = paramInfo.isConsumed();
198+
ETAD.DestroyAddrUse = false;
199+
if ((CalleeArgs[Idx]->getType().getPreferredExistentialRepresentation(
200+
F->getModule()))
201+
!= ExistentialRepresentation::Class)
202+
findIfCalleeUsesArgInDestroyUse(CalleeArg, ETAD);
198203

199204
/// Save the attributes
200205
ExistentialArgDescriptor[Idx] = ETAD;
201206
LLVM_DEBUG(llvm::dbgs()
202207
<< "ExistentialSpecializer Pass:Function: " << F->getName()
203-
<< " Arg:" << Idx << " has a concrete type.\n");
208+
<< " Arg:" << Idx << "has a concrete type.\n");
204209
returnFlag |= true;
205210
}
206211
return returnFlag;

0 commit comments

Comments
 (0)