Skip to content

Commit 46063d3

Browse files
authored
Merge pull request #39476 from slavapestov/rqm-avoid-hashtable-lookup
AST: GenericSignatures point directly to their RequirementMachine
2 parents b654bf0 + 47ab4a9 commit 46063d3

File tree

7 files changed

+52
-52
lines changed

7 files changed

+52
-52
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
265265

266266
GenericEnvironment *GenericEnv = nullptr;
267267

268+
rewriting::RequirementMachine *Machine = nullptr;
269+
268270
// Make vanilla new/delete illegal.
269271
void *operator new(size_t Bytes) = delete;
270272
void operator delete(void *Data) = delete;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
#include <algorithm>
6868
#include <memory>
6969

70-
#include "RequirementMachine/RequirementMachine.h"
7170
#include "RequirementMachine/RewriteContext.h"
7271

7372
using namespace swift;
@@ -538,14 +537,6 @@ struct ASTContext::Implementation {
538537

539538
/// Memory allocation arena for the term rewriting system.
540539
std::unique_ptr<rewriting::RewriteContext> TheRewriteContext;
541-
542-
/// Stored requirement machines for canonical generic signatures.
543-
///
544-
/// This should come after TheRewriteContext above, since various destructors
545-
/// compile stats in the histograms stored in our RewriteContext.
546-
llvm::DenseMap<GenericSignature,
547-
std::unique_ptr<rewriting::RequirementMachine>>
548-
RequirementMachines;
549540
};
550541

551542
ASTContext::Implementation::Implementation()
@@ -1965,32 +1956,7 @@ ASTContext::getOrCreateRequirementMachine(CanGenericSignature sig) {
19651956
if (!rewriteCtx)
19661957
rewriteCtx.reset(new rewriting::RewriteContext(*this));
19671958

1968-
// Check whether we already have a requirement machine for this
1969-
// signature.
1970-
auto &machines = getImpl().RequirementMachines;
1971-
1972-
auto &machinePtr = machines[sig];
1973-
if (machinePtr) {
1974-
auto *machine = machinePtr.get();
1975-
if (!machine->isComplete()) {
1976-
llvm::errs() << "Re-entrant construction of requirement "
1977-
<< "machine for " << sig << "\n";
1978-
abort();
1979-
}
1980-
1981-
return machine;
1982-
}
1983-
1984-
auto *machine = new rewriting::RequirementMachine(*rewriteCtx);
1985-
1986-
// Store this requirement machine before adding the signature,
1987-
// to catch re-entrant construction via addGenericSignature()
1988-
// below.
1989-
machinePtr.reset(machine);
1990-
1991-
machine->addGenericSignature(sig);
1992-
1993-
return machine;
1959+
return rewriteCtx->getRequirementMachine(sig);
19941960
}
19951961

19961962
bool ASTContext::isRecursivelyConstructingRequirementMachine(
@@ -1999,13 +1965,7 @@ bool ASTContext::isRecursivelyConstructingRequirementMachine(
19991965
if (!rewriteCtx)
20001966
return false;
20011967

2002-
auto &machines = getImpl().RequirementMachines;
2003-
2004-
auto found = machines.find(sig);
2005-
if (found == machines.end())
2006-
return false;
2007-
2008-
return !found->second->isComplete();
1968+
return rewriteCtx->isRecursivelyConstructingRequirementMachine(sig);
20091969
}
20101970

20111971
Optional<llvm::TinyPtrVector<ValueDecl *>>

lib/AST/GenericSignature.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ GenericSignatureImpl::getGenericSignatureBuilder() const {
194194

195195
rewriting::RequirementMachine *
196196
GenericSignatureImpl::getRequirementMachine() const {
197-
// The requirement machine is associated with the canonical signature.
198-
if (!isCanonical())
199-
return getCanonicalSignature()->getRequirementMachine();
197+
if (Machine)
198+
return Machine;
200199

201-
// Requirement machines are stored on the ASTContext.
202-
return getASTContext().getOrCreateRequirementMachine(
203-
CanGenericSignature(this));
200+
const_cast<GenericSignatureImpl *>(this)->Machine
201+
= getASTContext().getOrCreateRequirementMachine(
202+
getCanonicalSignature());
203+
return Machine;
204204
}
205205

206206
bool GenericSignatureImpl::isEqual(GenericSignature Other) const {

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ RequirementMachine::RequirementMachine(RewriteContext &ctx)
343343

344344
RequirementMachine::~RequirementMachine() {}
345345

346-
void RequirementMachine::addGenericSignature(CanGenericSignature sig) {
346+
void RequirementMachine::initWithGenericSignature(CanGenericSignature sig) {
347347
Sig = sig;
348348

349349
PrettyStackTraceGenericSignature debugStack("building rewrite system for", sig);
@@ -360,7 +360,6 @@ void RequirementMachine::addGenericSignature(CanGenericSignature sig) {
360360
llvm::dbgs() << "Adding generic signature " << sig << " {\n";
361361
}
362362

363-
364363
// Collect the top-level requirements, and all transtively-referenced
365364
// protocol requirement signatures.
366365
RewriteSystemBuilder builder(Context, Dump);

lib/AST/RequirementMachine/RequirementMachine.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class RewriteContext;
4545
/// generic signatures and interface types.
4646
class RequirementMachine final {
4747
friend class swift::ASTContext;
48+
friend class swift::rewriting::RewriteContext;
4849

4950
CanGenericSignature Sig;
5051

@@ -77,7 +78,7 @@ class RequirementMachine final {
7778
RequirementMachine &operator=(const RequirementMachine &) = delete;
7879
RequirementMachine &operator=(RequirementMachine &&) = delete;
7980

80-
void addGenericSignature(CanGenericSignature sig);
81+
void initWithGenericSignature(CanGenericSignature sig);
8182

8283
bool isComplete() const;
8384

@@ -118,4 +119,4 @@ class RequirementMachine final {
118119

119120
} // end namespace swift
120121

121-
#endif
122+
#endif

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/AST/Decl.h"
1414
#include "swift/AST/Types.h"
1515
#include "ProtocolGraph.h"
16+
#include "RequirementMachine.h"
1617
#include "RewriteSystem.h"
1718
#include "RewriteContext.h"
1819

@@ -347,6 +348,37 @@ Type RewriteContext::getRelativeTypeForTerm(
347348
{ }, protos, *this);
348349
}
349350

351+
RequirementMachine *RewriteContext::getRequirementMachine(
352+
CanGenericSignature sig) {
353+
auto &machine = Machines[sig];
354+
if (machine) {
355+
if (!machine->isComplete()) {
356+
llvm::errs() << "Re-entrant construction of requirement "
357+
<< "machine for " << sig << "\n";
358+
abort();
359+
}
360+
361+
return machine;
362+
}
363+
364+
// Store this requirement machine before adding the signature,
365+
// to catch re-entrant construction via initWithGenericSignature()
366+
// below.
367+
machine = new rewriting::RequirementMachine(*this);
368+
machine->initWithGenericSignature(sig);
369+
370+
return machine;
371+
}
372+
373+
bool RewriteContext::isRecursivelyConstructingRequirementMachine(
374+
CanGenericSignature sig) {
375+
auto found = Machines.find(sig);
376+
if (found == Machines.end())
377+
return false;
378+
379+
return !found->second->isComplete();
380+
}
381+
350382
/// We print stats in the destructor, which should get executed at the end of
351383
/// a compilation job.
352384
RewriteContext::~RewriteContext() {

lib/AST/RequirementMachine/RewriteContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class RewriteContext final {
5252
/// Cache for merged associated type symbols.
5353
llvm::DenseMap<std::pair<Symbol, Symbol>, Symbol> MergedAssocTypes;
5454

55+
/// Requirement machines built from generic signatures.
56+
llvm::DenseMap<GenericSignature, RequirementMachine *> Machines;
57+
5558
ASTContext &Context;
5659

5760
DebugOptions Debug;
@@ -102,6 +105,9 @@ class RewriteContext final {
102105
Symbol mergeAssociatedTypes(Symbol lhs, Symbol rhs,
103106
const ProtocolGraph &protos);
104107

108+
RequirementMachine *getRequirementMachine(CanGenericSignature sig);
109+
bool isRecursivelyConstructingRequirementMachine(CanGenericSignature sig);
110+
105111
~RewriteContext();
106112
};
107113

0 commit comments

Comments
 (0)