Skip to content

GenericSpecializer: Allow simple function specialization cycles. #17885

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 2 commits into from
Jul 12, 2018
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
9 changes: 8 additions & 1 deletion lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {
auto *Callee = Apply.getCalleeFunction();
SILFunction *Caller = nullptr;
Caller = Apply.getFunction();
int numAcceptedCycles = 1;

// Name of the function to be specialized.
auto GenericFunc = Callee;
Expand Down Expand Up @@ -327,7 +328,13 @@ static bool createsInfiniteSpecializationLoop(ApplySite Apply) {
if (growingSubstitutions(CurSpecializationInfo->getSubstitutions(),
Apply.getSubstitutionMap())) {
DEBUG(llvm::dbgs() << "Found a generic specialization loop!\n");
return true;

// Accept a cycles up to a limit. This is necessary to generate
// efficient code for some library functions, like compactMap, which
// contain small specialization cycles.
if (numAcceptedCycles == 0)
return true;
numAcceptedCycles--;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@

// Check that the compiler has produced a specialization information for a call-site that
// was inlined from a specialized generic function.
// CHECK-LABEL: // Generic specialization information for call-site $S044generic_specialization_loops_detection_with_C04foo4yyx_q_tr0_lF <Array<Int>, Array<Double>>
// CHECK-LABEL: // Generic specialization information for call-site $S044generic_specialization_loops_detection_with_C04foo4yyx_q_tr0_lFSaySays5UInt8VGG_SaySaySiGGTg5:
// CHECK-NEXT: // Caller: $S044generic_specialization_loops_detection_with_C04foo4yyx_q_tr0_lFSi_SdTg5
// CHECK-NEXT: // Parent: $S044generic_specialization_loops_detection_with_C04bar4yyx_q_tr0_lF
// CHECK-NEXT: // Substitutions: <Int, Double>
// CHECK-NEXT: // Substitutions: <Array<UInt8>, Array<Int>>
// CHECK-NEXT: //
// CHECK-NEXT: // Caller: $S044generic_specialization_loops_detection_with_C011testFooBar4yyF
// CHECK-NEXT: // Parent: $S044generic_specialization_loops_detection_with_C04foo4yyx_q_tr0_lF
// CHECK-NEXT: // Substitutions: <Int, Double>
// CHECK-NEXT: //
// CHECK-NEXT: apply %{{[0-9]+}}<Array<Int>, Array<Double>>
// CHECK-NEXT: apply %{{.*}}Array<Array<UInt8>>

// Check specializations of mutually recursive functions which
// may result in an infinite specialization loop.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ func flood2<T>(_ x: T) {
@inline(never)
public func run_TypeFlood(_ N: Int) {
for _ in 1...N {
flood3(Some1<Some1<Some1<Int>>>())
flood3(Some1<Some1<Some0<Int>>>())
flood3(Some1<Some0<Some1<Int>>>())
flood3(Some1<Some0<Some0<Int>>>())
flood3(Some0<Some1<Some1<Int>>>())
flood3(Some0<Some1<Some0<Int>>>())
flood3(Some0<Some0<Some1<Int>>>())
flood3(Some0<Some0<Some0<Int>>>())
flood2(Some1<Some1<Some1<Int>>>())
flood2(Some1<Some1<Some0<Int>>>())
flood2(Some1<Some0<Some1<Int>>>())
flood2(Some1<Some0<Some0<Int>>>())
flood2(Some0<Some1<Some1<Int>>>())
flood2(Some0<Some1<Some0<Int>>>())
flood2(Some0<Some0<Some1<Int>>>())
flood2(Some0<Some0<Some0<Int>>>())
}
}