Skip to content

Commit be21aaf

Browse files
committed
Merge branch 'master' of github.com:apple/swift into default-derivative
2 parents f5d304c + b5985e0 commit be21aaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1131
-625
lines changed

docs/DifferentiableProgramming.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,48 +1699,49 @@ public protocol ElementaryFunctions {
16991699
...
17001700
}
17011701

1702-
public extension ElementaryFunctions where Self: Differentiable, Self == Self.TangentVector {
1702+
public extension ElementaryFunctions
1703+
where Self: Differentiable & FloatingPoint, Self == Self.TangentVector {
17031704
@inlinable
17041705
@derivative(of: sqrt)
1705-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1706+
static func _(_ x: Self) -> (value: Self, differential: @differentiable(linear) (Self) -> Self) {
17061707
(sqrt(x), { dx in (1 / 2) * (1 / sqrt(x)) * dx })
17071708
}
17081709

17091710
@inlinable
17101711
@derivative(of: cos)
1711-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1712+
static func _(_ x: Self) -> (value: Self, differential: @differentiable(linear) (Self) -> Self) {
17121713
(cos(x), { dx in -sin(x) * dx })
17131714
}
17141715

17151716
@inlinable
17161717
@derivative(of: asinh)
1717-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1718+
static func _(_ x: Self) -> (value: Self, differential: @differentiable(linear) (Self) -> Self) {
17181719
(asinh(x), { dx in 1 / (1 + x * x) * dx })
17191720
}
17201721

17211722
@inlinable
17221723
@derivative(of: exp)
1723-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1724+
static func _(_ x: Self) -> (value: Self, differential: @differentiable(linear) (Self) -> Self) {
17241725
let ret = exp(x)
17251726
return (ret, { dx in ret * dx })
17261727
}
17271728

17281729
@inlinable
17291730
@derivative(of: exp10)
1730-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1731+
static func _(_ x: Self) -> (value: Self, differential: @differentiable(linear) (Self) -> Self) {
17311732
let ret = exp10(x)
17321733
return (ret, { dx in exp(10) * ret * dx })
17331734
}
17341735

17351736
@inlinable
17361737
@derivative(of: log)
1737-
func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
1738+
static func _(_ x: Self) -> (value: Self, differential: @differential(linear) (Self) -> Self) {
17381739
(log(x), { dx in 1 / x * dx })
17391740
}
17401741

17411742
@inlinable
17421743
@derivative(of: pow)
1743-
func _(_ x: Self, _ y: Self) -> (value: Self, differential: @differential(linear) (Self, Self) -> Self) {
1744+
static func _(_ x: Self, _ y: Self) -> (value: Self, differential: @differentiable(linear) (Self, Self) -> Self) {
17441745
(pow(x, y), { (dx, dy) in
17451746
let l = y * pow(x, y-1) * dx
17461747
let r = pow(x, y) * log(x) * dy
@@ -2086,10 +2087,11 @@ differentiating either one will provide derivatives with respect to parameters
20862087
`T` and `U`. Here are some examples of first-order function types and their
20872088
corresponding curried function types:
20882089

2089-
| First-order function type | Curried function type |
2090-
| @differentiable (T, U) -> V | @differentiable (T) -> @differentiable (U) -> V |
2091-
| @differentiable (T, @noDerivative U) -> V | @differentiable (T) -> (U) -> V |
2092-
| @differentiable (@noDerivative T, U) -> V | (T) -> @differentiable (U) -> V |
2090+
| First-order function type | Curried function type |
2091+
|---------------------------------------------|---------------------------------------------------|
2092+
| `@differentiable (T, U) -> V` | `@differentiable (T) -> @differentiable (U) -> V` |
2093+
| `@differentiable (T, @noDerivative U) -> V` | `@differentiable (T) -> (U) -> V` |
2094+
| `@differentiable (@noDerivative T, U) -> V` | `(T) -> @differentiable (U) -> V` |
20932095

20942096
A curried differentiable function can be formed like any curried
20952097
non-differentiable function in Swift.
@@ -2610,9 +2612,7 @@ func valueWithDifferential<T: FloatingPoint, U: Differentiable>(
26102612

26112613
To differentiate `valueWithDifferential`, we need to be able to differentiate
26122614
its return value, a tuple of the original value and the differential, with
2613-
respect to its `x` argument. Since the return type contains a function,
2614-
[differentiation of higher-order functions](#differentiation-of-higher-order-functions)
2615-
is required for differentiating this differential operator.
2615+
respect to its `x` argument.
26162616

26172617
A kneejerk solution is to differentiate derivative functions generated by the
26182618
differentiation transform at compile-time, but this leads to problems. For

docs/WindowsBuild.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ git clone https://github.com/apple/swift-corelibs-libdispatch toolchain/swift-co
3232
git clone https://github.com/apple/swift-corelibs-foundation toolchain/swift-corelibs-foundation
3333
git clone https://github.com/apple/swift-corelibs-xctest toolchain/swift-corelibs-xctest
3434
git clone https://github.com/apple/swift-llbuild toolchain/llbuild
35-
git clone -c core.autocrlf=input https://github.com/apple/swift-package-manager toolchain/swift-package-manager
35+
git clone https://github.com/apple/swift-tools-support-core toolchain/swift-tools-support-core
36+
git clone -c core.autocrlf=input https://github.com/apple/swift-package-manager toolchain/swiftpm
3637
git clone https://github.com/compnerd/windows-swift windows-swift
3738
```
3839

@@ -155,17 +156,15 @@ path S:\b\llbuild\bin;%PATH%
155156
## Build swift-tools-core-support
156157

157158
```cmd
158-
md S:\b\tsc
159159
cmake -B S:\b\tsc -G Ninja -S S:\toolchain\swift-tools-support-core -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=cl -DCMAKE_Swift_COMPILER=S:/b/toolchain/bin/swiftc.exe -DFoundation_DIR=S:/b/foundation/cmake/modules -Ddispatch_DIR=S:/b/libdispatch/cmake/modules
160160
ninja -C S:\b\tsc
161161
```
162162

163163
## Build swift-package-manager
164164

165165
```cmd
166-
md S:\b\spm
167-
cd S:\b\spm
168-
C:\Python27\python.exe S:\swift-package-manager\Utilities\bootstrap --foundation S:\b\foundation --libdispatch-build-dir S:\b\libdispatch --libdispatch-source-dir S:\swift-corelibs-libdispatch --llbuild-build-dir S:\b\llbuild --llbuild-source-dir S:\llbuild --sqlite-build-dir S:\b\sqlite --sqlite-source-dir S:\sqlite-amalgamation-3270200
166+
cmake -B S:\b\spm -G Ninja -S S:\toolchain\swiftpm -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER=S:/b/toolchain/bin/clang-cl.exe -DCMAKE_CXX_COMPILER=S:/b/toolchain/bin/clang-cl.exe -DCMAKE_Swift_COMPILER=S:/b/toolchain/bin/swiftc.exe -DUSE_VENDORED_TSC=YES -DFoundation_DIR=S:/b/foundation/cmake/modules -Ddispatch_DIR=S:/b/libdispatch/cmake/modules -DLLBuild_DIR=S:/b/llbuild/cmake/modules
167+
ninja -C S:\b\spm
169168
```
170169

171170
## Install the Swift toolchain on Windows

include/swift/Driver/Compilation.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ class Compilation {
257257
/// limit filelists will be used.
258258
size_t FilelistThreshold;
259259

260+
/// Because each frontend job outputs the same info in its .d file, only do it
261+
/// on the first job that actually runs. Write out dummies for the rest of the
262+
/// jobs. This hack saves a lot of time in the build system when incrementally
263+
/// building a project with many files. Record if a scheduled job has already
264+
/// added -emit-dependency-path.
265+
bool HaveAlreadyAddedDependencyPath = false;
266+
267+
/// When set, only the first scheduled frontend job gets the argument needed
268+
/// to produce a make-style dependency file. The other jobs create dummy files
269+
/// in the driver. This hack speeds up incremental compilation by reducing the
270+
/// time for the build system to read each dependency file, which are all
271+
/// identical. This optimization can be disabled by passing
272+
/// -disable-only-one-dependency-file on the command line.
273+
const bool OnlyOneDependencyFile;
274+
260275
/// Scaffolding to permit experimentation with finer-grained dependencies and
261276
/// faster rebuilds.
262277
const bool EnableFineGrainedDependencies;
@@ -309,6 +324,7 @@ class Compilation {
309324
bool SaveTemps = false,
310325
bool ShowDriverTimeCompilation = false,
311326
std::unique_ptr<UnifiedStatsReporter> Stats = nullptr,
327+
bool OnlyOneDependencyFile = false,
312328
bool EnableFineGrainedDependencies = false,
313329
bool VerifyFineGrainedDependencyGraphAfterEveryImport = false,
314330
bool EmitFineGrainedDependencyDotFileAfterEveryImport = false,
@@ -427,6 +443,14 @@ class Compilation {
427443
return FilelistThreshold;
428444
}
429445

446+
/// Called to decide whether to add a dependency path argument, or whether to
447+
/// create a dummy file. Responds by invoking one of the two passed-in
448+
/// functions.
449+
void addDependencyPathOrCreateDummy(
450+
const CommandOutput &Output,
451+
function_ref<void(StringRef)> addDependencyPath,
452+
function_ref<void(StringRef)> createDummy);
453+
430454
UnifiedStatsReporter *getStatsReporter() const {
431455
return Stats.get();
432456
}

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def enable_fine_grained_dependencies :
139139
Flag<["-"], "enable-fine-grained-dependencies">, Flags<[FrontendOption, HelpHidden]>,
140140
HelpText<"Experimental work-in-progress to be more selective about incremental recompilation">;
141141

142+
143+
def disable_only_one_dependency_file :
144+
Flag<["-"], "disable-only-one-dependency-file">, Flags<[DoesNotAffectIncrementalBuild]>,
145+
HelpText<"Disables incremental build optimization that only produces one dependencies file">;
146+
142147
def enable_source_range_dependencies :
143148
Flag<["-"], "enable-source-range-dependencies">, Flags<[]>,
144149
HelpText<"Try using source range information">;

include/swift/SIL/OwnershipUtils.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ class LinearLifetimeChecker {
187187
bool validateLifetime(SILValue value,
188188
ArrayRef<SILInstruction *> consumingUses,
189189
ArrayRef<SILInstruction *> nonConsumingUses) {
190+
assert(llvm::all_of(
191+
consumingUses,
192+
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
193+
"Passed cond branch to a non-BranchPropagatedUser API");
194+
assert(llvm::all_of(
195+
nonConsumingUses,
196+
[](SILInstruction *i) { return !isa<CondBranchInst>(i); }) &&
197+
"Passed cond branch to a non-BranchPropagatedUser API");
190198
auto *consumingUsesCast =
191199
reinterpret_cast<const BranchPropagatedUser *>(consumingUses.data());
192200
auto *nonConsumingUsesCast =
@@ -420,11 +428,11 @@ struct BorrowScopeIntroducingValue {
420428
///
421429
/// NOTE: Scratch space is used internally to this method to store the end
422430
/// borrow scopes if needed.
423-
bool areInstructionsWithinScope(
424-
ArrayRef<BranchPropagatedUser> instructions,
425-
SmallVectorImpl<BranchPropagatedUser> &scratchSpace,
426-
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
427-
DeadEndBlocks &deadEndBlocks) const;
431+
bool
432+
areInstructionsWithinScope(ArrayRef<SILInstruction *> instructions,
433+
SmallVectorImpl<SILInstruction *> &scratchSpace,
434+
SmallPtrSetImpl<SILBasicBlock *> &visitedBlocks,
435+
DeadEndBlocks &deadEndBlocks) const;
428436

429437
private:
430438
/// Internal constructor for failable static constructor. Please do not expand

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ PASS(SimplifyUnreachableContainingBlocks, "simplify-unreachable-containing-block
311311
"Utility pass. Removes all non-term insts from blocks with unreachable terms")
312312
PASS(SerializeSILPass, "serialize-sil",
313313
"Utility pass. Serializes the current SILModule")
314+
PASS(CMOSerializeSILPass, "cmo-serialize-sil",
315+
"Utility pass. Serializes the current SILModule for cross-module-optimization")
314316
PASS(NonInlinableFunctionSkippingChecker, "check-non-inlinable-function-skipping",
315317
"Utility pass to ensure -experimental-skip-non-inlinable-function-bodies "
316318
"skips everything it should")

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,21 @@ bool tryCheckedCastBrJumpThreading(
161161
/// A structure containing callbacks that are called when an instruction is
162162
/// removed or added.
163163
struct InstModCallbacks {
164-
using CallbackTy = std::function<void(SILInstruction *)>;
165-
CallbackTy deleteInst = [](SILInstruction *inst) { inst->eraseFromParent(); };
166-
CallbackTy createdNewInst = [](SILInstruction *) {};
167-
168-
InstModCallbacks(CallbackTy deleteInst, CallbackTy createdNewInst)
169-
: deleteInst(deleteInst), createdNewInst(createdNewInst) {}
164+
std::function<void(SILInstruction *)> deleteInst = [](SILInstruction *inst) {
165+
inst->eraseFromParent();
166+
};
167+
std::function<void(SILInstruction *)> createdNewInst = [](SILInstruction *) {
168+
};
169+
std::function<void(SILValue, SILValue)> replaceValueUsesWith =
170+
[](SILValue oldValue, SILValue newValue) {
171+
oldValue->replaceAllUsesWith(newValue);
172+
};
173+
174+
InstModCallbacks(decltype(deleteInst) deleteInst,
175+
decltype(createdNewInst) createdNewInst,
176+
decltype(replaceValueUsesWith) replaceValueUsesWith)
177+
: deleteInst(deleteInst), createdNewInst(createdNewInst),
178+
replaceValueUsesWith(replaceValueUsesWith) {}
170179
InstModCallbacks() = default;
171180
~InstModCallbacks() = default;
172181
InstModCallbacks(const InstModCallbacks &) = default;
@@ -391,6 +400,10 @@ findLocalApplySites(FunctionRefBaseInst *fri);
391400
/// Gets the base implementation of a method.
392401
AbstractFunctionDecl *getBaseMethod(AbstractFunctionDecl *FD);
393402

403+
SILInstruction *
404+
tryOptimizeApplyOfPartialApply(PartialApplyInst *pai, SILBuilder &builder,
405+
InstModCallbacks callbacks = InstModCallbacks());
406+
394407
} // end namespace swift
395408

396409
#endif

lib/ClangImporter/ClangImporter.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,12 +2962,36 @@ void ClangImporter::loadExtensions(NominalTypeDecl *nominal,
29622962
// For an Objective-C class, import all of the visible categories.
29632963
if (auto objcClass = dyn_cast_or_null<clang::ObjCInterfaceDecl>(
29642964
effectiveClangContext.getAsDeclContext())) {
2965+
SmallVector<clang::NamedDecl *, 4> DelayedCategories;
2966+
29652967
// Simply importing the categories adds them to the list of extensions.
29662968
for (auto I = objcClass->visible_categories_begin(),
29672969
E = objcClass->visible_categories_end();
29682970
I != E; ++I) {
2971+
// Delay installing categories that don't have an owning module.
2972+
if (!I->hasOwningModule()) {
2973+
DelayedCategories.push_back(*I);
2974+
continue;
2975+
}
2976+
29692977
Impl.importDeclReal(*I, Impl.CurrentVersion);
29702978
}
2979+
2980+
// Install all the delayed categories.
2981+
//
2982+
// The very notion of a delayed category is a result of an emergent behavior
2983+
// of the visible categories list and the order we import modules. The list
2984+
// appears in deserialization order rather than some "source order", so it's
2985+
// possible for, say, a bridging header to import a module that defines an
2986+
// interface and some categories, but for the categories in the bridging
2987+
// header to appear *before* the categories in the module - the imported
2988+
// module will be deserialized on demand. We take it on faith that if there
2989+
// is no owning module for a given category, that it was created in such a
2990+
// way, and thus we install it last to try to emulate what we want
2991+
// "source order" to mean.
2992+
for (const auto *DelayedCat : DelayedCategories) {
2993+
Impl.importDeclReal(DelayedCat, Impl.CurrentVersion);
2994+
}
29712995
}
29722996

29732997
// Dig through each of the Swift lookup tables, creating extensions
@@ -3690,6 +3714,24 @@ void ClangImporter::Implementation::lookupAllObjCMembers(
36903714
}
36913715
}
36923716

3717+
// Force the named member of the entire inheritance hierarchy to be loaded and
3718+
// deserialized before loading the named member of this class. This allows the
3719+
// decl members table to be warmed up and enables the correct identification of
3720+
// overrides.
3721+
static void loadNamedMemberOfSuperclassesIfNeeded(const ClassDecl *CD,
3722+
DeclBaseName name) {
3723+
if (!CD)
3724+
return;
3725+
3726+
while ((CD = CD->getSuperclassDecl())) {
3727+
if (CD->hasClangNode()) {
3728+
auto ci = CD->getASTContext().getOrCreateLazyIterableContextData(
3729+
CD, /*lazyLoader=*/nullptr);
3730+
ci->loader->loadNamedMembers(CD, name, ci->memberData);
3731+
}
3732+
}
3733+
}
3734+
36933735
Optional<TinyPtrVector<ValueDecl *>>
36943736
ClangImporter::Implementation::loadNamedMembers(
36953737
const IterableDeclContext *IDC, DeclBaseName N, uint64_t contextData) {
@@ -3751,6 +3793,8 @@ ClangImporter::Implementation::loadNamedMembers(
37513793

37523794
assert(isa<clang::ObjCContainerDecl>(CD) || isa<clang::NamespaceDecl>(CD));
37533795

3796+
loadNamedMemberOfSuperclassesIfNeeded(dyn_cast<ClassDecl>(D), N);
3797+
37543798
TinyPtrVector<ValueDecl *> Members;
37553799
for (auto entry : table->lookup(SerializedSwiftName(N),
37563800
effectiveClangContext)) {

0 commit comments

Comments
 (0)