Skip to content

Commit 6ec3ab9

Browse files
committed
[NFC] Define SILGen Request Zone and GenerateSIL Request
Define a high-level request for SILGen and switch the high-level entrypoint to vector through it.
1 parent 2ec583f commit 6ec3ab9

10 files changed

+246
-2
lines changed

include/swift/AST/SILGenRequests.h

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//===--- SILGenRequests.h - SILGen Requests ---------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines SILGen requests.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_SILGEN_REQUESTS_H
18+
#define SWIFT_SILGEN_REQUESTS_H
19+
20+
#include "swift/AST/ASTTypeIDs.h"
21+
#include "swift/AST/SimpleRequest.h"
22+
23+
namespace swift {
24+
25+
class FileUnit;
26+
class LangOptions;
27+
class ModuleDecl;
28+
class SILModule;
29+
class SILOptions;
30+
class SourceFile;
31+
32+
namespace Lowering {
33+
class TypeConverter;
34+
}
35+
36+
/// Report that a request of the given kind is being evaluated, so it
37+
/// can be recorded by the stats reporter.
38+
template<typename Request>
39+
void reportEvaluatedRequest(UnifiedStatsReporter &stats,
40+
const Request &request);
41+
42+
struct SILGenDescriptor {
43+
llvm::PointerUnion<ModuleDecl *, FileUnit *> context;
44+
Lowering::TypeConverter &conv;
45+
const SILOptions &opts;
46+
47+
friend llvm::hash_code hash_value(const SILGenDescriptor &owner) {
48+
return llvm::hash_combine(owner.context, (void *)&owner.conv,
49+
(void *)&owner.opts);
50+
}
51+
52+
friend bool operator==(const SILGenDescriptor &lhs,
53+
const SILGenDescriptor &rhs) {
54+
return lhs.context == rhs.context &&
55+
&lhs.conv == &rhs.conv &&
56+
&lhs.opts == &rhs.opts;
57+
}
58+
59+
friend bool operator!=(const SILGenDescriptor &lhs,
60+
const SILGenDescriptor &rhs) {
61+
return !(lhs == rhs);
62+
}
63+
64+
public:
65+
static SILGenDescriptor forFile(FileUnit &sf, Lowering::TypeConverter &conv,
66+
const SILOptions &opts) {
67+
return SILGenDescriptor{&sf, conv, opts};
68+
}
69+
70+
static SILGenDescriptor forWholeModule(ModuleDecl *mod,
71+
Lowering::TypeConverter &conv,
72+
const SILOptions &opts) {
73+
return SILGenDescriptor{mod, conv, opts};
74+
}
75+
};
76+
77+
class GenerateSILRequest :
78+
public SimpleRequest<GenerateSILRequest,
79+
SILModule *(SILGenDescriptor),
80+
CacheKind::Uncached> {
81+
public:
82+
using SimpleRequest::SimpleRequest;
83+
84+
private:
85+
friend SimpleRequest;
86+
87+
// Evaluation.
88+
llvm::Expected<SILModule *>
89+
evaluate(Evaluator &evaluator, SILGenDescriptor desc) const;
90+
91+
public:
92+
bool isCached() const { return true; }
93+
};
94+
95+
void simple_display(llvm::raw_ostream &out, const SILGenDescriptor &d);
96+
97+
SourceLoc extractNearestSourceLoc(const SILGenDescriptor &desc);
98+
99+
/// The zone number for SILGen.
100+
#define SWIFT_TYPEID_ZONE SILGen
101+
#define SWIFT_TYPEID_HEADER "swift/AST/SILGenTypeIDZone.def"
102+
#include "swift/Basic/DefineTypeIDZone.h"
103+
#undef SWIFT_TYPEID_ZONE
104+
#undef SWIFT_TYPEID_HEADER
105+
106+
// Set up reporting of evaluated requests.
107+
#define SWIFT_REQUEST(Zone, RequestType, Sig, Caching, LocOptions) \
108+
template<> \
109+
inline void reportEvaluatedRequest(UnifiedStatsReporter &stats, \
110+
const RequestType &request) { \
111+
++stats.getFrontendCounters().RequestType; \
112+
}
113+
#include "swift/AST/SILGenTypeIDZone.def"
114+
#undef SWIFT_REQUEST
115+
116+
} // end namespace swift
117+
118+
#endif // SWIFT_SILGEN_REQUESTS_H
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===--- SILGenTypeIDZone.def -----------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This definition file describes the requests in SILGen's zone.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
SWIFT_REQUEST(SILGen, GenerateSILRequest,
18+
SILModule *(SILGenDescriptor),
19+
Uncached, NoLocationInfo)

include/swift/Basic/Statistics.def

+4
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ FRONTEND_STATISTIC(Sema, NumUnloadedLazyIterableDeclContexts)
266266
#include "swift/IDE/IDERequestIDZone.def"
267267
#undef SWIFT_REQUEST
268268

269+
#define SWIFT_REQUEST(ZONE, NAME, Sig, Caching, LocOptions) FRONTEND_STATISTIC(SILGen, NAME)
270+
#include "swift/AST/SILGenTypeIDZone.def"
271+
#undef SWIFT_REQUEST
272+
269273
/// The next 10 statistics count 5 kinds of SIL entities present
270274
/// after the SILGen and SILOpt phases. The entities are functions,
271275
/// vtables, witness tables, default witness tables and global

include/swift/Basic/TypeID.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum class Zone : uint8_t {
3737
NameLookup = 9,
3838
Parse = 8,
3939
TypeChecker = 10,
40+
SILGen = 12,
4041
// N.B. This is not a formal zone and exists solely to support the unit tests.
4142
ArithmeticEvaluator = 255,
4243
};

include/swift/Subsystems.h

+6
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ namespace swift {
383383
/// ASTContext.
384384
void registerTypeCheckerRequestFunctions(Evaluator &evaluator);
385385

386+
/// Register SILGen-level request functions with the evaluator.
387+
///
388+
/// Clients that form an ASTContext and will perform any SIL generation
389+
/// should call this functions after forming the ASTContext.
390+
void registerSILGenRequestFunctions(Evaluator &evaluator);
391+
386392
/// Register IDE-level request functions with the evaluator.
387393
///
388394
/// The ASTContext will automatically call these upon construction.

lib/Frontend/Frontend.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ bool CompilerInstance::setUpASTContextIfNeeded() {
212212
Invocation.getSearchPathOptions(), SourceMgr, Diagnostics));
213213
registerParseRequestFunctions(Context->evaluator);
214214
registerTypeCheckerRequestFunctions(Context->evaluator);
215+
registerSILGenRequestFunctions(Context->evaluator);
215216

216217
// Migrator, indexing and typo correction need some IDE requests.
217218
// The integrated REPL needs IDE requests for completion.

lib/IDE/CompletionInstance.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
208208
ASTContext::get(langOpts, typeckOpts, searchPathOpts, tmpSM, tmpDiags));
209209
registerIDERequestFunctions(Ctx->evaluator);
210210
registerTypeCheckerRequestFunctions(Ctx->evaluator);
211+
registerSILGenRequestFunctions(Ctx->evaluator);
211212
ModuleDecl *M = ModuleDecl::create(Identifier(), *Ctx);
212213
PersistentParserState newState;
213214
SourceFile *newSF =

lib/SILGen/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_swift_host_library(swiftSILGen STATIC
2828
SILGenPattern.cpp
2929
SILGenPoly.cpp
3030
SILGenProlog.cpp
31+
SILGenRequests.cpp
3132
SILGenStmt.cpp
3233
SILGenThunk.cpp
3334
SILGenType.cpp)

lib/SILGen/SILGen.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "SILGenFunctionBuilder.h"
1818
#include "Scope.h"
1919
#include "swift/AST/DiagnosticsSIL.h"
20+
#include "swift/AST/Evaluator.h"
2021
#include "swift/AST/GenericEnvironment.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/NameLookup.h"
@@ -34,6 +35,7 @@
3435
#include "swift/SIL/SILArgument.h"
3536
#include "swift/SIL/SILDebugScope.h"
3637
#include "swift/SIL/SILProfiler.h"
38+
#include "swift/AST/SILGenRequests.h"
3739
#include "swift/Serialization/SerializedModuleLoader.h"
3840
#include "swift/Serialization/SerializedSILLoader.h"
3941
#include "swift/Strings.h"
@@ -1752,11 +1754,30 @@ SILModule::constructSIL(ModuleDecl *mod, TypeConverter &tc,
17521754
std::unique_ptr<SILModule>
17531755
swift::performSILGeneration(ModuleDecl *mod, Lowering::TypeConverter &tc,
17541756
const SILOptions &options) {
1755-
return SILModule::constructSIL(mod, tc, options, nullptr);
1757+
auto desc = SILGenDescriptor::forWholeModule(mod, tc, options);
1758+
return std::unique_ptr<SILModule>(
1759+
evaluateOrDefault(mod->getASTContext().evaluator,
1760+
GenerateSILRequest{desc},
1761+
nullptr));
17561762
}
17571763

17581764
std::unique_ptr<SILModule>
17591765
swift::performSILGeneration(FileUnit &sf, Lowering::TypeConverter &tc,
17601766
const SILOptions &options) {
1761-
return SILModule::constructSIL(sf.getParentModule(), tc, options, &sf);
1767+
auto desc = SILGenDescriptor::forFile(sf, tc, options);
1768+
return std::unique_ptr<SILModule>(
1769+
evaluateOrDefault(sf.getASTContext().evaluator,
1770+
GenerateSILRequest{desc},
1771+
nullptr));
1772+
}
1773+
1774+
llvm::Expected<SILModule *>
1775+
GenerateSILRequest::evaluate(Evaluator &evaluator, SILGenDescriptor sgd) const {
1776+
if (auto *MD = sgd.context.dyn_cast<ModuleDecl *>()) {
1777+
return SILModule::constructSIL(MD, sgd.conv, sgd.opts, nullptr).release();
1778+
} else {
1779+
auto *SF = sgd.context.get<FileUnit *>();
1780+
return SILModule::constructSIL(SF->getParentModule(),
1781+
sgd.conv, sgd.opts, SF).release();
1782+
}
17621783
}

lib/SILGen/SILGenRequests.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===--- Condition.cpp - Implements the SILGen Condition class ------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/SILGenRequests.h"
14+
#include "swift/AST/ASTContext.h"
15+
#include "swift/AST/Module.h"
16+
#include "swift/AST/FileUnit.h"
17+
#include "swift/AST/SourceFile.h"
18+
#include "swift/SIL/SILModule.h"
19+
#include "swift/Subsystems.h"
20+
21+
using namespace swift;
22+
23+
namespace swift {
24+
// Implement the SILGen type zone (zone 12).
25+
#define SWIFT_TYPEID_ZONE SILGen
26+
#define SWIFT_TYPEID_HEADER "swift/AST/SILGenTypeIDZone.def"
27+
#include "swift/Basic/ImplementTypeIDZone.h"
28+
#undef SWIFT_TYPEID_ZONE
29+
#undef SWIFT_TYPEID_HEADER
30+
} // end namespace swift
31+
32+
void swift::simple_display(llvm::raw_ostream &out,
33+
const SILGenDescriptor &desc) {
34+
auto *MD = desc.context.dyn_cast<ModuleDecl *>();
35+
auto *unit = desc.context.dyn_cast<FileUnit *>();
36+
if (MD) {
37+
out << "SIL Generation for module " << MD->getName();
38+
} else {
39+
assert(unit);
40+
out << "SIL Generation for file ";
41+
switch (unit->getKind()) {
42+
case FileUnitKind::Source:
43+
out << '\"' << cast<SourceFile>(unit)->getFilename() << '\"';
44+
break;
45+
case FileUnitKind::Builtin:
46+
out << "(Builtin)";
47+
break;
48+
case FileUnitKind::DWARFModule:
49+
case FileUnitKind::ClangModule:
50+
case FileUnitKind::SerializedAST:
51+
out << '\"' << cast<LoadedFile>(unit)->getFilename() << '\"';
52+
break;
53+
}
54+
}
55+
}
56+
57+
SourceLoc swift::extractNearestSourceLoc(const SILGenDescriptor &desc) {
58+
return SourceLoc();
59+
}
60+
61+
// Define request evaluation functions for each of the SILGen requests.
62+
static AbstractRequestFunction *silGenRequestFunctions[] = {
63+
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
64+
reinterpret_cast<AbstractRequestFunction *>(&Name::evaluateRequest),
65+
#include "swift/AST/SILGenTypeIDZone.def"
66+
#undef SWIFT_REQUEST
67+
};
68+
69+
void swift::registerSILGenRequestFunctions(Evaluator &evaluator) {
70+
evaluator.registerRequestFunctions(Zone::SILGen,
71+
silGenRequestFunctions);
72+
}

0 commit comments

Comments
 (0)