Skip to content

Commit b80daa1

Browse files
authored
Merge pull request #1284 from swiftwasm/master
[pull] swiftwasm from master
2 parents 31c89a5 + 236237f commit b80daa1

35 files changed

+2060
-332
lines changed

include/swift/AST/ASTContext.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,15 @@ class ASTContext final {
649649
/// metadata.
650650
AvailabilityContext getPrespecializedGenericMetadataAvailability();
651651

652+
/// Get the runtime availability of the swift_compareTypeContextDescriptors
653+
/// for the target platform.
654+
AvailabilityContext getCompareTypeContextDescriptorsAvailability();
655+
656+
/// Get the runtime availability of the
657+
/// swift_compareProtocolConformanceDescriptors entry point for the target
658+
/// platform.
659+
AvailabilityContext getCompareProtocolConformanceDescriptorsAvailability();
660+
652661
/// Get the runtime availability of features introduced in the Swift 5.2
653662
/// compiler for the target platform.
654663
AvailabilityContext getSwift52Availability();

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ SWIFT_TYPEID(PropertyWrapperTypeInfo)
2727
SWIFT_TYPEID(Requirement)
2828
SWIFT_TYPEID(ResilienceExpansion)
2929
SWIFT_TYPEID(FragileFunctionKind)
30+
SWIFT_TYPEID(TangentPropertyInfo)
3031
SWIFT_TYPEID(Type)
3132
SWIFT_TYPEID(TypePair)
3233
SWIFT_TYPEID(TypeWitnessAndDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "swift/Basic/LLVM.h"
2121
#include "swift/Basic/TypeID.h"
22+
2223
namespace swift {
2324

2425
class AbstractFunctionDecl;
@@ -58,14 +59,14 @@ class Requirement;
5859
enum class ResilienceExpansion : unsigned;
5960
struct FragileFunctionKind;
6061
class SourceFile;
62+
struct TangentPropertyInfo;
6163
class Type;
62-
class ValueDecl;
63-
class VarDecl;
64-
class Witness;
6564
class TypeAliasDecl;
66-
class Type;
6765
struct TypePair;
6866
struct TypeWitnessAndDecl;
67+
class ValueDecl;
68+
class VarDecl;
69+
class Witness;
6970
enum class AncestryFlags : uint8_t;
7071
enum class ImplicitMemberAction : uint8_t;
7172
struct FingerprintAndMembers;

include/swift/AST/AutoDiff.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class AnyFunctionType;
3535
class SourceFile;
3636
class SILFunctionType;
3737
class TupleType;
38+
class VarDecl;
3839

3940
/// A function type differentiability kind.
4041
enum class DifferentiabilityKind : uint8_t {
@@ -459,6 +460,99 @@ class DerivativeFunctionTypeError
459460
}
460461
};
461462

463+
/// Describes the "tangent stored property" corresponding to an original stored
464+
/// property in a `Differentiable`-conforming type.
465+
///
466+
/// The tangent stored property is the stored property in the `TangentVector`
467+
/// struct of the `Differentiable`-conforming type, with the same name as the
468+
/// original stored property and with the original stored property's
469+
/// `TangentVector` type.
470+
struct TangentPropertyInfo {
471+
struct Error {
472+
enum class Kind {
473+
/// The original property is `@noDerivative`.
474+
NoDerivativeOriginalProperty,
475+
/// The nominal parent type does not conform to `Differentiable`.
476+
NominalParentNotDifferentiable,
477+
/// The original property's type does not conform to `Differentiable`.
478+
OriginalPropertyNotDifferentiable,
479+
/// The parent `TangentVector` type is not a struct.
480+
ParentTangentVectorNotStruct,
481+
/// The parent `TangentVector` struct does not declare a stored property
482+
/// with the same name as the original property.
483+
TangentPropertyNotFound,
484+
/// The tangent property's type is not equal to the original property's
485+
/// `TangentVector` type.
486+
TangentPropertyWrongType,
487+
/// The tangent property is not a stored property.
488+
TangentPropertyNotStored
489+
};
490+
491+
/// The error kind.
492+
Kind kind;
493+
494+
private:
495+
union Value {
496+
Type type;
497+
Value(Type type) : type(type) {}
498+
Value() {}
499+
} value;
500+
501+
public:
502+
Error(Kind kind) : kind(kind), value() {
503+
assert(kind == Kind::NoDerivativeOriginalProperty ||
504+
kind == Kind::NominalParentNotDifferentiable ||
505+
kind == Kind::OriginalPropertyNotDifferentiable ||
506+
kind == Kind::ParentTangentVectorNotStruct ||
507+
kind == Kind::TangentPropertyNotFound ||
508+
kind == Kind::TangentPropertyNotStored);
509+
};
510+
511+
Error(Kind kind, Type type) : kind(kind), value(type) {
512+
assert(kind == Kind::TangentPropertyWrongType);
513+
};
514+
515+
Type getType() const {
516+
assert(kind == Kind::TangentPropertyWrongType);
517+
return value.type;
518+
}
519+
520+
friend bool operator==(const Error &lhs, const Error &rhs);
521+
};
522+
523+
/// The tangent stored property.
524+
VarDecl *tangentProperty = nullptr;
525+
526+
/// An optional error.
527+
Optional<Error> error = None;
528+
529+
private:
530+
TangentPropertyInfo(VarDecl *tangentProperty, Optional<Error> error)
531+
: tangentProperty(tangentProperty), error(error) {}
532+
533+
public:
534+
TangentPropertyInfo(VarDecl *tangentProperty)
535+
: TangentPropertyInfo(tangentProperty, None) {}
536+
537+
TangentPropertyInfo(Error::Kind errorKind)
538+
: TangentPropertyInfo(nullptr, Error(errorKind)) {}
539+
540+
TangentPropertyInfo(Error::Kind errorKind, Type errorType)
541+
: TangentPropertyInfo(nullptr, Error(errorKind, errorType)) {}
542+
543+
/// Returns `true` iff this tangent property info is valid.
544+
bool isValid() const { return tangentProperty && !error; }
545+
546+
explicit operator bool() const { return isValid(); }
547+
548+
friend bool operator==(const TangentPropertyInfo &lhs,
549+
const TangentPropertyInfo &rhs) {
550+
return lhs.tangentProperty == rhs.tangentProperty && lhs.error == rhs.error;
551+
}
552+
};
553+
554+
void simple_display(llvm::raw_ostream &OS, TangentPropertyInfo info);
555+
462556
/// The key type used for uniquing `SILDifferentiabilityWitness` in
463557
/// `SILModule`: original function name, parameter indices, result indices, and
464558
/// derivative generic signature.

include/swift/AST/DiagnosticsSIL.def

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,26 @@ NOTE(autodiff_loadable_value_addressonly_tangent_unsupported,none,
504504
"properties", (Type, Type))
505505
NOTE(autodiff_enums_unsupported,none,
506506
"differentiating enum values is not yet supported", ())
507+
NOTE(autodiff_stored_property_parent_not_differentiable,none,
508+
"cannot differentiate access to property '%0.%1' because '%0' does not "
509+
"conform to 'Differentiable'", (StringRef, StringRef))
510+
NOTE(autodiff_stored_property_not_differentiable,none,
511+
"cannot differentiate access to property '%0.%1' because property type %2 "
512+
"does not conform to 'Differentiable'", (StringRef, StringRef, Type))
513+
NOTE(autodiff_stored_property_tangent_not_struct,none,
514+
"cannot differentiate access to property '%0.%1' because "
515+
"'%0.TangentVector' is not a struct", (StringRef, StringRef))
507516
NOTE(autodiff_stored_property_no_corresponding_tangent,none,
508-
"property cannot be differentiated because '%0.TangentVector' does not "
509-
"have a member named '%1'", (StringRef, StringRef))
517+
"cannot differentiate access to property '%0.%1' because "
518+
"'%0.TangentVector' does not have a stored property named '%1'",
519+
(StringRef, StringRef))
520+
NOTE(autodiff_tangent_property_wrong_type,none,
521+
"cannot differentiate access to property '%0.%1' because "
522+
"'%0.TangentVector.%1' does not have expected type %2",
523+
(StringRef, StringRef, /*originalPropertyTanType*/ Type))
524+
NOTE(autodiff_tangent_property_not_stored,none,
525+
"cannot differentiate access to property '%0.%1' because "
526+
"'%0.TangentVector.%1' is not a stored property", (StringRef, StringRef))
510527
NOTE(autodiff_coroutines_not_supported,none,
511528
"differentiation of coroutine calls is not yet supported", ())
512529
NOTE(autodiff_cannot_differentiate_writes_to_global_variables,none,

include/swift/AST/TypeCheckRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,27 @@ class DerivativeAttrOriginalDeclRequest
21912191
bool isCached() const { return true; }
21922192
};
21932193

2194+
/// Resolves the "tangent stored property" corresponding to an original stored
2195+
/// property in a `Differentiable`-conforming type.
2196+
class TangentStoredPropertyRequest
2197+
: public SimpleRequest<TangentStoredPropertyRequest,
2198+
TangentPropertyInfo(VarDecl *),
2199+
RequestFlags::Cached> {
2200+
public:
2201+
using SimpleRequest::SimpleRequest;
2202+
2203+
private:
2204+
friend SimpleRequest;
2205+
2206+
// Evaluation.
2207+
TangentPropertyInfo evaluate(Evaluator &evaluator,
2208+
VarDecl *originalField) const;
2209+
2210+
public:
2211+
// Caching.
2212+
bool isCached() const { return true; }
2213+
};
2214+
21942215
/// Checks whether a type eraser has a viable initializer.
21952216
class TypeEraserHasViableInitRequest
21962217
: public SimpleRequest<TypeEraserHasViableInitRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ SWIFT_REQUEST(TypeChecker, SuperclassTypeRequest,
203203
SWIFT_REQUEST(TypeChecker, SynthesizeAccessorRequest,
204204
AccessorDecl *(AbstractStorageDecl *, AccessorKind),
205205
SeparatelyCached, NoLocationInfo)
206+
SWIFT_REQUEST(TypeChecker, TangentStoredPropertyRequest,
207+
llvm::Expected<VarDecl *>(VarDecl *), Cached, NoLocationInfo)
206208
SWIFT_REQUEST(TypeChecker, TypeCheckFunctionBodyRequest,
207209
bool(AbstractFunctionDecl *), Cached, NoLocationInfo)
208210
SWIFT_REQUEST(TypeChecker, TypeCheckFunctionBodyAtLocRequest,

include/swift/Runtime/Metadata.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ const
294294
/// the same context.
295295
bool equalContexts(const ContextDescriptor *a, const ContextDescriptor *b);
296296

297+
/// Determines whether two type context descriptors describe the same type
298+
/// context.
299+
///
300+
/// Runtime availability: Swift 5.4.
301+
///
302+
/// \param lhs The first type context descriptor to compare.
303+
/// \param rhs The second type context descriptor to compare.
304+
///
305+
/// \returns true if both describe the same type context, false otherwise.
306+
SWIFT_RUNTIME_EXPORT
307+
SWIFT_CC(swift)
308+
bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
309+
const TypeContextDescriptor *rhs);
310+
297311
/// Compute the bounds of class metadata with a resilient superclass.
298312
ClassMetadataBounds getResilientMetadataBounds(
299313
const ClassDescriptor *descriptor);
@@ -409,6 +423,21 @@ const WitnessTable *swift_getAssociatedConformanceWitness(
409423
const ProtocolRequirement *reqBase,
410424
const ProtocolRequirement *assocConformance);
411425

426+
/// Determine whether two protocol conformance descriptors describe the same
427+
/// conformance of a type to a protocol.
428+
///
429+
/// Runtime availability: Swift 5.4
430+
///
431+
/// \param lhs The first protocol conformance descriptor to compare.
432+
/// \param rhs The second protocol conformance descriptor to compare.
433+
///
434+
/// \returns true if both describe the same conformance, false otherwise.
435+
SWIFT_RUNTIME_EXPORT
436+
SWIFT_CC(swift)
437+
bool swift_compareProtocolConformanceDescriptors(
438+
const ProtocolConformanceDescriptor *lhs,
439+
const ProtocolConformanceDescriptor *rhs);
440+
412441
/// Fetch a uniqued metadata for a function type.
413442
SWIFT_RUNTIME_EXPORT
414443
const FunctionTypeMetadata *

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ FUNCTION(GetForeignTypeMetadata, swift_getForeignTypeMetadata,
616616
ARGS(SizeTy, TypeMetadataPtrTy),
617617
ATTRS(NoUnwind, ReadNone)) // only writes to runtime-private fields
618618

619+
// SWIFT_RUNTIME_EXPORT
620+
// SWIFT_CC(swift)
621+
// bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
622+
// const TypeContextDescriptor *rhs);
623+
FUNCTION(CompareTypeContextDescriptors,
624+
swift_compareTypeContextDescriptors, SwiftCC,
625+
CompareTypeContextDescriptorsAvailability,
626+
RETURNS(Int1Ty),
627+
ARGS(TypeContextDescriptorPtrTy,
628+
TypeContextDescriptorPtrTy),
629+
ATTRS(NoUnwind, ReadNone))
630+
619631
// MetadataResponse swift_getSingletonMetadata(MetadataRequest request,
620632
// TypeContextDescriptor *type);
621633
FUNCTION(GetSingletonMetadata, swift_getSingletonMetadata,
@@ -723,6 +735,18 @@ FUNCTION(GetAssociatedConformanceWitness,
723735
ProtocolRequirementStructTy->getPointerTo()),
724736
ATTRS(NoUnwind, ReadNone))
725737

738+
// SWIFT_RUNTIME_EXPORT
739+
// SWIFT_CC(swift) bool swift_compareProtocolConformanceDescriptors(
740+
// const ProtocolConformanceDescriptor *lhs,
741+
// const ProtocolConformanceDescriptor *rhs);
742+
FUNCTION(CompareProtocolConformanceDescriptors,
743+
swift_compareProtocolConformanceDescriptors, SwiftCC,
744+
CompareProtocolConformanceDescriptorsAvailability,
745+
RETURNS(Int1Ty),
746+
ARGS(ProtocolConformanceDescriptorPtrTy,
747+
ProtocolConformanceDescriptorPtrTy),
748+
ATTRS(NoUnwind, ReadNone))
749+
726750
// Metadata *swift_getMetatypeMetadata(Metadata *instanceTy);
727751
FUNCTION(GetMetatypeMetadata, swift_getMetatypeMetadata, C_CC, AlwaysAvailable,
728752
RETURNS(TypeMetadataPtrTy),

include/swift/SIL/SILInstruction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5758,6 +5758,13 @@ class FieldIndexCacheBase : public SingleValueInstruction {
57585758
return s;
57595759
}
57605760

5761+
static bool classof(const SILNode *node) {
5762+
SILNodeKind kind = node->getKind();
5763+
return kind == SILNodeKind::StructExtractInst ||
5764+
kind == SILNodeKind::StructElementAddrInst ||
5765+
kind == SILNodeKind::RefElementAddrInst;
5766+
}
5767+
57615768
private:
57625769
unsigned cacheFieldIndex();
57635770
};

include/swift/SILOptimizer/Differentiation/ADContext.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ ADContext::emitNondifferentiabilityError(SILValue value,
253253
getADDebugStream() << "For value:\n" << value;
254254
getADDebugStream() << "With invoker:\n" << invoker << '\n';
255255
});
256-
auto valueLoc = value.getLoc().getSourceLoc();
257256
// If instruction does not have a valid location, use the function location
258257
// as a fallback. Improves diagnostics in some cases.
259-
if (valueLoc.isInvalid())
260-
valueLoc = value->getFunction()->getLocation().getSourceLoc();
258+
auto valueLoc = getValidLocation(value).getSourceLoc();
261259
return emitNondifferentiabilityError(valueLoc, invoker, diag,
262260
std::forward<U>(args)...);
263261
}
@@ -272,12 +270,10 @@ ADContext::emitNondifferentiabilityError(SILInstruction *inst,
272270
getADDebugStream() << "For instruction:\n" << *inst;
273271
getADDebugStream() << "With invoker:\n" << invoker << '\n';
274272
});
275-
auto instLoc = inst->getLoc().getSourceLoc();
276273
// If instruction does not have a valid location, use the function location
277274
// as a fallback. Improves diagnostics for `ref_element_addr` generated in
278275
// synthesized stored property getters.
279-
if (instLoc.isInvalid())
280-
instLoc = inst->getFunction()->getLocation().getSourceLoc();
276+
auto instLoc = getValidLocation(inst).getSourceLoc();
281277
return emitNondifferentiabilityError(instLoc, invoker, diag,
282278
std::forward<U>(args)...);
283279
}

0 commit comments

Comments
 (0)