Skip to content

Commit 4c26b26

Browse files
authored
Merge pull request #2345 from swiftwasm/main
[pull] swiftwasm from main
2 parents d10f353 + 1eb810f commit 4c26b26

File tree

62 files changed

+937
-452
lines changed

Some content is hidden

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

62 files changed

+937
-452
lines changed

include/swift/AST/AutoDiff.h

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -173,80 +173,59 @@ enum class AutoDiffGeneratedDeclarationKind : uint8_t {
173173
BranchingTraceEnum
174174
};
175175

176-
/// SIL-level automatic differentiation indices. Consists of:
177-
/// - The differentiability parameter indices.
178-
/// - The differentiability result indices.
179-
// TODO(TF-913): Remove `SILAutoDiffIndices` in favor of `AutoDiffConfig`.
180-
// `AutoDiffConfig` additionally stores a derivative generic signature.
181-
struct SILAutoDiffIndices {
182-
/// The indices of independent parameters to differentiate with respect to.
183-
IndexSubset *parameters;
184-
/// The indices of dependent results to differentiate from.
185-
IndexSubset *results;
186-
187-
/*implicit*/ SILAutoDiffIndices(IndexSubset *parameters, IndexSubset *results)
188-
: parameters(parameters), results(results) {
189-
assert(parameters && "Parameter indices must be non-null");
190-
assert(results && "Result indices must be non-null");
191-
}
192-
193-
bool operator==(const SILAutoDiffIndices &other) const;
194-
195-
bool operator!=(const SILAutoDiffIndices &other) const {
196-
return !(*this == other);
197-
};
176+
/// Identifies an autodiff derivative function configuration:
177+
/// - Parameter indices.
178+
/// - Result indices.
179+
/// - Derivative generic signature (optional).
180+
struct AutoDiffConfig {
181+
IndexSubset *parameterIndices;
182+
IndexSubset *resultIndices;
183+
GenericSignature derivativeGenericSignature;
184+
185+
/*implicit*/ AutoDiffConfig(
186+
IndexSubset *parameterIndices, IndexSubset *resultIndices,
187+
GenericSignature derivativeGenericSignature = GenericSignature())
188+
: parameterIndices(parameterIndices), resultIndices(resultIndices),
189+
derivativeGenericSignature(derivativeGenericSignature) {}
198190

199191
/// Returns true if `parameterIndex` is a differentiability parameter index.
200192
bool isWrtParameter(unsigned parameterIndex) const {
201-
return parameterIndex < parameters->getCapacity() &&
202-
parameters->contains(parameterIndex);
193+
return parameterIndex < parameterIndices->getCapacity() &&
194+
parameterIndices->contains(parameterIndex);
203195
}
204196

205-
void print(llvm::raw_ostream &s = llvm::outs()) const;
206-
SWIFT_DEBUG_DUMP;
197+
/// Returns true if `resultIndex` is a differentiability result index.
198+
bool isWrtResult(unsigned resultIndex) const {
199+
return resultIndex < resultIndices->getCapacity() &&
200+
resultIndices->contains(resultIndex);
201+
}
207202

203+
AutoDiffConfig withGenericSignature(GenericSignature signature) const {
204+
return AutoDiffConfig(parameterIndices, resultIndices, signature);
205+
}
206+
207+
// TODO(SR-13506): Use principled mangling for AD-generated symbols.
208208
std::string mangle() const {
209209
std::string result = "src_";
210210
interleave(
211-
results->getIndices(),
211+
resultIndices->getIndices(),
212212
[&](unsigned idx) { result += llvm::utostr(idx); },
213213
[&] { result += '_'; });
214214
result += "_wrt_";
215215
llvm::interleave(
216-
parameters->getIndices(),
216+
parameterIndices->getIndices(),
217217
[&](unsigned idx) { result += llvm::utostr(idx); },
218218
[&] { result += '_'; });
219219
return result;
220220
}
221-
};
222-
223-
/// Identifies an autodiff derivative function configuration:
224-
/// - Parameter indices.
225-
/// - Result indices.
226-
/// - Derivative generic signature (optional).
227-
struct AutoDiffConfig {
228-
IndexSubset *parameterIndices;
229-
IndexSubset *resultIndices;
230-
GenericSignature derivativeGenericSignature;
231-
232-
/*implicit*/ AutoDiffConfig(IndexSubset *parameterIndices,
233-
IndexSubset *resultIndices,
234-
GenericSignature derivativeGenericSignature)
235-
: parameterIndices(parameterIndices), resultIndices(resultIndices),
236-
derivativeGenericSignature(derivativeGenericSignature) {}
237-
238-
/// Returns the `SILAutoDiffIndices` corresponding to this config's indices.
239-
// TODO(TF-913): This is a temporary shim for incremental removal of
240-
// `SILAutoDiffIndices`. Eventually remove this.
241-
SILAutoDiffIndices getSILAutoDiffIndices() const;
242221

243222
void print(llvm::raw_ostream &s = llvm::outs()) const;
244223
SWIFT_DEBUG_DUMP;
245224
};
246225

247226
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
248-
const SILAutoDiffIndices &indices) {
249-
indices.print(s);
227+
const AutoDiffConfig &config) {
228+
config.print(s);
250229
return s;
251230
}
252231

include/swift/AST/DiagnosticsSema.def

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,6 +4132,10 @@ NOTE(note_add_async_to_function,none,
41324132
"add 'async' to function %0 to make it asynchronous", (DeclName))
41334133
NOTE(note_add_asynchandler_to_function,none,
41344134
"add '@asyncHandler' to function %0 to create an implicit asynchronous context", (DeclName))
4135+
NOTE(note_add_globalactor_to_function,none,
4136+
"add '@%0' to make %1 %2 part of global actor %3",
4137+
(StringRef, DescriptiveDeclKind, DeclName, Type))
4138+
FIXIT(insert_globalactor_attr, "@%0 ", (Type))
41354139
ERROR(not_objc_function_async,none,
41364140
"'async' function cannot be represented in Objective-C", ())
41374141
NOTE(not_objc_function_type_async,none,
@@ -4217,10 +4221,10 @@ ERROR(global_actor_from_other_global_actor_context,none,
42174221
"%0 %1 isolated to global actor %2 can not be referenced from "
42184222
"different global actor %3",
42194223
(DescriptiveDeclKind, DeclName, Type, Type))
4220-
ERROR(global_actor_from_independent_context,none,
4221-
"%0 %1 isolated to global actor %2 can not be referenced from an "
4222-
"'@actorIndependent' context",
4223-
(DescriptiveDeclKind, DeclName, Type))
4224+
ERROR(global_actor_from_nonactor_context,none,
4225+
"%0 %1 isolated to global actor %2 can not be referenced from "
4226+
"%select{this|an '@actorIndependent'}3 context",
4227+
(DescriptiveDeclKind, DeclName, Type, bool))
42244228
ERROR(actor_isolated_partial_apply,none,
42254229
"actor-isolated %0 %1 can not be partially applied",
42264230
(DescriptiveDeclKind, DeclName))

include/swift/AST/IndexSubset.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class IndexSubset : public llvm::FoldingSetNode {
134134
/// Returns the number of bit words used to store the index subset.
135135
// Note: Use `getCapacity()` to get the total index subset capacity.
136136
// This is public only for unit testing
137-
// (in unittests/AST/SILAutoDiffIndices.cpp).
137+
// (in unittests/AST/IndexSubsetTests.cpp).
138138
unsigned getNumBitWords() const {
139139
return numBitWords;
140140
}

include/swift/Runtime/Bincompat.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- Bincompat.h - Binary compatibility checks. -------------*- 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+
// Checks for enabling binary compatibility workarounds.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
namespace swift {
18+
19+
namespace runtime {
20+
21+
namespace bincompat {
22+
23+
/// Whether protocol conformance iteration should be reversed, to prefer
24+
/// conformances from images that are later in the list over earlier ones.
25+
bool workaroundProtocolConformanceReverseIteration();
26+
27+
} // namespace bincompat
28+
29+
} // namespace runtime
30+
31+
} // namespace swift

include/swift/SIL/SILDifferentiabilityWitness.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ class SILDifferentiabilityWitness
133133
bool isSerialized() const { return IsSerialized; }
134134
const DeclAttribute *getAttribute() const { return Attribute; }
135135

136-
/// Returns the `SILAutoDiffIndices` corresponding to this config's indices.
137-
// TODO(TF-893): This is a temporary shim for incremental removal of
138-
// `SILAutoDiffIndices`. Eventually remove this.
139-
SILAutoDiffIndices getSILAutoDiffIndices() const;
140-
141136
/// Verify that the differentiability witness is well-formed.
142137
void verify(const SILModule &module) const;
143138

include/swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,47 @@ class DifferentiableActivityInfo {
200200
/// (dependent variable) indices.
201201
bool isUseful(SILValue value, IndexSubset *dependentVariableIndices) const;
202202

203-
/// Returns true if the given value is active for the given
204-
/// `SILAutoDiffIndices` (parameter indices and result index).
205-
bool isActive(SILValue value, const SILAutoDiffIndices &indices) const;
203+
/// Returns true if the given value is active for the given parameter indices
204+
/// and result indices.
205+
bool isActive(SILValue value,
206+
IndexSubset *parameterIndices,
207+
IndexSubset *resultIndices) const;
208+
209+
/// Returns true if the given value is active for the given config.
210+
bool isActive(SILValue value, AutoDiffConfig config) const {
211+
return isActive(value, config.parameterIndices, config.resultIndices);
212+
}
213+
214+
/// Returns the activity of the given value for the given config.
215+
Activity getActivity(SILValue value,
216+
IndexSubset *parameterIndices,
217+
IndexSubset *resultIndices) const;
206218

207-
/// Returns the activity of the given value for the given `SILAutoDiffIndices`
208-
/// (parameter indices and result index).
209-
Activity getActivity(SILValue value, const SILAutoDiffIndices &indices) const;
219+
/// Returns the activity of the given value for the given config.
220+
Activity getActivity(SILValue value, AutoDiffConfig config) const {
221+
return getActivity(value, config.parameterIndices, config.resultIndices);
222+
}
210223

211-
/// Prints activity information for the `indices` of the given `value`.
212-
void dump(SILValue value, const SILAutoDiffIndices &indices,
224+
/// Prints activity information for the config of the given value.
225+
void dump(SILValue value,
226+
IndexSubset *parameterIndices, IndexSubset *resultIndices,
213227
llvm::raw_ostream &s = llvm::dbgs()) const;
214228

215-
/// Prints activity information for the given `indices`.
216-
void dump(SILAutoDiffIndices indices,
229+
/// Prints activity information for the config of the given value.
230+
void dump(SILValue value, AutoDiffConfig config,
231+
llvm::raw_ostream &s = llvm::dbgs()) const {
232+
return dump(value, config.parameterIndices, config.resultIndices, s);
233+
}
234+
235+
/// Prints all activity information for the given parameter indices and result
236+
/// indices.
237+
void dump(IndexSubset *parameterIndices, IndexSubset *resultIndices,
217238
llvm::raw_ostream &s = llvm::dbgs()) const;
239+
240+
/// Prints all activity information for the given config.
241+
void dump(AutoDiffConfig config, llvm::raw_ostream &s = llvm::dbgs()) const {
242+
return dump(config.parameterIndices, config.resultIndices, s);
243+
}
218244
};
219245

220246
class DifferentiableActivityCollection {

include/swift/SILOptimizer/Differentiation/ADContext.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ namespace autodiff {
4545

4646
/// Stores `apply` instruction information calculated by VJP generation.
4747
struct NestedApplyInfo {
48-
/// The differentiation indices that are used to differentiate this `apply`
48+
/// The differentiation config that is used to differentiate this `apply`
4949
/// instruction.
50-
SILAutoDiffIndices indices;
50+
AutoDiffConfig config;
5151
/// The original pullback type before reabstraction. `None` if the pullback
5252
/// type is not reabstracted.
5353
Optional<CanSILFunctionType> originalPullbackType;
@@ -120,6 +120,9 @@ class ADContext {
120120
/// Construct an ADContext for the given module.
121121
explicit ADContext(SILModuleTransform &transform);
122122

123+
// No copying.
124+
ADContext(const ADContext &) = delete;
125+
123126
//--------------------------------------------------------------------------//
124127
// General utilities
125128
//--------------------------------------------------------------------------//

include/swift/SILOptimizer/Differentiation/Common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void collectAllActualResultsInTypeOrder(
119119
/// - The set of minimal parameter and result indices for differentiating the
120120
/// `apply` instruction.
121121
void collectMinimalIndicesForFunctionCall(
122-
ApplyInst *ai, SILAutoDiffIndices parentIndices,
122+
ApplyInst *ai, AutoDiffConfig parentConfig,
123123
const DifferentiableActivityInfo &activityInfo,
124124
SmallVectorImpl<SILValue> &results, SmallVectorImpl<unsigned> &paramIndices,
125125
SmallVectorImpl<unsigned> &resultIndices);

include/swift/SILOptimizer/Differentiation/LinearMapInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class LinearMapInfo {
6767
SILLoopInfo *loopInfo;
6868

6969
/// Differentiation indices of the function.
70-
const SILAutoDiffIndices indices;
70+
const AutoDiffConfig config;
7171

7272
/// Mapping from original basic blocks to linear map structs.
7373
llvm::DenseMap<SILBasicBlock *, StructDecl *> linearMapStructs;
@@ -149,7 +149,7 @@ class LinearMapInfo {
149149

150150
explicit LinearMapInfo(ADContext &context, AutoDiffLinearMapKind kind,
151151
SILFunction *original, SILFunction *derivative,
152-
SILAutoDiffIndices indices,
152+
AutoDiffConfig config,
153153
const DifferentiableActivityInfo &activityInfo,
154154
SILLoopInfo *loopInfo);
155155

include/swift/SILOptimizer/Differentiation/Thunk.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ SILValue reabstractFunction(
106106
std::pair<SILFunction *, SubstitutionMap>
107107
getOrCreateSubsetParametersThunkForDerivativeFunction(
108108
SILOptFunctionBuilder &fb, SILValue origFnOperand, SILValue derivativeFn,
109-
AutoDiffDerivativeFunctionKind kind, SILAutoDiffIndices desiredIndices,
110-
SILAutoDiffIndices actualIndices);
109+
AutoDiffDerivativeFunctionKind kind, AutoDiffConfig desiredConfig,
110+
AutoDiffConfig actualConfig);
111111

112112
/// Get or create a derivative function parameter index subset thunk from
113113
/// `actualIndices` to `desiredIndices` for the given associated function
@@ -119,7 +119,7 @@ getOrCreateSubsetParametersThunkForLinearMap(
119119
SILOptFunctionBuilder &fb, SILFunction *assocFn,
120120
CanSILFunctionType origFnType, CanSILFunctionType linearMapType,
121121
CanSILFunctionType targetType, AutoDiffDerivativeFunctionKind kind,
122-
SILAutoDiffIndices desiredIndices, SILAutoDiffIndices actualIndices);
122+
AutoDiffConfig desiredConfig, AutoDiffConfig actualConfig);
123123

124124
} // end namespace autodiff
125125

include/swift/SILOptimizer/Differentiation/VJPCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class VJPCloner final {
5050
SILFunction &getVJP() const;
5151
SILFunction &getPullback() const;
5252
SILDifferentiabilityWitness *getWitness() const;
53-
const SILAutoDiffIndices getIndices() const;
53+
AutoDiffConfig getConfig() const;
5454
DifferentiationInvoker getInvoker() const;
5555
LinearMapInfo &getPullbackInfo() const;
5656
SILLoopInfo *getLoopInfo() const;

0 commit comments

Comments
 (0)