Skip to content

Commit f7a9eed

Browse files
committed
[AutoDiff] Add generated implicit declarations to SynthesizedFileUnit.
Add implicit declarations generated by the differentiation transform to a `SynthesizedFileUnit` instead of an ad-hoc pre-existing `SourceFile`. Resolves TF-1232: type reconstruction for AutoDiff-generated declarations. Previously, type reconstruction failed because retroactively adding declarations to a `SourceFile` did not update name lookup caches.
1 parent c834696 commit f7a9eed

File tree

8 files changed

+31
-24
lines changed

8 files changed

+31
-24
lines changed

include/swift/AST/SynthesizedFileUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SynthesizedFileUnit final : public FileUnit {
3333
~SynthesizedFileUnit() = default;
3434

3535
/// Add a synthesized top-level declaration.
36-
void addTopLevelDecl(ValueDecl *FD) { TopLevelDecls.push_back(FD); }
36+
void addTopLevelDecl(ValueDecl *D) { TopLevelDecls.push_back(D); }
3737

3838
virtual void lookupValue(DeclName name, NLKind lookupKind,
3939
SmallVectorImpl<ValueDecl *> &result) const override;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/AST/DiagnosticsSIL.h"
2121
#include "swift/AST/Expr.h"
22+
#include "swift/AST/SynthesizedFileUnit.h"
2223
#include "swift/SIL/SILBuilder.h"
2324
#include "swift/SILOptimizer/Utils/Differentiation/Common.h"
2425
#include "swift/SILOptimizer/Utils/Differentiation/DifferentiationInvoker.h"
@@ -66,6 +67,9 @@ class ADContext {
6667
/// Shared pass manager.
6768
SILPassManager &passManager;
6869

70+
/// A synthesized file unit.
71+
SynthesizedFileUnit &synthesizedFile;
72+
6973
/// The worklist (stack) of `differentiable_function` instructions to be
7074
/// processed.
7175
llvm::SmallVector<DifferentiableFunctionInst *, 32>
@@ -120,6 +124,7 @@ class ADContext {
120124
SILModule &getModule() const { return module; }
121125
ASTContext &getASTContext() const { return module.getASTContext(); }
122126
SILPassManager &getPassManager() const { return passManager; }
127+
SynthesizedFileUnit &getSynthesizedFile() { return synthesizedFile; }
123128
Lowering::TypeConverter &getTypeConverter() { return module.Types; }
124129

125130
/// Returns true if the `differentiable_function` instruction worklist is

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define SWIFT_SILOPTIMIZER_UTILS_DIFFERENTIATION_LINEARMAPINFO_H
1919

2020
#include "swift/AST/AutoDiff.h"
21+
#include "swift/AST/SynthesizedFileUnit.h"
2122
#include "swift/SIL/ApplySite.h"
2223
#include "swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h"
2324
#include "llvm/ADT/DenseMap.h"
@@ -85,6 +86,9 @@ class LinearMapInfo {
8586
/// Mapping from linear map structs to their branching trace enum fields.
8687
llvm::DenseMap<StructDecl *, VarDecl *> linearMapStructEnumFields;
8788

89+
/// A synthesized file unit.
90+
SynthesizedFileUnit &synthesizedFile;
91+
8892
/// A type converter, used to compute struct/enum SIL types.
8993
Lowering::TypeConverter &typeConverter;
9094

@@ -97,13 +101,8 @@ class LinearMapInfo {
97101
VarDecl *addVarDecl(NominalTypeDecl *nominal, StringRef name, Type type);
98102

99103
/// Retrieves the file unit that contains implicit declarations in the
100-
/// current Swift module. If it does not exist, create one.
101-
///
102-
// FIXME: Currently it defaults to the file containing `original`, if it can
103-
// be determined. Otherwise, it defaults to any file unit in the module. To
104-
// handle this more properly, we could revive the DerivedFileUnit class to
105-
// contain all synthesized implicit type declarations.
106-
SourceFile &getDeclarationFileUnit();
104+
/// current Swift module.
105+
SynthesizedFileUnit &getSynthesizedFile() { return synthesizedFile; }
107106

108107
/// Computes and sets the access level for the given nominal type, given the
109108
/// original function linkage.

lib/AST/Module.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,8 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
11801180
// The Builtin module declares no operators.
11811181
return nullptr;
11821182
case FileUnitKind::Synthesized:
1183+
// Synthesized files currently declare no operators.
1184+
return nullptr;
11831185
case FileUnitKind::Source:
11841186
break;
11851187
case FileUnitKind::SerializedAST:
@@ -1527,6 +1529,9 @@ StringRef ModuleDecl::getModuleFilename() const {
15271529
Result = LF->getFilename();
15281530
continue;
15291531
}
1532+
// Skip synthesized files.
1533+
if (auto *SFU = dyn_cast<SynthesizedFileUnit>(F))
1534+
continue;
15301535
return StringRef();
15311536
}
15321537
return Result;

lib/IRGen/IRGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ static void performParallelIRGeneration(
12411241
CurrentIGMPtr IGM = irgen.getGenModule(SF);
12421242
IGM->emitSourceFile(*SF);
12431243
} else if (auto *nextSFU = dyn_cast<SynthesizedFileUnit>(File)) {
1244-
CurrentIGMPtr IGM = irgen.getGenModule(SF);
1244+
CurrentIGMPtr IGM = irgen.getGenModule(nextSFU);
12451245
IGM->emitSynthesizedFileUnit(*nextSFU);
12461246
} else {
12471247
File->collectLinkLibraries([&](LinkLibrary LinkLib) {

lib/SILOptimizer/Utils/Differentiation/ADContext.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,17 @@ 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+
5764
ADContext::ADContext(SILModuleTransform &transform)
5865
: transform(transform), module(*transform.getModule()),
59-
passManager(*transform.getPassManager()) {}
66+
passManager(*transform.getPassManager()),
67+
synthesizedFile(*createNewSynthesizedFileUnit(module.getSwiftModule())) {}
6068

6169
FuncDecl *ADContext::getPlusDecl() const {
6270
if (!cachedPlusFn) {

lib/SILOptimizer/Utils/Differentiation/LinearMapInfo.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +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()),
6162
typeConverter(context.getTypeConverter()) {
6263
generateDifferentiationDataStructures(context, derivative);
6364
}
@@ -84,17 +85,6 @@ VarDecl *LinearMapInfo::addVarDecl(NominalTypeDecl *nominal, StringRef name,
8485
return varDecl;
8586
}
8687

87-
SourceFile &LinearMapInfo::getDeclarationFileUnit() {
88-
if (original->hasLocation())
89-
if (auto *declContext = original->getLocation().getAsDeclContext())
90-
if (auto *parentSourceFile = declContext->getParentSourceFile())
91-
return *parentSourceFile;
92-
for (auto *file : original->getModule().getSwiftModule()->getFiles())
93-
if (auto *src = dyn_cast<SourceFile>(file))
94-
return *src;
95-
llvm_unreachable("No files?");
96-
}
97-
9888
void LinearMapInfo::computeAccessLevel(NominalTypeDecl *nominal,
9989
SILLinkage originalLinkage) {
10090
auto &astCtx = nominal->getASTContext();
@@ -129,7 +119,7 @@ LinearMapInfo::createBranchingTraceDecl(SILBasicBlock *originalBB,
129119
assert(originalBB->getParent() == original);
130120
auto &astCtx = original->getASTContext();
131121
auto *moduleDecl = original->getModule().getSwiftModule();
132-
auto &file = getDeclarationFileUnit();
122+
auto &file = getSynthesizedFile();
133123
// Create a branching trace enum.
134124
Mangle::ASTMangler mangler;
135125
auto *resultIndices = IndexSubset::get(
@@ -200,7 +190,7 @@ LinearMapInfo::createLinearMapStruct(SILBasicBlock *originalBB,
200190
assert(originalBB->getParent() == original);
201191
auto *original = originalBB->getParent();
202192
auto &astCtx = original->getASTContext();
203-
auto &file = getDeclarationFileUnit();
193+
auto &file = getSynthesizedFile();
204194
// Create a linear map struct.
205195
Mangle::ASTMangler mangler;
206196
auto *resultIndices = IndexSubset::get(

test/AutoDiff/compiler_crashers/tf1232-autodiff-generated-declaration-mangling.swift renamed to test/AutoDiff/compiler_crashers_fixed/tf1232-autodiff-generated-declaration-mangling.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not %target-build-swift -g %s
1+
// RUN: %target-build-swift -g %s
22
// REQUIRES: asserts
33

44
// TF-1232: IRGenDebugInfo crash due to lack of proper mangling for

0 commit comments

Comments
 (0)