Skip to content

Commit d48c913

Browse files
authored
Merge pull request #2440 from swiftwasm/main
[pull] swiftwasm from main
2 parents 9301a29 + 7fb4815 commit d48c913

File tree

73 files changed

+1112
-306
lines changed

Some content is hidden

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

73 files changed

+1112
-306
lines changed

docs/ABI/Mangling.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ types where the metadata itself has unknown layout.)
229229
global ::= entity generic-signature? type type* 'Tk' // key path setter
230230
global ::= type generic-signature 'TH' // key path equality
231231
global ::= type generic-signature 'Th' // key path hasher
232+
global ::= global generic-signature? 'TJ' AUTODIFF-FUNCTION-KIND INDEX-SUBSET 'p' INDEX-SUBSET 'r' // autodiff function
232233

233234
global ::= protocol 'TL' // protocol requirements base descriptor
234235
global ::= assoc-type-name 'Tl' // associated type descriptor
@@ -271,6 +272,16 @@ are always non-polymorphic ``<impl-function-type>`` types.
271272
``<VALUE-WITNESS-KIND>`` differentiates the kinds of value
272273
witness functions for a type.
273274

275+
::
276+
277+
AUTODIFF-FUNCTION-KIND ::= 'f' // JVP (forward-mode derivative)
278+
AUTODIFF-FUNCTION-KIND ::= 'r' // VJP (reverse-mode derivative)
279+
AUTODIFF-FUNCTION-KIND ::= 'd' // differential
280+
AUTODIFF-FUNCTION-KIND ::= 'p' // pullback
281+
282+
``<AUTODIFF-FUNCTION-KIND>`` differentiates the kinds of functions assocaited
283+
with a differentiable function used for differentiable programming.
284+
274285
::
275286

276287
global ::= generic-signature? type 'WOy' // Outlined copy
@@ -1004,6 +1015,13 @@ Numbers and Indexes
10041015
``<INDEX>`` is a production for encoding numbers in contexts that can't
10051016
end in a digit; it's optimized for encoding smaller numbers.
10061017

1018+
::
1019+
1020+
INDEX-SUBSET ::= ('S' | 'U')+
1021+
1022+
``<INDEX-SUBSET>`` is encoded like a bit vector and is optimized for encoding
1023+
indices with a small upper bound.
1024+
10071025
Function Specializations
10081026
~~~~~~~~~~~~~~~~~~~~~~~~
10091027

include/swift/AST/ASTMangler.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,23 @@ class ASTMangler : public Mangler {
166166
bool predefined);
167167

168168
/// Mangle the derivative function (JVP/VJP) for the given:
169-
/// - Mangled original function name.
169+
/// - Mangled original function declaration.
170170
/// - Derivative function kind.
171171
/// - Derivative function configuration: parameter/result indices and
172172
/// derivative generic signature.
173173
std::string
174-
mangleAutoDiffDerivativeFunctionHelper(StringRef name,
175-
AutoDiffDerivativeFunctionKind kind,
176-
AutoDiffConfig config);
174+
mangleAutoDiffDerivativeFunction(const AbstractFunctionDecl *originalAFD,
175+
AutoDiffDerivativeFunctionKind kind,
176+
AutoDiffConfig config);
177177

178178
/// Mangle the linear map (differential/pullback) for the given:
179-
/// - Mangled original function name.
179+
/// - Mangled original function declaration.
180180
/// - Linear map kind.
181181
/// - Derivative function configuration: parameter/result indices and
182182
/// derivative generic signature.
183-
std::string mangleAutoDiffLinearMapHelper(StringRef name,
184-
AutoDiffLinearMapKind kind,
185-
AutoDiffConfig config);
183+
std::string mangleAutoDiffLinearMap(const AbstractFunctionDecl *originalAFD,
184+
AutoDiffLinearMapKind kind,
185+
AutoDiffConfig config);
186186

187187
/// Mangle the AutoDiff generated declaration for the given:
188188
/// - Generated declaration kind: linear map struct or branching trace enum.
@@ -255,6 +255,8 @@ class ASTMangler : public Mangler {
255255

256256
std::string mangleOpaqueTypeDecl(const ValueDecl *decl);
257257

258+
std::string mangleGenericSignature(const GenericSignature sig);
259+
258260
enum SpecialContext {
259261
ObjCContext,
260262
ClangImporterContext,
@@ -427,6 +429,12 @@ class ASTMangler : public Mangler {
427429
void appendSymbolicReference(SymbolicReferent referent);
428430

429431
void appendOpaqueDeclName(const OpaqueTypeDecl *opaqueDecl);
432+
433+
void beginManglingWithAutoDiffOriginalFunction(
434+
const AbstractFunctionDecl *afd);
435+
void appendAutoDiffFunctionParts(char functionKindCode,
436+
AutoDiffConfig config);
437+
void appendIndexSubset(IndexSubset *indexSubset);
430438
};
431439

432440
} // end namespace Mangle

include/swift/AST/AutoDiff.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/AST/TypeAlignments.h"
2727
#include "swift/Basic/Range.h"
2828
#include "swift/Basic/SourceLoc.h"
29+
#include "swift/Demangling/Demangle.h"
2930
#include "llvm/ADT/StringExtras.h"
3031
#include "llvm/Support/Error.h"
3132

@@ -655,6 +656,17 @@ getDifferentiabilityWitnessGenericSignature(GenericSignature origGenSig,
655656

656657
} // end namespace swift
657658

659+
namespace swift {
660+
namespace Demangle {
661+
662+
AutoDiffFunctionKind
663+
getAutoDiffFunctionKind(AutoDiffDerivativeFunctionKind kind);
664+
665+
AutoDiffFunctionKind getAutoDiffFunctionKind(AutoDiffLinearMapKind kind);
666+
667+
} // end namespace autodiff
668+
} // end namespace swift
669+
658670
namespace llvm {
659671

660672
using swift::AutoDiffConfig;

include/swift/Demangling/Demangle.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ enum class FunctionSigSpecializationParamKind : unsigned {
117117
ExistentialToGeneric = 1 << 10,
118118
};
119119

120+
enum class AutoDiffFunctionKind : char {
121+
JVP = 'f',
122+
VJP = 'r',
123+
Differential = 'd',
124+
Pullback = 'p',
125+
};
126+
120127
/// The pass that caused the specialization to occur. We use this to make sure
121128
/// that two passes that generate similar changes do not yield the same
122129
/// mangling. This currently cannot happen, so this is just a safety measure

include/swift/Demangling/DemangleNodes.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ NODE(CanonicalPrespecializedGenericTypeCachingOnceToken)
308308

309309
// Added in Swift 5.5
310310
NODE(AsyncFunctionPointer)
311+
NODE(AutoDiffFunction)
312+
NODE(AutoDiffFunctionKind)
313+
NODE(IndexSubset)
311314

312315
#undef CONTEXT_NODE
313316
#undef NODE

include/swift/Demangling/Demangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ class Demangler : public NodeFactory {
569569

570570
NodePointer demangleTypeMangling();
571571
NodePointer demangleSymbolicReference(unsigned char rawKind);
572+
NodePointer demangleAutoDiffFunctionKind();
573+
NodePointer demangleIndexSubset();
572574

573575
bool demangleBoundGenerics(Vector<NodePointer> &TypeListList,
574576
NodePointer &RetroactiveConformances);

include/swift/SILOptimizer/Differentiation/JVPCloner.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ class JVPCloner final {
3939
///
4040
/// The parent JVP cloner stores the original function and an empty
4141
/// to-be-generated pullback function.
42-
explicit JVPCloner(ADContext &context, SILFunction *original,
43-
SILDifferentiabilityWitness *witness, SILFunction *jvp,
44-
DifferentiationInvoker invoker);
42+
explicit JVPCloner(ADContext &context, SILDifferentiabilityWitness *witness,
43+
SILFunction *jvp, DifferentiationInvoker invoker);
4544
~JVPCloner();
4645

4746
/// Performs JVP generation on the empty JVP function. Returns true if any

include/swift/SILOptimizer/Differentiation/VJPCloner.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ class VJPCloner final {
3939
///
4040
/// The parent VJP cloner stores the original function and an empty
4141
/// to-be-generated pullback function.
42-
explicit VJPCloner(ADContext &context, SILFunction *original,
43-
SILDifferentiabilityWitness *witness, SILFunction *vjp,
44-
DifferentiationInvoker invoker);
42+
explicit VJPCloner(ADContext &context, SILDifferentiabilityWitness *witness,
43+
SILFunction *vjp, DifferentiationInvoker invoker);
4544
~VJPCloner();
4645

4746
ADContext &getContext() const;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===------- DifferentiationMangler.h --------- differentiation -*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
#ifndef SWIFT_SIL_UTILS_DIFFERENTIATIONMANGLER_H
14+
#define SWIFT_SIL_UTILS_DIFFERENTIATIONMANGLER_H
15+
16+
#include "swift/AST/ASTMangler.h"
17+
#include "swift/AST/AutoDiff.h"
18+
#include "swift/Basic/NullablePtr.h"
19+
#include "swift/Demangling/Demangler.h"
20+
#include "swift/SIL/SILFunction.h"
21+
22+
namespace swift {
23+
namespace Mangle {
24+
25+
/// A mangler for generated differentiation functions.
26+
class DifferentiationMangler : public ASTMangler {
27+
public:
28+
DifferentiationMangler() {}
29+
/// Returns the mangled name for a differentiation function of the given kind.
30+
std::string mangle(SILFunction *originalFunction,
31+
Demangle::AutoDiffFunctionKind kind,
32+
AutoDiffConfig config);
33+
/// Returns the mangled name for a derivative function of the given kind.
34+
std::string mangleDerivativeFunction(SILFunction *originalFunction,
35+
AutoDiffDerivativeFunctionKind kind,
36+
AutoDiffConfig config);
37+
/// Returns the mangled name for a linear map of the given kind.
38+
std::string mangleLinearMap(SILFunction *originalFunction,
39+
AutoDiffLinearMapKind kind,
40+
AutoDiffConfig config);
41+
};
42+
43+
} // end namespace Mangle
44+
} // end namespace swift
45+
46+
#endif /* SWIFT_SIL_UTILS_DIFFERENTIATIONMANGLER_H */

include/swift/Sema/ConstraintLocator.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,20 @@ class LocatorPathElt::ArgumentAttribute final : public StoredIntegerElement<1> {
762762
}
763763
};
764764

765+
class LocatorPathElt::ConformanceRequirement final
766+
: public StoredPointerElement<ProtocolDecl> {
767+
public:
768+
ConformanceRequirement(ProtocolDecl *protocol)
769+
: StoredPointerElement(PathElementKind::ConformanceRequirement,
770+
protocol) {}
771+
772+
ProtocolDecl *getRequirement() const { return getStoredPointer(); }
773+
774+
static bool classof(const LocatorPathElt *elt) {
775+
return elt->getKind() == ConstraintLocator::ConformanceRequirement;
776+
}
777+
};
778+
765779
namespace details {
766780
template <typename CustomPathElement>
767781
class PathElement {

0 commit comments

Comments
 (0)