Skip to content

[CSOptimizer] Few tweaks to make unapplied disjunction and literal array arguments faster #78357

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 3 commits into from
Jan 3, 2025
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
45 changes: 43 additions & 2 deletions lib/Sema/CSOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//

#include "TypeChecker.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericSignature.h"
#include "swift/Basic/OptionSet.h"
Expand Down Expand Up @@ -61,6 +62,12 @@ static bool isFloatType(Type type) {
return type->isFloat() || type->isDouble() || type->isFloat80();
}

static bool isUnboundArrayType(Type type) {
if (auto *UGT = type->getAs<UnboundGenericType>())
return UGT->getDecl() == type->getASTContext().getArrayDecl();
return false;
}

static bool isSupportedOperator(Constraint *disjunction) {
if (!isOperatorDisjunction(disjunction))
return false;
Expand Down Expand Up @@ -298,9 +305,19 @@ static void determineBestChoicesInContext(
case ExprKind::Binary:
case ExprKind::PrefixUnary:
case ExprKind::PostfixUnary:
case ExprKind::UnresolvedDot:
recordResult(disjunction, {/*score=*/1.0});
case ExprKind::UnresolvedDot: {
llvm::SmallVector<Constraint *, 2> favoredChoices;
// Favor choices that don't require application.
llvm::copy_if(
disjunction->getNestedConstraints(),
std::back_inserter(favoredChoices), [](Constraint *choice) {
auto *decl = getOverloadChoiceDecl(choice);
return decl &&
!decl->getInterfaceType()->is<AnyFunctionType>();
});
recordResult(disjunction, {/*score=*/1.0, favoredChoices});
continue;
}

default:
break;
Expand Down Expand Up @@ -538,6 +555,30 @@ static void determineBestChoicesInContext(
}
}

// Match `[...]` to Array<...> and/or `ExpressibleByArrayLiteral`
// conforming types.
if (options.contains(MatchFlag::OnParam) &&
options.contains(MatchFlag::Literal) &&
isUnboundArrayType(candidateType)) {
// If an exact match is requested favor only `[...]` to `Array<...>`
// since everything else is going to increase to score.
if (options.contains(MatchFlag::ExactOnly))
return paramType->isArrayType() ? 1 : 0;

// Otherwise, check if the other side conforms to
// `ExpressibleByArrayLiteral` protocol (in some way).
// We want an overly optimistic result here to avoid
// under-favoring.
auto &ctx = cs.getASTContext();
return checkConformanceWithoutContext(
paramType,
ctx.getProtocol(
KnownProtocolKind::ExpressibleByArrayLiteral),
/*allowMissing=*/true)
? 0.3
: 0;
}

if (options.contains(MatchFlag::ExactOnly))
return areEqual(candidateType, paramType) ? 1 : 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-typecheck-verify-swift -solver-scope-threshold=11000
// REQUIRES: tools-release,no_asan
// REQUIRES: OS=macosx

func f(n: Int, a: [Int]) {
let _ = [(0 ..< n + a.count).map { Int8($0) }] +
[(0 ..< n + a.count).map { Int8($0) }.reversed()] // Ok
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %scale-test --begin 1 --end 15 --step 1 --select NumLeafScopes %s --expected-exit-code 0
// REQUIRES: asserts,no_asan

enum E {
case a
case b
case c(Int32)
}

struct Tester {
mutating func test(arr: [E], cond: Bool = false) {}
mutating func test(arr: E..., cond: Bool = false) {}
}

func test() {
var tester = Tester()
tester.test(arr: [
.c(1), .a,
%for i in range(N):
.c(1 << 4 | 8), .c(0),
%end
.c(1), .b])
}