Skip to content

[Constraint solver] Shore up "common return type" optimization. #9494

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
May 11, 2017
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
22 changes: 0 additions & 22 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5035,12 +5035,6 @@ class FuncDecl final : public AbstractFunctionDecl,

TypeLoc FnRetType;

/// If this declaration is part of an overload set, determine if we've
/// searched for a common overload amongst all overloads, or if we've found
/// one.
unsigned HaveSearchedForCommonOverloadReturnType : 1;
unsigned HaveFoundCommonOverloadReturnType : 1;

/// Whether we are statically dispatched even if overridable
unsigned ForcedStaticDispatch : 1;

Expand Down Expand Up @@ -5081,8 +5075,6 @@ class FuncDecl final : public AbstractFunctionDecl,
Mutating = false;
HasDynamicSelf = false;
ForcedStaticDispatch = false;
HaveSearchedForCommonOverloadReturnType = false;
HaveFoundCommonOverloadReturnType = false;
}

static FuncDecl *createImpl(ASTContext &Context, SourceLoc StaticLoc,
Expand Down Expand Up @@ -5155,20 +5147,6 @@ class FuncDecl final : public AbstractFunctionDecl,
return getParameterLists()[i];
}


bool getHaveSearchedForCommonOverloadReturnType() {
return HaveSearchedForCommonOverloadReturnType;
}
void setHaveSearchedForCommonOverloadReturnType(bool b = true) {
HaveSearchedForCommonOverloadReturnType = b;
}
bool getHaveFoundCommonOverloadReturnType() {
return HaveFoundCommonOverloadReturnType;
}
void setHaveFoundCommonOverloadReturnType(bool b = true) {
HaveFoundCommonOverloadReturnType = b;
}

/// \returns true if this is non-mutating due to applying a 'mutating'
/// attribute. For example a "mutating set" accessor.
bool isExplicitNonMutating() const;
Expand Down
100 changes: 41 additions & 59 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2335,68 +2335,50 @@ namespace {
if (auto fnType = CS.getType(fnExpr)->getAs<AnyFunctionType>()) {
outputTy = fnType->getResult();
}
} else if (auto OSR = dyn_cast<OverloadSetRefExpr>(fnExpr)) {
if (auto FD = dyn_cast<FuncDecl>(OSR->getDecls()[0])) {
} else if (auto OSR = dyn_cast<OverloadedDeclRefExpr>(fnExpr)) {
// Determine if the overloads are all functions that share a common
// return type.
Type commonType;
for (auto OD : OSR->getDecls()) {
auto OFD = dyn_cast<AbstractFunctionDecl>(OD);
if (!OFD) {
commonType = Type();
break;
}

// If we've already agreed upon an overloaded return type, use it.
if (FD->getHaveSearchedForCommonOverloadReturnType()) {

if (FD->getHaveFoundCommonOverloadReturnType()) {
outputTy = FD->getInterfaceType()->getAs<AnyFunctionType>()
->getResult();
outputTy = FD->mapTypeIntoContext(outputTy);
}

} else {

// Determine if the overloads all share a common return type.
Type commonType;
Type resultType;

for (auto OD : OSR->getDecls()) {

if (auto OFD = dyn_cast<FuncDecl>(OD)) {
auto OFT = OFD->getInterfaceType()->getAs<AnyFunctionType>();

if (!OFT) {
commonType = Type();
break;
}

resultType = OFT->getResult();
resultType = OFD->mapTypeIntoContext(resultType);

if (commonType.isNull()) {
commonType = resultType;
} else if (!commonType->isEqual(resultType)) {
commonType = Type();
break;
}
} else {
// TODO: unreachable?
commonType = Type();
break;
}
}

// TODO: For now, disallow tyvar, archetype and function types.
if (!(commonType.isNull() ||
commonType->getAs<TypeVariableType>() ||
commonType->getAs<ArchetypeType>() ||
commonType->getAs<AnyFunctionType>())) {
outputTy = commonType;
}

// Set the search bits appropriately.
for (auto OD : OSR->getDecls()) {
if (auto OFD = dyn_cast<FuncDecl>(OD)) {
OFD->setHaveSearchedForCommonOverloadReturnType();

if (!outputTy.isNull())
OFD->setHaveFoundCommonOverloadReturnType();
}
auto OFT = OFD->getInterfaceType()->getAs<AnyFunctionType>();
if (!OFT) {
commonType = Type();
break;
}

// Look past the self parameter.
if (OFD->getDeclContext()->isTypeContext()) {
OFT = OFT->getResult()->getAs<AnyFunctionType>();
if (!OFT) {
commonType = Type();
break;
}
}

Type resultType = OFT->getResult();

// If there are any type parameters in the result,
if (resultType->hasTypeParameter()) {
commonType = Type();
break;
}

if (commonType.isNull()) {
commonType = resultType;
} else if (!commonType->isEqual(resultType)) {
commonType = Type();
break;
}
}

if (commonType) {
outputTy = commonType;
}
}

Expand Down
26 changes: 25 additions & 1 deletion lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//
#include "ConstraintSystem.h"
#include "ConstraintGraph.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeWalker.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Compiler.h"
Expand Down Expand Up @@ -2480,6 +2481,28 @@ static bool isGenericOperatorOrUnavailable(Constraint *constraint) {
decl->getAttrs().isUnavailable(ctx);
}

/// Whether this constraint refers to a symmetric operator.
static bool isSymmetricOperator(Constraint *constraint) {
if (constraint->getKind() != ConstraintKind::BindOverload ||
constraint->getOverloadChoice().getKind() != OverloadChoiceKind::Decl ||
!constraint->getOverloadChoice().getDecl()->isOperator())
return false;

// If it's a binary operator, check that the types on both sides are the
// same. Otherwise, don't perform this optimization.
auto func = dyn_cast<FuncDecl>(constraint->getOverloadChoice().getDecl());
auto paramList =
func->getParameterList(func->getDeclContext()->isTypeContext());
if (paramList->size() != 2)
return true;

auto firstType =
paramList->get(0)->getInterfaceType()->getLValueOrInOutObjectType();
auto secondType =
paramList->get(1)->getInterfaceType()->getLValueOrInOutObjectType();
return firstType->isEqual(secondType);
}

bool ConstraintSystem::solveSimplified(
SmallVectorImpl<Solution> &solutions,
FreeTypeVariableBinding allowFreeTypeVariables) {
Expand Down Expand Up @@ -2694,7 +2717,8 @@ bool ConstraintSystem::solveSimplified(

if (!solveRec(solutions, allowFreeTypeVariables)) {
if (!firstNonGenericOperatorSolution &&
!isGenericOperatorOrUnavailable(constraint))
!isGenericOperatorOrUnavailable(constraint) &&
isSymmetricOperator(constraint))
firstNonGenericOperatorSolution = constraint;

firstSolvedConstraint = constraint;
Expand Down
14 changes: 14 additions & 0 deletions lib/Sema/TypeCheckError.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,20 @@ class ApplyClassifier {
// count, then this is a call to the opaque value returned from
// the function.
if (args.size() != fnRef.getNumArgumentsForFullApply()) {
// Special case: a reference to an operator within a type might be
// missing 'self'.
// FIXME: The issue here is that this is an ill-formed expression, but
// we don't know it from the structure of the expression.
if (args.size() == 1 && fnRef.getKind() == AbstractFunction::Function &&
isa<FuncDecl>(fnRef.getFunction()) &&
cast<FuncDecl>(fnRef.getFunction())->isOperator() &&
fnRef.getNumArgumentsForFullApply() == 2 &&
fnRef.getFunction()->getDeclContext()->isTypeContext()) {
// Can only happen with invalid code.
assert(fnRef.getFunction()->getASTContext().Diags.hadAnyError());
return Classification::forInvalidCode();
}

assert(args.size() > fnRef.getNumArgumentsForFullApply() &&
"partial application was throwing?");
return Classification::forThrow(PotentialReason::forThrowingApply());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
// REQUIRES: asserts
func a{guard let[]=(a||()A