Skip to content

Commit cb09945

Browse files
committed
Allow SILGen to emit only a specific set of SILDeclRefs
Extend ASTLoweringDescriptor to store a set of SILDeclRefs to emit through `emitFunctionDefinition`.
1 parent 44a8b56 commit cb09945

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

include/swift/AST/SILGenRequests.h

+19-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/ASTTypeIDs.h"
2121
#include "swift/AST/EvaluatorDependencies.h"
2222
#include "swift/AST/SimpleRequest.h"
23+
#include "swift/SIL/SILDeclRef.h"
2324

2425
namespace swift {
2526

@@ -40,22 +41,30 @@ template<typename Request>
4041
void reportEvaluatedRequest(UnifiedStatsReporter &stats,
4142
const Request &request);
4243

44+
using SILRefsToEmit = llvm::SmallVector<SILDeclRef, 1>;
45+
4346
/// Describes a file or module to be lowered to SIL.
4447
struct ASTLoweringDescriptor {
4548
llvm::PointerUnion<FileUnit *, ModuleDecl *> context;
4649
Lowering::TypeConverter &conv;
4750
const SILOptions &opts;
4851

52+
/// A specific set of SILDeclRefs to emit. If set, only these refs will be
53+
/// emitted. Otherwise the entire \c context will be emitted.
54+
Optional<SILRefsToEmit> refsToEmit;
55+
4956
friend llvm::hash_code hash_value(const ASTLoweringDescriptor &owner) {
5057
return llvm::hash_combine(owner.context, (void *)&owner.conv,
51-
(void *)&owner.opts);
58+
(void *)&owner.opts,
59+
owner.refsToEmit);
5260
}
5361

5462
friend bool operator==(const ASTLoweringDescriptor &lhs,
5563
const ASTLoweringDescriptor &rhs) {
5664
return lhs.context == rhs.context &&
5765
&lhs.conv == &rhs.conv &&
58-
&lhs.opts == &rhs.opts;
66+
&lhs.opts == &rhs.opts &&
67+
lhs.refsToEmit == rhs.refsToEmit;
5968
}
6069

6170
friend bool operator!=(const ASTLoweringDescriptor &lhs,
@@ -65,14 +74,16 @@ struct ASTLoweringDescriptor {
6574

6675
public:
6776
static ASTLoweringDescriptor
68-
forFile(FileUnit &sf, Lowering::TypeConverter &conv, const SILOptions &opts) {
69-
return ASTLoweringDescriptor{&sf, conv, opts};
77+
forFile(FileUnit &sf, Lowering::TypeConverter &conv, const SILOptions &opts,
78+
Optional<SILRefsToEmit> refsToEmit = None) {
79+
return ASTLoweringDescriptor{&sf, conv, opts, refsToEmit};
7080
}
7181

72-
static ASTLoweringDescriptor forWholeModule(ModuleDecl *mod,
73-
Lowering::TypeConverter &conv,
74-
const SILOptions &opts) {
75-
return ASTLoweringDescriptor{mod, conv, opts};
82+
static ASTLoweringDescriptor
83+
forWholeModule(ModuleDecl *mod, Lowering::TypeConverter &conv,
84+
const SILOptions &opts,
85+
Optional<SILRefsToEmit> refsToEmit = None) {
86+
return ASTLoweringDescriptor{mod, conv, opts, refsToEmit};
7687
}
7788

7889
/// Retrieves the files to generate SIL for. If the descriptor is configured

include/swift/Basic/AnyValue.h

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ namespace llvm {
2929
hash_code hash_value(const llvm::PointerUnion<PT1, PT2> &ptr) {
3030
return hash_value(ptr.getOpaqueValue());
3131
}
32+
33+
// FIXME: Belongs in LLVM itself
34+
template<typename T>
35+
hash_code hash_value(const llvm::Optional<T> &opt) {
36+
if (!opt)
37+
return 1;
38+
return hash_value(*opt);
39+
}
3240
}
3341

3442
namespace swift {

include/swift/SIL/SILDeclRef.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ struct SILDeclRef {
277277
SILLinkage getLinkage(ForDefinition_t forDefinition) const;
278278

279279
/// Return the hash code for the SIL declaration.
280-
llvm::hash_code getHashCode() const {
281-
return llvm::hash_combine(loc.getOpaqueValue(),
282-
static_cast<int>(kind),
283-
isForeign, defaultArgIndex);
280+
friend llvm::hash_code hash_value(const SILDeclRef &ref) {
281+
return llvm::hash_combine(ref.loc.getOpaqueValue(),
282+
static_cast<int>(ref.kind),
283+
ref.isForeign, ref.defaultArgIndex);
284284
}
285285

286286
bool operator==(SILDeclRef rhs) const {

lib/SILGen/SILGen.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,9 @@ class SILGenModuleRAII {
19101910
SGM.visit(TD);
19111911
}
19121912
}
1913+
void emitSILFunctionDefinition(SILDeclRef ref) {
1914+
SGM.emitFunctionDefinition(ref, SGM.getFunction(ref, ForDefinition));
1915+
}
19131916

19141917
explicit SILGenModuleRAII(SILModule &M) : SGM{M, M.getSwiftModule()} {}
19151918

@@ -1942,11 +1945,17 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
19421945
return llvm::cantFail(evaluator(ParseSILModuleRequest{desc}));
19431946
}
19441947

1945-
// Otherwise perform SIL generation of the passed SourceFiles.
19461948
auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
19471949
desc.opts);
19481950
SILGenModuleRAII scope(*silMod);
19491951

1952+
// Emit a specific set of SILDeclRefs if needed.
1953+
if (auto refs = desc.refsToEmit) {
1954+
for (auto ref : *refs)
1955+
scope.emitSILFunctionDefinition(ref);
1956+
}
1957+
1958+
// Emit any whole-files needed.
19501959
for (auto file : desc.getFilesToEmit()) {
19511960
if (auto *nextSF = dyn_cast<SourceFile>(file))
19521961
scope.emitSourceFile(nextSF);

lib/SILGen/SILGenRequests.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ evaluator::DependencySource ASTLoweringRequest::readDependencySource(
6161
}
6262

6363
ArrayRef<FileUnit *> ASTLoweringDescriptor::getFilesToEmit() const {
64+
// If we have a specific set of SILDeclRefs to emit, we don't emit any whole
65+
// files.
66+
if (refsToEmit)
67+
return {};
68+
6469
if (auto *mod = context.dyn_cast<ModuleDecl *>())
6570
return mod->getFiles();
6671

lib/SILOptimizer/Transforms/CSE.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {
374374
OperandValueArrayRef Operands(X->getAllOperands());
375375
return llvm::hash_combine(X->getKind(),
376376
X->getLookupType().getPointer(),
377-
X->getMember().getHashCode(),
377+
X->getMember(),
378378
X->getConformance(),
379379
X->getType(),
380380
!X->getTypeDependentOperands().empty(),

0 commit comments

Comments
 (0)