Skip to content

Commit 6285cc5

Browse files
authored
Merge pull request #9529 from DougGregor/gsb-perf-cleanups
2 parents d2a800b + bab127f commit 6285cc5

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@
3333
#include "llvm/ADT/SetVector.h"
3434
#include "llvm/ADT/SmallPtrSet.h"
3535
#include "llvm/ADT/SmallString.h"
36+
#include "llvm/ADT/Statistic.h"
3637
#include "llvm/ADT/STLExtras.h"
3738
#include "llvm/Support/raw_ostream.h"
3839
#include <algorithm>
3940

4041
using namespace swift;
4142
using llvm::DenseMap;
4243

44+
/// Define this to 1 to enable expensive assertions.
45+
#define SWIFT_GSB_EXPENSIVE_ASSERTIONS 0
46+
4347
namespace {
4448
typedef GenericSignatureBuilder::RequirementSource RequirementSource;
4549
typedef GenericSignatureBuilder::FloatingRequirementSource
@@ -54,6 +58,18 @@ namespace {
5458

5559
} // end anonymous namespace
5660

61+
#define DEBUG_TYPE "Generic signature builder"
62+
STATISTIC(NumPotentialArchetypes, "# of potential archetypes");
63+
STATISTIC(NumConformances, "# of conformances tracked");
64+
STATISTIC(NumConformanceConstraints, "# of conformance constraints tracked");
65+
STATISTIC(NumSameTypeConstraints, "# of same-type constraints tracked");
66+
STATISTIC(NumConcreteTypeConstraints,
67+
"# of same-type-to-concrete constraints tracked");
68+
STATISTIC(NumSuperclassConstraints, "# of superclass constraints tracked");
69+
STATISTIC(NumLayoutConstraints, "# of layout constraints tracked");
70+
STATISTIC(NumSelfDerived, "# of self-derived constraints removed");
71+
STATISTIC(NumRecursive, "# of recursive types we bail out on");
72+
5773
struct GenericSignatureBuilder::Implementation {
5874
/// Function used to look up conformances.
5975
std::function<GenericFunction> LookupConformance;
@@ -988,10 +1004,12 @@ bool FloatingRequirementSource::isRecursive(
9881004
->getAffectedPotentialArchetype();
9891005
while (auto parent = pa->getParent()) {
9901006
if (pa->getNestedName() == nestedName) {
991-
if (++grossCount > 4) return true;
1007+
if (++grossCount > 4) {
1008+
++NumRecursive;
1009+
return true;
1010+
}
9921011
}
9931012

994-
9951013
pa = parent;
9961014
}
9971015
}
@@ -1000,6 +1018,8 @@ bool FloatingRequirementSource::isRecursive(
10001018
}
10011019

10021020
GenericSignatureBuilder::PotentialArchetype::~PotentialArchetype() {
1021+
++NumPotentialArchetypes;
1022+
10031023
for (const auto &nested : NestedTypes) {
10041024
for (auto pa : nested.second) {
10051025
if (pa != this)
@@ -1218,6 +1238,7 @@ const RequirementSource *GenericSignatureBuilder::resolveSuperConformance(
12181238
superclassSource =
12191239
superclassSource->viaSuperclass(*this, conformance->getConcrete());
12201240
paEquivClass->conformsTo[proto].push_back({pa, proto, superclassSource});
1241+
++NumConformanceConstraints;
12211242
return superclassSource;
12221243
}
12231244

@@ -1295,11 +1316,14 @@ bool PotentialArchetype::addConformance(ProtocolDecl *proto,
12951316
if (known != equivClass->conformsTo.end()) {
12961317
// We already knew about this conformance; record this specific constraint.
12971318
known->second.push_back({this, proto, source});
1319+
++NumConformanceConstraints;
12981320
return false;
12991321
}
13001322

13011323
// Add the conformance along with this constraint.
13021324
equivClass->conformsTo[proto].push_back({this, proto, source});
1325+
++NumConformanceConstraints;
1326+
++NumConformances;
13031327

13041328
// Determine whether there is a superclass constraint where the
13051329
// superclass conforms to this protocol.
@@ -1505,7 +1529,7 @@ PotentialArchetype *PotentialArchetype::getArchetypeAnchor(
15051529
anchor = pa;
15061530
}
15071531

1508-
#ifndef NDEBUG
1532+
#if SWIFT_GSB_EXPENSIVE_ASSERTIONS
15091533
// Make sure that we did, in fact, get one that is better than all others.
15101534
for (auto pa : anchor->getEquivalenceClassMembers()) {
15111535
assert((pa == anchor || compareDependentTypes(&anchor, &pa) < 0) &&
@@ -2716,6 +2740,7 @@ ConstraintResult GenericSignatureBuilder::addLayoutRequirementDirect(
27162740

27172741
// Record this layout constraint.
27182742
equivClass->layoutConstraints.push_back({PAT, Layout, Source});
2743+
++NumLayoutConstraints;
27192744

27202745
// Update the layout in the equivalence class, if we didn't have one already.
27212746
if (!equivClass->layout)
@@ -2840,6 +2865,7 @@ ConstraintResult GenericSignatureBuilder::addSuperclassRequirementDirect(
28402865
// Record the constraint.
28412866
T->getOrCreateEquivalenceClass()->superclassConstraints
28422867
.push_back(ConcreteConstraint{T, superclass, source});
2868+
++NumSuperclassConstraints;
28432869

28442870
// Update the equivalence class with the constraint.
28452871
updateSuperclass(T, superclass, source);
@@ -2976,11 +3002,13 @@ void GenericSignatureBuilder::PotentialArchetype::addSameTypeConstraint(
29763002
// Update the same-type constraints of this PA to reference the other PA.
29773003
getOrCreateEquivalenceClass()->sameTypeConstraints[this]
29783004
.push_back({this, otherPA, source});
3005+
++NumSameTypeConstraints;
29793006

29803007
if (this != otherPA) {
29813008
// Update the same-type constraints of the other PA to reference this PA.
29823009
otherPA->getOrCreateEquivalenceClass()->sameTypeConstraints[otherPA]
29833010
.push_back({otherPA, this, source});
3011+
++NumSameTypeConstraints;
29843012
}
29853013
}
29863014

@@ -3108,6 +3136,7 @@ ConstraintResult GenericSignatureBuilder::addSameTypeRequirementToConcrete(
31083136
// Record the concrete type and its source.
31093137
equivClass->concreteTypeConstraints.push_back(
31103138
ConcreteConstraint{T, Concrete, Source});
3139+
++NumConcreteTypeConstraints;
31113140

31123141
// If we've already been bound to a type, match that type.
31133142
if (equivClass->concreteType) {
@@ -3151,6 +3180,7 @@ ConstraintResult GenericSignatureBuilder::addSameTypeRequirementToConcrete(
31513180
? conformance->getConcrete()
31523181
: nullptr);
31533182
equivClass->conformsTo[protocol].push_back({T, protocol, concreteSource});
3183+
++NumConformanceConstraints;
31543184
}
31553185

31563186
// Eagerly resolve any existing nested types to their concrete forms (others
@@ -4107,8 +4137,10 @@ namespace {
41074137
[&](const Constraint<T> &constraint) {
41084138
bool derivedViaConcrete;
41094139
if (constraint.source->isSelfDerivedSource(
4110-
constraint.archetype, derivedViaConcrete))
4140+
constraint.archetype, derivedViaConcrete)) {
4141+
++NumSelfDerived;
41114142
return true;
4143+
}
41124144

41134145
if (!derivedViaConcrete)
41144146
return false;
@@ -4122,6 +4154,7 @@ namespace {
41224154
if (!remainingConcrete)
41234155
remainingConcrete = constraint;
41244156

4157+
++NumSelfDerived;
41254158
return true;
41264159
}),
41274160
constraints.end());
@@ -4274,15 +4307,19 @@ void GenericSignatureBuilder::checkConformanceConstraints(
42744307
bool derivedViaConcrete;
42754308
if (constraint.source->isSelfDerivedConformance(
42764309
constraint.archetype, entry.first,
4277-
derivedViaConcrete))
4310+
derivedViaConcrete)) {
4311+
++NumSelfDerived;
42784312
return true;
4313+
}
42794314

42804315
if (!derivedViaConcrete)
42814316
return false;
42824317

42834318
// Drop derived-via-concrete requirements.
42844319
if (!remainingConcrete)
42854320
remainingConcrete = constraint;
4321+
4322+
++NumSelfDerived;
42864323
return true;
42874324
}),
42884325
entry.second.end());

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,8 @@ static SILLocation getLocForValue(SILValue value) {
5454

5555
static GenericEnvironment *getGenericEnvironment(SILModule &Mod,
5656
CanSILFunctionType loweredTy) {
57-
auto *SM = Mod.getSwiftModule();
58-
auto &Ctx = loweredTy->getASTContext();
59-
auto *GSB = Ctx.getOrCreateGenericSignatureBuilder(
60-
loweredTy->getGenericSignature(), SM);
61-
auto *GenericEnv = Ctx.getOrCreateCanonicalGenericEnvironment(GSB, *SM);
62-
return GenericEnv;
57+
return loweredTy->getGenericSignature()->createGenericEnvironment(
58+
*Mod.getSwiftModule());
6359
}
6460

6561
/// Utility to determine if this is a large loadable type

0 commit comments

Comments
 (0)