Skip to content

Commit de4deb5

Browse files
committed
[AutoDiff] Lazily create synthesized file during differentiation.
Make `ADContext` lazily create a `SynthesizedFileUnit` instead of creating one during `ADContext` construction. This avoids always creating a `SynthesizedFileUnit` in every module, since differentiation is a mandatory transform that always runs. It was nonetheless useful to test always creating a `SynthesizedFileUnit` for testing purposes.
1 parent f7a9eed commit de4deb5

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

include/swift/SILOptimizer/Utils/Differentiation/ADContext.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class ADContext {
6868
SILPassManager &passManager;
6969

7070
/// A synthesized file unit.
71-
SynthesizedFileUnit &synthesizedFile;
71+
SynthesizedFileUnit *synthesizedFile = nullptr;
7272

7373
/// The worklist (stack) of `differentiable_function` instructions to be
7474
/// processed.
@@ -124,9 +124,12 @@ class ADContext {
124124
SILModule &getModule() const { return module; }
125125
ASTContext &getASTContext() const { return module.getASTContext(); }
126126
SILPassManager &getPassManager() const { return passManager; }
127-
SynthesizedFileUnit &getSynthesizedFile() { return synthesizedFile; }
128127
Lowering::TypeConverter &getTypeConverter() { return module.Types; }
129128

129+
/// Get or create a synthesized file for adding generated linear map structs
130+
/// and branching trace enums. Used by `LinearMapInfo`.
131+
SynthesizedFileUnit &getOrCreateSynthesizedFile();
132+
130133
/// Returns true if the `differentiable_function` instruction worklist is
131134
/// empty.
132135
bool isDifferentiableFunctionInstsWorklistEmpty() const {

lib/SILOptimizer/Utils/Differentiation/ADContext.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ static FuncDecl *findOperatorDeclInProtocol(DeclName operatorName,
5454
// ADContext methods
5555
//===----------------------------------------------------------------------===//
5656

57-
static SynthesizedFileUnit *createNewSynthesizedFileUnit(ModuleDecl *module) {
58-
auto &ctx = module->getASTContext();
59-
auto *file = new (ctx) SynthesizedFileUnit(*module);
60-
module->addFile(*file);
61-
return file;
62-
}
63-
6457
ADContext::ADContext(SILModuleTransform &transform)
6558
: transform(transform), module(*transform.getModule()),
66-
passManager(*transform.getPassManager()),
67-
synthesizedFile(*createNewSynthesizedFileUnit(module.getSwiftModule())) {}
59+
passManager(*transform.getPassManager()) {}
60+
61+
SynthesizedFileUnit &ADContext::getOrCreateSynthesizedFile() {
62+
if (synthesizedFile)
63+
return *synthesizedFile;
64+
auto *moduleDecl = module.getSwiftModule();
65+
auto &ctx = moduleDecl->getASTContext();
66+
synthesizedFile = new (ctx) SynthesizedFileUnit(*moduleDecl);
67+
moduleDecl->addFile(*synthesizedFile);
68+
return *synthesizedFile;
69+
}
6870

6971
FuncDecl *ADContext::getPlusDecl() const {
7072
if (!cachedPlusFn) {

lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ LinearMapInfo::LinearMapInfo(ADContext &context, AutoDiffLinearMapKind kind,
5858
const DifferentiableActivityInfo &activityInfo)
5959
: kind(kind), original(original), derivative(derivative),
6060
activityInfo(activityInfo), indices(indices),
61-
synthesizedFile(context.getSynthesizedFile()),
61+
synthesizedFile(context.getOrCreateSynthesizedFile()),
6262
typeConverter(context.getTypeConverter()) {
6363
generateDifferentiationDataStructures(context, derivative);
6464
}

0 commit comments

Comments
 (0)