Skip to content

Commit 6aab0b2

Browse files
committed
Merge branch 'main' of github.com:swiftwasm/swift into maxd/main-merge
2 parents e9074ee + 65db86b commit 6aab0b2

File tree

200 files changed

+5437
-917
lines changed

Some content is hidden

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

200 files changed

+5437
-917
lines changed

.mailmap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Adrian-Constantin Popescu <[email protected]> <[email protected]>
22
33
4-
4+
55
Alexis Beingessner <[email protected]> <[email protected]>
66
77

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ set(SWIFT_BENCH_MODULES
8080
single-source/DictionaryRemove
8181
single-source/DictionarySubscriptDefault
8282
single-source/DictionarySwap
83+
single-source/Differentiation
8384
single-source/Diffing
8485
single-source/DiffingMyers
8586
single-source/DropFirst
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--- Differentiation.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if canImport(_Differentiation)
14+
15+
import TestsUtils
16+
import _Differentiation
17+
18+
public let Differentiation = [
19+
BenchmarkInfo(
20+
name: "DifferentiationIdentity",
21+
runFunction: run_DifferentiationIdentity,
22+
tags: [.regression, .differentiation]
23+
),
24+
BenchmarkInfo(
25+
name: "DifferentiationSquare",
26+
runFunction: run_DifferentiationSquare,
27+
tags: [.regression, .differentiation]
28+
),
29+
BenchmarkInfo(
30+
name: "DifferentiationArraySum",
31+
runFunction: run_DifferentiationArraySum,
32+
tags: [.regression, .differentiation],
33+
setUpFunction: { blackHole(onesArray) }
34+
),
35+
]
36+
37+
@inline(never)
38+
public func run_DifferentiationIdentity(N: Int) {
39+
func f(_ x: Float) -> Float {
40+
x
41+
}
42+
for _ in 0..<1000*N {
43+
blackHole(valueWithGradient(at: 1, in: f))
44+
}
45+
}
46+
47+
@inline(never)
48+
public func run_DifferentiationSquare(N: Int) {
49+
func f(_ x: Float) -> Float {
50+
x * x
51+
}
52+
for _ in 0..<1000*N {
53+
blackHole(valueWithGradient(at: 1, in: f))
54+
}
55+
}
56+
57+
let onesArray: [Float] = Array(repeating: 1, count: 50)
58+
59+
@inline(never)
60+
public func run_DifferentiationArraySum(N: Int) {
61+
func sum(_ array: [Float]) -> Float {
62+
var result: Float = 0
63+
for i in withoutDerivative(at: 0..<array.count) {
64+
result += array[i]
65+
}
66+
return result
67+
}
68+
for _ in 0..<N {
69+
blackHole(valueWithGradient(at: onesArray, in: sum))
70+
}
71+
}
72+
73+
#endif

benchmark/utils/TestsUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum BenchmarkCategory : String {
2828
case runtime, refcount, metadata
2929
// Other general areas of compiled code validation.
3030
case abstraction, safetychecks, exceptions, bridging, concurrency, existential
31-
case exclusivity
31+
case exclusivity, differentiation
3232

3333
// Algorithms are "micro" that test some well-known algorithm in isolation:
3434
// sorting, searching, hashing, fibonaci, crypto, etc.

benchmark/utils/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ import DictionaryOfAnyHashableStrings
6969
import DictionaryRemove
7070
import DictionarySubscriptDefault
7171
import DictionarySwap
72+
#if canImport(_Differentiation)
73+
import Differentiation
74+
#endif
7275
import Diffing
7376
import DiffingMyers
7477
import DropFirst
@@ -258,6 +261,9 @@ registerBenchmark(DictionaryOfAnyHashableStrings)
258261
registerBenchmark(DictionaryRemove)
259262
registerBenchmark(DictionarySubscriptDefault)
260263
registerBenchmark(DictionarySwap)
264+
#if canImport(_Differentiation)
265+
registerBenchmark(Differentiation)
266+
#endif
261267
registerBenchmark(Diffing)
262268
registerBenchmark(DiffingMyers)
263269
registerBenchmark(DropFirst)

docs/ABI/Mangling.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,11 @@ Types
492492

493493
type ::= 'Bb' // Builtin.BridgeObject
494494
type ::= 'BB' // Builtin.UnsafeValueBuffer
495+
type ::= 'Bc' // Builtin.RawUnsafeContinuation
495496
type ::= 'Bf' NATURAL '_' // Builtin.Float<n>
496497
type ::= 'Bi' NATURAL '_' // Builtin.Int<n>
497498
type ::= 'BI' // Builtin.IntLiteral
499+
type ::= 'Bj' // Builtin.Job
498500
type ::= 'BO' // Builtin.UnknownObject (no longer a distinct type, but still used for AnyObject)
499501
type ::= 'Bo' // Builtin.NativeObject
500502
type ::= 'Bp' // Builtin.RawPointer

docs/DifferentiableProgramming.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ Since complex numbers are not yet defined in the standard library, we extended
18851885
the complex number type defined in the
18861886
[NumericAnnex](https://github.com/xwu/NumericAnnex) library to be
18871887
differentiable.
1888-
[The full implementation is here](https://github.com/tensorflow/swift-apis/blob/master/Sources/third_party/Experimental/Complex.swift).
1888+
[The full implementation is here](https://github.com/tensorflow/swift-apis/blob/main/Sources/third_party/Experimental/Complex.swift).
18891889
The implementation adopts the
18901890
[Autograd convention](https://github.com/HIPS/autograd/blob/master/docs/tutorial.md#complex-numbers)
18911891
for derivatives of functions with complex arguments or results, so that we can

include/swift/ABI/Actor.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===--- Actor.h - ABI structures for actors --------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Swift ABI describing actors.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_ABI_ACTOR_H
18+
#define SWIFT_ABI_ACTOR_H
19+
20+
#include "swift/ABI/HeapObject.h"
21+
#include "swift/ABI/MetadataValues.h"
22+
23+
namespace swift {
24+
25+
/// The default actor implementation. This is the layout of both
26+
/// the DefaultActor and NSDefaultActor classes.
27+
class alignas(Alignment_DefaultActor) DefaultActor : public HeapObject {
28+
public:
29+
// These constructors do not initialize the actor instance, and the
30+
// destructor does not destroy the actor instance; you must call
31+
// swift_defaultActor_{initialize,destroy} yourself.
32+
constexpr DefaultActor(const HeapMetadata *metadata)
33+
: HeapObject(metadata), PrivateData{} {}
34+
35+
constexpr DefaultActor(const HeapMetadata *metadata,
36+
InlineRefCounts::Immortal_t immortal)
37+
: HeapObject(metadata, immortal), PrivateData{} {}
38+
39+
void *PrivateData[NumWords_DefaultActor];
40+
};
41+
42+
} // end namespace swift
43+
44+
#endif

include/swift/ABI/Executor.h

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//===--- Executor.h - ABI structures for executors --------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Swift ABI describing executors.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_ABI_EXECUTOR_H
18+
#define SWIFT_ABI_EXECUTOR_H
19+
20+
#include <inttypes.h>
21+
22+
namespace swift {
23+
class AsyncContext;
24+
class AsyncTask;
25+
class DefaultActor;
26+
class Job;
27+
28+
/// An ExecutorRef isn't necessarily just a pointer to an executor
29+
/// object; it may have other bits set.
30+
class ExecutorRef {
31+
static constexpr uintptr_t IsDefaultActor = 1;
32+
static constexpr uintptr_t PointerMask = 7;
33+
34+
uintptr_t Value;
35+
36+
constexpr ExecutorRef(uintptr_t value) : Value(value) {}
37+
38+
public:
39+
/// A generic execution environment. When running in a generic
40+
/// environment, it's presumed to be okay to switch synchronously
41+
/// to an actor. As an executor request, this represents a request
42+
/// to drop whatever the current actor is.
43+
constexpr static ExecutorRef generic() {
44+
return ExecutorRef(0);
45+
}
46+
47+
/// Given a pointer to a default actor, return an executor reference
48+
/// for it.
49+
static ExecutorRef forDefaultActor(DefaultActor *actor) {
50+
assert(actor);
51+
return ExecutorRef(reinterpret_cast<uintptr_t>(actor) | IsDefaultActor);
52+
}
53+
54+
/// Is this the generic executor reference?
55+
bool isGeneric() const {
56+
return Value == 0;
57+
}
58+
59+
/// Is this a default-actor executor reference?
60+
bool isDefaultActor() const {
61+
return Value & IsDefaultActor;
62+
}
63+
DefaultActor *getDefaultActor() const {
64+
assert(isDefaultActor());
65+
return reinterpret_cast<DefaultActor*>(Value & ~PointerMask);
66+
}
67+
68+
/// Do we have to do any work to start running as the requested
69+
/// executor?
70+
bool mustSwitchToRun(ExecutorRef newExecutor) const {
71+
return *this != newExecutor;
72+
}
73+
74+
bool operator==(ExecutorRef other) const {
75+
return Value == other.Value;
76+
}
77+
bool operator!=(ExecutorRef other) const {
78+
return Value != other.Value;
79+
}
80+
};
81+
82+
using JobInvokeFunction =
83+
SWIFT_CC(swiftasync)
84+
void (Job *, ExecutorRef);
85+
86+
using TaskContinuationFunction =
87+
SWIFT_CC(swiftasync)
88+
void (AsyncTask *, ExecutorRef, AsyncContext *);
89+
90+
template <class Fn>
91+
struct AsyncFunctionTypeImpl;
92+
template <class Result, class... Params>
93+
struct AsyncFunctionTypeImpl<Result(Params...)> {
94+
// TODO: expand and include the arguments in the parameters.
95+
using type = TaskContinuationFunction;
96+
};
97+
98+
template <class Fn>
99+
using AsyncFunctionType = typename AsyncFunctionTypeImpl<Fn>::type;
100+
101+
/// A "function pointer" for an async function.
102+
///
103+
/// Eventually, this will always be signed with the data key
104+
/// using a type-specific discriminator.
105+
template <class FnType>
106+
class AsyncFunctionPointer {
107+
public:
108+
/// The function to run.
109+
RelativeDirectPointer<AsyncFunctionType<FnType>,
110+
/*nullable*/ false,
111+
int32_t> Function;
112+
113+
/// The expected size of the context.
114+
uint32_t ExpectedContextSize;
115+
};
116+
117+
}
118+
119+
#endif

include/swift/ABI/MetadataValues.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ enum {
4242

4343
/// The number of words in a yield-many coroutine buffer.
4444
NumWords_YieldManyBuffer = 8,
45+
46+
/// The number of words (in addition to the heap-object header)
47+
/// in a default actor.
48+
NumWords_DefaultActor = 10,
4549
};
4650

4751
struct InProcess;
@@ -114,6 +118,9 @@ enum class NominalTypeKind : uint32_t {
114118
/// The maximum supported type alignment.
115119
const size_t MaximumAlignment = 16;
116120

121+
/// The alignment of a DefaultActor.
122+
const size_t Alignment_DefaultActor = MaximumAlignment;
123+
117124
/// Flags stored in the value-witness table.
118125
template <typename int_type>
119126
class TargetValueWitnessFlags {
@@ -1885,7 +1892,11 @@ enum class JobKind : size_t {
18851892
Task = 0,
18861893

18871894
/// Job kinds >= 192 are private to the implementation.
1888-
First_Reserved = 192
1895+
First_Reserved = 192,
1896+
1897+
DefaultActorInline = First_Reserved,
1898+
DefaultActorSeparate,
1899+
DefaultActorOverride
18891900
};
18901901

18911902
/// The priority of a job. Higher priorities are larger values.
@@ -1920,6 +1931,10 @@ class JobFlags : public FlagSet<size_t> {
19201931

19211932
explicit JobFlags(size_t bits) : FlagSet(bits) {}
19221933
JobFlags(JobKind kind) { setKind(kind); }
1934+
JobFlags(JobKind kind, JobPriority priority) {
1935+
setKind(kind);
1936+
setPriority(priority);
1937+
}
19231938
constexpr JobFlags() {}
19241939

19251940
FLAGSET_DEFINE_FIELD_ACCESSORS(Kind, Kind_width, JobKind,

0 commit comments

Comments
 (0)