Skip to content

Commit 77d0d99

Browse files
committed
[AutoDiff] NFC: move isDifferentiableProgrammingEnabled.
Move `isDifferentiableProgrammingEnabled` to AutoDiff.h so it's more accessible.
1 parent fe20afb commit 77d0d99

File tree

4 files changed

+23
-21
lines changed

4 files changed

+23
-21
lines changed

include/swift/AST/AutoDiff.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -31,6 +31,7 @@
3131
namespace swift {
3232

3333
class AnyFunctionType;
34+
class SourceFile;
3435
class SILFunctionType;
3536
class TupleType;
3637

@@ -392,6 +393,9 @@ class TangentSpace {
392393
/// derivative generic signature.
393394
using SILDifferentiabilityWitnessKey = std::pair<StringRef, AutoDiffConfig>;
394395

396+
/// Returns `true` iff differentiable programming is enabled.
397+
bool isDifferentiableProgrammingEnabled(SourceFile &SF);
398+
395399
/// Automatic differentiation utility namespace.
396400
namespace autodiff {
397401

lib/AST/AutoDiff.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/AST/AutoDiff.h"
1414
#include "swift/AST/ASTContext.h"
1515
#include "swift/AST/GenericEnvironment.h"
16+
#include "swift/AST/ImportCache.h"
1617
#include "swift/AST/Module.h"
1718
#include "swift/AST/TypeCheckRequests.h"
1819
#include "swift/AST/Types.h"
@@ -124,6 +125,23 @@ void AutoDiffConfig::print(llvm::raw_ostream &s) const {
124125
s << ')';
125126
}
126127

128+
bool swift::isDifferentiableProgrammingEnabled(SourceFile &SF) {
129+
auto &ctx = SF.getASTContext();
130+
// Return true if differentiable programming is explicitly enabled.
131+
if (ctx.LangOpts.EnableExperimentalDifferentiableProgramming)
132+
return true;
133+
// Otherwise, return true iff the `_Differentiation` module is imported in
134+
// the given source file.
135+
bool importsDifferentiationModule = false;
136+
for (auto import : namelookup::getAllImports(&SF)) {
137+
if (import.second->getName() == ctx.Id_Differentiation) {
138+
importsDifferentiationModule = true;
139+
break;
140+
}
141+
}
142+
return importsDifferentiationModule;
143+
}
144+
127145
// TODO(TF-874): This helper is inefficient and should be removed. Unwrapping at
128146
// most once (for curried method types) is sufficient.
129147
static void unwrapCurryLevels(AnyFunctionType *fnTy,

lib/Sema/TypeChecker.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -410,23 +410,6 @@ void swift::performWholeModuleTypeChecking(SourceFile &SF) {
410410
#endif
411411
}
412412

413-
bool swift::isDifferentiableProgrammingEnabled(SourceFile &SF) {
414-
auto &ctx = SF.getASTContext();
415-
// Return true if differentiable programming is explicitly enabled.
416-
if (ctx.LangOpts.EnableExperimentalDifferentiableProgramming)
417-
return true;
418-
// Otherwise, return true iff the `_Differentiation` module is imported in
419-
// the given source file.
420-
bool importsDifferentiationModule = false;
421-
for (auto import : namelookup::getAllImports(&SF)) {
422-
if (import.second->getName() == ctx.Id_Differentiation) {
423-
importsDifferentiationModule = true;
424-
break;
425-
}
426-
}
427-
return importsDifferentiationModule;
428-
}
429-
430413
bool swift::isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF) {
431414
auto &ctx = SF.getASTContext();
432415
// Return true if `AdditiveArithmetic` derived conformances are explicitly

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,9 +1520,6 @@ bool isMemberOperator(FuncDecl *decl, Type type);
15201520
/// Complain if @objc or dynamic is used without importing Foundation.
15211521
void diagnoseAttrsRequiringFoundation(SourceFile &SF);
15221522

1523-
/// Returns `true` iff differentiable programming is enabled.
1524-
bool isDifferentiableProgrammingEnabled(SourceFile &SF);
1525-
15261523
/// Returns `true` iff `AdditiveArithmetic` derived conformances are enabled.
15271524
bool isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF);
15281525

0 commit comments

Comments
 (0)