Merge pull request #12658 from rajbarik/master

Improve Osize
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 598ad56..8c019f5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,9 @@
 Swift 4.1
 ---------
 
+* [SE-0161][] is fully implemented. KeyPaths now support subscript, optional
+  chaining, and optional force-unwrapping components.
+
 * [SE-0186][]
 
   It is no longer valid to use the ownership keywords `weak` and `unowned` for property declarations in protocols. These keywords are meaningless and misleading when used in a protocol as they don't have any effect.
diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake
index 53081bd..6db2e9a 100644
--- a/cmake/modules/AddSwift.cmake
+++ b/cmake/modules/AddSwift.cmake
@@ -1531,6 +1531,11 @@
             if("${sdk}" STREQUAL "ANDROID")
               list(APPEND swiftlib_private_link_libraries_targets
                    "-latomic")
+            # the same issue on FreeBSD, missing symbols:
+            # __atomic_store, __atomic_compare_exchange, __atomic_load
+            elseif("${sdk}" STREQUAL "FREEBSD")
+              list(APPEND swiftlib_private_link_libraries_targets
+                   "${SWIFTLIB_DIR}/clang/lib/freebsd/libclang_rt.builtins-${arch}.a")
             endif()
           elseif("${lib}" STREQUAL "ICU_I18N")
             list(APPEND swiftlib_private_link_libraries_targets
diff --git a/include/swift/AST/DiagnosticsDriver.def b/include/swift/AST/DiagnosticsDriver.def
index a589010..fbafa1f 100644
--- a/include/swift/AST/DiagnosticsDriver.def
+++ b/include/swift/AST/DiagnosticsDriver.def
@@ -136,6 +136,11 @@
 ERROR(error_profile_missing,none,
       "no profdata file exists at '%0'", (StringRef))
 
+WARNING(warn_opt_remark_disabled, none,
+        "Emission of optimization records has been disabled, because it "
+        "requires a single compiler invocation: consider enabling the "
+        "-whole-module-optimization flag", ())
+
 #ifndef DIAG_NO_UNDEF
 # if defined(DIAG)
 #  undef DIAG
diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def
index f18ce60..8fa09bc 100644
--- a/include/swift/AST/DiagnosticsSema.def
+++ b/include/swift/AST/DiagnosticsSema.def
@@ -1635,17 +1635,18 @@
      (DescriptiveDeclKind, DeclName, DeclName))
 
 // "Near matches"
-WARNING(optional_req_near_match,none,
-        "%0 %1 nearly matches optional requirement %2 of protocol %3",
-        (DescriptiveDeclKind, DeclName, DeclName, DeclName))
+WARNING(req_near_match,none,
+        "%0 %1 nearly matches %select{defaulted|optional}2 requirement %3 "
+        "of protocol %4",
+        (DescriptiveDeclKind, DeclName, bool, DeclName, DeclName))
 NOTE(optional_req_nonobjc_near_match_add_objc,none,
      "add '@objc' to provide an Objective-C entrypoint", ())
-NOTE(optional_req_near_match_move,none,
+NOTE(req_near_match_move,none,
      "move %0 to %select{an|another}1 extension to silence this warning",
      (DeclName, unsigned))
-NOTE(optional_req_near_match_nonobjc,none,
+NOTE(req_near_match_nonobjc,none,
      "add '@nonobjc' to silence this %select{warning|error}0", (bool))
-NOTE(optional_req_near_match_access,none,
+NOTE(req_near_match_access,none,
      "make %0 %select{ERROR|private|private|non-public|non-public}1 to silence this "
      "warning", (DeclName, AccessLevel))
 
@@ -1924,10 +1925,6 @@
 NOTE(override_unnecessary_IUO_silence,none,
      "add parentheses to silence this warning", ())
 
-ERROR(iuo_in_illegal_position,none,
-      "implicitly unwrapped optionals are only allowed at top level and as "
-      "function results", ())
-
 ERROR(override_mutable_covariant_property,none,
       "cannot override mutable property %0 of type %1 with covariant type %2",
       (Identifier, Type, Type))
@@ -3111,6 +3108,22 @@
 ERROR(tuple_ellipsis,none,
       "cannot create a variadic tuple", ())
 
+WARNING(implicitly_unwrapped_optional_spelling_deprecated,none,
+        "the spelling 'ImplicitlyUnwrappedOptional' is deprecated", ())
+
+WARNING(implicitly_unwrapped_optional_spelling_deprecated_with_fixit,none,
+        "the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name", ())
+
+ERROR(implicitly_unwrapped_optional_spelling_error,none,
+        "the spelling 'ImplicitlyUnwrappedOptional' in unsupported; use an explicit type followed by '!'", ())
+
+ERROR(implicitly_unwrapped_optional_spelling_error_with_fixit,none,
+        "the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name", ())
+
+ERROR(iuo_in_illegal_position,none,
+      "implicitly unwrapped optionals are only allowed at top level and as "
+      "function results", ())
+
 // Ownership
 ERROR(invalid_ownership_type,none,
       "'%select{strong|weak|unowned|unowned}0' may only be applied to "
diff --git a/include/swift/AST/GenericSignatureBuilder.h b/include/swift/AST/GenericSignatureBuilder.h
index 508f9e2..cd38cde 100644
--- a/include/swift/AST/GenericSignatureBuilder.h
+++ b/include/swift/AST/GenericSignatureBuilder.h
@@ -91,9 +91,12 @@
   using UnresolvedType = llvm::PointerUnion<PotentialArchetype *, Type>;
   class ResolvedType;
 
-  using RequirementRHS =
+  using UnresolvedRequirementRHS =
       llvm::PointerUnion3<Type, PotentialArchetype *, LayoutConstraint>;
 
+  using RequirementRHS =
+    llvm::PointerUnion3<Type, PotentialArchetype *, LayoutConstraint>;
+
   /// The location of a requirement as written somewhere in the source.
   typedef llvm::PointerUnion<const TypeRepr *, const RequirementRepr *>
     WrittenRequirementLoc;
@@ -158,7 +161,7 @@
     /// the equivalence class that is held together by derived constraints.
     struct DerivedSameTypeComponent {
       /// The potential archetype that acts as the anchor for this component.
-      PotentialArchetype *anchor;
+      UnresolvedType anchor;
 
       /// The (best) requirement source within the component that makes the
       /// potential archetypes in this component equivalent to the concrete
@@ -223,15 +226,13 @@
     /// archetype in this equivalence class to a concrete type along with
     /// that concrete type as written.
     Optional<ConcreteConstraint>
-    findAnyConcreteConstraintAsWritten(
-                              PotentialArchetype *preferredPA = nullptr) const;
+    findAnyConcreteConstraintAsWritten(Type preferredType = Type()) const;
 
     /// Find a source of the superclass constraint in this equivalence class
     /// that has a type equivalence to \c superclass, along with that
     /// superclass type as written.
     Optional<ConcreteConstraint>
-    findAnySuperclassConstraintAsWritten(
-                              PotentialArchetype *preferredPA = nullptr) const;
+    findAnySuperclassConstraintAsWritten(Type preferredType = Type()) const;
 
     /// Determine whether conformance to the given protocol is satisfied by
     /// a superclass requirement.
@@ -267,8 +268,8 @@
 
     /// The cached archetype anchor.
     struct {
-      /// The cached archetype anchor itself.
-      PotentialArchetype *anchor = nullptr;
+      /// The cached anchor itself.
+      Type anchor;
 
       /// The number of members of the equivalence class when the archetype
       /// anchor was cached.
@@ -339,7 +340,7 @@
   /// as appropriate based on \c unresolvedHandling.
   ConstraintResult handleUnresolvedRequirement(RequirementKind kind,
                                    UnresolvedType lhs,
-                                   RequirementRHS rhs,
+                                   UnresolvedRequirementRHS rhs,
                                    FloatingRequirementSource source,
                                    EquivalenceClass *unresolvedEquivClass,
                                    UnresolvedHandlingKind unresolvedHandling);
@@ -536,13 +537,14 @@
   ///
   /// \param f A function object that will be passed each requirement
   /// and requirement source.
-  void enumerateRequirements(llvm::function_ref<
+  void enumerateRequirements(
+                    ArrayRef<GenericTypeParamType *> genericParams,
+                    llvm::function_ref<
                       void (RequirementKind kind,
-                            PotentialArchetype *archetype,
+                            Type type,
                             RequirementRHS constraint,
                             const RequirementSource *source)> f);
 
-public:
   /// Retrieve the generic parameters used to describe the generic
   /// signature being built.
   ArrayRef<GenericTypeParamType *> getGenericParams() const;
@@ -727,30 +729,36 @@
   /// class of the given potential archetype.
   void checkConcreteTypeConstraints(
                             ArrayRef<GenericTypeParamType *> genericParams,
-                            PotentialArchetype *pa);
+                            EquivalenceClass *equivClass);
 
   /// Check the superclass constraints within the equivalence
   /// class of the given potential archetype.
   void checkSuperclassConstraints(
                             ArrayRef<GenericTypeParamType *> genericParams,
-                            PotentialArchetype *pa);
+                            EquivalenceClass *equivClass);
 
   /// Check conformance constraints within the equivalence class of the
   /// given potential archetype.
   void checkConformanceConstraints(
                             ArrayRef<GenericTypeParamType *> genericParams,
-                            PotentialArchetype *pa);
+                            EquivalenceClass *equivClass);
 
   /// Check layout constraints within the equivalence class of the given
   /// potential archetype.
   void checkLayoutConstraints(ArrayRef<GenericTypeParamType *> genericParams,
-                              PotentialArchetype *pa);
+                              EquivalenceClass *equivClass);
 
   /// Check same-type constraints within the equivalence class of the
   /// given potential archetype.
   void checkSameTypeConstraints(
                             ArrayRef<GenericTypeParamType *> genericParams,
-                            PotentialArchetype *pa);
+                            EquivalenceClass *equivClass);
+
+  /// Realize a potential archetype for the given type.
+  ///
+  /// The resolved archetype will be written back into the unresolved type,
+  /// to make the next resolution more efficient.
+  PotentialArchetype *realizePotentialArchetype(UnresolvedType &type);
 
 public:
   /// \brief Try to resolve the equivalence class of the given type.
@@ -1656,20 +1664,6 @@
                                        const_cast<PotentialArchetype *>(this));
   }
 
-  /// \brief Retrieve the potential archetype to be used as the anchor for
-  /// potential archetype computations.
-  PotentialArchetype *getArchetypeAnchor(GenericSignatureBuilder &builder);
-
-  /// \brief Retrieve (or create) a nested type that is the current best
-  /// nested archetype anchor (locally) with the given name.
-  ///
-  /// When called on the archetype anchor, this will produce the named
-  /// archetype anchor.
-  PotentialArchetype *getNestedArchetypeAnchor(
-                       Identifier name,
-                       GenericSignatureBuilder &builder,
-                       ArchetypeResolutionKind kind);
-
   /// Update the named nested type when we know this type conforms to the given
   /// protocol.
   ///
@@ -1727,7 +1721,7 @@
 
   Kind kind;
   UnresolvedType lhs;
-  RequirementRHS rhs;
+  UnresolvedRequirementRHS rhs;
   FloatingRequirementSource source;
 
   /// Dump a debugging representation of this delayed requirement class.
diff --git a/include/swift/AST/SILOptions.h b/include/swift/AST/SILOptions.h
index 71c096c..bbd37b9 100644
--- a/include/swift/AST/SILOptions.h
+++ b/include/swift/AST/SILOptions.h
@@ -156,6 +156,10 @@
   /// \brief Enable large loadable types IRGen pass.
   bool EnableLargeLoadableTypes = true;
 
+  /// The name of the file to which the backend should save YAML optimization
+  /// records.
+  std::string OptRecordFile;
+
   SILOptions() {}
 
   /// Return a hash code of any components from these options that should
diff --git a/include/swift/Driver/Types.def b/include/swift/Driver/Types.def
index 14c69b4..3d29dcc 100644
--- a/include/swift/Driver/Types.def
+++ b/include/swift/Driver/Types.def
@@ -61,6 +61,7 @@
 TYPE("tbd",             TBD,                "tbd",             "")
 TYPE("module-trace",    ModuleTrace,        "trace.json",      "")
 TYPE("index-data",      IndexData,          "",                "")
+TYPE("opt-record",      OptRecord,          "opt.yaml",        "")
 
 // Misc types
 TYPE("pcm",             ClangModuleFile,    "pcm",             "")
diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td
index 3013666..f6178af 100644
--- a/include/swift/Option/Options.td
+++ b/include/swift/Option/Options.td
@@ -331,6 +331,13 @@
   HelpText<"Report missed transformations by optimization passes whose "
            "name matches the given POSIX regular expression">;
 
+def save_optimization_record : Flag<["-"], "save-optimization-record">,
+  Flags<[FrontendOption]>, HelpText<"Generate a YAML optimization record file">;
+def save_optimization_record_path :
+  Separate<["-"], "save-optimization-record-path">,
+  Flags<[FrontendOption]>,
+  HelpText<"Specify the file name of any generated YAML optimization record">;
+
 // Platform options.
 def enable_app_extension : Flag<["-"], "application-extension">,
   Flags<[FrontendOption, NoInteractiveOption]>,
diff --git a/include/swift/Runtime/RuntimeFunctions.def b/include/swift/Runtime/RuntimeFunctions.def
index f1dd504..4183f1b 100644
--- a/include/swift/Runtime/RuntimeFunctions.def
+++ b/include/swift/Runtime/RuntimeFunctions.def
@@ -142,13 +142,13 @@
          ATTRS(NoUnwind))
 
 // void swift_errorInMain(error *ptr);
-FUNCTION(ErrorInMain, swift_errorInMain, DefaultCC,
+FUNCTION(ErrorInMain, swift_errorInMain, SwiftCC,
          RETURNS(VoidTy),
          ARGS(ErrorPtrTy),
          ATTRS(NoUnwind))
 
 // void swift_unexpectedError(error *ptr);
-FUNCTION(UnexpectedError, swift_unexpectedError, DefaultCC,
+FUNCTION(UnexpectedError, swift_unexpectedError, SwiftCC,
          RETURNS(VoidTy),
          ARGS(ErrorPtrTy),
          ATTRS(NoUnwind, NoReturn))
diff --git a/include/swift/SIL/OptimizationRemark.h b/include/swift/SIL/OptimizationRemark.h
index b18a783..d19d923 100644
--- a/include/swift/SIL/OptimizationRemark.h
+++ b/include/swift/SIL/OptimizationRemark.h
@@ -20,12 +20,13 @@
 #define SWIFT_SIL_OPTIMIZATIONREMARKEMITTER_H
 
 #include "swift/Basic/SourceLoc.h"
+#include "swift/SIL/SILBasicBlock.h"
 #include "swift/SIL/SILInstruction.h"
+#include "swift/SIL/SILModule.h"
 #include "llvm/ADT/StringRef.h"
 
 namespace swift {
 
-class ASTContext;
 class SILFunction;
 
 namespace OptRemark {
@@ -60,6 +61,9 @@
   /// Arguments collected via the streaming interface.
   SmallVector<Argument, 4> Args;
 
+  /// The name of the pass generating the remark.
+  StringRef PassName;
+
   /// Textual identifier for the remark (single-word, camel-case). Can be used
   /// by external tools reading the YAML output file for optimization remarks to
   /// identify the remark.
@@ -68,9 +72,13 @@
   /// Source location for the diagnostics.
   SourceLoc Location;
 
+  /// The function for the diagnostics.
+  SILFunction *Function;
+
 protected:
   Remark(StringRef Identifier, SILInstruction &I)
-      : Identifier(Identifier), Location(I.getLoc().getSourceLoc()) {}
+      : Identifier(Identifier), Location(I.getLoc().getSourceLoc()),
+        Function(I.getParent()->getParent()) {}
 
 public:
   DerivedT &operator<<(StringRef S) {
@@ -83,8 +91,15 @@
     return *static_cast<DerivedT *>(this);
   }
 
+  StringRef getPassName() const { return PassName; }
+  StringRef getIdentifier() const { return Identifier; }
+  SILFunction *getFunction() const { return Function; }
   SourceLoc getLocation() const { return Location; }
   std::string getMsg() const;
+  Remark<DerivedT> &getRemark() { return *this; }
+  SmallVector<Argument, 4> &getArgs() { return Args; }
+
+  void setPassName(StringRef PN) { PassName = PN; }
 };
 
 /// Remark to report a successful optimization.
@@ -99,7 +114,7 @@
 /// Used to emit the remarks.  Passes reporting remarks should create an
 /// instance of this.
 class Emitter {
-  ASTContext &Ctx;
+  SILModule &Module;
   std::string PassName;
   bool PassedEnabled;
   bool MissedEnabled;
@@ -110,7 +125,7 @@
   template <typename RemarkT> bool isEnabled();
 
 public:
-  Emitter(StringRef PassName, ASTContext &Ctx);
+  Emitter(StringRef PassName, SILModule &M);
 
   /// \brief Take a lambda that returns a remark which will be emitted.  The
   /// lambda is not evaluated unless remarks are enabled.  Second argument is
@@ -119,8 +134,9 @@
   void emit(T RemarkBuilder, decltype(RemarkBuilder()) * = nullptr) {
     using RemarkT = decltype(RemarkBuilder());
     // Avoid building the remark unless remarks are enabled.
-    if (isEnabled<RemarkT>()) {
+    if (isEnabled<RemarkT>() || Module.getOptRecordStream()) {
       auto R = RemarkBuilder();
+      R.setPassName(PassName);
       emit(R);
     }
   }
diff --git a/include/swift/SIL/SILInstruction.h b/include/swift/SIL/SILInstruction.h
index ce25549..5610ab7 100644
--- a/include/swift/SIL/SILInstruction.h
+++ b/include/swift/SIL/SILInstruction.h
@@ -664,9 +664,6 @@
       : SILInstruction(kind, loc),
         ValueBase(ValueKind(kind), type, IsRepresentative::No) {}
 
-  using SILInstruction::getFunction;
-  using SILInstruction::getModule;
-  using SILInstruction::getKind;
   using SILInstruction::operator new;
   using SILInstruction::dumpInContext;
   using SILInstruction::print;
@@ -674,6 +671,12 @@
 
   // Redeclare because lldb currently doesn't know about using-declarations
   void dump() const;
+  SILFunction *getFunction() { return SILInstruction::getFunction(); }
+  const SILFunction *getFunction() const {
+    return SILInstruction::getFunction();
+  }
+  SILModule &getModule() const { return SILInstruction::getModule(); }
+  SILInstructionKind getKind() const { return SILInstruction::getKind(); }
 
   void operator delete(void *Ptr, size_t) SWIFT_DELETE_OPERATOR_DELETED
 
diff --git a/include/swift/SIL/SILModule.h b/include/swift/SIL/SILModule.h
index c7d37c8..16981d6 100644
--- a/include/swift/SIL/SILModule.h
+++ b/include/swift/SIL/SILModule.h
@@ -46,6 +46,12 @@
 #include "llvm/Support/raw_ostream.h"
 #include <functional>
 
+namespace llvm {
+namespace yaml {
+class Output;
+} // end namespace yaml
+} // end namespace llvm
+
 namespace swift {
   class AnyFunctionType;
   class ASTContext;
@@ -178,6 +184,14 @@
   // The list of SILCoverageMaps in the module.
   CoverageMapListType coverageMaps;
 
+  /// This is the underlying raw stream of OptRecordStream.
+  ///
+  /// It is also owned by SILModule in order to keep their lifetime in sync.
+  std::unique_ptr<llvm::raw_ostream> OptRecordRawStream;
+
+  /// If non-null, the YAML file where remarks should be recorded.
+  std::unique_ptr<llvm::yaml::Output> OptRecordStream;
+
   /// This is a cache of intrinsic Function declarations to numeric ID mappings.
   llvm::DenseMap<Identifier, IntrinsicInfo> IntrinsicIDCache;
 
@@ -448,7 +462,11 @@
   }
   iterator_range<coverage_map_const_iterator> getCoverageMaps() const {
     return {coverageMaps.begin(), coverageMaps.end()};
- }
+  }
+
+  llvm::yaml::Output *getOptRecordStream() { return OptRecordStream.get(); }
+  void setOptRecordStream(std::unique_ptr<llvm::yaml::Output> &&Stream,
+                          std::unique_ptr<llvm::raw_ostream> &&RawStream);
 
   /// Look for a global variable by name.
   ///
diff --git a/include/swift/SIL/SILWitnessTable.h b/include/swift/SIL/SILWitnessTable.h
index 9d6c75f..0fe98f9 100644
--- a/include/swift/SIL/SILWitnessTable.h
+++ b/include/swift/SIL/SILWitnessTable.h
@@ -272,8 +272,7 @@
                            IsSerialized_t isSerialized);
 
   // Whether a conformance should be serialized.
-  static bool conformanceIsSerialized(ProtocolConformance *conformance,
-                                      ResilienceStrategy strategy);
+  static bool conformanceIsSerialized(ProtocolConformance *conformance);
 
   /// Print the witness table.
   void print(llvm::raw_ostream &OS, bool Verbose = false) const;
diff --git a/include/swift/SIL/TypeLowering.h b/include/swift/SIL/TypeLowering.h
index 7f1904c..84376c5 100644
--- a/include/swift/SIL/TypeLowering.h
+++ b/include/swift/SIL/TypeLowering.h
@@ -531,9 +531,9 @@
 
   llvm::SmallVector<DependentTypeState, 1> DependentTypes;
 
-  llvm::DenseMap<SILDeclRef, SILConstantInfo> ConstantTypes;
+  llvm::DenseMap<SILDeclRef, SILConstantInfo *> ConstantTypes;
   
-  llvm::DenseMap<OverrideKey, SILConstantInfo> ConstantOverrideTypes;
+  llvm::DenseMap<OverrideKey, SILConstantInfo *> ConstantOverrideTypes;
 
   llvm::DenseMap<AnyFunctionRef, CaptureInfo> LoweredCaptures;
 
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 8aefea5..ac5aba6 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -2874,7 +2874,7 @@
     if (auto objcAttr = conflicts[0]->getAttrs().getAttribute<ObjCAttr>())
       hasExplicitObjCAttribute = !objcAttr->isImplicit();
     if (!hasExplicitObjCAttribute)
-      Diags.diagnose(conflicts[0], diag::optional_req_near_match_nonobjc, true)
+      Diags.diagnose(conflicts[0], diag::req_near_match_nonobjc, true)
         .fixItInsert(
           conflicts[0]->getAttributeInsertionLoc(/*forModifier=*/false),
           "@nonobjc ");
diff --git a/lib/AST/ASTVerifier.cpp b/lib/AST/ASTVerifier.cpp
index dc4cd51..cd864b4 100644
--- a/lib/AST/ASTVerifier.cpp
+++ b/lib/AST/ASTVerifier.cpp
@@ -2097,10 +2097,17 @@
                  "Storage overrides but setter does not");
         if (ASD->getMaterializeForSetFunc() &&
             baseASD->getMaterializeForSetFunc() &&
-            baseASD->isSetterAccessibleFrom(ASD->getDeclContext()))
-          assert(ASD->getMaterializeForSetFunc()->getOverriddenDecl() ==
-                 baseASD->getMaterializeForSetFunc() &&
-                 "Storage override but materializeForSet does not");
+            baseASD->isSetterAccessibleFrom(ASD->getDeclContext())) {
+          if (baseASD->getMaterializeForSetFunc()->hasForcedStaticDispatch()) {
+            assert(ASD->getMaterializeForSetFunc()->getOverriddenDecl() == nullptr
+                   && "Forced static dispatch materializeForSet should not be "
+                   "overridden");
+          } else {
+            assert(ASD->getMaterializeForSetFunc()->getOverriddenDecl() ==
+                   baseASD->getMaterializeForSetFunc() &&
+                   "Storage override but materializeForSet does not");
+          }
+        }
       } else {
         if (ASD->getGetter())
           assert(!ASD->getGetter()->getOverriddenDecl() &&
diff --git a/lib/AST/ASTWalker.cpp b/lib/AST/ASTWalker.cpp
index 053e189..87a506b 100644
--- a/lib/AST/ASTWalker.cpp
+++ b/lib/AST/ASTWalker.cpp
@@ -137,6 +137,12 @@
       if (doIt(Inherit))
         return true;
     }
+    if (auto *Where = ED->getTrailingWhereClause()) {
+      for(auto &Req: Where->getRequirements()) {
+        if (doIt(Req))
+          return true;
+      }
+    }
     for (Decl *M : ED->getMembers()) {
       if (doIt(M))
         return true;
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 6a079db..7551830 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -3401,7 +3401,7 @@
 }
 
 GenericParamList *ProtocolDecl::createGenericParams(DeclContext *dc) {
-  auto *outerGenericParams = dc->getParent()->getGenericParamsOfContext();
+  auto *outerGenericParams = getParent()->getGenericParamsOfContext();
 
   // The generic parameter 'Self'.
   auto &ctx = getASTContext();
diff --git a/lib/AST/GenericSignature.cpp b/lib/AST/GenericSignature.cpp
index e4e89e9..2b31ca1 100644
--- a/lib/AST/GenericSignature.cpp
+++ b/lib/AST/GenericSignature.cpp
@@ -975,6 +975,16 @@
       return;
     }
 
+    // If we have a superclass or concrete requirement, the conformance
+    // we need is stored in it.
+    if (source->kind == RequirementSource::Superclass ||
+        source->kind == RequirementSource::Concrete) {
+      auto conformance = source->getProtocolConformance();
+      assert(conformance.getRequirement() == conformingProto);
+      path.path.push_back({source->getAffectedType(), conformingProto});
+      return;
+    }
+
     // If we still have a parent, keep going.
     if (source->parent) {
       buildPath(reqs, source->parent, conformingProto, rootType,
diff --git a/lib/AST/GenericSignatureBuilder.cpp b/lib/AST/GenericSignatureBuilder.cpp
index 4a48432..0ce322a 100644
--- a/lib/AST/GenericSignatureBuilder.cpp
+++ b/lib/AST/GenericSignatureBuilder.cpp
@@ -61,6 +61,8 @@
   typedef EquivalenceClass::DerivedSameTypeComponent DerivedSameTypeComponent;
   typedef GenericSignatureBuilder::DelayedRequirement DelayedRequirement;
   typedef GenericSignatureBuilder::ResolvedType ResolvedType;
+  typedef GenericSignatureBuilder::UnresolvedType GSBUnresolvedType;
+  typedef GenericSignatureBuilder::RequirementRHS RequirementRHS;
 } // end anonymous namespace
 
 namespace llvm {
@@ -391,6 +393,20 @@
   };
 } // end namespace llvm
 
+namespace {
+  /// Retrieve the type described by the given unresolved tyoe.
+  Type getUnresolvedType(GSBUnresolvedType type,
+                         ArrayRef<GenericTypeParamType *> genericParams) {
+    if (auto concrete = type.dyn_cast<Type>())
+      return concrete;
+
+    if (auto pa = type.dyn_cast<PotentialArchetype *>())
+      return pa->getDependentType(genericParams);
+
+    return Type();
+  }
+}
+
 #pragma mark Requirement sources
 
 #ifndef NDEBUG
@@ -1681,8 +1697,7 @@
 }
 
 Optional<ConcreteConstraint>
-EquivalenceClass::findAnyConcreteConstraintAsWritten(
-                                      PotentialArchetype *preferredPA) const {
+EquivalenceClass::findAnyConcreteConstraintAsWritten(Type preferredType) const {
   // If we don't have a concrete type, there's no source.
   if (!concreteType) return None;
 
@@ -1691,7 +1706,8 @@
   for (const auto &constraint : concreteTypeConstraints) {
     if (constraint.source->getLoc().isValid()) {
       result = constraint;
-      if (!preferredPA || constraint.isSubjectEqualTo(preferredPA))
+      if (!preferredType ||
+          constraint.getSubjectDependentType({ })->isEqual(preferredType))
         return result;
     }
   }
@@ -1701,7 +1717,7 @@
 
 Optional<ConcreteConstraint>
 EquivalenceClass::findAnySuperclassConstraintAsWritten(
-                                      PotentialArchetype *preferredPA) const {
+                                                   Type preferredType) const {
   // If we don't have a superclass, there's no source.
   if (!superclass) return None;
 
@@ -1712,7 +1728,8 @@
         constraint.value->isEqual(superclass)) {
       result = constraint;
 
-      if (!preferredPA || constraint.isSubjectEqualTo(preferredPA))
+      if (!preferredType ||
+          constraint.getSubjectDependentType({ })->isEqual(preferredType))
         return result;
     }
   }
@@ -1915,11 +1932,108 @@
   return populateResult((nestedTypeNameCache[name] = std::move(entry)));
 }
 
+/// Determine whether any part of this potential archetype's path to the
+/// root contains the given equivalence class.
+static bool pathContainsEquivalenceClass(GenericSignatureBuilder &builder,
+                                         PotentialArchetype *pa,
+                                         EquivalenceClass *equivClass) {
+  // Chase the potential archetype up to the root.
+  for (; pa; pa = pa->getParent()) {
+    // Check whether this potential archetype is in the given equivalence
+    // class.
+    if (pa->getOrCreateEquivalenceClass(builder) == equivClass)
+      return true;
+  }
+
+  return false;
+}
+
 Type EquivalenceClass::getAnchor(
                             GenericSignatureBuilder &builder,
                             ArrayRef<GenericTypeParamType *> genericParams) {
-  auto anchorPA = members.front()->getArchetypeAnchor(builder);
-  return anchorPA->getDependentType(genericParams);
+  // Check whether the cache is valid.
+  if (archetypeAnchorCache.anchor &&
+      archetypeAnchorCache.numMembers == members.size()) {
+    ++NumArchetypeAnchorCacheHits;
+
+    // Reparent the anchor using genericParams.
+    return archetypeAnchorCache.anchor.subst(
+             [&](SubstitutableType *dependentType) {
+               if (auto gp = dyn_cast<GenericTypeParamType>(dependentType)) {
+                 unsigned index =
+                   GenericParamKey(gp).findIndexIn(genericParams);
+                 return Type(genericParams[index]);
+               }
+
+               return Type(dependentType);
+             },
+             MakeAbstractConformanceForGenericType());
+  }
+
+  // Map the members of this equivalence class to the best associated type
+  // within that equivalence class.
+  llvm::SmallDenseMap<EquivalenceClass *, AssociatedTypeDecl *> nestedTypes;
+
+  PotentialArchetype *bestGenericParam = nullptr;
+  for (auto member : members) {
+    // If the member is a generic parameter, keep the best generic parameter.
+    if (member->isGenericParam()) {
+      if (!bestGenericParam ||
+          compareDependentTypes(&member, &bestGenericParam) < 0)
+        bestGenericParam = member;
+      continue;
+    }
+
+    // If we saw a generic parameter, ignore any nested types.
+    if (bestGenericParam) continue;
+
+    // If the nested type doesn't have an associated type, skip it.
+    auto assocType = member->getResolvedAssociatedType();
+    if (!assocType) continue;
+
+    // Dig out the equivalence class of the parent.
+    auto parentEquivClass =
+      member->getParent()->getOrCreateEquivalenceClass(builder);
+
+    // If the path from this member to the root contains this equivalence
+    // class, it cannot be part of the anchor.
+    if (pathContainsEquivalenceClass(builder, member->getParent(), this))
+      continue;
+
+    // Take the best associated type for this equivalence class.
+    assocType = assocType->getAssociatedTypeAnchor();
+    auto &bestAssocType = nestedTypes[parentEquivClass];
+    if (!bestAssocType ||
+        compareAssociatedTypes(assocType, bestAssocType) < 0)
+      bestAssocType = assocType;
+  }
+
+  // If we found a generic parameter, return that.
+  if (bestGenericParam)
+    return bestGenericParam->getDependentType(genericParams);
+
+  // Determine the best anchor among the parent equivalence classes.
+  Type bestParentAnchor;
+  AssociatedTypeDecl *bestAssocType = nullptr;
+  std::pair<EquivalenceClass *, Identifier> bestNestedType;
+  for (const auto &nestedType : nestedTypes) {
+    auto parentAnchor = nestedType.first->getAnchor(builder, genericParams);
+    if (!bestParentAnchor ||
+        compareDependentTypes(parentAnchor, bestParentAnchor) < 0) {
+      bestParentAnchor = parentAnchor;
+      bestAssocType = nestedType.second;
+    }
+  }
+
+  // Form the anchor type.
+  Type anchorType = DependentMemberType::get(bestParentAnchor, bestAssocType);
+
+  // Record the cache miss and update the cache.
+  ++NumArchetypeAnchorCacheMisses;
+  archetypeAnchorCache.anchor = anchorType;
+  archetypeAnchorCache.numMembers = members.size();
+
+  return anchorType;
 }
 
 Type EquivalenceClass::getTypeInContext(GenericSignatureBuilder &builder,
@@ -2157,7 +2271,7 @@
 ConstraintResult GenericSignatureBuilder::handleUnresolvedRequirement(
                                    RequirementKind kind,
                                    UnresolvedType lhs,
-                                   RequirementRHS rhs,
+                                   UnresolvedRequirementRHS rhs,
                                    FloatingRequirementSource source,
                                    EquivalenceClass *unresolvedEquivClass,
                                    UnresolvedHandlingKind unresolvedHandling) {
@@ -2272,7 +2386,7 @@
   // appropriately.
   const RequirementSource *superclassSource;
   if (auto writtenSource =
-        equivClass->findAnySuperclassConstraintAsWritten(nullptr))
+        equivClass->findAnySuperclassConstraintAsWritten())
     superclassSource = writtenSource->source;
   else
     superclassSource = equivClass->superclassConstraints.front().source;
@@ -2287,22 +2401,8 @@
 /// Realize a potential archetype for this type parameter.
 PotentialArchetype *ResolvedType::realizePotentialArchetype(
                                            GenericSignatureBuilder &builder) {
-  if (auto pa = getPotentialArchetypeIfKnown())
-    return pa;
-
-  // Resolve the potential archetype now.
-  Type type = this->type.get<Type>();
-  assert(type->isTypeParameter());
-  auto pa =
-    builder.maybeResolveEquivalenceClass(type,
-                                         ArchetypeResolutionKind::WellFormed,
-                                         /*wantExactPotentialArchetype=*/true)
-      .getPotentialArchetypeIfKnown();
-  assert(pa && "Not a resolvable type!");
-
-  // Cache the potential archetype, now that it's been realized.
-  this->type = pa;
-  return pa;
+  // Realize and cache the potential archetype.
+  return builder.realizePotentialArchetype(type);
 }
 
 Type ResolvedType::getDependentType(GenericSignatureBuilder &builder) const {
@@ -2328,7 +2428,8 @@
 
   // Dig out the associated type.
   AssociatedTypeDecl *assocType = nullptr;
-  if (auto depMemTy = nested.getDependentType(builder)->getAs<DependentMemberType>())
+  if (auto depMemTy =
+        nested.getDependentType(builder)->getAs<DependentMemberType>())
     assocType = depMemTy->getAssocType();
 
   if (!assocType) return;
@@ -2546,60 +2647,6 @@
   llvm_unreachable("potential archetype total order failure");
 }
 
-PotentialArchetype *PotentialArchetype::getArchetypeAnchor(
-                                           GenericSignatureBuilder &builder) {
-  // Find the best archetype within this equivalence class.
-  PotentialArchetype *rep = getRepresentative();
-  PotentialArchetype *anchor;
-  if (auto parent = getParent()) {
-    // For a nested type, retrieve the parent archetype anchor first.
-    auto parentAnchor = parent->getArchetypeAnchor(builder);
-    assert(parentAnchor->getNestingDepth() <= parent->getNestingDepth());
-    anchor = parentAnchor->getNestedArchetypeAnchor(
-                                  getNestedName(), builder,
-                                  ArchetypeResolutionKind::CompleteWellFormed);
-
-    // FIXME: Hack for cases where we couldn't resolve the nested type.
-    if (!anchor)
-      anchor = rep;
-  } else {
-    anchor = rep;
-  }
-
-  auto equivClass = rep->getEquivalenceClassIfPresent();
-  if (!equivClass) return anchor;
-
-  // Check whether
-  if (equivClass->archetypeAnchorCache.anchor &&
-      equivClass->archetypeAnchorCache.numMembers
-        == equivClass->members.size()) {
-    ++NumArchetypeAnchorCacheHits;
-    return equivClass->archetypeAnchorCache.anchor;
-  }
-
-  // Find the best type within this equivalence class.
-  for (auto pa : equivClass->members) {
-    if (compareDependentTypes(&pa, &anchor) < 0)
-      anchor = pa;
-  }
-
-#if SWIFT_GSB_EXPENSIVE_ASSERTIONS
-  // Make sure that we did, in fact, get one that is better than all others.
-  for (auto pa : equivClass->members) {
-    assert((pa == anchor || compareDependentTypes(&anchor, &pa) < 0) &&
-           compareDependentTypes(&pa, &anchor) >= 0 &&
-           "archetype anchor isn't a total order");
-  }
-#endif
-
-  // Record the cache miss and update the cache.
-  ++NumArchetypeAnchorCacheMisses;
-  equivClass->archetypeAnchorCache.anchor = anchor;
-  equivClass->archetypeAnchorCache.numMembers = equivClass->members.size();
-
-  return anchor;
-}
-
 namespace {
   /// Function object used to suppress conflict diagnoses when we know we'll
   /// see them again later.
@@ -2667,32 +2714,6 @@
          SameTypeConflictCheckedLater());
 }
 
-PotentialArchetype *PotentialArchetype::getNestedArchetypeAnchor(
-                                           Identifier name,
-                                           GenericSignatureBuilder &builder,
-                                           ArchetypeResolutionKind kind) {
-  SmallVector<TypeDecl *, 4> concreteDecls;
-  auto bestType =
-    getOrCreateEquivalenceClass(builder)->lookupNestedType(builder, name,
-                                                           &concreteDecls);
-
-  // We didn't find any type with this name.
-  if (!bestType) return nullptr;
-
-  // Resolve the nested type.
-  auto resultPA = updateNestedTypeForConformance(builder, bestType, kind);
-
-  // Update for all of the concrete decls with this name, which will introduce
-  // various same-type constraints.
-  for (auto concreteDecl : concreteDecls) {
-    (void)updateNestedTypeForConformance(builder, concreteDecl,
-                                         ArchetypeResolutionKind::WellFormed);
-  }
-
-  return resultPA;
-}
-
-
 PotentialArchetype *PotentialArchetype::updateNestedTypeForConformance(
                                               GenericSignatureBuilder &builder,
                                               TypeDecl *type,
@@ -3091,6 +3112,20 @@
   });
 }
 
+PotentialArchetype *GenericSignatureBuilder::realizePotentialArchetype(
+                                                     UnresolvedType &type) {
+  if (auto pa = type.dyn_cast<PotentialArchetype *>())
+    return pa;
+
+  auto pa = maybeResolveEquivalenceClass(type.get<Type>(),
+                                         ArchetypeResolutionKind::WellFormed,
+                                         /*wantExactPotentialArchetype=*/true)
+    .getPotentialArchetypeIfKnown();
+  if (pa) type = pa;
+
+  return pa;
+}
+
 ResolvedType GenericSignatureBuilder::maybeResolveEquivalenceClass(
                                     Type type,
                                     ArchetypeResolutionKind resolutionKind,
@@ -3119,11 +3154,14 @@
     // Find the nested type declaration for this.
     auto baseEquivClass = resolvedBase.getEquivalenceClass(*this);
     TypeDecl *nestedTypeDecl;
+    SmallVector<TypeDecl *, 4> concreteDecls;
     if (auto assocType = depMemTy->getAssocType()) {
       nestedTypeDecl = assocType;
     } else {
       nestedTypeDecl =
-        baseEquivClass->lookupNestedType(*this, depMemTy->getName());
+        baseEquivClass->lookupNestedType(*this, depMemTy->getName(),
+                                         &concreteDecls);
+
       if (!nestedTypeDecl) {
         return ResolvedType::forUnresolved(baseEquivClass);
       }
@@ -3146,6 +3184,15 @@
     if (!nestedPA)
       return ResolvedType::forUnresolved(baseEquivClass);
 
+    if (resolutionKind != ArchetypeResolutionKind::AlreadyKnown) {
+      // Update for all of the concrete decls with this name, which will
+      // introduce various same-type constraints.
+      for (auto concreteDecl : concreteDecls) {
+        (void)basePA->updateNestedTypeForConformance(*this, concreteDecl,
+                                                     resolutionKind);
+      }
+    }
+
     // If base resolved to the anchor, then the nested potential archetype
     // we found is the resolved potential archetype. Return it directly,
     // so it doesn't need to be resolved again.
@@ -3497,7 +3544,9 @@
 
       bool shouldWarnAboutRedeclaration =
         source->kind == RequirementSource::RequirementSignatureSelf &&
-        assocTypeDecl->getDefaultDefinitionLoc().isNull();
+        assocTypeDecl->getDefaultDefinitionLoc().isNull() &&
+        (!assocTypeDecl->getInherited().empty() ||
+         assocTypeDecl->getTrailingWhereClause());
       for (auto inheritedType : knownInherited->second) {
         // If we have inherited associated type...
         if (auto inheritedAssocTypeDecl =
@@ -3784,8 +3833,8 @@
 }
 
 /// Map an unresolved type to a requirement right-hand-side.
-static GenericSignatureBuilder::RequirementRHS
-toRequirementRHS(GenericSignatureBuilder::UnresolvedType unresolved) {
+static GenericSignatureBuilder::UnresolvedRequirementRHS
+toUnresolvedRequirementRHS(GenericSignatureBuilder::UnresolvedType unresolved) {
   if (auto pa = unresolved.dyn_cast<PotentialArchetype *>())
     return pa;
 
@@ -3801,7 +3850,7 @@
   if (!resolvedConstraint) {
     return handleUnresolvedRequirement(
                              RequirementKind::Conformance, subject,
-                             toRequirementRHS(constraint), source,
+                             toUnresolvedRequirementRHS(constraint), source,
                              resolvedConstraint.getUnresolvedEquivClass(),
                              unresolvedHandling);
   }
@@ -4034,7 +4083,8 @@
   if (equivClass2 && equivClass2->superclass) {
     const RequirementSource *source2;
     if (auto existingSource2 =
-          equivClass2->findAnySuperclassConstraintAsWritten(OrigT2))
+          equivClass2->findAnySuperclassConstraintAsWritten(
+            OrigT2->getDependentType(getGenericParams())))
       source2 = existingSource2->source;
     else
       source2 = equivClass2->superclassConstraints.front().source;
@@ -4213,7 +4263,8 @@
   auto resolved1 = resolve(paOrT1, source);
   if (!resolved1) {
     return handleUnresolvedRequirement(RequirementKind::SameType, paOrT1,
-                                       toRequirementRHS(paOrT2), source,
+                                       toUnresolvedRequirementRHS(paOrT2),
+                                       source,
                                        resolved1.getUnresolvedEquivClass(),
                                        unresolvedHandling);
   }
@@ -4221,7 +4272,8 @@
   auto resolved2 = resolve(paOrT2, source);
   if (!resolved2) {
     return handleUnresolvedRequirement(RequirementKind::SameType, paOrT1,
-                                       toRequirementRHS(paOrT2), source,
+                                       toUnresolvedRequirementRHS(paOrT2),
+                                       source,
                                        resolved2.getUnresolvedEquivClass(),
                                        unresolvedHandling);
   }
@@ -4796,8 +4848,6 @@
   // Check for recursive or conflicting same-type bindings and superclass
   // constraints.
   for (auto &equivClass : Impl->EquivalenceClasses) {
-    auto archetype = equivClass.members.front()->getRepresentative();
-
     if (equivClass.concreteType) {
       // Check for recursive same-type bindings.
       if (isRecursiveConcreteType(&equivClass, /*isSuperclass=*/false)) {
@@ -4807,13 +4857,13 @@
 
           Diags.diagnose(constraint->source->getLoc(),
                          diag::recursive_same_type_constraint,
-                         archetype->getDependentType(genericParams),
+                         constraint->getSubjectDependentType(genericParams),
                          constraint->value);
         }
 
         equivClass.recursiveConcreteType = true;
       } else {
-        checkConcreteTypeConstraints(genericParams, archetype);
+        checkConcreteTypeConstraints(genericParams, &equivClass);
       }
     }
 
@@ -4831,12 +4881,12 @@
 
         equivClass.recursiveSuperclassType = true;
       } else {
-        checkSuperclassConstraints(genericParams, archetype);
+        checkSuperclassConstraints(genericParams, &equivClass);
       }
     }
 
-    checkConformanceConstraints(genericParams, archetype);
-    checkLayoutConstraints(genericParams, archetype);
+    checkConformanceConstraints(genericParams, &equivClass);
+    checkLayoutConstraints(genericParams, &equivClass);
   };
 
   // FIXME: Expand all conformance requirements. This is expensive :(
@@ -4845,10 +4895,9 @@
   }
 
   // Check same-type constraints.
-  for (const auto &equivClass : Impl->EquivalenceClasses) {
-    checkSameTypeConstraints(genericParams,
-                             equivClass.members[0]->getRepresentative());
-  };
+  for (auto &equivClass : Impl->EquivalenceClasses) {
+    checkSameTypeConstraints(genericParams, &equivClass);
+  }
 
   // Check for generic parameters which have been made concrete or equated
   // with each other.
@@ -4926,7 +4975,7 @@
 
 /// Turn a requirement right-hand side into an unresolved type.
 static GenericSignatureBuilder::UnresolvedType asUnresolvedType(
-                                GenericSignatureBuilder::RequirementRHS rhs) {
+                        GenericSignatureBuilder::UnresolvedRequirementRHS rhs) {
   if (auto pa = rhs.dyn_cast<PotentialArchetype *>())
     return GenericSignatureBuilder::UnresolvedType(pa);
 
@@ -5289,11 +5338,7 @@
 
 void GenericSignatureBuilder::checkConformanceConstraints(
                           ArrayRef<GenericTypeParamType *> genericParams,
-                          PotentialArchetype *pa) {
-  auto equivClass = pa->getEquivalenceClassIfPresent();
-  if (!equivClass || equivClass->conformsTo.empty())
-    return;
-
+                          EquivalenceClass *equivClass) {
   for (auto &entry : equivClass->conformsTo) {
     // Remove self-derived constraints.
     assert(!entry.second.empty() && "No constraints to work with?");
@@ -5341,7 +5386,8 @@
 namespace swift {
   bool operator<(const DerivedSameTypeComponent &lhs,
                  const DerivedSameTypeComponent &rhs) {
-    return compareDependentTypes(&lhs.anchor, &rhs.anchor) < 0;
+    return compareDependentTypes(getUnresolvedType(lhs.anchor, { }),
+                                 getUnresolvedType(rhs.anchor, { })) < 0;
   }
 } // namespace swift
 
@@ -5477,7 +5523,9 @@
     componentOf[depType] = componentIndex;
 
     // If this is a better anchor, record it.
-    if (compareDependentTypes(&pa, &components[componentIndex].anchor) < 0)
+    if (compareDependentTypes(
+                depType,
+                getUnresolvedType(components[componentIndex].anchor, { })) < 0)
       components[componentIndex].anchor = pa;
   }
 
@@ -5794,7 +5842,9 @@
       auto &newComponent = newComponents[newRepresentativeIndex];
 
       // If the old component has a better anchor, keep it.
-      if (compareDependentTypes(&oldComponent.anchor, &newComponent.anchor) < 0)
+      if (compareDependentTypes(
+                            getUnresolvedType(oldComponent.anchor, { }),
+                            getUnresolvedType(newComponent.anchor, { })) < 0)
         newComponent.anchor = oldComponent.anchor;
 
       // If the old component has a better concrete type source, keep it.
@@ -5816,9 +5866,8 @@
 
 void GenericSignatureBuilder::checkSameTypeConstraints(
                           ArrayRef<GenericTypeParamType *> genericParams,
-                          PotentialArchetype *pa) {
-  auto equivClass = pa->getEquivalenceClassIfPresent();
-  if (!equivClass || !equivClass->derivedSameTypeComponents.empty())
+                          EquivalenceClass *equivClass) {
+  if (!equivClass->derivedSameTypeComponents.empty())
     return;
 
   bool anyDerivedViaConcrete = false;
@@ -6020,10 +6069,7 @@
 
 void GenericSignatureBuilder::checkConcreteTypeConstraints(
                                  ArrayRef<GenericTypeParamType *> genericParams,
-                                 PotentialArchetype *representative) {
-  auto equivClass = representative->getOrCreateEquivalenceClass(*this);
-  assert(equivClass->concreteType && "No concrete type to check");
-
+                                 EquivalenceClass *equivClass) {
   checkConstraintList<Type>(
     genericParams, equivClass->concreteTypeConstraints,
     [&](const ConcreteConstraint &constraint) {
@@ -6057,8 +6103,7 @@
 
 void GenericSignatureBuilder::checkSuperclassConstraints(
                                  ArrayRef<GenericTypeParamType *> genericParams,
-                                 PotentialArchetype *representative) {
-  auto equivClass = representative->getOrCreateEquivalenceClass(*this);
+                                 EquivalenceClass *equivClass) {
   assert(equivClass->superclass && "No superclass constraint?");
 
   // FIXME: We should be substituting in the canonical type in context so
@@ -6146,9 +6191,8 @@
 
 void GenericSignatureBuilder::checkLayoutConstraints(
                                 ArrayRef<GenericTypeParamType *> genericParams,
-                                PotentialArchetype *pa) {
-  auto equivClass = pa->getEquivalenceClassIfPresent();
-  if (!equivClass || !equivClass->layout) return;
+                                EquivalenceClass *equivClass) {
+  if (!equivClass->layout) return;
 
   checkConstraintList<LayoutConstraint>(
     genericParams, equivClass->layoutConstraints,
@@ -6186,98 +6230,103 @@
 
     return bestSource;
   }
+
+  using SameTypeComponentRef = std::pair<EquivalenceClass *, unsigned>;
+
 } // end anonymous namespace
 
-void GenericSignatureBuilder::enumerateRequirements(llvm::function_ref<
+static int compareSameTypeComponents(const SameTypeComponentRef *lhsPtr,
+                                     const SameTypeComponentRef *rhsPtr){
+  Type lhsType = getUnresolvedType(
+      lhsPtr->first->derivedSameTypeComponents[lhsPtr->second].anchor,
+      { });
+  Type rhsType = getUnresolvedType(
+      rhsPtr->first->derivedSameTypeComponents[rhsPtr->second].anchor,
+      { });
+
+  return compareDependentTypes(lhsType, rhsType);
+}
+
+void GenericSignatureBuilder::enumerateRequirements(
+                   ArrayRef<GenericTypeParamType *> genericParams,
+                   llvm::function_ref<
                      void (RequirementKind kind,
-                           PotentialArchetype *archetype,
-                           GenericSignatureBuilder::RequirementRHS constraint,
+                           Type type,
+                           RequirementRHS constraint,
                            const RequirementSource *source)> f) {
-  // Collect all archetypes.
-  SmallVector<PotentialArchetype *, 8> archetypes;
+  // Collect all of the subject types that will be involved in constraints.
+  SmallVector<SameTypeComponentRef, 8> subjects;
   for (auto &equivClass : Impl->EquivalenceClasses) {
     if (equivClass.derivedSameTypeComponents.empty()) {
-      checkSameTypeConstraints(getGenericParams(),
-                               equivClass.members.front()->getRepresentative());
+      checkSameTypeConstraints(getGenericParams(), &equivClass);
     }
 
-    for (const auto &component : equivClass.derivedSameTypeComponents) {
-      archetypes.push_back(component.anchor);
-    }
+    for (unsigned i : indices(equivClass.derivedSameTypeComponents))
+      subjects.push_back({&equivClass, i});
   }
 
-  // Sort the archetypes in canonical order.
-  llvm::array_pod_sort(archetypes.begin(), archetypes.end(),
-                       compareDependentTypes);
+  // Sort the subject types in canonical order.
+  llvm::array_pod_sort(subjects.begin(), subjects.end(),
+                       compareSameTypeComponents);
 
-  auto genericParams = getGenericParams();
-  for (auto *archetype : archetypes) {
-    // Check whether this archetype is one of the anchors within its
-    // connected component. If so, we may need to emit a same-type constraint.
-    //
-    // FIXME: O(n) in the number of implied connected components within the
-    // equivalence class. The equivalence class should be small, but...
-    auto equivClass = archetype->getOrCreateEquivalenceClass(*this);
+  for (const auto &subject : subjects) {
+    // Dig out the subject type and its corresponding component.
+    auto equivClass = subject.first;
+    auto &component = equivClass->derivedSameTypeComponents[subject.second];
+    Type subjectType = getUnresolvedType(component.anchor, genericParams);
 
-    assert(!equivClass->derivedSameTypeComponents.empty() &&
-           "Didn't compute derived same-type components?");
-    auto knownAnchor =
-      std::find_if(equivClass->derivedSameTypeComponents.begin(),
-                   equivClass->derivedSameTypeComponents.end(),
-                   [&](const DerivedSameTypeComponent &component) {
-                     return component.anchor == archetype;
-                   });
+    // If this equivalence class is bound to a concrete type, equate the
+    // anchor with a concrete type.
+    if (Type concreteType = equivClass->concreteType) {
+      // If the parent of this anchor is also a concrete type, don't
+      // create a requirement.
+      if (!subjectType->is<GenericTypeParamType>() &&
+          maybeResolveEquivalenceClass(
+            subjectType->castTo<DependentMemberType>()->getBase(),
+            ArchetypeResolutionKind::WellFormed,
+            /*wantExactPotentialArchetype=*/false)
+            .getEquivalenceClass(*this)->concreteType)
+        continue;
+
+      auto source =
+        component.concreteTypeSource
+          ? component.concreteTypeSource
+          : RequirementSource::forAbstract(*this, subjectType);
+
+      // Drop recursive and invalid concrete-type constraints.
+      if (equivClass->recursiveConcreteType ||
+          equivClass->invalidConcreteType)
+        continue;
+
+      f(RequirementKind::SameType, subjectType, concreteType, source);
+      continue;
+    }
+
     std::function<void()> deferredSameTypeRequirement;
 
-    if (knownAnchor != equivClass->derivedSameTypeComponents.end()) {
-      // If this equivalence class is bound to a concrete type, equate the
-      // anchor with a concrete type.
-      if (Type concreteType = equivClass->concreteType) {
-        // If the parent of this anchor is also a concrete type, don't
-        // create a requirement.
-        if (!archetype->isGenericParam() &&
-            archetype->getParent()->isConcreteType())
-          continue;
-
-        auto source =
-          knownAnchor->concreteTypeSource
-            ? knownAnchor->concreteTypeSource
-            : RequirementSource::forAbstract(*this,
-                             archetype->getDependentType(getGenericParams()));
-
-        // Drop recursive and invalid concrete-type constraints.
-        if (equivClass->recursiveConcreteType ||
-            equivClass->invalidConcreteType)
-          continue;
-
-        f(RequirementKind::SameType, archetype, concreteType, source);
-        continue;
-      }
-
-      // If we're at the last anchor in the component, do nothing;
-      auto nextAnchor = knownAnchor;
-      ++nextAnchor;
-      if (nextAnchor != equivClass->derivedSameTypeComponents.end() /* &&
-          !equivClass->areAllRequirementsDerived()*/) {
-        // Form a same-type constraint from this anchor within the component
-        // to the next.
-        // FIXME: Distinguish between explicit and inferred here?
-        auto otherPA = nextAnchor->anchor;
-        deferredSameTypeRequirement =
-          [&f, archetype, otherPA, this, genericParams] {
-            f(RequirementKind::SameType, archetype, otherPA,
-              RequirementSource::forAbstract(
-                                     *this,
-                                     archetype->getDependentType(genericParams)));
-          };
-      }
+    // If we're at the last anchor in the component, do nothing;
+    if (subject.second + 1 != equivClass->derivedSameTypeComponents.size()) {
+      // Form a same-type constraint from this anchor within the component
+      // to the next.
+      // FIXME: Distinguish between explicit and inferred here?
+      auto &nextComponent =
+        equivClass->derivedSameTypeComponents[subject.second + 1];
+      Type otherSubjectType =
+        getUnresolvedType(nextComponent.anchor, genericParams);
+      deferredSameTypeRequirement =
+        [&f, subjectType, otherSubjectType, this, genericParams] {
+          f(RequirementKind::SameType, subjectType, otherSubjectType,
+            RequirementSource::forAbstract(*this, otherSubjectType));
+        };
     }
+
     SWIFT_DEFER {
       if (deferredSameTypeRequirement) deferredSameTypeRequirement();
     };
 
-    // If this is not the archetype anchor, we're done.
-    if (archetype != archetype->getArchetypeAnchor(*this))
+    // If this is not the first component anchor in its equivalence class,
+    // we're done.
+    if (subject.second > 0)
       continue;
 
     // If we have a superclass, produce a superclass requirement
@@ -6289,12 +6338,9 @@
           });
 
       if (!bestSource)
-        bestSource =
-          RequirementSource::forAbstract(
-                                 *this,
-                                 archetype->getDependentType(genericParams));
+        bestSource = RequirementSource::forAbstract(*this, subjectType);
 
-      f(RequirementKind::Superclass, archetype, equivClass->superclass,
+      f(RequirementKind::Superclass, subjectType, equivClass->superclass,
         *bestSource);
     }
 
@@ -6306,12 +6352,9 @@
                             return layout == equivClass->layout;
                           });
       if (!bestSource)
-        bestSource =
-          RequirementSource::forAbstract(
-                                   *this,
-                                   archetype->getDependentType(genericParams));
+        bestSource = RequirementSource::forAbstract(*this, subjectType);
 
-      f(RequirementKind::Layout, archetype, equivClass->layout, *bestSource);
+      f(RequirementKind::Layout, subjectType, equivClass->layout, *bestSource);
     }
 
     // Enumerate conformance requirements.
@@ -6339,7 +6382,7 @@
     // Enumerate the conformance requirements.
     for (auto proto : protocols) {
       assert(protocolSources.count(proto) == 1 && "Missing conformance?");
-      f(RequirementKind::Conformance, archetype, 
+      f(RequirementKind::Conformance, subjectType,
         proto->getDeclaredInterfaceType(),
         protocolSources.find(proto)->second);
     }
@@ -6352,29 +6395,30 @@
 
 void GenericSignatureBuilder::dump(llvm::raw_ostream &out) {
   out << "Requirements:";
-  enumerateRequirements([&](RequirementKind kind,
-                            PotentialArchetype *archetype,
-                            GenericSignatureBuilder::RequirementRHS constraint,
+  enumerateRequirements(getGenericParams(),
+                        [&](RequirementKind kind,
+                            Type type,
+                            RequirementRHS constraint,
                             const RequirementSource *source) {
     switch (kind) {
     case RequirementKind::Conformance:
     case RequirementKind::Superclass:
       out << "\n  ";
-      out << archetype->getDebugName() << " : " 
+      out << type.getString() << " : "
           << constraint.get<Type>().getString() << " [";
       source->print(out, &Context.SourceMgr);
       out << "]";
       break;
     case RequirementKind::Layout:
       out << "\n  ";
-      out << archetype->getDebugName() << " : "
+      out << type.getString() << " : "
           << constraint.get<LayoutConstraint>().getString() << " [";
       source->print(out, &Context.SourceMgr);
       out << "]";
       break;
     case RequirementKind::SameType:
       out << "\n  ";
-      out << archetype->getDebugName() << " == " ;
+      out << type.getString() << " == " ;
       if (auto secondType = constraint.dyn_cast<Type>()) {
         out << secondType.getString();
       } else {
@@ -6410,9 +6454,11 @@
 static void collectRequirements(GenericSignatureBuilder &builder,
                                 ArrayRef<GenericTypeParamType *> params,
                                 SmallVectorImpl<Requirement> &requirements) {
-  builder.enumerateRequirements([&](RequirementKind kind,
-          GenericSignatureBuilder::PotentialArchetype *archetype,
-          GenericSignatureBuilder::RequirementRHS type,
+  builder.enumerateRequirements(
+      params,
+      [&](RequirementKind kind,
+          Type depTy,
+          RequirementRHS type,
           const RequirementSource *source) {
     // Filter out derived requirements... except for concrete-type requirements
     // on generic parameters. The exception is due to the canonicalization of
@@ -6420,31 +6466,26 @@
     // they have been mapped to a concrete type.
     if (source->isDerivedRequirement() &&
         !(kind == RequirementKind::SameType &&
-          archetype->isGenericParam() &&
+          depTy->is<GenericTypeParamType>() &&
           type.is<Type>()))
       return;
 
-    auto depTy = archetype->getDependentType(params);
-
     if (depTy->hasError())
       return;
 
     Type repTy;
     if (auto concreteTy = type.dyn_cast<Type>()) {
-      // Maybe we were equated to a concrete type...
+      // Maybe we were equated to a concrete or dependent type...
       repTy = concreteTy;
 
       // Drop requirements involving concrete types containing
       // unresolved associated types.
       if (repTy->findUnresolvedDependentMemberType())
         return;
-    } else if (auto layoutConstraint = type.dyn_cast<LayoutConstraint>()) {
+    } else {
+      auto layoutConstraint = type.get<LayoutConstraint>();
       requirements.push_back(Requirement(kind, depTy, layoutConstraint));
       return;
-    } else {
-      // ...or to a dependent type.
-      repTy = type.get<GenericSignatureBuilder::PotentialArchetype *>()
-          ->getDependentType(params);
     }
 
     if (repTy->hasError())
diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp
index a72b63a..1844f1f 100644
--- a/lib/Driver/Driver.cpp
+++ b/lib/Driver/Driver.cpp
@@ -1453,6 +1453,7 @@
       case types::TY_ImportedModules:
       case types::TY_TBD:
       case types::TY_ModuleTrace:
+      case types::TY_OptRecord:
         // We could in theory handle assembly or LLVM input, but let's not.
         // FIXME: What about LTO?
         Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
@@ -2236,6 +2237,19 @@
     }
   }
 
+  if (C.getArgs().hasArg(options::OPT_save_optimization_record,
+                         options::OPT_save_optimization_record_path)) {
+    if (OI.CompilerMode == OutputInfo::Mode::SingleCompile) {
+      auto filename = *getOutputFilenameFromPathArgOrAsTopLevel(
+          OI, C.getArgs(), options::OPT_save_optimization_record_path,
+          types::TY_OptRecord, /*TreatAsTopLevelOutput=*/true, "opt.yaml", Buf);
+
+      Output->setAdditionalOutputForType(types::TY_OptRecord, filename);
+    } else
+      // FIXME: We should use the OutputMap in this case.
+      Diags.diagnose({}, diag::warn_opt_remark_disabled);
+  }
+
   // Choose the Objective-C header output path.
   if ((isa<MergeModuleJobAction>(JA) ||
        (isa<CompileJobAction>(JA) &&
diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp
index bca1dfc..f5c638e 100644
--- a/lib/Driver/ToolChains.cpp
+++ b/lib/Driver/ToolChains.cpp
@@ -272,6 +272,7 @@
     case types::TY_SwiftDeps:
     case types::TY_ModuleTrace:
     case types::TY_TBD:
+    case types::TY_OptRecord:
       llvm_unreachable("Output type can never be primary output.");
     case types::TY_INVALID:
       llvm_unreachable("Invalid type ID");
@@ -448,6 +449,13 @@
     Arguments.push_back(TBDPath.c_str());
   }
 
+  const std::string &OptRecordPath =
+      context.Output.getAdditionalOutputForType(types::TY_OptRecord);
+  if (!OptRecordPath.empty()) {
+    Arguments.push_back("-save-optimization-record-path");
+    Arguments.push_back(OptRecordPath.c_str());
+  }
+
   if (context.Args.hasArg(options::OPT_migrate_keep_objc_visibility)) {
     Arguments.push_back("-migrate-keep-objc-visibility");
   }
@@ -586,6 +594,7 @@
     case types::TY_SwiftDeps:
     case types::TY_Remapping:
     case types::TY_ModuleTrace:
+    case types::TY_OptRecord:
       llvm_unreachable("Output type can never be primary output.");
     case types::TY_INVALID:
       llvm_unreachable("Invalid type ID");
diff --git a/lib/Driver/Types.cpp b/lib/Driver/Types.cpp
index d0b60bc..d69378e 100644
--- a/lib/Driver/Types.cpp
+++ b/lib/Driver/Types.cpp
@@ -77,6 +77,7 @@
   case types::TY_ImportedModules:
   case types::TY_TBD:
   case types::TY_ModuleTrace:
+  case types::TY_OptRecord:
     return true;
   case types::TY_Image:
   case types::TY_Object:
@@ -131,6 +132,7 @@
   case types::TY_Remapping:
   case types::TY_IndexData:
   case types::TY_ModuleTrace:
+  case types::TY_OptRecord:
     return false;
   case types::TY_INVALID:
     llvm_unreachable("Invalid type ID.");
@@ -169,6 +171,7 @@
   case types::TY_Remapping:
   case types::TY_IndexData:
   case types::TY_ModuleTrace:
+  case types::TY_OptRecord:
     return false;
   case types::TY_INVALID:
     llvm_unreachable("Invalid type ID.");
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 46cdc03..8d88e15 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -1319,6 +1319,9 @@
       !Args.hasArg(OPT_disable_mandatory_semantic_arc_opts);
   Opts.EnableLargeLoadableTypes |= Args.hasArg(OPT_enable_large_loadable_types);
 
+  if (const Arg *A = Args.getLastArg(OPT_save_optimization_record_path))
+    Opts.OptRecordFile = A->getValue();
+
   if (Args.hasArg(OPT_debug_on_sil)) {
     // Derive the name of the SIL file for debugging from
     // the regular outputfile.
diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp
index 644294f..fba8441 100644
--- a/lib/FrontendTool/FrontendTool.cpp
+++ b/lib/FrontendTool/FrontendTool.cpp
@@ -75,6 +75,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/Timer.h"
+#include "llvm/Support/YAMLTraits.h"
 #include "llvm/Target/TargetMachine.h"
 
 #include <memory>
@@ -507,6 +508,21 @@
   C.NumSILOptGlobalVariables = Module.getSILGlobalList().size();
 }
 
+static std::unique_ptr<llvm::raw_fd_ostream>
+createOptRecordFile(StringRef Filename, DiagnosticEngine &DE) {
+  if (Filename.empty())
+    return nullptr;
+
+  std::error_code EC;
+  auto File = llvm::make_unique<llvm::raw_fd_ostream>(Filename, EC,
+                                                      llvm::sys::fs::F_None);
+  if (EC) {
+    DE.diagnose(SourceLoc(), diag::cannot_open_file, Filename, EC.message());
+    return nullptr;
+  }
+  return File;
+}
+
 /// Performs the compile requested by the user.
 /// \param Instance Will be reset after performIRGeneration when the verifier
 ///                 mode is NoVerify and there were no errors.
@@ -748,9 +764,9 @@
     return Context.hadError();
   }
 
+  const auto &SILOpts = Invocation.getSILOptions();
   if (!opts.TBDPath.empty()) {
-    const auto &silOpts = Invocation.getSILOptions();
-    auto hasMultipleIRGenThreads = silOpts.NumThreads > 1;
+    auto hasMultipleIRGenThreads = SILOpts.NumThreads > 1;
     auto installName = opts.TBDInstallName.empty()
                            ? "lib" + Invocation.getModuleName().str() + ".dylib"
                            : opts.TBDInstallName;
@@ -825,6 +841,13 @@
     return Context.hadError();
   }
 
+  std::unique_ptr<llvm::raw_fd_ostream> OptRecordFile =
+      createOptRecordFile(SILOpts.OptRecordFile, Instance.getDiags());
+  if (OptRecordFile)
+    SM->setOptRecordStream(llvm::make_unique<llvm::yaml::Output>(
+                               *OptRecordFile, &Instance.getSourceMgr()),
+                           std::move(OptRecordFile));
+
   // Perform "stable" optimizations that are invariant across compiler versions.
   if (Action == FrontendOptions::MergeModules) {
     // Don't run diagnostic passes at all.
@@ -1059,8 +1082,8 @@
         !astGuaranteedToCorrespondToSIL)
       break;
 
-    const auto &silOpts = Invocation.getSILOptions();
-    auto hasMultipleIRGenThreads = silOpts.NumThreads > 1;
+    const auto &SILOpts = Invocation.getSILOptions();
+    auto hasMultipleIRGenThreads = SILOpts.NumThreads > 1;
     bool error;
     if (PrimarySourceFile)
       error = validateTBD(PrimarySourceFile, *IRModule, hasMultipleIRGenThreads,
diff --git a/lib/IRGen/GenBuiltin.cpp b/lib/IRGen/GenBuiltin.cpp
index d8ea216..d34a0a1 100644
--- a/lib/IRGen/GenBuiltin.cpp
+++ b/lib/IRGen/GenBuiltin.cpp
@@ -271,14 +271,14 @@
     return out.add(v); \
   }
 
-#define BUILTIN_RUNTIME_CALL(id, name, attrs) \
-  if (Builtin.ID == BuiltinValueKind::id) { \
-    llvm::CallInst *call = IGF.Builder.CreateCall(IGF.IGM.get##id##Fn(),  \
-                           args.claimNext()); \
-    call->setCallingConv(IGF.IGM.DefaultCC); \
-    call->setDoesNotThrow(); \
-    return out.add(call); \
- }
+#define BUILTIN_RUNTIME_CALL(id, name, attrs)                                  \
+  if (Builtin.ID == BuiltinValueKind::id) {                                    \
+    auto *fn = cast<llvm::Function>(IGF.IGM.get##id##Fn());                    \
+    llvm::CallInst *call = IGF.Builder.CreateCall(fn, args.claimNext());       \
+    call->setCallingConv(fn->getCallingConv());                                \
+    call->setAttributes(fn->getAttributes());                                  \
+    return out.add(call);                                                      \
+  }
 
 #define BUILTIN_BINARY_OPERATION_WITH_OVERFLOW(id, name, uncheckedID, attrs, overload) \
 if (Builtin.ID == BuiltinValueKind::id) { \
diff --git a/lib/IRGen/GenCall.cpp b/lib/IRGen/GenCall.cpp
index 7072d75..e12ce75 100644
--- a/lib/IRGen/GenCall.cpp
+++ b/lib/IRGen/GenCall.cpp
@@ -938,13 +938,16 @@
         break;
       }
 
-      // If the coercion type is a struct, we need to expand it.
-      auto type = AI.getCoerceToType();
-      if (auto expandedType = dyn_cast<llvm::StructType>(type)) {
-        for (size_t j = 0, e = expandedType->getNumElements(); j != e; ++j)
-          ParamIRTypes.push_back(expandedType->getElementType(j));
+      // If the coercion type is a struct which can be flattened, we need to
+      // expand it.
+      auto *coercedTy = AI.getCoerceToType();
+      if (AI.isDirect() && AI.getCanBeFlattened() &&
+          isa<llvm::StructType>(coercedTy)) {
+        const auto *ST = cast<llvm::StructType>(coercedTy);
+        for (unsigned EI : range(ST->getNumElements()))
+          ParamIRTypes.push_back(ST->getElementType(EI));
       } else {
-        ParamIRTypes.push_back(type);
+        ParamIRTypes.push_back(coercedTy);
       }
       break;
     }
@@ -1685,18 +1688,24 @@
 }
 
 static void emitDirectExternalArgument(IRGenFunction &IGF, SILType argType,
-                                       llvm::Type *toTy, Explosion &in,
-                                       Explosion &out) {
+                                       const clang::CodeGen::ABIArgInfo &AI,
+                                       Explosion &in, Explosion &out) {
+  bool IsDirectFlattened = AI.isDirect() && AI.getCanBeFlattened();
+  bool IsIndirect = !AI.isDirect();
+
   // If we're supposed to pass directly as a struct type, that
   // really means expanding out as multiple arguments.
-  ArrayRef<llvm::Type *> expandedTys = expandScalarOrStructTypeToArray(toTy);
+  llvm::Type *coercedTy = AI.getCoerceToType();
+  ArrayRef<llvm::Type *> expandedTys =
+      expandScalarOrStructTypeToArray(coercedTy);
 
   auto &argTI = cast<LoadableTypeInfo>(IGF.getTypeInfo(argType));
   auto inputSchema = argTI.getSchema();
 
   // Check to see if we can pairwise coerce Swift's exploded scalars
   // to Clang's expanded elements.
-  if (canCoerceToSchema(IGF.IGM, expandedTys, inputSchema)) {
+  if ((IsDirectFlattened || IsIndirect) &&
+      canCoerceToSchema(IGF.IGM, expandedTys, inputSchema)) {
     for (auto outputTy : expandedTys) {
       llvm::Value *arg = in.claimNext();
       if (arg->getType() != outputTy)
@@ -1710,7 +1719,7 @@
   Address temporary;
   Size tempSize;
   std::tie(temporary, tempSize) =
-      allocateForCoercion(IGF, argTI.getStorageType(), toTy, "coerced-arg");
+      allocateForCoercion(IGF, argTI.getStorageType(), coercedTy, "coerced-arg");
   IGF.Builder.CreateLifetimeStart(temporary, tempSize);
 
   // Store to a temporary.
@@ -1720,19 +1729,19 @@
 
   // Bitcast the temporary to the expected type.
   Address coercedAddr =
-    IGF.Builder.CreateBitCast(temporary, toTy->getPointerTo());
+      IGF.Builder.CreateBitCast(temporary, coercedTy->getPointerTo());
 
-  // Project out individual elements if necessary.
-  if (auto expansionTy = dyn_cast<llvm::StructType>(toTy)) {
-    auto layout = IGF.IGM.DataLayout.getStructLayout(expansionTy);
-    for (unsigned i = 0, e = expansionTy->getNumElements(); i != e; ++i) {
-      auto fieldOffset = Size(layout->getElementOffset(i));
-      auto fieldAddr = IGF.Builder.CreateStructGEP(coercedAddr, i, fieldOffset);
-      out.add(IGF.Builder.CreateLoad(fieldAddr));
+  if (IsDirectFlattened && isa<llvm::StructType>(coercedTy)) {
+    // Project out individual elements if necessary.
+    auto *ST = cast<llvm::StructType>(coercedTy);
+    const auto *layout = IGF.IGM.DataLayout.getStructLayout(ST);
+    for (unsigned EI : range(ST->getNumElements())) {
+      auto offset = Size(layout->getElementOffset(EI));
+      auto address = IGF.Builder.CreateStructGEP(coercedAddr, EI, offset);
+      out.add(IGF.Builder.CreateLoad(address));
     }
-
-  // Otherwise, collect the single scalar.
   } else {
+    // Otherwise, collect the single scalar.
     out.add(IGF.Builder.CreateLoad(coercedAddr));
   }
 
@@ -1880,7 +1889,7 @@
         break;
       }
 
-      emitDirectExternalArgument(IGF, paramType, toTy, in, out);
+      emitDirectExternalArgument(IGF, paramType, AI, in, out);
       break;
     }
     case clang::CodeGen::ABIArgInfo::Indirect: {
@@ -1960,24 +1969,24 @@
 }
 
 /// Emit a direct parameter that was passed under a C-based CC.
-static void emitDirectForeignParameter(IRGenFunction &IGF,
-                                       Explosion &in,
-                                       llvm::Type *coercionTy,
-                                       Explosion &out,
-                                       SILType paramType,
+static void emitDirectForeignParameter(IRGenFunction &IGF, Explosion &in,
+                                       const clang::CodeGen::ABIArgInfo &AI,
+                                       Explosion &out, SILType paramType,
                                        const LoadableTypeInfo &paramTI) {
   // The ABI IR types for the entrypoint might differ from the
   // Swift IR types for the body of the function.
 
-  ArrayRef<llvm::Type*> expandedTys;
-  if (auto expansionTy = dyn_cast<llvm::StructType>(coercionTy)) {
-    expandedTys = makeArrayRef(expansionTy->element_begin(),
-                               expansionTy->getNumElements());
+  llvm::Type *coercionTy = AI.getCoerceToType();
 
-  // Fast-path a really common case.  This check assumes that either
-  // the storage type of a type is an llvm::StructType or it has a
-  // single-element explosion.
+  ArrayRef<llvm::Type*> expandedTys;
+  if (AI.isDirect() && AI.getCanBeFlattened() &&
+      isa<llvm::StructType>(coercionTy)) {
+    const auto *ST = cast<llvm::StructType>(coercionTy);
+    expandedTys = makeArrayRef(ST->element_begin(), ST->getNumElements());
   } else if (coercionTy == paramTI.getStorageType()) {
+    // Fast-path a really common case.  This check assumes that either
+    // the storage type of a type is an llvm::StructType or it has a
+    // single-element explosion.
     out.add(in.claimNext());
     return;
   } else {
@@ -2060,11 +2069,10 @@
 
   switch (AI.getKind()) {
   case clang::CodeGen::ABIArgInfo::Extend:
-  case clang::CodeGen::ABIArgInfo::Direct: {
-    emitDirectForeignParameter(IGF, params, AI.getCoerceToType(),
-                               paramExplosion, paramTy, paramTI);
+  case clang::CodeGen::ABIArgInfo::Direct:
+    emitDirectForeignParameter(IGF, params, AI, paramExplosion, paramTy,
+                               paramTI);
     return;
-  }
   case clang::CodeGen::ABIArgInfo::Indirect: {
     Address address = paramTI.getAddressForPointer(params.claimNext());
     paramTI.loadAsTake(IGF, address, paramExplosion);
diff --git a/lib/SIL/OptimizationRemark.cpp b/lib/SIL/OptimizationRemark.cpp
index 7147554..87f8b26 100644
--- a/lib/SIL/OptimizationRemark.cpp
+++ b/lib/SIL/OptimizationRemark.cpp
@@ -17,11 +17,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "swift/SIL/OptimizationRemark.h"
-#include "swift/AST/ASTContext.h"
 #include "swift/AST/DiagnosticEngine.h"
 #include "swift/AST/DiagnosticsSIL.h"
-#include "swift/SIL/SILFunction.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/YAMLTraits.h"
 #include "llvm/Support/raw_ostream.h"
 
 using namespace swift;
@@ -57,25 +56,93 @@
   return OS.str();
 }
 
-Emitter::Emitter(StringRef PassName, ASTContext &Ctx)
-    : Ctx(Ctx), PassName(PassName),
+Emitter::Emitter(StringRef PassName, SILModule &M)
+    : Module(M), PassName(PassName),
       PassedEnabled(
-          Ctx.LangOpts.OptimizationRemarkPassedPattern &&
-          Ctx.LangOpts.OptimizationRemarkPassedPattern->match(PassName)),
+          M.getASTContext().LangOpts.OptimizationRemarkPassedPattern &&
+          M.getASTContext().LangOpts.OptimizationRemarkPassedPattern->match(
+              PassName)),
       MissedEnabled(
-          Ctx.LangOpts.OptimizationRemarkMissedPattern &&
-          Ctx.LangOpts.OptimizationRemarkMissedPattern->match(PassName)) {}
+          M.getASTContext().LangOpts.OptimizationRemarkMissedPattern &&
+          M.getASTContext().LangOpts.OptimizationRemarkMissedPattern->match(
+              PassName)) {}
 
 template <typename RemarkT, typename... ArgTypes>
-static void emitRemark(ASTContext &Ctx, const RemarkT &R,
-                       Diag<ArgTypes...> ID) {
-  Ctx.Diags.diagnose(R.getLocation(), ID, R.getMsg());
+static void emitRemark(SILModule &Module, const Remark<RemarkT> &R,
+                       Diag<ArgTypes...> ID, bool DiagEnabled) {
+  if (auto *Out = Module.getOptRecordStream())
+    // YAMLTraits takes a non-const reference even when outputting.
+    *Out << const_cast<Remark<RemarkT> &>(R);
+  if (DiagEnabled)
+    Module.getASTContext().Diags.diagnose(R.getLocation(), ID, R.getMsg());
 }
 
 void Emitter::emit(const RemarkPassed &R) {
-  emitRemark(Ctx, R, diag::opt_remark_passed);
+  emitRemark(Module, R, diag::opt_remark_passed, isEnabled<RemarkPassed>());
 }
 
 void Emitter::emit(const RemarkMissed &R) {
-  emitRemark(Ctx, R, diag::opt_remark_missed);
+  emitRemark(Module, R, diag::opt_remark_missed, isEnabled<RemarkMissed>());
 }
+
+namespace llvm {
+namespace yaml {
+
+template <typename KindT> struct MappingTraits<Remark<KindT>> {
+  static void mapping(llvm::yaml::IO &io, Remark<KindT> &R) {
+    assert(io.outputting() && "input not implemented");
+
+    if (io.mapTag("!Passed", std::is_same<KindT, RemarkPassed>::value))
+      ;
+    else if (io.mapTag("!Missed", std::is_same<KindT, RemarkMissed>::value))
+      ;
+    else
+      llvm_unreachable("Unknown remark type");
+
+    // The attributes are read-only for now since we're only support outputting
+    // them.
+    StringRef PassName = R.getPassName();
+    io.mapRequired("Pass", PassName);
+    StringRef Id = R.getIdentifier();
+    io.mapRequired("Name", Id);
+
+    SourceLoc Loc = R.getLocation();
+    if (!io.outputting() || Loc.isValid())
+      io.mapOptional("DebugLoc", Loc);
+
+    StringRef FN = R.getFunction()->getName();
+    io.mapRequired("Function", FN);
+    io.mapOptional("Args", R.getArgs());
+  }
+};
+
+template <> struct MappingTraits<SourceLoc> {
+  static void mapping(IO &io, SourceLoc &Loc) {
+    assert(io.outputting() && "input not yet implemented");
+
+    SourceManager *SM = static_cast<SourceManager *>(io.getContext());
+    unsigned BufferID = SM->findBufferContainingLoc(Loc);
+    StringRef File = SM->getIdentifierForBuffer(BufferID);
+    unsigned Line, Col;
+    std::tie(Line, Col) = SM->getLineAndColumn(Loc, BufferID);
+
+    io.mapRequired("File", File);
+    io.mapRequired("Line", Line);
+    io.mapRequired("Column", Col);
+  }
+};
+
+// Implement this as a mapping for now to get proper quotation for the value.
+template <> struct MappingTraits<OptRemark::Argument> {
+  static void mapping(IO &io, OptRemark::Argument &A) {
+    assert(io.outputting() && "input not yet implemented");
+    io.mapRequired(A.Key.data(), A.Val);
+    if (A.Loc.isValid())
+      io.mapOptional("DebugLoc", A.Loc);
+  }
+};
+
+} // end namespace yaml
+} // end namespace llvm
+
+LLVM_YAML_IS_SEQUENCE_VECTOR(OptRemark::Argument)
diff --git a/lib/SIL/SILFunctionType.cpp b/lib/SIL/SILFunctionType.cpp
index eec64f7..3acbc1c 100644
--- a/lib/SIL/SILFunctionType.cpp
+++ b/lib/SIL/SILFunctionType.cpp
@@ -1924,7 +1924,7 @@
 const SILConstantInfo &TypeConverter::getConstantInfo(SILDeclRef constant) {
   auto found = ConstantTypes.find(constant);
   if (found != ConstantTypes.end())
-    return found->second;
+    return *found->second;
 
   // First, get a function type for the constant.  This creates the
   // right type for a getter or setter.
@@ -1956,14 +1956,17 @@
         silFnType.print(llvm::dbgs());
         llvm::dbgs() << "\n");
 
-  auto result = ConstantTypes.try_emplace(constant,
-                                          formalInterfaceType,
-                                          bridgedTypes.Pattern,
-                                          loweredInterfaceType,
-                                          silFnType,
-                                          genericEnv);
-  assert(result.second);
-  return result.first->second;
+  auto resultBuf = M.allocate(sizeof(SILConstantInfo),
+                              alignof(SILConstantInfo));
+
+  auto result = ::new (resultBuf) SILConstantInfo{formalInterfaceType,
+                                                  bridgedTypes.Pattern,
+                                                  loweredInterfaceType,
+                                                  silFnType,
+                                                  genericEnv};
+  auto inserted = ConstantTypes.insert({constant, result});
+  assert(inserted.second);
+  return *result;
 }
 
 /// Returns the SILParameterInfo for the given declaration's `self` parameter.
@@ -2092,7 +2095,7 @@
 
   auto found = ConstantOverrideTypes.find({derived, base});
   if (found != ConstantOverrideTypes.end())
-    return found->second;
+    return *found->second;
 
   assert(requiresNewVTableEntry(base) && "base must not be an override");
 
@@ -2146,14 +2149,18 @@
                                                derived);
 
   // Build the SILConstantInfo and cache it.
-  auto result = ConstantOverrideTypes.try_emplace({derived, base},
+  auto resultBuf = M.allocate(sizeof(SILConstantInfo),
+                              alignof(SILConstantInfo));
+  auto result = ::new (resultBuf) SILConstantInfo{
     derivedInterfaceTy,
     bridgedTypes.Pattern,
     overrideLoweredInterfaceTy,
     fnTy,
-    derivedInfo.GenericEnv);
-  assert(result.second);
-  return result.first->second;
+    derivedInfo.GenericEnv};
+  
+  auto inserted = ConstantOverrideTypes.insert({{derived, base}, result});
+  assert(inserted.second);
+  return *result;
 }
 
 namespace {
diff --git a/lib/SIL/SILModule.cpp b/lib/SIL/SILModule.cpp
index cbef5b8..56ea768 100644
--- a/lib/SIL/SILModule.cpp
+++ b/lib/SIL/SILModule.cpp
@@ -27,6 +27,7 @@
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/YAMLTraits.h"
 #include <functional>
 using namespace swift;
 using namespace Lowering;
@@ -781,3 +782,9 @@
   setSerialized();
 }
 
+void SILModule::setOptRecordStream(
+    std::unique_ptr<llvm::yaml::Output> &&Stream,
+    std::unique_ptr<llvm::raw_ostream> &&RawStream) {
+  OptRecordStream = std::move(Stream);
+  OptRecordRawStream = std::move(RawStream);
+}
diff --git a/lib/SIL/SILWitnessTable.cpp b/lib/SIL/SILWitnessTable.cpp
index 7374be5..4e0728e 100644
--- a/lib/SIL/SILWitnessTable.cpp
+++ b/lib/SIL/SILWitnessTable.cpp
@@ -158,8 +158,7 @@
   return Mod.getASTContext().getIdentifier(Name);
 }
 
-bool SILWitnessTable::conformanceIsSerialized(ProtocolConformance *conformance,
-                                              ResilienceStrategy strategy) {
+bool SILWitnessTable::conformanceIsSerialized(ProtocolConformance *conformance) {
   // Serialize witness tables for conformances synthesized by
   // the ClangImporter.
   if (isa<ClangModuleUnit>(conformance->getDeclContext()->getModuleScopeContext()))
diff --git a/lib/SILGen/ArgumentSource.cpp b/lib/SILGen/ArgumentSource.cpp
index 1ed7c72..5661f17 100644
--- a/lib/SILGen/ArgumentSource.cpp
+++ b/lib/SILGen/ArgumentSource.cpp
@@ -59,8 +59,26 @@
     Storage.get<RValueStorage>(StoredKind).Value.rewriteType(newType);
     return;
   case Kind::Expr:
-    Expr *expr = Storage.get<Expr*>(StoredKind);
-    if (expr->getType()->isEqual(newType)) return;
+    Expr *&expr = Storage.get<Expr*>(StoredKind);
+    CanType oldType = expr->getType()->getCanonicalType();
+
+    // Usually nothing is required.
+    if (oldType == newType) return;
+
+    // Sometimes we need to wrap the expression in a single-element tuple.
+    // This is only necessary because we don't break down the argument list
+    // when dealing with SILGenApply.
+    if (auto newTuple = dyn_cast<TupleType>(newType)) {
+      if (newTuple->getNumElements() == 1 &&
+          newTuple.getElementType(0) == oldType) {
+        expr = TupleExpr::create(newType->getASTContext(),
+                                 SourceLoc(), expr, {}, {}, SourceLoc(),
+                                 /*trailing closure*/ false,
+                                 /*implicit*/ true, newType);
+        return;
+      }
+    }
+
     llvm_unreachable("unimplemented! hope it doesn't happen");
   }
   llvm_unreachable("bad kind");
diff --git a/lib/SILGen/SILGenType.cpp b/lib/SILGen/SILGenType.cpp
index c148189..ce8f9b6 100644
--- a/lib/SILGen/SILGenType.cpp
+++ b/lib/SILGen/SILGenType.cpp
@@ -381,8 +381,7 @@
     Serialized = IsNotSerialized;
 
     // ... or if the conformance itself thinks it should be.
-    if (SILWitnessTable::conformanceIsSerialized(
-            Conformance, SGM.M.getSwiftModule()->getResilienceStrategy()))
+    if (SILWitnessTable::conformanceIsSerialized(Conformance))
       Serialized = IsSerialized;
 
     // Not all protocols use witness tables; in this case we just skip
diff --git a/lib/SILOptimizer/IPO/GlobalOpt.cpp b/lib/SILOptimizer/IPO/GlobalOpt.cpp
index 423922d..6f39a33 100644
--- a/lib/SILOptimizer/IPO/GlobalOpt.cpp
+++ b/lib/SILOptimizer/IPO/GlobalOpt.cpp
@@ -108,7 +108,9 @@
   bool handleTailAddr(int TailIdx, SILInstruction *I,
                       llvm::SmallVectorImpl<StoreInst *> &TailStores);
 
-  void optimizeObjectAllocation(AllocRefInst *ARI);
+  void
+  optimizeObjectAllocation(AllocRefInst *ARI,
+                           llvm::SmallVector<SILInstruction *, 4> &ToRemove);
   void replaceFindStringCall(ApplyInst *FindStringCall);
 
   SILGlobalVariable *getVariableOfGlobalInit(SILFunction *AddrF);
@@ -1218,7 +1220,8 @@
 ///     func getarray() -> [Int] {
 ///       return [1, 2, 3]
 ///     }
-void SILGlobalOpt::optimizeObjectAllocation(AllocRefInst *ARI) {
+void SILGlobalOpt::optimizeObjectAllocation(
+    AllocRefInst *ARI, llvm::SmallVector<SILInstruction *, 4> &ToRemove) {
 
   if (ARI->isObjC())
     return;
@@ -1270,6 +1273,10 @@
   DEBUG(llvm::dbgs() << "Outline global variable in " <<
         ARI->getFunction()->getName() << '\n');
 
+  assert(Cl->hasFixedLayout(Module->getSwiftModule(),
+                            ResilienceExpansion::Minimal) &&
+    "constructor call of resilient class should prevent static allocation");
+
   // Create a name for the outlined global variable.
   GlobalVariableMangler Mangler;
   std::string GlobName =
@@ -1296,14 +1303,14 @@
     assert(MemberStore);
     ObjectArgs.push_back(Cloner.clone(
                            cast<SingleValueInstruction>(MemberStore->getSrc())));
-    MemberStore->eraseFromParent();
+    ToRemove.push_back(MemberStore);
   }
   // Create the initializers for the tail elements.
   unsigned NumBaseElements = ObjectArgs.size();
   for (StoreInst *TailStore : TailStores) {
     ObjectArgs.push_back(Cloner.clone(
                            cast<SingleValueInstruction>(TailStore->getSrc())));
-    TailStore->eraseFromParent();
+    ToRemove.push_back(TailStore);
   }
   // Create the initializer for the object itself.
   SILBuilder StaticInitBuilder(Glob);
@@ -1314,12 +1321,13 @@
   SILBuilder B(ARI);
   GlobalValueInst *GVI = B.createGlobalValue(ARI->getLoc(), Glob);
   B.createStrongRetain(ARI->getLoc(), GVI, B.getDefaultAtomicity());
-  while (!ARI->use_empty()) {
-    Operand *Use = *ARI->use_begin();
+  llvm::SmallVector<Operand *, 8> Worklist(ARI->use_begin(), ARI->use_end());
+  while (!Worklist.empty()) {
+    auto *Use = Worklist.pop_back_val();
     SILInstruction *User = Use->getUser();
     switch (User->getKind()) {
       case SILInstructionKind::DeallocRefInst:
-        User->eraseFromParent();
+        ToRemove.push_back(User);
         break;
       default:
         Use->set(GVI);
@@ -1332,8 +1340,7 @@
     replaceFindStringCall(FindStringCall);
   }
 
-  ARI->eraseFromParent();
-
+  ToRemove.push_back(ARI);
   HasChanged = true;
 }
 
@@ -1366,6 +1373,9 @@
   if (!cacheDecl)
     return;
 
+  assert(cacheDecl->hasFixedLayout(Module->getSwiftModule(),
+                                   ResilienceExpansion::Minimal));
+
   SILType wordTy = cacheType.getFieldType(
                             cacheDecl->getStoredProperties().front(), *Module);
 
@@ -1456,6 +1466,15 @@
     for (auto &BB : F) {
       bool IsCold = ColdBlocks.isCold(&BB);
       auto Iter = BB.begin();
+
+      // We can't remove instructions willy-nilly as we iterate because
+      // that might cause a pointer to the next instruction to become
+      // garbage, causing iterator invalidations (and crashes).
+      // Instead, we collect in a list the instructions we want to remove
+      // and erase the BB they belong to at the end of the loop, once we're
+      // sure it's safe to do so.
+      llvm::SmallVector<SILInstruction *, 4> ToRemove;
+
       while (Iter != BB.end()) {
         SILInstruction *I = &*Iter;
         Iter++;
@@ -1473,10 +1492,12 @@
             // for serializable functions.
             // TODO: We may do the optimization _after_ serialization in the
             // pass pipeline.
-            optimizeObjectAllocation(ARI);
+            optimizeObjectAllocation(ARI, ToRemove);
           }
         }
       }
+      for (auto *I : ToRemove)
+        I->eraseFromParent();
     }
   }
 
diff --git a/lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp b/lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp
index 0613f31..6df2adb 100644
--- a/lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp
+++ b/lib/SILOptimizer/Mandatory/DIMemoryUseCollector.cpp
@@ -419,8 +419,7 @@
     }
 
     // Stores *to* the allocation are writes.
-    if ((isa<StoreInst>(User) || isa<AssignInst>(User)) &&
-        UI->getOperandNumber() == 1) {
+    if (isa<StoreInst>(User) && UI->getOperandNumber() == 1) {
       if (PointeeType.is<TupleType>()) {
         UsesToScalarize.push_back(User);
         continue;
@@ -431,8 +430,6 @@
       DIUseKind Kind;
       if (InStructSubElement)
         Kind = DIUseKind::PartialStore;
-      else if (isa<AssignInst>(User))
-        Kind = DIUseKind::InitOrAssign;
       else if (PointeeType.isTrivial(User->getModule()))
         Kind = DIUseKind::InitOrAssign;
       else
@@ -632,17 +629,6 @@
         continue;
       }
 
-      // Scalarize AssignInst
-      if (auto *AI = dyn_cast<AssignInst>(User)) {
-        SILBuilderWithScope B(User, AI);
-        getScalarizedElements(AI->getOperand(0), ElementTmps, AI->getLoc(), B);
-
-        for (unsigned i = 0, e = ElementAddrs.size(); i != e; ++i)
-          B.createAssign(AI->getLoc(), ElementTmps[i], ElementAddrs[i]);
-        AI->eraseFromParent();
-        continue;
-      }
-      
       // Scalarize StoreInst
       if (auto *SI = dyn_cast<StoreInst>(User)) {
         SILBuilderWithScope B(User, SI);
diff --git a/lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp b/lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
index 92ec185..15f1745 100644
--- a/lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
+++ b/lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp
@@ -324,7 +324,7 @@
                       SmallVectorImpl<std::pair<SILValue, unsigned>> &Result,
                       llvm::SmallBitVector &ConflictingValues) {
   // Handle store and assign.
-  if (isa<StoreInst>(Inst) || isa<AssignInst>(Inst)) {
+  if (isa<StoreInst>(Inst)) {
     unsigned StartSubElt = computeSubelement(Inst->getOperand(1), TheMemory);
     assert(StartSubElt != ~0U && "Store within enum projection not handled");
     SILType ValTy = Inst->getOperand(0)->getType();
diff --git a/lib/SILOptimizer/Transforms/PerformanceInliner.cpp b/lib/SILOptimizer/Transforms/PerformanceInliner.cpp
index 1aa398d..8190a3e 100644
--- a/lib/SILOptimizer/Transforms/PerformanceInliner.cpp
+++ b/lib/SILOptimizer/Transforms/PerformanceInliner.cpp
@@ -899,8 +899,7 @@
     DominanceAnalysis *DA = PM->getAnalysis<DominanceAnalysis>();
     SILLoopAnalysis *LA = PM->getAnalysis<SILLoopAnalysis>();
     SideEffectAnalysis *SEA = PM->getAnalysis<SideEffectAnalysis>();
-    OptRemark::Emitter ORE(DEBUG_TYPE,
-                           getFunction()->getModule().getASTContext());
+    OptRemark::Emitter ORE(DEBUG_TYPE, getFunction()->getModule());
 
     if (getOptions().InlineThreshold == 0) {
       return;
diff --git a/lib/Sema/CodeSynthesis.cpp b/lib/Sema/CodeSynthesis.cpp
index 261ada8..3e45117 100644
--- a/lib/Sema/CodeSynthesis.cpp
+++ b/lib/Sema/CodeSynthesis.cpp
@@ -857,6 +857,7 @@
     // materializeForSet either.
     auto *baseMFS = baseASD->getMaterializeForSetFunc();
     if (baseMFS != nullptr &&
+        !baseMFS->hasForcedStaticDispatch() &&
         baseASD->isSetterAccessibleFrom(storage->getDeclContext())) {
       materializeForSet->setOverriddenDecl(baseMFS);
     }
diff --git a/lib/Sema/TypeCheckConstraints.cpp b/lib/Sema/TypeCheckConstraints.cpp
index 6715f8b..68d90dd 100644
--- a/lib/Sema/TypeCheckConstraints.cpp
+++ b/lib/Sema/TypeCheckConstraints.cpp
@@ -1012,6 +1012,7 @@
   options |= TR_AllowUnspecifiedTypes;
   options |= TR_AllowUnboundGenerics;
   options |= TR_InExpression;
+  options |= TR_AllowIUO;
   bool hadParameterError = false;
 
   GenericTypeToArchetypeResolver resolver(closure);
diff --git a/lib/Sema/TypeCheckDecl.cpp b/lib/Sema/TypeCheckDecl.cpp
index 0023391..73d54ed 100644
--- a/lib/Sema/TypeCheckDecl.cpp
+++ b/lib/Sema/TypeCheckDecl.cpp
@@ -1118,6 +1118,8 @@
   // top-level variables in a script file are accessible from other files,
   // even though the PBD is inside a TopLevelCodeDecl.
   TypeResolutionOptions options = TR_InExpression;
+
+  options |= TR_AllowIUO;
   if (binding->getInit(entryNumber)) {
     // If we have an initializer, we can also have unknown types.
     options |= TR_AllowUnspecifiedTypes;
@@ -2717,7 +2719,7 @@
 
       // Suggest '@nonobjc' to suppress this error, and not try to
       // infer @objc for anything.
-      tc.diagnose(decl, diag::optional_req_near_match_nonobjc, true)
+      tc.diagnose(decl, diag::req_near_match_nonobjc, true)
         .fixItInsert(decl->getAttributeInsertionLoc(false), "@nonobjc ");
       break;
     }
@@ -4246,10 +4248,14 @@
     GenericTypeToArchetypeResolver resolver(SD);
 
     bool isInvalid = TC.validateType(SD->getElementTypeLoc(), SD,
-                                     TypeResolutionOptions(),
+                                     TR_AllowIUO,
                                      &resolver);
+    TypeResolutionOptions options;
+    options |= TR_SubscriptParameters;
+    options |= TR_AllowIUO;
+
     isInvalid |= TC.typeCheckParameterList(SD->getIndices(), SD,
-                                           TR_SubscriptParameters,
+                                           options,
                                            resolver);
 
     if (isInvalid || SD->isInvalid()) {
@@ -4786,9 +4792,8 @@
                              GenericTypeResolver &resolver) {
     bool hadError = false;
     for (auto paramList : fd->getParameterLists()) {
-      hadError |= TC.typeCheckParameterList(paramList, fd,
-                                            TypeResolutionOptions(),
-                                            resolver);
+      hadError |=
+          TC.typeCheckParameterList(paramList, fd, TR_AllowIUO, resolver);
     }
 
     return hadError;
@@ -4799,9 +4804,10 @@
 
     bool badType = false;
     if (!FD->getBodyResultTypeLoc().isNull()) {
-      TypeResolutionOptions options;
+      TypeResolutionOptions options = TR_AllowIUO;
       if (FD->hasDynamicSelf())
         options |= TR_DynamicSelfResult;
+
       if (TC.validateType(FD->getBodyResultTypeLoc(), FD, options,
                           &resolver)) {
         badType = true;
@@ -6705,6 +6711,13 @@
             !baseASD->isSetterAccessibleFrom(overridingASD->getDeclContext()))
           return;
 
+        // A materializeForSet for an override of storage with a
+        // forced static dispatch materializeForSet is not itself an
+        // override.
+        if (kind == AccessorKind::IsMaterializeForSet &&
+            baseAccessor->hasForcedStaticDispatch())
+          return;
+
         // FIXME: Egregious hack to set an 'override' attribute.
         if (!overridingAccessor->getAttrs().hasAttribute<OverrideAttr>()) {
           auto loc = overridingASD->getOverrideLoc();
@@ -8157,6 +8170,14 @@
     return;
   }
 
+  // If the nominal type has a generic signature that we didn't otherwise
+  // handle yet, use it directly.
+  if (auto genericSig = nominal->getGenericSignature()) {
+    auto genericEnv = genericSig->createGenericEnvironment();
+    ext->setGenericEnvironment(genericEnv);
+    return;
+  }
+
   assert(extendedType->is<NominalType>());
 }
 
diff --git a/lib/Sema/TypeCheckGeneric.cpp b/lib/Sema/TypeCheckGeneric.cpp
index 7aca106..4fb3087 100644
--- a/lib/Sema/TypeCheckGeneric.cpp
+++ b/lib/Sema/TypeCheckGeneric.cpp
@@ -457,7 +457,7 @@
   // Check the parameter patterns.
   for (auto params : func->getParameterLists()) {
     // Check the pattern.
-    if (tc.typeCheckParameterList(params, func, TypeResolutionOptions(),
+    if (tc.typeCheckParameterList(params, func, TR_AllowIUO,
                                   resolver))
       badType = true;
 
@@ -472,7 +472,7 @@
   if (auto fn = dyn_cast<FuncDecl>(func)) {
     if (!fn->getBodyResultTypeLoc().isNull()) {
       // Check the result type of the function.
-      TypeResolutionOptions options;
+      TypeResolutionOptions options = TR_AllowIUO;
       if (fn->hasDynamicSelf())
         options |= TR_DynamicSelfResult;
 
@@ -949,8 +949,12 @@
   // Check the indices.
   auto params = subscript->getIndices();
 
+  TypeResolutionOptions options;
+  options |= TR_SubscriptParameters;
+  options |= TR_AllowIUO;
+
   badType |= tc.typeCheckParameterList(params, subscript,
-                                       TR_SubscriptParameters,
+                                       options,
                                        resolver);
 
   // Infer requirements from the pattern.
diff --git a/lib/Sema/TypeCheckProtocol.cpp b/lib/Sema/TypeCheckProtocol.cpp
index dc6ba29..e86a809 100644
--- a/lib/Sema/TypeCheckProtocol.cpp
+++ b/lib/Sema/TypeCheckProtocol.cpp
@@ -2063,6 +2063,7 @@
     llvm::SmallVector<ConformanceChecker, 4> AllUsedCheckers;
     llvm::SmallVector<NormalProtocolConformance*, 4> AllConformances;
     llvm::SetVector<ValueDecl*> MissingWitnesses;
+    llvm::SmallPtrSet<ValueDecl *, 8> CoveredMembers;
 
     /// Check one conformance.
     ProtocolConformance * checkIndividualConformance(
@@ -2083,6 +2084,11 @@
       return llvm::makeArrayRef(UnsatisfiedReqs);
     }
 
+    /// Whether this member is "covered" by one of the conformances.
+    bool isCoveredMember(ValueDecl *member) const {
+      return CoveredMembers.count(member) > 0;
+    }
+
     /// Check all conformances and emit diagnosis globally.
     void checkAllConformances();
   };
@@ -2092,11 +2098,24 @@
     if (conformance->isInvalid()) return false;
     if (isa<TypeDecl>(req)) return false;
 
+    auto witness = conformance->hasWitness(req)
+      ? conformance->getWitness(req, nullptr).getDecl()
+      : nullptr;
+
     // An optional requirement might not have a witness...
-    if (!conformance->hasWitness(req) ||
-        !conformance->getWitness(req, nullptr).getDecl())
+    if (!witness)
       return req->getAttrs().hasAttribute<OptionalAttr>();
 
+    // If the witness lands within the declaration context of the conformance,
+    // record it as a "covered" member.
+    if (witness->getDeclContext() == conformance->getDeclContext())
+      CoveredMembers.insert(witness);
+
+    // The witness might come from a protocol or protocol extension.
+    if (witness->getDeclContext()
+          ->getAsProtocolOrProtocolExtensionContext())
+      return true;
+
     return false;
   }
 
@@ -5535,7 +5554,7 @@
           // If the requirement is optional, @nonobjc suppresses the
           // diagnostic.
           if (isOptional) {
-            TC.diagnose(witness, diag::optional_req_near_match_nonobjc, false)
+            TC.diagnose(witness, diag::req_near_match_nonobjc, false)
               .fixItInsert(witness->getAttributeInsertionLoc(false),
                            "@nonobjc ");
           }
@@ -6213,11 +6232,14 @@
   DeclName reqName = req->getFullName();
   DeclName witnessName = witness->getFullName();
 
-  // Apply the omit-needless-words heuristics to both names.
-  if (auto adjustedReqName = ::omitNeedlessWords(tc, req))
-    reqName = *adjustedReqName;
-  if (auto adjustedWitnessName = ::omitNeedlessWords(tc, witness))
-    witnessName = *adjustedWitnessName;
+  // For @objc protocols, apply the omit-needless-words heuristics to
+  // both names.
+  if (cast<ProtocolDecl>(req->getDeclContext())->isObjC()) {
+    if (auto adjustedReqName = ::omitNeedlessWords(tc, req))
+      reqName = *adjustedReqName;
+    if (auto adjustedWitnessName = ::omitNeedlessWords(tc, witness))
+      witnessName = *adjustedWitnessName;
+  }
 
   return scorePotentiallyMatchingNames(reqName, witnessName, isa<FuncDecl>(req),
                                        limit);
@@ -6317,19 +6339,40 @@
   return length;
 }
 
+/// Determine whether a particular declaration is generic.
+static bool isGeneric(ValueDecl *decl) {
+  if (auto func = dyn_cast<AbstractFunctionDecl>(decl))
+    return func->isGeneric();
+  if (auto subscript = dyn_cast<SubscriptDecl>(decl))
+    return subscript->isGeneric();
+  return false;
+}
+
 /// Determine whether we should warn about the given witness being a close
 /// match for the given optional requirement.
-static bool shouldWarnAboutPotentialWitness(ValueDecl *req,
-                                            ValueDecl *witness,
-                                            AccessLevel access,
-                                            unsigned score) {
+static bool shouldWarnAboutPotentialWitness(
+                                      MultiConformanceChecker &groupChecker,
+                                      ValueDecl *req,
+                                      ValueDecl *witness,
+                                      AccessLevel access,
+                                      unsigned score) {
+  // If the witness is covered, don't warn about it.
+  if (groupChecker.isCoveredMember(witness))
+    return false;
+
   // If the warning couldn't be suppressed, don't warn.
   if (!canSuppressPotentialWitnessWarningWithMovement(req, witness) &&
       !canSuppressPotentialWitnessWarningWithNonObjC(req, witness))
     return false;
 
-  // If the potential witness is already marked @nonobjc, don't warn.
-  if (witness->getAttrs().hasAttribute<NonObjCAttr>())
+  // If the potential witness for an @objc requirement is already
+  // marked @nonobjc, don't warn.
+  if (req->isObjC() && witness->getAttrs().hasAttribute<NonObjCAttr>())
+    return false;
+
+  // If the witness is generic and requirement is not, or vice-versa,
+  // don't warn.
+  if (isGeneric(req) != isGeneric(witness))
     return false;
 
   // Don't warn if the potential witness has been explicitly given less
@@ -6360,9 +6403,10 @@
   auto proto = cast<ProtocolDecl>(req->getDeclContext());
 
   // Primary warning.
-  tc.diagnose(witness, diag::optional_req_near_match,
+  tc.diagnose(witness, diag::req_near_match,
               witness->getDescriptiveKind(),
               witness->getFullName(),
+              req->getAttrs().hasAttribute<OptionalAttr>(),
               req->getFullName(),
               proto->getFullName());
 
@@ -6385,21 +6429,21 @@
   // If moving the declaration can help, suggest that.
   if (auto move
         = canSuppressPotentialWitnessWarningWithMovement(req, witness)) {
-    tc.diagnose(witness, diag::optional_req_near_match_move,
+    tc.diagnose(witness, diag::req_near_match_move,
                 witness->getFullName(), static_cast<unsigned>(*move));
   }
 
   // If adding 'private', 'fileprivate', or 'internal' can help, suggest that.
   if (access > AccessLevel::FilePrivate &&
       !witness->getAttrs().hasAttribute<AccessControlAttr>()) {
-    tc.diagnose(witness, diag::optional_req_near_match_access,
+    tc.diagnose(witness, diag::req_near_match_access,
                 witness->getFullName(), access)
       .fixItInsert(witness->getAttributeInsertionLoc(true), "private ");
   }
 
   // If adding @nonobjc can help, suggest that.
   if (canSuppressPotentialWitnessWarningWithNonObjC(req, witness)) {
-    tc.diagnose(witness, diag::optional_req_near_match_nonobjc, false)
+    tc.diagnose(witness, diag::req_near_match_nonobjc, false)
       .fixItInsert(witness->getAttributeInsertionLoc(false), "@nonobjc ");
   }
 
@@ -6647,8 +6691,8 @@
 
         bool valueIsType = isa<TypeDecl>(value);
         for (auto requirement
-              : diag.Protocol->lookupDirect(value->getFullName(),
-                                            /*ignoreNewExtensions=*/true)) {
+                : diag.Protocol->lookupDirect(value->getFullName(),
+                                              /*ignoreNewExtensions=*/true)) {
           auto requirementIsType = isa<TypeDecl>(requirement);
           if (valueIsType != requirementIsType)
             continue;
@@ -6742,8 +6786,8 @@
             bestOptionalReqs.begin(),
             bestOptionalReqs.end(),
             [&](ValueDecl *req) {
-              return !shouldWarnAboutPotentialWitness(req, value, defaultAccess,
-                                                      bestScore);
+              return !shouldWarnAboutPotentialWitness(groupChecker, req, value,
+                                                      defaultAccess, bestScore);
             }),
           bestOptionalReqs.end());
       }
@@ -6764,7 +6808,7 @@
         assert(diagnosed && "Failed to find conformance to diagnose?");
         (void)diagnosed;
 
-        // Remove this optional requirement from the list. We don't want to
+        // Remove this requirement from the list. We don't want to
         // complain about it twice.
         unsatisfiedReqs.erase(std::find(unsatisfiedReqs.begin(),
                                         unsatisfiedReqs.end(),
diff --git a/lib/Sema/TypeCheckType.cpp b/lib/Sema/TypeCheckType.cpp
index fd97d7c..72005aa 100644
--- a/lib/Sema/TypeCheckType.cpp
+++ b/lib/Sema/TypeCheckType.cpp
@@ -1180,6 +1180,63 @@
     return ErrorType::get(TC.Context);
   }
 
+  // Emit a warning about directly spelling
+  // ImplicitlyUnwrappedOptional rather than using a trailing '!'.
+  auto *IUODecl = TC.Context.getImplicitlyUnwrappedOptionalDecl();
+  if (currentDecl == IUODecl) {
+    if (options.contains(TR_AllowIUO)) {
+      if (isa<GenericIdentTypeRepr>(comp)) {
+        auto *genericTyR = cast<GenericIdentTypeRepr>(comp);
+        assert(genericTyR->getGenericArgs().size() == 1);
+        auto *genericArgTyR = genericTyR->getGenericArgs()[0];
+
+        Diagnostic diag = diag::implicitly_unwrapped_optional_spelling_deprecated_with_fixit;
+
+        // For Swift 5 and later, spelling the full name is an error.
+        if (TC.Context.isSwiftVersionAtLeast(5))
+          diag = diag::implicitly_unwrapped_optional_spelling_error_with_fixit;
+
+        TC.diagnose(comp->getStartLoc(), diag)
+          .fixItRemoveChars(
+              genericTyR->getStartLoc(),
+              genericTyR->getAngleBrackets().Start.getAdvancedLoc(1))
+          .fixItInsertAfter(genericArgTyR->getEndLoc(), "!")
+          .fixItRemoveChars(
+              genericTyR->getAngleBrackets().End,
+              genericTyR->getAngleBrackets().End.getAdvancedLoc(1));
+      } else {
+        Diagnostic diag = diag::implicitly_unwrapped_optional_spelling_deprecated;
+
+        // For Swift 5 and later, spelling the full name is an error.
+        if (TC.Context.isSwiftVersionAtLeast(5))
+          diag = diag::implicitly_unwrapped_optional_spelling_error;
+
+        TC.diagnose(comp->getStartLoc(), diag);
+      }
+    } else if (TC.Context.isSwiftVersionAtLeast(5)) {
+      if (isa<GenericIdentTypeRepr>(comp)) {
+        auto *genericTyR = cast<GenericIdentTypeRepr>(comp);
+        assert(genericTyR->getGenericArgs().size() == 1);
+        auto *genericArgTyR = genericTyR->getGenericArgs()[0];
+
+        TC.diagnose(comp->getStartLoc(), diag::iuo_in_illegal_position)
+          .fixItRemoveChars(
+              genericTyR->getStartLoc(),
+              genericTyR->getAngleBrackets().Start.getAdvancedLoc(1))
+          .fixItInsertAfter(genericArgTyR->getEndLoc(), "?")
+          .fixItRemoveChars(
+              genericTyR->getAngleBrackets().End,
+              genericTyR->getAngleBrackets().End.getAdvancedLoc(1));
+      } else {
+        TC.diagnose(comp->getStartLoc(), diag::iuo_in_illegal_position);
+      }
+    } else {
+      // Pre-Swift-5 warning for spelling ImplicitlyUnwrappedOptional
+      // in places we shouldn't even allow it.
+      TC.diagnose(comp->getStartLoc(), diag::implicitly_unwrapped_optional_spelling_deprecated);
+    }
+  }
+
   // If we found nothing, complain and give ourselves a chance to recover.
   if (current.isNull()) {
     // If we're not allowed to complain or we couldn't fix the
@@ -1582,8 +1639,12 @@
     Context.Stats->getFrontendCounters().NumTypesValidated++;
 
   if (Loc.getType().isNull()) {
-    // Raise error if we parse an IUO type in an illegal position.
-    checkForIllegalIUOs(*this, Loc.getTypeRepr(), options);
+    // Swift version < 5? Use the old "illegal IUO" check for
+    // backwards compatibiliy.
+    if (!Context.isSwiftVersionAtLeast(5)) {
+      // Raise error if we parse an IUO type in an illegal position.
+      checkForIllegalIUOs(*this, Loc.getTypeRepr(), options);
+    }
 
     auto type = resolveType(Loc.getTypeRepr(), DC, options, resolver,
                             unsatisfiedDependency);
@@ -2716,6 +2777,14 @@
   auto elementOptions = withoutContext(options, true);
   elementOptions |= TR_ImmediateOptionalTypeArgument;
 
+  // Swift version >= 5? Use the newer check for IUOs appearing in
+  // illegal positions.
+  if (TC.Context.isSwiftVersionAtLeast(5) &&
+      !elementOptions.contains(TR_AllowIUO)) {
+    TC.diagnose(repr->getStartLoc(), diag::iuo_in_illegal_position)
+      .fixItReplace(repr->getExclamationLoc(), "?");
+  }
+
   // The T in T! is a generic type argument and therefore always an AST type.
   // FIXME: diagnose non-materializability of element type!
   Type baseTy = resolveType(repr->getBase(), elementOptions);
diff --git a/lib/Sema/TypeChecker.h b/lib/Sema/TypeChecker.h
index ba2e86b..7942dc3 100644
--- a/lib/Sema/TypeChecker.h
+++ b/lib/Sema/TypeChecker.h
@@ -536,6 +536,9 @@
 
   /// Whether we are checking the parameter list of a subscript.
   TR_SubscriptParameters = 0x2000000,
+
+  /// Is it okay to resolve an IUO sigil ("!") here?
+  TR_AllowIUO = 0x4000000,
 };
 
 /// Option set describing how type resolution should work.
diff --git a/lib/Serialization/Deserialization.cpp b/lib/Serialization/Deserialization.cpp
index 77750af..e40ef87 100644
--- a/lib/Serialization/Deserialization.cpp
+++ b/lib/Serialization/Deserialization.cpp
@@ -760,6 +760,10 @@
       break;
   }
 
+  // Don't create empty generic parameter lists.
+  if (params.empty())
+    return nullptr;
+
   auto paramList = GenericParamList::create(getContext(), SourceLoc(),
                                             params, SourceLoc(), { },
                                             SourceLoc());
diff --git a/lib/Serialization/SerializeSIL.cpp b/lib/Serialization/SerializeSIL.cpp
index 384f779..237f38f 100644
--- a/lib/Serialization/SerializeSIL.cpp
+++ b/lib/Serialization/SerializeSIL.cpp
@@ -2297,7 +2297,8 @@
 
   // Write out fragile WitnessTables.
   for (const SILWitnessTable &wt : SILMod->getWitnessTables()) {
-    if (wt.getConformance()->getDeclContext()->isChildContextOf(assocDC))
+    if ((ShouldSerializeAll || wt.isSerialized()) &&
+        wt.getConformance()->getDeclContext()->isChildContextOf(assocDC))
       writeSILWitnessTable(wt);
   }
 
diff --git a/lib/TBDGen/TBDGen.cpp b/lib/TBDGen/TBDGen.cpp
index 1089960..46f8bbc 100644
--- a/lib/TBDGen/TBDGen.cpp
+++ b/lib/TBDGen/TBDGen.cpp
@@ -109,7 +109,7 @@
     // sometimes witness thunks need to be manually made public.
 
     auto conformanceIsFixed = SILWitnessTable::conformanceIsSerialized(
-        normalConformance, SwiftModule->getResilienceStrategy());
+        normalConformance);
     auto addSymbolIfNecessary = [&](ValueDecl *valueReq,
                                     SILLinkage witnessLinkage) {
       if (conformanceIsFixed &&
diff --git a/stdlib/public/core/RangeReplaceableCollection.swift.gyb b/stdlib/public/core/RangeReplaceableCollection.swift.gyb
index eda9d20..fd539dd 100644
--- a/stdlib/public/core/RangeReplaceableCollection.swift.gyb
+++ b/stdlib/public/core/RangeReplaceableCollection.swift.gyb
@@ -354,6 +354,9 @@
   mutating func removeAll(keepingCapacity keepCapacity: Bool /*= false*/)
 
   // FIXME(ABI): Associated type inference requires this.
+  subscript(bounds: Index) -> Element { get }
+
+  // FIXME(ABI): Associated type inference requires this.
   subscript(bounds: Range<Index>) -> SubSequence { get }
 }
 
diff --git a/stdlib/public/core/Shims.swift b/stdlib/public/core/Shims.swift
index 4596d47..3b453a1 100644
--- a/stdlib/public/core/Shims.swift
+++ b/stdlib/public/core/Shims.swift
@@ -29,6 +29,7 @@
 /// A dummy value to be used as the target for `mutationsPtr` in fast
 /// enumeration implementations.
 @_versioned // FIXME(sil-serialize-all)
+@_fixed_layout
 internal var _fastEnumerationStorageMutationsTarget: CUnsignedLong = 0
 
 /// A dummy pointer to be used as `mutationsPtr` in fast enumeration
@@ -36,6 +37,8 @@
 @_inlineable // FIXME(sil-serialize-all)
 public // SPI(Foundation)
 var _fastEnumerationStorageMutationsPtr: UnsafeMutablePointer<CUnsignedLong> {
+  // Note that either _fastEnumerationStorageMutationsPtr should be
+  // @_fixed_layout, or this function should not be @_inlineable.
   return UnsafeMutablePointer(
       Builtin.addressof(&_fastEnumerationStorageMutationsTarget))
 }
diff --git a/stdlib/public/core/StringSwitch.swift b/stdlib/public/core/StringSwitch.swift
index b1e90c8..d35b107 100644
--- a/stdlib/public/core/StringSwitch.swift
+++ b/stdlib/public/core/StringSwitch.swift
@@ -32,6 +32,7 @@
   return -1
 }
 
+@_fixed_layout // needs known size for static allocation
 public // used by COMPILER_INTRINSIC
 struct _OpaqueStringSwitchCache {
   var a: Builtin.Word
diff --git a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
index 74f658c..50f7456 100644
--- a/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
+++ b/stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
@@ -467,7 +467,7 @@
       _debugPrecondition(bounds.upperBound <= endIndex)
       _debugPrecondition(bounds.count == newValue.count)
 
-      if newValue.count > 0 {
+      if !newValue.isEmpty {
         (baseAddress! + bounds.lowerBound).copyBytes(
           from: newValue.base.baseAddress! + newValue.startIndex,
           count: newValue.count)
diff --git a/stdlib/public/runtime/ErrorObject.h b/stdlib/public/runtime/ErrorObject.h
index 74c4774..f023a5e 100644
--- a/stdlib/public/runtime/ErrorObject.h
+++ b/stdlib/public/runtime/ErrorObject.h
@@ -208,6 +208,8 @@
 void swift_errorInMain(SwiftError *object);
 SWIFT_RUNTIME_EXPORT
 void swift_willThrow(SwiftError *object);
+
+SWIFT_CC(swift)
 SWIFT_RUNTIME_EXPORT LLVM_ATTRIBUTE_NORETURN
 void swift_unexpectedError(SwiftError *object);
 
diff --git a/stdlib/public/runtime/ImageInspectionELF.cpp b/stdlib/public/runtime/ImageInspectionELF.cpp
index 7d89704..2fd9bef 100644
--- a/stdlib/public/runtime/ImageInspectionELF.cpp
+++ b/stdlib/public/runtime/ImageInspectionELF.cpp
@@ -27,7 +27,7 @@
 #include <link.h>
 #include <string.h>
 
-#if defined(__ANDROID__)
+#if defined(__ANDROID__) || defined(__FreeBSD__)
 #include "llvm/ADT/StringRef.h"
 #endif
 
@@ -72,9 +72,14 @@
   SectionInfo sectionInfo = { 0, nullptr };
   void *handle = dlopen(imageName, RTLD_LAZY | RTLD_NOLOAD);
   if (!handle) {
-#ifdef __ANDROID__
+#if defined(__ANDROID__) || defined(__FreeBSD__)
+#if defined(__ANDROID__)
+    const char *systemPath = "/system/lib";
+#elif defined(__FreeBSD__)
+    const char *systemPath = "/libexec";
+#endif
     llvm::StringRef imagePath = llvm::StringRef(imageName);
-    if (imagePath.startswith("/system/lib") ||
+    if (imagePath.startswith(systemPath) ||
         (imageName && !imagePath.endswith(".so"))) {
       return sectionInfo;
     }
diff --git a/test/Constraints/generic_overload.swift b/test/Constraints/generic_overload.swift
index c300c62..12451b3 100644
--- a/test/Constraints/generic_overload.swift
+++ b/test/Constraints/generic_overload.swift
@@ -1,7 +1,7 @@
 // RUN: %target-typecheck-verify-swift
 
-protocol P1 { associatedtype Assoc } // expected-note{{declared here}}
-protocol P2 : P1 { associatedtype Assoc } // expected-warning{{redeclaration of associated type}}
+protocol P1 { associatedtype Assoc }
+protocol P2 : P1 { associatedtype Assoc }
 protocol P3 { }
 
 struct X1 : P1 { typealias Assoc = X3 }
diff --git a/test/Driver/opt-record.swift b/test/Driver/opt-record.swift
new file mode 100644
index 0000000..823f519
--- /dev/null
+++ b/test/Driver/opt-record.swift
@@ -0,0 +1,43 @@
+// RUN: %empty-directory(%t)
+// RUN: %target-swiftc_driver -O -wmo -save-optimization-record %s -module-name optrecordmod -o %t/opt-record 2>&1 | %FileCheck -allow-empty %s
+// RUN: %FileCheck -check-prefix=YAML %s < %t/optrecordmod.opt.yaml
+// RUN: %target-swiftc_driver -O -wmo -save-optimization-record-path %t/specified.opt.yaml %s -module-name optrecordmod -o %t/opt-record 2>&1 | %FileCheck -allow-empty %s
+// RUN: %FileCheck -check-prefix=YAML %s < %t/specified.opt.yaml
+
+// CHECK-NOT: remark
+
+var a: Int = 1
+
+func foo() {
+  a = 2
+}
+
+public func bar() {
+  // YAML:      --- !Passed
+  // YAML-NEXT: Pass:            sil-inliner
+  // YAML-NEXT: Name:            Inlined
+  // YAML-NEXT: DebugLoc:
+  // YAML-NEXT:   File:            {{.*}}opt-record.swift
+  // YAML-NEXT:   Line:            42
+  // YAML-NEXT:   Column:          3
+  // YAML-NEXT: Function:        _T012optrecordmod3baryyF
+  // YAML-NEXT: Args:
+  // YAML-NEXT:   - Callee:          _T012optrecordmod3fooyyF
+  // YAML-NEXT:     DebugLoc:
+  // YAML-NEXT:       File:            {{.*}}opt-record.swift
+  // YAML-NEXT:       Line:            11
+  // YAML-NEXT:       Column:          6
+  // YAML-NEXT:   - String:          ' inlined into '
+  // YAML-NEXT:   - Caller:          _T012optrecordmod3baryyF
+  // YAML-NEXT:     DebugLoc:
+  // YAML-NEXT:       File:            {{.*}}opt-record.swift
+  // YAML-NEXT:       Line:            15
+  // YAML-NEXT:       Column:          13
+  // YAML-NEXT:   - String:          ' (cost = '
+  // YAML-NEXT:   - Cost:            '{{.*}}'
+  // YAML-NEXT:   - String:          ', benefit = '
+  // YAML-NEXT:   - Benefit:         '{{.*}}'
+  // YAML-NEXT:   - String:          ')'
+  // YAML-NEXT: ...
+  foo()
+}
diff --git a/test/Frontend/sil-merge-partial-modules.swift b/test/Frontend/sil-merge-partial-modules.swift
index 630ae29..2fb01dc 100644
--- a/test/Frontend/sil-merge-partial-modules.swift
+++ b/test/Frontend/sil-merge-partial-modules.swift
@@ -72,6 +72,6 @@
 
 // NEGATIVE-NOT: sil {{.*}}internalFunction
 
-// NEGATIVE: sil_witness_table {{.*}}Circle: Shape
+// NEGATIVE-NOT: sil_witness_table {{.*}}Circle: Shape
 
 // NEGATIVE-NOT: sil_vtable
diff --git a/test/Generics/associated_type_where_clause.swift b/test/Generics/associated_type_where_clause.swift
index 5c936e1..a342e62 100644
--- a/test/Generics/associated_type_where_clause.swift
+++ b/test/Generics/associated_type_where_clause.swift
@@ -138,16 +138,16 @@
 
 // Lookup of same-named associated types aren't ambiguous in this context.
 protocol P1 {
-  associatedtype A // expected-note 2{{declared here}}
+  associatedtype A
 }
 
 protocol P2: P1 {
-  associatedtype A // expected-warning{{redeclaration of associated type}}
+  associatedtype A
   associatedtype B where A == B
 }
 
 protocol P3: P1 {
-  associatedtype A // expected-warning{{redeclaration of associated type}}
+  associatedtype A
 }
 
 protocol P4 {
diff --git a/test/Generics/canonicalization.swift b/test/Generics/canonicalization.swift
index 8f262ed..256ea96 100644
--- a/test/Generics/canonicalization.swift
+++ b/test/Generics/canonicalization.swift
@@ -4,11 +4,11 @@
 protocol P0 { }
 
 protocol P {
-  associatedtype A // expected-note{{declared here}}
+  associatedtype A
 }
 
 protocol Q : P {
-  associatedtype A // expected-warning{{redeclaration of associated type 'A' from protocol 'P' is better expressed as a 'where' clause on the protocol}}
+  associatedtype A
 }
 
 func f<T>(t: T) where T : P, T : Q, T.A : P0 { } // expected-note{{'f(t:)' previously declared here}}
diff --git a/test/Generics/protocol_requirement_signatures.swift b/test/Generics/protocol_requirement_signatures.swift
index 676b1bf..8b65d7a 100644
--- a/test/Generics/protocol_requirement_signatures.swift
+++ b/test/Generics/protocol_requirement_signatures.swift
@@ -22,7 +22,7 @@
 // CHECK-NEXT: Requirement signature: <Self where Self.X : P1>
 // CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.X : P1>
 protocol Q1 {
-    associatedtype X: P1 // expected-note 2{{declared here}}
+    associatedtype X: P1 // expected-note {{declared here}}
 }
 
 // inheritance
@@ -36,7 +36,7 @@
 // CHECK-NEXT: Requirement signature: <Self where Self : Q1>
 // CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0 : Q1>
 protocol Q3: Q1 {
-    associatedtype X // expected-warning{{redeclaration of associated type 'X'}}
+    associatedtype X
 }
 
 // inheritance adding a new conformance
diff --git a/test/Generics/requirement_inference.swift b/test/Generics/requirement_inference.swift
index b86e80f..bf6bfa2 100644
--- a/test/Generics/requirement_inference.swift
+++ b/test/Generics/requirement_inference.swift
@@ -139,13 +139,13 @@
 }
 
 protocol P8 {
-  associatedtype A // expected-note{{'A' declared here}}
-  associatedtype B // expected-note{{'B' declared here}}
+  associatedtype A
+  associatedtype B
 }
 
 protocol P9 : P8 {
-  associatedtype A // expected-warning{{redeclaration of associated type 'A' from protocol 'P8' is better expressed as a 'where' clause on the protocol}}
-  associatedtype B // expected-warning{{redeclaration of associated type 'B' from protocol 'P8' is better expressed as a 'where' clause on the protocol}}
+  associatedtype A
+  associatedtype B
 }
 
 protocol P10 {
@@ -295,8 +295,8 @@
 // CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : P22>
 // CHECK: Protocol requirement signature:
 // CHECK: .P22@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : P20, Self.A == X20<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : P20, τ_0_0.A == X20<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X20<Self.B>, Self.B : P20>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X20<τ_0_0.B>, τ_0_0.B : P20>
 protocol P22 {
   associatedtype A
   associatedtype B where A == X20<B>
@@ -306,8 +306,8 @@
 // CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : P23>
 // CHECK: Protocol requirement signature:
 // CHECK: .P23@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : P20, Self.A == X20<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : P20, τ_0_0.A == X20<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X20<Self.B>, Self.B : P20>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X20<τ_0_0.B>, τ_0_0.B : P20>
 protocol P23 {
   associatedtype A
   associatedtype B: P20
@@ -323,16 +323,16 @@
 }
 
 // CHECK-LABEL: .P25a@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : P20, Self.A == X24<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : P20, τ_0_0.A == X24<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X24<Self.B>, Self.B : P20>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X24<τ_0_0.B>, τ_0_0.B : P20>
 protocol P25a {
   associatedtype A: P24 // expected-warning{{redundant conformance constraint 'Self.A': 'P24'}}
   associatedtype B where A == X24<B> // expected-note{{conformance constraint 'Self.A': 'P24' implied here}}
 }
 
 // CHECK-LABEL: .P25b@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : P20, Self.A == X24<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : P20, τ_0_0.A == X24<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X24<Self.B>, Self.B : P20>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X24<τ_0_0.B>, τ_0_0.B : P20>
 protocol P25b {
   associatedtype A
   associatedtype B where A == X24<B>
@@ -353,16 +353,16 @@
 }
 
 // CHECK-LABEL: .P27a@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : X3, Self.A == X26<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : X3, τ_0_0.A == X26<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X26<Self.B>, Self.B : X3>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X26<τ_0_0.B>, τ_0_0.B : X3>
 protocol P27a {
   associatedtype A: P26 // expected-warning{{redundant conformance constraint 'Self.A': 'P26'}}
   associatedtype B where A == X26<B> // expected-note{{conformance constraint 'Self.A': 'P26' implied here}}
 }
 
 // CHECK-LABEL: .P27b@
-// CHECK-NEXT: Requirement signature: <Self where Self.B : X3, Self.A == X26<Self.B>>
-// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.B : X3, τ_0_0.A == X26<τ_0_0.B>>
+// CHECK-NEXT: Requirement signature: <Self where Self.A == X26<Self.B>, Self.B : X3>
+// CHECK-NEXT: Canonical requirement signature: <τ_0_0 where τ_0_0.A == X26<τ_0_0.B>, τ_0_0.B : X3>
 protocol P27b {
   associatedtype A
   associatedtype B where A == X26<B>
diff --git a/test/IDE/complete_associated_types.swift b/test/IDE/complete_associated_types.swift
index 79a6af9..fad21e7 100644
--- a/test/IDE/complete_associated_types.swift
+++ b/test/IDE/complete_associated_types.swift
@@ -32,8 +32,8 @@
   associatedtype FooBaseDefaultedTypeB = Int
   associatedtype FooBaseDefaultedTypeC = Int
 
-  associatedtype DeducedTypeCommonA // expected-note{{declared here}}
-  associatedtype DeducedTypeCommonB // expected-note{{declared here}}
+  associatedtype DeducedTypeCommonA
+  associatedtype DeducedTypeCommonB
   associatedtype DeducedTypeCommonC
   associatedtype DeducedTypeCommonD
   func deduceCommonA() -> DeducedTypeCommonA
@@ -57,8 +57,8 @@
 
   associatedtype FooBaseDefaultedTypeB = Double
 
-  associatedtype DeducedTypeCommonA // expected-warning{{redeclaration of associated type}}
-  associatedtype DeducedTypeCommonB // expected-warning{{redeclaration of associated type}}
+  associatedtype DeducedTypeCommonA
+  associatedtype DeducedTypeCommonB
   func deduceCommonA() -> DeducedTypeCommonA
   func deduceCommonB() -> DeducedTypeCommonB
 
@@ -79,7 +79,7 @@
   associatedtype DefaultedTypeCommonA = Int
   associatedtype DefaultedTypeCommonC = Int
 
-  associatedtype DeducedTypeCommonA // expected-note{{'DeducedTypeCommonA' declared here}}
+  associatedtype DeducedTypeCommonA
   associatedtype DeducedTypeCommonC
   func deduceCommonA() -> DeducedTypeCommonA
   func deduceCommonC() -> DeducedTypeCommonC
@@ -102,7 +102,7 @@
   associatedtype DefaultedTypeCommonA = Int
   associatedtype DefaultedTypeCommonD = Int
 
-  associatedtype DeducedTypeCommonA // expected-warning{{redeclaration of associated type}}
+  associatedtype DeducedTypeCommonA
   associatedtype DeducedTypeCommonD
   func deduceCommonA() -> DeducedTypeCommonA
   func deduceCommonD() -> DeducedTypeCommonD
diff --git a/test/IRGen/Inputs/usr/include/SRoA.h b/test/IRGen/Inputs/usr/include/SRoA.h
new file mode 100644
index 0000000..e838c0f
--- /dev/null
+++ b/test/IRGen/Inputs/usr/include/SRoA.h
@@ -0,0 +1,8 @@
+
+typedef struct S {
+  float f;
+  float g;
+} S;
+
+void f(S);
+
diff --git a/test/IRGen/Inputs/usr/include/module.map b/test/IRGen/Inputs/usr/include/module.map
index befdc45..44fdc95 100644
--- a/test/IRGen/Inputs/usr/include/module.map
+++ b/test/IRGen/Inputs/usr/include/module.map
@@ -1,4 +1,11 @@
-module gizmo { header "Gizmo.h" }
+
+module gizmo {
+  header "Gizmo.h"
+}
+
+module SRoA {
+  header "SRoA.h"
+}
 
 module ObjectiveC {
   header "BridgeTestObjectiveC.h"
diff --git a/test/IRGen/builtins.swift b/test/IRGen/builtins.swift
index 6e02601..bca276d 100644
--- a/test/IRGen/builtins.swift
+++ b/test/IRGen/builtins.swift
@@ -11,6 +11,8 @@
 typealias Int = Builtin.Int32
 typealias Bool = Builtin.Int1
 
+// CHECK: call swiftcc void @swift_errorInMain(
+
 infix operator * {
   associativity left
   precedence 200
@@ -838,5 +840,22 @@
   Builtin.atomicstore_seqcst_volatile_FPIEEE32(p, d)
 }
 
+func createInt(_ fn: () -> ()) throws {}
+// CHECK-LABEL: define {{.*}}testForceTry
+// CHECK: call swiftcc void @swift_unexpectedError(%swift.error*
+func testForceTry(_ fn: () -> ()) {
+  try! createInt(fn)
+}
+
+// CHECK-LABEL: declare swiftcc void @swift_unexpectedError(%swift.error*
+
+enum MyError : Error {
+  case A, B
+}
+
+throw MyError.A
+
+
+
 // CHECK: ![[R]] = !{i64 0, i64 9223372036854775807}
 
diff --git a/test/IRGen/c_layout.sil b/test/IRGen/c_layout.sil
index 250e18f..0455530 100644
--- a/test/IRGen/c_layout.sil
+++ b/test/IRGen/c_layout.sil
@@ -126,11 +126,11 @@
 }
 
 // CHECK-x86_64-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-x86_64:         call signext i8 @chareth(i8 signext %0)
-// CHECK-x86_64:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-x86_64:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-x86_64:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-x86_64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-x86_64:         call signext i8 @chareth(i8 signext
+// CHECK-x86_64:         call signext i8 @signedChareth(i8 signext
+// CHECK-x86_64:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-x86_64:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-x86_64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-x86_64:         call i32 @ints(i32 %5)
 // CHECK-x86_64:         call i32 @unsigneds(i32 %6)
 // CHECK-x86_64-LABEL: declare signext i8 @chareth(i8 signext)
@@ -142,11 +142,11 @@
 // CHECK-x86_64-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-i386-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-i386:         call signext i8 @chareth(i8 signext %0)
-// CHECK-i386:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-i386:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-i386:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-i386:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-i386:         call signext i8 @chareth(i8 signext
+// CHECK-i386:         call signext i8 @signedChareth(i8 signext
+// CHECK-i386:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-i386:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-i386:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-i386:         call i32 @ints(i32 %5)
 // CHECK-i386:         call i32 @unsigneds(i32 %6)
 // CHECK-i386-LABEL: declare signext i8 @chareth(i8 signext)
@@ -158,11 +158,11 @@
 // CHECK-i386-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-armv7-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-armv7:         call signext i8 @chareth(i8 signext %0)
-// CHECK-armv7:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-armv7:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-armv7:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-armv7:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-armv7:         call signext i8 @chareth(i8 signext
+// CHECK-armv7:         call signext i8 @signedChareth(i8 signext
+// CHECK-armv7:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-armv7:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-armv7:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-armv7:         call i32 @ints(i32 %5)
 // CHECK-armv7:         call i32 @unsigneds(i32 %6)
 // CHECK-armv7-LABEL: declare signext i8 @chareth(i8 signext)
@@ -174,11 +174,11 @@
 // CHECK-armv7-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-armv7s-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-armv7s:         call signext i8 @chareth(i8 signext %0)
-// CHECK-armv7s:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-armv7s:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-armv7s:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-armv7s:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-armv7s:         call signext i8 @chareth(i8 signext
+// CHECK-armv7s:         call signext i8 @signedChareth(i8 signext
+// CHECK-armv7s:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-armv7s:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-armv7s:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-armv7s:         call i32 @ints(i32 %5)
 // CHECK-armv7s:         call i32 @unsigneds(i32 %6)
 // CHECK-armv7s-LABEL: declare signext i8 @chareth(i8 signext)
@@ -190,11 +190,11 @@
 // CHECK-armv7s-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-armv7k-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-armv7k:         call signext i8 @chareth(i8 signext %0)
-// CHECK-armv7k:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-armv7k:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-armv7k:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-armv7k:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-armv7k:         call signext i8 @chareth(i8 signext
+// CHECK-armv7k:         call signext i8 @signedChareth(i8 signext
+// CHECK-armv7k:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-armv7k:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-armv7k:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-armv7k:         call i32 @ints(i32 %5)
 // CHECK-armv7k:         call i32 @unsigneds(i32 %6)
 // CHECK-armv7k-LABEL: declare signext i8 @chareth(i8 signext)
@@ -206,11 +206,11 @@
 // CHECK-armv7k-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-arm64-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-arm64:         call signext i8 @chareth(i8 signext %0)
-// CHECK-arm64:         call signext i8 @signedChareth(i8 signext %1)
-// CHECK-arm64:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-arm64:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-arm64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-arm64:         call signext i8 @chareth(i8 signext
+// CHECK-arm64:         call signext i8 @signedChareth(i8 signext
+// CHECK-arm64:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-arm64:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-arm64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-arm64:         call i32 @ints(i32 %5)
 // CHECK-arm64:         call i32 @unsigneds(i32 %6)
 // CHECK-arm64-LABEL: declare signext i8 @chareth(i8 signext)
@@ -238,11 +238,11 @@
 // CHECK-aarch64-LABEL: declare i32 @unsigneds(i32)
 
 // CHECK-powerpc64-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-powerpc64:         call zeroext i8 @chareth(i8 zeroext %0)
-// CHECK-powerpc64:         call zeroext i8 @signedChareth(i8 zeroext %1)
-// CHECK-powerpc64:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-powerpc64:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-powerpc64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-powerpc64:         call zeroext i8 @chareth(i8 zeroext
+// CHECK-powerpc64:         call zeroext i8 @signedChareth(i8 zeroext
+// CHECK-powerpc64:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-powerpc64:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-powerpc64:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-powerpc64:         call signext i32 @ints(i32 signext %5)
 // CHECK-powerpc64:         call zeroext i32 @unsigneds(i32 zeroext %6)
 // CHECK-powerpc64-LABEL: declare zeroext i8 @chareth(i8 zeroext)
@@ -254,11 +254,11 @@
 // CHECK-powerpc64-LABEL: declare zeroext i32 @unsigneds(i32 zeroext)
 
 // CHECK-powerpc64le-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-powerpc64le:         call zeroext i8 @chareth(i8 zeroext %0)
-// CHECK-powerpc64le:         call zeroext i8 @signedChareth(i8 zeroext %1)
-// CHECK-powerpc64le:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-powerpc64le:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-powerpc64le:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-powerpc64le:         call zeroext i8 @chareth(i8 zeroext
+// CHECK-powerpc64le:         call zeroext i8 @signedChareth(i8 zeroext
+// CHECK-powerpc64le:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-powerpc64le:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-powerpc64le:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-powerpc64le:         call signext i32 @ints(i32 signext %5)
 // CHECK-powerpc64le:         call zeroext i32 @unsigneds(i32 zeroext %6)
 // CHECK-powerpc64le-LABEL: declare zeroext i8 @chareth(i8 zeroext)
@@ -270,11 +270,11 @@
 // CHECK-powerpc64le-LABEL: declare zeroext i32 @unsigneds(i32 zeroext)
 
 // CHECK-s390x-LABEL: define{{( protected)?}} swiftcc void @testIntegerExtension
-// CHECK-s390x:         call zeroext i8 @chareth(i8 zeroext %0)
-// CHECK-s390x:         call zeroext i8 @signedChareth(i8 zeroext %1)
-// CHECK-s390x:         call zeroext i8 @unsignedChareth(i8 zeroext %2)
-// CHECK-s390x:         call signext i16 @eatMyShorts(i16 signext %3)
-// CHECK-s390x:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext %4)
+// CHECK-s390x:         call zeroext i8 @chareth(i8 zeroext
+// CHECK-s390x:         call zeroext i8 @signedChareth(i8 zeroext
+// CHECK-s390x:         call zeroext i8 @unsignedChareth(i8 zeroext
+// CHECK-s390x:         call signext i16 @eatMyShorts(i16 signext
+// CHECK-s390x:         call zeroext i16 @eatMyUnsignedShorts(i16 zeroext
 // CHECK-s390x:         call signext i32 @ints(i32 signext %5)
 // CHECK-s390x:         call zeroext i32 @unsigneds(i32 zeroext %6)
 // CHECK-s390x-LABEL: declare zeroext i8 @chareth(i8 zeroext)
diff --git a/test/IRGen/unexploded-calls.swift b/test/IRGen/unexploded-calls.swift
new file mode 100644
index 0000000..e9046f4
--- /dev/null
+++ b/test/IRGen/unexploded-calls.swift
@@ -0,0 +1,34 @@
+// RUN: %swift -target thumbv7-unknown-windows-msvc -parse-stdlib -parse-as-library -I %S/Inputs/usr/include -module-name Swift -S -emit-ir -o - %s | %FileCheck %s
+// RUN: %swift -target thumbv7-unknown-linux-gnueabihf -parse-stdlib -parse-as-library -I %S/Inputs/usr/include -module-name Swift -S -emit-ir -o - %s | %FileCheck %s
+// RUN: %swift -target thumbv7-unknown-linux-gnueabi -Xcc -mfloat-abi=hard -parse-stdlib -parse-as-library -I %S/Inputs/usr/include -module-name Swift -S -emit-ir -o - %s | %FileCheck %s
+
+// REQUIRES: CODEGENERATOR=ARM
+
+struct Float {
+  let _value: Builtin.FPIEEE32
+}
+
+typealias CFloat = Float
+typealias Void = ()
+
+import SRoA
+
+public func g(_ s : S) {
+  return f(s)
+}
+
+// CHECK: define {{.*}}swiftcc void @_T0s1gySC1SVF(float, float) {{.*}}{
+// CHECK: entry:
+// CHECK:   [[ALLOCA:%[-._0-9a-zA-Z]+]] = alloca %TSC1SV, align 4
+// CHECK:   %2 = bitcast %TSC1SV* [[ALLOCA]] to i8*
+// CHECK:   [[ALLOCA]].f = getelementptr inbounds %TSC1SV, %TSC1SV* [[ALLOCA]], i32 0, i32 0
+// CHECK:   [[ALLOCA]].f._value = getelementptr inbounds %TSf, %TSf* [[ALLOCA]].f, i32 0, i32 0
+// CHECK:   store float %0, float* [[ALLOCA]].f._value, align 4
+// CHECK:   [[ALLOCA]].g = getelementptr inbounds %TSC1SV, %TSC1SV* [[ALLOCA]], i32 0, i32 1
+// CHECK:   [[ALLOCA]].g._value = getelementptr inbounds %TSf, %TSf* [[ALLOCA]].g, i32 0, i32 0
+// CHECK:   store float %1, float* [[ALLOCA]].g._value, align 4
+// CHECK:   %3 = bitcast %TSC1SV* [[ALLOCA]] to %struct.S*
+// CHECK:   %4 = load %struct.S, %struct.S* %3, align 4
+// CHECK:   call void @f(%struct.S %4)
+// CHECK: }
+
diff --git a/test/Index/roles.swift b/test/Index/roles.swift
index 76fe95a..8c8acaf 100644
--- a/test/Index/roles.swift
+++ b/test/Index/roles.swift
@@ -313,6 +313,12 @@
   func foo() -> Int { return 1 }
 }
 
+protocol ExtendMe {}
+protocol Whatever {}
+// CHECK: [[@LINE-1]]:10 | protocol/Swift | Whatever | [[Whatever_USR:.*]] | Def | rel: 0
+extension ExtendMe where Self: Whatever {}
+// CHECK: [[@LINE-1]]:32 | protocol/Swift | Whatever | [[Whatever_USR]] | Ref | rel: 0
+
 var anInstance = AClass(x: 1)
 // CHECK: [[@LINE-1]]:18 | class/Swift | AClass | s:14swift_ide_test6AClassC | Ref | rel: 0
 // CHECK: [[@LINE-2]]:18 | constructor/Swift | init(x:) | s:14swift_ide_test6AClassCACSi1x_tcfc | Ref,Call | rel: 0
diff --git a/test/Parse/errors.swift b/test/Parse/errors.swift
index 8fb0ecf..628ae9d 100644
--- a/test/Parse/errors.swift
+++ b/test/Parse/errors.swift
@@ -4,8 +4,8 @@
   case Foo, Bar, Baz
   case CarriesInt(Int)
 
-  var domain: String { return "" }
-  var code: Int { return 0 }
+  var _domain: String { return "" }
+  var _code: Int { return 0 }
 }
 
 func opaque_error() -> Error { return MSV.Foo }
diff --git a/test/SIL/Serialization/witness_tables.sil b/test/SIL/Serialization/witness_tables.sil
index bedac29..d8e8e61 100644
--- a/test/SIL/Serialization/witness_tables.sil
+++ b/test/SIL/Serialization/witness_tables.sil
@@ -73,14 +73,6 @@
   associated_type_protocol (AssocWithReqt: AssocReqt): ConformingAssoc: AssocReqt module witness_tables
 }
 
-// CHECK-LABEL: sil_witness_table InheritedConformance2: InheritedProtocol1 module witness_tables
-// CHECK-NOT: }
-sil_witness_table InheritedConformance2: InheritedProtocol1 module witness_tables
-// CHECK-LABEL: sil_witness_table InheritedConformance2: AnyProtocol module witness_tables
-// CHECK-NOT: }
-sil_witness_table InheritedConformance2: AnyProtocol module witness_tables
-
-
 protocol Proto {
 	func abc()
 }
diff --git a/test/SILGen/Inputs/usr/include/BridgeTestFoundation.h b/test/SILGen/Inputs/usr/include/BridgeTestFoundation.h
index c09b5da..fc360c6 100644
--- a/test/SILGen/Inputs/usr/include/BridgeTestFoundation.h
+++ b/test/SILGen/Inputs/usr/include/BridgeTestFoundation.h
@@ -89,3 +89,7 @@
 void noescapeBlockAlias(__attribute__((noescape)) dispatch_block_t block);
 void noescapeNonnullBlockAlias(__attribute__((noescape)) _Nonnull dispatch_block_t block);
 void escapeBlockAlias(dispatch_block_t block);
+
+@interface ObjectWithSplitProperty : NSObject
+@property (nonatomic, setter=private_setFlagForSomething:) BOOL flagForSomething;
+@end
diff --git a/test/SILGen/objc_properties.swift b/test/SILGen/objc_properties.swift
index 7fd6c2b..941fbb5 100644
--- a/test/SILGen/objc_properties.swift
+++ b/test/SILGen/objc_properties.swift
@@ -248,3 +248,11 @@
 // CHECK-LABEL: sil hidden @_T015objc_properties15HasLazyPropertyC6windowSo8NSObjectCSgvg : $@convention(method) (@guaranteed HasLazyProperty) -> @owned Optional<NSObject> {
 // CHECK: class_method %0 : $HasLazyProperty, #HasLazyProperty.instanceMethod!1 : (HasLazyProperty) -> () -> NSObject?
 // CHECK: return
+
+//   The way we import this setter splits the name into the parameter list,
+//   which can cause fits for SILGenApply the way it's currently implemented.
+// CHECK-LABEL: sil hidden @_T015objc_properties26testPropSetWithPreposition
+func testPropSetWithPreposition(object: ObjectWithSplitProperty?) {
+  // CHECK: #ObjectWithSplitProperty.flagForSomething!setter.1.foreign : (ObjectWithSplitProperty) -> (Bool) -> (), $@convention(objc_method) ({{Bool|ObjCBool}}, ObjectWithSplitProperty) -> ()
+  object?.flagForSomething = false
+}
diff --git a/test/SILOptimizer/bridged_casts_folding.swift b/test/SILOptimizer/bridged_casts_folding.swift
index 25f5f12..ccd0ab6 100644
--- a/test/SILOptimizer/bridged_casts_folding.swift
+++ b/test/SILOptimizer/bridged_casts_folding.swift
@@ -2,9 +2,6 @@
 
 // REQUIRES: objc_interop
 
-// FIXME: https://bugs.swift.org/browse/SR-2808
-// XFAIL: resilient_stdlib
-
 // Check that casts between bridged types are replaced by more 
 // efficient code sequences.
 // 
diff --git a/test/SILOptimizer/cast_folding_no_bridging.sil b/test/SILOptimizer/cast_folding_no_bridging.sil
index 47dffe2..88c00cf 100644
--- a/test/SILOptimizer/cast_folding_no_bridging.sil
+++ b/test/SILOptimizer/cast_folding_no_bridging.sil
@@ -1,9 +1,6 @@
 // RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -O -emit-sil %s | %FileCheck %s
 // REQUIRES: objc_interop
 
-// FIXME: https://bugs.swift.org/browse/SR-2808
-// XFAIL: resilient_stdlib
-
 // We want to check that casts between two types which are both Swift types or
 // both are ObjC types are not optimized by the cast optimizer into casts
 // to/from ObjC.
diff --git a/test/SILOptimizer/globalopt-iter.sil b/test/SILOptimizer/globalopt-iter.sil
new file mode 100644
index 0000000..1be24b1
--- /dev/null
+++ b/test/SILOptimizer/globalopt-iter.sil
@@ -0,0 +1,22 @@
+// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all %s -global-opt | %FileCheck %s
+
+
+import Builtin
+import Swift
+class B { }
+class E : B { }
+
+// CHECK: sil @patatino : $@convention(thin) () -> () {
+// CHECK: bb0:
+// CHECK:  %0 = global_value @patatinoTv_ : $B             // user: %1
+// CHECK:  strong_retain %0 : $B                           // id: %1
+// CHECK: %2 = tuple ()                                   // user: %3
+// CHECK: return %2 : $()                                 // id: %3
+// CHECK: }
+
+sil @patatino : $@convention(thin) () -> () {
+  %1 = alloc_ref [stack] $B
+  dealloc_ref [stack] %1 : $B
+  %45 = tuple ()
+  return %45 : $()
+}
diff --git a/test/SILOptimizer/inliner_coldblocks.sil b/test/SILOptimizer/inliner_coldblocks.sil
index a627c37..18ec8a5 100644
--- a/test/SILOptimizer/inliner_coldblocks.sil
+++ b/test/SILOptimizer/inliner_coldblocks.sil
@@ -2,9 +2,12 @@
 // RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all %s -inline -sil-remarks=sil-inliner -o %t.sil 2>&1 | %FileCheck -check-prefix=REMARKS_PASSED %s
 // RUN: %FileCheck %s < %t.sil
 // RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all %s -inline -sil-remarks-missed=sil-inliner -o /dev/null 2>&1 | %FileCheck -check-prefix=REMARKS_MISSED %s
+// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -enable-sil-verify-all %s -inline -save-optimization-record-path %t.yaml -o /dev/null 2>&1 | %FileCheck -allow-empty -check-prefix=NO_REMARKS %s
+// RUN: %FileCheck -check-prefix=YAML %s < %t.yaml
 
 // REMARKS_PASSED-NOT: remark:
 // REMARKS_MISSED-NOT: remark:
+// NO_REMARKS-NOT: remark:
 
 sil_stage canonical
 
@@ -190,7 +193,33 @@
   %c30 = builtin "assert_configuration"() : $Builtin.Int32
 
   %f = function_ref @update_global: $@convention(thin) () -> ()
-  // REMARKS_PASSED: inliner_coldblocks.sil:194:3: remark: update_global inlined into regular_large_callee (cost = {{.*}}, benefit = {{.*}})
+  // REMARKS_PASSED: inliner_coldblocks.sil:223:3: remark: update_global inlined into regular_large_callee (cost = {{.*}}, benefit = {{.*}})
+  // YAML:      --- !Passed
+  // YAML-NEXT: Pass:            sil-inliner
+  // YAML-NEXT: Name:            Inlined
+  // YAML-NEXT: DebugLoc:
+  // YAML-NEXT:   File:            {{.*}}inliner_coldblocks.sil
+  // YAML-NEXT:   Line:            223
+  // YAML-NEXT:   Column:          3
+  // YAML-NEXT: Function:        regular_large_callee
+  // YAML-NEXT: Args:
+  // YAML-NEXT:   - Callee:          update_global
+  // YAML-NEXT:     DebugLoc:
+  // YAML-NEXT:       File:            {{.*}}inliner_coldblocks.sil
+  // YAML-NEXT:       Line:            20
+  // YAML-NEXT:       Column:          6
+  // YAML-NEXT:   - String:          ' inlined into '
+  // YAML-NEXT:   - Caller:          regular_large_callee
+  // YAML-NEXT:     DebugLoc:
+  // YAML-NEXT:       File:            {{.*}}inliner_coldblocks.sil
+  // YAML-NEXT:       Line:            162
+  // YAML-NEXT:       Column:          6
+  // YAML-NEXT:   - String:          ' (cost = '
+  // YAML-NEXT:   - Cost:            '{{.*}}'
+  // YAML-NEXT:   - String:          ', benefit = '
+  // YAML-NEXT:   - Benefit:         '{{.*}}'
+  // YAML-NEXT:   - String:          ')'
+  // YAML-NEXT: ...
   apply %f() : $@convention(thin) () -> ()
 
   %r = tuple ()
@@ -204,7 +233,22 @@
 sil @dont_inline_regular_large_callee : $@convention(thin) () -> () {
 bb0:
   %f = function_ref @regular_large_callee : $@convention(thin) () -> ()
-  // REMARKS_MISSED: inliner_coldblocks.sil:208:8: remark: Not profitable to inline (cost = {{.*}}, benefit = {{.*}})
+  // REMARKS_MISSED: inliner_coldblocks.sil:252:8: remark: Not profitable to inline (cost = {{.*}}, benefit = {{.*}})
+  // YAML:      --- !Missed
+  // YAML-NEXT: Pass:            sil-inliner
+  // YAML-NEXT: Name:            NoInlinedCost
+  // YAML-NEXT: DebugLoc:
+  // YAML-NEXT:   File:            {{.*}}inliner_coldblocks.sil
+  // YAML-NEXT:   Line:            252
+  // YAML-NEXT:   Column:          8
+  // YAML-NEXT: Function:        dont_inline_regular_large_callee
+  // YAML-NEXT: Args:
+  // YAML-NEXT:   - String:          'Not profitable to inline (cost = '
+  // YAML-NEXT:   - Cost:            '{{.*}}'
+  // YAML-NEXT:   - String:          ', benefit = '
+  // YAML-NEXT:   - Benefit:         '{{.*}}'
+  // YAML-NEXT:   - String:          ')'
+  // YAML-NEXT: ...
   %a = apply %f() : $@convention(thin) () -> ()
   %r = tuple ()
   return %r : $()
diff --git a/test/SILOptimizer/predictable_memopt.sil b/test/SILOptimizer/predictable_memopt.sil
index f798389..4e2ccdf 100644
--- a/test/SILOptimizer/predictable_memopt.sil
+++ b/test/SILOptimizer/predictable_memopt.sil
@@ -327,7 +327,7 @@
   %9 = apply %f() : $@convention(thin) () -> @owned SomeClass
   // CHECK: [[CVAL:%[0-9]+]] = apply
 
-  assign %9 to %3 : $*SomeClass
+  store %9 to %3 : $*SomeClass
   destroy_addr %3 : $*SomeClass
   dealloc_stack %3 : $*SomeClass
   %15 = tuple ()
diff --git a/test/SILOptimizer/prespecialize.swift b/test/SILOptimizer/prespecialize.swift
index b8d0c00..bddb2b9 100644
--- a/test/SILOptimizer/prespecialize.swift
+++ b/test/SILOptimizer/prespecialize.swift
@@ -2,9 +2,6 @@
 
 // REQUIRES: optimized_stdlib
 
-// FIXME: https://bugs.swift.org/browse/SR-2808
-// XFAIL: resilient_stdlib
-
 // Check that pre-specialization works at -Onone.
 // This test requires the standard library to be compiled with pre-specializations!
 
diff --git a/test/Sema/diag_deprecated_iuo.swift b/test/Sema/diag_deprecated_iuo.swift
new file mode 100644
index 0000000..6fc6525
--- /dev/null
+++ b/test/Sema/diag_deprecated_iuo.swift
@@ -0,0 +1,45 @@
+// RUN: %target-typecheck-verify-swift
+
+let _: ImplicitlyUnwrappedOptional<Int> = 1 // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{8-36=}}{{39-39=!}}{{39-40=}}
+let _: ImplicitlyUnwrappedOptional = 1 // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+
+extension ImplicitlyUnwrappedOptional {} // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+
+func function(
+  _: ImplicitlyUnwrappedOptional<Int> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{6-34=}}{{37-37=!}}{{37-38=}}
+) -> ImplicitlyUnwrappedOptional<Int> { // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{6-34=}}{{37-37=!}}{{37-38=}}
+  return 1
+}
+
+func genericFunction<T>(
+  iuo: ImplicitlyUnwrappedOptional<T> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{8-36=}}{{37-37=!}}{{37-38=}}
+) -> ImplicitlyUnwrappedOptional<T> { // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{6-34=}}{{35-35=!}}{{35-36=}}
+  return iuo
+}
+
+protocol P {
+  associatedtype T
+  associatedtype U
+}
+
+struct S : P {
+  typealias T = ImplicitlyUnwrappedOptional<Int> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+  typealias U = Optional<ImplicitlyUnwrappedOptional<Int>> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+
+  subscript (
+    index: ImplicitlyUnwrappedOptional<Int> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{12-40=}}{{43-43=!}}{{43-44=}}
+  )     -> ImplicitlyUnwrappedOptional<Int> { // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{12-40=}}{{43-43=!}}{{43-44=}}
+    return index
+  }
+}
+
+func generic<T : P>(_: T) where T.T == ImplicitlyUnwrappedOptional<Int> { } // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+func genericOptIUO<T : P>(_: T) where T.U == Optional<ImplicitlyUnwrappedOptional<Int>> {} // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+
+func testClosure() -> Int {
+  return {
+    (i: ImplicitlyUnwrappedOptional<Int>) // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{9-37=}}{{40-40=!}}{{40-41=}}
+     -> ImplicitlyUnwrappedOptional<Int> in // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
+    return i
+  }(1)
+}
diff --git a/test/Sema/diag_erroneous_iuo.swift b/test/Sema/diag_erroneous_iuo.swift
new file mode 100644
index 0000000..b2df2c2
--- /dev/null
+++ b/test/Sema/diag_erroneous_iuo.swift
@@ -0,0 +1,60 @@
+// RUN: %target-typecheck-verify-swift -swift-version 5
+
+let _: ImplicitlyUnwrappedOptional<Int> = 1 // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{8-36=}}{{39-39=!}}{{39-40=}}
+let _: ImplicitlyUnwrappedOptional = 1 // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' in unsupported; use an explicit type followed by '!'}}
+
+extension ImplicitlyUnwrappedOptional {} // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+
+func function(
+  _: ImplicitlyUnwrappedOptional<Int> // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{6-34=}}{{37-37=!}}{{37-38=}}
+) -> ImplicitlyUnwrappedOptional<Int> { // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{6-34=}}{{37-37=!}}{{37-38=}}
+  return 1
+}
+
+func genericFunction<T>(
+  iuo: ImplicitlyUnwrappedOptional<T> // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{8-36=}}{{37-37=!}}{{37-38=}}
+) -> ImplicitlyUnwrappedOptional<T> { // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{6-34=}}{{35-35=!}}{{35-36=}}
+  return iuo
+}
+
+protocol P {
+  associatedtype T
+  associatedtype U
+}
+
+struct S : P {
+  typealias T = ImplicitlyUnwrappedOptional<Int> // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+  typealias U = Optional<ImplicitlyUnwrappedOptional<Int>> // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+
+  subscript (
+    index: ImplicitlyUnwrappedOptional<Int> // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{12-40=}}{{43-43=!}}{{43-44=}}
+  )     -> ImplicitlyUnwrappedOptional<Int> { // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{12-40=}}{{43-43=!}}{{43-44=}}
+    return index
+  }
+}
+
+func generic<T : P>(_: T) where T.T == ImplicitlyUnwrappedOptional<Int> { } // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+func genericOptIUO<T : P>(_: T) where T.U == Optional<ImplicitlyUnwrappedOptional<Int>> {} // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+
+func testClosure() -> Int {
+  return {
+    (i: ImplicitlyUnwrappedOptional<Int>) // expected-error {{the spelling 'ImplicitlyUnwrappedOptional' is unsupported; use '!' after the type name}}{{9-37=}}{{40-40=!}}{{40-41=}}
+     -> ImplicitlyUnwrappedOptional<Int> in // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+    return i
+  }(1)
+}
+
+_ = Array<Int!>() // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+_ = [Int!]() // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+_ = Optional<Int!>(nil) // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+_ = Int!?(0) // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+_ = (
+  Int!, // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+  Float!, // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+  String! // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+)(1, 2.0, "3")
+
+struct Generic<T, U, C> {}
+_ = Generic<Int!, // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+            Float!, // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
+            String!>() // expected-error {{implicitly unwrapped optionals are only allowed at top level and as function results}}
diff --git a/test/Serialization/Inputs/def_basic.sil b/test/Serialization/Inputs/def_basic.sil
index e0559ff..8701d1b 100644
--- a/test/Serialization/Inputs/def_basic.sil
+++ b/test/Serialization/Inputs/def_basic.sil
@@ -1319,10 +1319,10 @@
 }
 
 
-// CHECK-LABEL: sil_witness_table public_external ConformingAssoc: AssocReqt module
+// CHECK-LABEL: sil_witness_table public_external [serialized] ConformingAssoc: AssocReqt module
 // CHECK: #AssocReqt.requiredMethod!1: {{.*}} : @_TTWV14witness_tables15ConformingAssocS_9AssocReqtS_FS1_14requiredMethodU_fRQPS1_FT_T_
 // CHECK: }
-sil_witness_table ConformingAssoc: AssocReqt module def_basic {
+sil_witness_table [serialized] ConformingAssoc: AssocReqt module def_basic {
   method #AssocReqt.requiredMethod!1: @_TTWV14witness_tables15ConformingAssocS_9AssocReqtS_FS1_14requiredMethodU_fRQPS1_FT_T_
 }
 
@@ -1345,18 +1345,18 @@
   func inheritedMethod()
 }
 
-// CHECK-LABEL: sil_witness_table public_external InheritedConformance: InheritedProtocol1 module
+// CHECK-LABEL: sil_witness_table public_external [serialized] InheritedConformance: InheritedProtocol1 module
 // CHECK: base_protocol AnyProtocol: InheritedConformance: AnyProtocol module
 // CHECK: }
-sil_witness_table InheritedConformance: InheritedProtocol1 module witness_tables {
+sil_witness_table [serialized] InheritedConformance: InheritedProtocol1 module witness_tables {
   base_protocol AnyProtocol: InheritedConformance: AnyProtocol module witness_tables
 }
 
-// CHECK-LABEL: sil_witness_table public_external InheritedConformance: AnyProtocol module
+// CHECK-LABEL: sil_witness_table public_external [serialized] InheritedConformance: AnyProtocol module
 // CHECK: associated_type AssocType: SomeAssoc
 // CHECK: associated_type_protocol (AssocWithReqt: AssocReqt): ConformingAssoc: AssocReqt module
 // CHECK: }
-sil_witness_table InheritedConformance: AnyProtocol module witness_tables {
+sil_witness_table [serialized] InheritedConformance: AnyProtocol module witness_tables {
   associated_type AssocType: SomeAssoc
   associated_type_protocol (AssocWithReqt: AssocReqt): ConformingAssoc: AssocReqt module witness_tables
 }
diff --git a/test/SourceKit/DocSupport/doc_swift_module.swift.response b/test/SourceKit/DocSupport/doc_swift_module.swift.response
index ea22b0d..4683135 100644
--- a/test/SourceKit/DocSupport/doc_swift_module.swift.response
+++ b/test/SourceKit/DocSupport/doc_swift_module.swift.response
@@ -1039,7 +1039,7 @@
     key.length: 4
   },
   {
-    key.kind: source.lang.swift.syntaxtype.identifier,
+    key.kind: source.lang.swift.syntaxtype.typeidentifier,
     key.offset: 1207,
     key.length: 7
   },
diff --git a/test/attr/attr_autoclosure.swift b/test/attr/attr_autoclosure.swift
index 4b85666..ecf4236 100644
--- a/test/attr/attr_autoclosure.swift
+++ b/test/attr/attr_autoclosure.swift
@@ -42,10 +42,10 @@
 }
 
 protocol P1 {
-  associatedtype Element // expected-note{{declared here}}
+  associatedtype Element
 }
 protocol P2 : P1 {
-  associatedtype Element // expected-warning{{redeclaration of associated type 'Element'}}
+  associatedtype Element
 }
 
 func overloadedEach<O: P1>(_ source: O, _ closure: @escaping () -> ()) {
diff --git a/test/attr/attr_escaping.swift b/test/attr/attr_escaping.swift
index 863fa30..4b7892a 100644
--- a/test/attr/attr_escaping.swift
+++ b/test/attr/attr_escaping.swift
@@ -121,7 +121,7 @@
   func setIUOClosure(_ fn: () -> Void) { // expected-note {{parameter 'fn' is implicitly non-escaping}} {{28-28=@escaping }}
     iuoClosure = fn // expected-error{{assigning non-escaping parameter 'fn' to an @escaping closure}}
   }
-  var iuoClosureExplicit: ImplicitlyUnwrappedOptional<() -> Void>
+  var iuoClosureExplicit: ImplicitlyUnwrappedOptional<() -> Void> // expected-warning {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}
   func setExplicitIUOClosure(_ fn: () -> Void) { // expected-note {{parameter 'fn' is implicitly non-escaping}} {{36-36=@escaping }}
     iuoClosureExplicit = fn // expected-error{{assigning non-escaping parameter 'fn' to an @escaping closure}}
   }
diff --git a/test/attr/attr_noescape.swift b/test/attr/attr_noescape.swift
index 6f29afa..4ed3535 100644
--- a/test/attr/attr_noescape.swift
+++ b/test/attr/attr_noescape.swift
@@ -183,10 +183,10 @@
 
 
 protocol P1 {
-  associatedtype Element // expected-note{{declared here}}
+  associatedtype Element
 }
 protocol P2 : P1 {
-  associatedtype Element // expected-warning{{redeclaration of associated type 'Element'}}
+  associatedtype Element
 }
 
 func overloadedEach<O: P1, T>(_ source: O, _ transform: @escaping (O.Element) -> (), _: T) {}
diff --git a/test/decl/class/override.swift b/test/decl/class/override.swift
index 565f865..77ce63f 100644
--- a/test/decl/class/override.swift
+++ b/test/decl/class/override.swift
@@ -221,8 +221,10 @@
   // expected-note@-1 {{remove '!' to make the parameter required}} {{36-37=}}
   // expected-note@-2 {{add parentheses to silence this warning}} {{27-27=(}} {{37-37=)}}
 
-  override func oneB(x: ImplicitlyUnwrappedOptional<AnyObject>) {}  // expected-warning {{overriding instance method parameter of type 'AnyObject' with implicitly unwrapped optional type 'ImplicitlyUnwrappedOptional<AnyObject>'}}
-  // expected-note@-1 {{add parentheses to silence this warning}} {{25-25=(}} {{63-63=)}}
+  override func oneB(x: ImplicitlyUnwrappedOptional<AnyObject>) {}
+  // expected-warning@-1 {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated; use '!' after the type name}}{{25-53=}}{{62-62=!}}{{62-63=}}
+  // expected-warning@-2 {{overriding instance method parameter of type 'AnyObject' with implicitly unwrapped optional type 'ImplicitlyUnwrappedOptional<AnyObject>'}}
+  // expected-note@-3 {{add parentheses to silence this warning}} {{25-25=(}} {{63-63=)}}
 
   override func oneC(_: AnyObject!) {} // expected-warning {{overriding instance method parameter of type 'AnyObject' with implicitly unwrapped optional type 'AnyObject!'}}
   // expected-note@-1 {{remove '!' to make the parameter required}} {{34-35=}}
diff --git a/test/decl/ext/protocol.swift b/test/decl/ext/protocol.swift
index 73e3ac4..117a0b5 100644
--- a/test/decl/ext/protocol.swift
+++ b/test/decl/ext/protocol.swift
@@ -510,7 +510,7 @@
 protocol PConforms8 {
   associatedtype Assoc
 
-  func method() -> Assoc
+  func method() -> Assoc // expected-note{{requirement 'method()' declared here}}
   var property: Assoc { get }
   subscript (i: Assoc) -> Assoc { get }
 }
@@ -535,7 +535,10 @@
 }
 
 struct SConforms8c : PConforms8 { 
-  func method() -> String { return "" }
+  func method() -> String { return "" } // expected-warning{{instance method 'method()' nearly matches defaulted requirement 'method()' of protocol 'PConforms8'}}
+  // expected-note@-1{{candidate has non-matching type '() -> String' [with Assoc = Int]}}
+  // expected-note@-2{{move 'method()' to an extension to silence this warning}}
+  // expected-note@-3{{make 'method()' private to silence this warning}}
 }
 
 func testSConforms8c() {
diff --git a/test/decl/protocol/indirectly_recursive_requirement.swift b/test/decl/protocol/indirectly_recursive_requirement.swift
index 90f0c7a..5c40a24 100644
--- a/test/decl/protocol/indirectly_recursive_requirement.swift
+++ b/test/decl/protocol/indirectly_recursive_requirement.swift
@@ -5,7 +5,7 @@
 }
 
 protocol _ForwardIndex  {
-  associatedtype Distance  = MyInt // expected-note{{declared here}}
+  associatedtype Distance  = MyInt
 }
 
 protocol ForwardIndex : _ForwardIndex {
@@ -19,7 +19,7 @@
 }
 
 protocol _RandomAccessIndex : _BidirectionalIndex {
-  associatedtype Distance // expected-warning{{redeclaration of associated type 'Distance}}
+  associatedtype Distance
 }
 
 protocol RandomAccessIndex 
diff --git a/test/multifile/class-layout/final-stored-property/library.swift b/test/multifile/class-layout/final-stored-property/Inputs/library.swift
similarity index 84%
rename from test/multifile/class-layout/final-stored-property/library.swift
rename to test/multifile/class-layout/final-stored-property/Inputs/library.swift
index 687a15f..2e67157 100644
--- a/test/multifile/class-layout/final-stored-property/library.swift
+++ b/test/multifile/class-layout/final-stored-property/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 final class Burger {
   let onions: Bool = true
   let cheeseSlices: Int = 0
diff --git a/test/multifile/class-layout/final-stored-property/main.swift b/test/multifile/class-layout/final-stored-property/main.swift
index afceade..c45e982 100644
--- a/test/multifile/class-layout/final-stored-property/main.swift
+++ b/test/multifile/class-layout/final-stored-property/main.swift
@@ -1,7 +1,5 @@
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
-
-// REQUIRES: executable_test
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
 func meltCheese(_ burger: Burger) -> Int {
   return burger.cheeseSlices
diff --git a/test/multifile/constant-struct-with-padding/Other.swift b/test/multifile/constant-struct-with-padding/Inputs/other.swift
similarity index 88%
rename from test/multifile/constant-struct-with-padding/Other.swift
rename to test/multifile/constant-struct-with-padding/Inputs/other.swift
index bbab75c..704be3b 100644
--- a/test/multifile/constant-struct-with-padding/Other.swift
+++ b/test/multifile/constant-struct-with-padding/Inputs/other.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 struct t {
   var a = false // (or e.g. var a: Int32 = 0)
   var b = 0.0 // (or e.g. var b: Int64 = 0)
diff --git a/test/multifile/constant-struct-with-padding/main.swift b/test/multifile/constant-struct-with-padding/main.swift
index 22bc152..3f7d5d9 100644
--- a/test/multifile/constant-struct-with-padding/main.swift
+++ b/test/multifile/constant-struct-with-padding/main.swift
@@ -1,5 +1,5 @@
 // RUN: %empty-directory(%t)
 
-// RUN: %target-build-swift -O -whole-module-optimization %S/main.swift %S/Other.swift
+// RUN: %target-build-swift -O -whole-module-optimization %S/main.swift %S/Inputs/other.swift
 
 print( g.a )
diff --git a/test/multifile/constant-tuple-with-padding/Other.swift b/test/multifile/constant-tuple-with-padding/Inputs/other.swift
similarity index 60%
rename from test/multifile/constant-tuple-with-padding/Other.swift
rename to test/multifile/constant-tuple-with-padding/Inputs/other.swift
index dd0dd03..9b48c3d 100644
--- a/test/multifile/constant-tuple-with-padding/Other.swift
+++ b/test/multifile/constant-tuple-with-padding/Inputs/other.swift
@@ -1,3 +1 @@
-// RUN: true
-
 var g = (false, 0.0)
diff --git a/test/multifile/constant-tuple-with-padding/main.swift b/test/multifile/constant-tuple-with-padding/main.swift
index c05a46b..9eb64dd 100644
--- a/test/multifile/constant-tuple-with-padding/main.swift
+++ b/test/multifile/constant-tuple-with-padding/main.swift
@@ -1,5 +1,5 @@
 // RUN: %empty-directory(%t)
 
-// RUN: %target-build-swift -O -whole-module-optimization %S/main.swift %S/Other.swift
+// RUN: %target-build-swift -O -whole-module-optimization %S/main.swift %S/Inputs/other.swift
 
 print( g.0 )
diff --git a/test/multifile/error-type/imported/library.swift b/test/multifile/error-type/imported/Inputs/library.swift
similarity index 76%
rename from test/multifile/error-type/imported/library.swift
rename to test/multifile/error-type/imported/Inputs/library.swift
index e228152..f0955e2 100644
--- a/test/multifile/error-type/imported/library.swift
+++ b/test/multifile/error-type/imported/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public func go() throws {
   throw AXError(0)
 }
diff --git a/test/multifile/error-type/imported/objc_enum_errortype.h b/test/multifile/error-type/imported/Inputs/objc_enum_errortype.h
similarity index 100%
rename from test/multifile/error-type/imported/objc_enum_errortype.h
rename to test/multifile/error-type/imported/Inputs/objc_enum_errortype.h
diff --git a/test/multifile/error-type/imported/main.swift b/test/multifile/error-type/imported/main.swift
index 29d20c1..e4a07bb 100644
--- a/test/multifile/error-type/imported/main.swift
+++ b/test/multifile/error-type/imported/main.swift
@@ -1,4 +1,4 @@
-// RUN: %target-build-swift -module-name objc_enum_errortype -emit-library %S/main.swift %S/library.swift -import-objc-header %S/objc_enum_errortype.h
+// RUN: %target-build-swift -module-name objc_enum_errortype -emit-library %S/main.swift %S/Inputs/library.swift -import-objc-header %S/Inputs/objc_enum_errortype.h
 
 // REQUIRES: objc_interop
 
diff --git a/test/multifile/error-type/one-module/library.swift b/test/multifile/error-type/one-module/Inputs/library.swift
similarity index 79%
rename from test/multifile/error-type/one-module/library.swift
rename to test/multifile/error-type/one-module/Inputs/library.swift
index 75d1068..2842df3 100644
--- a/test/multifile/error-type/one-module/library.swift
+++ b/test/multifile/error-type/one-module/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 enum NuclearMeltdown {
   case Critical
   case Mild
diff --git a/test/multifile/error-type/one-module/main.swift b/test/multifile/error-type/one-module/main.swift
index 383fe63..a7de192 100644
--- a/test/multifile/error-type/one-module/main.swift
+++ b/test/multifile/error-type/one-module/main.swift
@@ -1,8 +1,6 @@
 // Try with and without whole module optimization
 
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
-
-// REQUIRES: executable_test
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
 extension NuclearMeltdown : Error {}
diff --git a/test/multifile/error-type/two-modules/library.swift b/test/multifile/error-type/two-modules/Inputs/library.swift
similarity index 80%
rename from test/multifile/error-type/two-modules/library.swift
rename to test/multifile/error-type/two-modules/Inputs/library.swift
index 76cd46c..2f3a2dd 100644
--- a/test/multifile/error-type/two-modules/library.swift
+++ b/test/multifile/error-type/two-modules/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public enum NuclearMeltdown {
   case Critical
   case Mild
diff --git a/test/multifile/error-type/two-modules/main.swift b/test/multifile/error-type/two-modules/main.swift
index 4a68233..3a2c36a 100644
--- a/test/multifile/error-type/two-modules/main.swift
+++ b/test/multifile/error-type/two-modules/main.swift
@@ -1,12 +1,10 @@
 // RUN: %empty-directory(%t)
 
 // RUN: %empty-directory(%t/linker)
-// RUN: %target-build-swift -emit-module -c %S/library.swift -o %t/linker/library.o
-// RUN: %target-build-swift -emit-library -c %S/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -emit-module -c %S/Inputs/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -emit-library -c %S/Inputs/library.swift -o %t/linker/library.o
 // RUN: %target-build-swift %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main
 
-// REQUIRES: executable_test
-
 import library
 
 extension NuclearMeltdown : Error {}
diff --git a/test/multifile/extensions/two-modules/library.swift b/test/multifile/extensions/two-modules/Inputs/library.swift
similarity index 86%
rename from test/multifile/extensions/two-modules/library.swift
rename to test/multifile/extensions/two-modules/Inputs/library.swift
index e9223e1..6cbaf24 100644
--- a/test/multifile/extensions/two-modules/library.swift
+++ b/test/multifile/extensions/two-modules/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public struct Point {
   public let x: Int
   public let y: Int
diff --git a/test/multifile/extensions/two-modules/main.swift b/test/multifile/extensions/two-modules/main.swift
index 524cf20..4c9e90d 100644
--- a/test/multifile/extensions/two-modules/main.swift
+++ b/test/multifile/extensions/two-modules/main.swift
@@ -1,14 +1,12 @@
 // RUN: %empty-directory(%t)
 
 // RUN: mkdir -p %t/onone %t/wmo
-// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -module-name=library -emit-library %S/library.swift -o %t/onone/library.%target-dylib-extension
+// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -module-name=library -emit-library %S/Inputs/library.swift -o %t/onone/library.%target-dylib-extension
 // RUN: %target-build-swift %S/main.swift %t/onone/library.%target-dylib-extension -I %t/onone/ -o %t/onone/main
 
-// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -module-name=library -emit-library -O -wmo %S/library.swift -o %t/wmo/library.%target-dylib-extension
+// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -module-name=library -emit-library -O -wmo %S/Inputs/library.swift -o %t/wmo/library.%target-dylib-extension
 // RUN: %target-build-swift %S/main.swift %t/wmo/library.%target-dylib-extension -I %t/wmo/ -o %t/wmo/main
 
-// REQUIRES: executable_test
-
 import library
 
 extension Point {
diff --git a/test/multifile/imported-conformance/option-set/library.swift b/test/multifile/imported-conformance/option-set/Inputs/library.swift
similarity index 93%
rename from test/multifile/imported-conformance/option-set/library.swift
rename to test/multifile/imported-conformance/option-set/Inputs/library.swift
index d0894c2..158fc8e 100644
--- a/test/multifile/imported-conformance/option-set/library.swift
+++ b/test/multifile/imported-conformance/option-set/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 import Foundation
 
 @inline(__always)
diff --git a/test/multifile/imported-conformance/option-set/main.swift b/test/multifile/imported-conformance/option-set/main.swift
index fa06202..32db519 100644
--- a/test/multifile/imported-conformance/option-set/main.swift
+++ b/test/multifile/imported-conformance/option-set/main.swift
@@ -1,10 +1,9 @@
 // RUN: %empty-directory(%t)
 
 // RUN: %empty-directory(%t/linker)
-// RUN: %target-build-swift -emit-module -emit-library %S/library.swift -o %t/linker/liblibrary.%target-dylib-extension -emit-module-path %t/linker/library.swiftmodule -module-name library
+// RUN: %target-build-swift -emit-module -emit-library %S/Inputs/library.swift -o %t/linker/liblibrary.%target-dylib-extension -emit-module-path %t/linker/library.swiftmodule -module-name library
 // RUN: %target-build-swift %S/main.swift -I %t/linker/ -L %t/linker/ -llibrary -o %t/linker/main
 
-// REQUIRES: executable_test
 // REQUIRES: objc_interop
 
 import Foundation
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/counter.h b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/counter.h
new file mode 100644
index 0000000..930dc7b
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/counter.h
@@ -0,0 +1,5 @@
+#import <Foundation/Foundation.h>
+
+@interface Counter : NSObject
+@property(readwrite) int value;
+@end
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/library.swift b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/library.swift
new file mode 100644
index 0000000..9211695
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/library.swift
@@ -0,0 +1,12 @@
+import Foundation
+import CounterFramework
+
+public protocol CounterProtocol {
+  var value: Int32 { get set }
+}
+
+extension Counter : CounterProtocol {}
+
+open class MyCounter : Counter {
+  open override var value: Int32 { didSet { } }
+}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/module.map b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/module.map
new file mode 100644
index 0000000..ac68c70
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-1/Inputs/module.map
@@ -0,0 +1,4 @@
+module CounterFramework {
+  header "counter.h"
+  export *
+}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-1/main.swift b/test/multifile/synthesized-accessors/materialize-for-set-1/main.swift
new file mode 100644
index 0000000..7944043
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-1/main.swift
@@ -0,0 +1,17 @@
+// RUN: %empty-directory(%t)
+
+// RUN: mkdir -p %t/onone %t/wmo
+// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -I %S/Inputs/ -module-name=library %S/Inputs/library.swift
+// RUN: %target-build-swift %S/main.swift -I %S/Inputs/ -I %t/onone/ -emit-ir > /dev/null
+
+// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -I %S/Inputs/ -module-name=library -wmo %S/Inputs/library.swift
+// RUN: %target-build-swift %S/main.swift -I %S/Inputs/ -I %t/wmo/ -emit-ir > /dev/null
+
+// REQUIRES: objc_interop
+
+import Foundation
+import library
+
+class CustomCounter : MyCounter {
+  override var value: Int32 { didSet { } }
+}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/counter.h b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/counter.h
new file mode 100644
index 0000000..930dc7b
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/counter.h
@@ -0,0 +1,5 @@
+#import <Foundation/Foundation.h>
+
+@interface Counter : NSObject
+@property(readwrite) int value;
+@end
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library1.swift b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library1.swift
new file mode 100644
index 0000000..7a2041c
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library1.swift
@@ -0,0 +1,8 @@
+import Foundation
+import CounterFramework
+
+public protocol CounterProtocol {
+  var value: Int32 { get set }
+}
+
+extension Counter : CounterProtocol {}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library2.swift b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library2.swift
new file mode 100644
index 0000000..7ac2f8c
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/library2.swift
@@ -0,0 +1,6 @@
+import Foundation
+import CounterFramework
+
+open class MyCounter : Counter {
+  open override var value: Int32 { didSet { } }
+}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/module.map b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/module.map
new file mode 100644
index 0000000..ac68c70
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-2/Inputs/module.map
@@ -0,0 +1,4 @@
+module CounterFramework {
+  header "counter.h"
+  export *
+}
diff --git a/test/multifile/synthesized-accessors/materialize-for-set-2/main.swift b/test/multifile/synthesized-accessors/materialize-for-set-2/main.swift
new file mode 100644
index 0000000..a9d3ddc
--- /dev/null
+++ b/test/multifile/synthesized-accessors/materialize-for-set-2/main.swift
@@ -0,0 +1,17 @@
+// RUN: %empty-directory(%t)
+
+// RUN: mkdir -p %t/onone %t/wmo
+// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -I %S/Inputs/ -module-name=library %S/Inputs/library1.swift %S/Inputs/library2.swift
+// RUN: %target-build-swift %S/main.swift -I %S/Inputs/ -I %t/onone/ -emit-ir > /dev/null
+
+// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -I %S/Inputs/ -module-name=library -wmo %S/Inputs/library1.swift %S/Inputs/library2.swift
+// RUN: %target-build-swift %S/main.swift -I %S/Inputs/ -I %t/wmo/ -emit-ir > /dev/null
+
+// REQUIRES: objc_interop
+
+import Foundation
+import library
+
+class CustomCounter : MyCounter {
+  override var value: Int32 { didSet { } }
+}
diff --git a/test/multifile/synthesized-accessors/one-module-imported/library.swift b/test/multifile/synthesized-accessors/one-module-imported/Inputs/library.swift
similarity index 95%
rename from test/multifile/synthesized-accessors/one-module-imported/library.swift
rename to test/multifile/synthesized-accessors/one-module-imported/Inputs/library.swift
index 5ed3ccc..38b47a0 100644
--- a/test/multifile/synthesized-accessors/one-module-imported/library.swift
+++ b/test/multifile/synthesized-accessors/one-module-imported/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 import CoreGraphics
 
 // Case 1 - witness is imported accessor
diff --git a/test/multifile/synthesized-accessors/one-module-imported/main.swift b/test/multifile/synthesized-accessors/one-module-imported/main.swift
index 237e114..99b7ccf 100644
--- a/test/multifile/synthesized-accessors/one-module-imported/main.swift
+++ b/test/multifile/synthesized-accessors/one-module-imported/main.swift
@@ -1,9 +1,8 @@
 // Try with and without whole module optimization
 
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
-// REQUIRES: executable_test
 // REQUIRES: objc_interop
 
 import CoreGraphics
diff --git a/test/multifile/synthesized-accessors/one-module-internal/library.swift b/test/multifile/synthesized-accessors/one-module-internal/Inputs/library.swift
similarity index 96%
rename from test/multifile/synthesized-accessors/one-module-internal/library.swift
rename to test/multifile/synthesized-accessors/one-module-internal/Inputs/library.swift
index 9a9ab77..b0672e6 100644
--- a/test/multifile/synthesized-accessors/one-module-internal/library.swift
+++ b/test/multifile/synthesized-accessors/one-module-internal/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 struct FishAndChips {
   var costPounds: Float
   var costEuros: Float {
diff --git a/test/multifile/synthesized-accessors/one-module-internal/main.swift b/test/multifile/synthesized-accessors/one-module-internal/main.swift
index d9c16a5..1d54852 100644
--- a/test/multifile/synthesized-accessors/one-module-internal/main.swift
+++ b/test/multifile/synthesized-accessors/one-module-internal/main.swift
@@ -1,9 +1,7 @@
 // Try with and without whole module optimization
 
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
-
-// REQUIRES: executable_test
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
 protocol Takeaway {
   var costPounds: Float { get set }
diff --git a/test/multifile/synthesized-accessors/one-module-public/library.swift b/test/multifile/synthesized-accessors/one-module-public/Inputs/library.swift
similarity index 96%
rename from test/multifile/synthesized-accessors/one-module-public/library.swift
rename to test/multifile/synthesized-accessors/one-module-public/Inputs/library.swift
index c6c187c..094309c 100644
--- a/test/multifile/synthesized-accessors/one-module-public/library.swift
+++ b/test/multifile/synthesized-accessors/one-module-public/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public struct FishAndChips {
   public var costPounds: Float
   public var costEuros: Float {
diff --git a/test/multifile/synthesized-accessors/one-module-public/main.swift b/test/multifile/synthesized-accessors/one-module-public/main.swift
index d9c16a5..1d54852 100644
--- a/test/multifile/synthesized-accessors/one-module-public/main.swift
+++ b/test/multifile/synthesized-accessors/one-module-public/main.swift
@@ -1,9 +1,7 @@
 // Try with and without whole module optimization
 
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
-
-// REQUIRES: executable_test
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
 protocol Takeaway {
   var costPounds: Float { get set }
diff --git a/test/multifile/synthesized-accessors/two-modules-imported/library.swift b/test/multifile/synthesized-accessors/two-modules-imported/Inputs/library.swift
similarity index 92%
rename from test/multifile/synthesized-accessors/two-modules-imported/library.swift
rename to test/multifile/synthesized-accessors/two-modules-imported/Inputs/library.swift
index c362f2d..c5b1255 100644
--- a/test/multifile/synthesized-accessors/two-modules-imported/library.swift
+++ b/test/multifile/synthesized-accessors/two-modules-imported/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 import CoreGraphics
 
 public protocol OtherPoint {
diff --git a/test/multifile/synthesized-accessors/two-modules-imported/main.swift b/test/multifile/synthesized-accessors/two-modules-imported/main.swift
index f6c391f..2167beb 100644
--- a/test/multifile/synthesized-accessors/two-modules-imported/main.swift
+++ b/test/multifile/synthesized-accessors/two-modules-imported/main.swift
@@ -1,9 +1,8 @@
 // Try with and without whole module optimization
 
-// RUN: %target-build-swift %S/library.swift %S/main.swift
-// RUN: %target-build-swift -whole-module-optimization %S/library.swift %S/main.swift
+// RUN: %target-build-swift %S/Inputs/library.swift %S/main.swift
+// RUN: %target-build-swift -whole-module-optimization %S/Inputs/library.swift %S/main.swift
 
-// REQUIRES: executable_test
 // REQUIRES: objc_interop
 
 import CoreGraphics
diff --git a/test/multifile/synthesized-accessors/two-modules/library.swift b/test/multifile/synthesized-accessors/two-modules/Inputs/library.swift
similarity index 97%
rename from test/multifile/synthesized-accessors/two-modules/library.swift
rename to test/multifile/synthesized-accessors/two-modules/Inputs/library.swift
index ebd0748..d1baac1 100644
--- a/test/multifile/synthesized-accessors/two-modules/library.swift
+++ b/test/multifile/synthesized-accessors/two-modules/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 #if _runtime(_ObjC)
 import Foundation
 #endif
diff --git a/test/multifile/synthesized-accessors/two-modules/main.swift b/test/multifile/synthesized-accessors/two-modules/main.swift
index 38cb615..cfe9089 100644
--- a/test/multifile/synthesized-accessors/two-modules/main.swift
+++ b/test/multifile/synthesized-accessors/two-modules/main.swift
@@ -1,14 +1,12 @@
 // RUN: %empty-directory(%t)
 
 // RUN: mkdir -p %t/onone %t/wmo
-// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -module-name=library -emit-library %S/library.swift -o %t/onone/library.%target-dylib-extension
+// RUN: %target-build-swift -emit-module -emit-module-path %t/onone/library.swiftmodule -module-name=library -emit-library %S/Inputs/library.swift -o %t/onone/library.%target-dylib-extension
 // RUN: %target-build-swift %S/main.swift %t/onone/library.%target-dylib-extension -I %t/onone/ -o %t/onone/main
 
-// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -module-name=library -emit-library -O -wmo %S/library.swift -o %t/wmo/library.%target-dylib-extension
+// RUN: %target-build-swift -emit-module -emit-module-path %t/wmo/library.swiftmodule -module-name=library -emit-library -O -wmo %S/Inputs/library.swift -o %t/wmo/library.%target-dylib-extension
 // RUN: %target-build-swift %S/main.swift %t/wmo/library.%target-dylib-extension -I %t/wmo/ -o %t/wmo/main
 
-// REQUIRES: executable_test
-
 import library
 
 protocol Takeaway {
diff --git a/test/multifile/typealias/one-module/library.swift b/test/multifile/typealias/one-module/Inputs/library.swift
similarity index 89%
rename from test/multifile/typealias/one-module/library.swift
rename to test/multifile/typealias/one-module/Inputs/library.swift
index 7785da5..756a569 100644
--- a/test/multifile/typealias/one-module/library.swift
+++ b/test/multifile/typealias/one-module/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public enum Result<T, U>
 {
     case success(T)
diff --git a/test/multifile/typealias/one-module/main.swift b/test/multifile/typealias/one-module/main.swift
index 1adcd71..73ca7b4 100644
--- a/test/multifile/typealias/one-module/main.swift
+++ b/test/multifile/typealias/one-module/main.swift
@@ -1,9 +1,7 @@
 // RUN: %empty-directory(%t)
 
-// RUN: %target-build-swift %S/main.swift %S/library.swift
-// RUN: %target-build-swift -g %S/main.swift %S/library.swift
-
-// REQUIRES: executable_test
+// RUN: %target-build-swift %S/main.swift %S/Inputs/library.swift
+// RUN: %target-build-swift -g %S/main.swift %S/Inputs/library.swift
 
 func testFunction<T>(withCompletion completion: (Result<T, Error>) -> Void) { }
 testFunction { (result: GenericResult<Int>) in }
diff --git a/test/multifile/typealias/one-module/library.swift b/test/multifile/typealias/two-modules/Inputs/library.swift
similarity index 89%
copy from test/multifile/typealias/one-module/library.swift
copy to test/multifile/typealias/two-modules/Inputs/library.swift
index 7785da5..756a569 100644
--- a/test/multifile/typealias/one-module/library.swift
+++ b/test/multifile/typealias/two-modules/Inputs/library.swift
@@ -1,5 +1,3 @@
-// RUN: true
-
 public enum Result<T, U>
 {
     case success(T)
diff --git a/test/multifile/typealias/two-modules/library.swift b/test/multifile/typealias/two-modules/library.swift
deleted file mode 100644
index 7785da5..0000000
--- a/test/multifile/typealias/two-modules/library.swift
+++ /dev/null
@@ -1,9 +0,0 @@
-// RUN: true
-
-public enum Result<T, U>
-{
-    case success(T)
-    case failure(U)
-}
-
-public typealias GenericResult<T> = Result<T, Error>
diff --git a/test/multifile/typealias/two-modules/main.swift b/test/multifile/typealias/two-modules/main.swift
index 5d69887..bcab552 100644
--- a/test/multifile/typealias/two-modules/main.swift
+++ b/test/multifile/typealias/two-modules/main.swift
@@ -1,16 +1,14 @@
 // RUN: %empty-directory(%t)
 
 // RUN: %empty-directory(%t/linker)
-// RUN: %target-build-swift -emit-module -c %S/library.swift -o %t/linker/library.o
-// RUN: %target-build-swift -emit-library -c %S/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -emit-module -c %S/Inputs/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -emit-library -c %S/Inputs/library.swift -o %t/linker/library.o
 // RUN: %target-build-swift %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main
 
-// RUN: %target-build-swift -g -emit-module -c %S/library.swift -o %t/linker/library.o
-// RUN: %target-build-swift -g -emit-library -c %S/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -g -emit-module -c %S/Inputs/library.swift -o %t/linker/library.o
+// RUN: %target-build-swift -g -emit-library -c %S/Inputs/library.swift -o %t/linker/library.o
 // RUN: %target-build-swift -g %S/main.swift %t/linker/library.o -I %t/linker/ -L %t/linker/ -o %t/linker/main
 
-// REQUIRES: executable_test
-
 import library
 
 func testFunction<T>(withCompletion completion: (Result<T, Error>) -> Void) { }
diff --git a/test/stdlib/RangeTraps.swift b/test/stdlib/RangeTraps.swift
index 865f940..51bde98 100644
--- a/test/stdlib/RangeTraps.swift
+++ b/test/stdlib/RangeTraps.swift
@@ -16,8 +16,6 @@
 // RUN: %target-run %t/a.out_Debug
 // RUN: %target-run %t/a.out_Release
 // REQUIRES: executable_test
-// CountablePartialRangeFrom fails in resilient mode. <rdar://problem/31909976>
-// XFAIL: resilient_stdlib
 
 
 import StdlibUnittest
diff --git a/test/stmt/errors.swift b/test/stmt/errors.swift
index 02051ce..688f15e 100644
--- a/test/stmt/errors.swift
+++ b/test/stmt/errors.swift
@@ -2,8 +2,8 @@
 enum MSV : Error {
   case Foo, Bar, Baz
 
-  var domain: String { return "" }
-  var code: Int { return 0 }
+  var _domain: String { return "" }
+  var _code: Int { return 0 }
 }
 
 func a() {}
diff --git a/tools/sil-opt/SILOpt.cpp b/tools/sil-opt/SILOpt.cpp
index eac4a27..845fc1d 100644
--- a/tools/sil-opt/SILOpt.cpp
+++ b/tools/sil-opt/SILOpt.cpp
@@ -40,6 +40,7 @@
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/YAMLTraits.h"
 #include <cstdio>
 using namespace swift;
 
@@ -230,6 +231,11 @@
              "the given regular expression"),
     cl::Hidden);
 
+static cl::opt<std::string>
+    RemarksFilename("save-optimization-record-path",
+                    cl::desc("YAML output filename for pass remarks"),
+                    cl::value_desc("filename"));
+
 static void runCommandLineSelectedPasses(SILModule *Module,
                                          irgen::IRGenModule *IRGenMod) {
   SILPassManager PM(Module, IRGenMod);
@@ -419,6 +425,21 @@
   if (CI.getSILModule())
     CI.getSILModule()->setSerializeSILAction([]{});
 
+  std::unique_ptr<llvm::raw_fd_ostream> OptRecordFile;
+  if (RemarksFilename != "") {
+    std::error_code EC;
+    OptRecordFile = llvm::make_unique<llvm::raw_fd_ostream>(
+        RemarksFilename, EC, llvm::sys::fs::F_None);
+    if (EC) {
+      llvm::errs() << EC.message() << '\n';
+      return 1;
+    }
+    CI.getSILModule()->setOptRecordStream(
+        llvm::make_unique<llvm::yaml::Output>(*OptRecordFile,
+                                              &CI.getSourceMgr()),
+        std::move(OptRecordFile));
+  }
+
   if (OptimizationGroup == OptGroup::Diagnostics) {
     runSILDiagnosticPasses(*CI.getSILModule());
   } else if (OptimizationGroup == OptGroup::Performance) {
diff --git a/utils/build-script-impl b/utils/build-script-impl
index 70f214c..ff0bbb2 100755
--- a/utils/build-script-impl
+++ b/utils/build-script-impl
@@ -3444,8 +3444,8 @@
           with_pushd "${host_install_destdir}" \
               call tar -c -z -f "${package_for_host}" "${TOOLCHAIN_PREFIX/#\/}"
         else
-            # tar on OS X doesn't support --owner/--group.
-            if [[ "$(uname -s)" == "Darwin" ]] ; then
+            # BSD tar doesn't support --owner/--group.
+            if [[ "$(uname -s)" == "Darwin" || "$(uname -s)" == "FreeBSD" ]] ; then
                 with_pushd "${host_install_destdir}" \
                     tar -c -z -f "${package_for_host}" "${host_install_prefix/#\/}"
             else
diff --git a/utils/vim/syntax/sil.vim b/utils/vim/syntax/sil.vim
index b54e434..c5553e3 100644
--- a/utils/vim/syntax/sil.vim
+++ b/utils/vim/syntax/sil.vim
@@ -5,6 +5,33 @@
     finish
 endif
 
+syn keyword silStage skipwhite nextgroup=silStages
+      \ sil_stage
+syn keyword silStages
+      \ canonical
+      \ raw
+
+syn match silIdentifier skipwhite
+      \ /@\<[A-Za-z_0-9]\+\>/
+
+syn match silConvention skipwhite
+      \ /$\?@convention/
+syn region silConvention contained contains=silConventions
+      \ start="@convention(" end=")"
+syn keyword silConventions
+      \ block
+      \ c
+      \ method
+      \ objc_method
+      \ thick
+      \ thin
+      \ witness_method
+
+syn match silFunctionType skipwhite
+      \ /@\(\<autoreleased\>\|\<callee_guaranteed\>\|\<callee_owned\>\|\<error\>\|\<guaranteed\>\|\<in\>\|\<in_constant\>\|\<in_guaranteed\>\|\<inout\>\|\<inout_aliasable\>\|\<noescape\>\|\<out\>\|\<owned\>\)/
+syn match silMetatypeType skipwhite
+      \ /@\(\<thick\>\|\<thin\>\|\<objc\>\)/
+
 syn keyword swiftImport import skipwhite nextgroup=swiftImportModule
 syn match swiftImportModule /\<[A-Za-z_][A-Za-z_0-9]*\>/ contained nextgroup=swiftImportComponent
 syn match swiftImportComponent /\.\<[A-Za-z_][A-Za-z_0-9]*\>/ contained nextgroup=swiftImportComponent
@@ -67,7 +94,7 @@
 hi def link swiftImport Include
 hi def link swiftImportModule Title
 hi def link swiftImportComponent Identifier
-hi def link swiftApplyKeyword ModeMsg
+hi def link swiftApplyKeyword Statement
 hi def link swiftKeyword Statement
 hi def link swiftTypeDefinition Define
 hi def link swiftTypeName Type
@@ -97,4 +124,13 @@
 hi def link swiftLabel Label
 hi def link swiftNew Operator
 
+hi def link silStage Special
+hi def link silStages Type
+hi def link silConvention Special
+hi def link silConventionParameter Special
+hi def link silConventions Type
+hi def link silIdentifier Identifier
+hi def link silFunctionType Special
+hi def link silMetatypeType Special
+
 let b:current_syntax = "sil"
diff --git a/validation-test/Sema/type_checker_crashers_fixed/rdar28048391.swift b/validation-test/Sema/type_checker_crashers_fixed/rdar28048391.swift
index 5dc135f..224a6df 100644
--- a/validation-test/Sema/type_checker_crashers_fixed/rdar28048391.swift
+++ b/validation-test/Sema/type_checker_crashers_fixed/rdar28048391.swift
@@ -12,3 +12,4 @@
 }
 
 extension ImplicitlyUnwrappedOptional : rdar28048391 { }
+// expected-warning@-1 {{the spelling 'ImplicitlyUnwrappedOptional' is deprecated}}
diff --git a/validation-test/Sema/type_checker_perf/slow/rdar35213699.swift b/validation-test/Sema/type_checker_perf/slow/rdar35213699.swift
new file mode 100644
index 0000000..3f40370
--- /dev/null
+++ b/validation-test/Sema/type_checker_perf/slow/rdar35213699.swift
@@ -0,0 +1,8 @@
+// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=1
+// REQUIRES: tools-release,no_asserts
+
+func test() {
+  let x: UInt = 1 * 2 + 3 * 4 + 5 * 6 + 7 * 8 + 9 * 10 + 11 * 12 + 13 * 14
+  // expected-error@-1 {{expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions}}
+}
+
diff --git a/validation-test/compiler_crashers_2_fixed/0075-rdar30248571.swift b/validation-test/compiler_crashers_2_fixed/0075-rdar30248571.swift
index e0d33be..967d912 100644
--- a/validation-test/compiler_crashers_2_fixed/0075-rdar30248571.swift
+++ b/validation-test/compiler_crashers_2_fixed/0075-rdar30248571.swift
@@ -1,5 +1,5 @@
 // RUN: %target-swift-frontend %s -typecheck
-// FIXME: %target-swift-frontend %s -emit-ir -- see https://github.com/apple/swift/pull/7414
+// RUN: %target-swift-frontend %s -emit-ir -o /dev/null
 
 protocol P {
   associatedtype A
diff --git a/validation-test/compiler_crashers_2_fixed/0129-rdar35019075.swift b/validation-test/compiler_crashers_2_fixed/0129-rdar35019075.swift
new file mode 100644
index 0000000..ae35eef
--- /dev/null
+++ b/validation-test/compiler_crashers_2_fixed/0129-rdar35019075.swift
@@ -0,0 +1,43 @@
+// RUN: not %target-swift-frontend %s -typecheck
+
+enum SortOrder {
+    case ascending
+    case equal
+    case descending
+}
+
+struct SortedArray<Element> {
+    var contents = [Element]()
+    let compare: (Element, Element) -> SortOrder
+
+    init(_ comparator: @escaping (Element, Element) -> SortOrder) {
+        compare = comparator
+    }
+
+    mutating func add(_ element: Element) {
+
+    }
+}
+
+extension SortedArray where Element: Comparable {
+    init() {
+        compare = { a, b in
+            if a < b { return .ascending }
+            else if a > b { return .descending }
+            else { return .equal }
+        }
+    }
+
+    init<S: Sequence>(_ sequence: S) where S.Iterator.Element == Element {
+        self.init()
+
+        for element in sequence {
+            add(element)
+        }
+    }
+}
+
+extension SortedArray: Sequence {
+    typealias Iterator = IndexingIterator
+
+}
diff --git a/validation-test/compiler_crashers/28803-swift-typebase-getdesugaredtype.swift b/validation-test/compiler_crashers_fixed/28803-swift-typebase-getdesugaredtype.swift
similarity index 87%
rename from validation-test/compiler_crashers/28803-swift-typebase-getdesugaredtype.swift
rename to validation-test/compiler_crashers_fixed/28803-swift-typebase-getdesugaredtype.swift
index 4511df4..4ae3193 100644
--- a/validation-test/compiler_crashers/28803-swift-typebase-getdesugaredtype.swift
+++ b/validation-test/compiler_crashers_fixed/28803-swift-typebase-getdesugaredtype.swift
@@ -5,6 +5,6 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol P{typealias a:Collection
 typealias a=(t:Self.a
diff --git a/validation-test/compiler_crashers/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift b/validation-test/compiler_crashers_fixed/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
similarity index 87%
rename from validation-test/compiler_crashers/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
rename to validation-test/compiler_crashers_fixed/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
index 2b015fa..9d62d8f 100644
--- a/validation-test/compiler_crashers/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
+++ b/validation-test/compiler_crashers_fixed/28819-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
@@ -5,5 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 class a<a{protocol b}extension a.b
diff --git a/validation-test/compiler_crashers/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift b/validation-test/compiler_crashers_fixed/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
similarity index 87%
rename from validation-test/compiler_crashers/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
rename to validation-test/compiler_crashers_fixed/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
index aad1964..a9fbd25 100644
--- a/validation-test/compiler_crashers/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
+++ b/validation-test/compiler_crashers_fixed/28830-formextensioninterfacetype-swift-type-swift-genericparamlist.swift
@@ -5,5 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol P{protocol a:P}extension P.a.a
diff --git a/validation-test/compiler_crashers/28846-swift-typebase-getdesugaredtype.swift b/validation-test/compiler_crashers_fixed/28846-swift-typebase-getdesugaredtype.swift
similarity index 87%
rename from validation-test/compiler_crashers/28846-swift-typebase-getdesugaredtype.swift
rename to validation-test/compiler_crashers_fixed/28846-swift-typebase-getdesugaredtype.swift
index 56eb011..c70c62f 100644
--- a/validation-test/compiler_crashers/28846-swift-typebase-getdesugaredtype.swift
+++ b/validation-test/compiler_crashers_fixed/28846-swift-typebase-getdesugaredtype.swift
@@ -5,5 +5,5 @@
 // See https://swift.org/LICENSE.txt for license information
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol A{typealias a:A=a{}typealias a=Self.a
diff --git a/validation-test/compiler_crashers/28848-iscanonicaltypeincontext-result-builder.swift b/validation-test/compiler_crashers_fixed/28848-iscanonicaltypeincontext-result-builder.swift
similarity index 88%
rename from validation-test/compiler_crashers/28848-iscanonicaltypeincontext-result-builder.swift
rename to validation-test/compiler_crashers_fixed/28848-iscanonicaltypeincontext-result-builder.swift
index 72c53f2..504f8d5 100644
--- a/validation-test/compiler_crashers/28848-iscanonicaltypeincontext-result-builder.swift
+++ b/validation-test/compiler_crashers_fixed/28848-iscanonicaltypeincontext-result-builder.swift
@@ -6,5 +6,5 @@
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
 // REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol P{typealias a{}func a:a.a{}class a{{}protocol 0
diff --git a/validation-test/compiler_crashers/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift b/validation-test/compiler_crashers_fixed/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift
similarity index 88%
rename from validation-test/compiler_crashers/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift
rename to validation-test/compiler_crashers_fixed/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift
index 0e26155..350c824 100644
--- a/validation-test/compiler_crashers/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift
+++ b/validation-test/compiler_crashers_fixed/28851-hasconformanceinsignature-inprotocol-getrequirementsignature-subjecttype-conform.swift
@@ -6,5 +6,5 @@
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
 // REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol A:a{{}typealias a=a{}func a{}typealias a}class a:A{func a
diff --git a/validation-test/compiler_crashers/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift b/validation-test/compiler_crashers_fixed/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift
similarity index 88%
rename from validation-test/compiler_crashers/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift
rename to validation-test/compiler_crashers_fixed/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift
index ea97cf1..40f673d 100644
--- a/validation-test/compiler_crashers/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift
+++ b/validation-test/compiler_crashers_fixed/28864-bool-typesig-bool-extensionsig-unexpected-generic-ness-mismatch-on-conformance.swift
@@ -6,6 +6,6 @@
 // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
 
 // REQUIRES: asserts
-// RUN: not --crash %target-swift-frontend %s -emit-ir
+// RUN: not %target-swift-frontend %s -emit-ir
 protocol P{class a}protocol A:P
 extension P.a:A